@phren/cli 0.2.14 → 0.2.16
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 +2 -2
- package/dist/agent-instructions.d.ts +12 -0
- package/dist/agent-instructions.js +52 -0
- package/dist/bridge/agent-hook-stores.d.ts +32 -0
- package/dist/bridge/agent-hook-stores.js +67 -0
- package/dist/bridge/agent-hooks.d.ts +193 -2
- package/dist/bridge/agent-hooks.js +1045 -38
- package/dist/bridge/canary.d.ts +27 -0
- package/dist/bridge/canary.js +189 -0
- package/dist/bridge/changes.d.ts +57 -0
- package/dist/bridge/changes.js +403 -0
- package/dist/bridge/client.d.ts +3 -0
- package/dist/bridge/client.js +36 -0
- package/dist/bridge/code-note.d.ts +22 -0
- package/dist/bridge/code-note.js +42 -0
- package/dist/bridge/code-routes.d.ts +133 -0
- package/dist/bridge/code-routes.js +319 -0
- package/dist/bridge/codex-threads.d.ts +27 -0
- package/dist/bridge/codex-threads.js +442 -0
- package/dist/bridge/command.d.ts +7 -0
- package/dist/bridge/command.js +50 -5
- package/dist/bridge/computer-names.d.ts +4 -0
- package/dist/bridge/computer-names.js +16 -0
- package/dist/bridge/computers.d.ts +9 -0
- package/dist/bridge/computers.js +111 -0
- package/dist/bridge/context.d.ts +14 -0
- package/dist/bridge/context.js +149 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +93 -0
- package/dist/bridge/dispatch-hosts.d.ts +23 -0
- package/dist/bridge/dispatch-hosts.js +25 -0
- package/dist/bridge/dispatch-returns.d.ts +123 -0
- package/dist/bridge/dispatch-returns.js +279 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +207 -0
- package/dist/bridge/dispatch.js +261 -0
- package/dist/bridge/enroll.d.ts +31 -0
- package/dist/bridge/enroll.js +236 -0
- package/dist/bridge/fanout-messages.d.ts +74 -0
- package/dist/bridge/fanout-messages.js +230 -0
- package/dist/bridge/fanouts.d.ts +160 -0
- package/dist/bridge/fanouts.js +702 -0
- package/dist/bridge/file-range.d.ts +16 -0
- package/dist/bridge/file-range.js +90 -0
- package/dist/bridge/files.d.ts +20 -0
- package/dist/bridge/files.js +54 -0
- package/dist/bridge/git-publish.d.ts +23 -0
- package/dist/bridge/git-publish.js +145 -0
- package/dist/bridge/git-worktrees.d.ts +21 -0
- package/dist/bridge/git-worktrees.js +150 -0
- package/dist/bridge/git.d.ts +54 -0
- package/dist/bridge/git.js +552 -0
- package/dist/bridge/grants.d.ts +31 -0
- package/dist/bridge/grants.js +181 -0
- package/dist/bridge/hand-off.d.ts +79 -0
- package/dist/bridge/hand-off.js +151 -0
- package/dist/bridge/health.d.ts +128 -0
- package/dist/bridge/health.js +260 -0
- package/dist/bridge/herdr.d.ts +79 -6
- package/dist/bridge/herdr.js +292 -27
- package/dist/bridge/hook-main.js +2 -0
- package/dist/bridge/install.d.ts +32 -1
- package/dist/bridge/install.js +225 -38
- package/dist/bridge/launch-worktree.d.ts +21 -0
- package/dist/bridge/launch-worktree.js +70 -0
- package/dist/bridge/limits.d.ts +20 -0
- package/dist/bridge/limits.js +65 -0
- package/dist/bridge/locate.d.ts +16 -0
- package/dist/bridge/locate.js +75 -0
- package/dist/bridge/metrics.d.ts +29 -0
- package/dist/bridge/metrics.js +71 -0
- package/dist/bridge/model-switch.d.ts +35 -0
- package/dist/bridge/model-switch.js +250 -0
- package/dist/bridge/models.d.ts +46 -0
- package/dist/bridge/models.js +257 -0
- package/dist/bridge/opencode-approvals.d.ts +6 -0
- package/dist/bridge/opencode-approvals.js +60 -0
- package/dist/bridge/pane-text.d.ts +15 -0
- package/dist/bridge/pane-text.js +23 -0
- package/dist/bridge/peers.d.ts +27 -0
- package/dist/bridge/peers.js +130 -0
- package/dist/bridge/projects.d.ts +12 -1
- package/dist/bridge/projects.js +268 -20
- package/dist/bridge/protocol.d.ts +35 -2
- package/dist/bridge/protocol.js +42 -7
- package/dist/bridge/push.d.ts +83 -0
- package/dist/bridge/push.js +199 -0
- package/dist/bridge/questions.d.ts +38 -0
- package/dist/bridge/questions.js +205 -0
- package/dist/bridge/schedule-format.d.ts +103 -0
- package/dist/bridge/schedule-format.js +266 -0
- package/dist/bridge/schedule-launch.d.ts +11 -0
- package/dist/bridge/schedule-launch.js +169 -0
- package/dist/bridge/schedule-watch.d.ts +52 -0
- package/dist/bridge/schedule-watch.js +242 -0
- package/dist/bridge/schedules.d.ts +48 -0
- package/dist/bridge/schedules.js +205 -0
- package/dist/bridge/server-launch.d.ts +14 -0
- package/dist/bridge/server-launch.js +218 -0
- package/dist/bridge/server-overview.d.ts +45 -0
- package/dist/bridge/server-overview.js +102 -0
- package/dist/bridge/server-pane-routes.d.ts +29 -0
- package/dist/bridge/server-pane-routes.js +400 -0
- package/dist/bridge/server-routes.d.ts +106 -0
- package/dist/bridge/server-routes.js +538 -0
- package/dist/bridge/server-stream.d.ts +46 -0
- package/dist/bridge/server-stream.js +253 -0
- package/dist/bridge/server.d.ts +3 -15
- package/dist/bridge/server.js +162 -264
- package/dist/bridge/session-servers.d.ts +24 -0
- package/dist/bridge/session-servers.js +136 -0
- package/dist/bridge/side-questions.d.ts +74 -0
- package/dist/bridge/side-questions.js +252 -0
- package/dist/bridge/simulators.d.ts +28 -0
- package/dist/bridge/simulators.js +198 -0
- package/dist/bridge/speech-transcribe.d.ts +24 -0
- package/dist/bridge/speech-transcribe.js +124 -0
- package/dist/bridge/speech.d.ts +35 -0
- package/dist/bridge/speech.js +98 -0
- package/dist/bridge/steps.d.ts +13 -0
- package/dist/bridge/steps.js +222 -0
- package/dist/bridge/tab-activity.d.ts +18 -0
- package/dist/bridge/tab-activity.js +99 -0
- package/dist/bridge/terminal-choice.d.ts +64 -0
- package/dist/bridge/terminal-choice.js +281 -0
- package/dist/bridge/transcript-claude.d.ts +22 -0
- package/dist/bridge/transcript-claude.js +353 -0
- package/dist/bridge/transcript-codex.d.ts +30 -0
- package/dist/bridge/transcript-codex.js +282 -0
- package/dist/bridge/transcript-copilot.d.ts +4 -0
- package/dist/bridge/transcript-copilot.js +14 -0
- package/dist/bridge/transcript-opencode.d.ts +5 -0
- package/dist/bridge/transcript-opencode.js +26 -0
- package/dist/bridge/transcript-preview.d.ts +94 -0
- package/dist/bridge/transcript-preview.js +476 -0
- package/dist/bridge/transcripts.d.ts +81 -4
- package/dist/bridge/transcripts.js +332 -67
- package/dist/bridge/transport.d.ts +3 -0
- package/dist/bridge/transport.js +96 -15
- package/dist/bridge/uploads.d.ts +15 -0
- package/dist/bridge/uploads.js +44 -2
- package/dist/bridge/usage.d.ts +124 -0
- package/dist/bridge/usage.js +859 -0
- package/dist/bridge-hook.mjs +41246 -21385
- package/dist/cli/actions.js +14 -1
- package/dist/cli/config-proactivity.d.ts +3 -1
- package/dist/cli/config-proactivity.js +35 -3
- package/dist/cli/config.js +4 -1
- package/dist/cli/extract.js +3 -6
- package/dist/cli/govern.js +5 -2
- package/dist/cli/hooks-globs.js +1 -1
- package/dist/cli/hooks.d.ts +1 -1
- package/dist/cli/hooks.js +9 -7
- package/dist/cli/namespaces-projects.js +12 -6
- package/dist/cli/namespaces-store.js +37 -6
- package/dist/cli/namespaces-tasks.js +1 -1
- package/dist/cli/schedules.d.ts +1 -0
- package/dist/cli/schedules.js +164 -0
- package/dist/cli/session-background.js +1 -1
- package/dist/cli/session-git.d.ts +8 -5
- package/dist/cli/session-git.js +45 -120
- package/dist/cli/session-start.js +3 -1
- package/dist/cli/session-stop.js +19 -8
- package/dist/cli/team.js +13 -10
- package/dist/cli-handlers.d.ts +1 -1
- package/dist/cli-handlers.js +8 -4
- package/dist/cli-help.d.ts +5 -4
- package/dist/cli-help.js +15 -15
- package/dist/cli-registry.d.ts +4 -1
- package/dist/cli-registry.js +119 -6
- package/dist/content/archive.js +2 -2
- package/dist/content/citation.d.ts +11 -0
- package/dist/content/citation.js +18 -0
- package/dist/content/learning.js +2 -0
- package/dist/content/summarize.d.ts +14 -15
- package/dist/content/summarize.js +60 -5
- package/dist/content/validate.d.ts +3 -1
- package/dist/content/validate.js +26 -8
- package/dist/core/project-registry.d.ts +26 -0
- package/dist/core/project-registry.js +573 -0
- package/dist/core/project.js +2 -3
- package/dist/data/access.d.ts +7 -1
- package/dist/data/access.js +13 -3
- package/dist/data/tasks.d.ts +18 -3
- package/dist/data/tasks.js +8 -7
- package/dist/entrypoint.js +44 -10
- package/dist/fanout/adapters/claude.d.ts +2 -0
- package/dist/fanout/adapters/claude.js +7 -0
- package/dist/fanout/adapters/codex.d.ts +2 -0
- package/dist/fanout/adapters/codex.js +9 -0
- package/dist/fanout/adapters/opencode.d.ts +4 -0
- package/dist/fanout/adapters/opencode.js +9 -0
- package/dist/fanout/adapters/types.d.ts +21 -0
- package/dist/fanout/adapters/types.js +3 -0
- package/dist/fanout/command.d.ts +2 -0
- package/dist/fanout/command.js +78 -0
- package/dist/fanout/launcher.d.ts +27 -0
- package/dist/fanout/launcher.js +156 -0
- package/dist/fanout/opencode-serve.d.ts +24 -0
- package/dist/fanout/opencode-serve.js +191 -0
- package/dist/fanout/picker.d.ts +91 -0
- package/dist/fanout/picker.js +51 -0
- package/dist/fanout/usage.d.ts +5 -0
- package/dist/fanout/usage.js +51 -0
- package/dist/fanout/watchdog.d.ts +10 -0
- package/dist/fanout/watchdog.js +27 -0
- package/dist/filenames.d.ts +9 -0
- package/dist/filenames.js +11 -0
- package/dist/finding/context.js +4 -22
- package/dist/generated/memory-ui-graph.browser.js +279 -283
- package/dist/governance/policy-constants.d.ts +11 -0
- package/dist/governance/policy-constants.js +9 -0
- package/dist/governance/policy.d.ts +5 -8
- package/dist/governance/policy.js +7 -5
- package/dist/graph-core/camera.d.ts +24 -0
- package/dist/graph-core/camera.js +18 -0
- package/dist/graph-core/index.d.ts +2 -0
- package/dist/graph-core/index.js +2 -0
- package/dist/graph-core/labels.d.ts +77 -0
- package/dist/graph-core/labels.js +124 -0
- package/dist/graph-core/model.d.ts +13 -0
- package/dist/graph-core/model.js +33 -0
- package/dist/graph-core/types.d.ts +2 -0
- package/dist/home-paths.d.ts +7 -0
- package/dist/home-paths.js +21 -0
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.js +13 -0
- package/dist/index.js +24 -2
- package/dist/init/guard-globals.d.ts +1 -1
- package/dist/init/guard-globals.js +4 -4
- package/dist/init/init-configure.js +3 -3
- package/dist/init/init-uninstall.js +2 -2
- package/dist/init/init-walkthrough.d.ts +2 -0
- package/dist/init/init-walkthrough.js +17 -6
- package/dist/init/init.js +26 -6
- package/dist/init/management-preset.d.ts +1 -1
- package/dist/init/management-preset.js +2 -2
- package/dist/init/preferences.js +1 -1
- package/dist/init/self-wiring.js +1 -1
- package/dist/init/setup.d.ts +3 -26
- package/dist/init/setup.js +49 -588
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -1
- package/dist/init/store-gitignore.js +2 -0
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +100 -54
- package/dist/link/link.js +29 -61
- package/dist/link/refresh.js +2 -2
- package/dist/link/skills.js +26 -5
- package/dist/machine-identity.d.ts +3 -0
- package/dist/machine-identity.js +6 -0
- package/dist/mcp/profile.d.ts +2 -15
- package/dist/mcp/profile.js +64 -9
- package/dist/memory-ui-graph.runtime.js +279 -283
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +152 -0
- package/dist/modules/command.d.ts +2 -0
- package/dist/modules/command.js +67 -0
- package/dist/modules/config.d.ts +4 -0
- package/dist/modules/config.js +70 -0
- package/dist/modules/manifest.d.ts +41 -0
- package/dist/modules/manifest.js +1 -0
- package/dist/modules/provision.d.ts +3 -0
- package/dist/modules/provision.js +84 -0
- package/dist/modules/registry.d.ts +16 -0
- package/dist/modules/registry.js +250 -0
- package/dist/modules/runtime.d.ts +15 -0
- package/dist/modules/runtime.js +59 -0
- package/dist/package-metadata.js +10 -2
- package/dist/phren-dotenv.js +4 -2
- package/dist/phren-paths.d.ts +3 -2
- package/dist/phren-paths.js +4 -5
- package/dist/profile-store.js +4 -5
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.js +4 -4
- package/dist/provider-adapters.js +1 -4
- package/dist/query-correlation.js +6 -6
- package/dist/session/artifacts.js +5 -5
- package/dist/session/checkpoints.js +5 -0
- package/dist/session/utils.d.ts +2 -2
- package/dist/session/utils.js +6 -6
- package/dist/shared/content.d.ts +1 -1
- package/dist/shared/content.js +1 -1
- package/dist/shared/data-utils.js +1 -1
- package/dist/shared/fragment-graph.d.ts +1 -1
- package/dist/shared/fragment-graph.js +5 -5
- package/dist/shared/index.js +48 -7
- package/dist/shared/retrieval.d.ts +18 -1
- package/dist/shared/retrieval.js +100 -11
- package/dist/shared/search-fallback.js +4 -6
- package/dist/shared/sqljs.d.ts +2 -0
- package/dist/shared/sqljs.js +4 -2
- package/dist/shared.d.ts +0 -1
- package/dist/shared.js +0 -1
- package/dist/shell/input.d.ts +1 -5
- package/dist/shell/input.js +16 -10
- package/dist/shell/palette.d.ts +2 -2
- package/dist/shell/palette.js +3 -7
- package/dist/shell/render.d.ts +3 -1
- package/dist/shell/render.js +4 -4
- package/dist/shell/shell.js +4 -1
- package/dist/shell/types.d.ts +1 -0
- package/dist/shell/types.js +4 -0
- package/dist/shell/view.d.ts +0 -4
- package/dist/shell/view.js +19 -27
- package/dist/skill/registry.js +25 -17
- package/dist/skill/state.d.ts +3 -0
- package/dist/skill/state.js +13 -7
- package/dist/status.js +38 -17
- package/dist/store-registry.d.ts +44 -18
- package/dist/store-registry.js +169 -60
- package/dist/store-weight.d.ts +1 -6
- package/dist/store-weight.js +4 -2
- package/dist/sync/auth-doctor.d.ts +8 -0
- package/dist/sync/auth-doctor.js +27 -0
- package/dist/sync/auth.d.ts +27 -0
- package/dist/sync/auth.js +114 -0
- package/dist/sync/conflict-resolve.d.ts +9 -0
- package/dist/sync/conflict-resolve.js +146 -0
- package/dist/sync/git-state.d.ts +4 -0
- package/dist/sync/git-state.js +49 -0
- package/dist/sync/outcome.d.ts +34 -0
- package/dist/sync/outcome.js +57 -0
- package/dist/sync/pull.d.ts +15 -7
- package/dist/sync/pull.js +84 -35
- package/dist/sync/store-merge.d.ts +29 -0
- package/dist/sync/store-merge.js +137 -0
- package/dist/sync/task-merge.d.ts +22 -0
- package/dist/sync/task-merge.js +158 -0
- package/dist/task/github.js +1 -1
- package/dist/task/lifecycle.d.ts +5 -0
- package/dist/task/lifecycle.js +67 -23
- package/dist/terminal-text.d.ts +5 -0
- package/dist/terminal-text.js +11 -0
- package/dist/test-global-setup.js +7 -0
- package/dist/tool-registry.d.ts +1 -1
- package/dist/tool-registry.js +3 -1
- package/dist/tools/code.d.ts +3 -0
- package/dist/tools/code.js +178 -0
- package/dist/tools/config.js +5 -0
- package/dist/tools/data.js +14 -7
- package/dist/tools/dispatch.d.ts +2 -0
- package/dist/tools/dispatch.js +66 -0
- package/dist/tools/finding.js +43 -41
- package/dist/tools/ops.js +5 -2
- package/dist/tools/search.js +21 -8
- package/dist/tools/session.js +21 -22
- package/dist/tools/tasks.js +8 -2
- package/dist/tools/types.d.ts +2 -2
- package/dist/tools/types.js +2 -2
- package/dist/ui/assets.js +1 -1
- package/dist/ui/data.js +14 -15
- package/dist/ui/deep-void.d.ts +1 -1
- package/dist/ui/deep-void.js +0 -1
- package/dist/ui/graph-chrome.d.ts +1 -1
- package/dist/ui/graph-chrome.js +51 -0
- package/dist/ui/page.js +5 -5
- package/dist/ui/scripts.js +67 -30
- package/dist/ui/server.js +34 -6
- package/dist/update.js +1 -1
- package/dist/utils-fts.js +1 -0
- package/dist/utils-helpers.d.ts +7 -0
- package/dist/utils-helpers.js +17 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +45 -3
- package/plugins/opencode/phren-transcript.js +340 -0
- package/starter/README.md +4 -4
- package/starter/global/{CLAUDE.md → AGENTS.md} +2 -2
- package/starter/global/skills/conductor/SKILL.md +106 -0
- package/starter/global/skills/fanout/SKILL.md +20 -0
- package/starter/global/skills/phren-discover/SKILL.md +6 -6
- package/starter/global/skills/phren-init/SKILL.md +4 -4
- package/starter/global/skills/phren-sync/SKILL.md +3 -3
- package/starter/templates/frontend/reference/README.md +1 -1
- package/starter/templates/library/reference/README.md +1 -1
- package/starter/templates/monorepo/reference/README.md +1 -1
- package/starter/templates/python-project/reference/README.md +1 -1
- /package/starter/templates/frontend/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/library/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/monorepo/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/python-project/{CLAUDE.md → AGENTS.md} +0 -0
package/dist/bridge/herdr.js
CHANGED
|
@@ -1,21 +1,64 @@
|
|
|
1
|
+
import { createHmac, randomBytes } from "node:crypto";
|
|
1
2
|
import { execFile } from "node:child_process";
|
|
2
3
|
import { connect } from "node:net";
|
|
3
4
|
import { homedir } from "node:os";
|
|
4
5
|
import { readdir, readlink, realpath, stat } from "node:fs/promises";
|
|
5
6
|
import path from "node:path";
|
|
6
7
|
import { promisify } from "node:util";
|
|
7
|
-
import { BridgeError, id, object, objects, requestID, serverName } from "./protocol.js";
|
|
8
|
-
import {
|
|
8
|
+
import { BridgeError, id, object, objects, requestID, serverName, provider, sessionId } from "./protocol.js";
|
|
9
|
+
import { logger } from "../logger.js";
|
|
10
|
+
import { recordedSession } from "./agent-hook-stores.js";
|
|
11
|
+
import { tabActivityKey } from "./tab-activity.js";
|
|
12
|
+
import { intervalFromEnv } from "./limits.js";
|
|
13
|
+
import { countHerdr, countIdentity } from "./metrics.js";
|
|
9
14
|
const exec = promisify(execFile);
|
|
10
15
|
export function herdrRoot() { return process.env.PHREN_HERDR_HOME || path.join(homedir(), ".config/herdr"); }
|
|
11
|
-
|
|
16
|
+
function herdrSocket(server) {
|
|
12
17
|
serverName.parse(server);
|
|
13
18
|
return path.join(herdrRoot(), ...(server === "default" ? [] : ["sessions", server]), "herdr.sock");
|
|
14
19
|
}
|
|
20
|
+
/** The Herdr pane this process runs in, from the variables Herdr sets for
|
|
21
|
+
* every pane: the server its socket belongs to plus the pane's ids. */
|
|
22
|
+
export function herdrPaneFromEnv(env = process.env) {
|
|
23
|
+
if (env.HERDR_ENV !== "1" || !env.HERDR_SOCKET_PATH)
|
|
24
|
+
return undefined;
|
|
25
|
+
const socket = path.resolve(env.HERDR_SOCKET_PATH), root = path.resolve(herdrRoot());
|
|
26
|
+
const server = socket === path.join(root, "herdr.sock") ? "default"
|
|
27
|
+
: socket.startsWith(path.join(root, "sessions") + path.sep) ? path.basename(path.dirname(socket)) : undefined;
|
|
28
|
+
const { HERDR_WORKSPACE_ID: workspace, HERDR_TAB_ID: tab, HERDR_PANE_ID: pane } = env;
|
|
29
|
+
if (!server || !workspace || !tab || !pane)
|
|
30
|
+
return undefined;
|
|
31
|
+
return { server, workspace, tab, pane };
|
|
32
|
+
}
|
|
33
|
+
/** A socket failure keeps its errno so "not running", "stale socket" and "permissions" stay distinct. */
|
|
34
|
+
export function herdrSocketError(error) {
|
|
35
|
+
const code = error.code;
|
|
36
|
+
const reason = code === "ENOENT" ? "Herdr is not running"
|
|
37
|
+
: code === "ECONNREFUSED" ? "stale socket, Herdr is not listening"
|
|
38
|
+
: code === "EACCES" || code === "EPERM" ? "this user may not open Herdr's socket"
|
|
39
|
+
: undefined;
|
|
40
|
+
const tag = code && /^[A-Z0-9_]{1,32}$/.test(code) ? code : "unknown error";
|
|
41
|
+
return new BridgeError(503, `Herdr is not reachable on this computer (${tag}${reason ? `: ${reason}` : ""}).`);
|
|
42
|
+
}
|
|
43
|
+
const optionalReadFailures = new Map();
|
|
44
|
+
/**
|
|
45
|
+
* For pane reads whose callers treat text as optional: the caller still gets
|
|
46
|
+
* nothing, but the reason is logged once per target (again only if it changes).
|
|
47
|
+
*/
|
|
48
|
+
export function noteOptionalReadFailure(what, key, error) {
|
|
49
|
+
const reason = (error instanceof Error ? error.message : String(error)).split("\n")[0].slice(0, 200);
|
|
50
|
+
if (optionalReadFailures.get(key) === reason)
|
|
51
|
+
return;
|
|
52
|
+
if (optionalReadFailures.size >= 256)
|
|
53
|
+
optionalReadFailures.clear();
|
|
54
|
+
optionalReadFailures.set(key, reason);
|
|
55
|
+
logger.warn("herdr", `${what} for ${key} failed: ${reason}`);
|
|
56
|
+
}
|
|
15
57
|
/** Herdr's documented newline JSON socket API. No shell, UI focus, or inherited caller context. */
|
|
16
|
-
export async function rpc(server, method, params = {}, signal) {
|
|
58
|
+
export async function rpc(server, method, params = {}, signal, timeoutMs = 10_000) {
|
|
17
59
|
const socket = herdrSocket(server);
|
|
18
|
-
|
|
60
|
+
countHerdr(method);
|
|
61
|
+
const metadata = await stat(socket).catch(error => { throw herdrSocketError(error); });
|
|
19
62
|
if (!metadata.isSocket() || (process.getuid && metadata.uid !== process.getuid()))
|
|
20
63
|
throw new BridgeError(503, "The Herdr socket is unavailable.");
|
|
21
64
|
return new Promise((resolve, reject) => {
|
|
@@ -36,8 +79,8 @@ export async function rpc(server, method, params = {}, signal) {
|
|
|
36
79
|
return;
|
|
37
80
|
}
|
|
38
81
|
signal?.addEventListener("abort", abort, { once: true });
|
|
39
|
-
client.setTimeout(
|
|
40
|
-
client.on("error",
|
|
82
|
+
client.setTimeout(timeoutMs, () => finish(new BridgeError(504, "Herdr did not answer. Refresh before trying again.")));
|
|
83
|
+
client.on("error", error => finish(herdrSocketError(error)));
|
|
41
84
|
client.on("end", () => finish(new BridgeError(503, "Herdr closed the request before confirming it.")));
|
|
42
85
|
client.on("connect", () => client.write(JSON.stringify({ id: key, method, params }) + "\n"));
|
|
43
86
|
client.on("data", bytes => {
|
|
@@ -53,8 +96,12 @@ export async function rpc(server, method, params = {}, signal) {
|
|
|
53
96
|
const value = object(JSON.parse(pending.subarray(0, end).toString()));
|
|
54
97
|
if (value.id !== key)
|
|
55
98
|
throw new Error("Mismatched response");
|
|
56
|
-
if (value.error)
|
|
57
|
-
|
|
99
|
+
if (value.error) {
|
|
100
|
+
const code = typeof object(value.error).code === "string" ? String(object(value.error).code).slice(0, 64) : undefined;
|
|
101
|
+
// Herdr's own words say what went wrong; keep them, bounded and plain.
|
|
102
|
+
const said = String(object(value.error).message ?? "").replace(/[\x00-\x1f\x7f]/g, " ").trim().slice(0, 200);
|
|
103
|
+
throw new BridgeError(409, said ? `Herdr: ${said}` : "Herdr could not perform this action. Refresh the session before trying again.", code ? { herdrCode: code } : undefined);
|
|
104
|
+
}
|
|
58
105
|
finish(undefined, object(value.result));
|
|
59
106
|
}
|
|
60
107
|
catch (error) {
|
|
@@ -63,6 +110,23 @@ export async function rpc(server, method, params = {}, signal) {
|
|
|
63
110
|
});
|
|
64
111
|
});
|
|
65
112
|
}
|
|
113
|
+
/** How long the activity timer reuses the list of running Herdr servers
|
|
114
|
+
* before pinging every server directory again. */
|
|
115
|
+
export const SERVER_LIST_REUSE_MS = intervalFromEnv("PHREN_SERVER_LIST_REUSE_MS", 30_000, 0, 600_000);
|
|
116
|
+
let serverList;
|
|
117
|
+
/** The names the last `servers()` found running. */
|
|
118
|
+
let knownServers;
|
|
119
|
+
/** The running servers, reusing a list at most `maxAgeMs` old. For
|
|
120
|
+
* background work only: a route that answers the phone calls `servers()`. */
|
|
121
|
+
export async function recentServers(maxAgeMs = SERVER_LIST_REUSE_MS) {
|
|
122
|
+
if (serverList && Date.now() - serverList.at < maxAgeMs)
|
|
123
|
+
return serverList.value;
|
|
124
|
+
const value = servers();
|
|
125
|
+
serverList = { at: Date.now(), value };
|
|
126
|
+
value.catch(() => { if (serverList?.value === value)
|
|
127
|
+
serverList = undefined; });
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
66
130
|
export async function servers() {
|
|
67
131
|
const names = ["default", ...(await readdir(path.join(herdrRoot(), "sessions")).catch(() => [])).filter(n => serverName.safeParse(n).success)].slice(0, 64);
|
|
68
132
|
const results = await Promise.all(names.map(async (name) => {
|
|
@@ -74,10 +138,92 @@ export async function servers() {
|
|
|
74
138
|
return null;
|
|
75
139
|
}
|
|
76
140
|
}));
|
|
77
|
-
|
|
141
|
+
const running = results.filter((v) => v !== null);
|
|
142
|
+
knownServers = running.map(server => server.session);
|
|
143
|
+
return running;
|
|
144
|
+
}
|
|
145
|
+
/** How old a shared `session.snapshot` may be for readers that poll: open
|
|
146
|
+
* chat and status streams and the activity timer. The overview and every
|
|
147
|
+
* action take a fresh one, which the pollers then reuse. */
|
|
148
|
+
export const SNAPSHOT_SHARE_MS = intervalFromEnv("PHREN_SNAPSHOT_SHARE_MS", 2_500, 0, 10_000);
|
|
149
|
+
const sharedSnapshots = new Map();
|
|
150
|
+
const inFlightSnapshots = new Map();
|
|
151
|
+
/** A fresh snapshot. Its answer also becomes the shared one, so pollers reuse it. */
|
|
152
|
+
export async function snapshot(server) {
|
|
153
|
+
const started = Date.now();
|
|
154
|
+
const value = object((await rpc(server, "session.snapshot")).snapshot);
|
|
155
|
+
const current = sharedSnapshots.get(server);
|
|
156
|
+
// A slower request that started before the current answer never replaces it.
|
|
157
|
+
if (!current || current.at <= started) {
|
|
158
|
+
if (!current && sharedSnapshots.size >= 64)
|
|
159
|
+
sharedSnapshots.delete(sharedSnapshots.keys().next().value);
|
|
160
|
+
sharedSnapshots.set(server, { at: Date.now(), value });
|
|
161
|
+
}
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* One `session.snapshot` per server shared by every poller: an answer less
|
|
166
|
+
* than `maxAgeMs` old is reused and concurrent callers join the request in
|
|
167
|
+
* flight, so N open chats cost one snapshot per window, not one each, and
|
|
168
|
+
* reuse the overview's when it is recent enough. A pane that disappears or changes identity shows in the next
|
|
169
|
+
* snapshot, at most `maxAgeMs` after the change. Failures are never kept.
|
|
170
|
+
* Anything about to act on a pane (a send, a key, a launch) calls `snapshot`.
|
|
171
|
+
*/
|
|
172
|
+
export async function sharedSnapshot(server, maxAgeMs = SNAPSHOT_SHARE_MS) {
|
|
173
|
+
const cached = sharedSnapshots.get(server);
|
|
174
|
+
if (cached && Date.now() - cached.at < maxAgeMs)
|
|
175
|
+
return cached.value;
|
|
176
|
+
const pending = inFlightSnapshots.get(server);
|
|
177
|
+
if (pending)
|
|
178
|
+
return pending;
|
|
179
|
+
const value = snapshot(server).finally(() => { if (inFlightSnapshots.get(server) === value)
|
|
180
|
+
inFlightSnapshots.delete(server); });
|
|
181
|
+
inFlightSnapshots.set(server, value);
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Every pane on the running servers, from snapshots already held and no older
|
|
186
|
+
* than `maxAgeMs`, without asking Herdr; undefined unless the server list is
|
|
187
|
+
* known and every listed server has such a snapshot.
|
|
188
|
+
*/
|
|
189
|
+
export function knownPanes(maxAgeMs) {
|
|
190
|
+
if (!knownServers)
|
|
191
|
+
return undefined;
|
|
192
|
+
const panes = [];
|
|
193
|
+
for (const name of knownServers) {
|
|
194
|
+
const held = sharedSnapshots.get(name);
|
|
195
|
+
if (!held || Date.now() - held.at >= maxAgeMs)
|
|
196
|
+
return undefined;
|
|
197
|
+
panes.push(...objects(held.value.panes));
|
|
198
|
+
}
|
|
199
|
+
return panes;
|
|
200
|
+
}
|
|
201
|
+
/** For tests: forget every shared snapshot and the server list. */
|
|
202
|
+
export function resetSharedHerdrState() { sharedSnapshots.clear(); inFlightSnapshots.clear(); serverList = undefined; knownServers = undefined; }
|
|
203
|
+
/** The agent's Herdr name. Newer Herdr keeps it in `agents[].name` rather
|
|
204
|
+
* than on the pane (`agent_name`); read either. */
|
|
205
|
+
export function paneAgentName(s, pane) {
|
|
206
|
+
if (!pane)
|
|
207
|
+
return undefined;
|
|
208
|
+
if (typeof pane.agent_name === "string")
|
|
209
|
+
return pane.agent_name;
|
|
210
|
+
const named = objects(s.agents).find(agent => agent.pane_id === pane.pane_id);
|
|
211
|
+
return typeof named?.name === "string" ? named.name : undefined;
|
|
212
|
+
}
|
|
213
|
+
/** Every agent name in use on a server, in either Herdr shape. */
|
|
214
|
+
export function agentNames(s) {
|
|
215
|
+
return new Set([...objects(s.panes), ...objects(s.agents)].map(item => String(item.agent_name ?? item.name ?? "")));
|
|
216
|
+
}
|
|
217
|
+
/** The snapshot's pane at `address`; with a `source`, only while that agent runs there. */
|
|
218
|
+
export function findPane(s, address) {
|
|
219
|
+
return objects(s.panes).find(p => p.pane_id === address.pane && p.tab_id === address.tab && p.workspace_id === address.workspace
|
|
220
|
+
&& (address.source === undefined || p.agent === address.source));
|
|
221
|
+
}
|
|
222
|
+
/** A conductor's Herdr name: "conductor", or "conductor-" plus its label. */
|
|
223
|
+
export function isConductorName(name) {
|
|
224
|
+
return name === "conductor" || !!name?.startsWith("conductor-");
|
|
78
225
|
}
|
|
79
|
-
export
|
|
80
|
-
export function workspaceSnapshot(s) {
|
|
226
|
+
export function workspaceSnapshot(s, contextUsedPercent, approvalPanes, lastChanged) {
|
|
81
227
|
const focusedPane = objects(s.panes).find(p => p.pane_id === s.focused_pane_id
|
|
82
228
|
&& p.tab_id === s.focused_tab_id && p.workspace_id === s.focused_workspace_id);
|
|
83
229
|
const focus = focusedPane && id.safeParse(s.focused_workspace_id).success
|
|
@@ -89,43 +235,114 @@ export function workspaceSnapshot(s) {
|
|
|
89
235
|
// validated independently through panes(), never inferred from a directory.
|
|
90
236
|
return { kind: "herdr", focus, groups: objects(s.workspaces).map(w => ({ id: w.workspace_id, label: w.label,
|
|
91
237
|
children: objects(s.tabs).filter(t => t.workspace_id === w.workspace_id).map(t => {
|
|
92
|
-
const panes = objects(s.panes).filter(p => p.tab_id === t.tab_id);
|
|
238
|
+
const panes = objects(s.panes).filter(p => p.tab_id === t.tab_id && p.workspace_id === t.workspace_id);
|
|
93
239
|
const agent = panes.find(p => p.agent);
|
|
240
|
+
// Herdr's per-pane state_change_seq climbs with every agent status
|
|
241
|
+
// change; the phone orders "just finished" ahead of "finished an hour ago" by it.
|
|
242
|
+
const changed = Math.max(0, ...panes.map(p => Number.isSafeInteger(p.state_change_seq) ? Number(p.state_change_seq) : 0));
|
|
94
243
|
return { id: t.tab_id, label: t.label, title: agent?.title || agent?.terminal_title_stripped,
|
|
95
244
|
agent: agent?.agent, agentStatus: t.agent_status, cwd: agent?.foreground_cwd || agent?.cwd,
|
|
245
|
+
role: isConductorName(paneAgentName(s, agent)) ? "conductor" : undefined,
|
|
246
|
+
changedSeq: changed || undefined,
|
|
247
|
+
lastChangedAt: lastChanged?.get(tabActivityKey(t.workspace_id, t.tab_id)),
|
|
248
|
+
approvalPending: panes.some(p => approvalPanes?.has(String(p.pane_id))) || undefined,
|
|
249
|
+
contextUsedPercent: agent && panes.filter(p => p.agent).length === 1 ? contextUsedPercent?.get(agent) : undefined,
|
|
96
250
|
agentPaneCount: panes.filter(p => p.agent).length, paneCount: panes.length };
|
|
97
251
|
}) })) };
|
|
98
252
|
}
|
|
99
253
|
/** Only file descriptors held by this pane's foreground processes establish identity.
|
|
100
254
|
* A directory match, latest log, or focused tab must never select a conversation. */
|
|
101
|
-
async function
|
|
255
|
+
async function foregroundPids(server, pane) {
|
|
102
256
|
const info = object((await rpc(server, "pane.process_info", { pane_id: pane.pane_id })).process_info);
|
|
103
257
|
const pids = objects(info.foreground_processes).map(p => p.pid).filter((p) => Number.isSafeInteger(p) && Number(p) > 0).slice(0, 16);
|
|
258
|
+
return pids.sort((a, b) => a - b);
|
|
259
|
+
}
|
|
260
|
+
async function processLogs(pids) {
|
|
104
261
|
const paths = await Promise.all(pids.map(async (pid) => {
|
|
105
262
|
if (process.platform === "linux") {
|
|
263
|
+
countIdentity("proc");
|
|
106
264
|
const base = `/proc/${pid}/fd`;
|
|
107
265
|
return Promise.all((await readdir(base).catch(() => [])).slice(0, 4096).map(fd => readlink(path.join(base, fd)).catch(() => "")));
|
|
108
266
|
}
|
|
267
|
+
countIdentity("lsof");
|
|
109
268
|
const result = await exec("/usr/sbin/lsof", ["-a", "-p", String(pid), "-Fn"], { timeout: 3000, maxBuffer: 1_048_576 }).catch(() => ({ stdout: "" }));
|
|
110
269
|
return result.stdout.split("\n").filter(n => n.startsWith("n/")).map(n => n.slice(1));
|
|
111
270
|
}));
|
|
112
|
-
return
|
|
271
|
+
return [...new Set(paths.flat())].filter(p => p.endsWith(".jsonl") || p.endsWith(".lock"));
|
|
113
272
|
}
|
|
114
|
-
|
|
273
|
+
/** How long a pane's process-based identity probe is reused. */
|
|
274
|
+
const IDENTITY_CACHE_MS = intervalFromEnv("PHREN_IDENTITY_CACHE_MS", 2_000);
|
|
275
|
+
const identities = new Map();
|
|
276
|
+
const identityKey = (server, pane, pids) => JSON.stringify([server, pane.pane_id, pane.terminal_id, pids, pane.agent]);
|
|
277
|
+
/** The pane's identity and, when it had to look, the foreground PIDs it read,
|
|
278
|
+
* so a caller that also needs them does not ask Herdr a second time. */
|
|
279
|
+
async function resolveIdentity(server, pane, fresh) {
|
|
115
280
|
const reported = object(pane.agent_session);
|
|
116
|
-
if (reported.kind === "id" && reported.agent === pane.agent && typeof reported.value === "string" &&
|
|
117
|
-
|
|
118
|
-
|
|
281
|
+
if (reported.kind === "id" && reported.agent === pane.agent && typeof reported.value === "string" && sessionId.safeParse(reported.value).success) {
|
|
282
|
+
countIdentity("reported");
|
|
283
|
+
return { sessionId: reported.value };
|
|
284
|
+
}
|
|
285
|
+
const pids = await foregroundPids(server, pane);
|
|
286
|
+
const key = identityKey(server, pane, pids);
|
|
287
|
+
const cached = identities.get(key);
|
|
288
|
+
if (!fresh && cached && Date.now() - cached.at < IDENTITY_CACHE_MS) {
|
|
289
|
+
countIdentity("cached");
|
|
290
|
+
return { sessionId: (await cached.result).sessionId, pids };
|
|
291
|
+
}
|
|
292
|
+
countIdentity(fresh ? "probe-fresh" : "probe");
|
|
293
|
+
const result = identityFromProcesses(server, pane, pids);
|
|
294
|
+
if (identities.size >= 128)
|
|
295
|
+
identities.delete(identities.keys().next().value);
|
|
296
|
+
identities.set(key, { at: Date.now(), result });
|
|
297
|
+
return { sessionId: (await result).sessionId, pids };
|
|
298
|
+
}
|
|
299
|
+
export async function paneIdentity(server, pane, fresh = false) {
|
|
300
|
+
return (await resolveIdentity(server, pane, fresh)).sessionId;
|
|
301
|
+
}
|
|
302
|
+
async function identityFromProcesses(server, pane, pids) {
|
|
303
|
+
const files = await processLogs(pids);
|
|
119
304
|
const candidates = files.flatMap(file => {
|
|
120
|
-
const match = pane.agent === "codex" ? /rollout-.*-([a-f0-9-]{36})\.jsonl$/i.exec(file)
|
|
305
|
+
const match = pane.agent === "codex" ? (/rollout-.*-([a-f0-9-]{36})\.jsonl$/i.exec(file) ?? /thread-writer-locks\/([a-f0-9-]{36})\.lock$/i.exec(file))
|
|
121
306
|
: pane.agent === "claude" ? /\/([a-f0-9-]{36})\.jsonl$/i.exec(file)
|
|
122
|
-
|
|
307
|
+
// phren-agent appends and closes per event, so its log is rarely open;
|
|
308
|
+
// the lifecycle binding below is the usual path for it.
|
|
309
|
+
: pane.agent === "phren" ? /\/session-([a-f0-9-]{36})\.events\.jsonl$/i.exec(file)
|
|
310
|
+
: /\/session-state\/([a-f0-9-]{36})\/events\.jsonl$/i.exec(file);
|
|
123
311
|
return match ? [match[1]] : [];
|
|
124
312
|
});
|
|
125
313
|
if (new Set(candidates).size === 1)
|
|
126
|
-
return candidates[0];
|
|
314
|
+
return { sessionId: candidates[0], noTranscriptLogs: false };
|
|
127
315
|
// A lifecycle callback is bound to the process and terminal, never just cwd.
|
|
128
|
-
|
|
316
|
+
// Codex can hold its parent and subagent transcripts in the same process.
|
|
317
|
+
// Use the verified binding to disambiguate only if its log is still open;
|
|
318
|
+
// a stale binding must not override evidence of other conversations.
|
|
319
|
+
const recorded = await recordedSession(server, pane, pids);
|
|
320
|
+
return { sessionId: candidates.length === 0 || (recorded && candidates.includes(recorded)) ? recorded : undefined, noTranscriptLogs: candidates.length === 0 };
|
|
321
|
+
}
|
|
322
|
+
const startingKey = randomBytes(32);
|
|
323
|
+
/** Bind first-send permission to the actual terminal/process, never a cwd or
|
|
324
|
+
* a guessed conversation. Tokens expire naturally when the Hook/process restarts. */
|
|
325
|
+
export async function paneChatState(server, pane, options = {}) {
|
|
326
|
+
if (!provider.safeParse(pane.agent).success)
|
|
327
|
+
return {};
|
|
328
|
+
const identity = await resolveIdentity(server, pane, false);
|
|
329
|
+
const sessionId = identity.sessionId;
|
|
330
|
+
// A pane whose conversation is known is not starting, so a caller that
|
|
331
|
+
// reads only `sessionId` and `starting` (the overview) needs no PIDs.
|
|
332
|
+
if (sessionId && options.tokenWhenIdentified === false)
|
|
333
|
+
return { sessionId };
|
|
334
|
+
const pids = identity.pids ?? await foregroundPids(server, pane);
|
|
335
|
+
// The token binds to the agent's own process, the oldest in the pane's
|
|
336
|
+
// foreground group. Helpers it spawns while starting up (Codex forks
|
|
337
|
+
// several in its first seconds) must not turn the phone's first send away.
|
|
338
|
+
const startingToken = typeof pane.terminal_id === "string" && pids.length
|
|
339
|
+
? createHmac("sha256", startingKey).update(JSON.stringify([server, pane.workspace_id, pane.tab_id, pane.pane_id, pane.terminal_id, pane.agent, pids[0]])).digest("hex") : undefined;
|
|
340
|
+
// An ambiguous set of open logs is not a brand-new conversation.
|
|
341
|
+
// Reuse the same two-second identity probe as context/overview polling;
|
|
342
|
+
// discovering a new chat must not run lsof again for every list refresh.
|
|
343
|
+
const evidence = identities.get(identityKey(server, pane, pids));
|
|
344
|
+
const starting = !sessionId && !!startingToken && !!evidence && Date.now() - evidence.at < IDENTITY_CACHE_MS && (await evidence.result).noTranscriptLogs;
|
|
345
|
+
return { sessionId, ...(startingToken ? { startingToken } : {}), ...(starting ? { starting: true } : {}) };
|
|
129
346
|
}
|
|
130
347
|
export async function panes(server, workspace, tab) {
|
|
131
348
|
id.parse(workspace);
|
|
@@ -137,12 +354,60 @@ export async function panes(server, workspace, tab) {
|
|
|
137
354
|
.filter(p => p.workspace_id === workspace && p.tab_id === tab).map(async (p) => ({ id: p.pane_id,
|
|
138
355
|
label: p.label || p.pane_id, agent: p.agent, agentStatus: p.agent_status,
|
|
139
356
|
title: p.title || p.terminal_title_stripped, cwd: p.foreground_cwd || p.cwd,
|
|
140
|
-
|
|
357
|
+
...await paneChatState(server, p) }))) };
|
|
141
358
|
}
|
|
142
|
-
|
|
359
|
+
/** The pane a starting target names, if its terminal/process binding still
|
|
360
|
+
* holds. Status is not checked here: a starting agent's first prompt (folder
|
|
361
|
+
* trust, a login) is exactly what the phone answers with a key. */
|
|
362
|
+
export async function startingPane(target) {
|
|
143
363
|
const s = await snapshot(target.server);
|
|
144
|
-
const pane =
|
|
145
|
-
if (!pane
|
|
364
|
+
const pane = findPane(s, target);
|
|
365
|
+
if (!pane)
|
|
366
|
+
throw new BridgeError(409, "This agent pane changed. Reopen the chat.");
|
|
367
|
+
const state = await paneChatState(target.server, pane);
|
|
368
|
+
if (state.sessionId || state.startingToken !== target.startingToken)
|
|
369
|
+
throw new BridgeError(409, "This starting agent changed. Reopen the chat.");
|
|
370
|
+
return pane;
|
|
371
|
+
}
|
|
372
|
+
export async function validateStartingTarget(target) {
|
|
373
|
+
const s = await snapshot(target.server);
|
|
374
|
+
const pane = findPane(s, target);
|
|
375
|
+
if (!pane)
|
|
376
|
+
throw new BridgeError(409, "This agent pane changed. Reopen the chat.");
|
|
377
|
+
// Force fresh process/log evidence before a mutation.
|
|
378
|
+
if (await paneIdentity(target.server, pane, true))
|
|
379
|
+
throw new BridgeError(409, "The conversation is ready. Wait for chat to attach before sending.");
|
|
380
|
+
const state = await paneChatState(target.server, pane);
|
|
381
|
+
if (!state.starting || state.startingToken !== target.startingToken)
|
|
382
|
+
throw new BridgeError(409, "This starting agent changed. Reopen the chat.");
|
|
383
|
+
if (["blocked", "waiting", "unknown"].includes(String(pane.agent_status)))
|
|
384
|
+
throw new BridgeError(409, "This agent needs input in the terminal first.");
|
|
385
|
+
return pane;
|
|
386
|
+
}
|
|
387
|
+
/** Identities already resolved for a shared snapshot's pane objects, so every
|
|
388
|
+
* stream reading the same snapshot resolves a pane once. */
|
|
389
|
+
const sharedIdentities = new WeakMap();
|
|
390
|
+
/**
|
|
391
|
+
* The target's pane, only while it still runs the target's conversation.
|
|
392
|
+
* `sharedWithinMs` lets a poller (a stream tick) accept a shared snapshot up
|
|
393
|
+
* to that old; sends, keys and every other mutation keep a fresh one.
|
|
394
|
+
*/
|
|
395
|
+
export async function validateTarget(target, sending = false, refreshIdentity = sending, sharedWithinMs = 0) {
|
|
396
|
+
const shared = sharedWithinMs > 0 && !sending && !refreshIdentity;
|
|
397
|
+
const s = shared ? await sharedSnapshot(target.server, sharedWithinMs) : await snapshot(target.server);
|
|
398
|
+
const pane = findPane(s, target);
|
|
399
|
+
let identity;
|
|
400
|
+
if (pane && shared) {
|
|
401
|
+
identity = sharedIdentities.get(pane);
|
|
402
|
+
if (!identity) {
|
|
403
|
+
identity = paneIdentity(target.server, pane);
|
|
404
|
+
sharedIdentities.set(pane, identity);
|
|
405
|
+
identity.catch(() => sharedIdentities.delete(pane));
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
else if (pane)
|
|
409
|
+
identity = paneIdentity(target.server, pane, refreshIdentity);
|
|
410
|
+
if (!pane || await identity !== target.session)
|
|
146
411
|
throw new BridgeError(409, "This pane's conversation changed. Reopen the chat.");
|
|
147
412
|
if (sending && ["blocked", "waiting", "unknown"].includes(String(pane.agent_status)))
|
|
148
413
|
throw new BridgeError(409, "This agent needs input in the terminal first.");
|
package/dist/bridge/hook-main.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { runBridge } from "./command.js";
|
|
2
|
+
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
3
|
+
Object.assign(process.env, nonInteractiveGitEnv());
|
|
2
4
|
runBridge(process.argv.slice(2), PHREN_HOOK_VERSION).then(code => {
|
|
3
5
|
process.exitCode = code;
|
|
4
6
|
}).catch(error => {
|
package/dist/bridge/install.d.ts
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
|
+
import { type ModuleSnapshot } from "../modules/runtime.js";
|
|
1
2
|
export declare const forcedCommand = "command=\"sh ~/.local/share/phren/bridge/dispatch\"";
|
|
2
3
|
export declare function upgradeKeys(text: string): {
|
|
3
4
|
text: string;
|
|
4
5
|
changed: number;
|
|
5
6
|
};
|
|
7
|
+
/** The forwarder the forced command uses for the phone's byte pipe. */
|
|
8
|
+
export type GatewayKind = "socat" | "nc" | "node";
|
|
9
|
+
export interface GatewayEnvironment {
|
|
10
|
+
root: string;
|
|
11
|
+
herdr: string;
|
|
12
|
+
store: string;
|
|
13
|
+
profile: string;
|
|
14
|
+
node: string;
|
|
15
|
+
bundle: string;
|
|
16
|
+
socket: string;
|
|
17
|
+
timing: string;
|
|
18
|
+
}
|
|
19
|
+
/** Pick the cheapest reliable forwarder at install time: socat, then an nc that
|
|
20
|
+
* understands Unix sockets, then the node gateway that always works. */
|
|
21
|
+
export declare function detectGateway(env?: NodeJS.ProcessEnv): Promise<GatewayKind>;
|
|
22
|
+
/** The POSIX sh forced command: the phone's byte pipe goes straight to the Hook
|
|
23
|
+
* socket through a tiny forwarder, so a loaded machine never pays for a fresh
|
|
24
|
+
* node process; every other SSH command falls through to the node gateway. */
|
|
25
|
+
export declare function gatewayScript(gateway: GatewayKind, environment: GatewayEnvironment): string;
|
|
26
|
+
/** The launchd domain for the LaunchAgent: the GUI session when someone is
|
|
27
|
+
* logged in at the screen, else the per-user background domain an SSH login
|
|
28
|
+
* has (gui/<uid> does not exist there, so a bootstrap into it fails). */
|
|
29
|
+
export declare function launchDomain(uid: number, guiSession: boolean): string;
|
|
30
|
+
/** The commands that restart the Hook in `domain`, printed when launchd refuses. */
|
|
31
|
+
export declare function launchCommands(domain: string, plist: string): string[];
|
|
6
32
|
export declare function install(version: string, noService?: boolean): Promise<void>;
|
|
7
33
|
export declare function uninstall(): Promise<void>;
|
|
8
34
|
interface SettingsEdit {
|
|
@@ -10,6 +36,11 @@ interface SettingsEdit {
|
|
|
10
36
|
before?: string;
|
|
11
37
|
after: string;
|
|
12
38
|
}
|
|
13
|
-
export declare function planAgentHooks(program: string, remove?: boolean): Promise<SettingsEdit[]>;
|
|
39
|
+
export declare function planAgentHooks(program: string, remove?: boolean, modules?: ModuleSnapshot): Promise<SettingsEdit[]>;
|
|
40
|
+
/** The first line of every plugin copy phren wrote; a copy without it belongs to the user. */
|
|
41
|
+
export declare const OPENCODE_PLUGIN_MARKER = "// Installed by Phren Hook";
|
|
42
|
+
/** Whether to (re)write the installed plugin: a missing copy, or one phren wrote that is now out of date. */
|
|
43
|
+
export declare function opencodePluginNeedsWrite(existing: string | undefined, source: string): boolean;
|
|
14
44
|
export declare function rollback(): Promise<void>;
|
|
45
|
+
export declare function reconcileModuleHooks(store: string, profile?: string): Promise<void>;
|
|
15
46
|
export {};
|