@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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +369 -369
- package/dist/client/static/js/{index-nqvfpgm2.js → index-amss8pge.js} +151 -151
- package/package.json +1 -1
- package/src/client/hooks/chat/timeline/index.ts +1 -0
- package/src/client/hooks/chat/timeline/send-execute.test.ts +14 -207
- package/src/client/hooks/chat/timeline/send-refusal.test.ts +72 -0
- package/src/client/hooks/chat/timeline/send-steer.test.ts +1 -0
- package/src/client/hooks/chat/timeline/send.test-util.ts +245 -0
- package/src/client/hooks/chat/timeline/send.test.ts +1 -0
- package/src/client/hooks/chat/timeline/send.ts +10 -0
- package/src/server/lib/omp/rpc/constants.ts +15 -1
- package/src/server/lib/omp/rpc/manager.ts +1 -1
- package/src/server/lib/omp/rpc/prewarm-pool.ts +166 -0
- package/src/server/lib/omp/rpc/session-registry.ts +80 -164
- package/src/server/lib/omp/session/resumable-model.recovery.test.ts +194 -0
- package/src/server/lib/omp/session/resumable-model.server.test.ts +90 -0
- package/src/server/lib/omp/session/resumable-model.server.ts +181 -0
- package/src/server/lib/omp/session/standby.server.ts +1 -1
- package/src/server/routes/agent/command.ts +7 -1
- package/src/server/routes/agent/prewarm.ts +1 -1
- package/src/test-support/fake-omp-restore.ts +153 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The prewarm pool: one idle "new session" spawned ahead of the first prompt
|
|
8
|
+
* so adopting it on send skips the multi-second omp boot.
|
|
9
|
+
*
|
|
10
|
+
* Split out of `session-registry.ts` so that file stays under the repo's
|
|
11
|
+
* per-file size ceiling: the pool is a concern of its own (a cache with its
|
|
12
|
+
* own lifecycle), and the registry is the map of LIVE sessions.
|
|
13
|
+
*
|
|
14
|
+
* Keyed per cwd (the spawn's project directory) with its approval mode and
|
|
15
|
+
* mode selection; consumed exactly once by `startNewRpcSession`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { RpcProcess } from '@/server/lib/omp/rpc/process';
|
|
19
|
+
import { buildSessionSpawnArgs } from '@/server/lib/omp/rpc/constants';
|
|
20
|
+
import { AgentSessionWrapper } from '@/server/lib/omp/rpc/manager';
|
|
21
|
+
import { recordSpawnProvenance } from '@/server/lib/omp/rpc/spawn-provenance';
|
|
22
|
+
import { DEFAULT_APPROVAL_MODE, type ApprovalMode } from '@/shared/lib/omp/config/access-mode';
|
|
23
|
+
|
|
24
|
+
interface PrewarmedEntry {
|
|
25
|
+
cwd: string;
|
|
26
|
+
approvalMode: ApprovalMode;
|
|
27
|
+
/** Mode selection the entry was spawned with; a mismatch makes it unusable. */
|
|
28
|
+
modeEnv?: Record<string, string>;
|
|
29
|
+
/** Resolves when the wrapper is ready, or rejects when the spawn failed or
|
|
30
|
+
* the entry was torn down before being claimed. */
|
|
31
|
+
ready: Promise<AgentSessionWrapper>;
|
|
32
|
+
claimed: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare global {
|
|
36
|
+
// eslint-disable-next-line no-var
|
|
37
|
+
var __ompPrewarmed: Map<string, PrewarmedEntry> | undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getPrewarmed(): Map<string, PrewarmedEntry> {
|
|
41
|
+
if (!globalThis.__ompPrewarmed) globalThis.__ompPrewarmed = new Map();
|
|
42
|
+
return globalThis.__ompPrewarmed;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Spawn (at most one) idle omp process for `cwd` so the next new-session
|
|
46
|
+
* send starts instantly. Fire-and-forget; a failed spawn just clears itself.
|
|
47
|
+
* Safe to call repeatedly — an existing live or in-flight entry wins. */
|
|
48
|
+
export function prewarmRpcSession(cwd: string, approvalMode?: ApprovalMode, modeEnv?: Record<string, string>): void {
|
|
49
|
+
const pool = getPrewarmed();
|
|
50
|
+
const existing = pool.get(cwd);
|
|
51
|
+
if (existing) return;
|
|
52
|
+
const mode = approvalMode ?? DEFAULT_APPROVAL_MODE;
|
|
53
|
+
// A pending chat has no mode selection yet, so its prewarmed process is
|
|
54
|
+
// spawned with none — the first send races it and any real selection kills
|
|
55
|
+
// the entry (see `startNewRpcSession`).
|
|
56
|
+
const holder: { wrapper?: AgentSessionWrapper } = {};
|
|
57
|
+
const proc = new RpcProcess({
|
|
58
|
+
cwd,
|
|
59
|
+
extraArgs: buildSessionSpawnArgs('', mode),
|
|
60
|
+
env: modeEnv,
|
|
61
|
+
onExit: ({ stderrTail }) => holder.wrapper?.handleProcessExit(stderrTail),
|
|
62
|
+
});
|
|
63
|
+
const wrapper = new AgentSessionWrapper(proc, cwd);
|
|
64
|
+
recordSpawnProvenance(wrapper, mode, modeEnv);
|
|
65
|
+
holder.wrapper = wrapper;
|
|
66
|
+
wrapper.start();
|
|
67
|
+
const ready = wrapper
|
|
68
|
+
.waitUntilReady()
|
|
69
|
+
.then(() => {
|
|
70
|
+
// A dead wrapper (idle-kill, crash, exit) must not be handed out.
|
|
71
|
+
if (!wrapper.isAlive()) throw new Error('prewarmed process exited');
|
|
72
|
+
return wrapper;
|
|
73
|
+
})
|
|
74
|
+
.catch((error) => {
|
|
75
|
+
void wrapper.destroyAndWait();
|
|
76
|
+
const entry = pool.get(cwd);
|
|
77
|
+
if (entry?.ready === ready) pool.delete(cwd);
|
|
78
|
+
throw error;
|
|
79
|
+
});
|
|
80
|
+
pool.set(cwd, { cwd, approvalMode: mode, modeEnv, ready, claimed: false });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Recycle every UNCLAIMED prewarmed process.
|
|
85
|
+
*
|
|
86
|
+
* A prewarmed child is a full session host that has no session yet, spawned
|
|
87
|
+
* from whatever binary was current when the user opened a pending chat. After
|
|
88
|
+
* `omp update` it is a stale build waiting to be adopted by the next send, so
|
|
89
|
+
* the recycle happens here rather than being left to the idle timer.
|
|
90
|
+
*
|
|
91
|
+
* Safe because there is nothing to lose: an unclaimed entry owns no session and
|
|
92
|
+
* no turn. An entry a send is already adopting is left alone — the claim is a
|
|
93
|
+
* synchronous check-and-set (`startNewRpcSession`), so an entry is either taken
|
|
94
|
+
* before this pass reads it or taken after this pass removed it, never both.
|
|
95
|
+
* A wrapper still booting is destroyed through `destroyAndWait`, which is also
|
|
96
|
+
* the error path the prewarm itself would have taken.
|
|
97
|
+
*/
|
|
98
|
+
export async function restartPrewarmedSessions(): Promise<number> {
|
|
99
|
+
const pool = getPrewarmed();
|
|
100
|
+
if (pool.size === 0) return 0;
|
|
101
|
+
const doomed: PrewarmedEntry[] = [];
|
|
102
|
+
for (const [cwd, entry] of pool) {
|
|
103
|
+
if (entry.claimed) continue;
|
|
104
|
+
entry.claimed = true;
|
|
105
|
+
pool.delete(cwd);
|
|
106
|
+
doomed.push(entry);
|
|
107
|
+
}
|
|
108
|
+
await Promise.all(doomed.map(async (entry) => {
|
|
109
|
+
try {
|
|
110
|
+
const wrapper = await entry.ready;
|
|
111
|
+
await wrapper.destroyAndWait();
|
|
112
|
+
} catch {
|
|
113
|
+
// A spawn that failed already tore its own wrapper down; nothing to do.
|
|
114
|
+
}
|
|
115
|
+
}));
|
|
116
|
+
return doomed.length;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Take the prewarmed process for `cwd` when it can serve the request.
|
|
121
|
+
*
|
|
122
|
+
* Returns the wrapper, or null when there is none to take: no entry, an entry
|
|
123
|
+
* whose approval mode or mode selection differs from the request's, or an
|
|
124
|
+
* entry that died before it was claimed. The mismatching entry is destroyed —
|
|
125
|
+
* it cannot serve this request — and the caller spawns cold.
|
|
126
|
+
*/
|
|
127
|
+
export async function claimPrewarmedSession(
|
|
128
|
+
cwd: string,
|
|
129
|
+
approvalMode: ApprovalMode,
|
|
130
|
+
modeEnv?: Record<string, string>,
|
|
131
|
+
): Promise<AgentSessionWrapper | null> {
|
|
132
|
+
const pool = getPrewarmed();
|
|
133
|
+
const entry = pool.get(cwd);
|
|
134
|
+
if (!entry) return null;
|
|
135
|
+
|
|
136
|
+
// A prewarmed entry was spawned with the environment current at prewarm
|
|
137
|
+
// time, so a MODE selection that differs from its own can no longer be served
|
|
138
|
+
// by it — the extension reads the mode env once, at session start. Treated
|
|
139
|
+
// exactly like a mode mismatch: claim and kill, then spawn cold.
|
|
140
|
+
const entryModes = entry.modeEnv?.KABMO_MODES ?? entry.modeEnv?.CHAMBER_MODES ?? '';
|
|
141
|
+
const wantedModes = modeEnv?.KABMO_MODES ?? modeEnv?.CHAMBER_MODES ?? '';
|
|
142
|
+
const usable = !entry.claimed && entry.approvalMode === approvalMode && entryModes === wantedModes;
|
|
143
|
+
|
|
144
|
+
if (usable) {
|
|
145
|
+
entry.claimed = true;
|
|
146
|
+
pool.delete(cwd);
|
|
147
|
+
try {
|
|
148
|
+
const candidate = await entry.ready;
|
|
149
|
+
// Died between readiness and claim (idle kill, crash) → cold spawn.
|
|
150
|
+
if (candidate.isAlive()) return candidate;
|
|
151
|
+
await candidate.destroyAndWait();
|
|
152
|
+
} catch {
|
|
153
|
+
// fall through to a cold spawn
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (entry.claimed) return null;
|
|
159
|
+
// Wrong approval mode, or a different session-mode selection: the prewarmed
|
|
160
|
+
// process cannot serve this request. Kill it and spawn cold —
|
|
161
|
+
// reconcileSpawnApprovalMode would refuse (no session file).
|
|
162
|
+
entry.claimed = true;
|
|
163
|
+
pool.delete(cwd);
|
|
164
|
+
void entry.ready.then((w) => w.destroyAndWait()).catch(() => {});
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
@@ -20,7 +20,10 @@ import { localRuns } from '@/server/lib/omp/session/local-runs.server';
|
|
|
20
20
|
import { recordSpawnProvenance } from '@/server/lib/omp/rpc/spawn-provenance';
|
|
21
21
|
import { registerSpawnedSession } from '@/server/lib/omp/rpc/session-registration';
|
|
22
22
|
import { resolveSessionOwnership, SessionOwnedElsewhereError } from '@/server/lib/omp/session/ownership.server';
|
|
23
|
+
import { claimPrewarmedSession } from '@/server/lib/omp/rpc/prewarm-pool';
|
|
24
|
+
import { isUnrestorableModelFailure, resolveResumableModel } from '@/server/lib/omp/session/resumable-model.server';
|
|
23
25
|
import { DEFAULT_APPROVAL_MODE, type ApprovalMode } from '@/shared/lib/omp/config/access-mode';
|
|
26
|
+
import type { SelectedModel } from '@/shared/types/models';
|
|
24
27
|
|
|
25
28
|
export { getSpawnApprovalMode, getSpawnModeEnv, reconcileSpawnApprovalMode } from '@/server/lib/omp/rpc/spawn-provenance';
|
|
26
29
|
|
|
@@ -111,6 +114,7 @@ export async function startRpcSession(
|
|
|
111
114
|
recordedCwd?: string | null,
|
|
112
115
|
approvalMode?: ApprovalMode,
|
|
113
116
|
modeEnv?: Record<string, string>,
|
|
117
|
+
resumeModel?: SelectedModel,
|
|
114
118
|
): Promise<{ session: AgentSessionWrapper; realSessionId: string }> {
|
|
115
119
|
const registry = getRegistry();
|
|
116
120
|
const locks = getLocks();
|
|
@@ -137,156 +141,95 @@ export async function startRpcSession(
|
|
|
137
141
|
const inflight = locks.get(sessionId);
|
|
138
142
|
if (inflight) return inflight;
|
|
139
143
|
|
|
140
|
-
const starting = (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
// reconcile attaches its watchers asynchronously, and a caller that wrote a
|
|
165
|
-
// skill the moment the spawn returned would land it in the gap before the
|
|
166
|
-
// watch existed (measured: the write was missed on a fast follow-up and
|
|
167
|
-
// picked up once the attach had settled).
|
|
168
|
-
await syncDiscoveryRootsWatch();
|
|
169
|
-
// A `session:<id>` topic subscribed before this spawn had no child to bind
|
|
170
|
-
// to, so it published nothing and its subscribers hold a `running:false`
|
|
171
|
-
// snapshot. The child exists now: tell the realtime layer to re-snapshot
|
|
172
|
-
// that topic for whoever is watching.
|
|
173
|
-
emitRealtimeSignal('session-attached', realSessionId);
|
|
174
|
-
// A forked resume answers a NEW id, and the client is still subscribed (and
|
|
175
|
-
// still posting) under the id it asked for. Publishing only the real id
|
|
176
|
-
// leaves that subscriber attached to a topic whose `getRpcSession` now
|
|
177
|
-
// resolves — through the alias `registerSpawnedSession` installs — while
|
|
178
|
-
// nothing ever told it to re-snapshot, so a reload-less tab would keep the
|
|
179
|
-
// pre-spawn snapshot.
|
|
180
|
-
if (sessionId !== realSessionId) emitRealtimeSignal('session-attached', sessionId);
|
|
181
|
-
return { session: created, realSessionId };
|
|
182
|
-
})().finally(() => locks.delete(sessionId));
|
|
144
|
+
const starting = spawnOnce(sessionId, sessionFile, cwd, recordedCwd, approvalMode, modeEnv)
|
|
145
|
+
.catch(async (error) => {
|
|
146
|
+
// omp restores the model the session's transcript records, and it does
|
|
147
|
+
// NOT fall back when that model is gone: the child exits(1) with
|
|
148
|
+
// "Could not restore model <provider>/<id>" and EVERY command to the
|
|
149
|
+
// session fails with it. Measured on omp 18.13.1 — a session whose last
|
|
150
|
+
// model_change named a provider that is no longer in the registry could
|
|
151
|
+
// not receive a prompt at all.
|
|
152
|
+
//
|
|
153
|
+
// The recovery respawns it with a model omp CAN resolve. The command
|
|
154
|
+
// that triggered this spawn then runs against the new child, so the
|
|
155
|
+
// user's own `set_model` still lands on it.
|
|
156
|
+
//
|
|
157
|
+
// The first attempt is deliberately left UNCHANGED (no `--model`): a
|
|
158
|
+
// caller's pick can itself be stale — the picker's list is a 60 s
|
|
159
|
+
// snapshot — and naming it up front made a healthy session unopenable
|
|
160
|
+
// ("Model <x> not found"). It is only a CANDIDATE here, and one that has
|
|
161
|
+
// to survive the resolvability check.
|
|
162
|
+
if (!isUnrestorableModelFailure(error)) throw error;
|
|
163
|
+
const replacement = await resolveResumableModel(resumeModel);
|
|
164
|
+
if (!replacement) throw error;
|
|
165
|
+
return spawnOnce(sessionId, sessionFile, cwd, recordedCwd, approvalMode, modeEnv, replacement);
|
|
166
|
+
})
|
|
167
|
+
.finally(() => locks.delete(sessionId));
|
|
183
168
|
|
|
184
169
|
locks.set(sessionId, starting);
|
|
185
170
|
return starting;
|
|
186
171
|
}
|
|
187
172
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
modeEnv?: Record<string, string>;
|
|
200
|
-
/** Resolves when the wrapper is ready, or rejects when the spawn failed or
|
|
201
|
-
* the entry was torn down before being claimed. */
|
|
202
|
-
ready: Promise<AgentSessionWrapper>;
|
|
203
|
-
claimed: boolean;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
declare global {
|
|
207
|
-
// eslint-disable-next-line no-var
|
|
208
|
-
var __ompPrewarmed: Map<string, PrewarmedEntry> | undefined;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function getPrewarmed(): Map<string, PrewarmedEntry> {
|
|
212
|
-
if (!globalThis.__ompPrewarmed) globalThis.__ompPrewarmed = new Map();
|
|
213
|
-
return globalThis.__ompPrewarmed;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/** Spawn (at most one) idle omp process for `cwd` so the next new-session
|
|
217
|
-
* send starts instantly. Fire-and-forget; a failed spawn just clears itself.
|
|
218
|
-
* Safe to call repeatedly — an existing live or in-flight entry wins. */
|
|
219
|
-
export function prewarmRpcSession(cwd: string, approvalMode?: ApprovalMode, modeEnv?: Record<string, string>): void {
|
|
220
|
-
const pool = getPrewarmed();
|
|
221
|
-
const existing = pool.get(cwd);
|
|
222
|
-
if (existing) return;
|
|
223
|
-
const mode = approvalMode ?? DEFAULT_APPROVAL_MODE;
|
|
224
|
-
// A pending chat has no mode selection yet, so its prewarmed process is
|
|
225
|
-
// spawned with none — the first send races it and any real selection kills
|
|
226
|
-
// the entry (see `startNewRpcSession`).
|
|
173
|
+
/** One spawn attempt: build the child, wait for it to be ready, register it. */
|
|
174
|
+
async function spawnOnce(
|
|
175
|
+
sessionId: string,
|
|
176
|
+
sessionFile: string,
|
|
177
|
+
cwd: string,
|
|
178
|
+
recordedCwd: string | null | undefined,
|
|
179
|
+
approvalMode: ApprovalMode | undefined,
|
|
180
|
+
modeEnv: Record<string, string> | undefined,
|
|
181
|
+
resumeModel?: SelectedModel | undefined,
|
|
182
|
+
): Promise<{ session: AgentSessionWrapper; realSessionId: string }> {
|
|
183
|
+
const registry = getRegistry();
|
|
227
184
|
const holder: { wrapper?: AgentSessionWrapper } = {};
|
|
228
185
|
const proc = new RpcProcess({
|
|
229
186
|
cwd,
|
|
230
|
-
extraArgs: buildSessionSpawnArgs(
|
|
187
|
+
extraArgs: buildSessionSpawnArgs(sessionFile, approvalMode, resumeModel),
|
|
231
188
|
env: modeEnv,
|
|
232
189
|
onExit: ({ stderrTail }) => holder.wrapper?.handleProcessExit(stderrTail),
|
|
233
190
|
});
|
|
234
|
-
const
|
|
235
|
-
recordSpawnProvenance(
|
|
236
|
-
holder.wrapper =
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
.waitUntilReady()
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return wrapper;
|
|
244
|
-
})
|
|
245
|
-
.catch((error) => {
|
|
246
|
-
void wrapper.destroyAndWait();
|
|
247
|
-
const entry = pool.get(cwd);
|
|
248
|
-
if (entry?.ready === ready) pool.delete(cwd);
|
|
249
|
-
throw error;
|
|
250
|
-
});
|
|
251
|
-
pool.set(cwd, { cwd, approvalMode: mode, modeEnv, ready, claimed: false });
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Recycle every UNCLAIMED prewarmed process.
|
|
256
|
-
*
|
|
257
|
-
* A prewarmed child is a full session host that has no session yet, spawned
|
|
258
|
-
* from whatever binary was current when the user opened a pending chat. After
|
|
259
|
-
* `omp update` it is a stale build waiting to be adopted by the next send, so
|
|
260
|
-
* the recycle happens here rather than being left to the idle timer.
|
|
261
|
-
*
|
|
262
|
-
* Safe because there is nothing to lose: an unclaimed entry owns no session and
|
|
263
|
-
* no turn. An entry a send is already adopting is left alone — the claim is a
|
|
264
|
-
* synchronous check-and-set (`startNewRpcSession`), so an entry is either taken
|
|
265
|
-
* before this pass reads it or taken after this pass removed it, never both.
|
|
266
|
-
* A wrapper still booting is destroyed through `destroyAndWait`, which is also
|
|
267
|
-
* the error path the prewarm itself would have taken.
|
|
268
|
-
*/
|
|
269
|
-
export async function restartPrewarmedSessions(): Promise<number> {
|
|
270
|
-
const pool = getPrewarmed();
|
|
271
|
-
if (pool.size === 0) return 0;
|
|
272
|
-
const doomed: PrewarmedEntry[] = [];
|
|
273
|
-
for (const [cwd, entry] of pool) {
|
|
274
|
-
if (entry.claimed) continue;
|
|
275
|
-
entry.claimed = true;
|
|
276
|
-
pool.delete(cwd);
|
|
277
|
-
doomed.push(entry);
|
|
191
|
+
const created = new AgentSessionWrapper(proc, cwd, recordedCwd);
|
|
192
|
+
recordSpawnProvenance(created, approvalMode, modeEnv);
|
|
193
|
+
holder.wrapper = created;
|
|
194
|
+
created.start();
|
|
195
|
+
try {
|
|
196
|
+
await created.waitUntilReady();
|
|
197
|
+
} catch (error) {
|
|
198
|
+
await created.destroyAndWait();
|
|
199
|
+
throw error;
|
|
278
200
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
201
|
+
|
|
202
|
+
const realSessionId = created.sessionId;
|
|
203
|
+
registerSpawnedSession(registry, created, sessionId, realSessionId);
|
|
204
|
+
// A session's cwd may sit outside every registered workspace, and omp
|
|
205
|
+
// resolves project skills from it all the same — so the discovery watcher
|
|
206
|
+
// has to learn this directory. AWAITED, not fired and forgotten: the
|
|
207
|
+
// reconcile attaches its watchers asynchronously, and a caller that wrote a
|
|
208
|
+
// skill the moment the spawn returned would land it in the gap before the
|
|
209
|
+
// watch existed (measured: the write was missed on a fast follow-up and
|
|
210
|
+
// picked up once the attach had settled).
|
|
211
|
+
await syncDiscoveryRootsWatch();
|
|
212
|
+
// A `session:<id>` topic subscribed before this spawn had no child to bind
|
|
213
|
+
// to, so it published nothing and its subscribers hold a `running:false`
|
|
214
|
+
// snapshot. The child exists now: tell the realtime layer to re-snapshot
|
|
215
|
+
// that topic for whoever is watching.
|
|
216
|
+
emitRealtimeSignal('session-attached', realSessionId);
|
|
217
|
+
// A forked resume answers a NEW id, and the client is still subscribed (and
|
|
218
|
+
// still posting) under the id it asked for. Publishing only the real id
|
|
219
|
+
// leaves that subscriber attached to a topic whose `getRpcSession` now
|
|
220
|
+
// resolves — through the alias `registerSpawnedSession` installs — while
|
|
221
|
+
// nothing ever told it to re-snapshot, so a reload-less tab would keep the
|
|
222
|
+
// pre-spawn snapshot.
|
|
223
|
+
if (sessionId !== realSessionId) emitRealtimeSignal('session-attached', sessionId);
|
|
224
|
+
return { session: created, realSessionId };
|
|
288
225
|
}
|
|
289
226
|
|
|
227
|
+
// ============================================================================
|
|
228
|
+
// The prewarm pool (one idle "new session" spawned ahead of the first prompt)
|
|
229
|
+
// lives in `prewarm-pool.ts`: it is a cache with its own lifecycle, not part
|
|
230
|
+
// of the live-session map below.
|
|
231
|
+
// ============================================================================
|
|
232
|
+
|
|
290
233
|
/** Consume the prewarmed process for `cwd` when its approval mode matches, or
|
|
291
234
|
* spawn a fresh session. The returned wrapper is already registered under its
|
|
292
235
|
* real session id, mirroring startRpcSession's bookkeeping. */
|
|
@@ -295,35 +238,8 @@ export async function startNewRpcSession(
|
|
|
295
238
|
approvalMode?: ApprovalMode,
|
|
296
239
|
modeEnv?: Record<string, string>,
|
|
297
240
|
): Promise<{ session: AgentSessionWrapper; realSessionId: string }> {
|
|
298
|
-
const pool = getPrewarmed();
|
|
299
|
-
const entry = pool.get(cwd);
|
|
300
241
|
const mode = approvalMode ?? DEFAULT_APPROVAL_MODE;
|
|
301
|
-
|
|
302
|
-
// A prewarmed entry was spawned with the environment current at prewarm
|
|
303
|
-
// time, so a MODE selection that differs from its own can no longer be served
|
|
304
|
-
// by it — the extension reads the mode env once, at session start. Treated
|
|
305
|
-
// exactly like a mode mismatch: claim and kill, then spawn cold.
|
|
306
|
-
const entryModes = entry?.modeEnv?.KABMO_MODES ?? entry?.modeEnv?.CHAMBER_MODES ?? '';
|
|
307
|
-
const wantedModes = modeEnv?.KABMO_MODES ?? modeEnv?.CHAMBER_MODES ?? '';
|
|
308
|
-
if (entry && !entry.claimed && entry.approvalMode === mode && entryModes === wantedModes) {
|
|
309
|
-
entry.claimed = true;
|
|
310
|
-
pool.delete(cwd);
|
|
311
|
-
try {
|
|
312
|
-
const candidate = await entry.ready;
|
|
313
|
-
// Died between readiness and claim (idle kill, crash) → cold spawn.
|
|
314
|
-
if (candidate.isAlive()) wrapper = candidate;
|
|
315
|
-
else await candidate.destroyAndWait();
|
|
316
|
-
} catch {
|
|
317
|
-
wrapper = undefined; // fall through to a cold spawn
|
|
318
|
-
}
|
|
319
|
-
} else if (entry && !entry.claimed) {
|
|
320
|
-
// Wrong approval mode, or a different session-mode selection: the prewarmed
|
|
321
|
-
// process cannot serve this request. Kill it and spawn cold —
|
|
322
|
-
// reconcileSpawnApprovalMode would refuse (no session file).
|
|
323
|
-
entry.claimed = true;
|
|
324
|
-
pool.delete(cwd);
|
|
325
|
-
void entry.ready.then((w) => w.destroyAndWait()).catch(() => {});
|
|
326
|
-
}
|
|
242
|
+
const wrapper = await claimPrewarmedSession(cwd, mode, modeEnv);
|
|
327
243
|
|
|
328
244
|
if (!wrapper) {
|
|
329
245
|
return startRpcSession(`__new__${Bun.randomUUIDv7()}`, '', cwd, undefined, mode, modeEnv);
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A session whose RECORDED model cannot be restored still starts.
|
|
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 `error: Could not restore model <provider>/<id>`, so every
|
|
12
|
+
* command to that session fails with it — the `set_model` a model change
|
|
13
|
+
* posts, the `get_state` warmup, and the `prompt` alike.
|
|
14
|
+
*
|
|
15
|
+
* Measured on omp 18.13.1 against a real session file: the user-visible shape
|
|
16
|
+
* was "I changed the model, then sent a prompt to the previous session, and
|
|
17
|
+
* the prompt never arrived". `--model <a model omp can resolve>` makes the
|
|
18
|
+
* resume start (verified against the real binary), which is the lever the
|
|
19
|
+
* recovery uses.
|
|
20
|
+
*
|
|
21
|
+
* This drives the REAL spawn path (`startRpcSession` → `RpcProcess` → the
|
|
22
|
+
* scripted child), so the retry, the `--model` argument and the registration
|
|
23
|
+
* bookkeeping are the shipped code — not a stub of it.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { afterEach, beforeEach, expect, test } from 'bun:test';
|
|
27
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
28
|
+
import { tmpdir } from 'node:os';
|
|
29
|
+
import { join } from 'node:path';
|
|
30
|
+
import { describePosix } from '@/test-support/platform';
|
|
31
|
+
|
|
32
|
+
import { invalidateOmpCliCache } from '@/server/lib/omp/core/cli';
|
|
33
|
+
import { stopDiscoveryRootsWatch } from '@/server/lib/omp/config/roots-watch.server';
|
|
34
|
+
import { getRpcSession, startRpcSession } from '@/server/lib/omp/rpc/session-registry';
|
|
35
|
+
import { invalidateResolvableModels } from '@/server/lib/omp/session/resumable-model.server';
|
|
36
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
37
|
+
|
|
38
|
+
let root: string;
|
|
39
|
+
let agentDir: string;
|
|
40
|
+
let saved: Record<string, string | undefined>;
|
|
41
|
+
|
|
42
|
+
const ENV_KEYS = [
|
|
43
|
+
'KABMO_OMP_BIN',
|
|
44
|
+
'PI_CODING_AGENT_DIR',
|
|
45
|
+
'KABMO_DB_PATH',
|
|
46
|
+
'SYNC_WORKSPACE',
|
|
47
|
+
'FAKE_OMP_RESTORE_FAILS',
|
|
48
|
+
'FAKE_OMP_RESTORE_MODELS',
|
|
49
|
+
'FAKE_OMP_RESTORE_LOG',
|
|
50
|
+
] as const;
|
|
51
|
+
|
|
52
|
+
/** The argv of every child the scripted omp started, in spawn order. */
|
|
53
|
+
function spawnedArgs(): string[][] {
|
|
54
|
+
const path = Bun.env.FAKE_OMP_RESTORE_LOG;
|
|
55
|
+
if (!path || !existsSync(path)) return [];
|
|
56
|
+
return readFileSync(path, 'utf8')
|
|
57
|
+
.trim()
|
|
58
|
+
.split('\n')
|
|
59
|
+
.filter(Boolean)
|
|
60
|
+
.map((line) => JSON.parse(line) as string[]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
beforeEach(async () => {
|
|
64
|
+
root = mkdtempSync(join(tmpdir(), 'omp-restore-test-'));
|
|
65
|
+
saved = Object.fromEntries(ENV_KEYS.map((key) => [key, Bun.env[key]]));
|
|
66
|
+
await isolateDb();
|
|
67
|
+
agentDir = join(root, 'agent');
|
|
68
|
+
Bun.env.KABMO_OMP_BIN = join(import.meta.dir, '../../../../test-support/fake-omp-restore.ts');
|
|
69
|
+
Bun.env.PI_CODING_AGENT_DIR = agentDir;
|
|
70
|
+
Bun.env.SYNC_WORKSPACE = 'false';
|
|
71
|
+
Bun.env.FAKE_OMP_RESTORE_FAILS = '1';
|
|
72
|
+
Bun.env.FAKE_OMP_RESTORE_MODELS = 'kenari/deepseek-v4-flash,kenari/deepseek-v4-pro';
|
|
73
|
+
Bun.env.FAKE_OMP_RESTORE_LOG = join(root, 'spawns.jsonl');
|
|
74
|
+
invalidateOmpCliCache();
|
|
75
|
+
invalidateResolvableModels();
|
|
76
|
+
delete globalThis.__ompSessions;
|
|
77
|
+
delete globalThis.__ompStartLocks;
|
|
78
|
+
// The registry's fallback candidate: config.yml's `modelRoles.default`.
|
|
79
|
+
mkdirSync(agentDir, { recursive: true });
|
|
80
|
+
writeFileSync(
|
|
81
|
+
join(agentDir, 'config.yml'),
|
|
82
|
+
'modelRoles:\n default: kenari/deepseek-v4-flash\n',
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterEach(async () => {
|
|
87
|
+
stopDiscoveryRootsWatch();
|
|
88
|
+
delete globalThis.__ompSessions;
|
|
89
|
+
delete globalThis.__ompStartLocks;
|
|
90
|
+
invalidateOmpCliCache();
|
|
91
|
+
invalidateResolvableModels();
|
|
92
|
+
await releaseDb();
|
|
93
|
+
for (const key of ENV_KEYS) {
|
|
94
|
+
const value = saved[key];
|
|
95
|
+
if (value === undefined) delete Bun.env[key];
|
|
96
|
+
else Bun.env[key] = value;
|
|
97
|
+
}
|
|
98
|
+
rmSync(root, { recursive: true, force: true });
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// The scripted omp is spawned from a `#!/usr/bin/env bun` file; Windows can
|
|
102
|
+
// start a real executable, not a shebang file, so `uv_spawn` answers EFTYPE.
|
|
103
|
+
describePosix('a session whose recorded model cannot be restored', () => {
|
|
104
|
+
test('is respawned with a resolvable --model and comes up', async () => {
|
|
105
|
+
const cwd = join(root, 'ws');
|
|
106
|
+
const sessionsDir = join(root, 'agent', 'sessions', 'ws');
|
|
107
|
+
mkdirSync(cwd, { recursive: true });
|
|
108
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
109
|
+
const sessionFile = join(sessionsDir, 's.jsonl');
|
|
110
|
+
writeFileSync(sessionFile, '');
|
|
111
|
+
|
|
112
|
+
const { session } = await startRpcSession('restore-test-session', sessionFile, cwd);
|
|
113
|
+
|
|
114
|
+
// The session is USABLE: the wrapper is alive and registered, which is what
|
|
115
|
+
// the prompt path needs.
|
|
116
|
+
expect(session.isAlive()).toBe(true);
|
|
117
|
+
expect(getRpcSession('restore-test-session')).toBe(session);
|
|
118
|
+
|
|
119
|
+
// Two spawns: the bare resume (which died with omp's own restore error),
|
|
120
|
+
// then the recovered one carrying the model that resolves.
|
|
121
|
+
const attempts = spawnedArgs();
|
|
122
|
+
expect(attempts.length).toBe(2);
|
|
123
|
+
expect(attempts[0]).not.toContain('--model');
|
|
124
|
+
expect(attempts[1]).toContain('--model');
|
|
125
|
+
expect(attempts[1][attempts[1].indexOf('--model') + 1]).toBe('kenari/deepseek-v4-flash');
|
|
126
|
+
|
|
127
|
+
session.destroy();
|
|
128
|
+
}, 30_000);
|
|
129
|
+
|
|
130
|
+
test('the new child runs on the model it was resumed with', async () => {
|
|
131
|
+
const cwd = join(root, 'ws');
|
|
132
|
+
const sessionsDir = join(root, 'agent', 'sessions', 'ws');
|
|
133
|
+
mkdirSync(cwd, { recursive: true });
|
|
134
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
135
|
+
const sessionFile = join(sessionsDir, 's.jsonl');
|
|
136
|
+
writeFileSync(sessionFile, '');
|
|
137
|
+
|
|
138
|
+
const { session } = await startRpcSession('restore-test-model', sessionFile, cwd);
|
|
139
|
+
const state = await session.send({ type: 'get_state' }) as { model?: { provider?: string; id?: string } };
|
|
140
|
+
|
|
141
|
+
expect(state.model?.provider).toBe('kenari');
|
|
142
|
+
expect(state.model?.id).toBe('deepseek-v4-flash');
|
|
143
|
+
session.destroy();
|
|
144
|
+
}, 30_000);
|
|
145
|
+
|
|
146
|
+
test('the caller\'s own pick is the first recovery candidate', async () => {
|
|
147
|
+
const cwd = join(root, 'ws');
|
|
148
|
+
const sessionsDir = join(root, 'agent', 'sessions', 'ws');
|
|
149
|
+
mkdirSync(cwd, { recursive: true });
|
|
150
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
151
|
+
const sessionFile = join(sessionsDir, 's.jsonl');
|
|
152
|
+
writeFileSync(sessionFile, '');
|
|
153
|
+
|
|
154
|
+
// A `set_model` posts the operator's own pick, so the session resumes on
|
|
155
|
+
// what they chose — but only through the recovery: the first attempt names
|
|
156
|
+
// no model, because a stale pick must not make a healthy session
|
|
157
|
+
// unopenable (omp answers `Model <x> not found` for one).
|
|
158
|
+
const { session } = await startRpcSession(
|
|
159
|
+
'restore-test-explicit',
|
|
160
|
+
sessionFile,
|
|
161
|
+
cwd,
|
|
162
|
+
undefined,
|
|
163
|
+
undefined,
|
|
164
|
+
undefined,
|
|
165
|
+
{ provider: 'kenari', modelId: 'deepseek-v4-pro' },
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const attempts = spawnedArgs();
|
|
169
|
+
expect(attempts.length).toBe(2);
|
|
170
|
+
expect(attempts[0]).not.toContain('--model');
|
|
171
|
+
expect(attempts[1][attempts[1].indexOf('--model') + 1]).toBe('kenari/deepseek-v4-pro');
|
|
172
|
+
session.destroy();
|
|
173
|
+
}, 30_000);
|
|
174
|
+
|
|
175
|
+
test('an unrecoverable resume still reports the failure', async () => {
|
|
176
|
+
// No model omp can resolve: the catalog is empty and no default is set.
|
|
177
|
+
Bun.env.FAKE_OMP_RESTORE_MODELS = '';
|
|
178
|
+
writeFileSync(join(agentDir, 'config.yml'), '{}\n');
|
|
179
|
+
invalidateResolvableModels();
|
|
180
|
+
|
|
181
|
+
const cwd = join(root, 'ws');
|
|
182
|
+
const sessionsDir = join(root, 'agent', 'sessions', 'ws');
|
|
183
|
+
mkdirSync(cwd, { recursive: true });
|
|
184
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
185
|
+
const sessionFile = join(sessionsDir, 's.jsonl');
|
|
186
|
+
writeFileSync(sessionFile, '');
|
|
187
|
+
|
|
188
|
+
// The failure is NOT swallowed: the caller sees omp's own sentence, which
|
|
189
|
+
// names the model that could not be restored.
|
|
190
|
+
const failure = await startRpcSession('restore-test-honest', sessionFile, cwd).catch((error: unknown) => error);
|
|
191
|
+
expect(failure).toBeInstanceOf(Error);
|
|
192
|
+
expect((failure as Error).message).toContain('Could not restore model');
|
|
193
|
+
}, 30_000);
|
|
194
|
+
});
|