@jbkjr/relay 0.0.0-stage → 1.2.2
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/LICENSE +21 -0
- package/LICENSES/Apache-2.0.txt +201 -0
- package/LICENSES/README.md +19 -0
- package/LICENSES/codex-NOTICE.txt +6 -0
- package/README.md +393 -2
- package/SECURITY.md +145 -0
- package/adapters/briefs/claude.md +43 -0
- package/adapters/briefs/codex.md +56 -0
- package/adapters/briefs/models.json +33 -0
- package/adapters/build.mjs +8 -0
- package/adapters/claude/.claude-plugin/marketplace.json +13 -0
- package/adapters/claude/relay/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/relay/agents/codex.md +21 -0
- package/adapters/claude/relay/agents/operator.md +21 -0
- package/adapters/claude/relay/skills/ask/SKILL.md +20 -0
- package/adapters/claude/relay/skills/chat/SKILL.md +13 -0
- package/adapters/claude/relay/skills/delegate/SKILL.md +21 -0
- package/adapters/claude/relay/skills/discuss/SKILL.md +19 -0
- package/adapters/claude/relay/skills/review/SKILL.md +21 -0
- package/adapters/claude/relay/skills/tree/SKILL.md +10 -0
- package/adapters/claude/relay/skills/write-for-codex/SKILL.md +78 -0
- package/adapters/codex/.agents/plugins/marketplace.json +20 -0
- package/adapters/codex/AGENTS.snippet.md +5 -0
- package/adapters/codex/relay/.codex-plugin/plugin.json +6 -0
- package/adapters/codex/relay/skills/relay-ask/SKILL.md +20 -0
- package/adapters/codex/relay/skills/relay-chat/SKILL.md +13 -0
- package/adapters/codex/relay/skills/relay-delegate/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-discuss/SKILL.md +19 -0
- package/adapters/codex/relay/skills/relay-review/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-tree/SKILL.md +10 -0
- package/adapters/codex/relay/skills/relay-write-for-claude/SKILL.md +66 -0
- package/adapters/generate.mjs +124 -0
- package/bin/relay +59 -0
- package/codex-pin.json +12 -0
- package/dist/backends/acp.js +1424 -0
- package/dist/backends/claude.js +890 -0
- package/dist/backends/cli-version.js +112 -0
- package/dist/backends/codex-app.js +1539 -0
- package/dist/backends/codex-daybreak.js +75 -0
- package/dist/backends/codex-env.js +656 -0
- package/dist/backends/codex-network.js +304 -0
- package/dist/backends/codex-pin.js +345 -0
- package/dist/backends/codex-shell-policy.js +182 -0
- package/dist/backends/codex-stage2.js +998 -0
- package/dist/backends/codex-subagents.js +47 -0
- package/dist/backends/codex.js +641 -0
- package/dist/backends/copilot.js +246 -0
- package/dist/backends/env.js +147 -0
- package/dist/backends/gemini.js +607 -0
- package/dist/backends/grok.js +337 -0
- package/dist/backends/index.js +8 -0
- package/dist/backends/native-temp.js +187 -0
- package/dist/backends/never-admission.js +90 -0
- package/dist/backends/run.js +67 -0
- package/dist/backends/stream.js +230 -0
- package/dist/backends/tool-discovery.js +15 -0
- package/dist/backends/types.js +264 -0
- package/dist/build-info.json +5 -0
- package/dist/cli.js +344 -0
- package/dist/confine/bridge.js +113 -0
- package/dist/confine/broker-core.js +76 -0
- package/dist/confine/broker-http.js +650 -0
- package/dist/confine/broker-names.js +9 -0
- package/dist/confine/broker.js +258 -0
- package/dist/confine/canary.js +380 -0
- package/dist/confine/diagnostics.js +122 -0
- package/dist/confine/gemini-tools-main.js +8 -0
- package/dist/confine/gemini-tools.js +181 -0
- package/dist/confine/hardlinks.js +58 -0
- package/dist/confine/harness-config.js +16 -0
- package/dist/confine/index.js +117 -0
- package/dist/confine/leases.js +217 -0
- package/dist/confine/paths.js +6 -0
- package/dist/confine/peer.js +115 -0
- package/dist/confine/port-policy.js +8 -0
- package/dist/confine/ports.js +365 -0
- package/dist/confine/private-root.js +208 -0
- package/dist/confine/probe-stage2.js +501 -0
- package/dist/confine/probe.js +1109 -0
- package/dist/confine/proxy.js +783 -0
- package/dist/confine/sbcheck.js +208 -0
- package/dist/confine/seatbelt-never.js +41 -0
- package/dist/confine/seatbelt-stage2.js +139 -0
- package/dist/confine/seatbelt.js +1129 -0
- package/dist/confine/sockets.js +36 -0
- package/dist/confine/turn-probe.js +19 -0
- package/dist/confine/turn.js +86 -0
- package/dist/core/adoption.js +124 -0
- package/dist/core/api-keys.js +423 -0
- package/dist/core/approvals.js +740 -0
- package/dist/core/build-identity.js +248 -0
- package/dist/core/caller.js +749 -0
- package/dist/core/calls.js +102 -0
- package/dist/core/claude-settings.js +1045 -0
- package/dist/core/config.js +453 -0
- package/dist/core/context.js +186 -0
- package/dist/core/db.js +753 -0
- package/dist/core/dispatch.js +110 -0
- package/dist/core/env-broker.js +505 -0
- package/dist/core/env-policy.js +182 -0
- package/dist/core/escalation-authority.js +76 -0
- package/dist/core/escalation-contract.js +13 -0
- package/dist/core/escalation-person-path.js +131 -0
- package/dist/core/escalation-policy.js +79 -0
- package/dist/core/escalation-runtime.js +135 -0
- package/dist/core/escalation-state.js +42 -0
- package/dist/core/escalations.js +191 -0
- package/dist/core/events.js +235 -0
- package/dist/core/executables.js +147 -0
- package/dist/core/extensions.js +645 -0
- package/dist/core/families.js +204 -0
- package/dist/core/git.js +657 -0
- package/dist/core/identity.js +113 -0
- package/dist/core/liveness.js +68 -0
- package/dist/core/mediated-network-provider.js +81 -0
- package/dist/core/never-checkpoint.js +76 -0
- package/dist/core/never-coverage.js +97 -0
- package/dist/core/never-network.js +49 -0
- package/dist/core/never-policy.js +302 -0
- package/dist/core/never-proxy-policy.js +109 -0
- package/dist/core/never-schema.js +61 -0
- package/dist/core/never-tools.js +56 -0
- package/dist/core/notices.js +48 -0
- package/dist/core/ops.js +959 -0
- package/dist/core/outcome.js +224 -0
- package/dist/core/params.js +32 -0
- package/dist/core/presence.js +174 -0
- package/dist/core/procs.js +546 -0
- package/dist/core/prompts.js +363 -0
- package/dist/core/protected-config.js +238 -0
- package/dist/core/raw-network.js +192 -0
- package/dist/core/redact.js +152 -0
- package/dist/core/refusal.js +33 -0
- package/dist/core/relay-command.js +310 -0
- package/dist/core/reported.js +3 -0
- package/dist/core/restart-overlay.js +651 -0
- package/dist/core/retention.js +1135 -0
- package/dist/core/secrets.js +329 -0
- package/dist/core/session-token.js +175 -0
- package/dist/core/session.js +744 -0
- package/dist/core/shell.js +2 -0
- package/dist/core/shipped-pin.js +47 -0
- package/dist/core/shown.js +42 -0
- package/dist/core/slots.js +67 -0
- package/dist/core/stage2-executors.js +453 -0
- package/dist/core/stage2-switch.js +5 -0
- package/dist/core/steering.js +149 -0
- package/dist/core/storage.js +358 -0
- package/dist/core/toml.js +175 -0
- package/dist/core/views.js +324 -0
- package/dist/core/wait.js +114 -0
- package/dist/core/wake.js +207 -0
- package/dist/core/watch.js +247 -0
- package/dist/core/worktree.js +213 -0
- package/dist/hosts/claude-desktop.js +274 -0
- package/dist/hosts/claude.js +249 -0
- package/dist/hosts/codex.js +345 -0
- package/dist/hosts/desktop-shared.js +20 -0
- package/dist/hosts/index.js +11 -0
- package/dist/hosts/types.js +4 -0
- package/dist/install/basics.js +18 -0
- package/dist/install/claude-init.js +133 -0
- package/dist/install/common.js +655 -0
- package/dist/install/doctor.js +635 -0
- package/dist/install/qualify-codex.js +205 -0
- package/dist/install.js +524 -0
- package/dist/mcp/elicit.js +209 -0
- package/dist/mcp/gateway.js +218 -0
- package/dist/mcp/host.js +253 -0
- package/dist/mcp/server.js +1124 -0
- package/dist/mcp/stdio.js +37 -0
- package/dist/modes/discussion.js +393 -0
- package/dist/modes/review.js +722 -0
- package/dist/native/relay-sbcheck +0 -0
- package/dist/targets/briefs.js +86 -0
- package/dist/targets/claude.js +94 -0
- package/dist/targets/codex.js +66 -0
- package/dist/targets/copilot.js +93 -0
- package/dist/targets/gemini.js +119 -0
- package/dist/targets/grok.js +126 -0
- package/dist/targets/index.js +192 -0
- package/dist/targets/types.js +18 -0
- package/dist/targets/verified.js +35 -0
- package/dist/worker.js +113 -0
- package/docs/UPGRADING.md +1518 -0
- package/docs/pinned-codex.md +333 -0
- package/experiments/stage2-redteam/lib/oracles.mjs +214 -0
- package/experiments/stage2-redteam/lib/report.mjs +35 -0
- package/npm-shrinkwrap.json +1632 -0
- package/package.json +82 -4
- package/scripts/codex-qualify/codex-pin-store.mjs +353 -0
- package/scripts/codex-qualify/codex-qualifier-process.mjs +265 -0
- package/scripts/codex-qualify/codex-qualifier.example.json +5 -0
- package/scripts/codex-qualify/codex-qualifier.mjs +281 -0
- package/scripts/codex-qualify/codex-shipped-pin.mjs +158 -0
- package/scripts/codex-qualify/codex-stage2-pin.mjs +48 -0
- package/scripts/codex-qualify/codex-stage2.mjs +2381 -0
- package/scripts/codex-qualify/install-codex-qualifier.mjs +98 -0
- package/scripts/codex-qualify/main-module.mjs +14 -0
- package/scripts/codex-qualify/relay-codex-qualifier.plist +37 -0
- package/scripts/codex-qualify/scratch-env.mjs +31 -0
- package/scripts/codex-qualify/session.mjs +96 -0
- package/scripts/codex-qualify/stage2-escalation-cases.mjs +125 -0
- package/test/codex-permissions.test.ts +875 -0
- package/test/fixtures/boundary-probes.mjs +17 -0
- package/test/fixtures/denial-stream-host.mjs +7 -0
- package/test/fixtures/families.json +246 -0
- package/test/fixtures/install-snapshots/claude-desktop-foreign.txt +158 -0
- package/test/fixtures/install-snapshots/claude-desktop-fresh.txt +101 -0
- package/test/fixtures/install-snapshots/claude-desktop-installed.txt +161 -0
- package/test/fixtures/install-snapshots/claude-foreign.txt +133 -0
- package/test/fixtures/install-snapshots/claude-fresh.txt +77 -0
- package/test/fixtures/install-snapshots/claude-installed.txt +133 -0
- package/test/fixtures/install-snapshots/codex-foreign.txt +159 -0
- package/test/fixtures/install-snapshots/codex-fresh.txt +107 -0
- package/test/fixtures/install-snapshots/codex-installed.txt +161 -0
- package/test/fixtures/link-parent.mjs +6 -0
- package/test/fixtures/mediated-command.cjs +72 -0
- package/test/fixtures/native-skill-isolation.json +280 -0
- package/test/fixtures/pinned-codex-app-server.mjs +25 -0
- package/test/fixtures/qualified-ext-adapters.mjs +36 -0
- package/test/fixtures/refusals/README.md +52 -0
- package/test/fixtures/refusals/claude-source-result.json +7 -0
- package/test/fixtures/refusals/openai-app-capacity.json +24 -0
- package/test/fixtures/refusals/openai-app-cyberPolicy.json +24 -0
- package/test/fixtures/refusals/openai-gpt-6-astra-max.json +12 -0
- package/test/fixtures/refusals/openai-gpt-6.1-sol-xhigh.json +12 -0
- package/test/fixtures/refusals/openai-review-capacity.json +24 -0
- package/test/fixtures/relay-0.2.0.sql +57 -0
- package/test/fixtures/relay-0.9.1.gen.mjs +212 -0
- package/test/fixtures/relay-0.9.1.sql +340 -0
- package/test/fixtures/slow-boundary-probes.mjs +37 -0
- package/test/fixtures/stable-surfaces/config.json +1122 -0
- package/test/fixtures/stable-surfaces/escalation.json +128 -0
- package/test/fixtures/stable-surfaces/events.json +37 -0
- package/test/fixtures/stable-surfaces/gemini-native-resume.json +4 -0
- package/test/fixtures/stable-surfaces/inbox.jsonl +2 -0
- package/test/fixtures/stable-surfaces/notice.txt +1 -0
- package/test/fixtures/stable-surfaces/refusal-item.json +28 -0
- package/test/fixtures/stable-surfaces/refusal-review.json +23 -0
- package/test/fixtures/stable-surfaces/registry.json +49 -0
- package/test/fixtures/stable-surfaces/tools-desktop.json +575 -0
- package/test/fixtures/stable-surfaces/tools.json +716 -0
- package/test/fixtures/stable-surfaces/usage.txt +46 -0
- package/test/fixtures/stage2-acp-native.mjs +138 -0
- package/test/fixtures/stage2-app-server.mjs +23 -0
- package/test/fixtures/stage2-catalog.json +91 -0
- package/test/fixtures/stage2-hostile-web.mjs +35 -0
- package/test/fixtures/stage2-native-network.mjs +62 -0
- package/test/fixtures/stage2-worker.ts +87 -0
- package/test/fixtures/status-views/expected.json +7825 -0
- package/test/fixtures/status-views/registries.json +1 -0
- package/test/fixtures/steering.json +209 -0
- package/test/helpers.ts +740 -0
- package/test/host-env.ts +7 -0
- package/test/keychain-decoy.ts +197 -0
- package/test/live-scope.ts +44 -0
- package/test/mediated-network.test.ts +192 -0
- package/test/mediated-proxy.test.ts +264 -0
- package/test/native-temp-alias.ts +10 -0
- package/test/never-policy.test.ts +629 -0
- package/test/never-tools.test.ts +136 -0
- package/test/process-home.ts +18 -0
- package/test/proxy.test.ts +531 -0
- package/test/restart-overlay-live.test.ts +85 -0
- package/test/restart-overlay-profile.test.ts +66 -0
- package/test/restart-overlay.test.ts +416 -0
- package/test/stable-surfaces.test.ts +249 -0
- package/test/stage2-escalation-live.test.ts +544 -0
- package/test/stage2-escalation-qualification.test.ts +170 -0
- package/test/stage2-escalation.test.ts +747 -0
- package/test/stage2-executor.test.ts +849 -0
- package/test/stage2-grant.test.ts +603 -0
- package/test/stage2-native-live.test.ts +647 -0
- package/test/stage2-network-decisions.test.ts +70 -0
- package/test/stage2-probe.test.ts +845 -0
- package/test/stage2-standing-live.test.ts +203 -0
- package/test/stubs/broker-host.mjs +59 -0
- package/test/stubs/broker-peer.mjs +20 -0
- package/test/stubs/confined-harness.mjs +91 -0
- package/test/stubs/ext-broken-server.mjs +2 -0
- package/test/stubs/ext-odd-server.mjs +21 -0
- package/test/stubs/ext-server.mjs +31 -0
- package/test/stubs/fake-host-cli.mjs +218 -0
- package/test/stubs/procargs.mjs +30 -0
- package/test/stubs/stub-acp +4 -0
- package/test/stubs/stub-agent.mjs +322 -0
- package/test/stubs/stub-claude +4 -0
- package/test/stubs/stub-codex +4 -0
- package/test/stubs/targets/acp.mjs +307 -0
- package/test/stubs/targets/claude.mjs +149 -0
- package/test/stubs/targets/codex.mjs +479 -0
- package/test/stubs/targets/gemini.mjs +604 -0
- package/test/temp-root.ts +11 -0
- package/test/wp4-adapters.test.ts +353 -0
- package/test/wp4-interlock.test.ts +136 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// What a session's turn produced, and how its item settles: the result recorded when a turn ends
|
|
2
|
+
// (a job's report and the git half of it, a thread's reply, what the agent was refused, process
|
|
3
|
+
// warnings), recorded with the session's settling in one transaction (finishTurn), the outcome its
|
|
4
|
+
// node ends with, and what is said up front when it is reported
|
|
5
|
+
// (blocked, no_changes, re-checked as the result stands now), and what a job's result showed of its
|
|
6
|
+
// work, which is what merge takes (shownWork). A session borrowed for a discussion keeps its own outcome
|
|
7
|
+
// apart from the discussion's turns (OwnOutcome).
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { refusalNote } from "./refusal.js";
|
|
10
|
+
import { nodeDir } from "./config.js";
|
|
11
|
+
import { ACTIVE, db, getNode, nextQueuedTurn, now, tx, updateNode } from "./db.js";
|
|
12
|
+
import { sessionParams } from "./params.js";
|
|
13
|
+
import { parseReport } from "./prompts.js";
|
|
14
|
+
import { keepRecord } from "./storage.js";
|
|
15
|
+
import { endJobTurn, stillUnchanged } from "./worktree.js";
|
|
16
|
+
/**
|
|
17
|
+
* Records a turn's end for its session, once the turn's agent has ended (runSessionTurn): what the
|
|
18
|
+
* turn produced, and the session going on to its next queued turn or settling. Relay's slow work on
|
|
19
|
+
* the turn comes first (its final message's file, a job's git half), so that a job's commit is in
|
|
20
|
+
* its result before the job shows as settled. Everything the registry is then told is one
|
|
21
|
+
* transaction: a reader, and a call waiting on the turn (db.ts turnSettled), finds the result with
|
|
22
|
+
* the session settled and a failed turn's queued turns dropped, or none of it. A message queued
|
|
23
|
+
* meanwhile is either seen here (it runs next, or is dropped with a failed turn), or finds the
|
|
24
|
+
* session settled and puts it back in the queue.
|
|
25
|
+
*/
|
|
26
|
+
export function finishTurn(node, turn, o, cfg) {
|
|
27
|
+
const made = turnRecord(node, turn, o, cfg);
|
|
28
|
+
tx(() => {
|
|
29
|
+
// A turn that never started is recorded as over here, with its session (runSessionTurn).
|
|
30
|
+
o.close?.();
|
|
31
|
+
recordTurn(node, made, o);
|
|
32
|
+
// With more messages queued the node goes straight on to the next turn. A discussion turn that
|
|
33
|
+
// failed is the discussion's failure: the session's own messages queued behind it still run.
|
|
34
|
+
// Going on, it shows as queued from here until its next turn holds a slot (worker.ts, which
|
|
35
|
+
// does the same before each turn), so that this turn's end shows as recorded (db.ts turnSettled).
|
|
36
|
+
if (o.restartPending || (nextQueuedTurn(node.id) && (o.status === "ok" || made.discussionTurn))) {
|
|
37
|
+
db().prepare("UPDATE nodes SET status = 'queued', ended_at = NULL WHERE id = ? AND status != 'cancelled'").run(node.id);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
settle(node.id, o, made);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Makes ready the record of what a turn produced, once it has ended: its final message, kept whole
|
|
45
|
+
* in a file, and the node's result, with a job's git half. A job's or thread's result is its own
|
|
46
|
+
* work: a reply it wrote in a discussion it was borrowed for belongs to the discussion (which reads
|
|
47
|
+
* it from the turn), not in place of that result, and so does not touch the job's result or own
|
|
48
|
+
* outcome (discussionTurn).
|
|
49
|
+
*/
|
|
50
|
+
function turnRecord(node, turn, o, cfg) {
|
|
51
|
+
// Kept whole in a file (by runSessionTurn, for a turn whose agent ran); where that cannot be
|
|
52
|
+
// written (a full disk, say), whole in the result instead.
|
|
53
|
+
const warnings = [...o.warnings];
|
|
54
|
+
let finalPath = o.finalPath;
|
|
55
|
+
if (finalPath === undefined) {
|
|
56
|
+
const at = path.join(nodeDir(node.run_id, node.id), `final-${turn.id}.md`);
|
|
57
|
+
const problem = keepRecord(at, o.finalMessage, { consequence: "the final message is kept only in the registry" });
|
|
58
|
+
if (problem)
|
|
59
|
+
warnings.push(`${problem}.`);
|
|
60
|
+
finalPath = problem ? null : at;
|
|
61
|
+
}
|
|
62
|
+
const unkept = finalPath === null;
|
|
63
|
+
let result;
|
|
64
|
+
const hasOwnOutcome = node.kind === "job" || node.kind === "thread";
|
|
65
|
+
const discussionTurn = hasOwnOutcome && turn.source.startsWith("discussion:");
|
|
66
|
+
if (node.kind === "job") {
|
|
67
|
+
const cap = 6000;
|
|
68
|
+
result = {
|
|
69
|
+
...parseReport(o.finalMessage),
|
|
70
|
+
final_message: o.finalMessage.length > cap && !unkept ? `${o.finalMessage.slice(0, cap)}\n… (truncated; full text at final_message_path)` : o.finalMessage,
|
|
71
|
+
...(finalPath === null ? {} : { final_message_path: finalPath }),
|
|
72
|
+
};
|
|
73
|
+
// The git half: commit, tip, diff, and whether the turn changed nothing (worktree.ts).
|
|
74
|
+
result = { ...result, ...endJobTurn(node, turn, o, cfg) };
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
result = { reply: o.finalMessage };
|
|
78
|
+
}
|
|
79
|
+
// Warnings about processes stay with the result however the turn ended, and so does what the agent
|
|
80
|
+
// was refused; a warning about the speed it ran at is shown with them.
|
|
81
|
+
warnings.push(...(o.speedWarnings ?? []));
|
|
82
|
+
if (warnings.length)
|
|
83
|
+
result = { ...result, warnings };
|
|
84
|
+
const notes = sessionParams(getNode(node.id)).notes;
|
|
85
|
+
if (notes?.length)
|
|
86
|
+
result = { ...result, notes };
|
|
87
|
+
if (o.denials?.length)
|
|
88
|
+
result = { ...result, blocked: o.denials };
|
|
89
|
+
if (o.refusal)
|
|
90
|
+
result = { ...result, outcome: "refused", refusal: o.refusal, unfinished: true, note: refusalNote(o.refusal), ...(node.worktree ? { worktree: node.worktree } : {}) };
|
|
91
|
+
return { discussionTurn, result, error: o.status === "ok" ? null : (o.error ?? o.status) };
|
|
92
|
+
}
|
|
93
|
+
/** Records what a turn produced (turnRecord) as the node's result and, for a job or thread, its own outcome; nothing for a discussion's turn. */
|
|
94
|
+
function recordTurn(node, made, o) {
|
|
95
|
+
if (made.discussionTurn)
|
|
96
|
+
return;
|
|
97
|
+
updateNode(node.id, { result_json: JSON.stringify(made.result), error: made.error });
|
|
98
|
+
if (node.kind === "job" || node.kind === "thread")
|
|
99
|
+
recordOwnOutcome(node.id, { status: outcomeStatus(o), error: made.error });
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Settles a session whose turns have run out: with its own outcome after a discussion turn (a
|
|
103
|
+
* job or thread borrowed for a discussion), otherwise with its turn's. A turn that did not finish takes the
|
|
104
|
+
* session's queued turns with it, unless it was a discussion's.
|
|
105
|
+
*/
|
|
106
|
+
function settle(nodeId, o, { discussionTurn }) {
|
|
107
|
+
const own = discussionTurn ? ownOutcome(getNode(nodeId)) : undefined;
|
|
108
|
+
if (own)
|
|
109
|
+
finishNode(nodeId, own.status, { error: own.error });
|
|
110
|
+
else
|
|
111
|
+
finishNode(nodeId, outcomeStatus(o));
|
|
112
|
+
if (o.status !== "ok" && !discussionTurn) {
|
|
113
|
+
db().prepare("UPDATE turns SET status = 'cancelled', ended_at = ? WHERE node_id = ? AND status = 'queued'").run(now(), nodeId);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Sets a terminal status unless the node was cancelled in the meantime; other fields always apply. */
|
|
117
|
+
export function finishNode(nodeId, status, fields = {}) {
|
|
118
|
+
updateNode(nodeId, { ...fields, ended_at: now() });
|
|
119
|
+
db().prepare("UPDATE nodes SET status = ? WHERE id = ? AND status IN ('queued', 'running')").run(status, nodeId);
|
|
120
|
+
}
|
|
121
|
+
export function recordOwnOutcome(nodeId, own) {
|
|
122
|
+
// One statement, so that it is atomic inside or outside a transaction.
|
|
123
|
+
db().prepare("UPDATE nodes SET params_json = json_set(COALESCE(params_json, '{}'), '$.own', json(?)) WHERE id = ?").run(JSON.stringify(own), nodeId);
|
|
124
|
+
}
|
|
125
|
+
const ownOutcome = (n) => sessionParams(n).own;
|
|
126
|
+
/**
|
|
127
|
+
* Settles a session left queued with nothing to run: it was requeued for a turn (a discussion's)
|
|
128
|
+
* that was cancelled before it ran. It goes back to how its work last ended: a job to its own
|
|
129
|
+
* outcome (as does a thread), any other session to its latest executed turn's, blocked where that turn finished but
|
|
130
|
+
* its agent was refused something (its denials, as outcomeStatus reads them; review C10/X3).
|
|
131
|
+
*/
|
|
132
|
+
export function settleIdle(nodeId) {
|
|
133
|
+
const n = getNode(nodeId);
|
|
134
|
+
if (!n || n.status !== "queued")
|
|
135
|
+
return;
|
|
136
|
+
if (db().prepare("SELECT 1 FROM turns WHERE node_id = ? AND status IN ('queued', 'running') LIMIT 1").get(nodeId))
|
|
137
|
+
return;
|
|
138
|
+
const hasOwnOutcome = n.kind === "job" || n.kind === "thread";
|
|
139
|
+
const own = hasOwnOutcome ? ownOutcome(n) : undefined;
|
|
140
|
+
const last = db().prepare(`SELECT status, error, denials FROM turns WHERE node_id = ? AND status IN ('done', 'failed', 'refused')${hasOwnOutcome ? " AND source NOT LIKE 'discussion:%'" : ""} ORDER BY ended_at DESC, id DESC LIMIT 1`)
|
|
141
|
+
.get(nodeId);
|
|
142
|
+
const refused = last?.status === "done" && (last.denials ? JSON.parse(last.denials) : []).length > 0;
|
|
143
|
+
const status = own?.status ?? (last?.status === "refused" ? "refused" : last?.status === "failed" ? "failed" : refused ? "blocked" : "done");
|
|
144
|
+
const error = own ? own.error : last?.status === "failed" || last?.status === "refused" ? last.error : null;
|
|
145
|
+
db().prepare("UPDATE nodes SET status = ?, error = ?, ended_at = ? WHERE id = ? AND status = 'queued'").run(status, error, now(), nodeId);
|
|
146
|
+
}
|
|
147
|
+
function outcomeStatus(o) {
|
|
148
|
+
if (o.status === "refused")
|
|
149
|
+
return "refused";
|
|
150
|
+
// The agent says it finished, but its own stream says it was refused something on the way.
|
|
151
|
+
if (o.status === "ok")
|
|
152
|
+
return o.denials?.length ? "blocked" : "done";
|
|
153
|
+
if (o.timedOut)
|
|
154
|
+
return "timeout";
|
|
155
|
+
return o.status === "interrupted" ? "cancelled" : "failed";
|
|
156
|
+
}
|
|
157
|
+
/** True when a job that changed nothing had its worktree and empty branch removed. */
|
|
158
|
+
export const removedBranch = (res) => Boolean(res?.no_changes) && res?.branch === null;
|
|
159
|
+
export function shownWork(job) {
|
|
160
|
+
const res = job.result_json ? JSON.parse(job.result_json) : null;
|
|
161
|
+
if (!res)
|
|
162
|
+
return { unshown: `${job.id} has no result (it is ${job.status}): no turn of it got as far as relay committing its work and writing a diff summary of it` };
|
|
163
|
+
if (typeof res.checkpoint_error === "string") {
|
|
164
|
+
return { unshown: `relay could not commit ${job.id}'s work when the turn its result is from ended (${res.checkpoint_error}), so that result's diff summary does not show the work` };
|
|
165
|
+
}
|
|
166
|
+
if (!("commit" in res))
|
|
167
|
+
return { legacy: true };
|
|
168
|
+
if (typeof res.commit !== "string")
|
|
169
|
+
return { unshown: `relay could not tell which commit ${job.id}'s result showed` };
|
|
170
|
+
return { commit: res.commit };
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A settled job's result as it stands now. no_changes is what its turn's end found; where the
|
|
174
|
+
* worktree was kept because something the job started may still be running in it, that can have
|
|
175
|
+
* written since, so the claim is checked again whenever it is reported.
|
|
176
|
+
*/
|
|
177
|
+
export function resultNow(n, res) {
|
|
178
|
+
if (!res?.no_changes || removedBranch(res) || ACTIVE.includes(n.status) || !n.base_sha)
|
|
179
|
+
return res;
|
|
180
|
+
const withdrawn = (late) => {
|
|
181
|
+
const { no_changes: _, ...rest } = res;
|
|
182
|
+
return { ...rest, warnings: [...(rest.warnings ?? []), late] };
|
|
183
|
+
};
|
|
184
|
+
// Relay commits what is left in a kept worktree before merging it or removing it (merge, clean),
|
|
185
|
+
// and records the branch's tip then (saveTip): a tip past the base is a change, whether or not
|
|
186
|
+
// the worktree is still there to look at (review C8).
|
|
187
|
+
if (n.head_sha && n.head_sha !== n.base_sha) {
|
|
188
|
+
return withdrawn("Its turn ended without changing anything, but its branch has commits since: relay committed what was written in its kept worktree after the turn (before a merge, or in relay clean). Look at the branch.");
|
|
189
|
+
}
|
|
190
|
+
const now_ = stillUnchanged(n);
|
|
191
|
+
if (now_ === true)
|
|
192
|
+
return res;
|
|
193
|
+
return withdrawn("error" in now_
|
|
194
|
+
? `Its turn ended without changing anything, but relay can no longer look at its worktree: ${now_.error}.`
|
|
195
|
+
: "Its turn ended without changing anything, but its worktree has changed since: something it started kept running there. Look at the worktree before merging it.");
|
|
196
|
+
}
|
|
197
|
+
/** What a turn's agent was refused, from the turn's record. */
|
|
198
|
+
export const denialsOf = (t) => (t.denials ? JSON.parse(t.denials) : []);
|
|
199
|
+
export const BLOCKED_NOTE = "The agent finished its turn but was refused something it tried (listed in blocked), so its work may be incomplete.";
|
|
200
|
+
/**
|
|
201
|
+
* What a settled item produced, where that is worth saying up front rather than leaving in its
|
|
202
|
+
* result: a turn whose agent was refused something (blocked), and a write job whose turn finished
|
|
203
|
+
* without changing anything.
|
|
204
|
+
*/
|
|
205
|
+
export function outcomeView(n, res) {
|
|
206
|
+
const out = {};
|
|
207
|
+
const notes = [];
|
|
208
|
+
if (n.status === "refused" && res?.refusal) {
|
|
209
|
+
Object.assign(out, { outcome: "refused", refusal: res.refusal, unfinished: true });
|
|
210
|
+
if (typeof res.note === "string")
|
|
211
|
+
notes.push(res.note);
|
|
212
|
+
}
|
|
213
|
+
if ((n.status === "blocked" || n.status === "refused") && Array.isArray(res?.blocked)) {
|
|
214
|
+
out.blocked = res.blocked;
|
|
215
|
+
notes.push(n.status === "refused" ? "Before its turn stopped, the agent was also refused actions listed in blocked." : `${BLOCKED_NOTE} Its final report should say what it needed: resume it with a grant, or with other instructions.`);
|
|
216
|
+
}
|
|
217
|
+
if (n.kind === "job" && n.mode === "write" && res?.no_changes) {
|
|
218
|
+
out.outcome ??= "no_changes";
|
|
219
|
+
// What relay checked is git's view (commits, uncommitted and untracked files), not every file:
|
|
220
|
+
// one git ignores is not counted (review C16).
|
|
221
|
+
notes.push("The job finished with no changes to merge: its branch is still at its base, with nothing uncommitted or untracked (files git ignores are not counted). Its final report may say why; resume it to try again.");
|
|
222
|
+
}
|
|
223
|
+
return Object.keys(out).length ? { ...out, ...(notes.length ? { outcome_note: notes.join(" ") } : {}) } : null;
|
|
224
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const sessionParams = (n) => JSON.parse(n.params_json ?? "{}");
|
|
2
|
+
const asSpeed = (s) => (s === "fast" || s === "ultrafast" ? s : "standard");
|
|
3
|
+
/**
|
|
4
|
+
* The speed of a session's turn now running or last started (SessionParams.speed), or standard
|
|
5
|
+
* (rows from before speeds, and anything relay did not write). What a launched agent's own turn
|
|
6
|
+
* runs at. The speed a call continuing the session goes on at is db.ts speedAfter.
|
|
7
|
+
*/
|
|
8
|
+
export function speedOf(n) {
|
|
9
|
+
return asSpeed(sessionParams(n).speed);
|
|
10
|
+
}
|
|
11
|
+
/** The speeds of a session's queued turns that calls were admitted at (SessionParams.speed_queued), by turn id, in turn order. */
|
|
12
|
+
export function queuedSpeeds(n) {
|
|
13
|
+
const q = sessionParams(n).speed_queued;
|
|
14
|
+
if (!q || typeof q !== "object")
|
|
15
|
+
return [];
|
|
16
|
+
return Object.entries(q).map(([t, s]) => [Number(t), asSpeed(s)]).filter(([t]) => Number.isInteger(t)).sort((a, b) => a[0] - b[0]);
|
|
17
|
+
}
|
|
18
|
+
/** The change to a session's speed that waits on the turn `turnId` reaching the model (SessionParams.speed_change), or null. */
|
|
19
|
+
export function speedChange(n, turnId) {
|
|
20
|
+
const c = sessionParams(n).speed_change;
|
|
21
|
+
return c && c.turn === turnId ? { from: c.from, to: c.to } : null;
|
|
22
|
+
}
|
|
23
|
+
export const discussionParams = (n) => JSON.parse(n.params_json);
|
|
24
|
+
export const reviewParams = (n) => JSON.parse(n.params_json);
|
|
25
|
+
/**
|
|
26
|
+
* What a consent to local network is bound to: the parts of the grant that decide what the job's
|
|
27
|
+
* shell can reach or write (session.ts localNetworkConsent). A grant that changes any of them is
|
|
28
|
+
* asked about again.
|
|
29
|
+
*/
|
|
30
|
+
export function localNetworkKey(grant) {
|
|
31
|
+
return JSON.stringify({ network: grant.network === true, local_network: grant.local_network === true, write_paths: [...(grant.write_paths ?? [])].sort() });
|
|
32
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Whether a person is at this terminal: the structural check behind `relay approve` (and the other
|
|
2
|
+
// commands that act with the person's authority). It does not read the command that started
|
|
3
|
+
// relay, so how a shell spelt it does not matter. Design and residual risks: docs/approve-boundary.md.
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { randomInt } from "node:crypto";
|
|
6
|
+
import readline from "node:readline";
|
|
7
|
+
import { db, launchLeaders } from "./db.js";
|
|
8
|
+
const normal = (start) => start.replace(/\s+/g, " ").trim();
|
|
9
|
+
/** The environment variables relay sets on every launched agent (backends/env.ts, backends/types.ts relayServerCommand). */
|
|
10
|
+
export const LAUNCH_MARKERS = ["RELAY_RUN_ID", "RELAY_NODE_ID", "RELAY_DEPTH", "RELAY_INJECTED"];
|
|
11
|
+
export function launchMarker(env = process.env) {
|
|
12
|
+
return LAUNCH_MARKERS.find((k) => env[k] !== undefined) ?? null;
|
|
13
|
+
}
|
|
14
|
+
/** One process-table snapshot: pid, parent, process group and start time. Null where ps cannot say. */
|
|
15
|
+
function processTable() {
|
|
16
|
+
const r = spawnSync("/bin/ps", ["-axo", "pid=,ppid=,pgid=,lstart="], { encoding: "utf8", maxBuffer: 32 * 1024 * 1024, env: { PATH: "/usr/bin:/bin", LC_ALL: "C", LANG: "C" } });
|
|
17
|
+
if (r.status !== 0 || !r.stdout)
|
|
18
|
+
return null;
|
|
19
|
+
const table = new Map();
|
|
20
|
+
for (const line of r.stdout.split("\n")) {
|
|
21
|
+
const m = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.+?)\s*$/.exec(line);
|
|
22
|
+
if (m)
|
|
23
|
+
table.set(Number(m[1]), { pid: Number(m[1]), parent: Number(m[2]), pgid: Number(m[3]), start: normal(m[4]) });
|
|
24
|
+
}
|
|
25
|
+
return table.size ? table : null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* This process's session id. Node has no getsid and macOS ps does not print one, so the system
|
|
29
|
+
* Perl (which relay already relies on, confine/peer.ts) asks the kernel: Darwin's SYS_getsid is 310.
|
|
30
|
+
* The Perl child is in this process's session. Null where it cannot say.
|
|
31
|
+
*/
|
|
32
|
+
function sessionId() {
|
|
33
|
+
if (process.platform !== "darwin")
|
|
34
|
+
return null;
|
|
35
|
+
const r = spawnSync("/usr/bin/perl", ["-e", "my $s = syscall(310, 0); print $s"], { encoding: "utf8", timeout: 5000, env: { PATH: "/usr/bin:/bin" } });
|
|
36
|
+
const sid = Number(r.stdout);
|
|
37
|
+
return r.status === 0 && Number.isInteger(sid) && sid > 0 ? sid : null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The processes relay launched or runs for launched agents, from the registry this command would
|
|
41
|
+
* decide in: each node's worker and agent while recorded, and the registered workers and child
|
|
42
|
+
* relay servers (relay_procs). Identified by pid and start time, so a number the system has given
|
|
43
|
+
* to another process since is not mistaken for one of them.
|
|
44
|
+
*/
|
|
45
|
+
function launchedProcesses() {
|
|
46
|
+
const out = new Map();
|
|
47
|
+
const add = (pid, start, what) => { if (pid && pid > 1)
|
|
48
|
+
out.set(pid, { start: start ? normal(start) : null, what }); };
|
|
49
|
+
for (const n of db().prepare("SELECT id, worker_pid, worker_start, agent_pid, agent_start FROM nodes WHERE worker_pid > 1 OR agent_pid > 1").all()) {
|
|
50
|
+
add(n.worker_pid, n.worker_start, `relay's worker for ${n.id}`);
|
|
51
|
+
add(n.agent_pid, n.agent_start, `the agent of ${n.id}`);
|
|
52
|
+
}
|
|
53
|
+
for (const p of db().prepare("SELECT pid, start, kind, node_id FROM relay_procs WHERE kind = 'worker' OR node_id IS NOT NULL").all()) {
|
|
54
|
+
add(p.pid, p.start, p.kind === "worker" ? `relay's worker${p.node_id ? ` for ${p.node_id}` : ""}` : `the relay server of ${p.node_id}`);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The process-group / session leaders relay launched, by leader pid, each with what it is and the
|
|
60
|
+
* leader's start time when recorded. Kept for the run's lifetime (db.ts launch_leaders), so these
|
|
61
|
+
* match even after the leader has died.
|
|
62
|
+
*/
|
|
63
|
+
function leaders() {
|
|
64
|
+
// Every recorded leader at each pid, across runs and within one: a pid can have led more than once.
|
|
65
|
+
const out = new Map();
|
|
66
|
+
for (const l of launchLeaders()) {
|
|
67
|
+
if (l.leader_pid <= 1)
|
|
68
|
+
continue;
|
|
69
|
+
const entry = { what: l.kind === "worker" ? `relay's worker${l.node_id ? ` for ${l.node_id}` : ""}` : `the agent of ${l.node_id ?? "a launched job"}`, start: l.start ? normal(l.start) : null };
|
|
70
|
+
out.set(l.leader_pid, [...(out.get(l.leader_pid) ?? []), entry]);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Why this process is (or may be) inside a relay-launched agent's process tree, or null. Checked
|
|
76
|
+
* against what relay recorded, whichever of these holds:
|
|
77
|
+
* - an environment marker relay sets on launched agents;
|
|
78
|
+
* - an ancestor, by parentage, is a process relay launched (launchedProcesses), matched by pid and
|
|
79
|
+
* start time while it is alive;
|
|
80
|
+
* - this process's group or session is one relay launched (launch_leaders, kept for the run's
|
|
81
|
+
* lifetime). Workers and agents are started detached, each leading its own session and group,
|
|
82
|
+
* which their descendants keep even once the leader has died and they are reparented to launchd.
|
|
83
|
+
* The group/session id is a bare number, so it matches the recorded leader id even when the
|
|
84
|
+
* leader is gone — unless a live process now holds that pid with a start time matching none of
|
|
85
|
+
* those recorded for it (and none is unknown), which means the old group/session is gone and the
|
|
86
|
+
* pid was reused: XNU never allocates a pid that is
|
|
87
|
+
* still in use as a process-group or session id (bsd/kern/kern_fork.c `forkproc`, the
|
|
88
|
+
* "Find an unused PID" loop skips a candidate when `pghash_exists_locked(pid)` or
|
|
89
|
+
* `session_find_locked(pid)` holds), so a reallocated pid proves the leader's group/session ended.
|
|
90
|
+
* An unreadable process table or session id counts as inside: what relay cannot inspect is not
|
|
91
|
+
* taken for the person's own terminal.
|
|
92
|
+
*/
|
|
93
|
+
const realProbe = { table: processTable, sid: sessionId };
|
|
94
|
+
export function launchedTree(env = process.env, probe = realProbe) {
|
|
95
|
+
const marker = launchMarker(env);
|
|
96
|
+
if (marker)
|
|
97
|
+
return `its environment carries ${marker}, which relay sets on the agents it launches`;
|
|
98
|
+
const table = probe.table();
|
|
99
|
+
if (!table)
|
|
100
|
+
return "relay could not read the process table to tell whether it runs inside a launched agent's process tree";
|
|
101
|
+
const sid = probe.sid();
|
|
102
|
+
if (sid === null)
|
|
103
|
+
return "relay could not read its session id to tell whether it runs inside a launched agent's process tree";
|
|
104
|
+
const self = probe.self ?? process.pid;
|
|
105
|
+
const launched = launchedProcesses();
|
|
106
|
+
const recordedLeaders = leaders();
|
|
107
|
+
if (!launched.size && !recordedLeaders.size)
|
|
108
|
+
return null;
|
|
109
|
+
// Ancestry: a live launched process, matched by pid and start time.
|
|
110
|
+
const liveAncestor = (pid) => {
|
|
111
|
+
const known = launched.get(pid), row = table.get(pid);
|
|
112
|
+
return known && row && (!known.start || known.start === row.start) ? known.what : null;
|
|
113
|
+
};
|
|
114
|
+
const seen = new Set();
|
|
115
|
+
for (let pid = table.get(self)?.parent ?? process.ppid; pid > 1 && !seen.has(pid); pid = table.get(pid)?.parent ?? 0) {
|
|
116
|
+
seen.add(pid);
|
|
117
|
+
const what = liveAncestor(pid);
|
|
118
|
+
if (what)
|
|
119
|
+
return `it was started inside the process tree of ${what} (pid ${pid})`;
|
|
120
|
+
}
|
|
121
|
+
// Group and session: a recorded leader id. A live process at that pid whose start matches no
|
|
122
|
+
// recorded start means the pid was reused and the old group/session is gone (XNU note above).
|
|
123
|
+
// It matches when nothing is alive at that pid, when the live process's start equals any start
|
|
124
|
+
// recorded for the pid, or when any recorded start is unknown — so no recorded launched leader
|
|
125
|
+
// that is still alive can be missed.
|
|
126
|
+
const byLeader = (id) => {
|
|
127
|
+
const entries = recordedLeaders.get(id);
|
|
128
|
+
if (!entries?.length)
|
|
129
|
+
return null;
|
|
130
|
+
const row = table.get(id);
|
|
131
|
+
if (!row)
|
|
132
|
+
return entries[0].what;
|
|
133
|
+
return entries.find((e) => e.start === null || e.start === row.start)?.what ?? null;
|
|
134
|
+
};
|
|
135
|
+
const group = table.get(self)?.pgid;
|
|
136
|
+
const byGroup = group ? byLeader(group) : null;
|
|
137
|
+
if (byGroup)
|
|
138
|
+
return `it is in the process group of ${byGroup} (leader pid ${group})`;
|
|
139
|
+
const bySession = byLeader(sid);
|
|
140
|
+
if (bySession)
|
|
141
|
+
return `it is in the session of ${bySession} (leader pid ${sid})`;
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
// No 0/O or 1/I/L: the person reads it off the screen and types it.
|
|
145
|
+
const CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
|
|
146
|
+
export function oneTimeCode(length = 6) {
|
|
147
|
+
return Array.from({ length }, () => CODE_ALPHABET[randomInt(CODE_ALPHABET.length)]).join("");
|
|
148
|
+
}
|
|
149
|
+
/** Null when stdin and stdout are both an interactive terminal; otherwise why not. */
|
|
150
|
+
export function terminalProblem() {
|
|
151
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
152
|
+
return `standard ${!process.stdin.isTTY ? "input" : "output"} is not an interactive terminal`;
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Shows `review`, prints a fresh code for this one request and waits for one line. True only when
|
|
158
|
+
* the line is that code (case and surrounding spaces aside). The code lives in this process alone,
|
|
159
|
+
* for this request: it cannot be reused for another request or another run of the command.
|
|
160
|
+
*/
|
|
161
|
+
export async function personTypesCode(requestId, review, code = oneTimeCode()) {
|
|
162
|
+
for (const line of review)
|
|
163
|
+
process.stdout.write(`${line}\n`);
|
|
164
|
+
process.stdout.write(`To allow ${requestId}, type the code ${code} and press Return. Anything else leaves it pending: `);
|
|
165
|
+
const input = readline.createInterface({ input: process.stdin, terminal: false });
|
|
166
|
+
let answer = null;
|
|
167
|
+
for await (const line of input) {
|
|
168
|
+
answer = line;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
input.close();
|
|
172
|
+
process.stdout.write("\n");
|
|
173
|
+
return answer !== null && answer.trim().toUpperCase() === code;
|
|
174
|
+
}
|