opencode-agent-orchestration-kit 1.0.45 → 1.1.1

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.
Files changed (107) hide show
  1. package/CONTRIBUTING.md +0 -1
  2. package/NOTICE.md +1 -5
  3. package/README.md +37 -49
  4. package/compatibility.json +7 -0
  5. package/docs/cli.md +69 -1
  6. package/docs/commands.md +1 -1
  7. package/docs/comparison.md +3 -3
  8. package/docs/compatibility.md +33 -14
  9. package/docs/installation.md +2 -0
  10. package/docs/models.md +1 -1
  11. package/docs/open-design.md +2 -0
  12. package/docs/opencode-v2.md +190 -0
  13. package/docs/releases/v1.1.0.md +91 -0
  14. package/docs/releases/v1.1.1.md +54 -0
  15. package/docs/supply-chain.md +10 -8
  16. package/docs/threat-model.md +16 -6
  17. package/docs/troubleshooting.md +7 -4
  18. package/docs/workflows.md +27 -4
  19. package/opencode/AGENTS.md +18 -10
  20. package/opencode/agents/designer.md +1 -1
  21. package/opencode/agents/developer.md +60 -6
  22. package/opencode/agents/lead.md +19 -9
  23. package/opencode/agents/reviewer.md +7 -0
  24. package/opencode/agents/scoper.md +1 -0
  25. package/opencode/agents/specifier.md +1 -0
  26. package/opencode/commands/autonomous.md +175 -24
  27. package/opencode/docs/ai/evolution/README.md +5 -4
  28. package/opencode/docs/ai/evolution/benchmarks/adversarial-scenarios.jsonl +3 -0
  29. package/opencode/docs/ai/evolution/session-sources.md +12 -0
  30. package/opencode/docs/ai/harness/agents.md +8 -6
  31. package/opencode/docs/ai/harness/checks.md +3 -2
  32. package/opencode/docs/ai/harness/commands.md +7 -4
  33. package/opencode/docs/ai/harness/skill_registry.json +107 -1
  34. package/opencode/docs/ai/harness/skill_registry.md +5 -0
  35. package/opencode/docs/ai/specs/mission-runtime.md +9 -0
  36. package/opencode/opencode.json +0 -1
  37. package/opencode/plugins/mission-runtime.test.mjs +15 -3
  38. package/opencode/plugins/mission-runtime.ts +11 -39
  39. package/opencode/plugins/oak-tui/server.ts +8 -0
  40. package/opencode/plugins/oak-tui/tui.tsx +81 -0
  41. package/opencode/plugins/open-design.ts +12 -0
  42. package/opencode/plugins/shell-export-guard.ts +13 -23
  43. package/opencode/plugins/token-tree-usage.tsx +4 -1
  44. package/opencode/runtime/v1/mission-runtime.ts +73 -0
  45. package/opencode/runtime/v1/shell-export-guard.ts +9 -0
  46. package/opencode/runtime/v2/mission-events.d.mts +13 -0
  47. package/opencode/runtime/v2/mission-events.mjs +64 -0
  48. package/opencode/runtime/v2/mission-events.test.mjs +120 -0
  49. package/opencode/runtime/v2/mission-runtime.ts +26 -0
  50. package/opencode/runtime/v2/open-design.ts +17 -0
  51. package/opencode/runtime/v2/shell-export-guard.ts +8 -0
  52. package/opencode/runtime/v2/token-usage.d.mts +15 -0
  53. package/opencode/runtime/v2/token-usage.mjs +35 -0
  54. package/opencode/runtime/v2/token-usage.test.mjs +30 -0
  55. package/opencode/scripts/adversarial-harness.test.mjs +50 -1
  56. package/opencode/scripts/check-harness.mjs +142 -33
  57. package/opencode/scripts/check-harness.test.mjs +81 -8
  58. package/opencode/scripts/collect-session-evidence.mjs +59 -43
  59. package/opencode/scripts/collect-session-evidence.test.mjs +164 -0
  60. package/opencode/scripts/deliver.d.mts +37 -0
  61. package/opencode/scripts/deliver.mjs +204 -0
  62. package/opencode/scripts/deliver.test.mjs +245 -0
  63. package/opencode/scripts/loop-state.mjs +50 -1
  64. package/opencode/scripts/loop-state.test.mjs +33 -0
  65. package/opencode/scripts/mission-runtime.test.mjs +1 -1
  66. package/opencode/scripts/mission-status.test.mjs +2 -0
  67. package/opencode/scripts/runtime-permission-policy.mjs +1 -1
  68. package/opencode/scripts/runtime-permission-policy.test.mjs +3 -2
  69. package/opencode/scripts/session-sources.d.mts +6 -0
  70. package/opencode/scripts/session-sources.mjs +49 -0
  71. package/opencode/scripts/session-sources.test.mjs +41 -0
  72. package/opencode/scripts/shell-export-guard-core.d.mts +2 -0
  73. package/opencode/scripts/shell-export-guard-core.mjs +21 -0
  74. package/opencode/scripts/shell-export-guard-core.test.mjs +43 -0
  75. package/opencode/scripts/task-run.d.mts +63 -0
  76. package/opencode/scripts/task-run.mjs +224 -0
  77. package/opencode/scripts/task-run.test.mjs +178 -0
  78. package/opencode/skills/debugging-and-error-recovery/SKILL.md +20 -0
  79. package/opencode/skills/oak-adversarial-review/SKILL.md +138 -0
  80. package/opencode/skills/oak-commit/SKILL.md +69 -0
  81. package/opencode/skills/oak-enrich-task/SKILL.md +109 -0
  82. package/opencode/skills/oak-runtime-verification/SKILL.md +103 -0
  83. package/opencode/skills/oak-update-docs/SKILL.md +54 -0
  84. package/opencode/tools/open-design-tools.d.mts +17 -0
  85. package/opencode/tools/open-design-tools.mjs +254 -0
  86. package/opencode/tools/open_design.ts +26 -259
  87. package/package.json +4 -3
  88. package/scripts/benchmark-tool-paths.mjs +1 -1
  89. package/scripts/check-compatibility.mjs +58 -4
  90. package/scripts/check-compatibility.test.mjs +159 -11
  91. package/scripts/check-public-boundary.mjs +3 -0
  92. package/scripts/check-supply-chain.mjs +24 -57
  93. package/scripts/check-supply-chain.test.mjs +37 -35
  94. package/scripts/check.sh +42 -7
  95. package/scripts/manage-installation.mjs +34 -20
  96. package/scripts/manage-installation.test.mjs +29 -0
  97. package/scripts/oak.mjs +72 -2
  98. package/scripts/oak.test.mjs +69 -1
  99. package/scripts/open-design-http.test.mjs +1 -1
  100. package/scripts/open-design-tools.test.mjs +49 -0
  101. package/scripts/opencode-compat-smoke.sh +45 -4
  102. package/scripts/opencode-v2-smoke.mjs +197 -0
  103. package/scripts/opencode-v2-smoke.test.mjs +59 -0
  104. package/scripts/package-smoke.test.mjs +3 -3
  105. package/scripts/version.test.mjs +2 -2
  106. package/supply-chain.json +0 -4
  107. package/docs/superpowers.md +0 -41
