opencode-agent-orchestration-kit 1.0.38 → 1.0.40
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 +12 -3
- package/docker/open-design/Dockerfile +3 -1
- package/docker/open-design/entrypoint.mjs +61 -0
- package/docs/docker-open-design.md +5 -0
- package/docs/models.md +1 -1
- package/docs/open-design.md +8 -0
- package/docs/performance/native-tools-benchmark.md +33 -0
- package/docs/releases/v1.0.39.md +44 -0
- package/docs/releases/v1.0.40.md +55 -0
- package/docs/sync-boundary.md +27 -0
- package/opencode/commands/loop-status.md +24 -0
- package/opencode/commands/loop.md +8 -0
- package/opencode/docs/ai/harness/agents.md +8 -0
- package/opencode/docs/ai/harness/checks.md +6 -0
- package/opencode/docs/ai/harness/commands.md +10 -0
- package/opencode/docs/ai/harness/orchestration-contracts.json +9 -1
- package/opencode/docs/ai/specs/mission-runtime.md +44 -0
- package/opencode/plugins/mission-runtime.test.mjs +46 -0
- package/opencode/plugins/mission-runtime.ts +41 -0
- package/opencode/scripts/check-harness.mjs +111 -0
- package/opencode/scripts/check-harness.test.mjs +37 -1
- package/opencode/scripts/loop-state.mjs +5 -0
- package/opencode/scripts/loop-state.test.mjs +13 -0
- package/opencode/scripts/mission-runtime-observer.d.mts +25 -0
- package/opencode/scripts/mission-runtime-observer.mjs +124 -0
- package/opencode/scripts/mission-runtime.test.mjs +97 -0
- package/opencode/scripts/mission-status.mjs +136 -0
- package/opencode/scripts/mission-status.test.mjs +196 -0
- package/opencode/scripts/project-capability-snapshot.mjs +2 -1
- package/opencode/scripts/project-capability-snapshot.test.mjs +17 -0
- package/opencode/scripts/runtime-permission-policy.mjs +72 -0
- package/opencode/scripts/runtime-permission-policy.test.mjs +89 -0
- package/opencode/tools/open-design-http.d.mts +45 -0
- package/opencode/tools/open-design-http.mjs +387 -0
- package/opencode/tools/open_design.ts +2 -103
- package/package.json +3 -3
- package/scripts/benchmark-tool-paths.mjs +226 -0
- package/scripts/benchmark-tool-paths.test.mjs +29 -0
- package/scripts/check-public-boundary.mjs +89 -0
- package/scripts/check-public-boundary.test.mjs +32 -0
- package/scripts/check.sh +17 -10
- package/scripts/install-smoke.sh +17 -0
- package/scripts/manage-installation.mjs +90 -7
- package/scripts/manage-installation.test.mjs +36 -0
- package/scripts/open-design-entrypoint.test.mjs +54 -0
- package/scripts/open-design-http.test.mjs +211 -0
- package/scripts/package-smoke.mjs +52 -3
- package/scripts/package-smoke.test.mjs +6 -6
- package/scripts/version.test.mjs +2 -2
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { execSync } from "node:child_process";
|
|
5
|
+
import {
|
|
6
|
+
AGENT_IDS as RUNTIME_AGENT_IDS,
|
|
7
|
+
AGENT_POLICIES,
|
|
8
|
+
POLICY_OPERATIONS,
|
|
9
|
+
decideToolAccess,
|
|
10
|
+
} from "./runtime-permission-policy.mjs";
|
|
5
11
|
|
|
6
12
|
const root = process.cwd();
|
|
7
13
|
const errors = [];
|
|
@@ -2913,6 +2919,108 @@ function checkShellExportGuardContract() {
|
|
|
2913
2919
|
}
|
|
2914
2920
|
}
|
|
2915
2921
|
|
|
2922
|
+
function checkRuntimePermissionPolicy() {
|
|
2923
|
+
const actualAgentIds = listMarkdown("agents")
|
|
2924
|
+
.map(stripMarkdownExtension)
|
|
2925
|
+
.sort();
|
|
2926
|
+
const expectedAgentIds = [...RUNTIME_AGENT_IDS].sort();
|
|
2927
|
+
if (JSON.stringify(actualAgentIds) !== JSON.stringify(expectedAgentIds)) {
|
|
2928
|
+
fail("runtime permission policy: agent catalog must match configured agents");
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
for (const agentId of RUNTIME_AGENT_IDS) {
|
|
2932
|
+
const policy = AGENT_POLICIES[agentId];
|
|
2933
|
+
if (!policy) {
|
|
2934
|
+
fail(`runtime permission policy: missing policy for ${agentId}`);
|
|
2935
|
+
continue;
|
|
2936
|
+
}
|
|
2937
|
+
for (const operation of POLICY_OPERATIONS) {
|
|
2938
|
+
const decision = operation === "delegate"
|
|
2939
|
+
? decideToolAccess({ agentId, operation, target: "unknown" })
|
|
2940
|
+
: decideToolAccess({ agentId, operation });
|
|
2941
|
+
if (!["allow", "ask", "deny"].includes(decision)) {
|
|
2942
|
+
fail(`runtime permission policy: invalid ${agentId}/${operation} decision`);
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
const rel = `agents/${agentId}.md`;
|
|
2947
|
+
if (!exists(rel)) continue;
|
|
2948
|
+
const declared = parsePortableAgentPermissions(rel);
|
|
2949
|
+
if (declared.repositoryWrite !== policy.repository_write) {
|
|
2950
|
+
fail(`runtime permission policy: ${agentId} repository_write diverges from frontmatter`);
|
|
2951
|
+
}
|
|
2952
|
+
if (declared.taskDefault !== policy.task_default) {
|
|
2953
|
+
fail(`runtime permission policy: ${agentId} task_default diverges from frontmatter`);
|
|
2954
|
+
}
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
if (decideToolAccess({ agentId: "unknown", operation: "write" }) !== "deny") {
|
|
2958
|
+
fail("runtime permission policy: unknown agent must deny");
|
|
2959
|
+
}
|
|
2960
|
+
if (decideToolAccess({ agentId: "developer", operation: "unknown" }) !== "deny") {
|
|
2961
|
+
fail("runtime permission policy: unknown operation must deny");
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
|
|
2965
|
+
function checkOpenDesignToolContract() {
|
|
2966
|
+
const rel = "tools/open_design.ts";
|
|
2967
|
+
if (!exists(rel)) {
|
|
2968
|
+
fail(`${rel}: missing Open Design tool`);
|
|
2969
|
+
return;
|
|
2970
|
+
}
|
|
2971
|
+
|
|
2972
|
+
const source = read(rel);
|
|
2973
|
+
for (const [label, present] of [
|
|
2974
|
+
["base URL environment", source.includes("OPEN_DESIGN_URL")],
|
|
2975
|
+
["base URL argument", source.includes("baseUrl: tool.schema.string().optional()")],
|
|
2976
|
+
["trailing slash normalization", source.includes('raw.replace(/\\/+$/, "")')],
|
|
2977
|
+
["project-path guard", source.includes("projects(?:\\/|$)")],
|
|
2978
|
+
]) {
|
|
2979
|
+
if (!present) fail(`${rel}: missing Open Design ${label}`);
|
|
2980
|
+
}
|
|
2981
|
+
|
|
2982
|
+
if (/randomUUID|node:crypto/.test(source)) {
|
|
2983
|
+
fail(`${rel}: Open Design tool must not depend on randomUUID or node:crypto`);
|
|
2984
|
+
}
|
|
2985
|
+
if (/juancanas|synology|\/Users\/|\/home\//i.test(source)) {
|
|
2986
|
+
fail(`${rel}: Open Design tool contains a private endpoint or local path`);
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2990
|
+
function checkMissionRuntimeContract() {
|
|
2991
|
+
const required = [
|
|
2992
|
+
"scripts/mission-status.mjs",
|
|
2993
|
+
"scripts/mission-runtime-observer.mjs",
|
|
2994
|
+
"scripts/mission-runtime.test.mjs",
|
|
2995
|
+
"plugins/mission-runtime.ts",
|
|
2996
|
+
"plugins/mission-runtime.test.mjs",
|
|
2997
|
+
"commands/loop-status.md",
|
|
2998
|
+
"docs/ai/specs/mission-runtime.md",
|
|
2999
|
+
];
|
|
3000
|
+
for (const rel of required) {
|
|
3001
|
+
if (!exists(rel)) fail(`${rel}: missing mission runtime surface`);
|
|
3002
|
+
}
|
|
3003
|
+
if (!required.every(exists)) return;
|
|
3004
|
+
|
|
3005
|
+
const status = read("scripts/mission-status.mjs");
|
|
3006
|
+
for (const token of ["inspectLoopState", "readLoopHistory", "--json", "next_action"]) {
|
|
3007
|
+
if (!status.includes(token)) fail(`${required[0]}: missing mission status token ${token}`);
|
|
3008
|
+
}
|
|
3009
|
+
|
|
3010
|
+
const observer = read("scripts/mission-runtime-observer.mjs");
|
|
3011
|
+
for (const token of ["session.status", "session.compacted", "session.error", "child_session_ids"]) {
|
|
3012
|
+
if (!observer.includes(token)) fail(`${required[1]}: missing observer token ${token}`);
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
const plugin = read("plugins/mission-runtime.ts");
|
|
3016
|
+
for (const token of ["event:", "chat.message", "showToast", "mission-runtime-observer.mjs"]) {
|
|
3017
|
+
if (!plugin.includes(token)) fail(`${required[3]}: missing plugin token ${token}`);
|
|
3018
|
+
}
|
|
3019
|
+
if (/writeFile|appendFile|rename|\.opencode\/loops/.test(`${observer}\n${plugin}`)) {
|
|
3020
|
+
fail("mission runtime observer: must not write durable loop state");
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
|
|
2916
3024
|
checkConfig();
|
|
2917
3025
|
checkAgentsIndex();
|
|
2918
3026
|
checkFrontmatter();
|
|
@@ -2953,6 +3061,9 @@ checkHitlDurableContract();
|
|
|
2953
3061
|
checkRoutingDeclarativoContract();
|
|
2954
3062
|
checkRetryPoliciesContract();
|
|
2955
3063
|
checkShellExportGuardContract();
|
|
3064
|
+
checkRuntimePermissionPolicy();
|
|
3065
|
+
checkOpenDesignToolContract();
|
|
3066
|
+
checkMissionRuntimeContract();
|
|
2956
3067
|
|
|
2957
3068
|
if (errors.length > 0) {
|
|
2958
3069
|
console.error("Harness check failed:");
|
|
@@ -17,7 +17,7 @@ function makeFixture() {
|
|
|
17
17
|
const rel = path.relative(root, source);
|
|
18
18
|
if (rel === ".git" || rel.startsWith(`.git${path.sep}`)) return false;
|
|
19
19
|
if (rel === ".codegraph" || rel.startsWith(`.codegraph${path.sep}`)) return false;
|
|
20
|
-
if (rel
|
|
20
|
+
if (rel.split(path.sep).includes("node_modules")) return false;
|
|
21
21
|
return true;
|
|
22
22
|
},
|
|
23
23
|
});
|
|
@@ -107,6 +107,42 @@ test("shell export guard surfaces are required", () => {
|
|
|
107
107
|
}
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
+
for (const [label, mutate, diagnostic] of [
|
|
111
|
+
[
|
|
112
|
+
"missing the Open Design project URL guard",
|
|
113
|
+
(source) => source.replace(
|
|
114
|
+
'if (/\\/projects(?:\\/|$)/.test(url)) {',
|
|
115
|
+
"if (false) {",
|
|
116
|
+
),
|
|
117
|
+
/tools\/open_design\.ts: missing Open Design project-path guard/,
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
"using randomUUID",
|
|
121
|
+
(source) => `${source}\nconst randomUUID = true\n`,
|
|
122
|
+
/tools\/open_design\.ts: Open Design tool must not depend on randomUUID/,
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
"containing a private endpoint",
|
|
126
|
+
(source) => `${source}\nconst endpoint = "https://private.synology.example"\n`,
|
|
127
|
+
/tools\/open_design\.ts: Open Design tool contains a private endpoint or local path/,
|
|
128
|
+
],
|
|
129
|
+
]) {
|
|
130
|
+
test(`Open Design contract rejects ${label}`, () => {
|
|
131
|
+
const cwd = makeFixture();
|
|
132
|
+
try {
|
|
133
|
+
const rel = "tools/open_design.ts";
|
|
134
|
+
const source = fs.readFileSync(path.join(cwd, rel), "utf8");
|
|
135
|
+
write(rel, mutate(source), cwd);
|
|
136
|
+
|
|
137
|
+
const result = runHarness(cwd);
|
|
138
|
+
assert.notEqual(result.status, 0, `checker accepted Open Design source ${label}`);
|
|
139
|
+
assert.match(result.stderr, diagnostic);
|
|
140
|
+
} finally {
|
|
141
|
+
fs.rmSync(cwd, { recursive: true, force: true });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
110
146
|
test("orchestration contract file must be regular and non-symlink", () => {
|
|
111
147
|
const cwd = makeFixture();
|
|
112
148
|
const external = path.join(cwd, "external-contract.json");
|
|
@@ -787,6 +787,11 @@ export function inspectLoopState({ root, slug }) {
|
|
|
787
787
|
return structuredClone(state);
|
|
788
788
|
}
|
|
789
789
|
|
|
790
|
+
export function readLoopHistory({ root, slug }) {
|
|
791
|
+
const paths = pathsFor(root, slug);
|
|
792
|
+
return structuredClone(readHistory(paths).events);
|
|
793
|
+
}
|
|
794
|
+
|
|
790
795
|
export function acquireLoop({
|
|
791
796
|
root,
|
|
792
797
|
slug,
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
initLoopState,
|
|
13
13
|
inspectLoopState,
|
|
14
14
|
migrateLoopState,
|
|
15
|
+
readLoopHistory,
|
|
15
16
|
recordLoopAction,
|
|
16
17
|
releaseLoop,
|
|
17
18
|
repairLoopState,
|
|
@@ -588,6 +589,18 @@ test("exposes the durable operations through the portable CLI", () => {
|
|
|
588
589
|
});
|
|
589
590
|
});
|
|
590
591
|
|
|
592
|
+
test("readLoopHistory returns validated append-only events without changing state", () => {
|
|
593
|
+
withRepo(({ root, contractPath }) => {
|
|
594
|
+
const before = initExample(root, contractPath, 1);
|
|
595
|
+
const history = readLoopHistory({ root, slug: "example" });
|
|
596
|
+
|
|
597
|
+
assert.equal(history.length, 1);
|
|
598
|
+
assert.equal(history[0].type, "initialized");
|
|
599
|
+
assert.equal(history[0].state_after.last_action_id, before.last_action_id);
|
|
600
|
+
assert.deepEqual(inspectLoopState({ root, slug: "example" }), before);
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
591
604
|
test("rejects a symlinked state ancestor that escapes the root", () => {
|
|
592
605
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-loop-root-"));
|
|
593
606
|
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-loop-outside-"));
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type MissionActivity = "running" | "idle" | "blocked";
|
|
2
|
+
|
|
3
|
+
export type MissionObservation = {
|
|
4
|
+
activity: MissionActivity;
|
|
5
|
+
event_type: string;
|
|
6
|
+
session_id: string;
|
|
7
|
+
event_count: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type MissionObserver = {
|
|
11
|
+
observe(event: unknown): Promise<MissionObservation | { ignored: string; session_id?: string }>;
|
|
12
|
+
stats(): {
|
|
13
|
+
root_session_id?: string;
|
|
14
|
+
child_session_ids: string[];
|
|
15
|
+
event_count: number;
|
|
16
|
+
unique_event_count: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function createMissionObserver(options?: {
|
|
21
|
+
notify?: (observation: MissionObservation) => Promise<void>;
|
|
22
|
+
log?: (message: string) => void;
|
|
23
|
+
}): MissionObserver;
|
|
24
|
+
|
|
25
|
+
export const ACTIVITIES: ReadonlySet<MissionActivity>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const ACTIVITIES = new Set(["running", "idle", "blocked"]);
|
|
2
|
+
|
|
3
|
+
function isObject(value) {
|
|
4
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function stable(value) {
|
|
8
|
+
if (Array.isArray(value)) return value.map(stable);
|
|
9
|
+
if (!isObject(value)) return value;
|
|
10
|
+
return Object.fromEntries(
|
|
11
|
+
Object.keys(value).sort().map((key) => [key, stable(value[key])]),
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function sessionIdFrom(event) {
|
|
16
|
+
const properties = event?.properties;
|
|
17
|
+
if (typeof properties?.sessionID === "string" && properties.sessionID) {
|
|
18
|
+
return properties.sessionID;
|
|
19
|
+
}
|
|
20
|
+
if (typeof properties?.info?.id === "string" && properties.info.id) {
|
|
21
|
+
return properties.info.id;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function activityFrom(event) {
|
|
27
|
+
const type = event?.type;
|
|
28
|
+
if (type === "session.status") {
|
|
29
|
+
const status = event.properties?.status;
|
|
30
|
+
if (status?.type === "idle") return "idle";
|
|
31
|
+
if (status?.type === "busy" || status?.type === "retry") return "running";
|
|
32
|
+
}
|
|
33
|
+
if (type === "session.idle") return "idle";
|
|
34
|
+
if (type === "session.error") return "blocked";
|
|
35
|
+
if (
|
|
36
|
+
type === "tool.execute.before"
|
|
37
|
+
|| type === "tool.execute.after"
|
|
38
|
+
|| type === "session.compacted"
|
|
39
|
+
|| type === "chat.message"
|
|
40
|
+
) {
|
|
41
|
+
return "running";
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function eventKey(event, sessionId) {
|
|
47
|
+
return JSON.stringify(stable({
|
|
48
|
+
type: event?.type ?? null,
|
|
49
|
+
session_id: sessionId ?? null,
|
|
50
|
+
properties: event?.properties ?? null,
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createMissionObserver({ notify = async () => {}, log = () => {} } = {}) {
|
|
55
|
+
const childSessions = new Set();
|
|
56
|
+
const seenEvents = new Set();
|
|
57
|
+
let rootSessionId;
|
|
58
|
+
let eventCount = 0;
|
|
59
|
+
|
|
60
|
+
const observe = async (event) => {
|
|
61
|
+
eventCount += 1;
|
|
62
|
+
const properties = isObject(event?.properties) ? event.properties : {};
|
|
63
|
+
const info = properties.info;
|
|
64
|
+
if (isObject(info) && typeof info.id === "string" && info.parentID) {
|
|
65
|
+
childSessions.add(info.id);
|
|
66
|
+
} else if (
|
|
67
|
+
event?.type === "session.created"
|
|
68
|
+
&& isObject(info)
|
|
69
|
+
&& typeof info.id === "string"
|
|
70
|
+
&& !info.parentID
|
|
71
|
+
&& !rootSessionId
|
|
72
|
+
) {
|
|
73
|
+
rootSessionId = info.id;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const sessionId = sessionIdFrom(event);
|
|
77
|
+
if (sessionId && childSessions.has(sessionId)) {
|
|
78
|
+
return { ignored: "child_session", session_id: sessionId };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const key = eventKey(event, sessionId);
|
|
82
|
+
if (seenEvents.has(key)) {
|
|
83
|
+
return { ignored: "duplicate", session_id: sessionId };
|
|
84
|
+
}
|
|
85
|
+
seenEvents.add(key);
|
|
86
|
+
|
|
87
|
+
const activity = activityFrom(event);
|
|
88
|
+
if (!activity || !sessionId) {
|
|
89
|
+
return { ignored: "unobservable", session_id: sessionId };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const observation = {
|
|
93
|
+
activity,
|
|
94
|
+
event_type: event.type,
|
|
95
|
+
session_id: sessionId,
|
|
96
|
+
event_count: eventCount,
|
|
97
|
+
};
|
|
98
|
+
try {
|
|
99
|
+
await notify(observation);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
try {
|
|
102
|
+
log(`[mission-runtime] notification failed: ${error instanceof Error ? error.name : "unknown"}`);
|
|
103
|
+
} catch {
|
|
104
|
+
// Logging is best effort and must not affect the loop.
|
|
105
|
+
}
|
|
106
|
+
return { ...observation, notification_error: true };
|
|
107
|
+
}
|
|
108
|
+
return observation;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
observe,
|
|
113
|
+
stats() {
|
|
114
|
+
return {
|
|
115
|
+
root_session_id: rootSessionId,
|
|
116
|
+
child_session_ids: [...childSessions].sort(),
|
|
117
|
+
event_count: eventCount,
|
|
118
|
+
unique_event_count: seenEvents.size,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { ACTIVITIES };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import test from "node:test";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
import { createMissionObserver } from "./mission-runtime-observer.mjs";
|
|
8
|
+
|
|
9
|
+
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
10
|
+
|
|
11
|
+
function rootCreated(id = "root-1") {
|
|
12
|
+
return {
|
|
13
|
+
type: "session.created",
|
|
14
|
+
properties: { info: { id, parentID: null } },
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function status(sessionID, type) {
|
|
19
|
+
return {
|
|
20
|
+
type: "session.status",
|
|
21
|
+
properties: { sessionID, status: { type } },
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test("derives running to idle without touching durable loop state", async () => {
|
|
26
|
+
const observations = [];
|
|
27
|
+
const observer = createMissionObserver({ notify: async value => observations.push(value) });
|
|
28
|
+
|
|
29
|
+
await observer.observe(rootCreated());
|
|
30
|
+
await observer.observe(status("root-1", "busy"));
|
|
31
|
+
await observer.observe({ type: "session.idle", properties: { sessionID: "root-1" } });
|
|
32
|
+
|
|
33
|
+
assert.deepEqual(observations.map(item => item.activity), ["running", "idle"]);
|
|
34
|
+
assert.equal(observer.stats().root_session_id, "root-1");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("maps errors to blocked and compaction back to running for the same session", async () => {
|
|
38
|
+
const observations = [];
|
|
39
|
+
const observer = createMissionObserver({ notify: async value => observations.push(value) });
|
|
40
|
+
|
|
41
|
+
await observer.observe(rootCreated("root-2"));
|
|
42
|
+
await observer.observe({ type: "session.error", properties: { sessionID: "root-2" } });
|
|
43
|
+
await observer.observe({ type: "session.compacted", properties: { sessionID: "root-2" } });
|
|
44
|
+
|
|
45
|
+
assert.deepEqual(observations.map(item => [item.activity, item.session_id]), [
|
|
46
|
+
["blocked", "root-2"],
|
|
47
|
+
["running", "root-2"],
|
|
48
|
+
]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("ignores child sessions and duplicate events", async () => {
|
|
52
|
+
const observations = [];
|
|
53
|
+
const observer = createMissionObserver({ notify: async value => observations.push(value) });
|
|
54
|
+
const childCreated = {
|
|
55
|
+
type: "session.created",
|
|
56
|
+
properties: { info: { id: "child-1", parentID: "root-3" } },
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
await observer.observe(rootCreated("root-3"));
|
|
60
|
+
await observer.observe(childCreated);
|
|
61
|
+
await observer.observe(status("child-1", "busy"));
|
|
62
|
+
const rootStatus = status("root-3", "busy");
|
|
63
|
+
await observer.observe(rootStatus);
|
|
64
|
+
await observer.observe(rootStatus);
|
|
65
|
+
|
|
66
|
+
assert.equal(observations.length, 1);
|
|
67
|
+
assert.equal(observations[0].session_id, "root-3");
|
|
68
|
+
assert.deepEqual(observer.stats(), {
|
|
69
|
+
root_session_id: "root-3",
|
|
70
|
+
child_session_ids: ["child-1"],
|
|
71
|
+
event_count: 5,
|
|
72
|
+
unique_event_count: 2,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("notification failures are logged and never escape the observer", async () => {
|
|
77
|
+
const logs = [];
|
|
78
|
+
const observer = createMissionObserver({
|
|
79
|
+
notify: async () => { throw new Error("transport down"); },
|
|
80
|
+
log: message => logs.push(message),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await observer.observe(rootCreated("root-4"));
|
|
84
|
+
const result = await observer.observe(status("root-4", "busy"));
|
|
85
|
+
|
|
86
|
+
assert.equal(result.notification_error, true);
|
|
87
|
+
assert.deepEqual(logs, ["[mission-runtime] notification failed: Error"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("plugin is an observer only and uses an optional TUI notification", () => {
|
|
91
|
+
const source = fs.readFileSync(path.join(root, "plugins/mission-runtime.ts"), "utf8");
|
|
92
|
+
assert.match(source, /event:/);
|
|
93
|
+
assert.match(source, /chat\.message/);
|
|
94
|
+
assert.match(source, /showToast/);
|
|
95
|
+
assert.doesNotMatch(source, /fs\.|writeFile|appendFile|rename|\.opencode\/loops/);
|
|
96
|
+
assert.match(source, /mission-runtime-observer\.mjs/);
|
|
97
|
+
});
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
LoopStateError,
|
|
8
|
+
inspectLoopState,
|
|
9
|
+
readLoopHistory,
|
|
10
|
+
} from "./loop-state.mjs";
|
|
11
|
+
|
|
12
|
+
const slugPattern = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/;
|
|
13
|
+
|
|
14
|
+
const NEXT_ACTIONS = Object.freeze({
|
|
15
|
+
approved: "resume after confirming the approved contract",
|
|
16
|
+
running: "continue only within the persisted iteration budget",
|
|
17
|
+
paused: "validate the contract and lock, then resume explicitly",
|
|
18
|
+
blocked: "inspect the blocking cause and resolve it before resuming",
|
|
19
|
+
completed: "no further action; start a new loop if more work is needed",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function fail(code, message) {
|
|
23
|
+
throw new LoopStateError(code, message);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function requireSlug(slug) {
|
|
27
|
+
if (typeof slug !== "string" || !slugPattern.test(slug)) {
|
|
28
|
+
fail("invalid_argument", "slug is invalid");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function parseArguments(argv = []) {
|
|
33
|
+
let root = process.cwd();
|
|
34
|
+
let slug;
|
|
35
|
+
let json = false;
|
|
36
|
+
let rootProvided = false;
|
|
37
|
+
|
|
38
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
39
|
+
const token = argv[index];
|
|
40
|
+
if (token === "--json") {
|
|
41
|
+
if (json) fail("invalid_argument", "--json was provided more than once");
|
|
42
|
+
json = true;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (token === "--slug" || token === "--root") {
|
|
46
|
+
const value = argv[index + 1];
|
|
47
|
+
if (value === undefined || value.startsWith("--")) {
|
|
48
|
+
fail("invalid_argument", `${token} requires a value`);
|
|
49
|
+
}
|
|
50
|
+
index += 1;
|
|
51
|
+
if (token === "--slug") {
|
|
52
|
+
if (slug !== undefined) fail("invalid_argument", "--slug was provided more than once");
|
|
53
|
+
slug = value;
|
|
54
|
+
} else {
|
|
55
|
+
if (rootProvided) fail("invalid_argument", "--root was provided more than once");
|
|
56
|
+
if (!path.isAbsolute(value)) fail("invalid_argument", "--root must be an absolute path");
|
|
57
|
+
root = value;
|
|
58
|
+
rootProvided = true;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
fail("invalid_argument", `unknown argument ${token}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (slug === undefined) fail("invalid_argument", "--slug is required");
|
|
66
|
+
requireSlug(slug);
|
|
67
|
+
return { root, slug, json };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function projectMission(state, events) {
|
|
71
|
+
if (!Array.isArray(events) || events.length === 0) {
|
|
72
|
+
fail("history_corrupt", "loop history is empty");
|
|
73
|
+
}
|
|
74
|
+
const latest = events.at(-1);
|
|
75
|
+
if (latest.state_after.last_action_id !== state.last_action_id) {
|
|
76
|
+
fail("history_corrupt", "latest history event does not match canonical state");
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
schema_version: 1,
|
|
80
|
+
slug: state.slug,
|
|
81
|
+
status: state.status,
|
|
82
|
+
current_iteration: state.current_iteration,
|
|
83
|
+
planned_iterations: state.planned_iterations,
|
|
84
|
+
last_completed_step: state.last_completed_step,
|
|
85
|
+
blocking_cause: state.blocking_cause,
|
|
86
|
+
session_id: state.lease?.session_id ?? state.session_id,
|
|
87
|
+
updated_at: latest.recorded_at,
|
|
88
|
+
next_action: NEXT_ACTIONS[state.status],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function readMissionStatus({ root, slug }) {
|
|
93
|
+
const state = inspectLoopState({ root, slug });
|
|
94
|
+
const events = readLoopHistory({ root, slug });
|
|
95
|
+
return projectMission(state, events);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function display(value) {
|
|
99
|
+
return value === null || value === undefined || value === "" ? "—" : value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function renderHuman(mission) {
|
|
103
|
+
return [
|
|
104
|
+
`Mission: ${mission.slug}`,
|
|
105
|
+
`Status: ${mission.status}`,
|
|
106
|
+
`Iteration: ${mission.current_iteration}/${mission.planned_iterations}`,
|
|
107
|
+
`Last step: ${display(mission.last_completed_step)}`,
|
|
108
|
+
`Blocking cause: ${display(mission.blocking_cause)}`,
|
|
109
|
+
`Session: ${display(mission.session_id)}`,
|
|
110
|
+
`Updated: ${mission.updated_at}`,
|
|
111
|
+
`Next action: ${mission.next_action}`,
|
|
112
|
+
].join("\n");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function main(argv = process.argv.slice(2), output = process.stdout) {
|
|
116
|
+
const options = parseArguments(argv);
|
|
117
|
+
const mission = readMissionStatus(options);
|
|
118
|
+
output.write(`${options.json ? JSON.stringify(mission, null, 2) : renderHuman(mission)}\n`);
|
|
119
|
+
return mission;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const isDirect = process.argv[1]
|
|
123
|
+
&& fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
|
|
124
|
+
|
|
125
|
+
if (isDirect) {
|
|
126
|
+
try {
|
|
127
|
+
main();
|
|
128
|
+
} catch (error) {
|
|
129
|
+
if (error instanceof LoopStateError) {
|
|
130
|
+
process.stderr.write(`${error.code}: ${error.message}\n`);
|
|
131
|
+
process.exitCode = 1;
|
|
132
|
+
} else {
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|