Skip to content

Commit ae1384c

Browse files
author
mp
committed
fix(tasks): keep OpenCode subagent polls off the event loop and hold a turn its session was found late for
The merge pass's gate found three things in #1987. A poll ran on the event loop and could wait there: on macOS each one ran ps for the OpenCode service's start, and a store held by its writer kept the read waiting its 250 ms busy timeout. The service's start is now looked up once per service process, without waiting (processStartUpperBoundAsync), and a poll's read waits on no writer: a store busy for a moment leaves the counts as they were. A child that still ran behind 1024 newer ended ones was neither counted nor listed, as the cap applied before children were classified. Every child whose run has not ended with an idle row is now read; only the ended ones, read for the list, stay capped. An OpenCode turn that followed another agent and ended on its subagent before its session was found read DONE: the first session's reset dropped the rest the turn was seen take, and so did Claude Code's tracker letting go. Such a reset now keeps the rests seen since the pane's first status event under its new agent (BackgroundWait.resetBefore). A status event of an OpenCode pane whose session is not known yet looks the pane up at once, so the turn is held while its finish alert still waits, and the alert is called off.
1 parent c706a7a commit ae1384c

6 files changed

Lines changed: 109 additions & 33 deletions

File tree

‎server/background-wait.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,31 @@ describe("BackgroundWait", () => {
207207
expect(waits.waiting("p1")).toBe(true);
208208
});
209209

