@kabmo-labs/kabmo 0.11.0 → 0.11.1

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.
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The recovery for a session whose RECORDED model no longer exists.
8
+ *
9
+ * `omp --resume <file>` restores the model the session's last `model_change`
10
+ * entry names, and it does NOT fall back when that model is gone: the child
11
+ * exits(1) with "Could not restore model <provider>/<id>", and EVERY command
12
+ * to that session fails with it — the `set_model` a model change posts, the
13
+ * `get_state` warmup, and the `prompt` alike.
14
+ *
15
+ * Measured on omp 18.13.1: a session whose last `model_change` named a
16
+ * provider that is not in the registry (`deepseek/deepseek-flash`) could not
17
+ * receive a prompt at all, and the user-visible shape was "I changed the
18
+ * model, then sent a prompt to the previous session, and the prompt never
19
+ * arrived".
20
+ *
21
+ * These cases pin the two halves of the recovery:
22
+ * - the FAILURE is recognised from omp's own message, and nothing else is;
23
+ * - the RESPAWN carries `--model <a model omp can resolve>`, which is what
24
+ * makes the resume succeed (verified against the real omp binary).
25
+ */
26
+
27
+ import { describe, expect, test } from 'bun:test';
28
+
29
+ import { isUnrestorableModelFailure } from '@/server/lib/omp/session/resumable-model.server';
30
+ import { buildSessionSpawnArgs } from '@/server/lib/omp/rpc/constants';
31
+
32
+ describe('the unrestorable-model failure', () => {
33
+ // omp's own wording, as it appears in the child's stderr tail.
34
+ const restoreFailure = 'omp exited (code 1, signal none): error: Could not restore model deepseek/deepseek-flash\n';
35
+
36
+ test('is recognised from omp\'s own sentence', () => {
37
+ expect(isUnrestorableModelFailure(new Error(restoreFailure))).toBe(true);
38
+ });
39
+
40
+ test('is recognised from a bare message too', () => {
41
+ expect(isUnrestorableModelFailure(new Error('Could not restore model kenari/gone'))).toBe(true);
42
+ });
43
+
44
+ test('another spawn failure is NOT this one', () => {
45
+ // A wrong cwd, a missing binary, a crash: each needs its own report, and
46
+ // respawning with a different model would hide the real cause.
47
+ expect(isUnrestorableModelFailure(new Error('omp exited (code 1): spawn ENOENT'))).toBe(false);
48
+ expect(isUnrestorableModelFailure(new Error('omp binary not found'))).toBe(false);
49
+ expect(isUnrestorableModelFailure(new Error('omp exited (code 137, signal SIGKILL)'))).toBe(false);
50
+ });
51
+
52
+ test('a non-Error throw is judged by its text', () => {
53
+ expect(isUnrestorableModelFailure('Could not restore model a/b')).toBe(true);
54
+ expect(isUnrestorableModelFailure(undefined)).toBe(false);
55
+ });
56
+ });
57
+
58
+ describe('the resume model on the spawn', () => {
59
+ test('is named so omp restores a model that resolves', () => {
60
+ const args = buildSessionSpawnArgs('/tmp/session.jsonl', 'always-ask', {
61
+ provider: 'kenari',
62
+ modelId: 'deepseek-v4-flash',
63
+ });
64
+ expect(args).toContain('--model');
65
+ expect(args[args.indexOf('--model') + 1]).toBe('kenari/deepseek-v4-flash');
66
+ });
67
+
68
+ test('is omitted when there is no replacement', () => {
69
+ // A healthy session is spawned exactly as before: omp restores the
70
+ // transcript's own model, and no catalog is read for it.
71
+ expect(buildSessionSpawnArgs('/tmp/session.jsonl', 'always-ask')).not.toContain('--model');
72
+ });
73
+
74
+ test('a half-specified model is not passed on', () => {
75
+ const args = buildSessionSpawnArgs('/tmp/session.jsonl', 'always-ask', {
76
+ provider: 'kenari',
77
+ modelId: '',
78
+ });
79
+ expect(args).not.toContain('--model');
80
+ });
81
+
82
+ test('the resume file and the approval mode are untouched', () => {
83
+ const args = buildSessionSpawnArgs('/tmp/session.jsonl', 'yolo', {
84
+ provider: 'kenari',
85
+ modelId: 'deepseek-v4-flash',
86
+ });
87
+ expect(args[args.indexOf('--resume') + 1]).toBe('/tmp/session.jsonl');
88
+ expect(args[args.indexOf('--approval-mode') + 1]).toBe('yolo');
89
+ });
90
+ });
@@ -0,0 +1,181 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Recovering a session whose RECORDED model no longer exists.
8
+ *
9
+ * ## The failure
10
+ *
11
+ * `omp --resume <file>` restores the model the session's last `model_change`
12
+ * entry names. When that model is no longer in the registry — a provider the
13
+ * user disconnected, a model removed from `models.yml`, a provider added to
14
+ * `disabledProviders` — omp does NOT fall back: the child exits(1) with
15
+ * `error: Could not restore model <provider>/<id>`.
16
+ *
17
+ * Measured on this machine (omp 18.13.1): a session whose last `model_change`
18
+ * was `deepseek/deepseek-flash` (a provider that is not in the registry at all)
19
+ * made every later command to that session fail with that error — the `set_model`
20
+ * a model change posts, the `get_state` warmup, and the `prompt` alike. The
21
+ * user-visible shape is "I changed the model, then sent a prompt to the
22
+ * previous session, and the prompt never arrived": the send never reached a
23
+ * model because the child could not even start.
24
+ *
25
+ * ## The recovery
26
+ *
27
+ * The session is RESPAWNED with `--model <a model omp can resolve>`. The
28
+ * command that triggered the spawn then runs against the new child, so the
29
+ * user's own `set_model` still lands and the pick they made is what the
30
+ * session ends up running.
31
+ *
32
+ * The respawn happens ONLY when the child died for this reason: a healthy
33
+ * session is spawned exactly as before, and no catalog is read (the listing
34
+ * costs ~1.3 s).
35
+ *
36
+ * ## Why the CLI listing is the oracle
37
+ *
38
+ * The set of models omp can actually resolve is its OWN listing
39
+ * (`omp models --json --kind all`), not the chat picker: the picker filters
40
+ * `disabledProviders` and per-model hides out, so a model the picker once
41
+ * offered can be absent from it while omp still resolves it, and a model the
42
+ * picker never offered (a `kind` other than `chat`) can still be what the
43
+ * transcript names. The listing is the union of everything omp can run, which
44
+ * is exactly the question "can this session's model be restored?".
45
+ */
46
+
47
+ import { getDb } from '@/server/db.server';
48
+ import { readSettingsJson } from '@/server/lib/db/settings-store';
49
+ import { childEnv } from '@/server/lib/fs/child-env';
50
+ import { resolveOmpBin } from '@/server/lib/omp/core/cli';
51
+ import { readModelRoles } from '@/server/lib/omp/config/roles';
52
+ import type { SelectedModel } from '@/shared/types/models';
53
+
54
+ /** How long the CLI listing stays usable. */
55
+ const CATALOG_TTL_MS = 60_000;
56
+ const CATALOG_TIMEOUT_MS = 30_000;
57
+
58
+ /** The app_settings row the model dropdown writes on every pick. */
59
+ const SELECTED_MODEL_KEY = 'omp_selected_model';
60
+
61
+ declare global {
62
+ // eslint-disable-next-line no-var
63
+ var __kabmoResolvableModels: { at: number; selectors: Set<string> } | undefined;
64
+ }
65
+
66
+ /** `provider/id` → the selector omp's own listing reports. */
67
+ function selectorOf(model: SelectedModel): string {
68
+ return `${model.provider}/${model.modelId}`;
69
+ }
70
+
71
+ /**
72
+ * Every model omp can resolve, as `provider/id` selectors.
73
+ *
74
+ * `null` when the listing could not be read — the caller then has no basis for
75
+ * a replacement and the spawn failure is reported as it is.
76
+ */
77
+ async function readResolvableSelectors(): Promise<Set<string> | null> {
78
+ const cached = globalThis.__kabmoResolvableModels;
79
+ if (cached && cached.at + CATALOG_TTL_MS > Date.now()) return cached.selectors;
80
+
81
+ const bin = resolveOmpBin();
82
+ if (!bin) return null;
83
+ try {
84
+ const proc = Bun.spawn({
85
+ env: childEnv(),
86
+ cmd: [bin, 'models', '--json', '--kind', 'all'],
87
+ stdout: 'pipe',
88
+ stderr: 'ignore',
89
+ timeout: CATALOG_TIMEOUT_MS,
90
+ maxBuffer: 32 * 1024 * 1024,
91
+ windowsHide: true,
92
+ });
93
+ const stdout = await new Response(proc.stdout).text();
94
+ if ((await proc.exited) !== 0) return null;
95
+ const parsed = JSON.parse(stdout) as { models?: unknown };
96
+ if (!Array.isArray(parsed.models)) return null;
97
+ const selectors = new Set<string>();
98
+ for (const entry of parsed.models) {
99
+ if (typeof entry !== 'object' || entry === null) continue;
100
+ const row = entry as { provider?: unknown; id?: unknown };
101
+ if (typeof row.provider !== 'string' || typeof row.id !== 'string') continue;
102
+ selectors.add(`${row.provider}/${row.id}`);
103
+ }
104
+ globalThis.__kabmoResolvableModels = { at: Date.now(), selectors };
105
+ return selectors;
106
+ } catch {
107
+ return null;
108
+ }
109
+ }
110
+
111
+ /** Drop the cached listing (a provider or model changed under us). */
112
+ export function invalidateResolvableModels(): void {
113
+ globalThis.__kabmoResolvableModels = undefined;
114
+ }
115
+
116
+ /**
117
+ * True when a child died because omp could not restore the session's model.
118
+ *
119
+ * The message is omp's own (`error: Could not restore model <provider>/<id>`),
120
+ * carried in the process's stderr tail by the exit error.
121
+ */
122
+ export function isUnrestorableModelFailure(error: unknown): boolean {
123
+ const message = error instanceof Error ? error.message : String(error);
124
+ return /Could not restore model\s+\S+/.test(message);
125
+ }
126
+
127
+ /** The model the model dropdown last recorded, as a `provider`/`modelId` pair. */
128
+ async function readPersistedPick(): Promise<SelectedModel | null> {
129
+ try {
130
+ const db = await getDb();
131
+ const stored = await readSettingsJson<unknown>(db, SELECTED_MODEL_KEY, null);
132
+ if (typeof stored !== 'object' || stored === null) return null;
133
+ const row = stored as { provider?: unknown; modelId?: unknown; id?: unknown };
134
+ const provider = typeof row.provider === 'string' ? row.provider : '';
135
+ const modelId = typeof row.modelId === 'string'
136
+ ? row.modelId
137
+ : typeof row.id === 'string' ? row.id : '';
138
+ if (!provider || !modelId) return null;
139
+ return { provider, modelId };
140
+ } catch {
141
+ return null;
142
+ }
143
+ }
144
+
145
+ /** The registry's default model (`config.yml`'s `modelRoles.default`). */
146
+ async function readRegistryDefault(): Promise<SelectedModel | null> {
147
+ try {
148
+ const { roles } = await readModelRoles();
149
+ const selector = roles.default;
150
+ if (!selector) return null;
151
+ const slash = selector.indexOf('/');
152
+ if (slash <= 0 || slash === selector.length - 1) return null;
153
+ return { provider: selector.slice(0, slash), modelId: selector.slice(slash + 1) };
154
+ } catch {
155
+ return null;
156
+ }
157
+ }
158
+
159
+ /**
160
+ * A model omp can resolve, to RESUME a session with.
161
+ *
162
+ * The order is the caller's own candidate first (the model the command being
163
+ * dispatched asks for — the operator's pick in the composer), then the model
164
+ * the dropdown last recorded, then the registry's default. The session's
165
+ * RECORDED model is deliberately not a candidate: it is the one that could not
166
+ * be restored, and offering it again would repeat the same failure.
167
+ */
168
+ export async function resolveResumableModel(
169
+ preferred?: SelectedModel | null,
170
+ ): Promise<SelectedModel | null> {
171
+ const selectors = await readResolvableSelectors();
172
+ // The listing could not be read: there is no basis for a replacement, and a
173
+ // guess would swap the session's model for an arbitrary one.
174
+ if (!selectors) return null;
175
+
176
+ for (const candidate of [preferred ?? null, await readPersistedPick(), await readRegistryDefault()]) {
177
+ if (!candidate) continue;
178
+ if (selectors.has(selectorOf(candidate))) return candidate;
179
+ }
180
+ return null;
181
+ }
@@ -34,7 +34,7 @@
34
34
  * see `standby.server.test.ts`.