@@ -0,0 +1,81 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import type { Plugin } from "@opencode/plugin/tui"
3
+ import { createMemo, Show } from "solid-js"
4
+ import { createMissionObserver } from "../../scripts/mission-runtime-observer.mjs"
5
+ import { toObserverEvent } from "../../runtime/v2/mission-events.mjs"
6
+ import { familyUsage, formatTokens } from "../../runtime/v2/token-usage.mjs"
7
+
8
+ type Context = Parameters<Plugin.Definition["setup"]>[0]
9
+
10
+ const TOAST_DURATION_MS = 1_500
11
+
12
+ function SidebarUsage(props: { context: Context; sessionID: string }) {
13
+ const usage = createMemo(() => {
14
+ const data = props.context.data.session
15
+ const sessions = data.family(props.sessionID).flatMap((id) => {
16
+ const session = data.get(id)
17
+ return session ? [session] : []
18
+ })
19
+ return familyUsage(props.sessionID, sessions)
20
+ })
21
+ const theme = () => props.context.theme
22
+
23
+ return (
24
+ <box flexDirection="column" paddingTop={1}>
25
+ <text fg={theme().text.muted}>
26
+ <b>Tokens</b>
27
+ </text>
28
+ <text fg={theme().text.base}>
29
+ Lead {formatTokens(usage().lead)} | Total {formatTokens(usage().lead + usage().subagents)}
30
+ </text>
31
+ <Show when={usage().sessions > 0}>
32
+ <text fg={theme().text.muted}>
33
+ Subagents +{formatTokens(usage().subagents)} | {usage().sessions}
34
+ </text>
35
+ </Show>
36
+ </box>
37
+ )
38
+ }
39
+
40
+ function registerMissionToasts(context: Context) {
41
+ const observer = createMissionObserver({
42
+ notify: async (observation) => {
43
+ context.ui.toast.show({
44
+ title: "Mission runtime",
45
+ message: `${observation.activity}: ${observation.session_id}`,
46
+ variant: observation.activity === "blocked" ? "error" : observation.activity === "idle" ? "info" : "success",
47
+ duration: TOAST_DURATION_MS,
48
+ })
49
+ },
50
+ log: (message) => console.warn(message),
51
+ })
52
+ const forward = (event: unknown) => {
53
+ const translated = toObserverEvent(event)
54
+ if (translated) void observer.observe(translated)
55
+ }
56
+ const stops = [
57
+ context.data.on("session.created", forward),
58
+ context.data.on("session.execution.started", forward),
59
+ context.data.on("session.execution.succeeded", forward),
60
+ context.data.on("session.execution.interrupted", forward),
61
+ context.data.on("session.execution.failed", forward),
62
+ ]
63
+ return () => stops.forEach((stop) => stop())
64
+ }
65
+
66
+ const plugin: Plugin.Definition = {
67
+ id: "oak.tui",
68
+ setup(context) {
69
+ const disposeSidebar = context.ui.slot({
70
+ append: "sidebar.content",
71
+ render: (input) => <SidebarUsage context={context} sessionID={input.sessionID} />,
72
+ })
73
+ const disposeToasts = registerMissionToasts(context)
74
+ return () => {
75
+ disposeSidebar()
76
+ disposeToasts()
77
+ }
78
+ },
79
+ }
80
+
81
+ export default plugin
@@ -0,0 +1,12 @@
1
+ import type { Plugin as V2Plugin } from "@opencode/plugin"
2
+ import { openDesignV2 } from "../runtime/v2/open-design.ts"
3
+
4
+ // OpenCode 1 loads the Open Design tools from tools/open_design.ts, so its
5
+ // `server` hook registers nothing. Retiring OpenCode 1 deletes the marked line.
6
+ const plugin = {
7
+ id: "oak.open-design",
8
+ server: async () => ({}), // oak:v1-only
9
+ setup: openDesignV2,
10
+ }
11
+
12
+ export default plugin satisfies Pick<V2Plugin.Plugin, "id" | "setup">
@@ -1,23 +1,13 @@
1
- import type { Plugin } from "@opencode-ai/plugin";
2
- import { classifyShellExport } from "../scripts/shell-export-policy.mjs";
3
-
4
- const BLOCK_MESSAGE =
5
- "Sensitive shell export blocked by policy; use an explicit non-secret value or a scoped secret-aware tool.";
6
-
7
- export const ShellExportGuardPlugin: Plugin = async () => ({
8
- "tool.execute.before": async (input, output) => {
9
- const tool = String(input?.tool ?? "").toLowerCase();
10
- if (tool !== "bash" && tool !== "shell") return;
11
-
12
- const args = output?.args;
13
- if (!args || typeof args !== "object") return;
14
-
15
- const command = (args as Record<string, unknown>).command;
16
- if (typeof command !== "string") return;
17
-
18
- const decision = classifyShellExport(command);
19
- if (decision.blocked) {
20
- throw new Error(`[shell-export-guard:${decision.rule}] ${BLOCK_MESSAGE}`);
21
- }
22
- },
23
- });
1
+ import type { Plugin as V2Plugin } from "@opencode/plugin"
2
+ import { shellExportGuardV1 } from "../runtime/v1/shell-export-guard.ts" // oak:v1-only
3
+ import { shellExportGuardV2 } from "../runtime/v2/shell-export-guard.ts"
4
+
5
+ // One default export serves both runtimes: OpenCode 1 calls `server`,
6
+ // OpenCode 2 calls `setup`. Retiring OpenCode 1 deletes the marked lines.
7
+ const plugin = {
8
+ id: "oak.shell-export-guard",
9
+ server: shellExportGuardV1, // oak:v1-only
10
+ setup: shellExportGuardV2,
11
+ }
12
+
13
+ export default plugin satisfies Pick<V2Plugin.Plugin, "id" | "setup">
@@ -1,4 +1,5 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
+ // oak:v1-only — whole file
2
3
  import type { TuiPlugin, TuiPluginApi, TuiTheme } from '@opencode-ai/plugin/tui';
