@kabmo-labs/kabmo 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +1 -0
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +374 -356
  4. package/dist/client/static/js/{index-mx658m43.js → index-cpydmygd.js} +644 -644
  5. package/dist/client/static/js/index-fjnq2z2d.css +1 -0
  6. package/marketplace/plugins/stats/README.md +27 -8
  7. package/marketplace/plugins/stats/package.json +2 -2
  8. package/marketplace/plugins/stats/src/HeaderReadout.test.tsx +2 -1
  9. package/marketplace/plugins/stats/src/HeaderReadout.tsx +6 -2
  10. package/marketplace/plugins/stats/src/KillConfirm.test.tsx +159 -0
  11. package/marketplace/plugins/stats/src/KillConfirm.tsx +145 -0
  12. package/marketplace/plugins/stats/src/ProcessTable.tsx +54 -8
  13. package/marketplace/plugins/stats/src/TotalsBar.test.tsx +5 -2
  14. package/marketplace/plugins/stats/src/TotalsBar.tsx +13 -3
  15. package/marketplace/plugins/stats/src/app.test.tsx +100 -4
  16. package/marketplace/plugins/stats/src/app.tsx +51 -4
  17. package/marketplace/plugins/stats/src/groups.test.ts +1 -1
  18. package/marketplace/plugins/stats/src/kill.test.ts +131 -0
  19. package/marketplace/plugins/stats/src/kill.ts +74 -0
  20. package/marketplace/plugins/stats/src/labels.test.ts +1 -0
  21. package/marketplace/plugins/stats/src/machine.test.ts +77 -0
  22. package/marketplace/plugins/stats/src/machine.ts +115 -0
  23. package/marketplace/plugins/stats/src/proc/darwin.ts +38 -0
  24. package/marketplace/plugins/stats/src/proc/linux.ts +4 -0
  25. package/marketplace/plugins/stats/src/proc/ps.ts +1 -0
  26. package/marketplace/plugins/stats/src/proc/types.ts +11 -0
  27. package/marketplace/plugins/stats/src/sampler.test.ts +42 -4
  28. package/marketplace/plugins/stats/src/sampler.ts +32 -18
  29. package/marketplace/plugins/stats/src/server.ts +29 -2
  30. package/marketplace/plugins/stats/src/snapshot.ts +31 -3
  31. package/package.json +3 -2
  32. package/src/client/components/common/Modal.tsx +7 -0
  33. package/src/client/components/layout/LeftDock.tsx +1 -0
  34. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +1 -0
  35. package/src/client/components/mobile/RightSidebar.tsx +1 -0
  36. package/src/client/components/mobile/mobile-session-sidebar/use-dock.ts +1 -0
  37. package/src/client/components/workspace/model-dropdown/Panel.tsx +7 -2
  38. package/src/client/components/workspace/model-dropdown/Section.test.tsx +13 -0
  39. package/src/client/components/workspace/model-dropdown/Section.tsx +5 -1
  40. package/src/client/components/workspace/panels/builtin.tsx +7 -0
  41. package/src/client/components/workspace/wiki-panel/NavModal.test.tsx +136 -0
  42. package/src/client/components/workspace/wiki-panel/NavModal.tsx +94 -0
  43. package/src/client/components/workspace/wiki-panel/PageView.tsx +16 -10
  44. package/src/client/components/workspace/wiki-panel/index.tsx +44 -9
  45. package/src/server/lib/db/schema.ts +11 -0
  46. package/src/server/lib/lifecycle/memory.test.ts +41 -1
  47. package/src/server/lib/lifecycle/memory.ts +131 -14
  48. package/src/server/lib/omp/session/subagent-presence-scan.test.ts +137 -0
  49. package/src/server/lib/omp/session/subagent-presence-scan.ts +120 -0
  50. package/src/server/lib/omp/session/subagent-presence-store.test.ts +120 -0
  51. package/src/server/lib/omp/session/subagent-presence-store.ts +65 -0
  52. package/src/server/lib/omp/session/subagent-presence.ts +52 -15
  53. package/dist/client/static/js/index-28pwtt69.css +0 -1
