rookery-agent 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/package.json +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/core/dist/index.d.ts +1 -0
- package/packages/core/dist/index.d.ts.map +1 -1
- package/packages/core/dist/index.js +1 -0
- package/packages/core/dist/index.js.map +1 -1
- package/packages/core/dist/org/controller.d.ts.map +1 -1
- package/packages/core/dist/org/controller.js +66 -4
- package/packages/core/dist/org/controller.js.map +1 -1
- package/packages/core/dist/providers/claude-login.d.ts +30 -0
- package/packages/core/dist/providers/claude-login.d.ts.map +1 -0
- package/packages/core/dist/providers/claude-login.js +92 -0
- package/packages/core/dist/providers/claude-login.js.map +1 -0
- package/packages/core/dist/providers/claude-tui.d.ts +74 -1
- package/packages/core/dist/providers/claude-tui.d.ts.map +1 -1
- package/packages/core/dist/providers/claude-tui.js +233 -4
- package/packages/core/dist/providers/claude-tui.js.map +1 -1
- package/packages/core/dist/util/interim-report.d.ts +12 -0
- package/packages/core/dist/util/interim-report.d.ts.map +1 -0
- package/packages/core/dist/util/interim-report.js +54 -0
- package/packages/core/dist/util/interim-report.js.map +1 -0
- package/packages/core/package.json +1 -1
- package/packages/server/dist/gateways/telegram.d.ts +9 -0
- package/packages/server/dist/gateways/telegram.d.ts.map +1 -1
- package/packages/server/dist/gateways/telegram.js +96 -2
- package/packages/server/dist/gateways/telegram.js.map +1 -1
- package/packages/server/package.json +1 -1
- package/packages/web/package.json +1 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type PtyModule } from './claude-tui.js';
|
|
2
|
+
import { type ResolvedBinary } from './process.js';
|
|
3
|
+
/** Long enough to sign in on a phone, short enough not to leave a process behind. */
|
|
4
|
+
export declare const LOGIN_TIMEOUT_MS: number;
|
|
5
|
+
export interface ClaudeLoginResult {
|
|
6
|
+
ok: boolean;
|
|
7
|
+
/** The CLI's last line, e.g. "Login successful." or "Login failed: …". */
|
|
8
|
+
message: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ClaudeLogin {
|
|
11
|
+
/** The sign-in page; rejects when the CLI never printed one. */
|
|
12
|
+
url: Promise<string>;
|
|
13
|
+
/** Resolves once the CLI is gone. */
|
|
14
|
+
result: Promise<ClaudeLoginResult>;
|
|
15
|
+
/** Type the code the sign-in page showed. */
|
|
16
|
+
submitCode(code: string): void;
|
|
17
|
+
cancel(): void;
|
|
18
|
+
}
|
|
19
|
+
export interface ClaudeLoginOptions {
|
|
20
|
+
binary?: ResolvedBinary;
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
/** Tests hand in a fake pty here; otherwise the native binding is loaded. */
|
|
23
|
+
pty?: PtyModule;
|
|
24
|
+
}
|
|
25
|
+
/** The sign-in page in what the CLI printed so far, if it is there yet. */
|
|
26
|
+
export declare function signInUrlIn(output: string): string | null;
|
|
27
|
+
/** What the CLI's exit means, in its own last words. */
|
|
28
|
+
export declare function loginOutcome(output: string, exitCode: number): ClaudeLoginResult;
|
|
29
|
+
export declare function startClaudeLogin(options?: ClaudeLoginOptions): Promise<ClaudeLogin>;
|
|
30
|
+
//# sourceMappingURL=claude-login.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-login.d.ts","sourceRoot":"","sources":["../../src/providers/claude-login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAA8B,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAe/E,qFAAqF;AACrF,eAAO,MAAM,gBAAgB,QAAiB,CAAC;AAU/C,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,OAAO,CAAC;IACZ,0EAA0E;IAC1E,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,gEAAgE;IAChE,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACrB,qCAAqC;IACrC,MAAM,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAC;IACnC,6CAA6C;IAC7C,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,MAAM,IAAI,IAAI,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6EAA6E;IAC7E,GAAG,CAAC,EAAE,SAAS,CAAC;CACjB;AAED,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAEzD;AAED,wDAAwD;AACxD,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,iBAAiB,CAKhF;AAMD,wBAAsB,gBAAgB,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,WAAW,CAAC,CA0D7F"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { loadPty } from './claude-tui.js';
|
|
2
|
+
import { quoteForCmd, resolveBinary } from './process.js';
|
|
3
|
+
/**
|
|
4
|
+
* `claude auth login`, driven from somewhere that is not a terminal.
|
|
5
|
+
*
|
|
6
|
+
* The CLI prints the sign-in page, then waits on "Paste code here if
|
|
7
|
+
* prompted >" - but only when both ends are a TTY, so it runs in a pty. The
|
|
8
|
+
* browser it would open lands on the server's desktop, where nobody is when
|
|
9
|
+
* the login is done from a phone; `BROWSER=none` keeps it shut. The code the
|
|
10
|
+
* sign-in page shows afterwards is typed in, and the exit code says whether
|
|
11
|
+
* the new token was stored.
|
|
12
|
+
*/
|
|
13
|
+
const MINUTE_MS = 60_000;
|
|
14
|
+
/** Long enough to sign in on a phone, short enough not to leave a process behind. */
|
|
15
|
+
export const LOGIN_TIMEOUT_MS = 10 * MINUTE_MS;
|
|
16
|
+
/** How long the CLI gets to print the sign-in page. */
|
|
17
|
+
const URL_TIMEOUT_MS = 30_000;
|
|
18
|
+
const SIGN_IN_URL = /https:\/\/\S+?\/oauth\/authorize\?[^\s\x07\x1b]+/;
|
|
19
|
+
const OSC_SEQUENCE = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
20
|
+
const CSI_SEQUENCE = /\x1b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
21
|
+
const CODE_PROMPT = /^.*Paste code here if prompted >\s*/;
|
|
22
|
+
/** The sign-in page in what the CLI printed so far, if it is there yet. */
|
|
23
|
+
export function signInUrlIn(output) {
|
|
24
|
+
return SIGN_IN_URL.exec(output)?.[0] ?? null;
|
|
25
|
+
}
|
|
26
|
+
/** What the CLI's exit means, in its own last words. */
|
|
27
|
+
export function loginOutcome(output, exitCode) {
|
|
28
|
+
const lines = plainText(output).split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
29
|
+
const last = lines.at(-1)?.replace(CODE_PROMPT, '') ?? '';
|
|
30
|
+
const fallback = exitCode === 0 ? 'Login successful.' : `Login failed (exit code ${exitCode}).`;
|
|
31
|
+
return { ok: exitCode === 0, message: last || fallback };
|
|
32
|
+
}
|
|
33
|
+
function plainText(output) {
|
|
34
|
+
return output.replace(OSC_SEQUENCE, '').replace(CSI_SEQUENCE, '');
|
|
35
|
+
}
|
|
36
|
+
export async function startClaudeLogin(options = {}) {
|
|
37
|
+
const binary = options.binary ?? resolveBinary('claude');
|
|
38
|
+
if (!binary)
|
|
39
|
+
throw new Error('Claude Code is not installed: `claude` is not on PATH.');
|
|
40
|
+
const pty = options.pty ?? (await loadPty());
|
|
41
|
+
if (!pty)
|
|
42
|
+
throw new Error('No terminal available on this platform to run `claude auth login` in.');
|
|
43
|
+
const env = {};
|
|
44
|
+
for (const [name, value] of Object.entries(process.env))
|
|
45
|
+
if (value !== undefined)
|
|
46
|
+
env[name] = value;
|
|
47
|
+
env.BROWSER = 'none';
|
|
48
|
+
const terminal = { name: 'xterm-256color', cols: 200, rows: 40, env };
|
|
49
|
+
const argv = ['auth', 'login'];
|
|
50
|
+
const child = binary.isShim
|
|
51
|
+
? pty.spawn(env.COMSPEC ?? 'cmd.exe', '/d /s /c ' + quoteForCmd([binary.path, ...argv]), terminal)
|
|
52
|
+
: pty.spawn(binary.path, argv, terminal);
|
|
53
|
+
let output = '';
|
|
54
|
+
let settled = false;
|
|
55
|
+
let resolveUrl = () => { };
|
|
56
|
+
let rejectUrl = () => { };
|
|
57
|
+
let finish = () => { };
|
|
58
|
+
const url = new Promise((resolve, reject) => {
|
|
59
|
+
resolveUrl = resolve;
|
|
60
|
+
rejectUrl = reject;
|
|
61
|
+
});
|
|
62
|
+
// A caller that only waits for the result must not see an unhandled rejection.
|
|
63
|
+
url.catch(() => { });
|
|
64
|
+
const result = new Promise((resolve) => {
|
|
65
|
+
const urlTimer = setTimeout(() => rejectUrl(new Error('Claude Code printed no sign-in page.')), URL_TIMEOUT_MS);
|
|
66
|
+
const giveUp = setTimeout(() => finish({ ok: false, message: 'The login timed out.' }), options.timeoutMs ?? LOGIN_TIMEOUT_MS);
|
|
67
|
+
finish = (outcome) => {
|
|
68
|
+
if (settled)
|
|
69
|
+
return;
|
|
70
|
+
settled = true;
|
|
71
|
+
clearTimeout(giveUp);
|
|
72
|
+
clearTimeout(urlTimer);
|
|
73
|
+
rejectUrl(new Error(outcome.message));
|
|
74
|
+
child.kill();
|
|
75
|
+
resolve(outcome);
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
child.onData((data) => {
|
|
79
|
+
output += data;
|
|
80
|
+
const found = signInUrlIn(output);
|
|
81
|
+
if (found)
|
|
82
|
+
resolveUrl(found);
|
|
83
|
+
});
|
|
84
|
+
child.onExit(({ exitCode }) => finish(loginOutcome(output, exitCode)));
|
|
85
|
+
return {
|
|
86
|
+
url,
|
|
87
|
+
result,
|
|
88
|
+
submitCode: (code) => child.write(code.trim() + '\r'),
|
|
89
|
+
cancel: () => finish({ ok: false, message: 'The login was cancelled.' }),
|
|
90
|
+
};
|
|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AAC;YACvB,SAAS,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;YACtC,KAAK,CAAC,IAAI,EAAE,CAAC;YACb,OAAO,CAAC,OAAO,CAAC,CAAC;QACnB,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;QACpB,MAAM,IAAI,IAAI,CAAC;QACf,MAAM,KAAK,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,KAAK;YAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;IACH,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEvE,OAAO;QACL,GAAG;QACH,MAAM;QACN,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC;QACrD,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,0BAA0B,EAAE,CAAC;KACzE,CAAC;AACJ,CAAC"}
|
|
@@ -47,7 +47,7 @@ interface PtyProcess {
|
|
|
47
47
|
resize(cols: number, rows: number): void;
|
|
48
48
|
kill(signal?: string): void;
|
|
49
49
|
}
|
|
50
|
-
interface PtyModule {
|
|
50
|
+
export interface PtyModule {
|
|
51
51
|
spawn(file: string, args: string[] | string, options: {
|
|
52
52
|
name: string;
|
|
53
53
|
cols: number;
|
|
@@ -133,6 +133,8 @@ export interface TuiSpawnSpec {
|
|
|
133
133
|
/** Runs once the process is gone - the temp folders go then, not earlier. */
|
|
134
134
|
cleanup: () => void;
|
|
135
135
|
lingerMs?: number;
|
|
136
|
+
/** Tests hand in a fake pty here; otherwise the native binding is loaded. */
|
|
137
|
+
pty?: PtyModule;
|
|
136
138
|
}
|
|
137
139
|
export interface TuiRunSpec extends TuiSpawnSpec {
|
|
138
140
|
prompt: string;
|
|
@@ -164,6 +166,8 @@ declare class TerminalWatch {
|
|
|
164
166
|
contextTokens: number | undefined;
|
|
165
167
|
/** The model of the newest assistant entry - `/model` can change it mid-session. */
|
|
166
168
|
model: string | undefined;
|
|
169
|
+
/** Sub-agents, workflows and background commands the session has not heard back from. */
|
|
170
|
+
readonly work: BackgroundWork;
|
|
167
171
|
constructor(spec: TuiSpawnSpec,
|
|
168
172
|
/** A resumed session: its transcript already has a history to skip. */
|
|
169
173
|
resumed: boolean);
|
|
@@ -192,6 +196,75 @@ declare class TerminalWatch {
|
|
|
192
196
|
holds: boolean;
|
|
193
197
|
}>;
|
|
194
198
|
}
|
|
199
|
+
/** What a session still has running in the background, by kind. */
|
|
200
|
+
export interface PendingWork {
|
|
201
|
+
agents: number;
|
|
202
|
+
workflows: number;
|
|
203
|
+
commands: number;
|
|
204
|
+
/** Of `agents`, those that sent an interim result and may or may not notify again. */
|
|
205
|
+
interim: number;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The work a Claude Code session started in the background and has not heard
|
|
209
|
+
* back from, read off its transcript.
|
|
210
|
+
*
|
|
211
|
+
* The Stop hook fires whenever the agent stops talking - also when it has
|
|
212
|
+
* just sent sub-agents off (`Agent` with `run_in_background`), started a
|
|
213
|
+
* workflow or a background command, and says it will write the report once
|
|
214
|
+
* they are back. Claude Code picks the conversation back up by itself when
|
|
215
|
+
* one of them finishes, so such a stop is not the end of the run.
|
|
216
|
+
*
|
|
217
|
+
* Two sources, because neither covers everything: the launches and their
|
|
218
|
+
* `<task-notification>`s (agents and commands, by id), and the
|
|
219
|
+
* `pendingBackgroundAgentCount` / `pendingWorkflowCount` Claude Code writes
|
|
220
|
+
* on the `turn_duration` entry right after a stop (the only trace of a
|
|
221
|
+
* workflow). That count is a snapshot that stops holding once the
|
|
222
|
+
* conversation goes on, so the next message drops it. Seen live, it also
|
|
223
|
+
* leaves out sub-agents that wait on background work of their own; those stay
|
|
224
|
+
* open here, counted as `interim`, until a notification for them no longer
|
|
225
|
+
* says so.
|
|
226
|
+
*/
|
|
227
|
+
export declare class BackgroundWork {
|
|
228
|
+
#private;
|
|
229
|
+
/**
|
|
230
|
+
* A result came back after the agent last spoke: Claude Code wakes the
|
|
231
|
+
* agent with it by itself. Seen live - the notifications land right behind the stop,
|
|
232
|
+
* and a nudge typed then only queues a second, redundant turn.
|
|
233
|
+
*/
|
|
234
|
+
resuming: boolean;
|
|
235
|
+
observe(entry: Record<string, unknown>): void;
|
|
236
|
+
pending(): PendingWork;
|
|
237
|
+
}
|
|
238
|
+
export declare const pendingTotal: (pending: PendingWork) => number;
|
|
239
|
+
/** "2 sub-agents and 1 background command", for the card and the timeout message. */
|
|
240
|
+
export declare function describePending(pending: PendingWork): string;
|
|
241
|
+
/** How a run's gate has dealt with its agent's stops so far. */
|
|
242
|
+
export interface StopGate {
|
|
243
|
+
commandNudges: number;
|
|
244
|
+
interimNudged: boolean;
|
|
245
|
+
}
|
|
246
|
+
/** What a stop means for the run: the end, or not yet - and whether the agent is told something. */
|
|
247
|
+
export type StopVerdict = {
|
|
248
|
+
action: 'finish';
|
|
249
|
+
} | {
|
|
250
|
+
action: 'wait';
|
|
251
|
+
detail: string;
|
|
252
|
+
nudge?: string;
|
|
253
|
+
finishAfterMs?: number;
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* The completion gate. A stop while sub-agents or workflows run is waited
|
|
257
|
+
* out: Claude Code wakes the agent with each result, and the stop after the
|
|
258
|
+
* last one is the real end. Background commands wake nobody once the agent
|
|
259
|
+
* no longer cares about them (a dev server left running), so the agent is
|
|
260
|
+
* asked to settle them. A sub-agent that has only sent an interim result
|
|
261
|
+
* holds the run while the agent's own report still says it is waiting; after
|
|
262
|
+
* a final-sounding one the run ends unless a result arrives within a grace
|
|
263
|
+
* period - live, some such sub-agents notified again and others never did.
|
|
264
|
+
* A report that itself says it is still waiting, with
|
|
265
|
+
* nothing left running, gets one request for the final report.
|
|
266
|
+
*/
|
|
267
|
+
export declare function stopVerdict(pending: PendingWork, text: string, gate: StopGate, resuming?: boolean): StopVerdict;
|
|
195
268
|
/**
|
|
196
269
|
* Runs Claude Code in a pty and yields what a print run would have yielded.
|
|
197
270
|
* Throws only before the process exists; afterwards failures are events.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-tui.d.ts","sourceRoot":"","sources":["../../src/providers/claude-tui.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"claude-tui.d.ts","sourceRoot":"","sources":["../../src/providers/claude-tui.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAe,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAGhE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,4DAA4D;AAC5D,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC;AAErD,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,QAAQ,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wDAAwD;AACxD,UAAU,UAAU;IAClB,MAAM,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IAClD,MAAM,CAAC,Q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,MAAM,EAAE,MAAM,CAAC;IACf,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;IACrC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;;;;;;GAQG;AACH,qBAAa,oBAAoB;;IAW7B,QAAQ,CAAC,KAAK,EAAE,aAAa;IAC7B,QAAQ,CAAC,QAAQ,EAAE,uBAAuB;gBADjC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,uBAAuB;IAG5C,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,KAAK,IAAI,OAAO;IAIhB,+DAA+D;IAC/D,IAAI,IAAI,IAAI,OAAO,CAElB;IAED,KAAK,IAAI,IAAI;IAIb,qFAAqF;IAC/E,QAAQ,CAAC,OAAO,SAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ3D;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC;IAkMlD,wFAAwF;IACxF,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IA4B7B,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAiB/B,oFAAoF;IACpF,MAAM,IAAI,IAAI;CAIf;AAqCD;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC,oBAAoB,CAAC,CAQ/B"}
|
|
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events';
|
|
|
2
2
|
import { access, open, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
+
import { looksInterim } from '../util/interim-report.js';
|
|
5
6
|
import { quoteForCmd } from './process.js';
|
|
6
7
|
import { userSettingsGuard } from './user-settings-guard.js';
|
|
7
8
|
const MINUTE_MS = 60_000;
|
|
@@ -35,6 +36,14 @@ const SETTLE_LIMIT_MS = 3000;
|
|
|
35
36
|
const POLL_MS = 250;
|
|
36
37
|
/** How long after a Stop the transcript is watched for work a blocking hook set going again. */
|
|
37
38
|
const HOOK_BLOCK_GRACE_MS = 4 * POLL_MS;
|
|
39
|
+
/**
|
|
40
|
+
* Background work finished, Claude Code stopped while it ran, and nothing has
|
|
41
|
+
* picked the work back up for this long: the agent is asked once for its
|
|
42
|
+
* final report instead of leaving the run hanging until its timeout.
|
|
43
|
+
*/
|
|
44
|
+
const RESUME_GRACE_MS = 30_000;
|
|
45
|
+
/** How often an agent is asked to settle the background commands it left running. */
|
|
46
|
+
const MAX_COMMAND_NUDGES = 2;
|
|
38
47
|
/** The slower pace at which a lingering terminal's transcript is followed. */
|
|
39
48
|
const LATE_POLL_MS = 4 * POLL_MS;
|
|
40
49
|
const ESC = String.fromCharCode(27);
|
|
@@ -502,7 +511,9 @@ function typedPrompt(entry) {
|
|
|
502
511
|
return null;
|
|
503
512
|
text = blocks.map((block) => (block.type === 'text' && typeof block.text === 'string' ? block.text : '')).join('\n');
|
|
504
513
|
}
|
|
505
|
-
text =
|
|
514
|
+
// Claude Code 2.1.296+ wraps pasted text in <pasted_content id="..."> tags;
|
|
515
|
+
// read as a wrapper, every report-back looked unsent and was typed again.
|
|
516
|
+
text = text.replace(/<\/?pasted_content(?: id="[^"]*")?>/g, '').trim();
|
|
506
517
|
if (!text || text.startsWith('<'))
|
|
507
518
|
return null;
|
|
508
519
|
return text;
|
|
@@ -553,6 +564,8 @@ class TerminalWatch {
|
|
|
553
564
|
contextTokens;
|
|
554
565
|
/** The model of the newest assistant entry - `/model` can change it mid-session. */
|
|
555
566
|
model;
|
|
567
|
+
/** Sub-agents, workflows and background commands the session has not heard back from. */
|
|
568
|
+
work = new BackgroundWork();
|
|
556
569
|
#tail = null;
|
|
557
570
|
/** The read in flight, if any; the next one starts when it is over. */
|
|
558
571
|
#reading = Promise.resolve();
|
|
@@ -594,6 +607,7 @@ class TerminalWatch {
|
|
|
594
607
|
const events = [];
|
|
595
608
|
for (const entry of await tail.read()) {
|
|
596
609
|
this.lastActivity = Date.now();
|
|
610
|
+
this.work.observe(entry);
|
|
597
611
|
if (entry.isSidechain === true)
|
|
598
612
|
continue;
|
|
599
613
|
const typed = typedPrompt(entry);
|
|
@@ -673,6 +687,178 @@ class TerminalWatch {
|
|
|
673
687
|
return { events, holds: this.assistantEntries === before };
|
|
674
688
|
}
|
|
675
689
|
}
|
|
690
|
+
const TASK_NOTIFICATION = /<task-notification>([\s\S]*?)<\/task-notification>/g;
|
|
691
|
+
const TASK_ID = /<task-id>([^<]+)<\/task-id>/;
|
|
692
|
+
/**
|
|
693
|
+
* A sub-agent that stopped with background work of its own still running
|
|
694
|
+
* (Claude Code 2.1.299). Seen live, the label is no promise either way: some
|
|
695
|
+
* notified again with "finished", others delivered their final result under
|
|
696
|
+
* the same label and never notified again.
|
|
697
|
+
*/
|
|
698
|
+
const STILL_WORKING = /is waiting on its own background work|result below may be interim/;
|
|
699
|
+
const countOf = (value) => (typeof value === 'number' && value > 0 ? value : 0);
|
|
700
|
+
/** The text of a transcript entry's message, whether a plain string or text blocks. */
|
|
701
|
+
function entryText(entry) {
|
|
702
|
+
const content = entry.message?.content;
|
|
703
|
+
if (typeof content === 'string')
|
|
704
|
+
return content;
|
|
705
|
+
if (!Array.isArray(content))
|
|
706
|
+
return '';
|
|
707
|
+
return content
|
|
708
|
+
.map((block) => (block.type === 'text' && typeof block.text === 'string' ? block.text : ''))
|
|
709
|
+
.join('\n');
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* The work a Claude Code session started in the background and has not heard
|
|
713
|
+
* back from, read off its transcript.
|
|
714
|
+
*
|
|
715
|
+
* The Stop hook fires whenever the agent stops talking - also when it has
|
|
716
|
+
* just sent sub-agents off (`Agent` with `run_in_background`), started a
|
|
717
|
+
* workflow or a background command, and says it will write the report once
|
|
718
|
+
* they are back. Claude Code picks the conversation back up by itself when
|
|
719
|
+
* one of them finishes, so such a stop is not the end of the run.
|
|
720
|
+
*
|
|
721
|
+
* Two sources, because neither covers everything: the launches and their
|
|
722
|
+
* `<task-notification>`s (agents and commands, by id), and the
|
|
723
|
+
* `pendingBackgroundAgentCount` / `pendingWorkflowCount` Claude Code writes
|
|
724
|
+
* on the `turn_duration` entry right after a stop (the only trace of a
|
|
725
|
+
* workflow). That count is a snapshot that stops holding once the
|
|
726
|
+
* conversation goes on, so the next message drops it. Seen live, it also
|
|
727
|
+
* leaves out sub-agents that wait on background work of their own; those stay
|
|
728
|
+
* open here, counted as `interim`, until a notification for them no longer
|
|
729
|
+
* says so.
|
|
730
|
+
*/
|
|
731
|
+
export class BackgroundWork {
|
|
732
|
+
#open = new Map();
|
|
733
|
+
/** Open sub-agents whose last notification was an interim one. */
|
|
734
|
+
#interim = new Set();
|
|
735
|
+
#reported = { agents: 0, workflows: 0 };
|
|
736
|
+
/**
|
|
737
|
+
* A result came back after the agent last spoke: Claude Code wakes the
|
|
738
|
+
* agent with it by itself. Seen live - the notifications land right behind the stop,
|
|
739
|
+
* and a nudge typed then only queues a second, redundant turn.
|
|
740
|
+
*/
|
|
741
|
+
resuming = false;
|
|
742
|
+
observe(entry) {
|
|
743
|
+
if (entry.isSidechain === true)
|
|
744
|
+
return;
|
|
745
|
+
if (entry.type === 'system' && entry.subtype === 'turn_duration') {
|
|
746
|
+
this.#reported = {
|
|
747
|
+
agents: countOf(entry.pendingBackgroundAgentCount),
|
|
748
|
+
workflows: countOf(entry.pendingWorkflowCount),
|
|
749
|
+
};
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (entry.type !== 'user' && entry.type !== 'assistant')
|
|
753
|
+
return;
|
|
754
|
+
this.#reported = { agents: 0, workflows: 0 };
|
|
755
|
+
if (entry.type !== 'user') {
|
|
756
|
+
this.resuming = false;
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
const result = entry.toolUseResult;
|
|
760
|
+
if (result?.isAsync === true && typeof result.agentId === 'string')
|
|
761
|
+
this.#open.set(result.agentId, 'agents');
|
|
762
|
+
if (typeof result?.backgroundTaskId === 'string')
|
|
763
|
+
this.#open.set(result.backgroundTaskId, 'commands');
|
|
764
|
+
for (const [, block = ''] of entryText(entry).matchAll(TASK_NOTIFICATION)) {
|
|
765
|
+
const id = block.match(TASK_ID)?.[1]?.trim();
|
|
766
|
+
if (!id)
|
|
767
|
+
continue;
|
|
768
|
+
this.resuming = true;
|
|
769
|
+
if (STILL_WORKING.test(block)) {
|
|
770
|
+
this.#interim.add(id);
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
this.#open.delete(id);
|
|
774
|
+
this.#interim.delete(id);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
pending() {
|
|
779
|
+
const open = [...this.#open.values()];
|
|
780
|
+
const agents = open.filter((kind) => kind === 'agents').length;
|
|
781
|
+
const interim = [...this.#interim].filter((id) => this.#open.has(id)).length;
|
|
782
|
+
return {
|
|
783
|
+
// Claude Code's own count leaves the interim ones out.
|
|
784
|
+
agents: Math.max(agents - interim, this.#reported.agents) + interim,
|
|
785
|
+
workflows: this.#reported.workflows,
|
|
786
|
+
commands: open.length - agents,
|
|
787
|
+
interim,
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
export const pendingTotal = (pending) => pending.agents + pending.workflows + pending.commands;
|
|
792
|
+
/** "2 sub-agents and 1 background command", for the card and the timeout message. */
|
|
793
|
+
export function describePending(pending) {
|
|
794
|
+
const parts = [];
|
|
795
|
+
const add = (count, one, many) => {
|
|
796
|
+
if (count > 0)
|
|
797
|
+
parts.push(count + ' ' + (count === 1 ? one : many));
|
|
798
|
+
};
|
|
799
|
+
add(pending.agents, 'sub-agent', 'sub-agents');
|
|
800
|
+
add(pending.workflows, 'workflow', 'workflows');
|
|
801
|
+
add(pending.commands, 'background command', 'background commands');
|
|
802
|
+
const last = parts.pop() ?? 'nothing';
|
|
803
|
+
return parts.length ? parts.join(', ') + ' and ' + last : last;
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* How long a final-sounding report waits for sub-agents that only sent an
|
|
807
|
+
* interim result. ponytail: a fixed grace; a sub-agent that reports again
|
|
808
|
+
* later than this after a report that did not sound like waiting is cut off.
|
|
809
|
+
*/
|
|
810
|
+
const INTERIM_AGENT_GRACE_MS = 2 * MINUTE_MS;
|
|
811
|
+
const COMMANDS_NUDGE = '[Rookery] Your run only ends once every background command you started has finished. ' +
|
|
812
|
+
'If you still need their output, wait for it; if not, stop them with TaskStop. Then give your final report.';
|
|
813
|
+
const INTERIM_NUDGE = '[Rookery] Your last message says you are still waiting for something, but nothing you started is running any more. ' +
|
|
814
|
+
'Finish the work now - what you answer next is the final report.';
|
|
815
|
+
const RESUME_NUDGE = '[Rookery] All background work you started has finished. Use its results and give your final report now.';
|
|
816
|
+
/**
|
|
817
|
+
* The completion gate. A stop while sub-agents or workflows run is waited
|
|
818
|
+
* out: Claude Code wakes the agent with each result, and the stop after the
|
|
819
|
+
* last one is the real end. Background commands wake nobody once the agent
|
|
820
|
+
* no longer cares about them (a dev server left running), so the agent is
|
|
821
|
+
* asked to settle them. A sub-agent that has only sent an interim result
|
|
822
|
+
* holds the run while the agent's own report still says it is waiting; after
|
|
823
|
+
* a final-sounding one the run ends unless a result arrives within a grace
|
|
824
|
+
* period - live, some such sub-agents notified again and others never did.
|
|
825
|
+
* A report that itself says it is still waiting, with
|
|
826
|
+
* nothing left running, gets one request for the final report.
|
|
827
|
+
*/
|
|
828
|
+
export function stopVerdict(pending, text, gate, resuming = false) {
|
|
829
|
+
const total = pendingTotal(pending);
|
|
830
|
+
if (total > pending.interim || (total > 0 && looksInterim(text))) {
|
|
831
|
+
const detail = 'waiting for ' + describePending(pending) + ' before the final report';
|
|
832
|
+
if (pending.agents + pending.workflows > 0 || gate.commandNudges >= MAX_COMMAND_NUDGES)
|
|
833
|
+
return { action: 'wait', detail };
|
|
834
|
+
gate.commandNudges += 1;
|
|
835
|
+
return { action: 'wait', detail, nudge: COMMANDS_NUDGE };
|
|
836
|
+
}
|
|
837
|
+
if (resuming)
|
|
838
|
+
return { action: 'wait', detail: 'all background work is back and Claude Code is picking up the results' };
|
|
839
|
+
if (total > 0) {
|
|
840
|
+
return {
|
|
841
|
+
action: 'wait',
|
|
842
|
+
detail: 'giving ' + describePending(pending) + ' with an interim result a moment to report again',
|
|
843
|
+
finishAfterMs: INTERIM_AGENT_GRACE_MS,
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
if (!gate.interimNudged && looksInterim(text)) {
|
|
847
|
+
gate.interimNudged = true;
|
|
848
|
+
return {
|
|
849
|
+
action: 'wait',
|
|
850
|
+
detail: 'the report said it was still waiting for something; asked for the final report',
|
|
851
|
+
nudge: INTERIM_NUDGE,
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
return { action: 'finish' };
|
|
855
|
+
}
|
|
856
|
+
/** Types one line into a run's terminal and sends it, the way a person would. */
|
|
857
|
+
function nudgeTerminal(key, text) {
|
|
858
|
+
if (!tuiSessions.write(key, text))
|
|
859
|
+
return;
|
|
860
|
+
setTimeout(() => tuiSessions.write(key, '\r'), DIALOG_KEY_DELAY_MS);
|
|
861
|
+
}
|
|
676
862
|
/**
|
|
677
863
|
* The prompt as it goes on the command line. Through a `.cmd` shim the whole
|
|
678
864
|
* command line is cmd.exe's to parse, and a task is outside text - a mail can
|
|
@@ -735,7 +921,7 @@ function startupDialogAnswerer(child, watch) {
|
|
|
735
921
|
}
|
|
736
922
|
/** Starts Claude Code in a pty and registers it. Throws only before the process exists. */
|
|
737
923
|
async function spawnTerminal(spec, resumed) {
|
|
738
|
-
const pty = await loadPty();
|
|
924
|
+
const pty = spec.pty ?? (await loadPty());
|
|
739
925
|
if (!pty)
|
|
740
926
|
throw new Error('Terminal support is not available on this system.');
|
|
741
927
|
const prompt = await promptArgument(spec);
|
|
@@ -803,6 +989,14 @@ export async function* runTui(spec) {
|
|
|
803
989
|
const watch = await spawnTerminal(spec, false);
|
|
804
990
|
const started = Date.now();
|
|
805
991
|
let finished = false;
|
|
992
|
+
const gate = { commandNudges: 0, interimNudged: false };
|
|
993
|
+
/** Set once a stop was not the end: the agent waits for its background work. */
|
|
994
|
+
let parked = false;
|
|
995
|
+
/** Parked on work that was still running; cleared, and said, once it is all back. */
|
|
996
|
+
let awaitingWork = false;
|
|
997
|
+
let resumeNudged = false;
|
|
998
|
+
/** A final-sounding report held for interim sub-agents: it stands unless the agent speaks again in time. */
|
|
999
|
+
let held = null;
|
|
806
1000
|
try {
|
|
807
1001
|
yield { type: 'status', label: 'terminal', detail: 'Claude Code is running in a terminal.' };
|
|
808
1002
|
while (true) {
|
|
@@ -823,16 +1017,51 @@ export async function* runTui(spec) {
|
|
|
823
1017
|
yield fatalError(watch.apiError);
|
|
824
1018
|
return;
|
|
825
1019
|
}
|
|
1020
|
+
const text = reported || watch.lastBlock;
|
|
1021
|
+
const verdict = stopVerdict(watch.work.pending(), text, gate, watch.work.resuming);
|
|
1022
|
+
if (verdict.action === 'wait') {
|
|
1023
|
+
// Not the end: the next stop is judged again once the work is back.
|
|
1024
|
+
await watch.clearMarker();
|
|
1025
|
+
parked = true;
|
|
1026
|
+
awaitingWork = pendingTotal(watch.work.pending()) > 0;
|
|
1027
|
+
resumeNudged = false;
|
|
1028
|
+
held = verdict.finishAfterMs
|
|
1029
|
+
? { text, until: Date.now() + verdict.finishAfterMs, entries: watch.assistantEntries }
|
|
1030
|
+
: null;
|
|
1031
|
+
yield { type: 'status', label: 'waiting', detail: verdict.detail };
|
|
1032
|
+
if (verdict.nudge)
|
|
1033
|
+
nudgeTerminal(spec.key, verdict.nudge);
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
826
1036
|
finished = true;
|
|
827
1037
|
yield {
|
|
828
1038
|
type: 'done',
|
|
829
|
-
text
|
|
1039
|
+
text,
|
|
830
1040
|
providerSessionId: spec.sessionId,
|
|
831
1041
|
usage: watch.usage(started),
|
|
832
1042
|
};
|
|
833
1043
|
return;
|
|
834
1044
|
}
|
|
835
|
-
if (
|
|
1045
|
+
if (held && watch.assistantEntries > held.entries)
|
|
1046
|
+
held = null;
|
|
1047
|
+
if (held && Date.now() > held.until) {
|
|
1048
|
+
finished = true;
|
|
1049
|
+
yield { type: 'done', text: held.text, providerSessionId: spec.sessionId, usage: watch.usage(started) };
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
// Sub-agents can work for a long while without a line in this
|
|
1053
|
+
// transcript; the run's own timeout bounds that wait, not the idle limit.
|
|
1054
|
+
const waiting = parked && pendingTotal(watch.work.pending()) > 0;
|
|
1055
|
+
if (awaitingWork && !waiting) {
|
|
1056
|
+
// Nothing is out any more; a timeout from here on is the report, not the wait.
|
|
1057
|
+
awaitingWork = false;
|
|
1058
|
+
yield { type: 'status', label: 'resumed', detail: 'all background work is back; the final report is being written' };
|
|
1059
|
+
}
|
|
1060
|
+
if (parked && !waiting && !resumeNudged && Date.now() - watch.lastActivity > RESUME_GRACE_MS) {
|
|
1061
|
+
resumeNudged = true;
|
|
1062
|
+
nudgeTerminal(spec.key, RESUME_NUDGE);
|
|
1063
|
+
}
|
|
1064
|
+
if (!waiting && Date.now() - watch.lastActivity > IDLE_LIMIT_MS) {
|
|
836
1065
|
yield fatalError(IDLE_MESSAGE);
|
|
837
1066
|
return;
|
|
838
1067
|
}
|