@phren/cli 0.3.19 → 0.3.20

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.
Files changed (43) hide show
  1. package/dist/bridge/agent-hooks.d.ts +16 -1
  2. package/dist/bridge/agent-hooks.js +51 -4
  3. package/dist/bridge/changes.d.ts +4 -0
  4. package/dist/bridge/changes.js +25 -2
  5. package/dist/bridge/command.js +24 -6
  6. package/dist/bridge/dispatch-returns.d.ts +11 -0
  7. package/dist/bridge/dispatch-returns.js +47 -7
  8. package/dist/bridge/dispatch.d.ts +19 -0
  9. package/dist/bridge/dispatch.js +164 -5
  10. package/dist/bridge/hook-fast.d.ts +13 -1
  11. package/dist/bridge/hook-fast.js +17 -2
  12. package/dist/bridge/models.d.ts +11 -1
  13. package/dist/bridge/models.js +47 -2
  14. package/dist/bridge/server-launch.d.ts +20 -0
  15. package/dist/bridge/server-launch.js +63 -1
  16. package/dist/bridge/server-routes.d.ts +4 -0
  17. package/dist/bridge/server-routes.js +2 -2
  18. package/dist/bridge/server.js +6 -1
  19. package/dist/bridge/speech-live.d.ts +45 -0
  20. package/dist/bridge/speech-live.js +413 -0
  21. package/dist/bridge/speech-transcribe.d.ts +5 -2
  22. package/dist/bridge/speech-transcribe.js +8 -5
  23. package/dist/bridge/speech-voice.d.ts +22 -1
  24. package/dist/bridge/speech-voice.js +20 -2
  25. package/dist/bridge/speech.d.ts +24 -4
  26. package/dist/bridge/speech.js +115 -14
  27. package/dist/bridge/stalls.d.ts +9 -2
  28. package/dist/bridge/stalls.js +20 -4
  29. package/dist/bridge/transcript-preview.d.ts +11 -1
  30. package/dist/bridge/transcript-preview.js +37 -11
  31. package/dist/bridge/transcripts.d.ts +2 -1
  32. package/dist/bridge/transcripts.js +7 -3
  33. package/dist/bridge/turn-records.d.ts +6 -3
  34. package/dist/bridge/turn-records.js +9 -4
  35. package/dist/bridge/usage.d.ts +1 -1
  36. package/dist/bridge/usage.js +4 -3
  37. package/dist/bridge/worker-close.js +2 -1
  38. package/dist/bridge/worker-unfinished.d.ts +37 -0
  39. package/dist/bridge/worker-unfinished.js +145 -0
  40. package/dist/bridge-hook.mjs +2001 -1130
  41. package/dist/cli-registry.js +2 -1
  42. package/dist/modules/registry.js +2 -2
  43. package/package.json +1 -1