@@ -0,0 +1,137 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The streaming presence scan: one bit, without materializing the transcript.
8
+ *
9
+ * Why it exists is in the module doc; what this file pins is the agreement
10
+ * with the roster fold (`subagent/history/fold.ts`), because a badge that
11
+ * disagrees with the panel it opens is worse than a slow badge:
12
+ *
13
+ * - a valid subagent id in `details.progress` or `details.results` is a hit;
14
+ * - an id the fold would reject (`../evil`, a dotted path fragment) is not;
15
+ * - a `task` toolResult that arrives AFTER a chunk boundary is still found —
16
+ * the scan streams, so the matching line is assembled across reads;
17
+ * - a plain tool result, an assistant toolCall without a result, and a
18
+ * lifecycle entry are not hits.
19
+ */
20
+
21
+ import { afterAll, describe, expect, test } from 'bun:test';
22
+ import fs from 'fs';
23
+ import { dirname, join } from 'path';
24
+ import { tmpdir } from 'node:os';
25
+
26
+ import { SESSION_PRESENCE_MAX_LINE_BYTES, sessionFileHasSubagents } from '@/server/lib/omp/session/subagent-presence-scan';
27
+
28
+ const tempDirs: string[] = [];
29
+
30
+ function tempDir(): string {
31
+ const dir = fs.mkdtempSync(join(tmpdir(), 'presence-scan-'));
32
+ tempDirs.push(dir);
33
+ return dir;
34
+ }
35
+
36
+ function writeLines(name: string, lines: unknown[]): string {
37
+ const file = join(tempDir(), name);
38
+ fs.mkdirSync(dirname(file), { recursive: true });
39
+ fs.writeFileSync(file, `${lines.map((line) => JSON.stringify(line)).join('\n')}\n`);
40
+ return file;
41
+ }
42
+
43
+ function header(id = 'session'): Record<string, unknown> {
44
+ return { type: 'session', version: 3, id, cwd: '/tmp/proj', timestamp: '2026-09-01T00:00:00.000Z' };
45
+ }
46
+
47
+ /** The exact shape the roster fold reads: a `task` toolResult with details. */
48
+ function taskResult(details: Record<string, unknown>): Record<string, unknown> {
49
+ return {
50
+ type: 'message',
51
+ timestamp: '2026-09-01T00:00:10.000Z',
52
+ message: { role: 'toolResult', toolName: 'task', toolCallId: 'call-1', details },
53
+ };
54
+ }
55
+
56
+ afterAll(() => {
57
+ for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
58
+ });
59
+
60
+ describe('sessionFileHasSubagents', () => {
61
+ test('a session with no task result is not a hit', async () => {
62
+ const file = writeLines('plain.jsonl', [
63
+ header(),
64
+ { type: 'message', timestamp: '2026-09-01T00:00:05.000Z', message: { role: 'user', content: 'hi' } },
65
+ {
66
+ type: 'message',
67
+ timestamp: '2026-09-01T00:00:06.000Z',
68
+ message: { role: 'toolResult', toolName: 'bash', details: { output: 'ok' } },
69
+ },
70
+ ]);
71
+ expect(await sessionFileHasSubagents(file)).toBe(false);
72
+ });
73
+
74
+ test('an assistant task toolCall without a result is not a hit', async () => {
75
+ // Presence comes from the RESULT fold, not from a call that never landed:
76
+ // an aborted launch has no roster entry to show.
77
+ const file = writeLines('call-only.jsonl', [
78
+ header(),
79
+ {
80
+ type: 'message',
81
+ timestamp: '2026-09-01T00:00:07.000Z',
82
+ message: {
83
+ role: 'assistant',
84
+ content: [{ type: 'toolCall', id: 'call-1', name: 'task', arguments: { task: 'x' } }],
85
+ },
86
+ },
87
+ ]);
88
+ expect(await sessionFileHasSubagents(file)).toBe(false);
89
+ });
90
+
91
+ test('progress naming a subagent is a hit', async () => {
92
+ const file = writeLines('progress.jsonl', [
93
+ header(),
94
+ taskResult({ progress: [{ id: 'AlphaBeta', agent: 'task', status: 'running' }] }),
95
+ ]);
96
+ expect(await sessionFileHasSubagents(file)).toBe(true);
97
+ });
98
+
99
+ test('results carrying a subagent id is a hit', async () => {
100
+ const file = writeLines('results.jsonl', [header(), taskResult({ results: [{ id: 'GammaDelta', exitCode: 0 }] })]);
101
+ expect(await sessionFileHasSubagents(file)).toBe(true);
102
+ });
103
+
104
+ test('an id the roster fold would reject is not a hit', async () => {
105
+ const file = writeLines('traversal.jsonl', [header(), taskResult({ results: [{ id: '../evil' }] })]);
106
+ expect(await sessionFileHasSubagents(file)).toBe(false);
107
+ });
108
+
109
+ test('a hit on the last line is found across chunk boundaries', async () => {
110
+ // The scan streams, so the matching line is assembled from more than one
111
+ // read. One oversized line before it proves the carry buffer survives.
112
+ const big = 'x'.repeat(512 * 1024);
113
+ const file = writeLines('streamed.jsonl', [
114
+ header(),
115
+ { type: 'message', timestamp: '2026-09-01T00:00:08.000Z', message: { role: 'toolResult', toolName: 'bash', details: { output: big } } },
116
+ taskResult({ progress: [{ id: 'OmegaSigma', agent: 'task', status: 'running' }] }),
117
+ ]);
118
+ expect(await sessionFileHasSubagents(file)).toBe(true);
119
+ });
120
+
121
+ test('an unreadable file throws, so the caller can decide whether to remember', async () => {
122
+ const missing = join(tempDir(), 'gone.jsonl');
123
+ let threw = false;
124
+ try {
125
+ await sessionFileHasSubagents(missing);
126
+ } catch {
127
+ threw = true;
128
+ }
129
+ expect(threw).toBe(true);
130
+ });
131
+
132
+ test('the oversized-line ceiling is far above a real task result', () => {
133
+ // A task result carries telemetry, not outputs; the cap exists to drop a
134
+ // tool output that would otherwise be held in full.
135
+ expect(SESSION_PRESENCE_MAX_LINE_BYTES).toBeGreaterThan(1024 * 1024);
136
+ });
137
+ });
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Does a session file hold any subagent, decided WITHOUT materializing it.
8
+ *
9
+ * `extractSubagentHistory` answers the same question and is the roster source,
10
+ * but it reads the whole transcript and lenient-parses every entry — for the
11
+ * sidebar's presence badge that was measured at 1.38 GB of reads and a 900 MB
12
+ * resident-set jump on a 1349-file / 2.0 GB agent directory, because Bun keeps
13
+ * the freed arena. The badge needs one bit, so this walks the file as a stream
14
+ * and stops at the first entry that would put something in the roster.
15
+ *
16
+ * The predicate is deliberately the SAME one the roster fold applies
17
+ * (`createFolder` in `subagent/history/fold.ts`), or the badge would disagree
18
+ * with the panel it opens:
19
+ *
20
+ * - a `task` toolResult entry whose `details.progress` names a subagent id
21
+ * (`parseSubagentProgress` → `id`), or
22
+ * - a `task` toolResult entry whose `details.results` carries an id.
23
+ *
24
+ * Both are then filtered by `SUBAGENT_ID_RE` on upsert, so an id the fold would
25
+ * reject is rejected here too.
26
+ *
27
+ * Memory stays bounded: one chunk plus the line being assembled, and an
28
+ * oversized line (a tool result carrying a file read) is dropped rather than
29
+ * held. A `task` result is small — its details are telemetry, outputs are
30
+ * deliberately not copied — so the only lines this rule can lose are ones that
31
+ * could not have matched.
32
+ */
33
+
34
+ import { isRecord } from '@/shared/lib/util/guards';
35
+ import { parseSubagentProgress } from '@/shared/lib/omp/subagent/parse';
36
+ import { SUBAGENT_ID_RE } from '@/server/lib/omp/subagent/history/paths';
37
+
38
+ /** A line longer than this is not a `task` result; it is dropped unparsed. */
39
+ export const SESSION_PRESENCE_MAX_LINE_BYTES = 8 * 1024 * 1024;
40
+
41
+ /**
42
+ * A cheap reject before the parse: every entry that could match contains both
43
+ * a `task` tool name and a `toolResult` role. Skipping the other ~99% of lines
44
+ * with two `includes` calls is what keeps a 2 GB cold scan in seconds.
45
+ */
46
+ function couldBeTaskResult(line: string): boolean {
47
+ return line.includes('"task"') && line.includes('toolResult');
48
+ }
49
+
50
+ /** The roster fold's own admission test, applied to one parsed entry. */
51
+ function admitsSubagent(entry: unknown): boolean {
52
+ if (!isRecord(entry) || entry.type !== 'message') return false;
53
+ const message = entry.message;
54
+ if (!isRecord(message) || message.role !== 'toolResult' || message.toolName !== 'task') return false;
55
+ const details = isRecord(message.details) ? message.details : {};
56
+ const progress = Array.isArray(details.progress) ? details.progress : [];
57
+ for (const raw of progress) {
58
+ const id = parseSubagentProgress(raw)?.id;
59
+ if (id !== undefined && SUBAGENT_ID_RE.test(id)) return true;
60
+ }
61
+ const results = Array.isArray(details.results) ? details.results : [];
62
+ for (const raw of results) {
63
+ if (!isRecord(raw)) continue;
64
+ const id = raw.id;
65
+ if (typeof id === 'string' && SUBAGENT_ID_RE.test(id)) return true;
66
+ }
67
+ return false;
68
+ }
69
+
70
+ /** One line of the file, parsed and admitted, with a torn line answering `false`. */
71
+ function lineAdmitsSubagent(line: string): boolean {
72
+ if (!couldBeTaskResult(line)) return false;
73
+ let parsed: unknown;
74
+ try {
75
+ parsed = JSON.parse(line);
76
+ } catch {
77
+ // A torn line is not a roster entry; the fold would skip it too.
78
+ return false;
79
+ }
80
+ return admitsSubagent(parsed);
81
+ }
82
+
83
+ /**
84
+ * True when the session file holds at least one subagent entry.
85
+ *
86
+ * A read failure THROWS rather than answering `false`: presence is a badge,
87
+ * and the caller decides whether an unreadable transcript should be reported
88
+ * as absent (it is, for this call) or remembered (it must not be).
89
+ */
90
+ export async function sessionFileHasSubagents(filePath: string): Promise<boolean> {
91
+ const decoder = new TextDecoder();
92
+ // A reader rather than `for await`: Bun's stream is async-iterable at
93
+ // runtime, but the TypeScript DOM definition of `ReadableStream` is not.
94
+ const reader = Bun.file(filePath).stream().getReader();
95
+ let pending = '';
96
+ try {
97
+ for (;;) {
98
+ const { done, value } = await reader.read();
99
+ if (done) break;
100
+ pending += decoder.decode(value, { stream: true });
101
+ let newline = pending.indexOf('\n');
102
+ while (newline !== -1) {
103
+ const line = pending.slice(0, newline);
104
+ pending = pending.slice(newline + 1);
105
+ if (line.length <= SESSION_PRESENCE_MAX_LINE_BYTES && lineAdmitsSubagent(line)) return true;
106
+ newline = pending.indexOf('\n');
107
+ }
108
+ if (pending.length > SESSION_PRESENCE_MAX_LINE_BYTES) {
109
+ // An oversized line: drop what we hold and let the rest of it arrive as
110
+ // a fragment the next newline discards. See the module note.
111
+ pending = '';
112
+ }
113
+ }
114
+ } finally {
115
+ // A hit returns early; the rest of the transcript is not needed.
116
+ reader.cancel().catch(() => {});
117
+ }
118
+ pending += decoder.decode();
119
+ return lineAdmitsSubagent(pending.trim());
120
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The saved presence verdict — the half of the probe that survives a restart.
8
+ *
9
+ * In-process caching already answered "cheap after the first call"; what this
10
+ * pins is the case the old module could not: a NEW process, whose sidebar load
11
+ * used to stream every transcript again (measured 1.38 GB of reads on a
12
+ * 1349-file agent directory). The verdict is only reusable for the exact file
13
+ * version that produced it, so both halves of the key are exercised:
14
+ *
15
+ * - same version, different file content → the saved answer stands (this is
16
+ * what a restart looks like);
17
+ * - new version → the transcript is read again, and the saved answer is
18
+ * replaced.
19
+ *
20
+ * The probe must also stay usable with NO database open: it runs in unit
21
+ * suites and in any caller that has not opened the store yet, and it must
22
+ * never create one.
23
+ */
24
+
25
+ import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
26
+ import fs from 'fs';
27
+ import { dirname, join } from 'path';
28
+ import { tmpdir } from 'node:os';
29
+
30
+ import { closeDb, getDb } from '@/server/db.server';
31
+ import { isolateDb, releaseDb } from '@/test-support/isolated-db';
32
+ import { clearSubagentPresenceCache, sessionHasSubagents } from '@/server/lib/omp/session/subagent-presence';
33
+ import { readPresenceVerdict } from '@/server/lib/omp/session/subagent-presence-store';
34
+
35
+ const tempDirs: string[] = [];
36
+
37
+ function tempDir(): string {
38
+ const dir = fs.mkdtempSync(join(tmpdir(), 'presence-store-'));
39
+ tempDirs.push(dir);
40
+ return dir;
41
+ }
42
+
43
+ function writeSession(name: string, lines: unknown[]): string {
44
+ const file = join(tempDir(), name);
45
+ fs.mkdirSync(dirname(file), { recursive: true });
46
+ fs.writeFileSync(file, `${lines.map((line) => JSON.stringify(line)).join('\n')}\n`);
47
+ return file;
48
+ }
49
+
50
+ function header(): Record<string, unknown> {
51
+ return { type: 'session', version: 3, id: 's1', cwd: '/tmp/proj', timestamp: '2026-09-01T00:00:00.000Z' };
52
+ }
53
+
54
+ function withSubagent(): Record<string, unknown> {
55
+ return {
56
+ type: 'message',
57
+ timestamp: '2026-09-01T00:00:10.000Z',
58
+ message: { role: 'toolResult', toolName: 'task', details: { progress: [{ id: 'AlphaBeta', agent: 'task', status: 'running' }] } },
59
+ };
60
+ }
61
+
62
+ beforeEach(async () => {
63
+ await isolateDb();
64
+ // Opening is what creates the schema (and drops this suite's in-process map).
65
+ await getDb();
66
+ clearSubagentPresenceCache();
67
+ });
68
+
69
+ afterEach(async () => {
70
+ clearSubagentPresenceCache();
71
+ await releaseDb();
72
+ for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
73
+ tempDirs.length = 0;
74
+ });
75
+
76
+ describe('saved presence verdict', () => {
77
+ test('answers from the database for the same version, even when the file changed', async () => {
78
+ const file = writeSession('same-version.jsonl', [header(), withSubagent()]);
79
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(true);
80
+
81
+ // What a restart does: the in-process map is gone...
82
+ clearSubagentPresenceCache();
83
+ // ...and the file no longer holds the entry the verdict came from. Only a
84
+ // saved verdict can still answer `true` here.
85
+ fs.writeFileSync(file, `${JSON.stringify(header())}\n`);
86
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(true);
87
+ });
88
+
89
+ test('a new version re-reads the file and replaces the saved verdict', async () => {
90
+ const file = writeSession('new-version.jsonl', [header(), withSubagent()]);
91
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(true);
92
+ // The transcript loses the entry AND the version moves: only a re-read can
93
+ // answer `false`, and it must replace what the store held.
94
+ fs.writeFileSync(file, `${JSON.stringify(header())}\n`);
95
+ clearSubagentPresenceCache();
96
+ expect(await sessionHasSubagents(file, 'entry-2')).toBe(false);
97
+ // The new verdict is what a restart sees for that version.
98
+ clearSubagentPresenceCache();
99
+ fs.writeFileSync(file, `${JSON.stringify(header())}\n${JSON.stringify(withSubagent())}\n`);
100
+ expect(await sessionHasSubagents(file, 'entry-2')).toBe(false);
101
+ });
102
+
103
+ test('the false case is saved too — a restart must not re-scan a plain session', async () => {
104
+ const file = writeSession('plain.jsonl', [header()]);
105
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(false);
106
+ // Same version, now holding an entry: a saved `false` answers without
107
+ // reading the file again (this is the cheap path a restart needs).
108
+ clearSubagentPresenceCache();
109
+ fs.writeFileSync(file, `${JSON.stringify(header())}\n${JSON.stringify(withSubagent())}\n`);
110
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(false);
111
+ });
112
+
113
+ test('a probe with no database open still answers, and saves nothing', async () => {
114
+ const file = writeSession('no-db.jsonl', [header(), withSubagent()]);
115
+ await closeDb();
116
+ expect(await sessionHasSubagents(file, 'entry-1')).toBe(true);
117
+ // Asking the store again is a miss, not an error: no handle means no cache.
118
+ expect(await readPresenceVerdict(file, 'entry-1:0')).toBeNull();
119
+ });
120
+ });
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The subagent-presence verdict, saved so a restart does not re-derive it.
8
+ *
9
+ * Presence is a fact about a FILE VERSION, not about a live process: the
10
+ * verdict only changes when the session gains entries or its sibling artifacts
11
+ * directory changes, and both are already encoded in the key. Deriving it
12
+ * means streaming the whole parent transcript, measured at 1.38 GB of reads on
13
+ * a 1349-file agent directory — the reason a freshly restarted server held
14
+ * ~900 MB of resident-set in freed arena after the console's sidebar load.
15
+ * An in-process map cannot survive the restart that pays that cost, so the one
16
+ * verdict the next boot would otherwise recompute lives in the database it
17
+ * already opens for the same request (AGENTS.md §6: anything that must survive
18
+ * a reload is server-side state).
19
+ *
20
+ * Two rules keep this safe for callers that are not the sidebar:
21
+ *
22
+ * 1. **It never opens a database.** `getDbSync()` answers the handle already
23
+ * open, and a probe that runs before `getDb()` (or in a test that has no
24
+ * database) simply misses the cache. Opening one here would make a badge
25
+ * read create a file and would leak the developer's real store into a test.
26
+ * 2. **A failure is a miss.** A read or write that throws costs one rescan; it
27
+ * must never fail the sidebar.
28
+ *
29
+ * Rows for sessions deleted long ago are not pruned: they are a hundred bytes
30
+ * each, they are never read (no file list entry names them), and a delete pass
31
+ * would be a write on the hot path of every boot.
32
+ */
33
+
34
+ import { getDbSync } from '@/server/db.server';
35
+
36
+ /** The saved verdict for this exact file version, or null when unknown. */
37
+ export async function readPresenceVerdict(sessionPath: string, version: string): Promise<boolean | null> {
38
+ try {
39
+ const row = await getDbSync().get<{ has_subagents: number }>(
40
+ 'SELECT has_subagents FROM session_subagent_presence WHERE session_path = ? AND version = ?',
41
+ [sessionPath, version],
42
+ );
43
+ if (!row) return null;
44
+ return row.has_subagents === 1;
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ /** Replace the saved verdict for a session. Best-effort by contract. */
51
+ export async function writePresenceVerdict(sessionPath: string, version: string, hasSubagents: boolean): Promise<void> {
52
+ try {
53
+ await getDbSync().run(
54
+ `INSERT INTO session_subagent_presence (session_path, version, has_subagents, updated_at)
55
+ VALUES (?, ?, ?, ?)
56
+ ON CONFLICT(session_path) DO UPDATE SET
57
+ version = excluded.version,
58
+ has_subagents = excluded.has_subagents,
59
+ updated_at = excluded.updated_at`,
60
+ [sessionPath, version, hasSubagents ? 1 : 0, Date.now()],
61
+ );
62
+ } catch {
63
+ // A miss here is one rescan on the next start, never a failed sidebar.
64
+ }
65
+ }
@@ -7,22 +7,37 @@
7
7
  * Cached "does this session have subagents?" probe.
8
8
  *
9
9
  * The sidebar loader asks this for EVERY session on EVERY fetch. The honest
10
- * answer needs the session file's `task` toolCalls, which is the authoritative
11
- * roster — but reading them means parsing a file that can reach hundreds of
12
- * megabytes, and doing that per session per fetch dominated the endpoint's cost
13
- * (the dataset scan itself is cached; this probe was not).
10
+ * answer needs the parent session's `task` toolResults, which are the
11
+ * authoritative roster — but deriving them by materializing the transcript is
12
+ * what made a freshly restarted server hold ~900 MB: on a 1349-file / 2.0 GB
13
+ * agent directory the old fold read and parsed 1.38 GB in one cold pass, and
14
+ * Bun keeps the freed arena (measured: RSS 233 MB → 1148 MB after one console
15
+ * load, physical footprint 62 MB, `Bun.gc(true)` returning nothing).
16
+ *
17
+ * Three answers, in ascending cost:
18
+ *
19
+ * 1. the in-process map, keyed by a version that changes when the roster could
20
+ * have changed;
21
+ * 2. the saved verdict for that same version (see `subagent-presence-store`),
22
+ * which is what makes the SECOND start's sidebar load cheap — the case the
23
+ * in-process map cannot cover, because a restart empties it;
24
+ * 3. the sibling artifacts directory (one stat + one readdir, the common case
25
+ * for a session that ever spawned anything) and, failing that, a streaming
26
+ * scan that stops at the first roster entry
27
+ * (`subagent-presence-scan`), which never holds the transcript.
14
28
  *
15
29
  * The roster can only change when the session file gains entries or its
16
30
  * sibling artifacts directory changes, so that (last-entry timestamp,
17
31
  * sibling-dir mtime) pair is a sufficient cache version. Both cost one stat —
18
- * orders of magnitude cheaper than the parse they gate. The timestamp is the
32
+ * orders of magnitude cheaper than the work they gate. The timestamp is the
19
33
  * file's last ENTRY time, not its mtime: a title-slot rewrite bumps mtime
20
34
  * without appending anything, and a spurious version bump would re-run the
21
- * parse it exists to avoid.
35
+ * work it exists to avoid.
22
36
  */
23
37
 
24
38
  import { promises as fs, statSync } from 'fs';
25
- import { extractSubagentHistory } from '@/server/lib/omp/subagent/history';
39
+ import { sessionFileHasSubagents } from '@/server/lib/omp/session/subagent-presence-scan';
40
+ import { readPresenceVerdict, writePresenceVerdict } from '@/server/lib/omp/session/subagent-presence-store';
26
41
  import { siblingDirForSession } from '@/server/lib/omp/subagent/history/paths';
27
42
 
28
43
  interface PresenceEntry {
@@ -35,6 +50,15 @@ const MAX_ENTRIES = 2_000;
35
50
 
36
51
  const cache = new Map<string, PresenceEntry>();
37
52
 
53
+ /** Record a verdict in the in-process map, evicting the oldest when full. */
54
+ function remember(sessionPath: string, version: string, hasSubagents: boolean): void {
55
+ if (!cache.has(sessionPath) && cache.size >= MAX_ENTRIES) {
56
+ const oldest = cache.keys().next().value;
57
+ if (oldest !== undefined) cache.delete(oldest);
58
+ }
59
+ cache.set(sessionPath, { version, hasSubagents });
60
+ }
61
+
38
62
  /**
39
63
  * Version of the sibling artifacts directory, or `0` when it does not exist —
40
64
  * which is the COMMON case, since most sessions never spawn a subagent.
@@ -67,6 +91,12 @@ function siblingDirVersion(dir: string): number {
67
91
  }
68
92
  }
69
93
 
94
+ /** Drop the in-process map, so a caller observes the saved verdict again.
95
+ * Exported for tests: a process restart is what does this in production. */
96
+ export function clearSubagentPresenceCache(): void {
97
+ cache.clear();
98
+ }
99
+
70
100
  export async function sessionHasSubagents(
71
101
  sessionPath: string,
72
102
  sessionLastEntryAt: string,
@@ -78,6 +108,12 @@ export async function sessionHasSubagents(
78
108
  const cached = cache.get(sessionPath);
79
109
  if (cached && cached.version === version) return cached.hasSubagents;
80
110
 
111
+ const saved = await readPresenceVerdict(sessionPath, version);
112
+ if (saved !== null) {
113
+ remember(sessionPath, version, saved);
114
+ return saved;
115
+ }
116
+
81
117
  let hasSubagents = false;
82
118
  // Guarded on the version: an unguarded readdir on the absent dir is the same
83
119
  // spurious-rejection leak, and it rejects on the same sessions. The try/catch
@@ -93,16 +129,17 @@ export async function sessionHasSubagents(
93
129
  }
94
130
  if (!hasSubagents) {
95
131
  try {
96
- hasSubagents = (await extractSubagentHistory(sessionPath)).length > 0;
132
+ hasSubagents = await sessionFileHasSubagents(sessionPath);
97
133
  } catch {
98
- hasSubagents = false;
134
+ // A transcript that cannot be read answers "no subagents" for this call,
135
+ // and is deliberately NOT saved: a transient failure must not freeze the
136
+ // badge until the file's next version bump.
137
+ remember(sessionPath, version, false);
138
+ return false;
99
139
  }
100
140
  }
101
141
 
102
- if (!cached && cache.size >= MAX_ENTRIES) {
103
- const oldest = cache.keys().next().value;
104
- if (oldest !== undefined) cache.delete(oldest);
105
- }
106
- cache.set(sessionPath, { version, hasSubagents });
142
+ remember(sessionPath, version, hasSubagents);
143
+ await writePresenceVerdict(sessionPath, version, hasSubagents);
107
144
  return hasSubagents;
108
- }
145
+ }