@kabmo-labs/kabmo 0.15.0 → 0.17.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.
- package/README.md +3 -2
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +412 -394
- package/dist/client/static/js/index-3ve31rzx.css +1 -0
- package/dist/client/static/js/{index-mx658m43.js → index-rerjq5th.js} +846 -844
- package/marketplace/plugins/stats/README.md +27 -8
- package/marketplace/plugins/stats/package.json +2 -2
- package/marketplace/plugins/stats/src/HeaderReadout.test.tsx +2 -1
- package/marketplace/plugins/stats/src/HeaderReadout.tsx +6 -2
- package/marketplace/plugins/stats/src/KillConfirm.test.tsx +159 -0
- package/marketplace/plugins/stats/src/KillConfirm.tsx +145 -0
- package/marketplace/plugins/stats/src/ProcessTable.tsx +54 -8
- package/marketplace/plugins/stats/src/TotalsBar.test.tsx +5 -2
- package/marketplace/plugins/stats/src/TotalsBar.tsx +13 -3
- package/marketplace/plugins/stats/src/app.test.tsx +100 -4
- package/marketplace/plugins/stats/src/app.tsx +51 -4
- package/marketplace/plugins/stats/src/groups.test.ts +1 -1
- package/marketplace/plugins/stats/src/kill.test.ts +131 -0
- package/marketplace/plugins/stats/src/kill.ts +74 -0
- package/marketplace/plugins/stats/src/labels.test.ts +1 -0
- package/marketplace/plugins/stats/src/machine.test.ts +77 -0
- package/marketplace/plugins/stats/src/machine.ts +115 -0
- package/marketplace/plugins/stats/src/proc/darwin.ts +38 -0
- package/marketplace/plugins/stats/src/proc/linux.ts +4 -0
- package/marketplace/plugins/stats/src/proc/ps.ts +1 -0
- package/marketplace/plugins/stats/src/proc/types.ts +11 -0
- package/marketplace/plugins/stats/src/sampler.test.ts +42 -4
- package/marketplace/plugins/stats/src/sampler.ts +32 -18
- package/marketplace/plugins/stats/src/server.ts +29 -2
- package/marketplace/plugins/stats/src/snapshot.ts +31 -3
- package/package.json +3 -2
- package/src/client/components/common/Modal.tsx +7 -0
- package/src/client/components/common/code-surface/bracket-guide.test.ts +2 -2
- package/src/client/components/common/code-surface/bracket-guide.ts +2 -2
- package/src/client/components/common/code-surface/conflict-markers.ts +5 -5
- package/src/client/components/common/code-surface/extensions.ts +7 -4
- package/src/client/components/common/code-surface/theme.ts +34 -9
- package/src/client/components/common/code-surface/whitespace.test.ts +223 -0
- package/src/client/components/common/code-surface/whitespace.ts +140 -0
- package/src/client/components/common/{server-switcher/index.tsx → server-control/Panel.tsx} +73 -35
- package/src/client/components/common/{server-switcher → server-control}/index.test.tsx +98 -37
- package/src/client/components/common/server-control/index.tsx +118 -0
- package/src/client/components/layout/LeftDock.tsx +1 -0
- package/src/client/components/layout/desktop-layout/TopNavbar.tsx +8 -50
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +1 -0
- package/src/client/components/mobile/RightSidebar.tsx +1 -0
- package/src/client/components/mobile/mobile-main-view/Header.tsx +6 -12
- package/src/client/components/mobile/mobile-session-sidebar/Header.tsx +41 -13
- package/src/client/components/mobile/mobile-session-sidebar/use-dock.ts +1 -0
- package/src/client/components/settings/Modal.tsx +9 -3
- package/src/client/components/settings/ProjectSelectorDropdown.tsx +19 -9
- package/src/client/components/settings/categories/AgentSettings.tsx +27 -6
- package/src/client/components/settings/categories/ProjectSettings.tsx +122 -156
- package/src/client/components/settings/categories/SkillCatalogSettings.tsx +220 -260
- package/src/client/components/settings/categories/SkillSettings.tsx +6 -3
- package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +5 -0
- package/src/client/components/settings/categories/project-settings/AccentPalette.test.tsx +189 -0
- package/src/client/components/settings/categories/project-settings/AccentPalette.tsx +156 -24
- package/src/client/components/settings/categories/project-settings/AddProjectModal.tsx +175 -0
- package/src/client/components/settings/categories/project-settings/DeleteProjectModal.tsx +107 -0
- package/src/client/components/settings/categories/project-settings/DetailsForm.tsx +40 -24
- package/src/client/components/settings/categories/project-settings/DetailsHeader.tsx +17 -7
- package/src/client/components/settings/categories/project-settings/IconGrid.tsx +164 -69
- package/src/client/components/settings/categories/project-settings/ModelDropdown.test.tsx +122 -0
- package/src/client/components/settings/categories/project-settings/ModelDropdown.tsx +124 -53
- package/src/client/components/settings/categories/project-settings/NameInput.tsx +57 -11
- package/src/client/components/settings/categories/project-settings/PathField.tsx +122 -0
- package/src/client/components/settings/categories/project-settings/SidebarList.tsx +100 -59
- package/src/client/components/settings/categories/skill-catalog/GitImportPanel.tsx +253 -0
- package/src/client/components/settings/categories/skill-catalog/SkillCard.tsx +89 -0
- package/src/client/components/settings/categories/skill-catalog/SourceRail.tsx +122 -0
- package/src/client/components/settings/categories/skill-catalog/catalog-ui.test.tsx +179 -0
- package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +208 -0
- package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +137 -232
- package/src/client/components/settings/categories/skill-settings/InstructionsEditor.tsx +84 -0
- package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +123 -87
- package/src/client/components/settings/categories/skill-settings/pane.test.tsx +179 -0
- package/src/client/components/settings/categories/skill-settings/row-meta.ts +58 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +6 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.test.ts +46 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.tsx +15 -6
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +29 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +11 -3
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.test.ts +24 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.ts +6 -2
- package/src/client/components/workspace/editor/viewers/SvgPreview.tsx +18 -2
- package/src/client/components/workspace/editor/viewers/viewers.test.ts +39 -0
- package/src/client/components/workspace/file-explorer/ContextMenu.tsx +18 -5
- package/src/client/components/workspace/file-explorer/Modals.tsx +15 -1
- package/src/client/components/workspace/file-explorer/RowLabel.tsx +68 -0
- package/src/client/components/workspace/file-explorer/TreeItem.tsx +47 -57
- package/src/client/components/workspace/file-explorer/collapsed-chain.test.ts +167 -0
- package/src/client/components/workspace/file-explorer/drag-props.ts +42 -0
- package/src/client/components/workspace/file-explorer/drag.test.ts +43 -0
- package/src/client/components/workspace/file-explorer/index.tsx +21 -20
- package/src/client/components/workspace/model-dropdown/Panel.tsx +7 -2
- package/src/client/components/workspace/model-dropdown/Section.test.tsx +13 -0
- package/src/client/components/workspace/model-dropdown/Section.tsx +5 -1
- package/src/client/components/workspace/panels/builtin.tsx +7 -0
- package/src/client/components/workspace/wiki-panel/NavModal.test.tsx +136 -0
- package/src/client/components/workspace/wiki-panel/NavModal.tsx +94 -0
- package/src/client/components/workspace/wiki-panel/PageView.tsx +16 -10
- package/src/client/components/workspace/wiki-panel/index.tsx +44 -9
- package/src/client/data/settings/omp-schema.json +91 -4
- package/src/client/data/settings/project.ts +9 -28
- package/src/client/data/settings/skill.ts +6 -32
- package/src/client/hooks/chat/timeline/pending-model.ts +28 -0
- package/src/client/hooks/settings/projects/index.ts +294 -0
- package/src/client/hooks/settings/projects.test.ts +237 -0
- package/src/client/hooks/settings/skill-catalog.ts +101 -9
- package/src/client/hooks/settings/skill-git-import.ts +153 -0
- package/src/client/hooks/shell/servers.test.ts +166 -0
- package/src/client/hooks/{desktop → shell}/servers.ts +17 -14
- package/src/client/hooks/ui/updates/shell-download.ts +1 -1
- package/src/client/hooks/ui/updates/shell-update.ts +2 -6
- package/src/client/hooks/workspace/file-row-drag.ts +41 -0
- package/src/client/hooks/workspace/file-tree-actions.ts +25 -1
- package/src/client/hooks/workspace/file-tree-edits.ts +23 -6
- package/src/client/hooks/workspace/root-drop-target.ts +77 -0
- package/src/client/hooks/workspace/unfolded-chains.ts +165 -0
- package/src/client/lib/desktop/dropped-paths.ts +1 -1
- package/src/client/lib/desktop/titlebar.test.ts +12 -3
- package/src/client/lib/desktop/titlebar.ts +1 -1
- package/src/client/lib/shell/desktop.ts +117 -0
- package/src/client/lib/shell/servers.ts +75 -0
- package/src/client/lib/shell/transport.ts +216 -0
- package/src/client/main.tsx +9 -0
- package/src/server/lib/db/schema.ts +11 -0
- package/src/server/lib/fs/git-merge-conflict.test.ts +14 -1
- package/src/server/lib/lifecycle/memory.test.ts +41 -1
- package/src/server/lib/lifecycle/memory.ts +131 -14
- package/src/server/lib/omp/config/skill-sources.ts +49 -0
- package/src/server/lib/omp/config/skills-git-install.ts +155 -0
- package/src/server/lib/omp/config/skills-git-repo.ts +288 -0
- package/src/server/lib/omp/config/skills-git.test.ts +220 -0
- package/src/server/lib/omp/session/subagent-presence-scan.test.ts +137 -0
- package/src/server/lib/omp/session/subagent-presence-scan.ts +120 -0
- package/src/server/lib/omp/session/subagent-presence-store.test.ts +120 -0
- package/src/server/lib/omp/session/subagent-presence-store.ts +65 -0
- package/src/server/lib/omp/session/subagent-presence.ts +52 -15
- package/src/server/lib/workspace/project-model.server.test.ts +102 -0
- package/src/server/lib/workspace/project-model.server.ts +47 -0
- package/src/server/lib/workspace/project-patch.server.ts +156 -0
- package/src/server/routes/agent/new.ts +8 -1
- package/src/server/routes/settings/agents.ts +19 -7
- package/src/server/routes/settings/projects.ts +53 -19
- package/src/server/routes/settings/settings-projects-routes.test.ts +214 -0
- package/src/server/routes/settings/settings-store-routes.test.ts +5 -2
- package/src/server/routes/settings/skills-catalog-actions.ts +195 -0
- package/src/server/routes/settings/skills.test.ts +225 -0
- package/src/server/routes/settings/skills.ts +18 -15
- package/src/shared/lib/fs/folder-chain.test.ts +81 -1
- package/src/shared/lib/fs/folder-chain.ts +84 -0
- package/src/shared/lib/omp/config/schema.test.ts +35 -0
- package/src/shared/lib/omp/session/job-progress.test.ts +37 -1
- package/src/shared/lib/omp/session/job-progress.ts +31 -0
- package/src/shared/lib/omp/session/proc.ts +5 -0
- package/src/shared/lib/omp/session/wait.ts +5 -0
- package/src/shared/lib/ui/mobile-overlay.ts +61 -0
- package/src/shared/lib/ui/svg-scope.test.ts +97 -0
- package/src/shared/lib/ui/svg-scope.ts +134 -0
- package/src/shared/lib/workspace/project-accent.test.ts +69 -0
- package/src/shared/lib/workspace/project-accent.ts +63 -0
- package/src/shared/lib/workspace/project-icon.ts +40 -0
- package/src/shared/lib/workspace/project-model.test.ts +79 -0
- package/src/shared/lib/workspace/project-model.ts +64 -0
- package/src/shared/types/settings/skill.ts +53 -3
- package/src/test-support/file-explorer-disk.test-util.ts +25 -1
- package/src/test-support/file-explorer-drag.test-util.ts +88 -0
- package/src/test-support/file-explorer.test-util.ts +47 -47
- package/dist/client/static/js/index-28pwtt69.css +0 -1
- package/src/client/components/common/code-surface/indent-guide.ts +0 -104
- package/src/client/components/settings/categories/skill-catalog/AddSourceModal.tsx +0 -133
- package/src/client/hooks/desktop/servers.test.ts +0 -73
- package/src/client/lib/desktop/bridge.ts +0 -203
- package/src/shared/lib/code/editor/indent-guide-rule.test.ts +0 -65
- package/src/shared/lib/code/editor/indent-guide-rule.ts +0 -47
|
@@ -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
|
|
11
|
-
* roster — but
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
|
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
|
-
*
|
|
35
|
+
* work it exists to avoid.
|
|
22
36
|
*/
|
|
23
37
|
|
|
24
38
|
import { promises as fs, statSync } from 'fs';
|
|
25
|
-
import {
|
|
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 =
|
|
132
|
+
hasSubagents = await sessionFileHasSubagents(sessionPath);
|
|
97
133
|
} catch {
|
|
98
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The project default model reaching a new chat.
|
|
8
|
+
*
|
|
9
|
+
* "Defaults for new chats" is only a setting if the spawn reads it: the column
|
|
10
|
+
* rendered in two panes while `POST /api/agent/new` fell back to Kabmo's global
|
|
11
|
+
* pick, so every chat started on the global model regardless. These cases pin
|
|
12
|
+
* the resolution — exact root, a worktree/nested cwd under the root, a folder
|
|
13
|
+
* with no model (inherit), and a legacy display name that cannot name a model.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
17
|
+
import { mkdtempSync } from 'node:fs';
|
|
18
|
+
import * as os from 'node:os';
|
|
19
|
+
import * as path from 'node:path';
|
|
20
|
+
|
|
21
|
+
import { createDb, type DbClient } from '@/server/lib/db/client';
|
|
22
|
+
import { projectModelForCwd } from '@/server/lib/workspace/project-model.server';
|
|
23
|
+
import { removeDir } from '@/test-support/remove-dir';
|
|
24
|
+
|
|
25
|
+
const ROOT = `${os.tmpdir()}/kabmo-project-model-${process.pid}`;
|
|
26
|
+
|
|
27
|
+
let workRoot = '';
|
|
28
|
+
let db: DbClient;
|
|
29
|
+
|
|
30
|
+
beforeAll(async () => {
|
|
31
|
+
workRoot = mkdtempSync(path.join(os.tmpdir(), 'kabmo-project-model-'));
|
|
32
|
+
db = createDb(path.join(workRoot, 'db.sqlite'));
|
|
33
|
+
await db.exec(`
|
|
34
|
+
CREATE TABLE workspace_folders (
|
|
35
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
36
|
+
name TEXT NOT NULL,
|
|
37
|
+
project_path TEXT,
|
|
38
|
+
model TEXT
|
|
39
|
+
);
|
|
40
|
+
`);
|
|
41
|
+
await db.run('INSERT INTO workspace_folders (name, project_path, model) VALUES (?, ?, ?)', [
|
|
42
|
+
'kenari-project',
|
|
43
|
+
ROOT,
|
|
44
|
+
'kenari:deepseek-v4-flash',
|
|
45
|
+
]);
|
|
46
|
+
await db.run('INSERT INTO workspace_folders (name, project_path, model) VALUES (?, ?, ?)', [
|
|
47
|
+
'legacy',
|
|
48
|
+
`${ROOT}-legacy`,
|
|
49
|
+
'GPT-4o',
|
|
50
|
+
]);
|
|
51
|
+
await db.run('INSERT INTO workspace_folders (name, project_path, model) VALUES (?, ?, ?)', [
|
|
52
|
+
'unset',
|
|
53
|
+
`${ROOT}-unset`,
|
|
54
|
+
'Not selected',
|
|
55
|
+
]);
|
|
56
|
+
await db.run('INSERT INTO workspace_folders (name, project_path, model) VALUES (?, ?, ?)', [
|
|
57
|
+
'nested',
|
|
58
|
+
`${ROOT}/nested`,
|
|
59
|
+
'openai:gpt-5',
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterAll(() => {
|
|
64
|
+
db.raw.close();
|
|
65
|
+
removeDir(workRoot);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('projectModelForCwd', () => {
|
|
69
|
+
test('resolves the bound root to the composite model the picker stored', async () => {
|
|
70
|
+
expect(await projectModelForCwd(db, ROOT)).toEqual({
|
|
71
|
+
provider: 'kenari',
|
|
72
|
+
modelId: 'deepseek-v4-flash',
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('a cwd nested under the root (a worktree) resolves to the same project', async () => {
|
|
77
|
+
expect(await projectModelForCwd(db, `${ROOT}/.worktrees/feature-x`)).toEqual({
|
|
78
|
+
provider: 'kenari',
|
|
79
|
+
modelId: 'deepseek-v4-flash',
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('the schema default and a legacy display name both yield no override', async () => {
|
|
84
|
+
expect(await projectModelForCwd(db, `${ROOT}-unset`)).toBeNull();
|
|
85
|
+
// A display name cannot name a model: falling back to Kabmo's pick is
|
|
86
|
+
// honest, and inventing a provider would start the chat on the wrong model.
|
|
87
|
+
expect(await projectModelForCwd(db, `${ROOT}-legacy`)).toBeNull();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('a nested workspace wins over the root that contains it', async () => {
|
|
91
|
+
expect(await projectModelForCwd(db, `${ROOT}/nested/crate`)).toEqual({
|
|
92
|
+
provider: 'openai',
|
|
93
|
+
modelId: 'gpt-5',
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('an unregistered cwd and an empty one yield no override', async () => {
|
|
98
|
+
expect(await projectModelForCwd(db, '/nowhere/at/all')).toBeNull();
|
|
99
|
+
expect(await projectModelForCwd(db, '')).toBeNull();
|
|
100
|
+
expect(await projectModelForCwd(db, null)).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
});
|