Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .ai/contexts/session-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ population, with at most two fresh folder attempts; unrelated folders are not
rescanned. Retry snapshot failures remain scoped to their folder so later
folders can publish. Shutdown prevents new retry workers and terminates admitted
retry workers through the same registered scan-worker set. Population has no
wall-clock deadline. Explicit remote/subset scans
wall-clock deadline. Folder writer commands use payload-scaled deadlines;
a retry preserves durably certified matching rows from an interrupted
publication in the same writer lifetime (see session-index-writer.md, #1135).
The retry still re-enumerates and validates sources; it does not claim to skip
transcript parsing on a forced/cold retry. Explicit remote/subset scans
retain the ten-minute ceiling. Population returns `{ok,...}`; initial completion,
Projects reconciliation time and continuation-choice sweeping require success.

Expand Down
59 changes: 57 additions & 2 deletions .ai/contexts/session-index-writer.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,9 @@ continuity across chunks. An unrelated write between chunks breaks Projects
receipt continuity without refusing token/witness-valid certificates.

The commands bracket producer operations at the facade. They do not alter
the generic chunk scheduler, producer-unit packing, transaction budgets,
admission or deadlines (#1135). Certificate commands participate in existing
the generic chunk scheduler, producer-unit packing, transaction budgets or
admission. The payload deadlines below apply to every `effects` command,
including refresh, project-worker and remote producers (#1135). Certificate commands participate in existing
project-deletion ownership fencing. Repair invalidates its folder seal before
scanning and joins startup population; its marker clears only after successful
sealed publication.
Expand Down Expand Up @@ -766,3 +767,57 @@ The obsolete single-row worker `certifyFullRead` command is removed; certificate
units invoke the store method directly inside `certifyFullReads`. The real
worker facade test uses 80 certificates and an intervening setting write to
prove `continuous:false` returns the body ACK.

## Large-folder deadlines and interrupted publication (#1135)

The transport scales the started-command deadline for every `effects` command,
full-read certification and batched name derivation. The multiplier is the larger
of one, the estimated payload's 32 KiB blocks, and its 32 work-row blocks.
Bulk session, search, entrypoint and title effects count their immediate rows;
other effects count once. Certification and name derivation count their candidates.
Nested metrics are not counted again as work rows in both session upserts and
metric replacement; their payload size still contributes to the byte estimate.
A single iterative walk estimates bytes from string/key lengths (two bytes per
UTF-16 code unit), structural delimiters and fixed-size scalar allowances,
without serializing a copy. A WeakSet visits shared/cyclic objects once; BigInt
uses the scalar allowance. An unreadable payload falls back to the maximum.
Each block receives the existing 30-second allowance, capped by
`MAX_WORK_COMMAND_TIMEOUT_MS` at ten minutes, matching the subset scan ceiling.
Small/control commands, bootstrap, heartbeat and admission retain
their existing budgets. This bounds a command by the work carried in its
payload without changing the worker's transaction packing, yields, fences
or scheduling. The work-row count also covers many small rows; estimated bytes
cover large text/metrics/witness payloads. This is a finite
work allowance, not a guarantee under arbitrary CPU starvation.

After a startup folder's body ACK, the facade retains its existing pending
token, configuration and proposed per-row certificates for a retry in this
writer lifetime. It does not treat this memory as committed progress: on
retry, it rereads the durable pending token and each row certificate/witness,
and compares them with the newly scanned source candidate and configuration.
Only matching, durably certified rows bypass replacement. A full retry uses
the existing partial writer to preserve those rows, deletes vanished inventory
rows, and still seals as a full scan only after the remaining writes and
certificates. Multiple failed retries retain the preserved candidates.
A superseded token, changed configuration/source/row or absent certificate
requires republication. An unknown body outcome has no new certified progress
and cannot authorize skipping its rows. No receipt table or parallel durable
progress mechanism is introduced. After process restart the provenance hint
is gone and A2 conservatively performs full validation again.

The scan coordinator still allows only three delayed retries (30 s, 120 s,
600 s), then retains its error with the retry time at Infinity until an
explicit forced scan or a later startup. Unknown certificate outcomes leave
the folder pending/unsealed and initial completion false on a cold failure.
Seal remains last; an unknown seal outcome is logged without compensation.
A2's successfully published deferred sources still count as complete for
population while their folder remains pending.

The regression uses real disposable SQLite, the production scan worker,
facade and writer, with a virtual slow transport to exercise command expiry.
The interrupted certificate test commits a prefix of single-row chunks,
then loses the next outcome and verifies that retry preserves certified rows.
Retry regressions also remove a transcript, edit a durable title without
clearing its certificate, and change only source ctime while retaining identical
parsed row witnesses. Each compares the resulting index with a separate full
rebuild; the source-only case isolates the source-stamp reuse guard.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ What changes for you in each release of Switchboard. How to write an entry: [doc
- Codex sessions run non-interactively by a program (`codex exec`: scripted reviews and fixes) are now left out of the sidebar and grid like Claude Agent SDK sessions, under the same **Hide SDK-launched Sessions** setting. They still count in the heatmap and are found by search; the ones you have open stay listed. Codex sessions already indexed are re-checked once at the next start. (#1145)

### Fixed
- Indexing a very large project now finishes on a busy machine. (#1135)
- After restarting the app or Windows, the sidebar shows the projects as they were at exit, even if the last project load finished just before you closed the app or signed out. (#1136)
- On Windows, a slow start no longer leaves tabs without process cleanup: tabs already open, restored ones included, are covered once cleanup starts, and programs they launch afterwards end when the tab closes. Programs launched before that can keep running. (#1140, #1156)
- Claude and Codex sessions in `.work-files/worktrees/<name>`, and Codex sessions in `.worktrees`, `.claude-worktrees` and `.claude/worktrees`, appear under their existing repository, including nested worktrees and sessions already indexed. Repository settings, archive and hidden state follow the group, and project deletion removes the session metadata it previews for both runtimes. Archiving a repository covers its worktrees' sessions; a new session in a worktree reopens the repository. Resume and fork use the recorded worktree directory and refuse to launch if it no longer exists; Claude sessions in a deleted `.claude/worktrees` directory are refused instead of starting in the repository. (#1137)
Expand Down
38 changes: 37 additions & 1 deletion session-index-writer-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
'use strict';

const MAX_WORK_COMMAND_TIMEOUT_MS = 10 * 60 * 1000;

// see .ai/contexts/session-index-writer.md
function commandTimeoutMs(command, args, timeoutMs) {
if (!['effects', 'certifyFullReads', 'deriveCachedNames'].includes(command)) return timeoutMs;
try {
let rows = 0, bytes = 0;
if (command === 'effects') {
for (const effect of args[0]) {
rows += ['upsertCachedSessions', 'upsertSearchEntries', 'setCachedEntrypoints', 'setCachedCustomTitles'].includes(effect.name)
? effect.args[0].length : 1;
}
} else rows = args[command === 'certifyFullReads' ? 2 : 0].length;
const pending = [args], seen = new WeakSet();
while (pending.length) {
const value = pending.pop();
if (typeof value === 'string') bytes += value.length * 2 + 2;
else if (value && typeof value === 'object') {
if (seen.has(value)) continue;
seen.add(value);
bytes += 2;
if (Array.isArray(value)) {
bytes += value.length;
for (const child of value) pending.push(child);
} else for (const key of Object.keys(value)) {
bytes += key.length * 2 + 3;
pending.push(value[key]);
}
} else bytes += 8;
}
const batches = Math.max(1, Math.ceil(rows / 32), Math.ceil(bytes / 32768));
return Math.min(MAX_WORK_COMMAND_TIMEOUT_MS, timeoutMs * batches);
} catch {
return MAX_WORK_COMMAND_TIMEOUT_MS;
}
}

function createWriterWorkerClient({ workerFactory, onSettings = () => {}, onBusyCounts = () => {}, onBootstrap = () => {}, timeoutMs = 30000, maxRestarts = 3,
now = () => Date.now(), setTimeout = global.setTimeout, clearTimeout = global.clearTimeout, heartbeatMs = 30000, log = console }) {
let worker, closing, stopped = false, terminated = false, nextId = 0, restartTimer;
Expand Down Expand Up @@ -130,7 +166,7 @@ function createWriterWorkerClient({ workerFactory, onSettings = () => {}, onBusy
const job = active.get(message.id);
if (!job) return;
if (message.started) {
job.timer ||= setTimeout(() => settle(job, Object.assign(new Error(`writer command timed out for request ${job.id}; outcome unknown`), { code: 'WRITER_TIMEOUT', requestId: job.id })), timeoutMs);
job.timer ||= setTimeout(() => settle(job, Object.assign(new Error(`writer command timed out for request ${job.id}; outcome unknown`), { code: 'WRITER_TIMEOUT', requestId: job.id })), commandTimeoutMs(job.command, job.args, timeoutMs));
return;
}
active.delete(message.id);
Expand Down
30 changes: 27 additions & 3 deletions session-index-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function createSessionIndexWriter(options = {}, cache = require('./session-cache
};
let backend, recorder, projectsDir, cacheTail = Promise.resolve(), closing, accepting = true, terminated = false;
const pending = new Set();
const scanResumes = new Map();
const commandScope = new AsyncLocalStorage();
const owner = {};
const admitted = () => !terminated && (accepting || commandScope.getStore() === owner);
Expand Down Expand Up @@ -119,7 +120,24 @@ function createSessionIndexWriter(options = {}, cache = require('./session-cache
let startupFence = scanResult?.startup ? { folder: scanFolder, sessionIds, witness: folderReadWitness(backend, scanFolder, sessionIds) } : null;
if (name === 'applyProjectsWorkerEffects' && backend.getProjectsRevision() !== sourceVersion) throw Object.assign(new Error('Projects effect snapshot changed while queued'), { code: 'WRITER_STALE' });
const continuationCollector = { deferred: new Set(), accounted: new Set() };
let computeArgs = args;
let computeArgs = args, computeName = name;
const resumed = new Map();
const previous = scanResult?.startup && !scanResult.unverified && scanResumes.get(scanFolder);
if (previous && previous.config === scanResult.fullIndexConfig && previous.config === currentConfig()
&& backend.getFolderMeta(scanFolder)?.fullIndexPending === previous.token) {
for (const row of scanResult.sessions) {
const candidate = previous.rows.get(row.sessionId);
const durable = backend.getCachedSession(row.sessionId);
if (candidate && row.fullReadStamp === candidate.stamp && rowWitness(row) === candidate.witness
&& durable?.fullReadStamp === candidate.stamp && rowWitness(durable) === candidate.witness) resumed.set(row.sessionId, candidate);
}
if (resumed.size) {
const present = new Set(scanResult.sessions.map(row => row.sessionId));
computeName = 'writeScannedFolderPartial';
computeArgs = [{ ...scanResult, sessions: scanResult.sessions.filter(row => !resumed.has(row.sessionId)),
toDelete: scanResult.partial ? scanResult.toDelete : scanResult.inventory.rows.filter(row => !present.has(row.sessionId)).map(row => row.sessionId) }];
}
}
if (folder !== undefined && cache.prepareContinuationRefresh) {
const opts = name === 'refreshFolder' ? args[1] : {};
const preparedContinuations = await cache.prepareContinuationRefresh(folder, opts);
Expand All @@ -129,7 +147,8 @@ function createSessionIndexWriter(options = {}, cache = require('./session-cache
recorder.begin();
let result, revision, effects;
try {
result = cache[name](...computeArgs);
result = cache[computeName](...computeArgs);
if (resumed.size) result += resumed.size;
revision = recorder.revision();
} finally {
effects = recorder.finish();
Expand Down Expand Up @@ -200,6 +219,8 @@ function createSessionIndexWriter(options = {}, cache = require('./session-cache
for (const [key, publication] of publications) {
if (!publication?.began || publication.failed) continue;
const rows = certificates.filter(row => row.folder === key);
if (scanResult?.startup) scanResumes.set(key, { token: publication.token, config: publication.config,
rows: new Map([...resumed, ...rows.map(row => [row.sessionId, row])]) });
if (!rows.length) continue;
try {
const certificateAck = await send('certifyFullReads', key, publication.token, rows, { receipt: forwarded });
Expand Down Expand Up @@ -229,7 +250,10 @@ function createSessionIndexWriter(options = {}, cache = require('./session-cache
if (forwarded) {
if (acknowledgment?.committedRevision !== undefined && sealed.beforeRevision !== acknowledgment.committedRevision) continuous = false;
acknowledgment = sealed;
} else if (scanResult) scanResult.fullIndexOutcome = sealed;
} else if (scanResult) {
scanResult.fullIndexOutcome = sealed;
if (sealed) scanResumes.delete(key);
}
}
} catch (error) { (options.log || console).warn('[session-index-writer] full index seal outcome unknown: ' + error.message); }
}
Expand Down
Loading
Loading