@phren/cli 0.3.30 → 0.3.32
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/bridge/agent-hooks.d.ts +5 -55
- package/dist/bridge/agent-hooks.js +45 -162
- package/dist/bridge/approval-rules.d.ts +70 -0
- package/dist/bridge/approval-rules.js +231 -0
- package/dist/bridge/authority.d.ts +1 -2
- package/dist/bridge/authority.js +2 -7
- package/dist/bridge/changes.d.ts +4 -0
- package/dist/bridge/changes.js +47 -33
- package/dist/bridge/claude-suggestion.js +1 -1
- package/dist/bridge/codex-servers.d.ts +12 -0
- package/dist/bridge/codex-servers.js +35 -3
- package/dist/bridge/codex-thread-origin.d.ts +12 -0
- package/dist/bridge/codex-thread-origin.js +31 -0
- package/dist/bridge/dispatch.js +1 -1
- package/dist/bridge/enroll.d.ts +6 -0
- package/dist/bridge/enroll.js +10 -4
- package/dist/bridge/git-worktrees.js +14 -17
- package/dist/bridge/git.d.ts +10 -2
- package/dist/bridge/git.js +148 -107
- package/dist/bridge/hand-off-queue.d.ts +10 -0
- package/dist/bridge/hand-off-queue.js +33 -10
- package/dist/bridge/health.js +9 -5
- package/dist/bridge/herdr.js +6 -1
- package/dist/bridge/hook-main.js +12 -2
- package/dist/bridge/locate.js +14 -1
- package/dist/bridge/projects.d.ts +8 -0
- package/dist/bridge/projects.js +142 -24
- package/dist/bridge/prompt-deliveries.d.ts +99 -0
- package/dist/bridge/prompt-deliveries.js +327 -0
- package/dist/bridge/prompt-once.d.ts +2 -0
- package/dist/bridge/prompt-once.js +2 -0
- package/dist/bridge/schedule-format.d.ts +10 -0
- package/dist/bridge/schedule-format.js +24 -1
- package/dist/bridge/schedules.d.ts +3 -1
- package/dist/bridge/schedules.js +96 -40
- package/dist/bridge/server-launch.js +39 -15
- package/dist/bridge/server-pane-routes.js +91 -45
- package/dist/bridge/server-routes.d.ts +3 -0
- package/dist/bridge/server-routes.js +36 -8
- package/dist/bridge/server-stream.js +4 -2
- package/dist/bridge/server.js +12 -3
- package/dist/bridge/speech-key.d.ts +2 -9
- package/dist/bridge/speech-key.js +4 -37
- package/dist/bridge/sudo.d.ts +1 -1
- package/dist/bridge/sudo.js +51 -17
- package/dist/bridge/terminal-tmux.js +22 -4
- package/dist/bridge/tmux-test-server.d.ts +13 -0
- package/dist/bridge/tmux-test-server.js +103 -0
- package/dist/bridge/transcript-claude.d.ts +3 -3
- package/dist/bridge/transcript-claude.js +7 -7
- package/dist/bridge/transcript-codex.js +6 -4
- package/dist/bridge/transcript-preview.js +1 -1
- package/dist/bridge/transcripts.js +14 -3
- package/dist/bridge-hook.mjs +16011 -14121
- package/dist/cli/schedules.js +8 -2
- package/dist/cli/session-stop.js +9 -1
- package/dist/cli-registry.js +1 -1
- package/dist/core/project-registry.d.ts +1 -0
- package/dist/core/project-registry.js +6 -2
- package/dist/core/project.d.ts +1 -0
- package/dist/core/project.js +5 -0
- package/dist/generated/memory-ui-graph.browser.js +328 -333
- package/dist/init/setup.js +2 -2
- package/dist/memory-ui-graph.runtime.js +328 -333
- package/dist/modules/registry.js +9 -7
- package/dist/phren-core.js +1 -1
- package/dist/phren-paths.d.ts +2 -2
- package/dist/phren-paths.js +2 -2
- package/dist/store-routing.d.ts +2 -2
- package/dist/store-routing.js +2 -2
- package/dist/sync/git-state.d.ts +13 -0
- package/dist/sync/git-state.js +22 -0
- package/dist/sync/store-merge.d.ts +2 -0
- package/dist/sync/store-merge.js +6 -1
- package/dist/test-global-setup.js +7 -0
- package/dist/tools/data.js +1 -1
- package/dist/tools/finding.js +1 -1
- package/dist/tools/search.js +1 -1
- package/dist/tools/tasks.js +1 -1
- package/package.json +8 -8
- package/starter/global/skills/conductor/SKILL.md +2 -2
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { open } from "node:fs/promises";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { atomicInPrivateDir, object, objects, provider } from "./protocol.js";
|
|
6
|
+
import { transcriptPath, unwrapPastedContent } from "./transcripts.js";
|
|
7
|
+
/** A message is kept this long, by its id, so a late status or a retry still
|
|
8
|
+
* finds it: a busy Codex turn can run far past the old ten minutes. */
|
|
9
|
+
export const DELIVERY_RETENTION_MS = 24 * 3_600_000;
|
|
10
|
+
/** An agent that ended its turn after the message was typed and then stayed
|
|
11
|
+
* idle this long without taking it has dropped it. */
|
|
12
|
+
export const DELIVERY_GIVE_UP_MS = 30_000;
|
|
13
|
+
const MAX_RECORDS = 512;
|
|
14
|
+
/** What the agent hands its UserPromptSubmit hook is the terminal's pasted
|
|
15
|
+
* form of what Phren typed; compare the words, not the wrapping. Claude Code
|
|
16
|
+
* also takes each attached picture's path line out of the text and puts an
|
|
17
|
+
* "[Image #N]" label at the front, so neither side keeps picture paths or
|
|
18
|
+
* labels. */
|
|
19
|
+
export function promptKey(text) {
|
|
20
|
+
return unwrapPastedContent(text).split("\n").filter(line => !PICTURE_PATH_LINE.test(line)).join("\n")
|
|
21
|
+
.replace(/\[Image #\d+\]/g, " ").replace(/\s+/g, " ").trim();
|
|
22
|
+
}
|
|
23
|
+
const PICTURE_PATH_LINE = /^\s*\/.*\.(?:png|jpe?g|gif|webp)\s*$/i;
|
|
24
|
+
export const promptHash = (text) => createHash("sha256").update(promptKey(text)).digest("hex");
|
|
25
|
+
const recordSchema = z.object({
|
|
26
|
+
id: z.string().regex(/^[A-Za-z0-9_-]{8,64}$/).optional(), hash: z.string().regex(/^[0-9a-f]{64}$/),
|
|
27
|
+
server: z.string().min(1).max(200), pane: z.string().min(1).max(200), source: provider, session: z.string().min(1).max(200),
|
|
28
|
+
terminal: z.string().max(200).optional(), state: z.enum(["typed", "queued", "delivered", "failed"]),
|
|
29
|
+
reason: z.string().max(500).optional(), at: z.number(), settledAt: z.number().optional(), stoppedAt: z.number().optional(),
|
|
30
|
+
}).strict();
|
|
31
|
+
/** One message per record, bound to the pane it was typed into (server, pane,
|
|
32
|
+
* agent kind), not to the conversation: a Codex thread or a Claude /clear can
|
|
33
|
+
* replace the conversation while the agent still holds the typed text, and
|
|
34
|
+
* the conversation that submits it is the one it reached. Records hold a hash
|
|
35
|
+
* of the words, never the text, and those with a phone id are written to
|
|
36
|
+
* `file` so a Hook restart keeps their state. */
|
|
37
|
+
export class PromptDeliveries {
|
|
38
|
+
file;
|
|
39
|
+
now;
|
|
40
|
+
landed;
|
|
41
|
+
records = [];
|
|
42
|
+
waiters = new Map();
|
|
43
|
+
writing = Promise.resolve();
|
|
44
|
+
constructor(file, now = Date.now, landed = transcriptHas) {
|
|
45
|
+
this.file = file;
|
|
46
|
+
this.now = now;
|
|
47
|
+
this.landed = landed;
|
|
48
|
+
if (!file)
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
for (const row of objects(object(JSON.parse(readFileSync(file, "utf8"))).records)) {
|
|
52
|
+
const parsed = recordSchema.safeParse(row);
|
|
53
|
+
// A message the Hook was typing when it stopped is in the pane or
|
|
54
|
+
// nowhere: wait for the agent, and give up as for any other.
|
|
55
|
+
if (parsed.success)
|
|
56
|
+
this.records.push({ ...parsed.data, state: parsed.data.state === "typed" ? "queued" : parsed.data.state });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch { /* No file yet, or an unreadable one: start empty. */ }
|
|
60
|
+
this.prune();
|
|
61
|
+
}
|
|
62
|
+
samePane(record, target) {
|
|
63
|
+
return record.server === target.server && record.pane === target.pane && record.source === target.source;
|
|
64
|
+
}
|
|
65
|
+
byId(id) { return this.records.find(record => record.id === id); }
|
|
66
|
+
prune() {
|
|
67
|
+
const cutoff = this.now() - DELIVERY_RETENTION_MS;
|
|
68
|
+
this.records = this.records.filter(record => record.at >= cutoff || this.waiters.has(record));
|
|
69
|
+
while (this.records.length > MAX_RECORDS)
|
|
70
|
+
this.records.shift();
|
|
71
|
+
}
|
|
72
|
+
save() {
|
|
73
|
+
if (!this.file)
|
|
74
|
+
return;
|
|
75
|
+
const rows = this.records.filter(record => record.id).map(({ idleSince: _, ...row }) => row);
|
|
76
|
+
const file = this.file;
|
|
77
|
+
this.writing = this.writing.then(() => atomicInPrivateDir(file, { records: rows })).catch(() => undefined);
|
|
78
|
+
}
|
|
79
|
+
/** Settled writes, for tests and an orderly shutdown. */
|
|
80
|
+
flush() { return this.writing; }
|
|
81
|
+
/** Register a message about to be typed into `target`'s pane. Settles
|
|
82
|
+
* "delivered" once a conversation in that pane submits the words, or
|
|
83
|
+
* "pending" after `waitMs`; the record outlives the wait. A retry of a
|
|
84
|
+
* failed id replaces its record. Aborting (the terminal refused before
|
|
85
|
+
* writing) drops it. */
|
|
86
|
+
expect(target, text, waitMs, signal, id, terminal) {
|
|
87
|
+
if (!promptKey(text) || signal?.aborted)
|
|
88
|
+
return Promise.resolve("pending");
|
|
89
|
+
this.prune();
|
|
90
|
+
if (id)
|
|
91
|
+
this.records = this.records.filter(record => record.id !== id);
|
|
92
|
+
const record = { ...(id ? { id } : {}), hash: promptHash(text), server: target.server, pane: target.pane, source: target.source,
|
|
93
|
+
session: target.session, ...(typeof terminal === "string" ? { terminal } : {}), state: "typed", at: this.now() };
|
|
94
|
+
this.records.push(record);
|
|
95
|
+
this.save();
|
|
96
|
+
return new Promise(resolve => {
|
|
97
|
+
const timer = setTimeout(() => this.release(record, "pending"), waitMs);
|
|
98
|
+
timer.unref?.();
|
|
99
|
+
this.waiters.set(record, { resolve, timer });
|
|
100
|
+
signal?.addEventListener("abort", () => {
|
|
101
|
+
this.release(record, "pending");
|
|
102
|
+
if (record.state === "typed") {
|
|
103
|
+
this.records = this.records.filter(entry => entry !== record);
|
|
104
|
+
this.save();
|
|
105
|
+
}
|
|
106
|
+
}, { once: true });
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
release(record, outcome) {
|
|
110
|
+
const waiter = this.waiters.get(record);
|
|
111
|
+
if (!waiter)
|
|
112
|
+
return;
|
|
113
|
+
clearTimeout(waiter.timer);
|
|
114
|
+
waiter.resolve?.(outcome);
|
|
115
|
+
waiter.resolve = undefined;
|
|
116
|
+
if (outcome !== "pending")
|
|
117
|
+
waiter.late?.(outcome);
|
|
118
|
+
if (outcome !== "pending" || !waiter.late)
|
|
119
|
+
this.waiters.delete(record);
|
|
120
|
+
}
|
|
121
|
+
open(target, text) {
|
|
122
|
+
const hash = promptHash(text);
|
|
123
|
+
return this.records.find(record => record.hash === hash && this.samePane(record, target) && (record.state === "typed" || record.state === "queued"));
|
|
124
|
+
}
|
|
125
|
+
/** A message Phren typed into `target`'s pane that no conversation there has submitted yet. */
|
|
126
|
+
pending(target, text) { return !!this.open(target, text); }
|
|
127
|
+
/** Wait again for a message `expect` reported pending, after a second Enter. */
|
|
128
|
+
awaitLate(target, text, waitMs = 2_500) {
|
|
129
|
+
const record = this.open(target, text);
|
|
130
|
+
if (!record)
|
|
131
|
+
return Promise.resolve("pending");
|
|
132
|
+
return new Promise(resolve => {
|
|
133
|
+
const waiter = this.waiters.get(record) ?? {};
|
|
134
|
+
const timer = setTimeout(() => { waiter.late = undefined; if (!waiter.resolve)
|
|
135
|
+
this.waiters.delete(record); resolve("pending"); }, waitMs);
|
|
136
|
+
timer.unref?.();
|
|
137
|
+
waiter.late = outcome => { clearTimeout(timer); waiter.late = undefined; resolve(outcome); };
|
|
138
|
+
this.waiters.set(record, waiter);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
find(id, target) {
|
|
142
|
+
const record = this.byId(id);
|
|
143
|
+
return record && this.samePane(record, target) && this.now() - record.at <= DELIVERY_RETENTION_MS ? record : undefined;
|
|
144
|
+
}
|
|
145
|
+
/** The Hook answered `id` as typed and held by the agent. */
|
|
146
|
+
queue(id, target) {
|
|
147
|
+
const record = this.find(id, target);
|
|
148
|
+
if (record?.state === "typed") {
|
|
149
|
+
record.state = "queued";
|
|
150
|
+
this.save();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Delivered without a hook: an app-server turn or a served OpenCode prompt. */
|
|
154
|
+
delivered(id, target, text) {
|
|
155
|
+
if (!id)
|
|
156
|
+
return;
|
|
157
|
+
this.records = this.records.filter(record => record.id !== id);
|
|
158
|
+
this.records.push({ id, hash: promptHash(text), server: target.server, pane: target.pane, source: target.source, session: target.session,
|
|
159
|
+
state: "delivered", at: this.now(), settledAt: this.now() });
|
|
160
|
+
this.save();
|
|
161
|
+
}
|
|
162
|
+
/** The message did not reach the agent, and why. It stays matchable, so a
|
|
163
|
+
* late submission still turns it delivered. */
|
|
164
|
+
fail(id, target, reason) {
|
|
165
|
+
const record = id ? this.find(id, target) : undefined;
|
|
166
|
+
if (!record || record.state === "delivered")
|
|
167
|
+
return;
|
|
168
|
+
record.state = "failed";
|
|
169
|
+
record.reason = reason.slice(0, 500);
|
|
170
|
+
record.settledAt = this.now();
|
|
171
|
+
this.save();
|
|
172
|
+
}
|
|
173
|
+
/** The pane's Codex server moved to another thread (/new, /resume):
|
|
174
|
+
* input still pending on the old one stays there, out of the pane's view.
|
|
175
|
+
* Fails loudly; a late UserPromptSubmit from the old thread still settles it. */
|
|
176
|
+
threadChanged(previous) {
|
|
177
|
+
let changed = false;
|
|
178
|
+
for (const record of this.records)
|
|
179
|
+
if ((record.state === "typed" || record.state === "queued") && this.samePane(record, previous) && record.session === previous.session) {
|
|
180
|
+
record.state = "failed";
|
|
181
|
+
record.settledAt = this.now();
|
|
182
|
+
changed = true;
|
|
183
|
+
record.reason = "The pane moved to another Codex conversation before the agent took the message.";
|
|
184
|
+
}
|
|
185
|
+
if (changed)
|
|
186
|
+
this.save();
|
|
187
|
+
}
|
|
188
|
+
/** False when `id` names another message: other words or another pane. */
|
|
189
|
+
sameMessage(id, target, text) {
|
|
190
|
+
const record = this.byId(id);
|
|
191
|
+
return !record || (this.samePane(record, target) && record.hash === promptHash(text));
|
|
192
|
+
}
|
|
193
|
+
/** What became of phone message `id` in `target`'s pane. `unknown` while the
|
|
194
|
+
* Hook has not answered it, for another pane, or past retention. */
|
|
195
|
+
status(id, target) {
|
|
196
|
+
const record = this.find(id, target);
|
|
197
|
+
if (!record || record.state === "typed")
|
|
198
|
+
return { state: "unknown" };
|
|
199
|
+
return { state: record.state, ...(record.reason && record.state === "failed" ? { reason: record.reason } : {}), session: record.session };
|
|
200
|
+
}
|
|
201
|
+
/** Every known phone message in `target`'s pane, for its conversation's stream. */
|
|
202
|
+
forPane(target) {
|
|
203
|
+
return this.records.filter(record => record.id && this.samePane(record, target)).map(record => ({ deliveryId: record.id, ...this.status(record.id, target) }))
|
|
204
|
+
.filter(item => item.state !== "unknown");
|
|
205
|
+
}
|
|
206
|
+
/** Conversation `target` just submitted `prompt`. Its own record wins, then
|
|
207
|
+
* the oldest open one in the same pane: the agent kept the text across a
|
|
208
|
+
* conversation change, and the conversation now in the pane is where it
|
|
209
|
+
* went. A prompt nothing matches was typed locally. Never refused. */
|
|
210
|
+
submitted(target, prompt) {
|
|
211
|
+
const hash = promptHash(prompt);
|
|
212
|
+
const open = this.records.filter(record => record.hash === hash && this.samePane(record, target) && record.state !== "delivered");
|
|
213
|
+
const record = open.find(entry => entry.session === target.session) ?? open[0];
|
|
214
|
+
if (!record)
|
|
215
|
+
return;
|
|
216
|
+
record.state = "delivered";
|
|
217
|
+
record.session = target.session;
|
|
218
|
+
record.settledAt = this.now();
|
|
219
|
+
delete record.reason;
|
|
220
|
+
this.save();
|
|
221
|
+
this.release(record, "delivered");
|
|
222
|
+
}
|
|
223
|
+
/** A turn in `target`'s pane ended: the agent now takes held input or never will. */
|
|
224
|
+
stopped(target) {
|
|
225
|
+
const at = this.now();
|
|
226
|
+
for (const record of this.records)
|
|
227
|
+
if (this.samePane(record, target) && (record.state === "typed" || record.state === "queued") && record.at < at)
|
|
228
|
+
record.stoppedAt = at;
|
|
229
|
+
}
|
|
230
|
+
/** Called on the Hook's activity tick with a server's panes. A message
|
|
231
|
+
* still held fails loudly when its pane closed, its agent restarted, or the
|
|
232
|
+
* agent ended a turn and stayed idle without taking it (unless its
|
|
233
|
+
* transcript shows it after all). */
|
|
234
|
+
async observe(server, panes) {
|
|
235
|
+
const now = this.now();
|
|
236
|
+
let changed = false;
|
|
237
|
+
for (const record of this.records) {
|
|
238
|
+
if (record.server !== server || (record.state !== "typed" && record.state !== "queued"))
|
|
239
|
+
continue;
|
|
240
|
+
const pane = panes.find(entry => entry.pane_id === record.pane);
|
|
241
|
+
if (!pane || (record.terminal && pane.terminal_id !== record.terminal) || (pane.agent && pane.agent !== record.source)) {
|
|
242
|
+
if (await this.landed(record).catch(() => false)) {
|
|
243
|
+
record.state = "delivered";
|
|
244
|
+
record.settledAt = now;
|
|
245
|
+
changed = true;
|
|
246
|
+
this.release(record, "delivered");
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
record.state = "failed";
|
|
250
|
+
record.settledAt = now;
|
|
251
|
+
changed = true;
|
|
252
|
+
record.reason = pane ? "The agent in this pane restarted before it took the message." : "The agent's pane closed before it took the message.";
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (!["idle", "done"].includes(String(pane.agent_status))) {
|
|
256
|
+
record.idleSince = undefined;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
record.idleSince ??= now;
|
|
260
|
+
if (record.stoppedAt === undefined || now - Math.max(record.idleSince, record.stoppedAt) < DELIVERY_GIVE_UP_MS)
|
|
261
|
+
continue;
|
|
262
|
+
if (await this.landed(record).catch(() => false)) {
|
|
263
|
+
record.state = "delivered";
|
|
264
|
+
record.settledAt = now;
|
|
265
|
+
changed = true;
|
|
266
|
+
this.release(record, "delivered");
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
record.state = "failed";
|
|
270
|
+
record.settledAt = now;
|
|
271
|
+
changed = true;
|
|
272
|
+
record.reason = "The agent finished its turn without taking the message.";
|
|
273
|
+
}
|
|
274
|
+
if (changed)
|
|
275
|
+
this.save();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/** Whether a user row with these words reached the conversation's transcript
|
|
279
|
+
* after the message was typed: the agent took it without its hook saying so
|
|
280
|
+
* (a mid-turn steer). Reads at most the last 512 KiB. */
|
|
281
|
+
async function transcriptHas(record) {
|
|
282
|
+
const file = await open(await transcriptPath(record.source, record.session), "r");
|
|
283
|
+
try {
|
|
284
|
+
const size = (await file.stat()).size, start = Math.max(0, size - 524_288), buffer = Buffer.alloc(size - start);
|
|
285
|
+
await file.read(buffer, 0, buffer.length, start);
|
|
286
|
+
for (const line of buffer.toString("utf8").split("\n").reverse()) {
|
|
287
|
+
let row;
|
|
288
|
+
try {
|
|
289
|
+
row = object(JSON.parse(line));
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const stamp = Date.parse(String(row.timestamp ?? ""));
|
|
295
|
+
if (Number.isFinite(stamp) && stamp < record.at - 5_000)
|
|
296
|
+
break;
|
|
297
|
+
for (const text of userTexts(row))
|
|
298
|
+
if (promptHash(text) === record.hash)
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
await file.close();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** A transcript row's user-typed text: Claude's user rows, Codex's
|
|
308
|
+
* user_message events and user response items. */
|
|
309
|
+
export function userTexts(row) {
|
|
310
|
+
const texts = [];
|
|
311
|
+
const blocks = (content) => {
|
|
312
|
+
if (typeof content === "string")
|
|
313
|
+
texts.push(content);
|
|
314
|
+
else
|
|
315
|
+
for (const block of objects(content))
|
|
316
|
+
if (typeof block.text === "string")
|
|
317
|
+
texts.push(block.text);
|
|
318
|
+
};
|
|
319
|
+
if (row.type === "user" && !row.isMeta)
|
|
320
|
+
blocks(object(row.message).content);
|
|
321
|
+
const payload = object(row.payload);
|
|
322
|
+
if (payload.type === "user_message" && typeof payload.message === "string")
|
|
323
|
+
texts.push(payload.message);
|
|
324
|
+
if (payload.type === "message" && payload.role === "user")
|
|
325
|
+
blocks(payload.content);
|
|
326
|
+
return texts;
|
|
327
|
+
}
|
|
@@ -17,6 +17,8 @@ export declare class PromptOnce {
|
|
|
17
17
|
private attempts;
|
|
18
18
|
constructor(now?: () => number);
|
|
19
19
|
run(id: string | undefined, scope: string, send: (typing: () => void) => Promise<Json>): Promise<Json>;
|
|
20
|
+
/** Lets a message the Hook found failed be typed again under its id. */
|
|
21
|
+
forget(id: string): void;
|
|
20
22
|
private prune;
|
|
21
23
|
}
|
|
22
24
|
/** What a delivery id is bound to: the pane, its agent and the exact text.
|
|
@@ -50,6 +50,8 @@ export class PromptOnce {
|
|
|
50
50
|
throw error;
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
+
/** Lets a message the Hook found failed be typed again under its id. */
|
|
54
|
+
forget(id) { this.attempts.delete(id); }
|
|
53
55
|
prune() {
|
|
54
56
|
const cutoff = this.now() - REMEMBER_MS;
|
|
55
57
|
for (const [id, attempt] of this.attempts)
|
|
@@ -26,6 +26,8 @@ export interface Schedule {
|
|
|
26
26
|
/** Claude account id (a slug) the run uses; absent means the default account. */
|
|
27
27
|
account?: string;
|
|
28
28
|
notify?: ScheduleNotify[];
|
|
29
|
+
/** Explicit targets; absent means the directory owning this schedule. */
|
|
30
|
+
projects?: string[];
|
|
29
31
|
every: ScheduleEvery;
|
|
30
32
|
at?: string;
|
|
31
33
|
days?: Weekday[];
|
|
@@ -50,6 +52,8 @@ export interface ScheduleRun {
|
|
|
50
52
|
id: string;
|
|
51
53
|
scheduleId: string;
|
|
52
54
|
project: string;
|
|
55
|
+
scheduleProject?: string;
|
|
56
|
+
batchId?: string;
|
|
53
57
|
startedAt: string;
|
|
54
58
|
finishedAt?: string;
|
|
55
59
|
status: ScheduleRunStatus;
|
|
@@ -84,6 +88,7 @@ export interface ScheduleStatus extends Schedule {
|
|
|
84
88
|
project: string;
|
|
85
89
|
nextRun: string | null;
|
|
86
90
|
lastRun: Omit<ScheduleRun, "id" | "scheduleId" | "project"> | null;
|
|
91
|
+
lastRuns: ScheduleRun[];
|
|
87
92
|
running: boolean;
|
|
88
93
|
}
|
|
89
94
|
interface ScheduleDocument {
|
|
@@ -100,6 +105,11 @@ export declare function computerMatches(wanted: string, current: string): boolea
|
|
|
100
105
|
export declare function scheduleNotifications(schedule: Schedule): Set<ScheduleNotify>;
|
|
101
106
|
export declare function scheduleSessionRoute(schedule: Schedule, launch: ScheduleLaunchRecord): string | undefined;
|
|
102
107
|
export declare function nextRun(schedule: Schedule, lastRun?: ScheduleRun): Date | null;
|
|
108
|
+
/** Summarize the newest firing of one owner's schedule, including old single runs. */
|
|
109
|
+
export declare function latestScheduleBatch(runs: ScheduleRun[]): {
|
|
110
|
+
lastRun: ScheduleRun | undefined;
|
|
111
|
+
lastRuns: ScheduleRun[];
|
|
112
|
+
};
|
|
103
113
|
export declare function readScheduleRuns(file: string): Promise<ScheduleRun[]>;
|
|
104
114
|
export declare function writeScheduleRuns(file: string, runs: ScheduleRun[]): Promise<void>;
|
|
105
115
|
export declare function newScheduleId(existing: Iterable<string>): string;
|
|
@@ -4,6 +4,7 @@ import path from "node:path";
|
|
|
4
4
|
import * as yaml from "js-yaml";
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
import { atomicInPrivateDir, bridgeRoot, object } from "./protocol.js";
|
|
7
|
+
import { isValidProjectName } from "../utils-paths.js";
|
|
7
8
|
import { isAccountSlug } from "./claude-accounts.js";
|
|
8
9
|
/** The schedules.yaml store: the schedule and run record shapes, validation,
|
|
9
10
|
* the five timing forms evaluated in local time, and the run history file. */
|
|
@@ -61,6 +62,9 @@ export function parseSchedule(value) {
|
|
|
61
62
|
createdAt: timestamp.parse(raw.createdAt),
|
|
62
63
|
updatedAt: timestamp.parse(raw.updatedAt),
|
|
63
64
|
};
|
|
65
|
+
if (raw.projects !== undefined)
|
|
66
|
+
schedule.projects = z.array(z.string().refine(isValidProjectName, "Invalid project slug."))
|
|
67
|
+
.min(1).max(64).refine(projects => new Set(projects).size === projects.length, "Projects must be unique.").parse(raw.projects);
|
|
64
68
|
if (raw.model !== undefined)
|
|
65
69
|
schedule.model = singleLine(200).parse(raw.model);
|
|
66
70
|
if (raw.account !== undefined)
|
|
@@ -227,6 +231,18 @@ export function nextRun(schedule, lastRun) {
|
|
|
227
231
|
return nextCron(schedule, after);
|
|
228
232
|
return nextDailyOrWeekly(schedule, after);
|
|
229
233
|
}
|
|
234
|
+
/** Summarize the newest firing of one owner's schedule, including old single runs. */
|
|
235
|
+
export function latestScheduleBatch(runs) {
|
|
236
|
+
const latest = runs.at(-1);
|
|
237
|
+
const lastRuns = latest ? runs.filter(run => latest.batchId ? run.batchId === latest.batchId : run.id === latest.id) : [];
|
|
238
|
+
const priority = ["blocked", "running", "launched", "failed", "needs-you", "finished", "skipped"];
|
|
239
|
+
const representative = [...lastRuns].sort((a, b) => priority.indexOf(a.status) - priority.indexOf(b.status))[0];
|
|
240
|
+
if (!representative)
|
|
241
|
+
return { lastRun: undefined, lastRuns };
|
|
242
|
+
const finishedAt = lastRuns.some(run => runningStatuses.has(run.status)) ? undefined
|
|
243
|
+
: lastRuns.map(run => run.finishedAt).filter((time) => Boolean(time)).sort().at(-1);
|
|
244
|
+
return { lastRun: { ...representative, finishedAt }, lastRuns };
|
|
245
|
+
}
|
|
230
246
|
export async function readScheduleRuns(file) {
|
|
231
247
|
let text;
|
|
232
248
|
try {
|
|
@@ -246,6 +262,8 @@ export async function readScheduleRuns(file) {
|
|
|
246
262
|
const launch = object(raw.launch);
|
|
247
263
|
if (typeof raw.id !== "string" || typeof raw.scheduleId !== "string" || typeof raw.project !== "string"
|
|
248
264
|
|| typeof raw.startedAt !== "string" || !SCHEDULE_RUN_STATUSES.includes(String(raw.status))
|
|
265
|
+
|| (raw.scheduleProject !== undefined && typeof raw.scheduleProject !== "string")
|
|
266
|
+
|| (raw.batchId !== undefined && typeof raw.batchId !== "string")
|
|
249
267
|
|| !["herdr", "headless"].includes(String(launch.mode)))
|
|
250
268
|
continue;
|
|
251
269
|
runs.push(raw);
|
|
@@ -255,7 +273,12 @@ export async function readScheduleRuns(file) {
|
|
|
255
273
|
return runs;
|
|
256
274
|
}
|
|
257
275
|
export async function writeScheduleRuns(file, runs) {
|
|
258
|
-
|
|
276
|
+
// Open children still block their owner. Keep entire retained batches so
|
|
277
|
+
// trimming cannot hide a sibling's failure from the aggregate status.
|
|
278
|
+
const recent = new Set(runs.slice(-MAX_RUNS).map(run => run.id));
|
|
279
|
+
const batches = new Set(runs.filter(run => recent.has(run.id) || runningStatuses.has(run.status))
|
|
280
|
+
.map(run => run.batchId).filter((id) => Boolean(id)));
|
|
281
|
+
const kept = runs.filter(run => recent.has(run.id) || runningStatuses.has(run.status) || (run.batchId && batches.has(run.batchId)));
|
|
259
282
|
await atomicInPrivateDir(file, kept.map(run => JSON.stringify(run)).join("\n") + (kept.length ? "\n" : ""));
|
|
260
283
|
}
|
|
261
284
|
export function newScheduleId(existing) {
|
|
@@ -13,6 +13,7 @@ export declare class Scheduler {
|
|
|
13
13
|
private readonly log;
|
|
14
14
|
private serial;
|
|
15
15
|
private ticking;
|
|
16
|
+
private readonly dispatching;
|
|
16
17
|
/** Runs this process launched; any other open run was left by an earlier Hook. */
|
|
17
18
|
private readonly own;
|
|
18
19
|
private recovered;
|
|
@@ -41,7 +42,8 @@ export declare class Scheduler {
|
|
|
41
42
|
id?: string;
|
|
42
43
|
limit?: number;
|
|
43
44
|
}): Promise<ScheduleRun[]>;
|
|
44
|
-
launchNow(projectName: string, id: string): Promise<ScheduleRun>;
|
|
45
|
+
launchNow(projectName: string, id: string, onlyIfDue?: boolean, expectedUpdatedAt?: string): Promise<ScheduleRun[]>;
|
|
46
|
+
private prepareAndLaunch;
|
|
45
47
|
private updateRun;
|
|
46
48
|
private finishRun;
|
|
47
49
|
private recordBlockedStartup;
|