@phren/cli 0.3.58 → 0.3.59
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 +18 -0
- package/dist/bridge/account-usage.js +57 -1
- 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 +5 -1
- package/dist/bridge/claude-credits.js +37 -29
- package/dist/bridge/command.js +12 -3
- 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/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/prompt-deliveries.d.ts +29 -1
- package/dist/bridge/prompt-deliveries.js +146 -10
- package/dist/bridge/schedule-watch.js +17 -3
- package/dist/bridge/server-routes.d.ts +3 -0
- package/dist/bridge/server-routes.js +66 -6
- package/dist/bridge/server.js +2 -1
- package/dist/bridge/session-move.d.ts +59 -1
- package/dist/bridge/session-move.js +266 -18
- 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-hook.mjs +11394 -9961
- 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 +3 -3
- package/dist/project-config.d.ts +14 -0
- package/dist/project-config.js +40 -1
- package/dist/tools/helper.d.ts +3 -0
- package/dist/tools/helper.js +44 -0
- package/package.json +1 -1
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
// the dispatch id (PHREN_DISPATCH_ID) its SessionStart or first prompt named,
|
|
16
16
|
// so a dispatch placed on this computer is matched by id as well as by pane
|
|
17
17
|
// and conversation.
|
|
18
|
-
import { lstat, readFile } from "node:fs/promises";
|
|
18
|
+
import { lstat, readdir, readFile } from "node:fs/promises";
|
|
19
19
|
import path from "node:path";
|
|
20
20
|
import { z } from "zod";
|
|
21
21
|
import { opencodeTurnStamps } from "./harness-status.js";
|
|
@@ -23,10 +23,14 @@ import { briefId } from "./launch-brief.js";
|
|
|
23
23
|
import { atomicInPrivateDir, bridgeRoot, provider, serverName, sessionId } from "./protocol.js";
|
|
24
24
|
/** Longest final reply kept from a Stop payload, in UTF-8 bytes (the receipt's limit). */
|
|
25
25
|
export const TURN_REPLY_LIMIT = 4000;
|
|
26
|
-
|
|
26
|
+
/** Most background tasks a record keeps from one Stop, running ones first. */
|
|
27
|
+
export const STOP_TASKS_LIMIT = 256;
|
|
28
|
+
// Room for a typical full task list beside the reply and cwd; a record still larger sheds tasks (`fitRecord`).
|
|
29
|
+
const MAX_RECORD_BYTES = 65_536;
|
|
27
30
|
// Not strict: a field a newer Hook adds (as `stop.cwd` was) is dropped on
|
|
28
31
|
// read here, never a reason to throw the pane's record away.
|
|
29
32
|
const stamp = z.object({ seq: z.number().int().min(0), at: z.string().datetime() });
|
|
33
|
+
const taskId = z.string().regex(/^[A-Za-z0-9._-]{1,128}$/);
|
|
30
34
|
const turnRecordSchema = z.object({
|
|
31
35
|
terminal: z.string().min(1).max(200), source: provider, session: sessionId,
|
|
32
36
|
/** Climbs with every recorded event, so prompt and stop are ordered without trusting the clock. */
|
|
@@ -41,6 +45,11 @@ const turnRecordSchema = z.object({
|
|
|
41
45
|
reply: z.string().max(TURN_REPLY_LIMIT).optional(), truncated: z.boolean().optional(),
|
|
42
46
|
/** The agent's working directory when the turn stopped: where its uncommitted work would be. */
|
|
43
47
|
cwd: z.string().max(4096).optional(),
|
|
48
|
+
/** Claude: the background tasks the Stop listed, by id (a sub-agent's is its agent id), still running
|
|
49
|
+
* or finished. The child tree trusts it over its quiet-transcript guess (transcript-claude.ts).
|
|
50
|
+
* `truncated`: the Stop listed more than STOP_TASKS_LIMIT; finished ones were dropped first. */
|
|
51
|
+
tasks: z.object({ running: z.array(taskId).max(STOP_TASKS_LIMIT), finished: z.array(taskId).max(STOP_TASKS_LIMIT),
|
|
52
|
+
truncated: z.boolean().optional() }).refine(tasks => tasks.running.length + tasks.finished.length <= STOP_TASKS_LIMIT).optional(),
|
|
44
53
|
}).optional(),
|
|
45
54
|
at: z.string().datetime(),
|
|
46
55
|
});
|
|
@@ -55,6 +64,23 @@ export function truncateUtf8(value, limit = TURN_REPLY_LIMIT) {
|
|
|
55
64
|
end--;
|
|
56
65
|
return { text: bytes.subarray(0, end).toString("utf8"), truncated: true };
|
|
57
66
|
}
|
|
67
|
+
/** A Stop's background task list as ids still running and ids finished; undefined when it sent none.
|
|
68
|
+
* Past STOP_TASKS_LIMIT, finished ids go before running ones and the list says it was cut. */
|
|
69
|
+
function stopTasks(value) {
|
|
70
|
+
if (!Array.isArray(value))
|
|
71
|
+
return undefined;
|
|
72
|
+
const running = [], finished = [];
|
|
73
|
+
for (const task of value) {
|
|
74
|
+
const { id, running: live } = (task ?? {});
|
|
75
|
+
if (typeof id !== "string" || !taskId.safeParse(id).success || typeof live !== "boolean")
|
|
76
|
+
continue;
|
|
77
|
+
(live ? running : finished).push(id);
|
|
78
|
+
}
|
|
79
|
+
if (running.length + finished.length <= STOP_TASKS_LIMIT)
|
|
80
|
+
return { running, finished };
|
|
81
|
+
const kept = running.slice(0, STOP_TASKS_LIMIT);
|
|
82
|
+
return { running: kept, finished: finished.slice(0, STOP_TASKS_LIMIT - kept.length), truncated: true };
|
|
83
|
+
}
|
|
58
84
|
/** The record after one lifecycle event; undefined for events that say nothing about turns.
|
|
59
85
|
* Another conversation or terminal in the pane starts a fresh record. */
|
|
60
86
|
export function nextTurn(previous, event) {
|
|
@@ -71,17 +97,33 @@ export function nextTurn(previous, event) {
|
|
|
71
97
|
record.startedAt ??= at;
|
|
72
98
|
// A resumed or cleared conversation in the same terminal no longer runs the background agents its
|
|
73
99
|
// last Stop counted; a compaction keeps them.
|
|
74
|
-
if ((event.start === "resume" || event.start === "clear") && record.stop
|
|
100
|
+
if ((event.start === "resume" || event.start === "clear") && record.stop) {
|
|
75
101
|
delete record.stop.background;
|
|
102
|
+
delete record.stop.tasks;
|
|
103
|
+
}
|
|
76
104
|
}
|
|
77
105
|
else if (event.event === "UserPromptSubmit")
|
|
78
106
|
record.prompt = { seq: record.seq, at };
|
|
79
107
|
else {
|
|
80
108
|
const reply = event.reply?.trim() ? truncateUtf8(event.reply.trim()) : undefined;
|
|
109
|
+
const tasks = stopTasks(event.backgroundTasks);
|
|
81
110
|
record.stop = { seq: record.seq, at, ...(event.background !== undefined ? { background: Math.min(999, Math.max(0, Math.floor(event.background))) } : {}),
|
|
111
|
+
...(tasks ? { tasks } : {}),
|
|
82
112
|
...(reply ? { reply: reply.text, ...(reply.truncated ? { truncated: true } : {}) } : {}),
|
|
83
113
|
...(event.cwd && path.isAbsolute(event.cwd) && event.cwd.length <= 4096 ? { cwd: event.cwd } : {}) };
|
|
84
114
|
}
|
|
115
|
+
return fitRecord(record);
|
|
116
|
+
}
|
|
117
|
+
/** `record` within MAX_RECORD_BYTES, which readRecord would otherwise throw away whole: its Stop's
|
|
118
|
+
* finished tasks go first, then running ones, and the list says it was cut. */
|
|
119
|
+
function fitRecord(record) {
|
|
120
|
+
const tasks = record.stop?.tasks;
|
|
121
|
+
while (tasks && Buffer.byteLength(JSON.stringify(record)) > MAX_RECORD_BYTES && (tasks.finished.length || tasks.running.length)) {
|
|
122
|
+
const over = Buffer.byteLength(JSON.stringify(record)) - MAX_RECORD_BYTES, drop = Math.max(1, Math.ceil(over / 64));
|
|
123
|
+
const list = tasks.finished.length ? tasks.finished : tasks.running;
|
|
124
|
+
list.splice(Math.max(0, list.length - drop));
|
|
125
|
+
tasks.truncated = true;
|
|
126
|
+
}
|
|
85
127
|
return record;
|
|
86
128
|
}
|
|
87
129
|
/** Where the recorded conversation is: no prompt yet, a prompt with no Stop
|
|
@@ -137,6 +179,39 @@ export function noteTurn(server, pane, event) {
|
|
|
137
179
|
export function readTurn(server, pane) {
|
|
138
180
|
return readRecord(turnPath(server, pane));
|
|
139
181
|
}
|
|
182
|
+
const STOP_TASKS_CACHE_MS = 2_000;
|
|
183
|
+
let stopTasksCache;
|
|
184
|
+
/** The background tasks the latest Stop of conversation `session` listed, from whichever pane's record
|
|
185
|
+
* holds it (the child tree knows the conversation, not its pane); undefined when no Stop listed any. */
|
|
186
|
+
export async function latestStopTasks(source, session) {
|
|
187
|
+
const root = path.join(bridgeRoot(), "turns"), now = Date.now();
|
|
188
|
+
if (!stopTasksCache || stopTasksCache.root !== root || now - stopTasksCache.at >= STOP_TASKS_CACHE_MS) {
|
|
189
|
+
stopTasksCache = { at: now, root, bySession: (async () => {
|
|
190
|
+
// The conversation's newest record decides, list or none: a resume in another pane (whose agents died
|
|
191
|
+
// with the old process) outdates the list a closed pane's record still holds.
|
|
192
|
+
// Panes are read in name order, and a record written in the same millisecond as another wins only when
|
|
193
|
+
// its last event came after its own Stop (the pane that went on), so a tie never depends on directory order.
|
|
194
|
+
const newest = new Map();
|
|
195
|
+
for (const server of (await readdir(root).catch(() => [])).sort()) {
|
|
196
|
+
for (const name of (await readdir(path.join(root, server)).catch(() => [])).filter(name => name.endsWith(".json")).sort()) {
|
|
197
|
+
const record = await readRecord(path.join(root, server, name));
|
|
198
|
+
if (!record)
|
|
199
|
+
continue;
|
|
200
|
+
const key = `${record.source}\0${record.session}`, previous = newest.get(key);
|
|
201
|
+
const at = Date.parse(record.at), before = previous ? Date.parse(previous.at) : -Infinity;
|
|
202
|
+
if (at > before || (at === before && record.stop?.seq !== record.seq && previous?.stop?.seq === previous?.seq))
|
|
203
|
+
newest.set(key, record);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const found = new Map();
|
|
207
|
+
for (const [key, record] of newest)
|
|
208
|
+
if (record.stop?.tasks)
|
|
209
|
+
found.set(key, { seq: record.stop.seq, at: record.stop.at, ...record.stop.tasks });
|
|
210
|
+
return found;
|
|
211
|
+
})() };
|
|
212
|
+
}
|
|
213
|
+
return (await stopTasksCache.bySession).get(`${source}\0${session}`);
|
|
214
|
+
}
|
|
140
215
|
/** OpenCode's turns for a pane running `pids`, from the stamps phren's OpenCode
|
|
141
216
|
* plugin writes on session.status busy / session.idle; undefined for an older
|
|
142
217
|
* plugin that writes none. `terminal` is the pane's, since the PIDs are. */
|