@phren/cli 0.2.15 → 0.3.0
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 +195 -2
- package/dist/bridge/agent-hooks.js +1049 -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 +23 -0
- package/dist/bridge/code-note.js +51 -0
- package/dist/bridge/code-routes.d.ts +151 -0
- package/dist/bridge/code-routes.js +442 -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 +125 -0
- package/dist/bridge/dispatch-returns.js +283 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +210 -0
- package/dist/bridge/dispatch.js +286 -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 +28 -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 +61 -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 +54 -0
- package/dist/bridge/schedule-watch.js +255 -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 +36 -0
- package/dist/bridge/server-pane-routes.js +479 -0
- package/dist/bridge/server-routes.d.ts +108 -0
- package/dist/bridge/server-routes.js +556 -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 +96 -0
- package/dist/bridge/transcript-preview.js +491 -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 +46545 -21051
- package/dist/cli/actions.js +14 -1
- package/dist/cli/config-policy.js +25 -0
- 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.d.ts +0 -8
- package/dist/cli/extract.js +4 -7
- 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 +17 -4
- package/dist/content/validate.js +200 -122
- 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 +16 -1
- package/dist/data/access.js +31 -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 +525 -419
- 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 +11 -8
- 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 -15
- package/dist/hooks.js +14 -1
- 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 +15 -5
- package/dist/init/init-walkthrough.js +1 -1
- 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 +68 -593
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -6
- package/dist/init/store-gitignore.js +2 -8
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context-imports.d.ts +15 -0
- package/dist/link/context-imports.js +101 -0
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +128 -58
- 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 -19
- package/dist/mcp/profile.js +64 -22
- package/dist/memory-ui-graph.runtime.js +525 -419
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +160 -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 +43 -8
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.d.ts +1 -0
- package/dist/project-topics.js +13 -10
- 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 -3
- package/dist/shared/retrieval.js +101 -12
- 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 +186 -0
- package/dist/tools/config.js +8 -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.d.ts +11 -0
- package/dist/tools/finding.js +180 -123
- 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 +52 -7
- package/dist/update.js +22 -11
- 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 +49 -7
- 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
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { open, stat } from "node:fs/promises";
|
|
2
|
+
import { findPane, paneIdentity, snapshot } from "./herdr.js";
|
|
3
|
+
import { readPaneText } from "./pane-text.js";
|
|
4
|
+
import { object, objects } from "./protocol.js";
|
|
5
|
+
import { transcriptPath } from "./transcripts.js";
|
|
6
|
+
import { stripTerminal } from "../terminal-text.js";
|
|
7
|
+
/** Watching a scheduled run in its Herdr pane: how it ended, whether the final
|
|
8
|
+
* reply asks the owner something, and a startup screen that blocked it. */
|
|
9
|
+
export const STARTUP_BLOCK_WINDOW_MS = 90_000;
|
|
10
|
+
export const STARTUP_BLOCK_WINDOW_OPEN_MS = 5_000;
|
|
11
|
+
const STARTUP_LATE_TRANSCRIPT_MS = 30_000;
|
|
12
|
+
const STARTUP_PANE_READ_LIMIT = 3;
|
|
13
|
+
const STARTUP_PROMPT_TAIL_LINES = 12;
|
|
14
|
+
const STARTUP_BLOCK_STATUSES = ["blocked", "waiting"];
|
|
15
|
+
const STARTUP_PROMPT_MARKER = /[?❯]|\(y\/?n\)|^\s*\d+[.)]\s/m;
|
|
16
|
+
export function classifyStartupBlock(input) {
|
|
17
|
+
if (input.elapsedMs < STARTUP_BLOCK_WINDOW_MS || input.elapsedMs > STARTUP_BLOCK_WINDOW_MS + STARTUP_BLOCK_WINDOW_OPEN_MS)
|
|
18
|
+
return undefined;
|
|
19
|
+
if (input.transcriptActive)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (!STARTUP_BLOCK_STATUSES.includes(String(input.status)))
|
|
22
|
+
return undefined;
|
|
23
|
+
const lines = input.lines.map(line => stripTerminal(line).trim()).filter(Boolean);
|
|
24
|
+
const text = lines.slice(-STARTUP_PROMPT_TAIL_LINES).join("\n");
|
|
25
|
+
if (!text || !STARTUP_PROMPT_MARKER.test(text))
|
|
26
|
+
return undefined;
|
|
27
|
+
return text.slice(0, 4000);
|
|
28
|
+
}
|
|
29
|
+
async function paneRecentLines(server, paneId) {
|
|
30
|
+
const text = await readPaneText(server, paneId, { method: "pane.read", source: "recent", lines: 40, what: "Scheduled run pane read" });
|
|
31
|
+
return text ? text.split(/\r?\n/) : [];
|
|
32
|
+
}
|
|
33
|
+
/** The watch loop's own words for a failure, instead of assuming Herdr went away. */
|
|
34
|
+
export function watchFailureReason(error) {
|
|
35
|
+
const first = (error instanceof Error ? error.message : String(error)).split("\n")[0].replace(/[\x00-\x1f\x7f]/g, " ").trim().slice(0, 200);
|
|
36
|
+
return `Watching the scheduled prompt failed: ${first || "unknown error"}`;
|
|
37
|
+
}
|
|
38
|
+
/** The public text of one assistant row, without reasoning or tool output. */
|
|
39
|
+
export function publicAssistant(raw, source) {
|
|
40
|
+
if (source === "codex") {
|
|
41
|
+
const payload = object(raw.payload);
|
|
42
|
+
if (raw.type !== "response_item" || payload.type !== "message" || payload.role !== "assistant" || payload.channel === "analysis")
|
|
43
|
+
return;
|
|
44
|
+
const text = typeof payload.content === "string" ? payload.content : objects(payload.content)
|
|
45
|
+
.filter(block => ["text", "output_text"].includes(String(block.type))).map(block => String(block.text ?? "")).join("\n");
|
|
46
|
+
return text.trim() || undefined;
|
|
47
|
+
}
|
|
48
|
+
if (source === "claude") {
|
|
49
|
+
const message = object(raw.message);
|
|
50
|
+
if (raw.type !== "assistant" || message.role !== "assistant")
|
|
51
|
+
return;
|
|
52
|
+
const text = typeof message.content === "string" ? message.content : objects(message.content)
|
|
53
|
+
.filter(block => block.type === "text").map(block => String(block.text ?? "")).join("\n");
|
|
54
|
+
return text.trim() || undefined;
|
|
55
|
+
}
|
|
56
|
+
if (source === "opencode") {
|
|
57
|
+
const data = object(raw.data), message = object(data.message);
|
|
58
|
+
if (raw.type !== "assistant/message" || message.role !== "assistant")
|
|
59
|
+
return;
|
|
60
|
+
const text = typeof message.content === "string" ? message.content : objects(message.content)
|
|
61
|
+
.filter(block => block.type === "text").map(block => String(block.text ?? "")).join("\n");
|
|
62
|
+
return text.trim() || undefined;
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const FINAL_TURN_TAIL_BYTES = 512 * 1024;
|
|
67
|
+
/** A turn's end as the harness writes it: Claude's end_turn reply or its
|
|
68
|
+
* turn_duration record, Codex's task_complete, opencode's end_turn step. */
|
|
69
|
+
function turnEnded(raw, source) {
|
|
70
|
+
const payload = object(raw.payload), message = object(raw.message), data = object(raw.data);
|
|
71
|
+
if (source === "claude")
|
|
72
|
+
return (raw.type === "assistant" && message.stop_reason === "end_turn")
|
|
73
|
+
|| (raw.type === "system" && raw.subtype === "turn_duration");
|
|
74
|
+
if (source === "codex")
|
|
75
|
+
return raw.type === "event_msg" && ["task_complete", "task_completed"].includes(String(payload.type));
|
|
76
|
+
return data.stop_reason === "end_turn";
|
|
77
|
+
}
|
|
78
|
+
/** The error a turn ended on, when the harness records one: Codex's
|
|
79
|
+
* task_complete carries `error.message` (a usage limit, a failed compaction). */
|
|
80
|
+
function turnError(raw, source) {
|
|
81
|
+
if (source !== "codex")
|
|
82
|
+
return;
|
|
83
|
+
const error = object(object(raw.payload).error), message = typeof error.message === "string" ? error.message.replace(/\s+/g, " ").trim() : "";
|
|
84
|
+
return message ? message.slice(0, 500) : undefined;
|
|
85
|
+
}
|
|
86
|
+
/** The last assistant reply in a transcript and whether its turn finished.
|
|
87
|
+
* A person's message after the reply opens a new turn, so it clears both. */
|
|
88
|
+
export function finalTurnFromLines(lines, source) {
|
|
89
|
+
let completed = false, lastAssistant, error;
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
if (!line.trim())
|
|
92
|
+
continue;
|
|
93
|
+
let raw;
|
|
94
|
+
try {
|
|
95
|
+
raw = object(JSON.parse(line));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const payload = object(raw.payload);
|
|
101
|
+
const userTurn = source === "claude" ? raw.type === "user" && !raw.isMeta && typeof object(raw.message).content === "string"
|
|
102
|
+
: source === "codex" ? raw.type === "response_item" && payload.type === "message" && payload.role === "user"
|
|
103
|
+
: raw.type === "user/message";
|
|
104
|
+
if (userTurn) {
|
|
105
|
+
completed = false;
|
|
106
|
+
lastAssistant = undefined;
|
|
107
|
+
error = undefined;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const text = publicAssistant(raw, source);
|
|
111
|
+
if (text) {
|
|
112
|
+
lastAssistant = text;
|
|
113
|
+
completed = false;
|
|
114
|
+
}
|
|
115
|
+
if (turnEnded(raw, source)) {
|
|
116
|
+
completed = true;
|
|
117
|
+
error = turnError(raw, source);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { completed, ...(lastAssistant ? { lastAssistant } : {}), ...(completed && error ? { error } : {}) };
|
|
121
|
+
}
|
|
122
|
+
/** The final turn of a conversation, read from the tail of its transcript. */
|
|
123
|
+
export async function readFinalTurn(source, sessionId) {
|
|
124
|
+
if (!sessionId)
|
|
125
|
+
return undefined;
|
|
126
|
+
const file = await transcriptPath(source, sessionId).catch(() => undefined);
|
|
127
|
+
if (!file)
|
|
128
|
+
return undefined;
|
|
129
|
+
const handle = await open(file, "r").catch(() => undefined);
|
|
130
|
+
if (!handle)
|
|
131
|
+
return undefined;
|
|
132
|
+
try {
|
|
133
|
+
const { size } = await handle.stat();
|
|
134
|
+
const start = Math.max(0, size - FINAL_TURN_TAIL_BYTES), buffer = Buffer.alloc(size - start);
|
|
135
|
+
await handle.read(buffer, 0, buffer.length, start);
|
|
136
|
+
const lines = buffer.toString("utf8").split("\n");
|
|
137
|
+
return finalTurnFromLines(start > 0 ? lines.slice(1) : lines, source);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await handle.close().catch(() => undefined);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const OPTION_LINE = /^\s*(?:[-*]\s+)?\(?\d{1,2}[.)]\s+\S/;
|
|
147
|
+
const CHOICE_WORDS = /\b(?:choose|pick|which|options?|prefer|want|should I|shall I|let me know|decide|approve|confirm|go ahead)\b/i;
|
|
148
|
+
function plainLine(line) {
|
|
149
|
+
return line.replace(/[*_`#>]+/g, "").replace(/\s+/g, " ").trim().slice(0, 200);
|
|
150
|
+
}
|
|
151
|
+
/** When a finished reply ends by asking the owner something (a question, or
|
|
152
|
+
* numbered options introduced as a choice), the question's first line. */
|
|
153
|
+
export function ownerQuestion(text) {
|
|
154
|
+
const lines = text.replace(/\r/g, "").split("\n").map(line => line.trimEnd());
|
|
155
|
+
while (lines.length && !lines.at(-1).trim())
|
|
156
|
+
lines.pop();
|
|
157
|
+
if (!lines.length)
|
|
158
|
+
return undefined;
|
|
159
|
+
let end = lines.length;
|
|
160
|
+
while (end > 0 && OPTION_LINE.test(lines[end - 1]))
|
|
161
|
+
end--;
|
|
162
|
+
if (lines.length - end >= 2) {
|
|
163
|
+
let intro = end - 1;
|
|
164
|
+
while (intro >= 0 && !lines[intro].trim())
|
|
165
|
+
intro--;
|
|
166
|
+
const line = intro >= 0 ? plainLine(lines[intro]) : "";
|
|
167
|
+
return line && (line.endsWith("?") || (line.endsWith(":") && CHOICE_WORDS.test(line))) ? line : undefined;
|
|
168
|
+
}
|
|
169
|
+
const last = plainLine(lines.at(-1));
|
|
170
|
+
if (!/\?\)?$/.test(last))
|
|
171
|
+
return undefined;
|
|
172
|
+
let first = lines.length - 1;
|
|
173
|
+
while (first > 0 && lines[first - 1].trim() && !OPTION_LINE.test(lines[first - 1]))
|
|
174
|
+
first--;
|
|
175
|
+
return plainLine(lines[first]) || last;
|
|
176
|
+
}
|
|
177
|
+
async function realTranscriptStamp(source, sessionId) {
|
|
178
|
+
if (!sessionId)
|
|
179
|
+
return undefined;
|
|
180
|
+
const file = await transcriptPath(source, sessionId).catch(() => undefined);
|
|
181
|
+
const metadata = file ? await stat(file).catch(() => undefined) : undefined;
|
|
182
|
+
return metadata ? { size: metadata.size, mtimeMs: metadata.mtimeMs } : undefined;
|
|
183
|
+
}
|
|
184
|
+
export async function watchHerdrRun(server, target, signal, startup, env = {}) {
|
|
185
|
+
const now = env.now ?? Date.now;
|
|
186
|
+
const pause = env.pause ?? ((ms) => new Promise(resolve => { const timer = setTimeout(resolve, ms); timer.unref(); }));
|
|
187
|
+
const readPane = env.readPane ?? paneRecentLines;
|
|
188
|
+
const resolveSession = env.resolveSession ?? ((name, pane) => paneIdentity(name, pane).catch(() => undefined));
|
|
189
|
+
const transcriptStamp = env.transcriptStamp ?? realTranscriptStamp;
|
|
190
|
+
const listPanes = env.panes ?? (async (name) => objects((await snapshot(name)).panes));
|
|
191
|
+
const finalTurn = env.finalTurn ?? readFinalTurn;
|
|
192
|
+
let sessionId = startup.sessionId;
|
|
193
|
+
let transcriptActive = false;
|
|
194
|
+
let stamp;
|
|
195
|
+
let recordedBlock = false;
|
|
196
|
+
let paneReads = 0;
|
|
197
|
+
while (!signal.aborted) {
|
|
198
|
+
await pause(1000);
|
|
199
|
+
if (signal.aborted)
|
|
200
|
+
break;
|
|
201
|
+
try {
|
|
202
|
+
const pane = findPane({ panes: await listPanes(server) }, { workspace: target.workspaceId, tab: target.tabId, pane: target.paneId });
|
|
203
|
+
if (!pane)
|
|
204
|
+
return { status: "failed", reason: "The Herdr pane closed before the scheduled prompt finished." };
|
|
205
|
+
const status = String(pane.agent_status);
|
|
206
|
+
// Herdr reports a finished turn as idle, or as done until someone looks
|
|
207
|
+
// at the pane. Either one is the agent stopping on its own.
|
|
208
|
+
if (status === "idle" || status === "done") {
|
|
209
|
+
if (!sessionId)
|
|
210
|
+
sessionId = await resolveSession(server, pane);
|
|
211
|
+
const turn = await finalTurn(startup.source, sessionId).catch(() => undefined);
|
|
212
|
+
if (turn?.completed && turn.error)
|
|
213
|
+
return { status: "failed", reason: turn.error };
|
|
214
|
+
const question = turn?.completed && turn.lastAssistant ? ownerQuestion(turn.lastAssistant) : undefined;
|
|
215
|
+
return question ? { status: "needs-you", reason: question } : { status: "finished" };
|
|
216
|
+
}
|
|
217
|
+
if (!["working", "starting", "blocked", "waiting", "unknown"].includes(status)) {
|
|
218
|
+
return { status: "failed", reason: "The scheduled agent stopped before the prompt finished." };
|
|
219
|
+
}
|
|
220
|
+
const elapsedMs = now() - startup.startedAt;
|
|
221
|
+
const withinWindow = elapsedMs <= STARTUP_BLOCK_WINDOW_MS + STARTUP_BLOCK_WINDOW_OPEN_MS;
|
|
222
|
+
if (!transcriptActive && startup.onBlocked && !recordedBlock && withinWindow) {
|
|
223
|
+
if (!sessionId)
|
|
224
|
+
sessionId = await resolveSession(server, pane);
|
|
225
|
+
const next = await transcriptStamp(startup.source, sessionId);
|
|
226
|
+
if (next) {
|
|
227
|
+
if (!stamp) {
|
|
228
|
+
stamp = next;
|
|
229
|
+
if (elapsedMs > STARTUP_LATE_TRANSCRIPT_MS)
|
|
230
|
+
transcriptActive = true;
|
|
231
|
+
}
|
|
232
|
+
else if (next.size !== stamp.size || next.mtimeMs !== stamp.mtimeMs) {
|
|
233
|
+
stamp = next;
|
|
234
|
+
transcriptActive = true;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (!recordedBlock && startup.onBlocked && withinWindow && !transcriptActive
|
|
239
|
+
&& elapsedMs >= STARTUP_BLOCK_WINDOW_MS && STARTUP_BLOCK_STATUSES.includes(status)
|
|
240
|
+
&& paneReads < STARTUP_PANE_READ_LIMIT) {
|
|
241
|
+
paneReads++;
|
|
242
|
+
const lines = await readPane(server, target.paneId);
|
|
243
|
+
const prompt = classifyStartupBlock({ elapsedMs, transcriptActive, status, lines });
|
|
244
|
+
if (prompt) {
|
|
245
|
+
recordedBlock = true;
|
|
246
|
+
await Promise.resolve(startup.onBlocked(prompt)).catch(() => undefined);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return { status: "failed", reason: watchFailureReason(error) };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return { status: "failed", reason: "Phren Hook stopped while the scheduled prompt was running." };
|
|
255
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type ScheduleLauncher, type ScheduleRun, type SchedulePushSender, type ScheduleStatus } from "./schedule-format.js";
|
|
2
|
+
export * from "./schedule-format.js";
|
|
3
|
+
export * from "./schedule-launch.js";
|
|
4
|
+
export * from "./schedule-watch.js";
|
|
5
|
+
export declare class Scheduler {
|
|
6
|
+
private readonly now;
|
|
7
|
+
private readonly store;
|
|
8
|
+
private readonly launch;
|
|
9
|
+
private readonly runsFile;
|
|
10
|
+
private readonly computer;
|
|
11
|
+
private readonly locateProject;
|
|
12
|
+
private readonly push?;
|
|
13
|
+
private readonly log;
|
|
14
|
+
private serial;
|
|
15
|
+
private ticking;
|
|
16
|
+
/** When the scheduler last looked for due work; a health read shows it. */
|
|
17
|
+
lastTickAt?: Date;
|
|
18
|
+
constructor(options: {
|
|
19
|
+
now: () => Date;
|
|
20
|
+
store: string;
|
|
21
|
+
launch: ScheduleLauncher;
|
|
22
|
+
runsFile: string;
|
|
23
|
+
computer?: string | (() => string);
|
|
24
|
+
locateProject?: (project: string) => Promise<string | undefined>;
|
|
25
|
+
push?: SchedulePushSender;
|
|
26
|
+
log?: (message: string) => void;
|
|
27
|
+
});
|
|
28
|
+
private projectDirectories;
|
|
29
|
+
private projectDirectory;
|
|
30
|
+
private exclusive;
|
|
31
|
+
statuses(): Promise<{
|
|
32
|
+
computer: string;
|
|
33
|
+
timeZone: string;
|
|
34
|
+
schedules: ScheduleStatus[];
|
|
35
|
+
}>;
|
|
36
|
+
history(filters?: {
|
|
37
|
+
project?: string;
|
|
38
|
+
id?: string;
|
|
39
|
+
limit?: number;
|
|
40
|
+
}): Promise<ScheduleRun[]>;
|
|
41
|
+
launchNow(projectName: string, id: string): Promise<ScheduleRun>;
|
|
42
|
+
private updateRun;
|
|
43
|
+
private finishRun;
|
|
44
|
+
private recordBlockedStartup;
|
|
45
|
+
private notifyRun;
|
|
46
|
+
tick(): Promise<void>;
|
|
47
|
+
close(): void;
|
|
48
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { hostname } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { BridgeError } from "./protocol.js";
|
|
5
|
+
import { getProjectSourcePath } from "../project-config.js";
|
|
6
|
+
import { getProjectDirs } from "../shared.js";
|
|
7
|
+
import { computerMatches, nextRun, readScheduleDocument, readScheduleRuns, runningStatuses, scheduleNotifications, scheduleSessionRoute, writeScheduleDocument, writeScheduleRuns, } from "./schedule-format.js";
|
|
8
|
+
export * from "./schedule-format.js";
|
|
9
|
+
export * from "./schedule-launch.js";
|
|
10
|
+
export * from "./schedule-watch.js";
|
|
11
|
+
export class Scheduler {
|
|
12
|
+
now;
|
|
13
|
+
store;
|
|
14
|
+
launch;
|
|
15
|
+
runsFile;
|
|
16
|
+
computer;
|
|
17
|
+
locateProject;
|
|
18
|
+
push;
|
|
19
|
+
log;
|
|
20
|
+
serial = Promise.resolve();
|
|
21
|
+
ticking = false;
|
|
22
|
+
/** When the scheduler last looked for due work; a health read shows it. */
|
|
23
|
+
lastTickAt;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.now = options.now;
|
|
26
|
+
this.store = options.store;
|
|
27
|
+
this.launch = options.launch;
|
|
28
|
+
this.runsFile = options.runsFile;
|
|
29
|
+
if (typeof options.computer === "function")
|
|
30
|
+
this.computer = options.computer;
|
|
31
|
+
else {
|
|
32
|
+
const computer = options.computer;
|
|
33
|
+
this.computer = () => computer ?? hostname();
|
|
34
|
+
}
|
|
35
|
+
this.locateProject = options.locateProject ?? (async () => undefined);
|
|
36
|
+
this.push = options.push;
|
|
37
|
+
this.log = options.log ?? (message => console.error(message));
|
|
38
|
+
}
|
|
39
|
+
projectDirectories() { return getProjectDirs(this.store); }
|
|
40
|
+
projectDirectory(project) {
|
|
41
|
+
const found = this.projectDirectories().find(directory => path.basename(directory).toLowerCase() === project.toLowerCase());
|
|
42
|
+
if (!found)
|
|
43
|
+
throw new BridgeError(404, `Unknown project "${project}".`);
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
async exclusive(operation) {
|
|
47
|
+
const previous = this.serial;
|
|
48
|
+
let release;
|
|
49
|
+
this.serial = new Promise(resolve => { release = resolve; });
|
|
50
|
+
await previous;
|
|
51
|
+
try {
|
|
52
|
+
return await operation();
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
release();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async statuses() {
|
|
59
|
+
const runs = await readScheduleRuns(this.runsFile), result = [], computer = this.computer();
|
|
60
|
+
for (const directory of this.projectDirectories()) {
|
|
61
|
+
let schedules;
|
|
62
|
+
try {
|
|
63
|
+
schedules = (await readScheduleDocument(directory)).schedules;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const project = path.basename(directory);
|
|
69
|
+
for (const schedule of schedules) {
|
|
70
|
+
const matching = runs.filter(run => run.project === project && run.scheduleId === schedule.id);
|
|
71
|
+
const last = matching.at(-1), running = matching.some(run => runningStatuses.has(run.status));
|
|
72
|
+
const due = computerMatches(schedule.computer, computer) ? nextRun(schedule, last) : null;
|
|
73
|
+
result.push({ ...schedule, project, nextRun: due?.toISOString() ?? null,
|
|
74
|
+
lastRun: last ? { startedAt: last.startedAt, ...(last.finishedAt ? { finishedAt: last.finishedAt } : {}), status: last.status,
|
|
75
|
+
...(last.reason ? { reason: last.reason } : {}),
|
|
76
|
+
...(last.blockedStartupPrompt ? { blockedStartupPrompt: last.blockedStartupPrompt } : {}),
|
|
77
|
+
...(last.blockNotified !== undefined ? { blockNotified: last.blockNotified } : {}), launch: last.launch } : null, running });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { computer, timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, schedules: result };
|
|
81
|
+
}
|
|
82
|
+
async history(filters = {}) {
|
|
83
|
+
const limit = Math.min(500, Math.max(1, filters.limit ?? 50));
|
|
84
|
+
return (await readScheduleRuns(this.runsFile)).filter(run => (!filters.project || run.project.toLowerCase() === filters.project.toLowerCase())
|
|
85
|
+
&& (!filters.id || run.scheduleId === filters.id)).slice(-limit).reverse();
|
|
86
|
+
}
|
|
87
|
+
async launchNow(projectName, id) {
|
|
88
|
+
const prepared = await this.exclusive(async () => {
|
|
89
|
+
const projectDir = this.projectDirectory(projectName), project = path.basename(projectDir);
|
|
90
|
+
const file = await readScheduleDocument(projectDir), schedule = file.schedules.find(item => item.id === id);
|
|
91
|
+
if (!schedule)
|
|
92
|
+
throw new BridgeError(404, `Schedule "${id}" was not found in ${project}.`);
|
|
93
|
+
if (!computerMatches(schedule.computer, this.computer()))
|
|
94
|
+
throw new BridgeError(409, `${schedule.computer} owns this schedule; run it from that computer.`);
|
|
95
|
+
const runs = await readScheduleRuns(this.runsFile);
|
|
96
|
+
if (runs.some(run => run.project === project && run.scheduleId === id && runningStatuses.has(run.status))) {
|
|
97
|
+
throw new BridgeError(409, `Schedule "${schedule.name}" is already running.`);
|
|
98
|
+
}
|
|
99
|
+
const run = { id: randomUUID(), scheduleId: id, project, startedAt: this.now().toISOString(), status: "launched", launch: { mode: "headless" } };
|
|
100
|
+
await writeScheduleRuns(this.runsFile, [...runs, run]);
|
|
101
|
+
if (schedule.every === "once" && schedule.enabled) {
|
|
102
|
+
const updatedAt = this.now().toISOString();
|
|
103
|
+
const schedules = file.schedules.map(item => item.id === id ? { ...item, enabled: false, updatedAt } : item);
|
|
104
|
+
await writeScheduleDocument(projectDir, schedules, file.document);
|
|
105
|
+
}
|
|
106
|
+
const source = getProjectSourcePath(this.store, project);
|
|
107
|
+
return { projectDir, project, schedule, run, cwd: source ?? await this.locateProject(project) ?? projectDir };
|
|
108
|
+
});
|
|
109
|
+
try {
|
|
110
|
+
const launched = await this.launch({ schedule: prepared.schedule, project: prepared.project, projectDir: prepared.projectDir,
|
|
111
|
+
cwd: prepared.cwd, runId: prepared.run.id,
|
|
112
|
+
blockedStartup: promptText => this.recordBlockedStartup(prepared.run.id, prepared.schedule, promptText) });
|
|
113
|
+
const running = await this.updateRun(prepared.run.id, { status: "running", launch: launched.launch });
|
|
114
|
+
const notified = this.notifyRun(running, prepared.schedule, "scheduleStarted");
|
|
115
|
+
if (launched.completion)
|
|
116
|
+
void launched.completion.then(async (result) => {
|
|
117
|
+
await this.finishRun(prepared.run.id, prepared.schedule, result.status, result.reason, notified);
|
|
118
|
+
}).catch(async (error) => {
|
|
119
|
+
await this.finishRun(prepared.run.id, prepared.schedule, "failed", error instanceof Error ? error.message : "The scheduled agent failed.", notified);
|
|
120
|
+
}).catch(error => this.log(`[schedule] Could not finish run ${prepared.run.id}: ${String(error)}`));
|
|
121
|
+
await notified;
|
|
122
|
+
return running;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
const reason = error instanceof Error ? error.message : "The scheduled agent could not start.";
|
|
126
|
+
await this.finishRun(prepared.run.id, prepared.schedule, "failed", reason);
|
|
127
|
+
throw error instanceof BridgeError ? error : new BridgeError(503, reason);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async updateRun(id, patch) {
|
|
131
|
+
return this.exclusive(async () => {
|
|
132
|
+
const runs = await readScheduleRuns(this.runsFile), index = runs.findIndex(run => run.id === id);
|
|
133
|
+
if (index < 0)
|
|
134
|
+
throw new Error("The schedule run record disappeared.");
|
|
135
|
+
runs[index] = { ...runs[index], ...patch };
|
|
136
|
+
await writeScheduleRuns(this.runsFile, runs);
|
|
137
|
+
return runs[index];
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
async finishRun(id, schedule, status, reason, previousNotification) {
|
|
141
|
+
const run = await this.updateRun(id, { status, finishedAt: this.now().toISOString(), ...(reason ? { reason } : {}) });
|
|
142
|
+
await previousNotification;
|
|
143
|
+
if (run.blockedStartupPrompt && run.blockNotified)
|
|
144
|
+
return;
|
|
145
|
+
await this.notifyRun(run, schedule, status === "failed" ? "scheduleFailed" : "scheduleFinished");
|
|
146
|
+
}
|
|
147
|
+
async recordBlockedStartup(id, schedule, promptText) {
|
|
148
|
+
try {
|
|
149
|
+
const prompt = promptText.slice(0, 4000);
|
|
150
|
+
const run = await this.updateRun(id, { status: "blocked", blockedStartupPrompt: prompt });
|
|
151
|
+
const delivered = await this.notifyRun(run, schedule, "scheduleBlocked", `Blocked at startup: ${prompt}`);
|
|
152
|
+
if (delivered)
|
|
153
|
+
await this.updateRun(id, { blockNotified: true });
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
this.log(`[schedule] blocked-at-startup record for run ${id} failed: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async notifyRun(run, schedule, kind, reason) {
|
|
160
|
+
const preference = kind === "scheduleStarted" ? "start" : kind === "scheduleFinished" ? "finish" : "failure";
|
|
161
|
+
if (!scheduleNotifications(schedule).has(preference))
|
|
162
|
+
return false;
|
|
163
|
+
const route = scheduleSessionRoute(schedule, run.launch);
|
|
164
|
+
const text = reason ?? run.reason;
|
|
165
|
+
const value = { kind, scheduleId: schedule.id, project: run.project, name: schedule.name,
|
|
166
|
+
computer: schedule.computer, runId: run.id,
|
|
167
|
+
status: kind === "scheduleStarted" ? "running" : kind === "scheduleFinished" ? (run.status === "needs-you" ? "needs-you" : "finished")
|
|
168
|
+
: kind === "scheduleBlocked" ? "blocked" : "failed",
|
|
169
|
+
...(text ? { reason: text } : {}), ...(route ? { route } : {}) };
|
|
170
|
+
let result;
|
|
171
|
+
try {
|
|
172
|
+
result = this.push ? await this.push.notify(value) : { notified: false, reason: "no push config" };
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
result = { notified: false, reason: "push delivery failed" };
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
await this.updateRun(run.id, { notified: result.notified, ...(result.reason ? { notifyReason: result.reason } : { notifyReason: undefined }) });
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
this.log(`[schedule] ${kind} notification result for run ${run.id} could not be recorded: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
182
|
+
}
|
|
183
|
+
if (!result.notified)
|
|
184
|
+
this.log(`[schedule] ${kind} notification for run ${run.id}: ${result.reason ?? "not delivered"}`);
|
|
185
|
+
return result.notified;
|
|
186
|
+
}
|
|
187
|
+
async tick() {
|
|
188
|
+
if (this.ticking)
|
|
189
|
+
return;
|
|
190
|
+
this.ticking = true;
|
|
191
|
+
this.lastTickAt = this.now();
|
|
192
|
+
try {
|
|
193
|
+
const status = await this.statuses(), now = this.now();
|
|
194
|
+
for (const schedule of status.schedules) {
|
|
195
|
+
if (!schedule.enabled || schedule.running || !schedule.nextRun || new Date(schedule.nextRun) > now)
|
|
196
|
+
continue;
|
|
197
|
+
await this.launchNow(schedule.project, schedule.id).catch(() => { });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
this.ticking = false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
close() { this.launch.close?.(); }
|
|
205
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
/** The Herdr agent-name slug for a human label: "Conductor smoke 4" becomes "conductor-smoke-4". */
|
|
3
|
+
export declare function herdrAgentName(label: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* "Open on a computer": a new Herdr workspace (or a tab in an existing one)
|
|
6
|
+
* in the project's directory, with the chosen agent started in its pane.
|
|
7
|
+
* Herdr's create calls do not return identifiers, so the new tab is found
|
|
8
|
+
* by diffing snapshots; `agent.start` returns once Herdr has detected the
|
|
9
|
+
* agent and it is ready for input, which can take most of `timeoutMs`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function launchSession(server: string, data: Json, options?: {
|
|
12
|
+
canary?: boolean;
|
|
13
|
+
}): Promise<Json>;
|
|
14
|
+
export declare function workspaceAction(server: string, operation: string, data: Json): Promise<Json>;
|