@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
package/dist/bridge/health.js
CHANGED
|
@@ -9,7 +9,7 @@ import { resolveAllStores } from "../store-registry.js";
|
|
|
9
9
|
import { publicComputerKey } from "./computers.js";
|
|
10
10
|
import { hookPeers, peerRequest } from "./peers.js";
|
|
11
11
|
import { BridgeError, bridgeRoot, errorCode } from "./protocol.js";
|
|
12
|
-
import { canonicalComputer, readScheduleDocument, readScheduleRuns, scheduleRunsFile } from "./schedules.js";
|
|
12
|
+
import { canonicalComputer, latestScheduleBatch, readScheduleDocument, readScheduleRuns, scheduleRunsFile } from "./schedules.js";
|
|
13
13
|
import { countGit } from "./metrics.js";
|
|
14
14
|
import { tmuxHealth } from "./terminal-tmux.js";
|
|
15
15
|
const exec = promisify(execFile);
|
|
@@ -134,14 +134,18 @@ export async function storeSync(store) {
|
|
|
134
134
|
/** The newest run in schedule-runs.jsonl, with its schedule's name when the project still has it. */
|
|
135
135
|
export async function lastScheduledRun(store, runsFile = scheduleRunsFile()) {
|
|
136
136
|
const runs = await readScheduleRuns(runsFile).catch(() => []);
|
|
137
|
-
const
|
|
138
|
-
if (!
|
|
137
|
+
const newest = runs.reduce((newest, run) => !newest || run.startedAt >= newest.startedAt ? run : newest, undefined);
|
|
138
|
+
if (!newest)
|
|
139
139
|
return null;
|
|
140
|
+
const owner = newest.scheduleProject ?? newest.project;
|
|
141
|
+
const matching = runs.filter(run => (run.scheduleProject ?? run.project) === owner && run.scheduleId === newest.scheduleId
|
|
142
|
+
&& (newest.batchId ? run.batchId === newest.batchId : run.id === newest.id));
|
|
143
|
+
const last = latestScheduleBatch(matching).lastRun;
|
|
140
144
|
let name;
|
|
141
|
-
const directory = getProjectDirs(store).find(dir => path.basename(dir).toLowerCase() ===
|
|
145
|
+
const directory = getProjectDirs(store).find(dir => path.basename(dir).toLowerCase() === owner.toLowerCase());
|
|
142
146
|
if (directory)
|
|
143
147
|
name = (await readScheduleDocument(directory).catch(() => undefined))?.schedules.find(item => item.id === last.scheduleId)?.name;
|
|
144
|
-
return { ...(name ? { name } : {}), project:
|
|
148
|
+
return { ...(name ? { name } : {}), project: owner, status: last.status, ...(last.reason ? { reason: plainError(last.reason) } : {}),
|
|
145
149
|
startedAt: last.startedAt, ...(last.finishedAt ? { finishedAt: last.finishedAt } : {}) };
|
|
146
150
|
}
|
|
147
151
|
/** This computer's public SSH host key, the pin a peer's hooks.yaml holds for it. */
|
package/dist/bridge/herdr.js
CHANGED
|
@@ -670,5 +670,10 @@ export async function trustedDirectory(pane) {
|
|
|
670
670
|
const cwd = pane.foreground_cwd || pane.cwd;
|
|
671
671
|
if (typeof cwd !== "string" || !path.isAbsolute(cwd))
|
|
672
672
|
throw new BridgeError(409, "This pane has no project folder.");
|
|
673
|
-
|
|
673
|
+
try {
|
|
674
|
+
return await realpath(cwd);
|
|
675
|
+
}
|
|
676
|
+
catch {
|
|
677
|
+
throw new BridgeError(409, "This pane's project folder is no longer available. Reopen the chat from its current checkout.");
|
|
678
|
+
}
|
|
674
679
|
}
|
package/dist/bridge/hook-main.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { runBridge } from "./command.js";
|
|
2
1
|
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
3
2
|
Object.assign(process.env, nonInteractiveGitEnv());
|
|
4
|
-
|
|
3
|
+
async function main() {
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
// sudo starts a fresh process for every password attempt. It needs only
|
|
6
|
+
// the askpass client, rather than initializing the Hook server and tools.
|
|
7
|
+
if (args[0] === "askpass") {
|
|
8
|
+
const { askpass } = await import("./sudo.js");
|
|
9
|
+
return askpass(args[1]);
|
|
10
|
+
}
|
|
11
|
+
const { runBridge } = await import("./command.js");
|
|
12
|
+
return runBridge(args, PHREN_HOOK_VERSION);
|
|
13
|
+
}
|
|
14
|
+
main().then(code => {
|
|
5
15
|
process.exitCode = code;
|
|
6
16
|
}).catch(error => {
|
|
7
17
|
console.error(error instanceof Error ? error.message : "Phren Hook failed.");
|
package/dist/bridge/locate.js
CHANGED
|
@@ -7,9 +7,18 @@ import { herdrRoot } from "./herdr.js";
|
|
|
7
7
|
import { phrenStoreRoot } from "./transcripts.js";
|
|
8
8
|
const PROJECT_NAME = /^[a-z0-9][a-z0-9-]*$/;
|
|
9
9
|
const SEARCH_ROOTS = ["", "Sites", "Projects", "projects", "Code", "code", "dev", "src", "repos", "workspace"];
|
|
10
|
+
function realHome() {
|
|
11
|
+
try {
|
|
12
|
+
return realpathSync.native(homedir());
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return path.resolve(homedir());
|
|
16
|
+
}
|
|
17
|
+
}
|
|
10
18
|
export async function locateProject(project, activity, env = process.env) {
|
|
11
19
|
if (!PROJECT_NAME.test(project) || project.length > 100)
|
|
12
20
|
throw new BridgeError(400, "Invalid project name.");
|
|
21
|
+
const home = realHome();
|
|
13
22
|
const found = [];
|
|
14
23
|
const seen = new Set();
|
|
15
24
|
const offer = async (directory, source, lastSeen) => {
|
|
@@ -24,6 +33,11 @@ export async function locateProject(project, activity, env = process.env) {
|
|
|
24
33
|
catch {
|
|
25
34
|
return; /* not on this computer */
|
|
26
35
|
}
|
|
36
|
+
// A folder that contains the home folder holds every project, so it is
|
|
37
|
+
// never one: a project named after the user ("sam") must not be offered
|
|
38
|
+
// /home/sam because every journal path runs through it.
|
|
39
|
+
if (dir === path.parse(dir).root || home === dir || home.startsWith(dir + path.sep))
|
|
40
|
+
return;
|
|
27
41
|
if (seen.has(dir))
|
|
28
42
|
return;
|
|
29
43
|
seen.add(dir);
|
|
@@ -67,7 +81,6 @@ export async function locateProject(project, activity, env = process.env) {
|
|
|
67
81
|
}
|
|
68
82
|
catch { /* unregistered */ }
|
|
69
83
|
// 4. The roots phren's locator searches.
|
|
70
|
-
const home = homedir();
|
|
71
84
|
for (const root of [...(env.PROJECTS_DIR ? [env.PROJECTS_DIR] : []), ...SEARCH_ROOTS.map(r => path.join(home, r))]) {
|
|
72
85
|
await offer(path.join(root, project), "search");
|
|
73
86
|
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { type Json } from "./protocol.js";
|
|
2
|
+
/** Nested reads share one deadline. Abort and reap outstanding Git processes on
|
|
3
|
+
* expiry or failure; never leave a timed-out request running its command chain. */
|
|
4
|
+
export declare function withGitReadDeadline<T>(read: () => Promise<T>): Promise<T>;
|
|
5
|
+
/** Bind Git to the selected checkout, even when Hook inherited another Git
|
|
6
|
+
* process's repository/index environment. Keep user config (including excludes). */
|
|
7
|
+
export declare function checkoutGitEnv(): NodeJS.ProcessEnv;
|
|
2
8
|
export declare function git(cwd: string, ...args: string[]): Promise<string>;
|
|
3
9
|
export declare function gitRoot(dir: string): Promise<string | undefined>;
|
|
10
|
+
/** An unborn HEAD is normal; process/configuration failures are not. */
|
|
11
|
+
export declare function gitHead(root: string): Promise<string | undefined>;
|
|
4
12
|
/** The pane's working tree, plus anything the command named: files in the
|
|
5
13
|
* same repository that a hook already committed, and files in other
|
|
6
14
|
* repositories — the phren store, a sibling checkout — grouped by root. */
|
package/dist/bridge/projects.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
1
2
|
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
2
3
|
import { execFile } from "node:child_process";
|
|
3
4
|
import { promisify } from "node:util";
|
|
@@ -11,18 +12,107 @@ import { locateProject } from "./locate.js";
|
|
|
11
12
|
import { BridgeError } from "./protocol.js";
|
|
12
13
|
import { countGit } from "./metrics.js";
|
|
13
14
|
const exec = promisify(execFile);
|
|
15
|
+
const gitReadBudget = new AsyncLocalStorage();
|
|
16
|
+
const GIT_READ_MS = 10_000;
|
|
17
|
+
const gitTimeout = () => new BridgeError(504, "Git did not finish within the request time limit. Retry when the repository is idle.", { code: "git-timeout" });
|
|
18
|
+
function remainingGitTime(budget) {
|
|
19
|
+
const remaining = budget.deadline - performance.now();
|
|
20
|
+
if (budget.controller.signal.aborted || remaining <= 0)
|
|
21
|
+
throw gitTimeout();
|
|
22
|
+
return Math.max(1, Math.ceil(remaining));
|
|
23
|
+
}
|
|
24
|
+
/** Nested reads share one deadline. Abort and reap outstanding Git processes on
|
|
25
|
+
* expiry or failure; never leave a timed-out request running its command chain. */
|
|
26
|
+
export async function withGitReadDeadline(read) {
|
|
27
|
+
const inherited = gitReadBudget.getStore();
|
|
28
|
+
if (inherited) {
|
|
29
|
+
remainingGitTime(inherited);
|
|
30
|
+
return read();
|
|
31
|
+
}
|
|
32
|
+
const budget = { deadline: performance.now() + GIT_READ_MS, controller: new AbortController(), pending: new Set() };
|
|
33
|
+
const timer = setTimeout(() => budget.controller.abort(), GIT_READ_MS);
|
|
34
|
+
try {
|
|
35
|
+
return await gitReadBudget.run(budget, async () => {
|
|
36
|
+
const result = await read();
|
|
37
|
+
remainingGitTime(budget);
|
|
38
|
+
return result;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
budget.controller.abort();
|
|
44
|
+
await Promise.allSettled([...budget.pending]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Bind Git to the selected checkout, even when Hook inherited another Git
|
|
48
|
+
* process's repository/index environment. Keep user config (including excludes). */
|
|
49
|
+
export function checkoutGitEnv() {
|
|
50
|
+
const env = nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0", LC_ALL: "C" });
|
|
51
|
+
for (const key of ["GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_PREFIX"])
|
|
52
|
+
delete env[key];
|
|
53
|
+
return env;
|
|
54
|
+
}
|
|
14
55
|
export async function git(cwd, ...args) {
|
|
56
|
+
const budget = gitReadBudget.getStore();
|
|
57
|
+
const timeout = budget ? remainingGitTime(budget) : 10_000;
|
|
15
58
|
countGit("projects");
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
59
|
+
let pending;
|
|
60
|
+
try {
|
|
61
|
+
pending = exec("git", ["-C", cwd, "--no-pager", ...args], {
|
|
62
|
+
timeout, maxBuffer: 4_194_304, env: checkoutGitEnv(),
|
|
63
|
+
...(budget ? { signal: budget.controller.signal, killSignal: "SIGKILL" } : {}),
|
|
64
|
+
});
|
|
65
|
+
budget?.pending.add(pending);
|
|
66
|
+
const result = await pending;
|
|
67
|
+
if (budget)
|
|
68
|
+
remainingGitTime(budget);
|
|
69
|
+
return result.stdout;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
const failure = error;
|
|
73
|
+
if (budget && (budget.controller.signal.aborted || performance.now() >= budget.deadline))
|
|
74
|
+
throw gitTimeout();
|
|
75
|
+
const stderr = String(failure.stderr ?? "");
|
|
76
|
+
// No-index uses exit 1 for differences, even with conversion warnings.
|
|
77
|
+
// Keep read errors and unknown diagnostics fatal, including partial output.
|
|
78
|
+
const diagnostics = stderr.split(/\r?\n/).filter(line => line.trim() && !line.startsWith("warning:"));
|
|
79
|
+
const options = args.slice(0, args.indexOf("--") < 0 ? args.length : args.indexOf("--"));
|
|
80
|
+
if (args[0] === "diff" && options.includes("--no-index") && String(failure.code) === "1"
|
|
81
|
+
&& diagnostics.length === 0 && !failure.killed && !failure.signal && failure.errno === undefined
|
|
82
|
+
&& failure.syscall === undefined && failure.name !== "AbortError")
|
|
83
|
+
return failure.stdout ?? "";
|
|
84
|
+
const [status, code, message] = failure.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER"
|
|
85
|
+
? [413, "git-output-limit", "Git output exceeded the 4 MiB limit. Narrow the selection."]
|
|
86
|
+
: failure.killed || failure.signal ? [504, "git-timeout", "Git did not finish within its time limit. Retry when the repository is idle."]
|
|
87
|
+
: /index\.lock/.test(stderr) ? [409, "git-index-locked", "Git could not lock the index. Wait for the other Git operation to finish; the Hook has not removed the lock."]
|
|
88
|
+
: /not a git repository/.test(stderr) ? [409, "git-not-repository", "This folder is not a Git repository."]
|
|
89
|
+
: [503, "git-failed", `Git ${args[0]} failed: ${stderr.trim().slice(0, 1000) || "the command could not complete"}`];
|
|
90
|
+
throw new BridgeError(status, message, { code, ...(failure.code !== undefined ? { exitCode: String(failure.code) } : {}) });
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
if (pending)
|
|
94
|
+
budget?.pending.delete(pending);
|
|
95
|
+
}
|
|
19
96
|
}
|
|
20
97
|
export async function gitRoot(dir) {
|
|
21
98
|
try {
|
|
22
|
-
return await realpath((await git(dir, "rev-parse", "--show-toplevel")).
|
|
99
|
+
return await realpath((await git(dir, "rev-parse", "--show-toplevel")).replace(/\r?\n$/, ""));
|
|
23
100
|
}
|
|
24
|
-
catch {
|
|
25
|
-
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (error instanceof BridgeError && error.details?.code === "git-not-repository")
|
|
103
|
+
return undefined;
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** An unborn HEAD is normal; process/configuration failures are not. */
|
|
108
|
+
export async function gitHead(root) {
|
|
109
|
+
try {
|
|
110
|
+
return (await git(root, "rev-parse", "--verify", "--quiet", "HEAD")).trim();
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof BridgeError && error.details?.exitCode === "1")
|
|
114
|
+
return undefined;
|
|
115
|
+
throw error;
|
|
26
116
|
}
|
|
27
117
|
}
|
|
28
118
|
/** `git status` as the app lists it: one record per file, a staged and an
|
|
@@ -30,30 +120,46 @@ export async function gitRoot(dir) {
|
|
|
30
120
|
async function statusFiles(root, pathspecs = []) {
|
|
31
121
|
const status = (await git(root, "status", "--porcelain=v1", "-z", "--untracked-files=normal", "--", ...pathspecs.map(spec => `:(literal)${spec}`))).split("\0");
|
|
32
122
|
const files = [];
|
|
33
|
-
|
|
123
|
+
let totalFiles = 0, truncated = false, remaining = 1_000_000;
|
|
124
|
+
for (let index = 0; index < status.length; index++) {
|
|
34
125
|
const record = status[index];
|
|
35
126
|
if (!record)
|
|
36
127
|
continue;
|
|
37
128
|
const code = record.slice(0, 2), file = record.slice(3);
|
|
38
|
-
|
|
39
|
-
|
|
129
|
+
const original = /[RC]/.test(code) ? status[++index] : undefined;
|
|
130
|
+
totalFiles++;
|
|
131
|
+
if (files.length >= 500) {
|
|
132
|
+
truncated = true;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
40
135
|
const sections = [];
|
|
41
|
-
for (const [kind, staged] of [["staged", true], ["unstaged", false]]) {
|
|
42
|
-
if (code === "??")
|
|
136
|
+
for (const [kind, staged, column] of [["staged", true, 0], ["unstaged", false, 1]]) {
|
|
137
|
+
if (code === "??" || code[column] === " ")
|
|
43
138
|
continue;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
139
|
+
if (remaining <= 0) {
|
|
140
|
+
sections.push({ id: `${kind}:${file}`, kind, loadState: "loaded", patch: "", truncated: true });
|
|
141
|
+
truncated = true;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const patch = await git(root, "diff", "--no-ext-diff", "--no-textconv", "--no-color", ...(staged ? ["--cached"] : []), "--", ...[file, ...(original ? [original] : [])].map(name => `:(literal)${name}`));
|
|
145
|
+
if (patch) {
|
|
146
|
+
const limit = Math.min(200_000, remaining), clipped = patch.length > limit;
|
|
147
|
+
sections.push({ id: `${kind}:${file}`, kind, binary: /^(?:Binary files |GIT binary patch)/m.test(patch), loadState: "loaded", patch: patch.slice(0, limit), truncated: clipped });
|
|
148
|
+
remaining -= Math.min(patch.length, limit);
|
|
149
|
+
truncated ||= clipped;
|
|
150
|
+
}
|
|
47
151
|
}
|
|
48
|
-
files.push({ path: file, status: code, sections });
|
|
152
|
+
files.push({ path: file, status: code, ...(file.endsWith("/") ? { directory: true, countsComplete: false } : {}), sections });
|
|
49
153
|
}
|
|
50
|
-
return files;
|
|
154
|
+
return { files, totalFiles, truncated };
|
|
51
155
|
}
|
|
52
156
|
/** The last commit that touched `pathspec` in the past half hour — what a
|
|
53
157
|
* command changed when a hook (phren's own Stop hook, say) committed it before
|
|
54
158
|
* anyone looked. */
|
|
55
159
|
async function committed(root, pathspec) {
|
|
56
|
-
|
|
160
|
+
if (!await gitHead(root))
|
|
161
|
+
return undefined;
|
|
162
|
+
const log = await git(root, "log", "-1", "--since=30.minutes", "--format=%h%x1f%s%x1f%cr", "-p", "--no-ext-diff", "--no-textconv", "--no-color", "--", `:(literal)${pathspec}`);
|
|
57
163
|
const newline = log.indexOf("\n");
|
|
58
164
|
if (newline < 0)
|
|
59
165
|
return undefined;
|
|
@@ -61,7 +167,7 @@ async function committed(root, pathspec) {
|
|
|
61
167
|
const patch = log.slice(newline + 1).replace(/^\n+/, "");
|
|
62
168
|
if (!patch)
|
|
63
169
|
return undefined;
|
|
64
|
-
return { id: `committed:${pathspec}`, kind: "committed", binary: patch.includes("Binary files"), loadState: "loaded", patch, note: `${hash} · ${subject.slice(0, 120)} · ${when}` };
|
|
170
|
+
return { id: `committed:${pathspec}`, kind: "committed", binary: patch.includes("Binary files"), loadState: "loaded", patch: patch.slice(0, 200_000), truncated: patch.length > 200_000, note: `${hash} · ${subject.slice(0, 120)} · ${when}` };
|
|
65
171
|
}
|
|
66
172
|
/** A path a command named, made absolute and real — `~/` expanded, relative
|
|
67
173
|
* ones taken from the pane. The caller checks conversation scope after
|
|
@@ -86,10 +192,13 @@ async function resolveTouched(raw, cwd) {
|
|
|
86
192
|
/** The pane's working tree, plus anything the command named: files in the
|
|
87
193
|
* same repository that a hook already committed, and files in other
|
|
88
194
|
* repositories — the phren store, a sibling checkout — grouped by root. */
|
|
89
|
-
export
|
|
195
|
+
export function repositoryDiff(cwd, touched = [], allowedPaths = []) {
|
|
196
|
+
return withGitReadDeadline(() => readRepositoryDiff(cwd, touched, allowedPaths));
|
|
197
|
+
}
|
|
198
|
+
async function readRepositoryDiff(cwd, touched, allowedPaths) {
|
|
90
199
|
const root = await gitRoot(cwd);
|
|
91
200
|
if (!root)
|
|
92
|
-
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
201
|
+
throw new BridgeError(409, "This pane is not in a Git repository.", { code: "git-not-repository" });
|
|
93
202
|
const allowed = (await Promise.all([root, phrenStoreRoot(), ...allowedPaths].map(raw => resolveTouched(raw, cwd)))).filter((p) => !!p);
|
|
94
203
|
const requested = [];
|
|
95
204
|
for (const raw of touched.slice(0, 24)) {
|
|
@@ -99,7 +208,7 @@ export async function repositoryDiff(cwd, touched = [], allowedPaths = []) {
|
|
|
99
208
|
requested.push(file);
|
|
100
209
|
}
|
|
101
210
|
const branch = (await git(root, "branch", "--show-current")).trim();
|
|
102
|
-
const
|
|
211
|
+
const listing = await statusFiles(root);
|
|
103
212
|
const byRoot = new Map();
|
|
104
213
|
for (const file of requested) {
|
|
105
214
|
const owner = await gitRoot((await stat(file).catch(() => undefined))?.isDirectory() ? file : path.dirname(file));
|
|
@@ -118,22 +227,31 @@ export async function repositoryDiff(cwd, touched = [], allowedPaths = []) {
|
|
|
118
227
|
for (const [owner, specs] of byRoot) {
|
|
119
228
|
if (related.length >= 8)
|
|
120
229
|
break;
|
|
121
|
-
const
|
|
230
|
+
const ownerListing = owner === root ? listing : await statusFiles(owner, specs);
|
|
231
|
+
const listed = ownerListing.files;
|
|
122
232
|
const seen = new Set(listed.map(file => file.path));
|
|
123
233
|
for (const spec of specs) {
|
|
124
234
|
// Uncommitted changes under the path are already listed; otherwise show the commit.
|
|
125
235
|
if ([...seen].some(file => file === spec || spec === "." || file.startsWith(spec + "/")))
|
|
126
236
|
continue;
|
|
237
|
+
if (ownerListing.truncated)
|
|
238
|
+
continue; // An omitted dirty row must not be replaced with a historical commit.
|
|
127
239
|
const section = await committed(owner, spec);
|
|
128
240
|
if (section) {
|
|
241
|
+
ownerListing.totalFiles++;
|
|
242
|
+
if (listed.length >= 500) {
|
|
243
|
+
ownerListing.truncated = true;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
129
246
|
listed.push({ path: spec, status: " ", sections: [section] });
|
|
130
247
|
seen.add(spec);
|
|
248
|
+
ownerListing.truncated ||= section.truncated === true;
|
|
131
249
|
}
|
|
132
250
|
}
|
|
133
251
|
if (owner !== root && listed.length)
|
|
134
|
-
related.push({ root: owner, branch: (await git(owner, "branch", "--show-current")).trim(),
|
|
252
|
+
related.push({ root: owner, branch: (await git(owner, "branch", "--show-current")).trim(), ...ownerListing });
|
|
135
253
|
}
|
|
136
|
-
return { branch, root, launchPath: cwd,
|
|
254
|
+
return { branch, root, repository: root, observedAt: new Date().toISOString(), launchPath: cwd, ...listing, ...(related.length ? { related } : {}) };
|
|
137
255
|
}
|
|
138
256
|
/** The HEAD file of the repository holding `cwd` (`.git/HEAD`, or a linked
|
|
139
257
|
* worktree's through its `.git` file), found without spawning git. */
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { type Json, type Provider, type Target } from "./protocol.js";
|
|
2
|
+
export type DeliveryOutcome = "delivered" | "pending";
|
|
3
|
+
/** What a phone message is known to have become. `queued`: typed into the
|
|
4
|
+
* pane and held by the agent until its turn ends. `failed` carries a reason
|
|
5
|
+
* and may still turn `delivered` if the agent takes it late. */
|
|
6
|
+
export type DeliveryState = "queued" | "delivered" | "failed";
|
|
7
|
+
export type TrackedState = DeliveryState | "unknown";
|
|
8
|
+
export interface DeliveryStatus {
|
|
9
|
+
state: TrackedState;
|
|
10
|
+
reason?: string;
|
|
11
|
+
session?: string;
|
|
12
|
+
}
|
|
13
|
+
/** A message is kept this long, by its id, so a late status or a retry still
|
|
14
|
+
* finds it: a busy Codex turn can run far past the old ten minutes. */
|
|
15
|
+
export declare const DELIVERY_RETENTION_MS: number;
|
|
16
|
+
/** An agent that ended its turn after the message was typed and then stayed
|
|
17
|
+
* idle this long without taking it has dropped it. */
|
|
18
|
+
export declare const DELIVERY_GIVE_UP_MS = 30000;
|
|
19
|
+
/** What the agent hands its UserPromptSubmit hook is the terminal's pasted
|
|
20
|
+
* form of what Phren typed; compare the words, not the wrapping. Claude Code
|
|
21
|
+
* also takes each attached picture's path line out of the text and puts an
|
|
22
|
+
* "[Image #N]" label at the front, so neither side keeps picture paths or
|
|
23
|
+
* labels. */
|
|
24
|
+
export declare function promptKey(text: string): string;
|
|
25
|
+
export declare const promptHash: (text: string) => string;
|
|
26
|
+
/** One message per record, bound to the pane it was typed into (server, pane,
|
|
27
|
+
* agent kind), not to the conversation: a Codex thread or a Claude /clear can
|
|
28
|
+
* replace the conversation while the agent still holds the typed text, and
|
|
29
|
+
* the conversation that submits it is the one it reached. Records hold a hash
|
|
30
|
+
* of the words, never the text, and those with a phone id are written to
|
|
31
|
+
* `file` so a Hook restart keeps their state. */
|
|
32
|
+
export declare class PromptDeliveries {
|
|
33
|
+
private file?;
|
|
34
|
+
private now;
|
|
35
|
+
private landed;
|
|
36
|
+
private records;
|
|
37
|
+
private waiters;
|
|
38
|
+
private writing;
|
|
39
|
+
constructor(file?: string | undefined, now?: () => number, landed?: (record: {
|
|
40
|
+
source: Provider;
|
|
41
|
+
session: string;
|
|
42
|
+
hash: string;
|
|
43
|
+
at: number;
|
|
44
|
+
}) => Promise<boolean>);
|
|
45
|
+
private samePane;
|
|
46
|
+
private byId;
|
|
47
|
+
private prune;
|
|
48
|
+
private save;
|
|
49
|
+
/** Settled writes, for tests and an orderly shutdown. */
|
|
50
|
+
flush(): Promise<void>;
|
|
51
|
+
/** Register a message about to be typed into `target`'s pane. Settles
|
|
52
|
+
* "delivered" once a conversation in that pane submits the words, or
|
|
53
|
+
* "pending" after `waitMs`; the record outlives the wait. A retry of a
|
|
54
|
+
* failed id replaces its record. Aborting (the terminal refused before
|
|
55
|
+
* writing) drops it. */
|
|
56
|
+
expect(target: Target, text: string, waitMs: number, signal?: AbortSignal, id?: string, terminal?: string): Promise<DeliveryOutcome>;
|
|
57
|
+
private release;
|
|
58
|
+
private open;
|
|
59
|
+
/** A message Phren typed into `target`'s pane that no conversation there has submitted yet. */
|
|
60
|
+
pending(target: Target, text: string): boolean;
|
|
61
|
+
/** Wait again for a message `expect` reported pending, after a second Enter. */
|
|
62
|
+
awaitLate(target: Target, text: string, waitMs?: number): Promise<DeliveryOutcome>;
|
|
63
|
+
private find;
|
|
64
|
+
/** The Hook answered `id` as typed and held by the agent. */
|
|
65
|
+
queue(id: string, target: Target): void;
|
|
66
|
+
/** Delivered without a hook: an app-server turn or a served OpenCode prompt. */
|
|
67
|
+
delivered(id: string | undefined, target: Target, text: string): void;
|
|
68
|
+
/** The message did not reach the agent, and why. It stays matchable, so a
|
|
69
|
+
* late submission still turns it delivered. */
|
|
70
|
+
fail(id: string | undefined, target: Target, reason: string): void;
|
|
71
|
+
/** The pane's Codex server moved to another thread (/new, /resume):
|
|
72
|
+
* input still pending on the old one stays there, out of the pane's view.
|
|
73
|
+
* Fails loudly; a late UserPromptSubmit from the old thread still settles it. */
|
|
74
|
+
threadChanged(previous: Target): void;
|
|
75
|
+
/** False when `id` names another message: other words or another pane. */
|
|
76
|
+
sameMessage(id: string, target: Pick<Target, "server" | "pane" | "source">, text: string): boolean;
|
|
77
|
+
/** What became of phone message `id` in `target`'s pane. `unknown` while the
|
|
78
|
+
* Hook has not answered it, for another pane, or past retention. */
|
|
79
|
+
status(id: string, target: Pick<Target, "server" | "pane" | "source">): DeliveryStatus;
|
|
80
|
+
/** Every known phone message in `target`'s pane, for its conversation's stream. */
|
|
81
|
+
forPane(target: Pick<Target, "server" | "pane" | "source">): ({
|
|
82
|
+
deliveryId: string;
|
|
83
|
+
} & DeliveryStatus)[];
|
|
84
|
+
/** Conversation `target` just submitted `prompt`. Its own record wins, then
|
|
85
|
+
* the oldest open one in the same pane: the agent kept the text across a
|
|
86
|
+
* conversation change, and the conversation now in the pane is where it
|
|
87
|
+
* went. A prompt nothing matches was typed locally. Never refused. */
|
|
88
|
+
submitted(target: Target, prompt: string): void;
|
|
89
|
+
/** A turn in `target`'s pane ended: the agent now takes held input or never will. */
|
|
90
|
+
stopped(target: Target): void;
|
|
91
|
+
/** Called on the Hook's activity tick with a server's panes. A message
|
|
92
|
+
* still held fails loudly when its pane closed, its agent restarted, or the
|
|
93
|
+
* agent ended a turn and stayed idle without taking it (unless its
|
|
94
|
+
* transcript shows it after all). */
|
|
95
|
+
observe(server: string, panes: Json[]): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
/** A transcript row's user-typed text: Claude's user rows, Codex's
|
|
98
|
+
* user_message events and user response items. */
|
|
99
|
+
export declare function userTexts(row: Json): string[];
|