GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(tasks): take a pane's OpenCode session in read order and poll its… · devswha/ovis@fa86923

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • server
    • core
      • status.ts
    • opencode-subagents.test.ts
    • opencode-subagents.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎server/core/status.ts‎

Copy file name to clipboardExpand all lines: server/core/status.ts
+27-7Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ 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+
/** the read (opencodeAgents.beginRead) each pane's agentNow came from: a snapshot read before a newer word never undoes it */
117+
const agentRead = new Map<string, number>();
116118
/** each pane's agent as its status events have it, and since when: the rests from then on are that agent's (newLifetime) */
117119
const eventAgent = new Map<string, { agent: string | null; since: number }>();
118120
const statusNow = options.statusNow ?? Date.now;
@@ -147,9 +149,15 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
147149
const found = readOpenCodeService(options.openCodeStateDir ?? openCodeServiceOptions.stateDir);
148150
if (found === null) return null;
149151
if (service?.pid !== found.pid) {
150-
// not known until the lookup answers: meanwhile no run is taken for one the service before took along
152+
// until the lookup answers no run is taken for one the service before took along; then the store is read again
153+
// at once. A lookup with no answer is asked again
151154
const looked: { pid: number; started: number | null } = service = { pid: found.pid, started: null };
152-
void processStartUpperBoundAsync(found.pid).then((at) => { looked.started = at; }, () => undefined);
155+
void processStartUpperBoundAsync(found.pid).then((at) => {
156+
if (service !== looked) return;
157+
if (at === null) { service = null; return; }
158+
looked.started = at;
159+
opencodeAgents.poll();
160+
}, () => { if (service === looked) service = null; });
153161
}
154162
return service.started;
155163
},
@@ -181,16 +189,26 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
181189
/** herdr's snapshot with OmO's and GJC's own status in it: what the completion tracker and web push are given */
182190
const rawSnapshot = async (): Promise<SessionSnapshot> => {
183191
const promptCheckpoint = options.openCodeState?.checkpoint();
192+
const read = opencodeAgents.beginRead();
184193
const snapshot = await omoSnapshot();
185194
await gjc.refresh(snapshot.panes);
186195
const named = gjc.apply(omo.apply(snapshot));
187-
agentNow.clear();
188-
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);
196+
const ids = new Set(named.panes.map((pane) => pane.pane_id));
197+
for (const paneId of [...agentNow.keys()]) {
198+
if (ids.has(paneId) || (agentRead.get(paneId) ?? 0) > read) continue;
199+
agentNow.delete(paneId);
200+
agentRead.delete(paneId);
201+
eventAgent.delete(paneId);
202+
}
203+
for (const pane of named.panes) {
204+
if ((agentRead.get(pane.pane_id) ?? 0) > read) continue;
205+
agentNow.set(pane.pane_id, pane.agent ?? null);
206+
agentRead.set(pane.pane_id, read);
207+
}
190208
// not waited for: finding a transcript costs process calls, and a snapshot must not wait on them.
191209
// The plans come after: a Claude pane's transcript is the one the subagent tracker found
192210
void claudeAgents.refresh(named.panes).catch(() => undefined).then(() => plans.refresh(named.panes)).catch(() => undefined);
193-
opencodeAgents.refresh(named.panes);
211+
opencodeAgents.refresh(named.panes, read);
194212
// an agent this server started has had no turn yet: its start screen is no work (launched-agents.ts)
195213
for (const pane of named.panes) pane.agent_status = launchedAgents.settle(pane.pane_id, pane.agent_status);
196214
options.openCodeState?.reconcile(named.panes, promptCheckpoint);
@@ -338,6 +356,7 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
338356
if (machines) machines.localAgents(named); else bridgeAgents!.status(named);
339357
// newer than the last snapshot: a route that looks the pane's session up before the next one reads it so
340358
agentNow.set(paneId, named[0]!.agent ?? null);
359+
agentRead.set(paneId, opencodeAgents.beginRead());
341360
if (eventAgent.get(paneId)?.agent !== (named[0]!.agent ?? null)) eventAgent.set(paneId, { agent: named[0]!.agent ?? null, since: statusNow() });
342361
// herdr says `claude/idle` for an OmO pane whatever it does, and a provider child's work for
343362
// a GJC one while its transcript is tracked: their own status stands, and no Claude turn waits there
@@ -371,7 +390,8 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
371390
// an OpenCode turn whose session is not known yet: its pane is looked up now, not at the next snapshot, so a
372391
// turn that ended on its subagents is held while the finish alert still waits (and is called off)
373392
if (tracker === opencodeAgents && opencodeAgents.sessionOf(paneId)?.live !== true) {
374-
void paneGet(paneId).then((pane) => { if (opencodeAgents.ensure(pane)) opencodeAgents.poll(paneId); }).catch(() => undefined);
393+
const begun = opencodeAgents.beginRead();
394+
void paneGet(paneId).then((pane) => { if (opencodeAgents.ensure(pane, begun)) opencodeAgents.poll(paneId); }).catch(() => undefined);
375395
}
376396
},
377397
// a finish reported as done, now in front at herdr's terminal: seen, idle again
Collapse file