3
4
  import { createEffect, createMemo, createSignal, onCleanup, Show } from 'solid-js';
4
5
 
@@ -203,4 +204,6 @@ const tui: TuiPlugin = async api => {
203
204
  });
204
205
  };
205
206
 
206
- export default { id, tui };
207
+ // oak:v1-only — whole file (loaded through tui.json). The no-op `setup` keeps
208
+ // OpenCode 2 quiet if it migrates tui.json; its sidebar lives in plugins/oak-tui.
209
+ export default { id, tui, setup() {} };
@@ -0,0 +1,73 @@
1
+ // oak:v1-only — OpenCode 1 server hooks. Delete with the OpenCode 1 line.
2
+ import type { Plugin } from "@opencode-ai/plugin"
3
+
4
+ import { createMissionObserver } from "../../scripts/mission-runtime-observer.mjs"
5
+ import { appendRunEvent } from "../../scripts/task-run.mjs"
6
+
7
+ const TOAST_DURATION_MS = 1_500
8
+
9
+ function toastVariant(activity: string) {
10
+ if (activity === "blocked") return "error" as const
11
+ if (activity === "idle") return "info" as const
12
+ return "success" as const
13
+ }
14
+
15
+ export const missionRuntimeV1: Plugin = async ({ client, directory }) => {
16
+ const observer = createMissionObserver({
17
+ notify: async observation => {
18
+ if (!client?.tui?.showToast) return
19
+ await client.tui.showToast({
20
+ body: {
21
+ title: "Mission runtime",
22
+ message: `${observation.activity}: ${observation.session_id}`,
23
+ variant: toastVariant(observation.activity),
24
+ duration: TOAST_DURATION_MS,
25
+ },
26
+ query: { directory },
27
+ })
28
+ },
29
+ log: message => console.warn(message),
30
+ })
31
+ const recordedSessions = new Set<string>()
32
+
33
+ // Link each session to the open task run, if any. Best effort: it must never
34
+ // affect the session itself.
35
+ // A session is marked as recorded only once an event was written, so a
36
+ // session that started before the run opened is still linked later.
37
+ function recordSession(info: { id: string; parentID?: string }) {
38
+ if (recordedSessions.has(info.id)) return
39
+ try {
40
+ const recorded = appendRunEvent({
41
+ root: directory,
42
+ type: "agent_session",
43
+ fields: {
44
+ session_id: info.id,
45
+ parent_session_id: info.parentID ?? "",
46
+ agent: (info as { agent?: string }).agent ?? "",
47
+ runtime: "opencode",
48
+ },
49
+ })
50
+ if (recorded) recordedSessions.add(info.id)
51
+ } catch {
52
+ // Recording is best effort and must never affect the session.
53
+ }
54
+ }
55
+
56
+ return {
57
+ "chat.message": async ({ sessionID }) => {
58
+ await observer.observe({
59
+ type: "chat.message",
60
+ properties: { sessionID },
61
+ })
62
+ },
63
+ event: async ({ event }) => {
64
+ if (event.type === "session.created" && event.properties.info?.id) {
65
+ recordSession(event.properties.info)
66
+ } else {
67
+ const sessionID = (event.properties as { sessionID?: unknown } | undefined)?.sessionID
68
+ if (typeof sessionID === "string" && sessionID) recordSession({ id: sessionID })
69
+ }
70
+ await observer.observe(event)
71
+ },
72
+ }
73
+ }
@@ -0,0 +1,9 @@
1
+ // oak:v1-only — OpenCode 1 server hook. Delete with the OpenCode 1 line.
2
+ import type { Plugin } from "@opencode-ai/plugin"
3
+ import { assertShellCallAllowed } from "../../scripts/shell-export-guard-core.mjs"
4
+
5
+ export const shellExportGuardV1: Plugin = async () => ({
6
+ "tool.execute.before": async (input, output) => {
7
+ assertShellCallAllowed(input?.tool, output?.args)
8
+ },
9
+ })
@@ -0,0 +1,13 @@
1
+ export type SessionRecord = {
2
+ session_id: string;
3
+ parent_session_id: string;
4
+ agent: string;
5
+ runtime: "opencode";
6
+ };
7
+
8
+ export function toObserverEvent(event: unknown): { type: string; properties: Record<string, unknown> } | null;
9
+ export function toSessionRecord(event: unknown): SessionRecord | null;
10
+ export function createSessionRecorder(
11
+ append: (record: SessionRecord) => unknown,
12
+ directory: string,
13
+ ): (event: unknown) => void;
@@ -0,0 +1,64 @@
1
+ // Translates OpenCode 2 events ({ type, data }) into the OpenCode 1 event shape
2
+ // ({ type, properties }) consumed by scripts/mission-runtime-observer.mjs, and
3
+ // into the agent_session run events that runtime/v1/mission-runtime.ts records.
4
+ const EXECUTION_EVENTS = {
5
+ "session.execution.started": (sessionID) => ({
6
+ type: "session.status",
7
+ properties: { sessionID, status: { type: "busy" } },
8
+ }),
9
+ "session.execution.succeeded": (sessionID) => ({ type: "session.idle", properties: { sessionID } }),
10
+ "session.execution.interrupted": (sessionID) => ({ type: "session.idle", properties: { sessionID } }),
11
+ "session.execution.failed": (sessionID) => ({ type: "session.error", properties: { sessionID } }),
12
+ };
13
+
14
+ function nonEmpty(value) {
15
+ return typeof value === "string" && value ? value : null;
16
+ }
17
+
18
+ export function toObserverEvent(event) {
19
+ const sessionID = nonEmpty(event?.data?.sessionID);
20
+ if (!sessionID) return null;
21
+
22
+ if (event.type === "session.created") {
23
+ return { type: "session.created", properties: { info: { id: sessionID, parentID: nonEmpty(event.data.parentID) } } };
24
+ }
25
+
26
+ const translate = EXECUTION_EVENTS[event.type];
27
+ return translate ? translate(sessionID) : null;
28
+ }
29
+
30
+ export function toSessionRecord(event) {
31
+ const sessionID = nonEmpty(event?.data?.sessionID);
32
+ if (!sessionID) return null;
33
+ const created = event.type === "session.created";
34
+ return {
35
+ session_id: sessionID,
36
+ parent_session_id: (created && nonEmpty(event.data.parentID)) || "",
37
+ agent: (created && nonEmpty(event.data.agent)) || "",
38
+ runtime: "opencode",
39
+ };
40
+ }
41
+
42
+ // Link each session to the open task run, if any. Best effort: it must never
43
+ // affect the session itself. A session is marked as recorded only once an
44
+ // event was written, so a session that started before the run opened is
45
+ // still linked later.
46
+ // The OpenCode 2 event stream covers every location a server has open, so a
47
+ // session seen in another directory is ignored, including its later events
48
+ // that carry no location.
49
+ export function createSessionRecorder(append, directory) {
50
+ const recorded = new Set();
51
+ const foreign = new Set();
52
+ return (event) => {
53
+ const record = toSessionRecord(event);
54
+ if (!record || recorded.has(record.session_id)) return;
55
+ const location = nonEmpty(event.location?.directory) ?? nonEmpty(event.data?.location?.directory);
56
+ if (location && location !== directory) foreign.add(record.session_id);
57
+ if (foreign.has(record.session_id)) return;
58
+ try {
59
+ if (append(record)) recorded.add(record.session_id);
60
+ } catch {
61
+ // Recording is best effort and must never affect the session.
62
+ }
63
+ };
64
+ }
@@ -0,0 +1,120 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { createMissionObserver } from "../../scripts/mission-runtime-observer.mjs";
4
+ import { createSessionRecorder, toObserverEvent, toSessionRecord } from "./mission-events.mjs";
5
+
6
+ test("maps session.created with and without a parent", () => {
7
+ assert.deepEqual(toObserverEvent({ type: "session.created", data: { sessionID: "root" } }), {
8
+ type: "session.created",
9
+ properties: { info: { id: "root", parentID: null } },
10
+ });
11
+ assert.deepEqual(
12
+ toObserverEvent({ type: "session.created", data: { sessionID: "child", parentID: "root" } }),
13
+ { type: "session.created", properties: { info: { id: "child", parentID: "root" } } },
14
+ );
15
+ });
16
+
17
+ test("maps execution lifecycle to observer activities", () => {
18
+ assert.deepEqual(toObserverEvent({ type: "session.execution.started", data: { sessionID: "s" } }), {
19
+ type: "session.status",
20
+ properties: { sessionID: "s", status: { type: "busy" } },
21
+ });
22
+ for (const type of ["session.execution.succeeded", "session.execution.interrupted"]) {
23
+ assert.deepEqual(toObserverEvent({ type, data: { sessionID: "s" } }), {
24
+ type: "session.idle",
25
+ properties: { sessionID: "s" },
26
+ });
27
+ }
28
+ assert.deepEqual(toObserverEvent({ type: "session.execution.failed", data: { sessionID: "s" } }), {
29
+ type: "session.error",
30
+ properties: { sessionID: "s" },
31
+ });
32
+ });
33
+
34
+ test("ignores unknown events and malformed payloads", () => {
35
+ for (const event of [
36
+ null,
37
+ { type: "session.text.started", data: { sessionID: "s" } },
38
+ { type: "session.execution.started" },
39
+ { type: "session.execution.started", data: { sessionID: "" } },
40
+ ]) {
41
+ assert.equal(toObserverEvent(event), null);
42
+ }
43
+ });
44
+
45
+ test("translated events drive the shared observer like OpenCode 1 events", async () => {
46
+ const observations = [];
47
+ const observer = createMissionObserver({ notify: async (value) => observations.push(value) });
48
+ for (const event of [
49
+ { type: "session.created", data: { sessionID: "root" } },
50
+ { type: "session.created", data: { sessionID: "child", parentID: "root" } },
51
+ { type: "session.execution.started", data: { sessionID: "root" } },
52
+ { type: "session.execution.started", data: { sessionID: "child" } },
53
+ { type: "session.execution.failed", data: { sessionID: "root" } },
54
+ ]) {
55
+ const translated = toObserverEvent(event);
56
+ if (translated) await observer.observe(translated);
57
+ }
58
+ assert.deepEqual(observations.map((entry) => entry.activity), ["running", "blocked"]);
59
+ assert.deepEqual(observer.stats().child_session_ids, ["child"]);
60
+ });
61
+
62
+ test("maps session.created to an agent_session record with parent and agent", () => {
63
+ assert.deepEqual(
64
+ toSessionRecord({ type: "session.created", data: { sessionID: "child", parentID: "root", agent: "developer" } }),
65
+ { session_id: "child", parent_session_id: "root", agent: "developer", runtime: "opencode" },
66
+ );
67
+ assert.deepEqual(toSessionRecord({ type: "session.created", data: { sessionID: "root" } }), {
68
+ session_id: "root",
69
+ parent_session_id: "",
70
+ agent: "",
71
+ runtime: "opencode",
72
+ });
73
+ });
74
+
75
+ test("maps any other event with a session id to a bare agent_session record", () => {
76
+ assert.deepEqual(toSessionRecord({ type: "session.execution.started", data: { sessionID: "s", agent: "x" } }), {
77
+ session_id: "s",
78
+ parent_session_id: "",
79
+ agent: "",
80
+ runtime: "opencode",
81
+ });
82
+ for (const event of [null, { type: "session.created" }, { type: "x", data: { sessionID: "" } }]) {
83
+ assert.equal(toSessionRecord(event), null);
84
+ }
85
+ });
86
+
87
+ test("recorder marks a session only after its event was written", () => {
88
+ const written = [];
89
+ let open = false;
90
+ const record = createSessionRecorder((fields) => {
91
+ if (!open) return null;
92
+ written.push(fields.session_id);
93
+ return { type: "agent_session" };
94
+ });
95
+ record({ type: "session.created", data: { sessionID: "s" } });
96
+ open = true;
97
+ record({ type: "session.execution.started", data: { sessionID: "s" } });
98
+ record({ type: "session.execution.succeeded", data: { sessionID: "s" } });
99
+ assert.deepEqual(written, ["s"]);
100
+ });
101
+
102
+ test("recorder never lets a recording failure escape", () => {
103
+ const record = createSessionRecorder(() => {
104
+ throw new Error("disk full");
105
+ });
106
+ assert.doesNotThrow(() => record({ type: "session.created", data: { sessionID: "s" } }));
107
+ });
108
+
109
+ test("recorder ignores sessions that belong to another location", () => {
110
+ const written = [];
111
+ const record = createSessionRecorder((fields) => {
112
+ written.push(fields.session_id);
113
+ return { type: "agent_session" };
114
+ }, "/work/a");
115
+ record({ type: "session.created", location: { directory: "/work/b" }, data: { sessionID: "other" } });
116
+ record({ type: "session.execution.started", data: { sessionID: "other" } });
117
+ record({ type: "session.created", location: { directory: "/work/a" }, data: { sessionID: "mine" } });
118
+ record({ type: "session.execution.started", data: { sessionID: "unlocated" } });
119
+ assert.deepEqual(written, ["mine", "unlocated"]);
120
+ });
@@ -0,0 +1,26 @@
1
+ import type { Plugin } from "@opencode/plugin"
2
+ import { appendRunEvent } from "../../scripts/task-run.mjs"
3
+ import { createSessionRecorder } from "./mission-events.mjs"
4
+
5
+ // OpenCode 2 core plugins have no toast API, so the mission toasts live in
6
+ // plugins/oak-tui/tui.tsx. This hook only links sessions to the open task run.
7
+ export const missionRuntimeV2: Plugin.Plugin["setup"] = (ctx) => {
8
+ const directory = ctx.location.directory
9
+ const record = createSessionRecorder(
10
+ (fields) => appendRunEvent({ root: directory, type: "agent_session", fields }),
11
+ directory,
12
+ )
13
+ const stop = new AbortController()
14
+ void (async () => {
15
+ // A slow consumer can fail the stream, so resubscribe until the plugin stops.
16
+ while (!stop.signal.aborted) {
17
+ try {
18
+ for await (const event of ctx.event.subscribe({ signal: stop.signal })) record(event)
19
+ } catch {
20
+ // Recording is best effort.
21
+ }
22
+ if (!stop.signal.aborted) await new Promise((resolve) => setTimeout(resolve, 1_000))
23
+ }
24
+ })()
25
+ return () => stop.abort()
26
+ }
@@ -0,0 +1,17 @@
1
+ import type { Plugin } from "@opencode/plugin"
2
+ import { OPEN_DESIGN_TOOLS, openDesignJsonSchema } from "../../tools/open-design-tools.mjs"
3
+
4
+ export const openDesignV2: Plugin.Plugin["setup"] = async (ctx) => {
5
+ await ctx.tool.transform((editor) => {
6
+ for (const definition of OPEN_DESIGN_TOOLS) {
7
+ editor.add({
8
+ name: `open_design_${definition.name}`,
9
+ description: definition.description,
10
+ input: openDesignJsonSchema(definition.args),
11
+ async execute(input) {
12
+ return { content: await definition.execute(input as Record<string, string | undefined>) }
13
+ },
14
+ })
15
+ }
16
+ })
17
+ }
@@ -0,0 +1,8 @@
1
+ import type { Plugin } from "@opencode/plugin"
2
+ import { assertShellCallAllowed } from "../../scripts/shell-export-guard-core.mjs"
3
+
4
+ export const shellExportGuardV2: Plugin.Plugin["setup"] = async (ctx) => {
5
+ await ctx.tool.hook("execute.before", (event) => {
6
+ assertShellCallAllowed(event.tool, event.input)
7
+ })
8
+ }
@@ -0,0 +1,15 @@
1
+ export type SessionLike = {
2
+ readonly id: string;
3
+ readonly parentID?: string;
4
+ readonly tokens?: {
5
+ readonly input?: number;
6
+ readonly output?: number;
7
+ readonly reasoning?: number;
8
+ readonly cache?: { readonly read?: number; readonly write?: number };
9
+ };
10
+ };
11
+ export function familyUsage(
12
+ sessionID: string,
13
+ sessions: readonly SessionLike[],
14
+ ): { lead: number; subagents: number; sessions: number };
15
+ export function formatTokens(tokens: number): string;
@@ -0,0 +1,35 @@
1
+ // Matches the OpenCode 1 sidebar (plugins/token-tree-usage.tsx), which shows
2
+ // OpenCode 1 message totals; those include cache reads and writes.
3
+ function sessionTokens(session) {
4
+ const tokens = session?.tokens;
5
+ if (!tokens) return 0;
6
+ return (tokens.input ?? 0) + (tokens.output ?? 0) + (tokens.reasoning ?? 0)
7
+ + (tokens.cache?.read ?? 0) + (tokens.cache?.write ?? 0);
8
+ }
9
+
10
+ export function familyUsage(sessionID, sessions) {
11
+ const byId = new Map(sessions.map((session) => [session.id, session]));
12
+ const visited = new Set([sessionID]);
13
+ const queue = [sessionID];
14
+ let subagents = 0;
15
+ let count = 0;
16
+
17
+ while (queue.length > 0) {
18
+ const parent = queue.shift();
19
+ for (const session of sessions) {
20
+ if (session.parentID !== parent || visited.has(session.id)) continue;
21
+ visited.add(session.id);
22
+ subagents += sessionTokens(session);
23
+ count += 1;
24
+ queue.push(session.id);
25
+ }
26
+ }
27
+
28
+ return { lead: sessionTokens(byId.get(sessionID)), subagents, sessions: count };
29
+ }
30
+
31
+ export function formatTokens(tokens) {
32
+ if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`;
33
+ if (tokens >= 1_000) return `${Math.round(tokens / 1_000)}k`;
34
+ return String(tokens);
35
+ }
@@ -0,0 +1,30 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { familyUsage, formatTokens } from "./token-usage.mjs";
4
+
5
+ const tokens = (input, output, reasoning = 0, read = 0, write = 0) => ({ input, output, reasoning, cache: { read, write } });
6
+
7
+ test("sums the viewed session and all of its descendants, including cache tokens like OpenCode 1 totals", () => {
8
+ const sessions = [
9
+ { id: "root", tokens: tokens(100, 50, 0, 1000, 5) },
10
+ { id: "child", parentID: "root", tokens: tokens(10, 5, 1, 2) },
11
+ { id: "grandchild", parentID: "child", tokens: tokens(1, 1) },
12
+ { id: "sibling-root", tokens: tokens(7, 7) },
13
+ ];
14
+ assert.deepEqual(familyUsage("root", sessions), { lead: 1155, subagents: 20, sessions: 2 });
15
+ assert.deepEqual(familyUsage("child", sessions), { lead: 18, subagents: 2, sessions: 1 });
16
+ });
17
+
18
+ test("tolerates missing tokens and cycles", () => {
19
+ const sessions = [
20
+ { id: "a", parentID: "b" },
21
+ { id: "b", parentID: "a", tokens: tokens(1, 1) },
22
+ ];
23
+ assert.deepEqual(familyUsage("a", sessions), { lead: 0, subagents: 2, sessions: 1 });
24
+ });
25
+
26
+ test("formats token counts like the OpenCode 1 sidebar", () => {
27
+ assert.equal(formatTokens(999), "999");
28
+ assert.equal(formatTokens(12_400), "12k");
29
+ assert.equal(formatTokens(3_250_000), "3.3M");
30
+ });
@@ -14,9 +14,12 @@ import {
14
14
  import {
15
15
  LoopStateError,
16
16
  acquireLoop,
17
+ attestReview,
17
18
  initLoopState,
18
19
  inspectLoopState,
20
+ recordLoopAction,
19
21
  } from "./loop-state.mjs";
22
+ import { DeliverError, deliver } from "./deliver.mjs";
20
23
 
21
24
  const scriptDir = path.dirname(fileURLToPath(import.meta.url));
22
25
  const root = path.dirname(scriptDir);
@@ -84,6 +87,29 @@ function initializeLoop(rootDir) {
84
87
  return contractPath;
85
88
  }
86
89
 
90
+ function completedDeliveryLoop(rootDir) {
91
+ const contractPath = initializeLoop(rootDir);
92
+ acquireLoop({ root: rootDir, slug: "example", contractPath, sessionId: "session-1", actionId: "resume-1" });
93
+ attestReview({ root: rootDir, slug: "example", reviewerSessionId: "child-1", reviewerAgent: "reviewer", reviewerVerdict: "APPROVE" });
94
+ recordLoopAction({ root: rootDir, slug: "example", sessionId: "session-1", actionId: "done-1", iteration: 1, completedStep: "reviewer_approved", blockingCause: null, status: "completed" });
95
+ fs.writeFileSync(path.join(rootDir, "pr-body.md"), "## What\nA change.\n");
96
+ }
97
+
98
+ function assertDeliveryRefused(rootDir, code, replies = {}) {
99
+ const calls = [];
100
+ const scripted = { "git rev-parse": "feat/x", "git symbolic-ref": "origin/main", "git status": "", "git rev-list": "1", ...replies };
101
+ const runner = (command, args) => {
102
+ calls.push([command, ...args]);
103
+ const reply = scripted[`${command} ${args[0]}`];
104
+ return reply === undefined ? { status: 1, stdout: "" } : { status: 0, stdout: `${reply}\n` };
105
+ };
106
+ assert.throws(
107
+ () => deliver({ root: rootDir, slug: "example", title: "Add x", bodyFile: "pr-body.md", runner }),
108
+ (error) => error instanceof DeliverError && error.code === code,
109
+ );
110
+ assert.equal(calls.some((call) => call[0] === "gh" || (call[0] === "git" && call[1] === "push")), false);
111
+ }
112
+
87
113
  function readAgent(rel) {
88
114
  return fs.readFileSync(path.join(root, rel), "utf8");
89
115
  }
@@ -270,6 +296,29 @@ const handlers = {
270
296
  assert.match(contract, /explicit human approval before installation/);
271
297
  },
272
298
 
299
+ "deliver-default-branch-refused"() {
300
+ withTempDir("opencode-adversarial-deliver-", (rootDir) => {
301
+ completedDeliveryLoop(rootDir);
302
+ assertDeliveryRefused(rootDir, "default_branch", { "git rev-parse": "main" });
303
+ });
304
+ },
305
+
306
+ "deliver-without-attestation-refused"() {
307
+ withTempDir("opencode-adversarial-deliver-", (rootDir) => {
308
+ completedDeliveryLoop(rootDir);
309
+ fs.rmSync(path.join(rootDir, ".opencode/loops/example.review.json"));
310
+ assertDeliveryRefused(rootDir, "attestation_missing");
311
+ });
312
+ },
313
+
314
+ "deliver-private-path-body-refused"() {
315
+ withTempDir("opencode-adversarial-deliver-", (rootDir) => {
316
+ completedDeliveryLoop(rootDir);
317
+ fs.writeFileSync(path.join(rootDir, "pr-body.md"), "Screenshot at /home/someone/shot.png\n");
318
+ assertDeliveryRefused(rootDir, "unsafe_body");
319
+ });
320
+ },
321
+
273
322
  "repeated-event-rejected"() {
274
323
  withTempDir("opencode-adversarial-event-", (rootDir) => {
275
324
  initializeLoop(rootDir);
@@ -285,7 +334,7 @@ const handlers = {
285
334
  };
286
335
 
287
336
  test("adversarial corpus maps each threat to one real harness defense", () => {
288
- assert.equal(scenarios.length, 11);
337
+ assert.equal(scenarios.length, 14);
289
338
  assert.deepEqual(
290
339
  scenarios.map((scenario) => scenario.id).sort(),
291
340
  Object.keys(handlers).sort(),