@@ -0,0 +1,145 @@
1
+ import { execFile } from "node:child_process";
2
+ import { lstat } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { promisify } from "node:util";
5
+ import { nonInteractiveGitEnv } from "../utils-helpers.js";
6
+ import { countGit } from "./metrics.js";
7
+ /**
8
+ * A worker whose turn ended is not always done. OpenCode in particular ends a
9
+ * turn on a line like "The worktree lacks node_modules. Let me install
10
+ * dependencies." with nothing run after it (hook-permission-mode, 2026-09-30:
11
+ * returned done, its pane closed, 9 files of uncommitted work left behind).
12
+ * Dispatch returns report such a turn as needs-you, never done, so its pane
13
+ * is not closed and the dispatcher nudges it on.
14
+ */
15
+ const exec = promisify(execFile);
16
+ /** A closing sentence that announces work the turn never did: "Let me…",
17
+ * "I'll…", "I need to…", or "Now" with a working verb. Anything else, and
18
+ * whatever follows the subject in HANDOVER or a sentence waiting on something
19
+ * (CONDITIONAL), reads as finished: a done worker that says "I'll wait for
20
+ * your review" must not read as needs-you. "Let me know…" is a sign-off. */
21
+ const NEXT_STEP = /^(?:(?:ok(?:ay)?|alright|now|next|then|first|great|good)\b[,.!:]?\s+)*(?:let me(?! know)|let's|let us|i'll|i will|i'm going to|i am going to|i need to|i'm now|i am now|now i)\s+(.*)$/i;
22
+ const NOW_DOING = /^now,?\s+(?:adding|applying|building|checking|cleaning|committing|compiling|creating|debugging|editing|fixing|generating|implementing|installing|investigating|launching|looking|moving|opening|patching|pushing|reading|rebuilding|refactoring|removing|rerunning|re-running|running|searching|setting|starting|testing|trying|updating|verifying|wiring|working|writing)\b/i;
23
+ /** What a finished worker says it does next: wait, leave it to the owner, stop, be around. */
24
+ const HANDOVER = /^(?:just\s+)?(?:wait|await|leave|stop|hold|pause|be|hand|defer|stand|end|wrap|let you|keep an eye|check back|merge|ship|land|release|discuss|review|done|finished|ready)\b/i;
25
+ const CONDITIONAL = /\b(?:once|when|whenever|after|as soon as|if|until|unless)\b/i;
26
+ /** The last prose line of a reply: blank lines and fenced code are skipped, so
27
+ * "Let me run:" before a closing code block is still the closing sentence. */
28
+ function lastProseLine(reply) {
29
+ let fenced = false, last;
30
+ for (const raw of reply.replace(/\r/g, "").split("\n")) {
31
+ const line = raw.trim();
32
+ if (/^(?:```|~~~)/.test(line)) {
33
+ fenced = !fenced;
34
+ continue;
35
+ }
36
+ if (!fenced && line)
37
+ last = line;
38
+ }
39
+ return last;
40
+ }
41
+ /** The reply's closing sentence when it announces a next step ("Let me…",
42
+ * "I'll…", "Now running…"), else undefined. */
43
+ export function announcedNextStep(reply) {
44
+ const line = reply ? lastProseLine(reply) : undefined;
45
+ if (!line)
46
+ return undefined;
47
+ const plain = line.replace(/^(?:[-*+>]|\d+[.)])\s+/, "").replace(/[*_`#]+/g, "").replace(/[\u2018\u2019]/g, "'").trim();
48
+ const last = plain.split(/(?<=[.!?\u2026])\s+/).filter(Boolean).at(-1)?.trim();
49
+ if (!last || CONDITIONAL.test(last))
50
+ return undefined;
51
+ const step = NEXT_STEP.exec(last);
52
+ const announced = step ? !HANDOVER.test(step[1]) : NOW_DOING.test(last);
53
+ return announced ? last.slice(0, 160) : undefined;
54
+ }
55
+ /** A checkout git could not read in time (or at all): its work is unknown,
56
+ * so a turn there is never closed on the strength of it. */
57
+ export const UNKNOWN = "unknown";
58
+ /** Tracked files with uncommitted changes (staged or not) in the checkout at
59
+ * `directory`. Untracked files are left out: a main checkout often holds the
60
+ * owner's own (worktree folders, scratch files) and they are not the worker's.
61
+ * Undefined when it is not a repository; UNKNOWN when git does not answer in
62
+ * time or fails otherwise. */
63
+ export async function uncommittedFiles(directory, timeoutMs = 3000) {
64
+ if (!path.isAbsolute(directory))
65
+ return undefined;
66
+ countGit("worker-unfinished");
67
+ try {
68
+ const { stdout } = await exec("git", ["-C", directory, "--no-pager", "-c", "core.fsmonitor=false", "status", "--porcelain=v1", "--untracked-files=no", "-z"], {
69
+ timeout: timeoutMs, maxBuffer: 1_048_576, env: nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0", GIT_CONFIG_NOSYSTEM: "1" }),
70
+ });
71
+ // -z: renames carry their source as a second entry with no status.
72
+ return stdout.split("\0").filter(entry => /^[ MADRCTU?!]{2} /.test(entry)).length;
73
+ }
74
+ catch (error) {
75
+ const failed = error;
76
+ // Exit 128 naming no repository (or no such folder) is a plain answer, not a slow one.
77
+ if (failed.code === 128 && !failed.killed && /not a git repository|cannot change to/i.test(String(failed.stderr ?? "")))
78
+ return undefined;
79
+ return UNKNOWN;
80
+ }
81
+ }
82
+ const recent = new Map();
83
+ /** `uncommittedFiles` for the returns loop, which asks about a finished
84
+ * worker on every poll (15 s) for as long as its receipt is watched: a read
85
+ * under a minute old answers. An UNKNOWN read is not kept, so the next poll
86
+ * (and the close recheck) asks git again. */
87
+ export function recentUncommitted(directory, now = Date.now(), read = uncommittedFiles) {
88
+ const hit = recent.get(directory);
89
+ if (hit && now - hit.at < 60_000)
90
+ return hit.files;
91
+ const files = read(directory).catch(() => UNKNOWN);
92
+ recent.delete(directory);
93
+ recent.set(directory, { at: now, files });
94
+ while (recent.size > 256)
95
+ recent.delete(recent.keys().next().value);
96
+ void files.then(value => { if (value === UNKNOWN && recent.get(directory)?.files === files)
97
+ recent.delete(directory); });
98
+ return files;
99
+ }
100
+ /** The checkout `directory` is in: the nearest folder holding `.git` (a
101
+ * linked worktree's is a file), or undefined outside any. */
102
+ export async function checkoutRoot(directory) {
103
+ if (!path.isAbsolute(directory))
104
+ return undefined;
105
+ let folder = path.resolve(directory);
106
+ for (let depth = 0; depth < 64; depth++) {
107
+ if (await lstat(path.join(folder, ".git")).then(() => true, () => false))
108
+ return folder;
109
+ const parent = path.dirname(folder);
110
+ if (parent === folder)
111
+ return undefined;
112
+ folder = parent;
113
+ }
114
+ return undefined;
115
+ }
116
+ /** Whether any of `folders` (other panes' working folders) is in the same
117
+ * checkout as `directory`. Uncommitted files there may be the owner's or a
118
+ * sibling worker's, so they say nothing about this worker. */
119
+ export async function sharedCheckout(directory, folders) {
120
+ const root = await checkoutRoot(directory);
121
+ if (!root)
122
+ return false;
123
+ const others = [...new Set(folders.filter((folder) => typeof folder === "string" && path.isAbsolute(folder)))].slice(0, 64);
124
+ for (const folder of others)
125
+ if (folder.startsWith(root) && await checkoutRoot(folder) === root)
126
+ return true;
127
+ return false;
128
+ }
129
+ /** A pull request the reply itself names: a GitHub PR link or "PR #12". */
130
+ const PR_NAMED = /\/pull\/\d+|\bPR\s*#\d+/i;
131
+ /** Why a turn that ended is not done: it announced a next step, or it left
132
+ * uncommitted work with no PR reported or named in the reply (`unfinished`). `unchecked` when its
133
+ * checkout could not be read: the turn may be done, but it is not safe to
134
+ * close. Empty when it reads as done. */
135
+ export async function unfinishedTurn(input, uncommitted) {
136
+ const step = announcedNextStep(input.reply);
137
+ if (step)
138
+ return { unfinished: `Stopped mid-task: ${step}` };
139
+ if (input.prs?.length || PR_NAMED.test(input.reply ?? "") || !input.directory || !uncommitted)
140
+ return {};
141
+ const files = await uncommitted(input.directory).catch(() => UNKNOWN);
142
+ if (files === UNKNOWN)
143
+ return { unchecked: true };
144
+ return files ? { unfinished: `Stopped with ${files} uncommitted file${files === 1 ? "" : "s"} and no PR.` } : {};
145
+ }