35
35
  */
36
36
 
37
- import { restartPrewarmedSessions } from '@/server/lib/omp/rpc/session-registry';
37
+ import { restartPrewarmedSessions } from '@/server/lib/omp/rpc/prewarm-pool';
38
38
  import { restartUtilityProcesses } from '@/server/lib/omp/rpc/utility';
39
39
  import { invalidateSkillCache } from '@/server/lib/omp/config/skills';
40
40
  import { invalidateOmpCliCache } from '@/server/lib/omp/core/cli';
@@ -127,7 +127,13 @@ export async function sendCommand({ params, request }: ActionFunctionArgs) {
127
127
  // Kabmo restart, a second instance, or a CLI-driven session all agree.
128
128
  const persisted = body.modes !== undefined ? parseModeSelection(body as Record<string, unknown>) : await loadPersistedModes(filePath);
129
129
  const modeEnv = kabmoModeEnv({ plan: persisted.plan, goal: persisted.goal });
130
- const { session } = await startRpcSession(sessionId, filePath, cwd, recordedCwd, spawnMode, modeEnv);
130
+ // The model this command ASKS for is the best candidate to RESUME with: a
131
+ // `set_model` carries the operator's own pick, and a session whose
132
+ // recorded model no longer exists needs a replacement that resolves.
133
+ const asked = body.type === 'set_model' && typeof body.provider === 'string' && typeof body.modelId === 'string'
134
+ ? { provider: body.provider, modelId: body.modelId }
135
+ : undefined;
136
+ const { session } = await startRpcSession(sessionId, filePath, cwd, recordedCwd, spawnMode, modeEnv, asked);
131
137
  const result = await session.send(forwarded);
132
138
  // Same Resume nudge as the live path: a resumed goal must not sit idle
133
139
  // waiting for the operator's next message.
@@ -1,6 +1,6 @@
1
1
  import { json } from '@/server/lib/remix-compat';
2
2
  import type { ActionFunctionArgs } from '@/server/lib/remix-compat';
3
- import { prewarmRpcSession } from '@/server/lib/omp/rpc/manager';
3
+ import { prewarmRpcSession } from '@/server/lib/omp/rpc/prewarm-pool';
4
4
  import { isApprovalMode } from '@/shared/lib/omp/config/access-mode';
5
5
  import { loadPersistedAccessMode } from '@/shared/lib/omp/config/access-mode.server';
6
6
  import { rpcErrorResponse } from '@/server/lib/omp/rpc/errors';
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * @license
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ /**
8
+ * A scripted `omp` RPC child that refuses to start a resume whose session
9
+ * records a model it cannot restore — the way the real binary does.
10
+ *
11
+ * Measured on omp 18.13.1: `omp --resume <file>` restores the model the
12
+ * session's last `model_change` entry names, and it does NOT fall back when
13
+ * that model is gone. The child exits(1) with
14
+ * `error: Could not restore model <provider>/<id>`, so EVERY command to that
15
+ * session fails — the `set_model` a model change posts, the `get_state`
16
+ * warmup, and the `prompt` alike.
17
+ *
18
+ * `--model <selector>` makes the resume start (verified against the real
19
+ * binary), which is the lever Kabmo's spawn uses to recover.
20
+ *
21
+ * Two modes, chosen by argv:
22
+ * - `models --json --kind all` answers the catalog Kabmo reads to decide
23
+ * which models omp can still resolve (`FAKE_OMP_RESTORE_MODELS` is a
24
+ * comma-separated `provider/id` list).
25
+ * - otherwise it speaks the RPC wire like the other fixtures.
26
+ *
27
+ * It also appends every received ARGUMENT LIST to `FAKE_OMP_RESTORE_LOG`, so a
28
+ * test can assert what Kabmo actually spawned (`--model` present or not)
29
+ * instead of inferring it.
30
+ */
31
+
32
+ import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs';
33
+ import { dirname, join } from 'node:path';
34
+
35
+ const argv = process.argv.slice(2);
36
+ const flag = (name: string): string | undefined => {
37
+ const index = argv.indexOf(name);
38
+ return index >= 0 ? argv[index + 1] : undefined;
39
+ };
40
+
41
+ /** The scripts' own `models` listing. */
42
+ if (argv[0] === 'models') {
43
+ const listed = (process.env.FAKE_OMP_RESTORE_MODELS ?? '')
44
+ .split(',')
45
+ .map((entry) => entry.trim())
46
+ .filter(Boolean)
47
+ .map((selector) => {
48
+ const slash = selector.indexOf('/');
49
+ const provider = slash > 0 ? selector.slice(0, slash) : '';
50
+ const modelId = slash > 0 ? selector.slice(slash + 1) : selector;
51
+ return { provider, id: modelId, kind: 'chat', name: modelId };
52
+ });
53
+ process.stdout.write(`\n${JSON.stringify({ models: listed })}\n`);
54
+ process.exit(0);
55
+ }
56
+
57
+ const logPath = process.env.FAKE_OMP_RESTORE_LOG;
58
+ if (logPath) {
59
+ try {
60
+ appendFileSync(logPath, `${JSON.stringify(argv)}\n`);
61
+ } catch {
62
+ // Logging is a test convenience; never break the child.
63
+ }
64
+ }
65
+
66
+ const agentDir = process.env.PI_CODING_AGENT_DIR ?? join(process.cwd(), '.fake-omp-restore');
67
+ const resume = flag('--resume');
68
+ const model = flag('--model');
69
+
70
+ // The failure under test: a resume WITHOUT an explicit model, over a session
71
+ // whose recorded model the catalog does not serve. The message is omp's own.
72
+ const restoreFailure = process.env.FAKE_OMP_RESTORE_FAILS === '1';
73
+ if (resume && restoreFailure && !model) {
74
+ process.stderr.write('error: Could not restore model deepseek/deepseek-flash\n');
75
+ process.exit(1);
76
+ }
77
+
78
+ const sessionId = resume
79
+ ? (resume.split('/').pop() ?? '').replace(/\.jsonl$/, '')
80
+ : crypto.randomUUID();
81
+ const sessionFile = resume ?? join(agentDir, 'sessions', `${sessionId}.jsonl`);
82
+ mkdirSync(dirname(sessionFile), { recursive: true });
83
+ writeFileSync(sessionFile, '');
84
+
85
+ const encoder = new TextEncoder();
86
+ const write = (frame: unknown) => {
87
+ process.stdout.write(encoder.encode(`${JSON.stringify(frame)}\n`));
88
+ };
89
+
90
+ const respond = (id: unknown, command: string, data: unknown, success = true) => {
91
+ write({ type: 'response', id, command, success, ...(success ? { data } : { error: 'fake' }) });
92
+ };
93
+
94
+ const state = () => ({
95
+ sessionId,
96
+ sessionFile,
97
+ sessionName: 'Restore fixture',
98
+ isStreaming: false,
99
+ isSettled: true,
100
+ isCompacting: false,
101
+ autoCompactionEnabled: true,
102
+ model: model
103
+ ? {
104
+ id: model.slice(model.indexOf('/') + 1),
105
+ provider: model.slice(0, model.indexOf('/')),
106
+ name: 'Restored',
107
+ reasoning: false,
108
+ }
109
+ : { id: 'restored-model', provider: 'fake', name: 'Restored', reasoning: false },
110
+ messageCount: 0,
111
+ queuedMessageCount: 0,
112
+ contextUsage: { tokens: 0, contextWindow: 100_000, percent: 0 },
113
+ thinkingLevel: 'medium',
114
+ });
115
+
116
+ const decoder = new TextDecoder();
117
+ let buffer = '';
118
+ const stdin = Bun.stdin.stream().getReader();
119
+
120
+ write({ type: 'ready', protocolVersion: 2 });
121
+
122
+ for (;;) {
123
+ const { done, value } = await stdin.read();
124
+ if (done) break;
125
+ buffer += decoder.decode(value, { stream: true });
126
+ let newline = buffer.indexOf('\n');
127
+ while (newline !== -1) {
128
+ const line = buffer.slice(0, newline).trim();
129
+ buffer = buffer.slice(newline + 1);
130
+ newline = buffer.indexOf('\n');
131
+ if (!line) continue;
132
+ let frame: Record<string, unknown>;
133
+ try {
134
+ frame = JSON.parse(line) as Record<string, unknown>;
135
+ } catch {
136
+ continue;
137
+ }
138
+ const id = frame.id;
139
+ switch (frame.type) {
140
+ case 'get_state':
141
+ respond(id, 'get_state', state());
142
+ break;
143
+ case 'prompt':
144
+ respond(id, 'prompt', { agentInvoked: true });
145
+ write({ type: 'agent_start' });
146
+ write({ type: 'agent_end', isTerminal: true });
147
+ break;
148
+ default:
149
+ respond(id, String(frame.type), {});
150
+ break;
151
+ }
152
+ }
153
+ }