‎server/opencode-subagents.test.ts‎

Copy file name to clipboardExpand all lines: server/opencode-subagents.test.ts
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ describe("the OpenCode subagent tracker", () => {
286286
expect(resets).toEqual([["w1:p2", true], ["w1:p2", false]]);
287287
});
288288

289+
it("never takes a read of a pane begun before one it already took", () => {
290+
const s = store();
291+
s.session("ses_working", NOW - 5 * MIN);
292+
s.prompt("ses_working", NOW - 5 * MIN);
293+
const resets: [string, boolean][] = [];
294+
const tracker = new OpencodeSubagentStatus({ store: () => s.path, now: () => NOW, onChange: () => undefined, onReset: (paneId, first) => resets.push([paneId, first]) });
295+
// a snapshot's read begins, then a lookup made since finds the pane's session
296+
const stale = tracker.beginRead();
297+
tracker.ensure(pane(s.parent));
298+
tracker.poll();
299+
// the snapshot, read before it, still shows OpenCode's home screen, or no such pane yet
300+
tracker.refresh([pane(null)], stale);
301+
tracker.refresh([], stale);
302+
expect(tracker.sessionOf("w1:p1")?.live).toBe(true);
303+
expect(tracker.countOf("w1:p1")).toBe(1);
304+
// a newer read of the same session: no new lifetime
305+
tracker.refresh([pane(s.parent)]);
306+
expect(resets).toEqual([]);
307+
});
308+
289309
it("keeps the count while the store cannot be read", () => {
290310
const s = store();
291311
let path = s.path;
Collapse file

‎server/opencode-subagents.ts‎

Copy file name to clipboardExpand all lines: server/opencode-subagents.ts
+32-13Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,18 @@ export function readSubagents(db: Database, sessionId: string, live: boolean, no
105105
const columns = new Set(db.query<{ name: string }, []>("SELECT name FROM pragma_table_info('session_v2')").all().map((column) => column.name));
106106
// 1.x tables, or a 2.0 store from before child sessions: no subagents to tell of
107107
if (!columns.has("parent_id")) return NOTHING;
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]>(`
108+
// every child's newest run row, in one read, without the row's whole data (a step can be large): each one whose run
109+
// has not ended with an idle row is classified, and of the ended ones the newest, for the list
110+
let ended = 0;
111+
const runs = db.query<ChildRun, [string]>(`
111112
SELECT s.id, s.time_created AS created, m.type, m.time_created AS at,
112113
${["outcome:$.outcome", "created_at:$.time.created", "completed:$.time.completed", "finish:$.finish", "error_type:$.error.type"]
113114
.map((field) => field.split(":")).map(([name, path]) => `CASE WHEN json_valid(m.data) THEN json_extract(m.data, '${path}') END AS ${name}`).join(", ")},
114115
CASE WHEN json_valid(m.data) THEN json_type(m.data, '$.error') END AS error
115116
FROM session_v2 AS s JOIN session_message AS m ON m.id = (
116117
SELECT id FROM session_message WHERE session_id = s.id AND type IN ${RUN_ROWS} ORDER BY seq DESC LIMIT 1)
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}`)];
118+
WHERE s.parent_id = ?${columns.has("time_archived") ? " AND s.time_archived IS NULL" : ""}
119+
ORDER BY s.time_created DESC`).all(sessionId).filter((run) => run.type !== "idle" || ++ended <= MAX_CHILDREN);
120120
const prompt = db.query<{ data: string; time_created: number }, [string]>(
121121
"SELECT data, time_created FROM session_message WHERE session_id = ? AND type = 'user' ORDER BY seq DESC LIMIT 1",
122122
).get(sessionId);
@@ -195,6 +195,9 @@ export class OpencodeSubagentStatus {
195195
private readonly panes = new Map<string, Tracked>();
196196
/** each pane's agent as last seen: whether the first session it runs follows another agent's */
197197
private readonly agents = new Map<string, string | null>();
198+
/** reads of herdr's panes, numbered as they begin, and the newest taken for each pane: an older one never undoes it */
199+
private reads = 0;
200+
private readonly lastRead = new Map<string, number>();
198201
private timer: ReturnType<typeof setInterval> | null = null;
199202
private readonly now: () => number;
200203

@@ -231,17 +234,33 @@ export class OpencodeSubagentStatus {
231234
return opencodeSubagentState(known.path, known.session, known.live, this.now(), this.deps.serviceStart?.() ?? null)?.tasks ?? null;
232235
}
233236

234-
/** The OpenCode panes of a snapshot and their sessions; a pane whose binding moved is read now. */
235-
refresh(panes: HerdrPane[]): void {
237+
/** Numbers a read of herdr's panes as it begins, before it is awaited: what refresh and ensure are given. */
238+
beginRead(): number {
239+
return ++this.reads;
240+
}
241+
242+
/** The OpenCode panes of a snapshot (`read`: when its read began) and their sessions; a pane whose binding moved is read now. */
243+
refresh(panes: HerdrPane[], read = this.beginRead()): void {
236244
const ids = new Set(panes.map((pane) => pane.pane_id));
237-
for (const paneId of this.panes.keys()) if (!ids.has(paneId)) this.panes.delete(paneId);
238-
for (const paneId of this.agents.keys()) if (!ids.has(paneId)) this.agents.delete(paneId);
239-
const moved = panes.filter((pane) => this.ensure(pane));
245+
for (const paneId of [...this.lastRead.keys()]) {
246+
if (ids.has(paneId) || this.newer(paneId, read)) continue;
247+
this.panes.delete(paneId);
248+
this.agents.delete(paneId);
249+
this.lastRead.delete(paneId);
250+
}
251+
const moved = panes.filter((pane) => this.ensure(pane, read));
240252
if (moved.length > 0) this.poll(new Set(moved.map((pane) => pane.pane_id)));
241253
}
242254

243-
/** One pane's session, as herdr reports it now; true when its binding changed. */
244-
ensure(pane: HerdrPane): boolean {
255+
/** Whether a read of the pane begun after `read` was taken already. */
256+
private newer(paneId: string, read: number): boolean {
257+
return (this.lastRead.get(paneId) ?? 0) > read;
258+
}
259+
260+
/** One pane's session, as herdr reports it (`read`: when that read began); true when its binding changed. */
261+
ensure(pane: HerdrPane, read = this.beginRead()): boolean {
262+
if (this.newer(pane.pane_id, read)) return false;
263+
this.lastRead.set(pane.pane_id, read);
245264
const tracked = this.panes.get(pane.pane_id);
246265
const before = this.agents.get(pane.pane_id);
247266
this.agents.set(pane.pane_id, pane.agent ?? null);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.