@phren/cli 0.3.58 → 0.3.61
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 +1 -1
- package/dist/bridge/account-add.d.ts +30 -0
- package/dist/bridge/account-add.js +125 -19
- package/dist/bridge/account-failover.d.ts +9 -2
- package/dist/bridge/account-failover.js +1 -1
- package/dist/bridge/account-policy.d.ts +44 -0
- package/dist/bridge/account-policy.js +103 -0
- package/dist/bridge/account-pools.d.ts +35 -0
- package/dist/bridge/account-pools.js +171 -0
- package/dist/bridge/account-usage.d.ts +29 -4
- package/dist/bridge/account-usage.js +117 -8
- package/dist/bridge/agent-hook-callback.d.ts +6 -0
- package/dist/bridge/agent-hook-callback.js +10 -2
- package/dist/bridge/agent-hook-stores.d.ts +3 -0
- package/dist/bridge/agent-hook-stores.js +13 -0
- package/dist/bridge/agent-hooks.d.ts +12 -2
- package/dist/bridge/agent-hooks.js +41 -4
- package/dist/bridge/claude-account-setup.d.ts +16 -0
- package/dist/bridge/claude-account-setup.js +46 -2
- package/dist/bridge/claude-accounts.d.ts +9 -0
- package/dist/bridge/claude-accounts.js +53 -15
- package/dist/bridge/claude-credits.d.ts +8 -2
- package/dist/bridge/claude-credits.js +52 -31
- package/dist/bridge/codex-threads.js +50 -2
- package/dist/bridge/command.js +12 -3
- package/dist/bridge/continuation-launch.d.ts +32 -0
- package/dist/bridge/continuation-launch.js +112 -1
- package/dist/bridge/dispatch-command.js +22 -0
- package/dist/bridge/dispatch-returns.d.ts +23 -0
- package/dist/bridge/dispatch-returns.js +57 -0
- package/dist/bridge/dispatch.d.ts +26 -4
- package/dist/bridge/dispatch.js +100 -11
- package/dist/bridge/git-host-gitlab.js +2 -1
- package/dist/bridge/git-hosts.d.ts +8 -0
- package/dist/bridge/git-hosts.js +16 -2
- package/dist/bridge/git-publish.js +18 -2
- package/dist/bridge/git.d.ts +8 -0
- package/dist/bridge/git.js +24 -1
- package/dist/bridge/hand-off-queue.d.ts +71 -0
- package/dist/bridge/hand-off-queue.js +38 -0
- package/dist/bridge/harnesses.d.ts +12 -0
- package/dist/bridge/harnesses.js +20 -4
- package/dist/bridge/helper.d.ts +54 -0
- package/dist/bridge/helper.js +186 -0
- package/dist/bridge/herdr.js +6 -2
- package/dist/bridge/hook-fast.d.ts +3 -0
- package/dist/bridge/hook-fast.js +10 -2
- package/dist/bridge/move-computers.d.ts +15 -1
- package/dist/bridge/move-computers.js +123 -26
- package/dist/bridge/owner-inbox-sources.d.ts +12 -1
- package/dist/bridge/owner-inbox-sources.js +127 -49
- package/dist/bridge/peers.d.ts +2 -0
- package/dist/bridge/peers.js +35 -4
- package/dist/bridge/prompt-deliveries.d.ts +69 -2
- package/dist/bridge/prompt-deliveries.js +342 -28
- package/dist/bridge/schedule-watch.js +17 -3
- package/dist/bridge/server-routes.d.ts +6 -0
- package/dist/bridge/server-routes.js +98 -14
- package/dist/bridge/server-stream.js +6 -4
- package/dist/bridge/server.js +2 -1
- package/dist/bridge/session-move.d.ts +70 -1
- package/dist/bridge/session-move.js +311 -19
- package/dist/bridge/simulators.d.ts +19 -0
- package/dist/bridge/simulators.js +93 -13
- package/dist/bridge/speech-live.js +132 -22
- package/dist/bridge/task-routes.d.ts +16 -16
- package/dist/bridge/terminal-herdr.js +6 -1
- package/dist/bridge/terminal-tmux.js +9 -5
- package/dist/bridge/terminal.d.ts +9 -2
- package/dist/bridge/transcript-claude.d.ts +3 -0
- package/dist/bridge/transcript-claude.js +175 -29
- package/dist/bridge/transcript-preview.d.ts +11 -2
- package/dist/bridge/transcript-preview.js +90 -8
- package/dist/bridge/turn-records.d.ts +19 -0
- package/dist/bridge/turn-records.js +78 -3
- package/dist/bridge/usage.d.ts +17 -0
- package/dist/bridge/usage.js +3 -2
- package/dist/bridge-hook.mjs +11964 -10084
- package/dist/cli-registry.js +2 -2
- package/dist/content/citation.d.ts +4 -0
- package/dist/content/citation.js +21 -21
- package/dist/data/task-contract.d.ts +4 -4
- package/dist/governance/policy.js +5 -2
- package/dist/index.js +1 -1
- package/dist/modules/registry.js +5 -5
- package/dist/project-config.d.ts +14 -0
- package/dist/project-config.js +40 -1
- package/dist/tools/dispatch.js +1 -1
- package/dist/tools/helper.d.ts +3 -0
- package/dist/tools/helper.js +44 -0
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
|
-
import { open } from "node:fs/promises";
|
|
3
|
+
import { open, stat } from "node:fs/promises";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import {
|
|
5
|
+
import { materializedPath } from "./codex-threads.js";
|
|
6
|
+
import { atomicInPrivateDir, BridgeError, object, objects, provider, sessionId } from "./protocol.js";
|
|
6
7
|
import { transcriptPath, unwrapPastedContent } from "./transcripts.js";
|
|
7
8
|
/** A delivery the agent's turn ended without: the reason the phone shows. */
|
|
8
9
|
export const TURN_ENDED_WITHOUT_IT = "The agent finished its turn without taking the message.";
|
|
@@ -17,8 +18,12 @@ const MAX_RECORDS = 512;
|
|
|
17
18
|
const LANDED_CHECK_MS = 3_000;
|
|
18
19
|
/** The transcript scan starts this long before a message was typed (clock skew), so entries up to this far back may belong to an earlier message. */
|
|
19
20
|
const ENTRY_SLACK_MS = 5_000;
|
|
21
|
+
/** A transcript is read from its last this many bytes: older rows are out of sight. */
|
|
22
|
+
const TRANSCRIPT_TAIL_BYTES = 524_288;
|
|
20
23
|
/** Claude writes a submitted row after its hooks return; a message its hook settled claims only a row stamped within this long of that. */
|
|
21
24
|
const HOOK_ROW_MS = 30_000;
|
|
25
|
+
/** How long after a phone message settled its row's line is still looked for. */
|
|
26
|
+
const LOCATE_FOR_MS = 5 * 60_000;
|
|
22
27
|
/** Typed text holds its pane (nothing more is typed there) at most this long when the pane sends no
|
|
23
28
|
* UserPromptSubmit or Stop to settle it: a message such an agent took or dropped never settles. */
|
|
24
29
|
export const IN_FLIGHT_MS = 45_000;
|
|
@@ -51,7 +56,13 @@ const recordSchema = z.object({
|
|
|
51
56
|
/** From a joined prompt (`settleJoined`): "part", a message settled inside it, claims no row of its own (the turn's
|
|
52
57
|
* row carries other words); "turn", the joined words themselves, no message of anyone's, claims that row. */
|
|
53
58
|
joined: z.enum(["part", "turn"]).optional(),
|
|
59
|
+
/** Codex: the transcript's size in bytes when the message was registered. Only a row at or past it is
|
|
60
|
+
* the message's own: position, not the clock, orders rows, and a row without a timestamp too. */
|
|
61
|
+
cursor: z.number().int().nonnegative().optional(),
|
|
54
62
|
}).strict();
|
|
63
|
+
const landedFile = (file) => `${file}.landed.json`;
|
|
64
|
+
const landedSchema = z.object({ line: z.number().int().nonnegative().optional(), uuid: z.string().min(1).max(100).optional(),
|
|
65
|
+
shown: z.number().int().nonnegative().optional(), late: z.literal(true).optional() }).strict();
|
|
55
66
|
/** One message per record, bound to the pane it was typed into (server, pane,
|
|
56
67
|
* agent kind), not to the conversation: a Codex thread or a Claude /clear can
|
|
57
68
|
* replace the conversation while the agent still holds the typed text, and
|
|
@@ -73,10 +84,20 @@ export class PromptDeliveries {
|
|
|
73
84
|
static open(record) { return record.state === "typed" || record.state === "queued"; }
|
|
74
85
|
/** Open, or failed with its own hook's claim still waiting for its row (a late delivery). */
|
|
75
86
|
static claimable(record) { return PromptDeliveries.open(record) || (record.state === "failed" && !!record.hookClaim); }
|
|
76
|
-
|
|
87
|
+
/** Codex messages whose `cursor` is still being read. */
|
|
88
|
+
cursors = new Map();
|
|
89
|
+
/** The size of a conversation's transcript: 0 before it has one, undefined when it cannot be read. */
|
|
90
|
+
size;
|
|
91
|
+
/** The line (0-based) of the row at a byte offset in a conversation's transcript, when one still starts there
|
|
92
|
+
* (and carries `uuid`, when given). */
|
|
93
|
+
lineOf;
|
|
94
|
+
constructor(file, now = Date.now, landed = transcriptEntries, size, lineOf) {
|
|
77
95
|
this.file = file;
|
|
78
96
|
this.now = now;
|
|
79
97
|
this.landed = landed;
|
|
98
|
+
// A store reading other transcripts than the agents' own has no sizes or lines for them.
|
|
99
|
+
this.size = size ?? (landed === transcriptEntries ? transcriptSize : async () => undefined);
|
|
100
|
+
this.lineOf = lineOf ?? (landed === transcriptEntries ? transcriptLine : async () => undefined);
|
|
80
101
|
if (!file)
|
|
81
102
|
return;
|
|
82
103
|
try {
|
|
@@ -89,6 +110,22 @@ export class PromptDeliveries {
|
|
|
89
110
|
}
|
|
90
111
|
}
|
|
91
112
|
catch { /* No file yet, or an unreadable one: start empty. */ }
|
|
113
|
+
try {
|
|
114
|
+
for (const [id, row] of Object.entries(object(JSON.parse(readFileSync(landedFile(file), "utf8"))))) {
|
|
115
|
+
const parsed = landedSchema.safeParse(row), record = this.byId(id);
|
|
116
|
+
if (!parsed.success || !record || record.entry === undefined)
|
|
117
|
+
continue;
|
|
118
|
+
if (parsed.data.line !== undefined)
|
|
119
|
+
record.entryLine = parsed.data.line;
|
|
120
|
+
if (parsed.data.uuid)
|
|
121
|
+
record.entryUuid = parsed.data.uuid;
|
|
122
|
+
if (parsed.data.shown !== undefined)
|
|
123
|
+
record.entryShown = parsed.data.shown;
|
|
124
|
+
if (parsed.data.late)
|
|
125
|
+
record.entryLate = true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch { /* None yet: rows found before this version, or none. */ }
|
|
92
129
|
this.prune();
|
|
93
130
|
}
|
|
94
131
|
samePane(record, target) {
|
|
@@ -105,9 +142,17 @@ export class PromptDeliveries {
|
|
|
105
142
|
if (!this.file)
|
|
106
143
|
return;
|
|
107
144
|
// A record without a phone id is kept once it claimed a transcript row, so after a restart that row is not offered to another message.
|
|
108
|
-
const
|
|
145
|
+
const kept = this.records.filter(record => record.id || record.entry !== undefined || record.joined === "turn");
|
|
146
|
+
const rows = kept.map(({ idleSince: _, checkedAt: __, hookClaim: ___, seenRow: ____, awaitingRow: _____, entryLate: _a, entryLine: _b, entryUuid: _c, entryShown: _d, ...row }) => row);
|
|
147
|
+
// The rows phone messages landed as, apart from the records an older Hook reads strictly.
|
|
148
|
+
const landed = {};
|
|
149
|
+
for (const record of kept)
|
|
150
|
+
if (record.id && record.entry !== undefined && (record.entryLine !== undefined || record.entryUuid || record.entryShown !== undefined || record.entryLate)) {
|
|
151
|
+
landed[record.id] = { ...(record.entryLine !== undefined ? { line: record.entryLine } : {}), ...(record.entryUuid ? { uuid: record.entryUuid } : {}),
|
|
152
|
+
...(record.entryShown !== undefined ? { shown: record.entryShown } : {}), ...(record.entryLate ? { late: true } : {}) };
|
|
153
|
+
}
|
|
109
154
|
const file = this.file;
|
|
110
|
-
this.writing = this.writing.then(() => atomicInPrivateDir(file, { records: rows })).catch(() => undefined);
|
|
155
|
+
this.writing = this.writing.then(async () => { await atomicInPrivateDir(file, { records: rows }); await atomicInPrivateDir(landedFile(file), landed); }).catch(() => undefined);
|
|
111
156
|
}
|
|
112
157
|
/** Settled writes, for tests and an orderly shutdown. */
|
|
113
158
|
flush() { return this.writing; }
|
|
@@ -126,6 +171,16 @@ export class PromptDeliveries {
|
|
|
126
171
|
session: target.session, ...(typeof terminal === "string" ? { terminal } : {}), state: "typed", at: this.now() };
|
|
127
172
|
this.records.push(record);
|
|
128
173
|
this.save();
|
|
174
|
+
// Started before the caller types the message (a stat, against typing, then the agent's own write), so
|
|
175
|
+
// the row it makes lands past the cursor; a row stamped after registration counts too (`codexRowIsOwn`).
|
|
176
|
+
if (record.source === "codex") {
|
|
177
|
+
const session = record.session;
|
|
178
|
+
this.cursors.set(record, this.size(record).then(size => { if (size !== undefined && record.session === session) {
|
|
179
|
+
record.cursor = size;
|
|
180
|
+
this.save();
|
|
181
|
+
} }, () => undefined)
|
|
182
|
+
.finally(() => this.cursors.delete(record)));
|
|
183
|
+
}
|
|
129
184
|
return new Promise(resolve => {
|
|
130
185
|
const timer = setTimeout(() => this.release(record, "pending"), waitMs);
|
|
131
186
|
timer.unref?.();
|
|
@@ -237,7 +292,10 @@ export class PromptDeliveries {
|
|
|
237
292
|
const record = this.find(id, target);
|
|
238
293
|
if (!record || record.state === "typed")
|
|
239
294
|
return { state: "unknown" };
|
|
240
|
-
|
|
295
|
+
// Once delivered and its row is known: the transcript row it landed as, to match a pending send by.
|
|
296
|
+
const landed = record.state === "delivered" && record.entryLine !== undefined
|
|
297
|
+
? { line: record.entryLine, ...(record.entryUuid ? { uuid: record.entryUuid } : {}) } : undefined;
|
|
298
|
+
return { state: record.state, ...(record.reason && record.state === "failed" ? { reason: record.reason } : {}), session: record.session, ...(landed ? { landed } : {}) };
|
|
241
299
|
}
|
|
242
300
|
/** Every known phone message in `target`'s pane, for its conversation's stream. */
|
|
243
301
|
forPane(target) {
|
|
@@ -289,7 +347,11 @@ export class PromptDeliveries {
|
|
|
289
347
|
return;
|
|
290
348
|
// Claims are per conversation: another conversation's rows can sit at the same byte offset.
|
|
291
349
|
// Only user rows count: a mid-turn queue removal never had a hook, so it says nothing about this callback.
|
|
292
|
-
const
|
|
350
|
+
const claimers = this.records.filter(other => other.hash === hash && this.samePane(other, target) && other.session === target.session && other.entry !== undefined && other.entryHook !== false);
|
|
351
|
+
const claimed = new Set(claimers.map(other => other.entry));
|
|
352
|
+
// A row an earlier message took only to name it in its receipt (`entryLate`) is never offered again, but it
|
|
353
|
+
// says nothing about this callback: that message's hook already came, so this one is not it, late.
|
|
354
|
+
const waitedOn = claimers.filter(other => !other.entryLate).length;
|
|
293
355
|
const free = rows?.filter(row => !claimed.has(row.offset)).sort((a, b) => a.offset - b.offset)[0];
|
|
294
356
|
// Claude writes the submitted row only after its UserPromptSubmit hooks return, and this callback is one
|
|
295
357
|
// of them: the row for this submission is never readable yet. So no free row (or a transcript that cannot
|
|
@@ -297,8 +359,11 @@ export class PromptDeliveries {
|
|
|
297
359
|
// writes next stays this message's (`taken` counts hook-settled messages first). Only when a message
|
|
298
360
|
// with these words already claimed a row in this conversation may the callback be that message's, late:
|
|
299
361
|
// then it waits for a row of its own, looked for in the conversation that made the claim.
|
|
300
|
-
if (!free && !
|
|
301
|
-
this.settle(record,
|
|
362
|
+
if (!free && !waitedOn) {
|
|
363
|
+
this.settle(record, target.session);
|
|
364
|
+
// A phone message claims the row Claude writes next, so its receipt can name it (`claimClaudeRow`).
|
|
365
|
+
if (record.id)
|
|
366
|
+
record.awaitingRow = true;
|
|
302
367
|
this.save();
|
|
303
368
|
return;
|
|
304
369
|
}
|
|
@@ -309,10 +374,19 @@ export class PromptDeliveries {
|
|
|
309
374
|
}
|
|
310
375
|
return;
|
|
311
376
|
}
|
|
312
|
-
entry = free
|
|
377
|
+
entry = free;
|
|
313
378
|
}
|
|
314
|
-
this.settle(record,
|
|
379
|
+
this.settle(record, target.session);
|
|
380
|
+
if (entry)
|
|
381
|
+
this.claim(record, entry, true);
|
|
315
382
|
this.save();
|
|
383
|
+
if (target.source === "codex") {
|
|
384
|
+
// Its row is its own from now on, so a later message with these words never takes it, even once the
|
|
385
|
+
// row has left the scanned tail. Codex writes the row after its hooks return, and this callback is one
|
|
386
|
+
// of them: not awaited (the agent waits on its answer), and claimed by the next ticks when the row appears.
|
|
387
|
+
record.awaitingRow = true;
|
|
388
|
+
void this.claimCodexRow(record).catch(() => undefined);
|
|
389
|
+
}
|
|
316
390
|
}
|
|
317
391
|
/** Claude submits the messages it held while busy as one turn, joined by newlines, so no single message's
|
|
318
392
|
* hash matches the prompt. Each open message in the pane whose words are a run of whole lines of it settles,
|
|
@@ -352,7 +426,7 @@ export class PromptDeliveries {
|
|
|
352
426
|
return false;
|
|
353
427
|
for (const record of matched) {
|
|
354
428
|
record.joined = "part";
|
|
355
|
-
this.settle(record,
|
|
429
|
+
this.settle(record, target.session);
|
|
356
430
|
}
|
|
357
431
|
// The turn's row carries the joined words: a hook-settled record of them (no phone id) claims it as
|
|
358
432
|
// `taken` lets any hook-settled message, so a later message with the same words cannot take it.
|
|
@@ -360,19 +434,60 @@ export class PromptDeliveries {
|
|
|
360
434
|
state: "delivered", at: this.now(), settledAt: this.now(), joined: "turn" });
|
|
361
435
|
return true;
|
|
362
436
|
}
|
|
363
|
-
/**
|
|
364
|
-
|
|
437
|
+
/** `record` takes transcript row `row` as its own: the entry no other message with these words may take, and,
|
|
438
|
+
* for a phone message, the row its receipt names (`landed`), whose line is counted in the background. */
|
|
439
|
+
claim(record, row, hook = row.hook, late = false) {
|
|
440
|
+
record.entry = row.offset;
|
|
441
|
+
record.entryHook = hook;
|
|
442
|
+
delete record.entryLine;
|
|
443
|
+
if (late)
|
|
444
|
+
record.entryLate = true;
|
|
445
|
+
else
|
|
446
|
+
delete record.entryLate;
|
|
447
|
+
if (row.uuid)
|
|
448
|
+
record.entryUuid = row.uuid;
|
|
449
|
+
else
|
|
450
|
+
delete record.entryUuid;
|
|
451
|
+
if (row.shown !== undefined)
|
|
452
|
+
record.entryShown = row.shown;
|
|
453
|
+
else
|
|
454
|
+
delete record.entryShown;
|
|
455
|
+
if (record.id)
|
|
456
|
+
void this.locate(record).catch(() => undefined);
|
|
457
|
+
}
|
|
458
|
+
/** Phone messages whose row's line is being counted. */
|
|
459
|
+
locating = new Set();
|
|
460
|
+
/** Counts the line of the row the stream shows for a phone message's claimed entry, once, and keeps it.
|
|
461
|
+
* In the background: a long transcript is read from its start. */
|
|
462
|
+
async locate(record) {
|
|
463
|
+
const entry = record.entry, session = record.session;
|
|
464
|
+
if (entry === undefined || record.entryLine !== undefined || this.locating.has(record))
|
|
465
|
+
return;
|
|
466
|
+
this.locating.add(record);
|
|
467
|
+
try {
|
|
468
|
+
const line = await this.lineOf(record, record.entryShown ?? entry, record.entryUuid);
|
|
469
|
+
// Claimed again, or moved to another conversation, while the transcript was read: that answer is not this row's.
|
|
470
|
+
if (line === undefined || record.entry !== entry || record.session !== session)
|
|
471
|
+
return;
|
|
472
|
+
record.entryLine = line;
|
|
473
|
+
this.save();
|
|
474
|
+
}
|
|
475
|
+
finally {
|
|
476
|
+
this.locating.delete(record);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
/** Marks `record` delivered, in `session` when given. */
|
|
480
|
+
settle(record, session) {
|
|
365
481
|
record.state = "delivered";
|
|
366
482
|
record.settledAt = this.now();
|
|
367
483
|
delete record.reason;
|
|
368
484
|
delete record.hookClaim;
|
|
369
485
|
delete record.seenRow;
|
|
486
|
+
// A cursor is a position in one conversation's transcript.
|
|
487
|
+
if (session && session !== record.session)
|
|
488
|
+
delete record.cursor;
|
|
370
489
|
if (session)
|
|
371
490
|
record.session = session;
|
|
372
|
-
if (entry !== undefined)
|
|
373
|
-
record.entry = entry;
|
|
374
|
-
if (hook !== undefined)
|
|
375
|
-
record.entryHook = hook;
|
|
376
491
|
this.release(record, "delivered");
|
|
377
492
|
}
|
|
378
493
|
/** Whether the transcript shows the agent took `record`'s message, claiming the entry it settled with.
|
|
@@ -383,9 +498,8 @@ export class PromptDeliveries {
|
|
|
383
498
|
* (`working` false) a user row settles too, for a hook that never came. Of several identical open
|
|
384
499
|
* messages the oldest take the entries. `pass` carries the entries still unclaimed within one observe. */
|
|
385
500
|
async taken(record, pass, working = false) {
|
|
386
|
-
// Only Claude's transcript says which entry a message settled with; Codex keeps the any-match check.
|
|
387
501
|
if (record.source !== "claude")
|
|
388
|
-
return
|
|
502
|
+
return this.takenCodex(record);
|
|
389
503
|
const same = (other) => other.hash === record.hash && other.server === record.server && other.pane === record.pane && other.session === record.session;
|
|
390
504
|
const open = this.records.filter(other => same(other) && PromptDeliveries.claimable(other)).sort((a, b) => a.at - b.at);
|
|
391
505
|
if (open[0] !== record)
|
|
@@ -410,8 +524,7 @@ export class PromptDeliveries {
|
|
|
410
524
|
&& (entry.at === undefined || (entry.at >= other.at - ENTRY_SLACK_MS && entry.at <= (other.settledAt ?? other.at) + HOOK_ROW_MS)));
|
|
411
525
|
if (!row)
|
|
412
526
|
continue;
|
|
413
|
-
other
|
|
414
|
-
other.entryHook = true;
|
|
527
|
+
this.claim(other, row, true);
|
|
415
528
|
used.add(row.offset);
|
|
416
529
|
claimedRows = true;
|
|
417
530
|
}
|
|
@@ -438,8 +551,101 @@ export class PromptDeliveries {
|
|
|
438
551
|
pass.set(key, free);
|
|
439
552
|
if (entry === undefined)
|
|
440
553
|
return false;
|
|
441
|
-
record
|
|
442
|
-
|
|
554
|
+
this.claim(record, entry);
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
/** Whether a Codex transcript row can be `record`'s own: written after it was registered. A row at or past
|
|
558
|
+
* its cursor was; one before it only if stamped after registration (written while the cursor was read),
|
|
559
|
+
* which a row without a timestamp never is. A materialized Codex 0.155 thread's copy is append-only, so a row
|
|
560
|
+
* past the cursor was copied after registration, but not necessarily created after it: an older item the copy
|
|
561
|
+
* had not reached yet lands there too. Its rows are stamped with when the item was created (codex-threads.ts),
|
|
562
|
+
* and the scan drops any row stamped more than `ENTRY_SLACK_MS` before registration, which is what keeps an
|
|
563
|
+
* earlier identical message from being taken for this one. */
|
|
564
|
+
static codexRowIsOwn(record, row) {
|
|
565
|
+
return (record.cursor !== undefined && row.offset >= record.cursor) || (row.at !== undefined && row.at >= record.at);
|
|
566
|
+
}
|
|
567
|
+
/** Rows of `record`'s words in its conversation, oldest first, and where the scan of them began: rows
|
|
568
|
+
* before `start` are out of sight, so whether they exist cannot be told. */
|
|
569
|
+
async codexRows(record) {
|
|
570
|
+
await this.cursors.get(record);
|
|
571
|
+
const rows = [...await this.landed(record).catch(() => [])].sort((a, b) => a.offset - b.offset);
|
|
572
|
+
// Read after the scan: a transcript that grew meanwhile puts `start` later than the scan's, never earlier.
|
|
573
|
+
const size = await this.size(record).catch(() => undefined);
|
|
574
|
+
return { rows, ...(size !== undefined ? { start: Math.max(0, size - TRANSCRIPT_TAIL_BYTES) } : {}) };
|
|
575
|
+
}
|
|
576
|
+
/** Rows with `record`'s words in its conversation claimed by other messages. */
|
|
577
|
+
codexClaimed(record) {
|
|
578
|
+
return new Set(this.records.filter(other => other !== record && other.hash === record.hash && this.samePane(other, record)
|
|
579
|
+
&& other.session === record.session && other.entry !== undefined).map(other => other.entry));
|
|
580
|
+
}
|
|
581
|
+
/** A Claude phone message its hook settled before its row was written takes the row once it appears: the first
|
|
582
|
+
* unclaimed user row with its words stamped from when it was typed to `HOOK_ROW_MS` after its hook, the rule
|
|
583
|
+
* `taken` applies to hook-settled messages. Only the receipt's `landed` depends on it. */
|
|
584
|
+
async claimClaudeRow(record) {
|
|
585
|
+
const session = record.session;
|
|
586
|
+
const rows = (await this.landed(record).catch(() => [])).filter(row => row.hook).sort((a, b) => a.offset - b.offset);
|
|
587
|
+
// Another path (`taken` for an identical message) may have given it its row meanwhile.
|
|
588
|
+
if (record.entry !== undefined) {
|
|
589
|
+
delete record.awaitingRow;
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (!record.awaitingRow || record.session !== session)
|
|
593
|
+
return;
|
|
594
|
+
const used = new Set(this.records.filter(other => other !== record && other.hash === record.hash && this.samePane(other, record)
|
|
595
|
+
&& other.session === record.session && other.entry !== undefined).map(other => other.entry));
|
|
596
|
+
const row = rows.find(row => !used.has(row.offset)
|
|
597
|
+
&& (row.at === undefined || (row.at >= record.at - ENTRY_SLACK_MS && row.at <= (record.settledAt ?? record.at) + HOOK_ROW_MS)));
|
|
598
|
+
if (!row)
|
|
599
|
+
return;
|
|
600
|
+
this.claim(record, row, true, true);
|
|
601
|
+
delete record.awaitingRow;
|
|
602
|
+
this.save();
|
|
603
|
+
}
|
|
604
|
+
/** A Codex message its hook settled takes the first free row written after it was registered. */
|
|
605
|
+
async claimCodexRow(record) {
|
|
606
|
+
const { rows, start } = await this.codexRows(record);
|
|
607
|
+
if (!record.awaitingRow || record.entry !== undefined)
|
|
608
|
+
return;
|
|
609
|
+
// Rows written after it have left the scanned tail: its own may be one of them, so it takes none of those left.
|
|
610
|
+
if (record.cursor !== undefined && start !== undefined && record.cursor < start) {
|
|
611
|
+
delete record.awaitingRow;
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
const used = this.codexClaimed(record);
|
|
615
|
+
const row = rows.find(row => !used.has(row.offset) && PromptDeliveries.codexRowIsOwn(record, row));
|
|
616
|
+
if (!row)
|
|
617
|
+
return;
|
|
618
|
+
this.claim(record, row);
|
|
619
|
+
delete record.awaitingRow;
|
|
620
|
+
this.save();
|
|
621
|
+
}
|
|
622
|
+
/** Whether Codex took `record`'s message: a row written after it was registered (`codexRowIsOwn`) that no
|
|
623
|
+
* other message with these words claimed. An earlier message its hook settled whose row was not yet
|
|
624
|
+
* claimed holds the first such row of its own first, but only when everything written after it is still
|
|
625
|
+
* in the scanned tail: otherwise its row may be gone, and the row that is left is this message's. */
|
|
626
|
+
async takenCodex(record) {
|
|
627
|
+
const { rows, start } = await this.codexRows(record);
|
|
628
|
+
const used = this.codexClaimed(record);
|
|
629
|
+
let claimed = false;
|
|
630
|
+
for (const other of this.records.filter(other => other !== record && other.hash === record.hash && this.samePane(other, record) && other.session === record.session
|
|
631
|
+
&& other.awaitingRow && other.entry === undefined && other.at < record.at).sort((a, b) => a.at - b.at)) {
|
|
632
|
+
if (other.cursor === undefined || start === undefined || other.cursor < start)
|
|
633
|
+
continue;
|
|
634
|
+
const row = rows.find(row => !used.has(row.offset) && PromptDeliveries.codexRowIsOwn(other, row));
|
|
635
|
+
if (!row)
|
|
636
|
+
continue;
|
|
637
|
+
this.claim(other, row);
|
|
638
|
+
delete other.awaitingRow;
|
|
639
|
+
used.add(row.offset);
|
|
640
|
+
claimed = true;
|
|
641
|
+
}
|
|
642
|
+
if (claimed)
|
|
643
|
+
this.save();
|
|
644
|
+
const entry = rows.find(row => !used.has(row.offset) && PromptDeliveries.codexRowIsOwn(record, row));
|
|
645
|
+
// The record may have settled (its hook came) while the transcript was read.
|
|
646
|
+
if (!entry || !PromptDeliveries.claimable(record))
|
|
647
|
+
return false;
|
|
648
|
+
this.claim(record, entry);
|
|
443
649
|
return true;
|
|
444
650
|
}
|
|
445
651
|
/** A turn in `target`'s pane ended: the agent now takes held input or never will. */
|
|
@@ -468,6 +674,27 @@ export class PromptDeliveries {
|
|
|
468
674
|
for (const record of this.records) {
|
|
469
675
|
if (record.server !== server)
|
|
470
676
|
continue;
|
|
677
|
+
// A message its hook settled claims its row once written (a Codex one before a later identical one is
|
|
678
|
+
// checked; a Claude phone message for its receipt's `landed`).
|
|
679
|
+
if (record.awaitingRow) {
|
|
680
|
+
if (now - (record.settledAt ?? record.at) > HOOK_ROW_MS)
|
|
681
|
+
delete record.awaitingRow;
|
|
682
|
+
else if (record.checkedAt === undefined || now - record.checkedAt >= LANDED_CHECK_MS) {
|
|
683
|
+
record.checkedAt = now;
|
|
684
|
+
await (record.source === "claude" ? this.claimClaudeRow(record) : this.claimCodexRow(record));
|
|
685
|
+
}
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
// A phone message's row whose line could not be counted yet (a Hook restart, a transcript being rewritten)
|
|
689
|
+
// is counted again for a few minutes after it settled.
|
|
690
|
+
if (record.id && record.state === "delivered" && record.entry !== undefined && record.entryLine === undefined
|
|
691
|
+
&& now - (record.settledAt ?? record.at) <= LOCATE_FOR_MS) {
|
|
692
|
+
if (record.checkedAt === undefined || now - record.checkedAt >= LANDED_CHECK_MS) {
|
|
693
|
+
record.checkedAt = now;
|
|
694
|
+
void this.locate(record).catch(() => undefined);
|
|
695
|
+
}
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
471
698
|
// A failed message whose own hook came late, before its row: settled when the row appears.
|
|
472
699
|
if (record.state === "failed" && record.hookClaim) {
|
|
473
700
|
if (record.source === "claude" && (record.checkedAt === undefined || now - record.checkedAt >= LANDED_CHECK_MS)) {
|
|
@@ -535,12 +762,28 @@ export class PromptDeliveries {
|
|
|
535
762
|
/** The byte offsets (in the file, `start` being the buffer's own offset) of the rows in `buffer`
|
|
536
763
|
* whose user text has `record`'s words, newest first. One offset per row. */
|
|
537
764
|
export function matchingOffsets(buffer, start, record) {
|
|
538
|
-
const entries = [];
|
|
765
|
+
const entries = [], items = [];
|
|
766
|
+
// A Codex rollout writes each user turn twice, as a `user_message` event and a user `response_item`:
|
|
767
|
+
// the event alone is the turn. Only a thread materialized from Codex 0.155's history has items without events.
|
|
768
|
+
let events = false;
|
|
539
769
|
const lines = buffer.toString("utf8").split("\n");
|
|
540
770
|
let offset = start + buffer.length;
|
|
771
|
+
// A materialized Codex thread writes input the agent holds in its queue as a `phrenQueued` row, and a content-free
|
|
772
|
+
// `phren_queue_consumed` row with the same key once the agent takes it. The scan runs newest first, so a queued
|
|
773
|
+
// row's marker, written after it, is seen before it: only a queued row whose marker came counts as taken.
|
|
774
|
+
const consumed = new Set();
|
|
541
775
|
for (let index = lines.length - 1; index >= 0; index--) {
|
|
542
776
|
const line = lines[index];
|
|
543
777
|
offset -= Buffer.byteLength(line) + (index < lines.length - 1 ? 1 : 0);
|
|
778
|
+
if (line.includes('"phren_queue_consumed"')) {
|
|
779
|
+
try {
|
|
780
|
+
const marker = object(JSON.parse(line));
|
|
781
|
+
if (marker.type === "phren_queue_consumed" && typeof marker.key === "string")
|
|
782
|
+
consumed.add(marker.key);
|
|
783
|
+
}
|
|
784
|
+
catch { /* a cut line */ }
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
544
787
|
// Only a row that can carry user text is parsed: the scan reads the whole tail now, not just its newest rows.
|
|
545
788
|
if (!line.includes('"user') && !line.includes("queue-operation"))
|
|
546
789
|
continue;
|
|
@@ -551,22 +794,93 @@ export function matchingOffsets(buffer, start, record) {
|
|
|
551
794
|
catch {
|
|
552
795
|
continue;
|
|
553
796
|
}
|
|
797
|
+
if (row.phrenQueued === true && row.type === "response_item" && !(typeof row.phrenQueueKey === "string" && consumed.has(row.phrenQueueKey)))
|
|
798
|
+
continue;
|
|
554
799
|
// Rows are not in time order: Claude writes a queued message's attachment rows, stamped when it was
|
|
555
800
|
// typed, after the queue removal that took it. An older row is skipped, never the end of the scan,
|
|
556
801
|
// or a message typed after an earlier queued one is never found (seen 2026-10-17: taken, reported failed).
|
|
802
|
+
const payload = object(row.payload), item = row.type === "response_item" && payload.type === "message";
|
|
803
|
+
if (row.type === "event_msg" && payload.type === "user_message")
|
|
804
|
+
events = true;
|
|
557
805
|
const stamp = Date.parse(String(row.timestamp ?? ""));
|
|
558
806
|
if (Number.isFinite(stamp) && stamp < record.at - 5_000)
|
|
559
807
|
continue;
|
|
560
808
|
// One entry per row, even when its content repeats the words.
|
|
561
809
|
if (userTexts(row).some(text => promptHash(text) === record.hash))
|
|
562
|
-
entries.push({ offset, hook: row.type === "user", ...(Number.isFinite(stamp) ? { at: stamp } : {})
|
|
810
|
+
(item ? items : entries).push({ offset, hook: row.type === "user", ...(Number.isFinite(stamp) ? { at: stamp } : {}),
|
|
811
|
+
...(typeof row.uuid === "string" && row.uuid && row.uuid.length <= 100 ? { uuid: row.uuid } : {}) });
|
|
812
|
+
}
|
|
813
|
+
if (!events)
|
|
814
|
+
return [...entries, ...items].sort((a, b) => b.offset - a.offset);
|
|
815
|
+
// The event is the turn, but the conversation stream shows the user item Codex wrote just before it (the event is
|
|
816
|
+
// not shown): each event names, as `shown`, the nearest earlier item with these words no later event took.
|
|
817
|
+
const taken = new Set();
|
|
818
|
+
return entries.map(entry => {
|
|
819
|
+
const item = items.filter(candidate => candidate.offset < entry.offset && !taken.has(candidate.offset)).sort((a, b) => b.offset - a.offset)[0];
|
|
820
|
+
if (!item)
|
|
821
|
+
return entry;
|
|
822
|
+
taken.add(item.offset);
|
|
823
|
+
return { ...entry, shown: item.offset };
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
export async function transcriptSize(record) {
|
|
827
|
+
// A Codex 0.155 thread's copy is read as it stands: finding the transcript first appends what the thread store
|
|
828
|
+
// holds now, which may already be this message, and a failed append must not read as an empty conversation.
|
|
829
|
+
const copy = record.source === "codex" && sessionId.safeParse(record.session).success
|
|
830
|
+
? await stat(materializedPath(record.session)).catch(() => undefined) : undefined;
|
|
831
|
+
if (copy)
|
|
832
|
+
return copy.size;
|
|
833
|
+
try {
|
|
834
|
+
const file = await transcriptPath(record.source, record.session);
|
|
835
|
+
// Its copy made just now holds both older messages and maybe this one: no position tells them apart,
|
|
836
|
+
// so only a row stamped after registration (a user row with its creation time) can confirm it.
|
|
837
|
+
return file === materializedPath(record.session) ? undefined : (await stat(file)).size;
|
|
838
|
+
}
|
|
839
|
+
catch (error) {
|
|
840
|
+
// A conversation not written yet (a new Codex thread) has nothing before its first row.
|
|
841
|
+
return error instanceof BridgeError && object(error.details).code === "transcript-unavailable" ? 0 : undefined;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
/** The 0-based line of the row starting at byte `offset`: the newlines before it, as the transcript index counts
|
|
845
|
+
* rows (transcript-index.ts), so it is the `line` the conversation stream gives that row. */
|
|
846
|
+
export async function transcriptLine(record, offset, uuid) {
|
|
847
|
+
const file = await open(await transcriptPath(record.source, record.session), "r");
|
|
848
|
+
try {
|
|
849
|
+
if ((await file.stat()).size <= offset)
|
|
850
|
+
return undefined;
|
|
851
|
+
// The file may have been replaced since the row was claimed (a copy made again): a row must still start at the
|
|
852
|
+
// offset, and be the claimed one when its id is known, or the count would name some other row.
|
|
853
|
+
const head = Buffer.alloc(Math.min(4_096, offset + 1));
|
|
854
|
+
const { bytesRead } = await file.read(head, 0, head.length, offset + 1 - head.length);
|
|
855
|
+
if (offset > 0 && head[bytesRead - 2] !== 10)
|
|
856
|
+
return undefined;
|
|
857
|
+
if (uuid) {
|
|
858
|
+
// Claude writes `uuid` after `message`, so the row is read whole, within a bound no typed message reaches.
|
|
859
|
+
const row = Buffer.alloc(2 * 1024 * 1024), read = (await file.read(row, 0, row.length, offset)).bytesRead;
|
|
860
|
+
const text = row.subarray(0, read).toString("utf8"), end = text.indexOf("\n");
|
|
861
|
+
if (!(end < 0 ? text : text.slice(0, end)).includes(`"uuid":"${uuid}"`))
|
|
862
|
+
return undefined;
|
|
863
|
+
}
|
|
864
|
+
const buffer = Buffer.alloc(65_536);
|
|
865
|
+
let line = 0, position = 0;
|
|
866
|
+
while (position < offset) {
|
|
867
|
+
const { bytesRead } = await file.read(buffer, 0, Math.min(buffer.length, offset - position), position);
|
|
868
|
+
if (!bytesRead)
|
|
869
|
+
return undefined;
|
|
870
|
+
for (let index = buffer.indexOf(10); index >= 0 && index < bytesRead; index = buffer.indexOf(10, index + 1))
|
|
871
|
+
line++;
|
|
872
|
+
position += bytesRead;
|
|
873
|
+
}
|
|
874
|
+
return line;
|
|
875
|
+
}
|
|
876
|
+
finally {
|
|
877
|
+
await file.close();
|
|
563
878
|
}
|
|
564
|
-
return entries;
|
|
565
879
|
}
|
|
566
880
|
async function transcriptEntries(record) {
|
|
567
881
|
const file = await open(await transcriptPath(record.source, record.session), "r");
|
|
568
882
|
try {
|
|
569
|
-
const size = (await file.stat()).size, start = Math.max(0, size -
|
|
883
|
+
const size = (await file.stat()).size, start = Math.max(0, size - TRANSCRIPT_TAIL_BYTES), buffer = Buffer.alloc(size - start);
|
|
570
884
|
await file.read(buffer, 0, buffer.length, start);
|
|
571
885
|
return matchingOffsets(buffer, start, record);
|
|
572
886
|
}
|
|
@@ -222,7 +222,7 @@ export function finalTurnFromLines(lines, source) {
|
|
|
222
222
|
completed = false;
|
|
223
223
|
continue;
|
|
224
224
|
}
|
|
225
|
-
const userTurn = source === "claude" ? raw.type === "user" && !raw.isMeta &&
|
|
225
|
+
const userTurn = source === "claude" ? raw.type === "user" && !raw.isMeta && claudeUserText(raw) !== undefined
|
|
226
226
|
: source === "codex" ? raw.type === "response_item" && payload.type === "message" && payload.role === "user"
|
|
227
227
|
: source === "copilot" ? raw.type === "user.message"
|
|
228
228
|
: raw.type === "user/message";
|
|
@@ -267,12 +267,26 @@ export function finalTurnFromLines(lines, source) {
|
|
|
267
267
|
/** A prompt the owner (or a dispatcher) typed, not a task's notification or
|
|
268
268
|
* a teammate's message waking the session. */
|
|
269
269
|
function ownerPrompt(raw) {
|
|
270
|
-
|
|
271
|
-
|
|
270
|
+
// Claude files "[Request interrupted by user]" as a user row too: that is a stop, not the owner moving on.
|
|
271
|
+
const content = raw.type === "user" && !raw.isMeta && !turnInterrupted(raw, "claude") ? claudeUserText(raw) : undefined;
|
|
272
|
+
if (content === undefined)
|
|
272
273
|
return false;
|
|
273
274
|
const text = content.trimStart();
|
|
274
275
|
return !text.startsWith("<task-notification>") && !text.startsWith("<teammate-message");
|
|
275
276
|
}
|
|
277
|
+
/** A Claude user row's text when it is a message someone typed: a string, or blocks of text and images (a
|
|
278
|
+
* prompt with a pasted screenshot). A tool's result, which Claude also files as a user row, is not one. */
|
|
279
|
+
function claudeUserText(raw) {
|
|
280
|
+
const content = object(raw.message).content;
|
|
281
|
+
if (typeof content === "string")
|
|
282
|
+
return content;
|
|
283
|
+
if (!Array.isArray(content) || !content.length)
|
|
284
|
+
return undefined;
|
|
285
|
+
const blocks = content.map(object);
|
|
286
|
+
if (!blocks.every(block => block.type === "text" || block.type === "image"))
|
|
287
|
+
return undefined;
|
|
288
|
+
return blocks.filter(block => block.type === "text" && typeof block.text === "string").map(block => String(block.text)).join("\n");
|
|
289
|
+
}
|
|
276
290
|
/** The final turn of a conversation, read from the tail of its transcript. */
|
|
277
291
|
export async function readFinalTurn(source, sessionId) {
|
|
278
292
|
if (!sessionId)
|
|
@@ -118,6 +118,8 @@ export declare const capabilities: {
|
|
|
118
118
|
approvalPush: string;
|
|
119
119
|
simulators: boolean;
|
|
120
120
|
simulatorStream: boolean;
|
|
121
|
+
simulatorDevices: boolean;
|
|
122
|
+
simulatorDrag: boolean;
|
|
121
123
|
code: boolean;
|
|
122
124
|
codeFiles: boolean;
|
|
123
125
|
overviewStream: boolean;
|
|
@@ -146,7 +148,10 @@ export declare const capabilities: {
|
|
|
146
148
|
moveToComputer: boolean;
|
|
147
149
|
moveConductorToComputer: boolean;
|
|
148
150
|
moveResume: boolean;
|
|
151
|
+
moveSummaryByTarget: boolean;
|
|
152
|
+
moveConversationCopy: boolean;
|
|
149
153
|
claudeAccountAdd: boolean;
|
|
154
|
+
accountPools: boolean;
|
|
150
155
|
workerRelocation: number;
|
|
151
156
|
projectMemory: boolean;
|
|
152
157
|
fileWrite: boolean;
|
|
@@ -155,6 +160,7 @@ export declare const capabilities: {
|
|
|
155
160
|
memoryStoreBatch: boolean;
|
|
156
161
|
gitHosts: boolean;
|
|
157
162
|
projectHostToken: boolean;
|
|
163
|
+
gitRemoteIdentity: boolean;
|
|
158
164
|
};
|
|
159
165
|
export declare function capabilitiesForModules(snapshot: ModuleSnapshot): Record<string, unknown>;
|
|
160
166
|
export declare function requireRoute(snapshot: ModuleSnapshot, method: string, route: string): void;
|