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
13 changes: 12 additions & 1 deletion src/backend/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export class EventStreamListener {
const isTimeout = resp.error.message === "timeout";
if (!isTimeout || attempt === MAX_ATTEMPTS) {
if (isTimeout) {
warn(`subscribe: all ${MAX_ATTEMPTS} attempts timed out (backend unresponsive for ~${Math.round((MAX_ATTEMPTS * 5000 + 500) / 1000)}s)`);
warn(
`subscribe: all ${MAX_ATTEMPTS} attempts timed out (backend unresponsive for ~${Math.round((MAX_ATTEMPTS * 5000 + 500) / 1000)}s)`,
);
}
throw new Error(formatSubscribeError(resp));
}
Expand Down Expand Up @@ -126,6 +128,15 @@ export class EventStreamListener {
}
}

/**
* True if events are queued waiting for a poll (non-destructive). Lets the
* turn loop check liveness without consuming an event — used by the stall
* reconciliation to confirm a turn is still alive before ending it.
*/
hasQueuedEvents(): boolean {
return this.queue.length > 0;
}

/**
* Wait for the next event, resolving once one arrives or `timeoutMs` elapses
* (resolves null on timeout). Events arriving with no active waiter are
Expand Down
53 changes: 27 additions & 26 deletions src/config/auto-compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,36 @@ export async function maybeAutoCompact(
const threshold = autoCompactThreshold();
if (threshold <= 0) return; // disabled

// Read current context usage via session/read.
let used = 0;
const msgId = randomUUID();
try {
const backend = server.ensureBackend();
const resp = await backend.request(
server.nextId(),
"session/read",
{ sessionId: zcodeSid },
5000,
);
if (resp.error) return;
const result = (resp.result ?? {}) as { projection?: { contextUsed?: number } };
used = result.projection?.contextUsed ?? 0;
} catch (e) {
warn(`auto-compact: session/read failed (${e instanceof Error ? e.message : String(e)})`);
return;
}
// Read current context usage via session/read.
let used = 0;
try {
const backend = server.ensureBackend();
const resp = await backend.request(
server.nextId(),
"session/read",
{ sessionId: zcodeSid },
5000,
);
if (resp.error) return;
const result = (resp.result ?? {}) as { projection?: { contextUsed?: number } };
used = result.projection?.contextUsed ?? 0;
} catch (e) {
warn(`auto-compact: session/read failed (${e instanceof Error ? e.message : String(e)})`);
return;
}

if (used < threshold) return;
if (used < threshold) return;

log(`auto-compact: contextUsed=${used} >= threshold=${threshold}, compacting…`);
await sendTextChunk(
cx,
acpSid,
`🔄 auto-compact: context usage ${used.toLocaleString()} ≥ threshold ${threshold.toLocaleString()}, compressing…`,
msgId,
);

log(`auto-compact: contextUsed=${used} >= threshold=${threshold}, compacting…`);
const msgId = randomUUID();
await sendTextChunk(
cx,
acpSid,
`🔄 auto-compact: context usage ${used.toLocaleString()} ≥ threshold ${threshold.toLocaleString()}, compressing…`,
msgId,
);
try {
// compact() handles: session/compact → waitForTurnIdle → emitInitialUsage.
const result = (await compact(server, { sessionId: acpSid }, cx)) as {
__lockTimeout?: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/handlers/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ async function dispatchUsageDelta(
// config.json limit.context so the editor can render the context bar.
let size = ev.size;
if (!size) {
const { providerId, modelId } = parseModelValue(await currentModelCached(server, acpSid));
// Resolve to the real backend session id — `acpSid` may be a lazy
// session/new placeholder that the backend rejects with "Session is not
// active", wasting a 5s request timeout on every usage_update.
const zcodeSid = server.resolveSid(acpSid) ?? acpSid;
const { providerId, modelId } = parseModelValue(await currentModelCached(server, zcodeSid));
size = modelContextWindow(providerId, modelId);
}
await sendSessionUpdate(cx, acpSid, {
Expand Down
Loading
Loading