210+
it("keeps, past another agent's lifetime, only the rests seen since the agent that followed began", () => {
211+
const time = clock();
212+
const waits = new BackgroundWait(time.now);
213+
// one agent's turn rests on its work
214+
waits.status("p", "working");
215+
waits.running("p", 1, null);
216+
waits.status("p", "done");
217+
expect(waits.waiting("p")).toBe(true);
218+
time.pass(1000);
219+
const since = time.now();
220+
// the next agent's turn rests before its tracker knows its session
221+
waits.status("p", "working");
222+
time.pass(1000);
223+
waits.status("p", "idle");
224+
waits.resetBefore("p", since);
225+
expect(waits.waiting("p")).toBe(false);
226+
// its tracker counts what that turn started: the turn holds
227+
waits.running("p", 1, since);
228+
expect(waits.waiting("p")).toBe(true);
229+
// a reset keeps no rest
230+
waits.reset("p");
231+
waits.running("p", 1, since);
232+
expect(waits.waiting("p")).toBe(false);
233+
});
234+
210235
it("forgets a pane", () => {
211236
const waits = new BackgroundWait(clock().now);
212237
waits.running("p1", 1);

‎server/background-wait.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ export class BackgroundWait {
101101
pane.waiting = false;
102102
}
103103

104+
/**
105+
* reset, keeping the rests observed from `since` on: another agent's lifetime ended there, and the one that
106+
* followed was seen rest before its tracker knew its session. Its counts come afresh from that tracker.
107+
*/
108+
resetBefore(paneId: string, since: number): void {
109+
const pane = this.panes.get(paneId);
110+
if (!pane) return;
111+
const rests = pane.rests.filter((at) => at >= since);
112+
this.reset(paneId);
113+
pane.rests = rests;
114+
}
115+
104116
forget(paneId: string): void {
105117
this.panes.delete(paneId);
106118
}

‎server/core/status.ts‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { releaseOpenCodePane, trackVerifiedOpenCodePanes } from "../opencode-nat
1616
import { opencodeDatabasePath } from "../opencode.ts";
1717
import { OpencodeSubagentStatus } from "../opencode-subagents.ts";
1818
import { openCodeServiceOptions, readOpenCodeService } from "../opencode-service.ts";
19-
import { processStartedAt } from "../process-start.ts";
19+
import { processStartUpperBoundAsync } from "../process-start.ts";
2020
import type { DeviceStore } from "../devices.ts";
2121
import { createGjcStatus, gjcPanes, readOmoSnapshot, reconcileProviderStatus } from "../gjc-status.ts";
2222
import type { HerdrMachineProfile } from "../herdr-profiles.ts";
@@ -113,15 +113,29 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
113113
let settling: string | null = null;
114114
/** each pane's agent as the newest snapshot or status event has it, a snapshot's set before either subagent tracker reads it (owns) */
115115
const agentNow = new Map<string, string | null>();
116+
/** each pane's agent as its status events have it, and since when: the rests from then on are that agent's (newLifetime) */
117+
const eventAgent = new Map<string, { agent: string | null; since: number }>();
118+
const statusNow = options.statusNow ?? Date.now;
119+
/**
120+
* A new lifetime in the pane: what came before is not its own. `keep`: the agent whose rests from its first status
121+
* event on stay, as it was seen rest before its tracker knew its session; null keeps none.
122+
*/
123+
function newLifetime(paneId: string, keep: string | null): void {
124+
const held = waits.waiting(paneId);
125+
const seen = eventAgent.get(paneId);
126+
if (keep !== null && seen?.agent === keep) waits.resetBefore(paneId, seen.since); else waits.reset(paneId);
127+
claudeAgentsChanged(paneId, 0, 0, null, held);
128+
}
129+
/** OpenCode's service and when it started, looked up once per service process, off the event loop */
130+
let service: { pid: number; started: number | null } | null = null;
116131
/** Claude panes count the subagents and commands they run as background tasks, read from the session's own files (server/claude-subagents.ts) */
117132
const claudeAgents = new ClaudeSubagentStatus({
118133
resolve: claudePaneSession,
119134
pid: claudePanePid,
135+
// a pane that left Claude Code for another agent keeps what that agent was seen do there since
120136
onReset: (paneId) => {
121-
if (!owns(paneId, "claude")) return;
122-
const held = waits.waiting(paneId);
123-
waits.reset(paneId);
124-
claudeAgentsChanged(paneId, 0, 0, null, held);
137+
const agent = agentNow.get(paneId) ?? null;
138+
if (owns(paneId, "claude")) newLifetime(paneId, agent === "claude" ? null : agent);
125139
},
126140
onChange: (paneId, running, turnRunning, promptAt) => { if (owns(paneId, "claude")) claudeAgentsChanged(paneId, running, turnRunning, promptAt); },
127141
});
@@ -130,15 +144,16 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
130144
store: () => options.opencodeDb ?? opencodeDatabasePath(),
131145
// the runs are its service's: one that started since took what ran before it
132146
serviceStart: () => {
133-
const service = readOpenCodeService(options.openCodeStateDir ?? openCodeServiceOptions.stateDir);
134-
return service === null ? null : processStartedAt(service.pid);
135-
},
136-
onReset: (paneId) => {
137-
if (!owns(paneId, "opencode")) return;
138-
const held = waits.waiting(paneId);
139-
waits.reset(paneId);
140-
claudeAgentsChanged(paneId, 0, 0, null, held);
147+
const found = readOpenCodeService(options.openCodeStateDir ?? openCodeServiceOptions.stateDir);
148+
if (found === null) return null;
149+
if (service?.pid !== found.pid) {
150+
// not known until the lookup answers: meanwhile no run is taken for one the service before took along
151+
const looked: { pid: number; started: number | null } = service = { pid: found.pid, started: null };
152+
void processStartUpperBoundAsync(found.pid).then((at) => { looked.started = at; }, () => undefined);
153+
}
154+
return service.started;
141155
},
156+
onReset: (paneId, first) => { if (owns(paneId, "opencode")) newLifetime(paneId, first ? "opencode" : null); },
142157
onChange: (paneId, running, turnRunning, promptAt) => { if (owns(paneId, "opencode")) claudeAgentsChanged(paneId, running, turnRunning, promptAt); },
143158
});
144159
/**
@@ -171,6 +186,7 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
171186
const named = gjc.apply(omo.apply(snapshot));
172187
agentNow.clear();
173188
for (const pane of named.panes) agentNow.set(pane.pane_id, pane.agent ?? null);
189+
for (const paneId of eventAgent.keys()) if (!agentNow.has(paneId)) eventAgent.delete(paneId);
174190
// not waited for: finding a transcript costs process calls, and a snapshot must not wait on them.
175191
// The plans come after: a Claude pane's transcript is the one the subagent tracker found
176192
void claudeAgents.refresh(named.panes).catch(() => undefined).then(() => plans.refresh(named.panes)).catch(() => undefined);
@@ -322,6 +338,7 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
322338
if (machines) machines.localAgents(named); else bridgeAgents!.status(named);
323339
// newer than the last snapshot: a route that looks the pane's session up before the next one reads it so
324340
agentNow.set(paneId, named[0]!.agent ?? null);
341+
if (eventAgent.get(paneId)?.agent !== (named[0]!.agent ?? null)) eventAgent.set(paneId, { agent: named[0]!.agent ?? null, since: statusNow() });
325342
// herdr says `claude/idle` for an OmO pane whatever it does, and a provider child's work for
326343
// a GJC one while its transcript is tracked: their own status stands, and no Claude turn waits there
327344
// (an OmO pane's wait on its background tasks is omoChanged's)
@@ -351,6 +368,11 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
351368
} else waits.forget(paneId);
352369
broadcastAll(paneStatus(paneId, status, tracker !== null ? { background_tasks: tracker.countOf(paneId) } : {}));
353370
push.onStatus(paneId, alertStatus(status, waits.waiting(paneId))).catch(logPushError);
371+
// an OpenCode turn whose session is not known yet: its pane is looked up now, not at the next snapshot, so a
372+
// turn that ended on its subagents is held while the finish alert still waits (and is called off)
373+
if (tracker === opencodeAgents && opencodeAgents.sessionOf(paneId)?.live !== true) {
374+
void paneGet(paneId).then((pane) => { if (opencodeAgents.ensure(pane)) opencodeAgents.poll(paneId); }).catch(() => undefined);
375+
}
354376
},
355377
// a finish reported as done, now in front at herdr's terminal: seen, idle again
356378
onFocus: (paneId) => {

‎server/opencode-subagents.test.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,16 @@ describe("an OpenCode session's subagents are its child sessions", () => {
179179
const s = store();
180180
s.session("ses_early", NOW - 50 * MIN);
181181
s.prompt("ses_early", NOW - 50 * MIN);
182-
for (let n = 0; n < 70; n++) {
182+
// more than are read for the list (MAX_CHILDREN)
183+
const ended = 1030;
184+
for (let n = 0; n < ended; n++) {
183185
s.session(`ses_done${n}`, NOW - 40 * MIN + n * 1000);
184186
s.prompt(`ses_done${n}`, NOW - 40 * MIN + n * 1000);
185187
s.idle(`ses_done${n}`, NOW - 30 * MIN + n * 1000);
186188
}
187189
const state = opencodeSubagentState(s.path, s.parent, true, NOW, null, false)!;
188190
expect(state.running).toBe(1);
189-
expect(state.tasks.map((task) => task.id)).toEqual(["ses_early", ...Array.from({ length: 10 }, (_, n) => `ses_done${69 - n}`)]);
191+
expect(state.tasks.map((task) => task.id)).toEqual(["ses_early", ...Array.from({ length: 10 }, (_, n) => `ses_done${ended - 1 - n}`)]);
190192
});
191193

192194
it("reads every other child when one's newest row is not JSON", () => {
@@ -268,8 +270,8 @@ describe("the OpenCode subagent tracker", () => {
268270

269271
it("drops another agent's wait when a pane first runs a session after it, never what the pane's own OpenCode turn was seen do", () => {
270272
const s = store();
271-
const resets: string[] = [];
272-
const tracker = new OpencodeSubagentStatus({ store: () => s.path, now: () => NOW, onChange: () => undefined, onReset: (paneId) => resets.push(paneId) });
273+
const resets: [string, boolean][] = [];
274+
const tracker = new OpencodeSubagentStatus({ store: () => s.path, now: () => NOW, onChange: () => undefined, onReset: (paneId, first) => resets.push([paneId, first]) });
273275
// a shell, then OpenCode's home screen, then its session found after the turn: one OpenCode lifetime
274276
tracker.refresh([{ pane_id: "w1:p1", agent: null, agent_status: "idle" } as HerdrPane]);
275277
tracker.refresh([pane(null)]);
@@ -278,7 +280,10 @@ describe("the OpenCode subagent tracker", () => {
278280
// a pane Claude Code ran until this snapshot
279281
tracker.refresh([{ ...pane(null, "claude"), pane_id: "w1:p2" }]);
280282
tracker.refresh([{ ...pane(s.parent), pane_id: "w1:p2" }]);
281-
expect(resets).toEqual(["w1:p2"]);
283+
expect(resets).toEqual([["w1:p2", true]]);
284+
// another session there: a new lifetime of OpenCode's own
285+
tracker.refresh([{ ...pane("ses_next"), pane_id: "w1:p2" }]);
286+
expect(resets).toEqual([["w1:p2", true], ["w1:p2", false]]);
282287
});
283288

284289
it("keeps the count while the store cannot be read", () => {

‎server/opencode-subagents.ts‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import { opencodeSessionId, withStore } from "./opencode.ts";
2929
/** As for Claude Code and OmO: the newest ten that ended in the last day are listed. */
3030
const RECENT_MS = 24 * 60 * 60_000;
3131
const RECENT_LIMIT = 10;
32-
/** A session's children are read up to this many, newest first: a session that started more is still read in bounded time. */
32+
/**
33+
* A session's ended children are read up to this many, newest first, for the list: a session that started more is still
34+
* read in bounded time. One whose run has not ended is read however many newer ones did: it counts.
35+
*/
3336
const MAX_CHILDREN = 1024;
3437
/** A run written this long before the service's start may still be the service's own (its start is read in whole ticks). */
3538
const START_SLACK_MS = 5_000;
@@ -102,16 +105,18 @@ export function readSubagents(db: Database, sessionId: string, live: boolean, no
102105
const columns = new Set(db.query<{ name: string }, []>("SELECT name FROM pragma_table_info('session_v2')").all().map((column) => column.name));
103106
// 1.x tables, or a 2.0 store from before child sessions: no subagents to tell of
104107
if (!columns.has("parent_id")) return NOTHING;
105-
// every child's newest run row, in one read, without the row's whole data (a step can be large)
106-
const runs = db.query<ChildRun, [string]>(`
108+
// every child's newest run row, without the row's whole data (a step can be large): each one whose run has not ended
109+
// with an idle row, to be classified here, then the newest ended ones, for the list
110+
const read = (only: string, limit: string): ChildRun[] => db.query<ChildRun, [string]>(`
107111
SELECT s.id, s.time_created AS created, m.type, m.time_created AS at,
108112
${["outcome:$.outcome", "created_at:$.time.created", "completed:$.time.completed", "finish:$.finish", "error_type:$.error.type"]
109113
.map((field) => field.split(":")).map(([name, path]) => `CASE WHEN json_valid(m.data) THEN json_extract(m.data, '${path}') END AS ${name}`).join(", ")},
110114
CASE WHEN json_valid(m.data) THEN json_type(m.data, '$.error') END AS error
111115
FROM session_v2 AS s JOIN session_message AS m ON m.id = (
112116
SELECT id FROM session_message WHERE session_id = s.id AND type IN ${RUN_ROWS} ORDER BY seq DESC LIMIT 1)
113-
WHERE s.parent_id = ?${columns.has("time_archived") ? " AND s.time_archived IS NULL" : ""}
114-
ORDER BY s.time_created DESC LIMIT ${MAX_CHILDREN}`).all(sessionId);
117+
WHERE s.parent_id = ?${columns.has("time_archived") ? " AND s.time_archived IS NULL" : ""}${only}
118+
ORDER BY s.time_created DESC${limit}`).all(sessionId);
119+
const runs = [...read(" AND m.type != 'idle'", ""), ...read(" AND m.type = 'idle'", ` LIMIT ${MAX_CHILDREN}`)];
115120
const prompt = db.query<{ data: string; time_created: number }, [string]>(
116121
"SELECT data, time_created FROM session_message WHERE session_id = ? AND type = 'user' ORDER BY seq DESC LIMIT 1",
117122
).get(sessionId);
@@ -164,10 +169,14 @@ export function opencodeSubagentState(path: string, sessionId: string, live: boo
164169
export interface OpencodeSubagentDeps {
165170
/** OpenCode's store: the one this server is configured with, else where OpenCode keeps it */
166171
store: () => string | null;
167-
/** when OpenCode's background service started, when it can be told */
172+
/** when OpenCode's background service started, when it can be told now: read on every poll, so never waited for */
168173
serviceStart?: () => number | null;
169-
/** The pane runs a session from now (another one, one it came back to, or its first after another agent): no earlier turn's wait is its own. */
170-
onReset?: (paneId: string) => void;
174+
/**
175+
* The pane runs a session from now: another one, or one it came back to (`first` false: no earlier turn's wait is its
176+
* own), or its first after another agent (`first`: what the other agent's turns left is not its own; what OpenCode was
177+
* seen do there since, before its session was found, is).
178+
*/
179+
onReset?: (paneId: string, first: boolean) => void;
171180
/** A pane's running subagents changed, or how many of them its turn started: no turn started or ended. */
172181
onChange: (paneId: string, running: number, turnRunning: number, promptAt: number | null) => void;
173182
pollMs?: number;
@@ -246,13 +255,14 @@ export class OpencodeSubagentStatus {
246255
if (tracked?.session === session) {
247256
if (tracked.live) return false;
248257
tracked.live = true;
249-
this.deps.onReset?.(pane.pane_id);
258+
this.deps.onReset?.(pane.pane_id, false);
250259
return true;
251260
}
252261
// another session than the one it ran, or the first after another agent's: a new lifetime. The first
253262
// session of a pane seen new, at a shell or at OpenCode's home screen is found after its turn began,
254263
// maybe after it ended: what that turn was seen do is this session's
255-
if (tracked !== undefined || (before != null && before !== "opencode")) this.deps.onReset?.(pane.pane_id);
264+
if (tracked !== undefined) this.deps.onReset?.(pane.pane_id, false);
265+
else if (before != null && before !== "opencode") this.deps.onReset?.(pane.pane_id, true);
256266
this.panes.set(pane.pane_id, { session, live: true, running: tracked?.running ?? 0, turnRunning: tracked?.turnRunning ?? 0, promptAt: tracked?.promptAt ?? null, reset: tracked !== undefined });
257267
return true;
258268
}
@@ -267,9 +277,10 @@ export class OpencodeSubagentStatus {
267277
const serviceStart = this.deps.serviceStart?.() ?? null;
268278
// one read for all: a store held by its writer is waited on once, not once per pane
269279
try {
280+
// on the event loop: a store its writer holds is not waited for
270281
withStore(path, (db) => {
271282
for (const [paneId, tracked] of live) states.set(paneId, readSubagents(db, tracked.session, true, this.now(), serviceStart, false));
272-
});
283+
}, 0);
273284
} catch { /* a store busy for a moment says nothing: the counts stand */ }
274285
}
275286
for (const [paneId, tracked] of this.panes) {

‎server/opencode.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,17 @@ export function forgetOpencodeRead(key: string): void {
305305
}
306306

307307
/** The store, read-only; null when it cannot be opened (no file, not SQLite, not readable). */
308-
function openStore(path: string): Database | null {
308+
function openStore(path: string, busyMs: number): Database | null {
309309
try {
310310
const db = new Database(path, { readonly: true, create: false });
311-
db.exec(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS}`);
311+
db.exec(`PRAGMA busy_timeout = ${busyMs}`);
312312
return db;
313313
} catch { return null; }
314314
}
315315

316-
export function withStore<T>(path: string, read: (db: Database) => T): T | null {
317-
const db = openStore(path);
316+
/** `busyMs`: how long a read waits on a store held by its writer; a poll that runs on the event loop waits not at all. */
317+
export function withStore<T>(path: string, read: (db: Database) => T, busyMs = BUSY_TIMEOUT_MS): T | null {
318+
const db = openStore(path, busyMs);
318319
if (db === null) return null;
319320
try { return read(db); }
320321
finally { db.close(); }

0 commit comments

Comments
 (0)