@dreamlake/dreamlake-cli 0.98.0 → 0.99.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/dist/api/index.d.ts +212 -10
- package/dist/api/index.js +6 -0
- package/dist/api/native-experiments.d.ts +77 -0
- package/dist/api/native-experiments.js +31 -0
- package/dist/api/sessions.d.ts +240 -7
- package/dist/api/sessions.js +56 -1
- package/dist/api/skills.d.ts +122 -0
- package/dist/api/skills.js +220 -0
- package/dist/cli/experiments/index.js +2 -0
- package/dist/cli/experiments/native-commands.d.ts +13 -0
- package/dist/cli/experiments/native-commands.js +88 -0
- package/dist/cli/help-examples.generated.js +22 -5
- package/dist/cli/index.js +2 -0
- package/dist/cli/session-affiliation.js +3 -2
- package/dist/cli/sessions/affiliation.d.ts +12 -1
- package/dist/cli/sessions/affiliation.js +37 -0
- package/dist/cli/sessions/control.d.ts +11 -0
- package/dist/cli/sessions/control.js +49 -4
- package/dist/cli/sessions/index.js +70 -17
- package/dist/cli/sessions/live.d.ts +85 -0
- package/dist/cli/sessions/live.js +258 -0
- package/dist/cli/sessions/runner/exec.d.ts +118 -6
- package/dist/cli/sessions/runner/exec.js +298 -47
- package/dist/cli/sessions/runner/native.js +4 -1
- package/dist/cli/sessions/runner/serve.d.ts +37 -2
- package/dist/cli/sessions/runner/serve.js +273 -28
- package/dist/cli/sessions/wrap.d.ts +63 -0
- package/dist/cli/sessions/wrap.js +226 -0
- package/dist/cli/skill/index.d.ts +19 -1
- package/dist/cli/skill/index.js +65 -12
- package/dist/cli/skill/read.d.ts +25 -0
- package/dist/cli/skill/read.js +65 -0
- package/dist/cli/skill/registry.d.ts +95 -0
- package/dist/cli/skill/registry.js +284 -0
- package/dist/cli/skill/reminders.js +2 -1
- package/dist/cli/version.d.ts +1 -1
- package/dist/cli/version.js +1 -1
- package/package.json +9 -9
|
@@ -15,10 +15,11 @@ import { hostname, homedir, userInfo, tmpdir } from 'node:os';
|
|
|
15
15
|
import { randomBytes } from 'node:crypto';
|
|
16
16
|
import { mkdirSync, readFileSync, writeFileSync, mkdtempSync, rmSync, realpathSync } from 'node:fs';
|
|
17
17
|
import { join } from 'node:path';
|
|
18
|
-
import { ApiError } from '../../../api/index.js';
|
|
18
|
+
import { ApiError, levelIndex } from '../../../api/index.js';
|
|
19
19
|
import { readConversationFile } from '../conversation.js';
|
|
20
|
+
import { redactSessionText } from '../redaction.js';
|
|
20
21
|
import { claudeSubagents, defaultRoots, findClaudeTranscript, findCodexRollout, runStateFrom, tmuxSessionName, transcriptFacts } from './native.js';
|
|
21
|
-
import { createWorktree, hasTmux, nativeWriterBusy, ownerMarker, resumeTmux, run as defaultRun, RunnerError, sendHeadless, startHeadless, startTmux, tmuxSend, tmuxStop, verifyOwnership } from './exec.js';
|
|
22
|
+
import { assertLivePolicy, createWorktree, hasTmux, headlessArgv, livePolicy, nativeWriterBusy, ownerMarker, removeWorktree, resumeTmux, run as defaultRun, RunnerError, sendHeadless, startHeadless, startTmux, tmuxSend, tmuxStop, transcriptMark, verifyOwnership } from './exec.js';
|
|
22
23
|
/** Stable per machine + Unix user; a random key, not a credential (the owner's token authenticates). */
|
|
23
24
|
export function runnerKey(stateFile) {
|
|
24
25
|
try {
|
|
@@ -85,8 +86,65 @@ export class Semaphore {
|
|
|
85
86
|
else
|
|
86
87
|
this.used--; }
|
|
87
88
|
}
|
|
89
|
+
/** The runner's own live-state reports (`source: runner`, every 10 s while a provider turn of a session
|
|
90
|
+
* runs, then the outcome). Best effort and bounded: a beat never fails or delays the command it describes;
|
|
91
|
+
* a session the server will not let this runner speak for (not bound to it, `409 session_runner_mismatch`;
|
|
92
|
+
* unknown, 404) is logged once and dropped until a resume or restart of it completes here. Without a
|
|
93
|
+
* runner id (tests, dry runs) nothing is sent. */
|
|
94
|
+
export class LiveBeats {
|
|
95
|
+
api;
|
|
96
|
+
runnerId;
|
|
97
|
+
log;
|
|
98
|
+
intervalMs;
|
|
99
|
+
muted = new Set();
|
|
100
|
+
pending = new Set();
|
|
101
|
+
constructor(api, runnerId, log = () => undefined, intervalMs = 10_000) {
|
|
102
|
+
this.api = api;
|
|
103
|
+
this.runnerId = runnerId;
|
|
104
|
+
this.log = log;
|
|
105
|
+
this.intervalMs = intervalMs;
|
|
106
|
+
}
|
|
107
|
+
beat(sessionId, liveState, current) {
|
|
108
|
+
if (!this.runnerId || this.muted.has(sessionId))
|
|
109
|
+
return Promise.resolve(false);
|
|
110
|
+
const sent = this.api.heartbeat(sessionId, { liveState, ...(current ? { current } : {}), at: new Date().toISOString(), runnerId: this.runnerId }).then(() => true, (error) => {
|
|
111
|
+
if (error instanceof ApiError && (error.status === 404 || error.status === 409)) {
|
|
112
|
+
this.muted.add(sessionId);
|
|
113
|
+
this.log(`live state of ${sessionId} not accepted (${error.code}); this runner stops reporting it`);
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
});
|
|
117
|
+
this.pending.add(sent);
|
|
118
|
+
void sent.finally(() => this.pending.delete(sent));
|
|
119
|
+
return sent;
|
|
120
|
+
}
|
|
121
|
+
/** The receipt of a resume / restart binds the session to this runner: its beats are welcome again. */
|
|
122
|
+
allow(sessionId) { this.muted.delete(sessionId); }
|
|
123
|
+
/** `running` now and every interval while `work` runs, then the outcome: `waiting-for-user` after a
|
|
124
|
+
* completed turn (the reply awaits a person), `stopped` when the turn was cancelled, `uncertain` when the
|
|
125
|
+
* provider failed. The outcome beat is not awaited: the receipt never waits on it. */
|
|
126
|
+
async during(sessionId, work) {
|
|
127
|
+
if (!this.runnerId)
|
|
128
|
+
return work();
|
|
129
|
+
void this.beat(sessionId, 'running');
|
|
130
|
+
// The timer never keeps the process alive on its own: a turn that outlives everything else is the turn's problem.
|
|
131
|
+
const timer = setInterval(() => { void this.beat(sessionId, 'running'); }, this.intervalMs).unref();
|
|
132
|
+
try {
|
|
133
|
+
const result = await work();
|
|
134
|
+
void this.beat(sessionId, 'waiting-for-user');
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
void this.beat(sessionId, error instanceof RunnerError && error.code === 'cancelled' ? 'stopped' : 'uncertain');
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
clearInterval(timer);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
88
146
|
export function commandExecutors(input) {
|
|
89
|
-
const { api, run, roots, home } = input, active = input.active ?? new ActiveProcesses(), slots = input.slots ?? new Semaphore(4);
|
|
147
|
+
const { api, run, roots, home } = input, active = input.active ?? new ActiveProcesses(), slots = input.slots ?? new Semaphore(4), beats = input.beats ?? new LiveBeats(api, undefined);
|
|
90
148
|
// Codex reveals a new conversation's id only through its rollout file; two starts in one
|
|
91
149
|
// directory at once could claim each other's, so starts in the same directory run one at a time.
|
|
92
150
|
const startLocks = new Map();
|
|
@@ -127,6 +185,30 @@ export function commandExecutors(input) {
|
|
|
127
185
|
throw new RunnerError('agent_permission_ceiling');
|
|
128
186
|
return level;
|
|
129
187
|
};
|
|
188
|
+
/** The level a restart moves the record to. A restart is the one sanctioned way above the recorded level,
|
|
189
|
+
* so the ceiling check of `policy()` does not apply; the server validated the same rule at submission and
|
|
190
|
+
* the runner re-applies it against its own advertised cap before anything is stopped: lowering always
|
|
191
|
+
* passes; raising needs the person's `raise: true`, never comes from the built-in agent, and stays within
|
|
192
|
+
* `--max-human-permission` (none advertised, none allowed). */
|
|
193
|
+
const restartPolicy = (row, i, origin) => {
|
|
194
|
+
const level = i.permissionLevel;
|
|
195
|
+
if (levelIndex(level) < 0)
|
|
196
|
+
throw new RunnerError('permission_level_unknown', 'A restart needs an explicit permission level.');
|
|
197
|
+
const current = levelIndex(row.permissionLevel) >= 0 ? row.permissionLevel : null;
|
|
198
|
+
if (levelIndex(level) > levelIndex(current)) {
|
|
199
|
+
if (origin.kind === 'builtin-agent')
|
|
200
|
+
throw new RunnerError('agent_permission_ceiling', 'The built-in agent cannot raise a session above its recorded level.');
|
|
201
|
+
if (i.raise !== true)
|
|
202
|
+
throw new RunnerError('session_permission_raise_unconfirmed', 'Raising the level needs the person\'s explicit confirmation (raise: true).');
|
|
203
|
+
if (!input.maxHumanPermission)
|
|
204
|
+
throw new RunnerError('session_permission_cap', 'This runner advertises no human permission cap; start it with --max-human-permission.');
|
|
205
|
+
if (levelIndex(level) > levelIndex(input.maxHumanPermission))
|
|
206
|
+
throw new RunnerError('session_permission_cap', `This runner caps raises at ${input.maxHumanPermission}.`);
|
|
207
|
+
}
|
|
208
|
+
if (origin.kind === 'builtin-agent' && levelIndex(level) > levelIndex(input.maxAgentPermission ?? 'workspace-write'))
|
|
209
|
+
throw new RunnerError('agent_permission_ceiling');
|
|
210
|
+
return level;
|
|
211
|
+
};
|
|
130
212
|
const observe = async (row) => {
|
|
131
213
|
if (row.sessionSandbox) {
|
|
132
214
|
if (!input.pods)
|
|
@@ -141,11 +223,22 @@ export function commandExecutors(input) {
|
|
|
141
223
|
return { sessionId: row.id, runState, alive, managed: owned === 'owned', transcriptFound: !!path, lastActivityAt: facts ? new Date(facts.lastAt).toISOString() : null, lastRole: facts?.lastRole ?? null, messages: facts?.messages ?? 0, heuristic: 'transcript tail + process liveness' };
|
|
142
224
|
};
|
|
143
225
|
const mode = (requested) => requested ?? (input.tmux ? 'tmux' : 'headless');
|
|
144
|
-
/** Headless turns must not race another process that wrote the transcript just now.
|
|
226
|
+
/** Headless turns must not race another process that wrote the transcript just now. The runner's own
|
|
227
|
+
* previous turn is not another process: the state it left is remembered per native session id. */
|
|
228
|
+
const ownMarks = new Map();
|
|
229
|
+
const markKey = (row) => `${row.provider}:${row.codexSessionId.toLowerCase()}`;
|
|
145
230
|
const guardWriter = (row) => {
|
|
146
|
-
if (nativeWriterBusy(transcript(row)))
|
|
231
|
+
if (nativeWriterBusy(transcript(row), Date.now(), 60_000, ownMarks.get(markKey(row))))
|
|
147
232
|
throw new RunnerError('native_session_active', 'The conversation was written in the last minute by another process; use tmux mode or wait.');
|
|
148
233
|
};
|
|
234
|
+
/** Called once the runner's own provider process has ended, whatever its outcome. */
|
|
235
|
+
const rememberOwnTurn = (row) => {
|
|
236
|
+
const mark = transcriptMark(transcript(row));
|
|
237
|
+
if (mark)
|
|
238
|
+
ownMarks.set(markKey(row), mark);
|
|
239
|
+
else
|
|
240
|
+
ownMarks.delete(markKey(row));
|
|
241
|
+
};
|
|
149
242
|
/** Reserve a session for one writer: the busy check and the registration are one synchronous
|
|
150
243
|
* step, so two commands that both finished their lookups cannot both pass. */
|
|
151
244
|
const reserve = (key, signal) => {
|
|
@@ -165,10 +258,32 @@ export function commandExecutors(input) {
|
|
|
165
258
|
slots.release();
|
|
166
259
|
}
|
|
167
260
|
};
|
|
261
|
+
/** A headless turn on an existing session. The writer guard is checked again once the provider slot
|
|
262
|
+
* is held: a command that queued behind other turns must not act on a check made before it waited.
|
|
263
|
+
* The transcript state is remembered as the runner's own only when its provider process actually ran
|
|
264
|
+
* (a result, or an uncertain failure); a turn cancelled in the queue, refused by the guard or whose
|
|
265
|
+
* process never spawned wrote nothing, and must not adopt a stranger's write as its own. */
|
|
266
|
+
const headlessTurn = (row, reservation, work) => provider(reservation, async (inner) => {
|
|
267
|
+
if (!row.sessionSandbox)
|
|
268
|
+
guardWriter(row);
|
|
269
|
+
let ran = true;
|
|
270
|
+
// The runner reports `running` every 10 s for the turn it executes; the session's own hooks, if any, refine it.
|
|
271
|
+
try {
|
|
272
|
+
return await beats.during(row.id, () => work(inner));
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
ran = error instanceof RunnerError && error.uncertain;
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
if (ran && !row.sessionSandbox)
|
|
280
|
+
rememberOwnTurn(row);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
168
283
|
return {
|
|
169
284
|
async execute(command, signal) {
|
|
170
285
|
signal.throwIfAborted();
|
|
171
|
-
if (['send', 'resume', 'start'].includes(command.kind) && input.quarantined?.size)
|
|
286
|
+
if (['send', 'resume', 'restart', 'start'].includes(command.kind) && input.quarantined?.size)
|
|
172
287
|
throw new RunnerError('runner_recovery_required', 'An earlier provider outcome is unconfirmed. Inspect retained runner journal before further execution.');
|
|
173
288
|
switch (command.kind) {
|
|
174
289
|
case 'start': {
|
|
@@ -193,10 +308,28 @@ export function commandExecutors(input) {
|
|
|
193
308
|
const chosen = i.isolation.kind === 'pod' ? 'headless' : mode(i.mode);
|
|
194
309
|
if (chosen === 'tmux' && !input.tmux)
|
|
195
310
|
throw new RunnerError('tmux_missing');
|
|
196
|
-
|
|
311
|
+
// Worktree creation runs inside the cleanup scope below: a `git worktree add` that is cancelled or
|
|
312
|
+
// times out may already have registered the worktree and created the branch, and the record prepared
|
|
313
|
+
// just before the add (identity, starting tip) is what lets that partial creation be reconciled.
|
|
314
|
+
let worktree = null, where = { path: i.isolation.repo, branch: null, root: i.isolation.repo };
|
|
197
315
|
const checkpoint = { cwd: where.path, branch: where.branch, isolation: i.isolation.kind };
|
|
198
316
|
let sandbox;
|
|
317
|
+
/** A worktree whose start produced no session is an orphan nobody can find later: tear it down,
|
|
318
|
+
* unless a process may still be alive in it (tmux teardown unverified) or the session exists
|
|
319
|
+
* and only its registration failed (the checkpoint lets an operator register it by hand). */
|
|
320
|
+
const orphaned = async (error) => {
|
|
321
|
+
if (!worktree || error.code === 'registration_failed')
|
|
322
|
+
return error;
|
|
323
|
+
const teardown = error.checkpoint.tornDown === false ? { removed: false, branchDeleted: false, reason: 'tmux session may still be running in the worktree' } : await removeWorktree(run, worktree).catch(e => ({ removed: false, branchDeleted: false, reason: e instanceof Error ? e.message : String(e) }));
|
|
324
|
+
input.journal?.checkpoint(command.id, { worktree: teardown });
|
|
325
|
+
return new RunnerError(error.code, error.message, error.uncertain, { ...checkpoint, ...error.checkpoint, worktree: teardown }, error.detail);
|
|
326
|
+
};
|
|
199
327
|
try {
|
|
328
|
+
if (i.isolation.kind === 'worktree') {
|
|
329
|
+
worktree = await createWorktree(run, { repo: i.isolation.repo, branch: i.isolation.branch, baseRef: i.isolation.baseRef, home, signal, checkpoint: value => input.journal?.checkpoint(command.id, value), prepared: record => { worktree = record; } });
|
|
330
|
+
where = worktree;
|
|
331
|
+
Object.assign(checkpoint, { cwd: worktree.path, branch: worktree.branch });
|
|
332
|
+
}
|
|
200
333
|
if (i.isolation.kind === 'pod')
|
|
201
334
|
sandbox = await input.pods.create(command.id, where.path, i.provider, i.permissionLevel, signal, binding => input.journal?.checkpoint(command.id, { sessionSandbox: binding }));
|
|
202
335
|
if (sandbox)
|
|
@@ -207,18 +340,20 @@ export function commandExecutors(input) {
|
|
|
207
340
|
try {
|
|
208
341
|
started = await provider(reservation, inner => serialized(i.provider === 'codex' ? `codex:${where.path}` : `claude:${command.id}`, async () => {
|
|
209
342
|
inner.throwIfAborted();
|
|
210
|
-
const launch = { provider: i.provider, cwd: sandbox ? '/workspace' : where.path, permissionLevel: i.permissionLevel, prompt: i.prompt, codexRoot: roots.codex, signal: inner, checkpoint: (value) => input.journal?.checkpoint(command.id, value) };
|
|
343
|
+
const launch = { provider: i.provider, cwd: sandbox ? '/workspace' : where.path, permissionLevel: i.permissionLevel, prompt: i.prompt, codexRoot: roots.codex, defaultClaudeConfigDir: roots.claude, signal: inner, checkpoint: (value) => input.journal?.checkpoint(command.id, value) };
|
|
211
344
|
return chosen === 'tmux' ? startTmux(run, { ...launch, marker: id => ownerMarker(input.runnerKey, id) }) : startHeadless(sandbox ? input.pods.run(sandbox) : run, launch);
|
|
212
345
|
}));
|
|
213
346
|
}
|
|
214
347
|
catch (error) {
|
|
215
348
|
if (error instanceof RunnerError)
|
|
216
|
-
throw new RunnerError(error.code, error.message, error.uncertain, { ...checkpoint, ...error.checkpoint });
|
|
349
|
+
throw new RunnerError(error.code, error.message, error.uncertain, { ...checkpoint, ...error.checkpoint }, error.detail);
|
|
217
350
|
throw error;
|
|
218
351
|
}
|
|
219
352
|
finally {
|
|
220
353
|
reservation.release();
|
|
221
354
|
}
|
|
355
|
+
if (chosen === 'headless' && !sandbox)
|
|
356
|
+
rememberOwnTurn({ provider: i.provider, codexSessionId: started.nativeSessionId, claudeConfigDir: null });
|
|
222
357
|
signal.throwIfAborted();
|
|
223
358
|
Object.assign(checkpoint, { nativeSessionId: started.nativeSessionId, tmuxSession: started.tmuxSession ?? null, mode: chosen });
|
|
224
359
|
input.journal?.checkpoint(command.id, checkpoint);
|
|
@@ -230,7 +365,7 @@ export function commandExecutors(input) {
|
|
|
230
365
|
catch (error) {
|
|
231
366
|
throw new RunnerError('registration_failed', error instanceof ApiError ? error.code : String(error), true, checkpoint);
|
|
232
367
|
}
|
|
233
|
-
return { sessionId: registered.session.id, ...checkpoint, provider: i.provider, permissionLevel: i.permissionLevel, reply: started.reply ?? null,
|
|
368
|
+
return { sessionId: registered.session.id, ...checkpoint, provider: i.provider, permissionLevel: i.permissionLevel, argv: started.argv, reply: started.reply ?? null, ...(started.claudeConfig ? { claudeConfig: started.claudeConfig } : {}),
|
|
234
369
|
runState: chosen === 'tmux' ? 'running' : 'idle', href: `/${api.namespace}/sessions?session=${registered.session.id}` };
|
|
235
370
|
}
|
|
236
371
|
catch (error) {
|
|
@@ -241,9 +376,9 @@ export function commandExecutors(input) {
|
|
|
241
376
|
stopped = true;
|
|
242
377
|
}
|
|
243
378
|
catch { }
|
|
244
|
-
throw new RunnerError('cancelled', undefined, true, { ...checkpoint, stopped });
|
|
379
|
+
throw await orphaned(new RunnerError('cancelled', undefined, true, { ...checkpoint, stopped }));
|
|
245
380
|
}
|
|
246
|
-
throw error;
|
|
381
|
+
throw error instanceof RunnerError ? await orphaned(error) : error;
|
|
247
382
|
}
|
|
248
383
|
}
|
|
249
384
|
case 'resume': {
|
|
@@ -256,7 +391,7 @@ export function commandExecutors(input) {
|
|
|
256
391
|
if (row.sessionSandbox && level !== row.permissionLevel)
|
|
257
392
|
throw new RunnerError('permission_change_requires_restart');
|
|
258
393
|
if (chosen === 'headless' && !i.prompt)
|
|
259
|
-
return { sessionId: row.id, mode: chosen, noop: true, note: 'Headless resume runs one turn and needs a prompt; nothing was executed.', runState: 'idle' };
|
|
394
|
+
return { sessionId: row.id, mode: chosen, noop: true, argv: null, note: 'Headless resume runs one turn and needs a prompt; nothing was executed.', runState: 'idle' };
|
|
260
395
|
// One writer per session in either mode, reserved before any further asynchronous step.
|
|
261
396
|
const reservation = reserve(row.id, signal);
|
|
262
397
|
try {
|
|
@@ -266,13 +401,61 @@ export function commandExecutors(input) {
|
|
|
266
401
|
throw new RunnerError('pane_ownership_unverified');
|
|
267
402
|
if (ownership === 'owned' && level !== row.permissionLevel)
|
|
268
403
|
throw new RunnerError('permission_change_requires_restart');
|
|
269
|
-
const r = await resumeTmux(run, { provider: row.provider, cwd: row.sessionSandbox ? '/workspace' : row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, prompt: i.prompt, marker: marker(row), signal: reservation.controller.signal, checkpoint: value => input.journal?.checkpoint(command.id, value) });
|
|
404
|
+
const r = await resumeTmux(run, { provider: row.provider, cwd: row.sessionSandbox ? '/workspace' : row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, prompt: i.prompt, marker: marker(row), signal: reservation.controller.signal, checkpoint: value => input.journal?.checkpoint(command.id, value) });
|
|
405
|
+
beats.allow(row.id);
|
|
406
|
+
void beats.beat(row.id, r.created ? 'starting' : r.delivered ? 'running' : 'idle');
|
|
270
407
|
return { sessionId: row.id, mode: chosen, permissionLevel: level, ...r, runState: 'running' };
|
|
271
408
|
}
|
|
272
409
|
if (!row.sessionSandbox)
|
|
273
410
|
guardWriter(row);
|
|
274
|
-
const r = await
|
|
275
|
-
|
|
411
|
+
const r = await headlessTurn(row, reservation, inner => sendHeadless(row.sessionSandbox ? input.pods.run(row.sessionSandbox) : run, { provider: row.provider, cwd: row.sessionSandbox ? '/workspace' : row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, text: i.prompt, signal: inner }));
|
|
412
|
+
beats.allow(row.id);
|
|
413
|
+
return { sessionId: row.id, mode: chosen, permissionLevel: level, created: false, delivered: true, argv: r.argv, reply: r.reply, ...(r.claudeConfig ? { claudeConfig: r.claudeConfig } : {}), runState: 'waiting' };
|
|
414
|
+
}
|
|
415
|
+
finally {
|
|
416
|
+
reservation.release();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
case 'restart': {
|
|
420
|
+
const i = command.input, row = await session(i.sessionId);
|
|
421
|
+
signal.throwIfAborted();
|
|
422
|
+
// A pod is created for one policy (its repository mount follows the level); moving a container
|
|
423
|
+
// between policies is a new start, not a restart.
|
|
424
|
+
if (row.sessionSandbox)
|
|
425
|
+
throw new RunnerError('pod_restart_unsupported', 'A pod session cannot change policy in place; start a new pod session at the wanted level.');
|
|
426
|
+
const level = restartPolicy(row, i, command.origin), chosen = mode(i.mode), previous = row.permissionLevel ?? null;
|
|
427
|
+
if (chosen === 'tmux' && !input.tmux)
|
|
428
|
+
throw new RunnerError('tmux_missing');
|
|
429
|
+
// Stop: the runner's own headless turn, and the tmux session it owns. Only a verified stop proceeds.
|
|
430
|
+
const { interrupted, ended } = await active.cancel(row.id);
|
|
431
|
+
signal.throwIfAborted();
|
|
432
|
+
const tmuxStopped = input.tmux ? await tmuxStop(run, name(row), marker(row)) : false;
|
|
433
|
+
const stopped = { interruptedTurns: interrupted, tmuxStopped };
|
|
434
|
+
if (interrupted && !ended)
|
|
435
|
+
throw new RunnerError('stop_unverified', 'The interrupted turn did not end within the wait; nothing was relaunched.', true, { sessionId: row.id, ...stopped, phase: 'stop' });
|
|
436
|
+
// What the stopped process left in the transcript is quiescent, not another writer's work.
|
|
437
|
+
if (tmuxStopped || interrupted)
|
|
438
|
+
rememberOwnTurn(row);
|
|
439
|
+
signal.throwIfAborted();
|
|
440
|
+
const base = { sessionId: row.id, mode: chosen, permissionLevel: level, previousPermissionLevel: previous, nativeSessionId: row.codexSessionId, stopped };
|
|
441
|
+
const reservation = reserve(row.id, signal);
|
|
442
|
+
try {
|
|
443
|
+
if (chosen === 'tmux') {
|
|
444
|
+
const r = await resumeTmux(run, { provider: row.provider, cwd: row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, prompt: i.prompt, marker: marker(row), signal: reservation.controller.signal, checkpoint: value => input.journal?.checkpoint(command.id, { ...stopped, ...value }) });
|
|
445
|
+
beats.allow(row.id);
|
|
446
|
+
void beats.beat(row.id, 'starting');
|
|
447
|
+
return { ...base, ...r, runState: 'running' };
|
|
448
|
+
}
|
|
449
|
+
// Headless: there is no persistent process to relaunch; the record moves on this receipt and every
|
|
450
|
+
// later turn runs under it. With a prompt, the first such turn runs now.
|
|
451
|
+
beats.allow(row.id);
|
|
452
|
+
if (!i.prompt) {
|
|
453
|
+
if (tmuxStopped || interrupted)
|
|
454
|
+
void beats.beat(row.id, 'idle');
|
|
455
|
+
return { ...base, argv: headlessArgv({ provider: row.provider, nativeSessionId: row.codexSessionId, permissionLevel: level, resume: true }), executed: false, note: 'Headless restart: no process was running to relaunch; the next headless turn runs under the new level.', runState: 'idle' };
|
|
456
|
+
}
|
|
457
|
+
const r = await headlessTurn(row, reservation, inner => sendHeadless(run, { provider: row.provider, cwd: row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, text: i.prompt, signal: inner }));
|
|
458
|
+
return { ...base, argv: r.argv, delivered: true, reply: r.reply, ...(r.claudeConfig ? { claudeConfig: r.claudeConfig } : {}), runState: 'waiting' };
|
|
276
459
|
}
|
|
277
460
|
finally {
|
|
278
461
|
reservation.release();
|
|
@@ -287,6 +470,8 @@ export function commandExecutors(input) {
|
|
|
287
470
|
// Reported only once the process is actually gone; a turn still winding down is an uncertain stop.
|
|
288
471
|
if (interrupted && !ended)
|
|
289
472
|
throw new RunnerError('stop_unverified', 'The interrupted turn did not end within the wait.', true, { sessionId: row.id, interruptedTurns: interrupted, tmuxStopped: stopped });
|
|
473
|
+
if (stopped || interrupted)
|
|
474
|
+
void beats.beat(row.id, 'stopped');
|
|
290
475
|
return { sessionId: row.id, stopped: stopped || interrupted > 0, interruptedTurns: interrupted, note: stopped || interrupted ? undefined : 'No managed process of this session was running on this runner.', runState: 'stopped' };
|
|
291
476
|
}
|
|
292
477
|
case 'send': {
|
|
@@ -302,13 +487,22 @@ export function commandExecutors(input) {
|
|
|
302
487
|
if ((ownership === 'owned' || row.sessionSandbox) && i.permissionLevel && i.permissionLevel !== row.permissionLevel)
|
|
303
488
|
throw new RunnerError('permission_change_requires_restart');
|
|
304
489
|
if (ownership === 'owned') {
|
|
305
|
-
|
|
306
|
-
|
|
490
|
+
// Never type into a process running above (or beside) the authorized level: a record moved by a
|
|
491
|
+
// PATCH means the live process is relaunched under the record first, then the text is delivered.
|
|
492
|
+
if (assertLivePolicy(await livePolicy(run, name(row)), level) === 'matches') {
|
|
493
|
+
await tmuxSend(run, name(row), marker(row), i.text, reservation.controller.signal);
|
|
494
|
+
void beats.beat(row.id, 'running');
|
|
495
|
+
return { sessionId: row.id, mode: 'tmux', permissionLevel: level, delivered: true, reply: null, argv: null, runState: 'working' };
|
|
496
|
+
}
|
|
497
|
+
const r = await resumeTmux(run, { provider: row.provider, cwd: row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, prompt: i.text, marker: marker(row), signal: reservation.controller.signal, checkpoint: value => input.journal?.checkpoint(command.id, value) });
|
|
498
|
+
beats.allow(row.id);
|
|
499
|
+
void beats.beat(row.id, 'starting');
|
|
500
|
+
return { sessionId: row.id, mode: 'tmux', permissionLevel: level, reply: null, ...r, runState: 'working' };
|
|
307
501
|
}
|
|
308
502
|
if (!row.sessionSandbox)
|
|
309
503
|
guardWriter(row);
|
|
310
|
-
const r = await
|
|
311
|
-
return { sessionId: row.id, mode: 'headless', permissionLevel: level, delivered: true, reply: r.reply, runState: 'waiting' };
|
|
504
|
+
const r = await headlessTurn(row, reservation, inner => sendHeadless(row.sessionSandbox ? input.pods.run(row.sessionSandbox) : run, { provider: row.provider, cwd: row.sessionSandbox ? '/workspace' : row.cwd, nativeSessionId: row.codexSessionId, permissionLevel: level, claudeConfigDir: row.claudeConfigDir, defaultClaudeConfigDir: roots.claude, text: i.text, signal: inner }));
|
|
505
|
+
return { sessionId: row.id, mode: 'headless', permissionLevel: level, delivered: true, argv: r.argv, reply: r.reply, ...(r.claudeConfig ? { claudeConfig: r.claudeConfig } : {}), runState: 'waiting' };
|
|
312
506
|
}
|
|
313
507
|
finally {
|
|
314
508
|
reservation.release();
|
|
@@ -362,17 +556,26 @@ function controllerFor(signal) {
|
|
|
362
556
|
signal.addEventListener('abort', () => controller.abort(signal.reason), { once: true });
|
|
363
557
|
return controller;
|
|
364
558
|
}
|
|
559
|
+
/** `error` stays the stable code (what callers branch on); `result.error` carries the underlying text:
|
|
560
|
+
* the runner's message, and for a provider failure its exit code, signal and redacted output tails. */
|
|
365
561
|
export function receiptFor(leaseToken, outcome, aborted) {
|
|
366
562
|
if (outcome.result)
|
|
367
563
|
return { leaseToken, status: 'completed', result: outcome.result };
|
|
368
564
|
const error = outcome.error;
|
|
369
|
-
|
|
370
|
-
|
|
565
|
+
const text = (message) => (message.trim() ? { message: redactSessionText(message.trim()).slice(0, 2000) } : {});
|
|
566
|
+
if (error instanceof RunnerError) {
|
|
567
|
+
const code = aborted && error.code !== 'cancelled' ? 'cancelled' : error.code;
|
|
568
|
+
const described = { code: error.code, ...(error.message !== error.code ? text(error.message) : {}), ...error.detail };
|
|
569
|
+
return { leaseToken, status: error.uncertain || aborted ? 'uncertain' : 'failed', error: code, result: { ...(Object.keys(error.checkpoint).length ? { checkpoint: error.checkpoint } : {}), error: described } };
|
|
570
|
+
}
|
|
371
571
|
if (error instanceof ApiError)
|
|
372
|
-
return { leaseToken, status: aborted ? 'uncertain' : 'failed', error: error.code };
|
|
373
|
-
return { leaseToken, status: aborted ? 'uncertain' : 'failed', error: aborted ? 'cancelled' : 'runner_error' };
|
|
572
|
+
return { leaseToken, status: aborted ? 'uncertain' : 'failed', error: error.code, result: { error: { code: error.code, status: error.status, ...(error.requestId ? { requestId: error.requestId } : {}) } } };
|
|
573
|
+
return { leaseToken, status: aborted ? 'uncertain' : 'failed', error: aborted ? 'cancelled' : 'runner_error', result: { error: { code: 'runner_error', ...text(error instanceof Error ? error.message : String(error)) } } };
|
|
374
574
|
}
|
|
375
575
|
/** One command end to end: lease renewal with a local deadline, execution, one receipt. Exported for tests. */
|
|
576
|
+
/** How often a running command asks the server whether its owner cancelled it. Independent of lease renewal,
|
|
577
|
+
* so a slow or hanging renewal never delays the interruption of a provider turn. */
|
|
578
|
+
export const CANCEL_CHECK_MS = 2_000;
|
|
376
579
|
export async function handleCommand(input) {
|
|
377
580
|
const { api, runnerId, executors, log, command } = input, now = input.now ?? Date.now;
|
|
378
581
|
// Command-scoped cancellation: lease refusal, local lease expiry, or runner shutdown — from the
|
|
@@ -415,10 +618,39 @@ export async function handleCommand(input) {
|
|
|
415
618
|
renewing = false;
|
|
416
619
|
}
|
|
417
620
|
};
|
|
418
|
-
|
|
621
|
+
// Renewal keeps the lease (every 10 s at most) and also carries a cancel when the server answers one.
|
|
622
|
+
const renewal = setInterval(() => { void renew(); }, input.renewMs ?? Math.max(5_000, Math.min(command.leaseMs / 4, 10_000)));
|
|
419
623
|
// Independent watchdog: a renewal that hangs is not a renewal; the deadline passes regardless.
|
|
420
624
|
const watchdog = setInterval(() => { if (now() >= expiresAt)
|
|
421
625
|
lose('lease_expired'); }, input.tickMs ?? 1_000);
|
|
626
|
+
// Cancellation is detected on its own clock: a read of the command every 2 s. Each probe has its own
|
|
627
|
+
// abort and a request deadline of one interval (independent of the client's default), and a monotonic
|
|
628
|
+
// id, so a probe that outlives its deadline is cut off rather than left to overlap the next, and a
|
|
629
|
+
// late reply can never clear a newer probe's guard. An owner's `control cancel` therefore reaches the
|
|
630
|
+
// provider within about 2 s plus one request, however slow renewal is; the process is then sent
|
|
631
|
+
// SIGTERM at once and SIGKILL 5 s later, so a running turn is dead well within 10 s.
|
|
632
|
+
const cancelCheckMs = input.cancelCheckMs ?? CANCEL_CHECK_MS;
|
|
633
|
+
let probes = 0, pending = null;
|
|
634
|
+
const checkCancel = async () => {
|
|
635
|
+
if (pending !== null || controller.signal.aborted)
|
|
636
|
+
return;
|
|
637
|
+
const id = ++probes, probe = new AbortController();
|
|
638
|
+
pending = id;
|
|
639
|
+
const deadline = setTimeout(() => { probe.abort(); if (pending === id)
|
|
640
|
+
pending = null; }, cancelCheckMs);
|
|
641
|
+
try {
|
|
642
|
+
const observed = await api.commands.get(command.id, undefined, { signal: probe.signal, timeoutMs: cancelCheckMs });
|
|
643
|
+
if (observed.cancelRequested || observed.status === 'cancelled')
|
|
644
|
+
controller.abort(new RunnerError('cancelled'));
|
|
645
|
+
}
|
|
646
|
+
catch { /* renewal and the watchdog still fence the work */ }
|
|
647
|
+
finally {
|
|
648
|
+
clearTimeout(deadline);
|
|
649
|
+
if (pending === id)
|
|
650
|
+
pending = null;
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
const cancelCheck = setInterval(() => { void checkCancel(); }, cancelCheckMs);
|
|
422
654
|
const outcome = {};
|
|
423
655
|
try {
|
|
424
656
|
input.journal?.update(command.id, { state: 'executing' });
|
|
@@ -431,6 +663,7 @@ export async function handleCommand(input) {
|
|
|
431
663
|
finally {
|
|
432
664
|
clearInterval(renewal);
|
|
433
665
|
clearInterval(watchdog);
|
|
666
|
+
clearInterval(cancelCheck);
|
|
434
667
|
}
|
|
435
668
|
const receipt = receiptFor(command.leaseToken, outcome, controller.signal.aborted);
|
|
436
669
|
input.journal?.update(command.id, { state: 'finished', receipt });
|
|
@@ -455,10 +688,10 @@ export async function serve(options) {
|
|
|
455
688
|
const signal = options.signal ? AbortSignal.any([options.signal, fatal.signal]) : fatal.signal;
|
|
456
689
|
const home = options.home ?? homedir(), run = options.run ?? defaultRun, roots = options.roots ?? defaultRoots(home), log = options.log ?? (line => console.error(line));
|
|
457
690
|
const api = options.client.sessions(options.namespace), tmux = await hasTmux(run);
|
|
458
|
-
const capabilities = { providers: ['codex', 'claude'], isolation: options.podConfig ? ['repo', 'worktree', 'pod'] : ['repo', 'worktree'], modes: tmux ? ['tmux', 'headless'] : ['headless'] };
|
|
691
|
+
const capabilities = { providers: ['codex', 'claude'], isolation: options.podConfig ? ['repo', 'worktree', 'pod'] : ['repo', 'worktree'], modes: tmux ? ['tmux', 'headless'] : ['headless'], ...(options.maxHumanPermission ? { humanPermissionCap: options.maxHumanPermission } : {}) };
|
|
459
692
|
const key = runnerKey(options.stateFile ?? join(home, '.dreamlake', 'session-runner.json'));
|
|
460
693
|
const runner = await api.runners.register({ runnerKey: key, hostname: hostname(), unixUser: userInfo().username, version: options.version, capabilities });
|
|
461
|
-
log(`runner ${runner.id} online (${capabilities.modes.join('/')}) for @${options.namespace}; Ctrl-C stops`);
|
|
694
|
+
log(`runner ${runner.id} online (${capabilities.modes.join('/')}${capabilities.humanPermissionCap ? `, human cap ${capabilities.humanPermissionCap}` : ', no human raise cap'}) for @${options.namespace}; Ctrl-C stops`);
|
|
462
695
|
const journal = new RunnerJournal(join(options.stateFile ? join(options.stateFile, '..') : join(home, '.dreamlake'), 'session-command-journal'));
|
|
463
696
|
const quarantined = new Set();
|
|
464
697
|
for (const e of journal.list())
|
|
@@ -509,7 +742,8 @@ export async function serve(options) {
|
|
|
509
742
|
await recover();
|
|
510
743
|
let recoveryAfter = Date.now() + 5000;
|
|
511
744
|
const active = new ActiveProcesses(), slots = new Semaphore(Math.max(1, options.concurrency ?? 4));
|
|
512
|
-
const
|
|
745
|
+
const beats = new LiveBeats(api, runner.id, log, options.heartbeatMs);
|
|
746
|
+
const executors = commandExecutors({ api, run, roots, home, tmux, runnerKey: key, active, slots, journal, quarantined, trustedRepositories: options.trustedRepositories ?? options.podConfig?.repositories ?? [process.cwd()], maxAgentPermission: options.maxAgentPermission, maxHumanPermission: options.maxHumanPermission, pods: options.podConfig ? new Pods(run, options.podConfig, key) : undefined, beats });
|
|
513
747
|
const inFlight = new Set();
|
|
514
748
|
let handled = 0;
|
|
515
749
|
signal.addEventListener('abort', () => active.cancelAll(), { once: true });
|
|
@@ -551,6 +785,17 @@ export async function serve(options) {
|
|
|
551
785
|
}
|
|
552
786
|
finally {
|
|
553
787
|
await Promise.all(inFlight);
|
|
788
|
+
await Promise.allSettled([...beats.pending]);
|
|
789
|
+
// Whatever ended the loop (Ctrl-C, SIGTERM, --once, a fatal error): every receipt is posted, so the
|
|
790
|
+
// runner can say goodbye instead of staying "online" until the server's heartbeat window passes.
|
|
791
|
+
try {
|
|
792
|
+
await api.runners.deregister(runner.id);
|
|
793
|
+
log(`runner ${runner.id} offline`);
|
|
794
|
+
}
|
|
795
|
+
catch (error) {
|
|
796
|
+
const reason = error instanceof ApiError ? (error.status === 404 || error.status === 405 ? 'this server has no deregistration route; it lists the runner offline once the heartbeat window passes' : error.code) : 'network';
|
|
797
|
+
log(`runner ${runner.id}: deregistration failed (${reason})`);
|
|
798
|
+
}
|
|
554
799
|
}
|
|
555
800
|
if (failure)
|
|
556
801
|
throw failure;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dreamlake claude …` / `dreamlake codex …`: the stock CLI, started under DreamLake. Light integration —
|
|
3
|
+
* the wrapper mints the native session id (Claude) or discovers it afterwards (Codex), registers the
|
|
4
|
+
* session (idempotent), exports the saved-session pair so every `dreamlake` call the session makes links to
|
|
5
|
+
* it, maps `--permission` onto the provider's own flag, hands the terminal over untouched, and uploads the
|
|
6
|
+
* transcript when the provider exits. Everything after the wrapper's own flags goes to the provider as is.
|
|
7
|
+
*/
|
|
8
|
+
import type { Command } from 'commander';
|
|
9
|
+
import { type ChildProcess } from 'node:child_process';
|
|
10
|
+
import { type DreamLakeClient, type PermissionLevel } from '../../api/index.js';
|
|
11
|
+
import { type NativeRoots, type Provider } from './runner/native.js';
|
|
12
|
+
export interface WrapOptions {
|
|
13
|
+
permission?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
namespace?: string;
|
|
16
|
+
visibility?: string;
|
|
17
|
+
upload?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare function providerArgv(provider: Provider, level: PermissionLevel, rest: string[], input: {
|
|
20
|
+
nativeSessionId?: string;
|
|
21
|
+
cwd: string;
|
|
22
|
+
}): string[];
|
|
23
|
+
export declare const needLevel: (value: unknown) => PermissionLevel;
|
|
24
|
+
/** The one Codex rollout this launch produced: created in this directory at or after the moment the
|
|
25
|
+
* launch began — a rollout from before it, however recent, is another session's. Two candidates mean
|
|
26
|
+
* another Codex started here meanwhile; nothing is guessed then. */
|
|
27
|
+
export declare function discoverCodexRollout(roots: NativeRoots, cwd: string, since: number): {
|
|
28
|
+
path: string;
|
|
29
|
+
nativeSessionId: string;
|
|
30
|
+
} | {
|
|
31
|
+
ambiguous: string[];
|
|
32
|
+
} | null;
|
|
33
|
+
/** The provider child's environment: the wrapper's own saved-session pair when it is known (Claude), and
|
|
34
|
+
* never an inherited one — a `dreamlake claude` started from a shell that sourced another session's env
|
|
35
|
+
* file, or from inside another agent session, must not link the child's writes to that parent. Inherited
|
|
36
|
+
* provider identities go too: the child sets its own (`CLAUDE_CODE_SESSION_ID`, `CODEX_THREAD_ID`), and a
|
|
37
|
+
* stale one from a parent agent would make the child's writes ambiguous or misattributed. */
|
|
38
|
+
export declare function childEnv(base: NodeJS.ProcessEnv, pair?: {
|
|
39
|
+
id: string;
|
|
40
|
+
namespace: string;
|
|
41
|
+
}): NodeJS.ProcessEnv;
|
|
42
|
+
export interface WrapDeps {
|
|
43
|
+
client: DreamLakeClient;
|
|
44
|
+
namespace: string;
|
|
45
|
+
roots: NativeRoots;
|
|
46
|
+
cwd: string;
|
|
47
|
+
home: string;
|
|
48
|
+
log: (line: string) => void;
|
|
49
|
+
spawn?: (command: string, args: string[], env: NodeJS.ProcessEnv) => ChildProcess;
|
|
50
|
+
now?: () => number;
|
|
51
|
+
env?: NodeJS.ProcessEnv;
|
|
52
|
+
}
|
|
53
|
+
export interface WrapResult {
|
|
54
|
+
exitCode: number;
|
|
55
|
+
sessionId: string | null;
|
|
56
|
+
nativeSessionId: string | null;
|
|
57
|
+
uploaded: boolean;
|
|
58
|
+
envFile: string | null;
|
|
59
|
+
}
|
|
60
|
+
/** Run the provider under DreamLake. Registration failures before the launch are fatal (nothing ran);
|
|
61
|
+
* every DreamLake call after the provider started is best effort and reported, never the exit code. */
|
|
62
|
+
export declare function wrap(provider: Provider, opts: WrapOptions, rest: string[], deps: WrapDeps): Promise<WrapResult>;
|
|
63
|
+
export declare function registerAgentWrappers(program: Command): void;
|