agent-mockingbird 0.0.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 (227) hide show
  1. package/.agents/skills/btca-cli/SKILL.md +64 -0
  2. package/.agents/skills/btca-cli/agents/openai.yaml +3 -0
  3. package/.agents/skills/frontend-design/SKILL.md +42 -0
  4. package/.agents/skills/frontend-design/agents/openai.yaml +3 -0
  5. package/.env.example +36 -0
  6. package/.githooks/pre-commit +33 -0
  7. package/.github/workflows/ci.yml +309 -0
  8. package/.opencode/bun.lock +18 -0
  9. package/.opencode/package.json +5 -0
  10. package/.opencode/tools/agent_type_manager.ts +100 -0
  11. package/.opencode/tools/config_manager.ts +87 -0
  12. package/.opencode/tools/cron_manager.ts +145 -0
  13. package/.opencode/tools/memory_get.ts +43 -0
  14. package/.opencode/tools/memory_remember.ts +53 -0
  15. package/.opencode/tools/memory_search.ts +48 -0
  16. package/AGENTS.md +126 -0
  17. package/MEMORY.md +2 -0
  18. package/README.md +451 -0
  19. package/THIRD_PARTY_NOTICES.md +11 -0
  20. package/agent-mockingbird.config.example.json +135 -0
  21. package/apps/server/package.json +32 -0
  22. package/apps/server/src/backend/agents/bootstrapContext.ts +362 -0
  23. package/apps/server/src/backend/agents/openclawImport.test.ts +133 -0
  24. package/apps/server/src/backend/agents/openclawImport.ts +797 -0
  25. package/apps/server/src/backend/agents/opencodeConfig.ts +428 -0
  26. package/apps/server/src/backend/agents/service.ts +10 -0
  27. package/apps/server/src/backend/config/example-config.test.ts +20 -0
  28. package/apps/server/src/backend/config/orchestration.ts +243 -0
  29. package/apps/server/src/backend/config/policy.ts +158 -0
  30. package/apps/server/src/backend/config/schema.test.ts +15 -0
  31. package/apps/server/src/backend/config/schema.ts +391 -0
  32. package/apps/server/src/backend/config/semantic.test.ts +34 -0
  33. package/apps/server/src/backend/config/semantic.ts +149 -0
  34. package/apps/server/src/backend/config/service.test.ts +75 -0
  35. package/apps/server/src/backend/config/service.ts +207 -0
  36. package/apps/server/src/backend/config/smoke.ts +77 -0
  37. package/apps/server/src/backend/config/store.test.ts +123 -0
  38. package/apps/server/src/backend/config/store.ts +581 -0
  39. package/apps/server/src/backend/config/testFixtures.ts +5 -0
  40. package/apps/server/src/backend/config/types.ts +56 -0
  41. package/apps/server/src/backend/contracts/events.ts +320 -0
  42. package/apps/server/src/backend/contracts/runtime.ts +111 -0
  43. package/apps/server/src/backend/cron/executor.ts +435 -0
  44. package/apps/server/src/backend/cron/repository.ts +170 -0
  45. package/apps/server/src/backend/cron/service.ts +660 -0
  46. package/apps/server/src/backend/cron/storage.ts +92 -0
  47. package/apps/server/src/backend/cron/types.ts +138 -0
  48. package/apps/server/src/backend/cron/utils.ts +351 -0
  49. package/apps/server/src/backend/db/client.ts +20 -0
  50. package/apps/server/src/backend/db/migrate.ts +40 -0
  51. package/apps/server/src/backend/db/repository.ts +1762 -0
  52. package/apps/server/src/backend/db/schema.ts +113 -0
  53. package/apps/server/src/backend/db/usageDashboard.test.ts +102 -0
  54. package/apps/server/src/backend/db/wipe.ts +13 -0
  55. package/apps/server/src/backend/defaults.ts +32 -0
  56. package/apps/server/src/backend/env.ts +48 -0
  57. package/apps/server/src/backend/heartbeat/activeHours.ts +45 -0
  58. package/apps/server/src/backend/heartbeat/defaultJob.ts +88 -0
  59. package/apps/server/src/backend/heartbeat/heartbeat.test.ts +110 -0
  60. package/apps/server/src/backend/heartbeat/runtimeService.ts +190 -0
  61. package/apps/server/src/backend/heartbeat/service.ts +176 -0
  62. package/apps/server/src/backend/heartbeat/state.test.ts +63 -0
  63. package/apps/server/src/backend/heartbeat/state.ts +167 -0
  64. package/apps/server/src/backend/heartbeat/types.ts +54 -0
  65. package/apps/server/src/backend/http/boundedQueue.test.ts +49 -0
  66. package/apps/server/src/backend/http/boundedQueue.ts +92 -0
  67. package/apps/server/src/backend/http/parsers.ts +40 -0
  68. package/apps/server/src/backend/http/router.ts +61 -0
  69. package/apps/server/src/backend/http/routes/agentRoutes.ts +67 -0
  70. package/apps/server/src/backend/http/routes/backgroundRoutes.ts +203 -0
  71. package/apps/server/src/backend/http/routes/chatRoutes.ts +107 -0
  72. package/apps/server/src/backend/http/routes/configRoutes.ts +602 -0
  73. package/apps/server/src/backend/http/routes/cronRoutes.ts +221 -0
  74. package/apps/server/src/backend/http/routes/dashboardRoutes.ts +308 -0
  75. package/apps/server/src/backend/http/routes/eventRoutes.ts +7 -0
  76. package/apps/server/src/backend/http/routes/heartbeatRoutes.test.ts +41 -0
  77. package/apps/server/src/backend/http/routes/heartbeatRoutes.ts +28 -0
  78. package/apps/server/src/backend/http/routes/index.ts +101 -0
  79. package/apps/server/src/backend/http/routes/mcpRoutes.ts +213 -0
  80. package/apps/server/src/backend/http/routes/memoryRoutes.ts +154 -0
  81. package/apps/server/src/backend/http/routes/runRoutes.ts +310 -0
  82. package/apps/server/src/backend/http/routes/runtimeRoutes.ts +197 -0
  83. package/apps/server/src/backend/http/routes/skillRoutes.ts +112 -0
  84. package/apps/server/src/backend/http/routes/uiRoutes.test.ts +161 -0
  85. package/apps/server/src/backend/http/routes/uiRoutes.ts +177 -0
  86. package/apps/server/src/backend/http/routes/usageRoutes.test.ts +104 -0
  87. package/apps/server/src/backend/http/routes/usageRoutes.ts +767 -0
  88. package/apps/server/src/backend/http/schemas.ts +64 -0
  89. package/apps/server/src/backend/http/sse.ts +144 -0
  90. package/apps/server/src/backend/integration/backend-core.test.ts +2316 -0
  91. package/apps/server/src/backend/logging/logger.ts +64 -0
  92. package/apps/server/src/backend/mcp/service.ts +326 -0
  93. package/apps/server/src/backend/memory/cli.ts +170 -0
  94. package/apps/server/src/backend/memory/conceptExpansion.test.ts +28 -0
  95. package/apps/server/src/backend/memory/conceptExpansion.ts +80 -0
  96. package/apps/server/src/backend/memory/qmdPort.test.ts +54 -0
  97. package/apps/server/src/backend/memory/qmdPort.ts +61 -0
  98. package/apps/server/src/backend/memory/records.test.ts +66 -0
  99. package/apps/server/src/backend/memory/records.ts +229 -0
  100. package/apps/server/src/backend/memory/service.ts +2012 -0
  101. package/apps/server/src/backend/memory/sqliteVec.ts +58 -0
  102. package/apps/server/src/backend/memory/types.ts +104 -0
  103. package/apps/server/src/backend/opencode/agentMockingbirdPlugin.test.ts +396 -0
  104. package/apps/server/src/backend/opencode/client.ts +98 -0
  105. package/apps/server/src/backend/opencode/models.ts +41 -0
  106. package/apps/server/src/backend/opencode/systemPrompt.test.ts +146 -0
  107. package/apps/server/src/backend/opencode/systemPrompt.ts +284 -0
  108. package/apps/server/src/backend/paths.ts +57 -0
  109. package/apps/server/src/backend/prompts/service.ts +100 -0
  110. package/apps/server/src/backend/queue/queue.test.ts +189 -0
  111. package/apps/server/src/backend/queue/service.ts +177 -0
  112. package/apps/server/src/backend/queue/types.ts +39 -0
  113. package/apps/server/src/backend/run/service.ts +576 -0
  114. package/apps/server/src/backend/run/storage.ts +47 -0
  115. package/apps/server/src/backend/run/types.ts +44 -0
  116. package/apps/server/src/backend/runtime/errors.ts +61 -0
  117. package/apps/server/src/backend/runtime/index.ts +72 -0
  118. package/apps/server/src/backend/runtime/memoryPromptDedup.test.ts +153 -0
  119. package/apps/server/src/backend/runtime/memoryPromptDedup.ts +76 -0
  120. package/apps/server/src/backend/runtime/opencodeRuntime/backgroundMethods.ts +765 -0
  121. package/apps/server/src/backend/runtime/opencodeRuntime/coreMethods.ts +705 -0
  122. package/apps/server/src/backend/runtime/opencodeRuntime/eventMethods.ts +503 -0
  123. package/apps/server/src/backend/runtime/opencodeRuntime/memoryMethods.ts +462 -0
  124. package/apps/server/src/backend/runtime/opencodeRuntime/promptMethods.ts +1167 -0
  125. package/apps/server/src/backend/runtime/opencodeRuntime/shared.ts +254 -0
  126. package/apps/server/src/backend/runtime/opencodeRuntime.test.ts +2899 -0
  127. package/apps/server/src/backend/runtime/opencodeRuntime.ts +135 -0
  128. package/apps/server/src/backend/runtime/sessionScope.ts +45 -0
  129. package/apps/server/src/backend/skills/service.ts +442 -0
  130. package/apps/server/src/backend/workspace/resolve.ts +27 -0
  131. package/apps/server/src/cli/agent-mockingbird.mjs +2522 -0
  132. package/apps/server/src/cli/agent-mockingbird.test.ts +68 -0
  133. package/apps/server/src/cli/runtime-assets.mjs +269 -0
  134. package/apps/server/src/cli/runtime-assets.test.ts +52 -0
  135. package/apps/server/src/cli/runtime-layout.mjs +75 -0
  136. package/apps/server/src/cli/standaloneBuild.test.ts +19 -0
  137. package/apps/server/src/cli/standaloneBuild.ts +19 -0
  138. package/apps/server/src/cli/standaloneCronBinary.test.ts +187 -0
  139. package/apps/server/src/index.ts +178 -0
  140. package/apps/server/tsconfig.json +12 -0
  141. package/backlog.md +5 -0
  142. package/bin/agent-mockingbird +2522 -0
  143. package/bin/runtime-layout.mjs +75 -0
  144. package/build-bin.ts +34 -0
  145. package/build-cli.mjs +37 -0
  146. package/build.ts +40 -0
  147. package/bun-env.d.ts +11 -0
  148. package/bun.lock +888 -0
  149. package/bunfig.toml +2 -0
  150. package/components.json +21 -0
  151. package/config.json +130 -0
  152. package/deploy/RELEASE_INSTALL.md +112 -0
  153. package/deploy/docker-compose.yml +42 -0
  154. package/deploy/systemd/README.md +46 -0
  155. package/deploy/systemd/agent-mockingbird.service +28 -0
  156. package/deploy/systemd/opencode.service +25 -0
  157. package/docs/legacy-config-ui-reference.md +51 -0
  158. package/docs/memory-e2e-trace-2026-03-04.md +63 -0
  159. package/docs/memory-ops.md +96 -0
  160. package/docs/memory-runtime-contract.md +42 -0
  161. package/docs/memory-tuning-remote-2026-03-04.md +59 -0
  162. package/docs/opencode-rebase-workflow-plan.md +614 -0
  163. package/docs/opencode-startup-sync-plan.md +94 -0
  164. package/docs/vendor-opencode.md +41 -0
  165. package/drizzle/0000_famous_turbo.sql +49 -0
  166. package/drizzle/0001_cron_memory_aux.sql +160 -0
  167. package/drizzle/0002_runtime_session_bindings.sql +28 -0
  168. package/drizzle/0003_background_runs.sql +27 -0
  169. package/drizzle/0004_memory_open_write.sql +63 -0
  170. package/drizzle/0005_signal_channel.sql +47 -0
  171. package/drizzle/0006_usage_event_dimensions.sql +7 -0
  172. package/drizzle/meta/0000_snapshot.json +341 -0
  173. package/drizzle/meta/_journal.json +55 -0
  174. package/drizzle.config.ts +14 -0
  175. package/eslint.config.mjs +77 -0
  176. package/knip.json +18 -0
  177. package/memory/2026-03-04.md +4 -0
  178. package/opencode.lock.json +16 -0
  179. package/package.json +67 -0
  180. package/packages/agent-mockingbird-installer/README.md +31 -0
  181. package/packages/agent-mockingbird-installer/bin/agent-mockingbird-installer.mjs +44 -0
  182. package/packages/agent-mockingbird-installer/opencode.lock.json +16 -0
  183. package/packages/agent-mockingbird-installer/package.json +23 -0
  184. package/packages/contracts/package.json +19 -0
  185. package/packages/contracts/src/agentTypes.ts +122 -0
  186. package/packages/contracts/src/cron.ts +146 -0
  187. package/packages/contracts/src/dashboard.ts +378 -0
  188. package/packages/contracts/src/index.ts +3 -0
  189. package/packages/contracts/tsconfig.json +4 -0
  190. package/patches/opencode/0001-Wafflebot-OpenCode-baseline.patch +2341 -0
  191. package/patches/opencode/0002-Fix-OpenCode-web-entry-and-settings-icons.patch +104 -0
  192. package/patches/opencode/0003-fix-app-remove-duplicate-sidebar-mount.patch +32 -0
  193. package/patches/opencode/0004-Add-heartbeat-settings-and-usage-nav.patch +506 -0
  194. package/patches/opencode/0005-Use-chart-icon-for-usage-nav.patch +38 -0
  195. package/patches/opencode/0006-Modernize-cron-settings.patch +399 -0
  196. package/patches/opencode/0007-Rename-waffle-namespaces-to-mockingbird.patch +1110 -0
  197. package/patches/opencode/0008-Remove-cron-contract-section.patch +178 -0
  198. package/patches/opencode/0009-Rework-cron-tab-as-operations-console.patch +414 -0
  199. package/patches/opencode/0010-Refine-heartbeat-settings-controls.patch +208 -0
  200. package/runtime-assets/opencode-config/opencode.jsonc +25 -0
  201. package/runtime-assets/opencode-config/package.json +5 -0
  202. package/runtime-assets/opencode-config/plugins/agent-mockingbird.ts +715 -0
  203. package/runtime-assets/workspace/.agents/skills/config-auditor/SKILL.md +25 -0
  204. package/runtime-assets/workspace/.agents/skills/config-editor/SKILL.md +24 -0
  205. package/runtime-assets/workspace/.agents/skills/cron-manager/SKILL.md +57 -0
  206. package/runtime-assets/workspace/.agents/skills/memory-ops/SKILL.md +120 -0
  207. package/runtime-assets/workspace/.agents/skills/runtime-diagnose/SKILL.md +25 -0
  208. package/runtime-assets/workspace/AGENTS.md +56 -0
  209. package/runtime-assets/workspace/MEMORY.md +4 -0
  210. package/scripts/build-release-bundle.sh +66 -0
  211. package/scripts/check-ship.ts +383 -0
  212. package/scripts/dev-opencode.sh +17 -0
  213. package/scripts/dev-stack-opencode.sh +15 -0
  214. package/scripts/dev-stack.sh +61 -0
  215. package/scripts/install-systemd.sh +87 -0
  216. package/scripts/memory-e2e.sh +76 -0
  217. package/scripts/memory-trace-e2e.sh +141 -0
  218. package/scripts/migrate-opencode-env.ts +108 -0
  219. package/scripts/onboard/bootstrap.sh +32 -0
  220. package/scripts/opencode-swap.ts +78 -0
  221. package/scripts/opencode-sync.ts +715 -0
  222. package/scripts/runtime-assets-sync.mjs +83 -0
  223. package/scripts/setup-git-hooks.ts +39 -0
  224. package/tsconfig.json +45 -0
  225. package/tui.json +98 -0
  226. package/turbo.json +36 -0
  227. package/vendor/OPENCODE_VENDOR.md +13 -0
@@ -0,0 +1,765 @@
1
+ import {
2
+ BACKGROUND_MESSAGE_SYNC_MIN_INTERVAL_MS,
3
+ BACKGROUND_SYNC_BATCH_LIMIT,
4
+ OPENCODE_RUNTIME_ID,
5
+ SESSION_SYNC_MESSAGE_LIMIT,
6
+ logger,
7
+ normalizeCostDelta,
8
+ normalizeUsageDelta,
9
+ type AssistantInfo,
10
+ type BackgroundRunHandle,
11
+ type BackgroundRunStatus,
12
+ type Message,
13
+ type OpencodeSessionStatus,
14
+ type Part,
15
+ type Session,
16
+ } from "./shared";
17
+ import {
18
+ createBackgroundRunUpdatedEvent,
19
+ createHeartbeatUpdatedEvent,
20
+ createSessionMessageCreatedEvent,
21
+ createSessionStateUpdatedEvent,
22
+ createUsageUpdatedEvent,
23
+ } from "../../contracts/events";
24
+ import {
25
+ appendAssistantMessage,
26
+ createBackgroundRun,
27
+ ensureSessionForRuntimeBinding,
28
+ getBackgroundRunByChildExternalSessionId,
29
+ getBackgroundRunById,
30
+ getLocalSessionIdByRuntimeBinding,
31
+ getRuntimeSessionBinding,
32
+ getSessionById,
33
+ getUsageSnapshot,
34
+ listBackgroundRunsForParentSession,
35
+ listBackgroundRunsPendingAnnouncement,
36
+ listInFlightBackgroundRuns,
37
+ listRuntimeSessionBindings,
38
+ recordUsageDelta,
39
+ setBackgroundRunStatus,
40
+ setRuntimeSessionBinding,
41
+ setSessionTitle,
42
+ upsertSessionMessages,
43
+ type BackgroundRunRecord,
44
+ } from "../../db/repository";
45
+ import { unwrapSdkData } from "../../opencode/client";
46
+ import { getLaneQueue } from "../../queue/service";
47
+ import type { OpencodeRuntime } from "../opencodeRuntime";
48
+
49
+ export interface OpencodeRuntimeBackgroundMethods {
50
+ syncBackgroundRuns(): Promise<void>;
51
+ reconcileBackgroundChildrenFromParents(): Promise<void>;
52
+ refreshInFlightBackgroundRuns(): Promise<void>;
53
+ processPendingBackgroundAnnouncements(): Promise<void>;
54
+ backgroundRecordToHandle(run: BackgroundRunRecord): BackgroundRunHandle;
55
+ backgroundRecordFingerprint(run: BackgroundRunRecord): string;
56
+ emitBackgroundRunUpdated(run: BackgroundRunRecord, force?: boolean): void;
57
+ ensureLocalSessionForBackgroundRun(
58
+ run: BackgroundRunRecord,
59
+ sessionInfo?: Session,
60
+ ): string | null;
61
+ mapOpencodeMessageContent(info: Message, parts: Array<Part>): string;
62
+ syncLocalSessionFromOpencode(input: {
63
+ localSessionId: string;
64
+ externalSessionId: string;
65
+ force?: boolean;
66
+ titleHint?: string;
67
+ messages?: Array<{ info: Message; parts: Array<Part> }>;
68
+ }): Promise<void>;
69
+ syncMessageById(input: {
70
+ localSessionId: string;
71
+ externalSessionId: string;
72
+ messageId: string;
73
+ titleHint?: string;
74
+ }): Promise<void>;
75
+ syncBackgroundSessionMessages(
76
+ run: BackgroundRunRecord,
77
+ force?: boolean,
78
+ messages?: Array<{ info: Message; parts: Array<Part> }>,
79
+ ): Promise<void>;
80
+ ensureBackgroundRunForSessionInfo(
81
+ sessionInfo: Session,
82
+ status?: BackgroundRunStatus,
83
+ knownParentSessionId?: string,
84
+ ): BackgroundRunRecord | null;
85
+ hydrateBackgroundRunFromSessionId(
86
+ childExternalSessionId: string,
87
+ status?: OpencodeSessionStatus,
88
+ ): Promise<BackgroundRunRecord | null>;
89
+ announceBackgroundRunIfNeeded(runId: string): Promise<boolean>;
90
+ applyBackgroundStatusBySessionId(
91
+ opencodeSessionId: string,
92
+ status: OpencodeSessionStatus,
93
+ ): void;
94
+ applyOpencodeBackgroundStatus(
95
+ run: BackgroundRunRecord,
96
+ status: OpencodeSessionStatus,
97
+ ): BackgroundRunRecord;
98
+ inFlightBackgroundChildRunCount(parentSessionId: string): number;
99
+ maybeDrainSessionQueue(sessionId: string): Promise<void>;
100
+ markBackgroundRunFailed(opencodeSessionId: string, message: string): void;
101
+ }
102
+
103
+ export const opencodeRuntimeBackgroundMethods: OpencodeRuntimeBackgroundMethods =
104
+ {
105
+ async syncBackgroundRuns(this: OpencodeRuntime) {
106
+ if (this["backgroundSyncInFlight"]) {
107
+ await this["backgroundSyncInFlight"];
108
+ return;
109
+ }
110
+ const task = (async () => {
111
+ try {
112
+ await this.reconcileBackgroundChildrenFromParents();
113
+ await this.refreshInFlightBackgroundRuns();
114
+ await this.processPendingBackgroundAnnouncements();
115
+ } catch (error) {
116
+ logger.warnWithCause("Background sync failed", error);
117
+ }
118
+ })();
119
+ this["backgroundSyncInFlight"] = task;
120
+ try {
121
+ await task;
122
+ } finally {
123
+ this["backgroundSyncInFlight"] = null;
124
+ }
125
+ },
126
+
127
+ async reconcileBackgroundChildrenFromParents(this: OpencodeRuntime) {
128
+ const bindings = listRuntimeSessionBindings(
129
+ OPENCODE_RUNTIME_ID,
130
+ BACKGROUND_SYNC_BATCH_LIMIT,
131
+ );
132
+ for (const binding of bindings) {
133
+ try {
134
+ const children = unwrapSdkData<Array<Session>>(
135
+ await this.getClient().session.children({
136
+ path: { id: binding.externalSessionId },
137
+ responseStyle: "data",
138
+ throwOnError: true,
139
+ signal: this.defaultRequestSignal(),
140
+ }),
141
+ );
142
+ for (const child of children) {
143
+ this.ensureBackgroundRunForSessionInfo(
144
+ child,
145
+ "created",
146
+ binding.sessionId,
147
+ );
148
+ }
149
+ } catch (error) {
150
+ logger.warnWithCause(
151
+ "Background child reconciliation failed",
152
+ error,
153
+ {
154
+ externalSessionId: binding.externalSessionId,
155
+ sessionId: binding.sessionId,
156
+ },
157
+ );
158
+ }
159
+ }
160
+ },
161
+
162
+ async refreshInFlightBackgroundRuns(this: OpencodeRuntime) {
163
+ const runs = listInFlightBackgroundRuns(
164
+ OPENCODE_RUNTIME_ID,
165
+ BACKGROUND_SYNC_BATCH_LIMIT,
166
+ );
167
+ for (const run of runs) {
168
+ await this.getBackgroundStatus(run.id);
169
+ }
170
+ },
171
+
172
+ async processPendingBackgroundAnnouncements(this: OpencodeRuntime) {
173
+ const pending = listBackgroundRunsPendingAnnouncement(
174
+ OPENCODE_RUNTIME_ID,
175
+ BACKGROUND_SYNC_BATCH_LIMIT,
176
+ );
177
+ for (const run of pending) {
178
+ await this.announceBackgroundRunIfNeeded(run.id);
179
+ }
180
+ },
181
+
182
+ backgroundRecordToHandle(this: OpencodeRuntime, run: BackgroundRunRecord) {
183
+ const childSessionId = getLocalSessionIdByRuntimeBinding(
184
+ OPENCODE_RUNTIME_ID,
185
+ run.childExternalSessionId,
186
+ );
187
+ return {
188
+ runId: run.id,
189
+ parentSessionId: run.parentSessionId,
190
+ parentExternalSessionId: run.parentExternalSessionId,
191
+ childExternalSessionId: run.childExternalSessionId,
192
+ childSessionId,
193
+ requestedBy: run.requestedBy,
194
+ prompt: run.prompt,
195
+ status: run.status,
196
+ resultSummary: run.resultSummary,
197
+ createdAt: run.createdAt,
198
+ updatedAt: run.updatedAt,
199
+ startedAt: run.startedAt,
200
+ completedAt: run.completedAt,
201
+ error: run.error,
202
+ };
203
+ },
204
+
205
+ backgroundRecordFingerprint(
206
+ this: OpencodeRuntime,
207
+ run: BackgroundRunRecord,
208
+ ) {
209
+ const childSessionId =
210
+ getLocalSessionIdByRuntimeBinding(
211
+ OPENCODE_RUNTIME_ID,
212
+ run.childExternalSessionId,
213
+ ) ?? "";
214
+ return [
215
+ run.status,
216
+ run.error ?? "",
217
+ run.resultSummary ?? "",
218
+ run.prompt,
219
+ childSessionId,
220
+ run.startedAt ?? "",
221
+ run.completedAt ?? "",
222
+ run.updatedAt,
223
+ ].join("|");
224
+ },
225
+
226
+ emitBackgroundRunUpdated(this: OpencodeRuntime, run, force = false) {
227
+ const nextFingerprint = this.backgroundRecordFingerprint(run);
228
+ const previous = this["backgroundLastEmitByRunId"].get(run.id);
229
+ if (!force && previous === nextFingerprint) return;
230
+ this["backgroundLastEmitByRunId"].set(run.id, nextFingerprint);
231
+ this.emit(
232
+ createBackgroundRunUpdatedEvent(
233
+ {
234
+ runId: run.id,
235
+ parentSessionId: run.parentSessionId,
236
+ parentExternalSessionId: run.parentExternalSessionId,
237
+ childExternalSessionId: run.childExternalSessionId,
238
+ childSessionId: getLocalSessionIdByRuntimeBinding(
239
+ OPENCODE_RUNTIME_ID,
240
+ run.childExternalSessionId,
241
+ ),
242
+ requestedBy: run.requestedBy,
243
+ prompt: run.prompt,
244
+ status: run.status,
245
+ resultSummary: run.resultSummary,
246
+ error: run.error,
247
+ createdAt: run.createdAt,
248
+ updatedAt: run.updatedAt,
249
+ startedAt: run.startedAt,
250
+ completedAt: run.completedAt,
251
+ },
252
+ "runtime",
253
+ ),
254
+ );
255
+ if (
256
+ run.status === "completed" ||
257
+ run.status === "failed" ||
258
+ run.status === "aborted"
259
+ ) {
260
+ this["backgroundLastEmitByRunId"].delete(run.id);
261
+ this["backgroundMessageSyncAtByChildSessionId"].delete(
262
+ run.childExternalSessionId,
263
+ );
264
+ }
265
+ },
266
+
267
+ ensureLocalSessionForBackgroundRun(
268
+ this: OpencodeRuntime,
269
+ run,
270
+ sessionInfo,
271
+ ) {
272
+ const existingSessionId = getLocalSessionIdByRuntimeBinding(
273
+ OPENCODE_RUNTIME_ID,
274
+ run.childExternalSessionId,
275
+ );
276
+ const existingSession = existingSessionId
277
+ ? getSessionById(existingSessionId)
278
+ : null;
279
+ const parentSession = getSessionById(run.parentSessionId);
280
+ const remoteTitle = sessionInfo?.title?.trim();
281
+ const ensured = ensureSessionForRuntimeBinding({
282
+ runtime: OPENCODE_RUNTIME_ID,
283
+ externalSessionId: run.childExternalSessionId,
284
+ title:
285
+ remoteTitle ||
286
+ existingSession?.title ||
287
+ `${parentSession?.title?.trim() || "Session"} background`,
288
+ model: parentSession?.model,
289
+ });
290
+ if (!ensured) return null;
291
+ if (
292
+ !existingSession ||
293
+ existingSession.title !== ensured.title ||
294
+ existingSession.model !== ensured.model
295
+ ) {
296
+ this.emit(createSessionStateUpdatedEvent(ensured, "runtime"));
297
+ }
298
+ return ensured.id;
299
+ },
300
+
301
+ mapOpencodeMessageContent(this: OpencodeRuntime, info, parts) {
302
+ const text = this.extractText(parts);
303
+ if (text && text.trim()) return text;
304
+ if (info.role === "assistant") {
305
+ const reasoningText = this.extractReasoningText(parts);
306
+ if (reasoningText) return reasoningText;
307
+ const toolOutputText = this.extractCompletedToolOutputText(parts);
308
+ if (toolOutputText) return toolOutputText;
309
+ }
310
+ if (info.role === "user") {
311
+ const subtaskPrompt = this.extractSubtaskPrompt(parts);
312
+ if (subtaskPrompt) return subtaskPrompt;
313
+ }
314
+ if (info.role === "assistant") {
315
+ const failure = this.extractAssistantError(
316
+ info as AssistantInfo,
317
+ parts,
318
+ );
319
+ if (failure) return `[assistant error] ${failure}`;
320
+ }
321
+ return "";
322
+ },
323
+
324
+ async syncLocalSessionFromOpencode(this: OpencodeRuntime, input) {
325
+ const localSessionId = input.localSessionId.trim();
326
+ const externalSessionId = input.externalSessionId.trim();
327
+ if (!localSessionId || !externalSessionId) return;
328
+ const existingSession = getSessionById(localSessionId);
329
+ if (!existingSession) return;
330
+ if (
331
+ getRuntimeSessionBinding(OPENCODE_RUNTIME_ID, localSessionId) !==
332
+ externalSessionId
333
+ ) {
334
+ setRuntimeSessionBinding(
335
+ OPENCODE_RUNTIME_ID,
336
+ localSessionId,
337
+ externalSessionId,
338
+ );
339
+ }
340
+ const now = Date.now();
341
+ if (!input.force) {
342
+ const lastSyncedAt =
343
+ this["backgroundMessageSyncAtByChildSessionId"].get(
344
+ externalSessionId,
345
+ ) ?? 0;
346
+ if (now - lastSyncedAt < BACKGROUND_MESSAGE_SYNC_MIN_INTERVAL_MS)
347
+ return;
348
+ }
349
+ this["backgroundMessageSyncAtByChildSessionId"].set(
350
+ externalSessionId,
351
+ now,
352
+ );
353
+ const normalizedTitle = input.titleHint?.trim();
354
+ if (normalizedTitle && normalizedTitle !== existingSession.title) {
355
+ const updatedTitle = setSessionTitle(localSessionId, normalizedTitle);
356
+ if (updatedTitle)
357
+ this.emit(createSessionStateUpdatedEvent(updatedTitle, "runtime"));
358
+ }
359
+ let messages = input.messages;
360
+ if (!messages) {
361
+ messages = unwrapSdkData<Array<{ info: Message; parts: Array<Part> }>>(
362
+ await this.getClient().session.messages({
363
+ path: { id: externalSessionId },
364
+ query: { limit: SESSION_SYNC_MESSAGE_LIMIT },
365
+ responseStyle: "data",
366
+ throwOnError: true,
367
+ signal: this.defaultRequestSignal(),
368
+ }),
369
+ );
370
+ }
371
+ for (const entry of messages) {
372
+ this.rememberMessageRole(
373
+ externalSessionId,
374
+ entry.info.id,
375
+ entry.info.role,
376
+ );
377
+ for (const part of entry.parts) {
378
+ this.rememberPartMetadata(part);
379
+ }
380
+ }
381
+ const imported = messages.flatMap((entry) => {
382
+ const content = this.mapOpencodeMessageContent(
383
+ entry.info,
384
+ entry.parts,
385
+ ).trim();
386
+ if (!content) return [];
387
+ const parts = entry.info.role === "assistant" ? this.buildChatMessageParts(entry.parts) : [];
388
+ return [
389
+ {
390
+ id: entry.info.id,
391
+ role: entry.info.role,
392
+ content,
393
+ createdAt: entry.info.time.created,
394
+ parts: parts.length > 0 ? parts : undefined,
395
+ },
396
+ ];
397
+ });
398
+ const synced = upsertSessionMessages({
399
+ sessionId: localSessionId,
400
+ messages: imported,
401
+ touchedAt: now,
402
+ });
403
+ if (!synced) return;
404
+ const entriesById = new Map(
405
+ messages.map((entry) => [entry.info.id, entry] as const),
406
+ );
407
+ for (const message of synced.inserted) {
408
+ if (message.role !== "assistant") continue;
409
+ const entry = entriesById.get(message.id);
410
+ if (!entry || entry.info.role !== "assistant") continue;
411
+ recordUsageDelta({
412
+ id: `assistant-message:${entry.info.id}`,
413
+ sessionId: localSessionId,
414
+ providerId: entry.info.providerID ?? null,
415
+ modelId: entry.info.modelID ?? null,
416
+ requestCountDelta: 1,
417
+ inputTokensDelta: normalizeUsageDelta(entry.info.tokens?.input),
418
+ outputTokensDelta: normalizeUsageDelta(entry.info.tokens?.output),
419
+ estimatedCostUsdDelta: normalizeCostDelta(entry.info.cost),
420
+ source: "runtime",
421
+ createdAt:
422
+ entry.info.time?.completed ?? entry.info.time?.created ?? now,
423
+ });
424
+ }
425
+ if (synced.inserted.length === 0) return;
426
+ for (const message of synced.inserted) {
427
+ this.emit(
428
+ createSessionMessageCreatedEvent(
429
+ { sessionId: localSessionId, message },
430
+ "runtime",
431
+ ),
432
+ );
433
+ }
434
+ this.emit(createSessionStateUpdatedEvent(synced.session, "runtime"));
435
+ this.emit(createUsageUpdatedEvent(getUsageSnapshot(), "runtime"));
436
+ },
437
+
438
+ async syncMessageById(this: OpencodeRuntime, input) {
439
+ try {
440
+ const entry = unwrapSdkData<{ info: Message; parts: Array<Part> }>(
441
+ await this.getClient().session.message({
442
+ path: { id: input.externalSessionId, messageID: input.messageId },
443
+ responseStyle: "data",
444
+ throwOnError: true,
445
+ signal: this.defaultRequestSignal(),
446
+ }),
447
+ );
448
+ await this.syncLocalSessionFromOpencode({
449
+ localSessionId: input.localSessionId,
450
+ externalSessionId: input.externalSessionId,
451
+ force: true,
452
+ titleHint: input.titleHint,
453
+ messages: [entry],
454
+ });
455
+ } catch (error) {
456
+ logger.warnWithCause("Message reconciliation failed", error, input);
457
+ }
458
+ },
459
+
460
+ async syncBackgroundSessionMessages(
461
+ this: OpencodeRuntime,
462
+ run,
463
+ force = false,
464
+ messages,
465
+ ) {
466
+ const childSessionId = this.ensureLocalSessionForBackgroundRun(run);
467
+ if (!childSessionId) return;
468
+ try {
469
+ await this.syncLocalSessionFromOpencode({
470
+ localSessionId: childSessionId,
471
+ externalSessionId: run.childExternalSessionId,
472
+ force,
473
+ messages,
474
+ });
475
+ } catch (error) {
476
+ logger.warnWithCause("Background transcript sync failed", error, {
477
+ runId: run.id,
478
+ childExternalSessionId: run.childExternalSessionId,
479
+ });
480
+ }
481
+ },
482
+
483
+ ensureBackgroundRunForSessionInfo(
484
+ this: OpencodeRuntime,
485
+ sessionInfo,
486
+ status = "created",
487
+ knownParentSessionId,
488
+ ) {
489
+ const childExternalSessionId = sessionInfo.id.trim();
490
+ const parentExternalSessionId = sessionInfo.parentID?.trim();
491
+ if (!childExternalSessionId || !parentExternalSessionId) return null;
492
+ const parentSessionId =
493
+ knownParentSessionId?.trim() ||
494
+ getLocalSessionIdByRuntimeBinding(
495
+ OPENCODE_RUNTIME_ID,
496
+ parentExternalSessionId,
497
+ );
498
+ if (!parentSessionId) return null;
499
+ const run = createBackgroundRun({
500
+ runtime: OPENCODE_RUNTIME_ID,
501
+ parentSessionId,
502
+ parentExternalSessionId,
503
+ childExternalSessionId,
504
+ requestedBy: "runtime-sync",
505
+ status,
506
+ });
507
+ if (run) {
508
+ this.ensureLocalSessionForBackgroundRun(run, sessionInfo);
509
+ this.emitBackgroundRunUpdated(run);
510
+ }
511
+ return run;
512
+ },
513
+
514
+ async hydrateBackgroundRunFromSessionId(
515
+ this: OpencodeRuntime,
516
+ childExternalSessionId,
517
+ status,
518
+ ) {
519
+ const normalizedChildExternalSessionId = childExternalSessionId.trim();
520
+ if (!normalizedChildExternalSessionId) return null;
521
+ if (
522
+ this["backgroundHydrationInFlight"].has(
523
+ normalizedChildExternalSessionId,
524
+ )
525
+ )
526
+ return null;
527
+ this["backgroundHydrationInFlight"].add(normalizedChildExternalSessionId);
528
+ try {
529
+ const sessionInfo = unwrapSdkData<Session>(
530
+ await this.getClient().session.get({
531
+ path: { id: normalizedChildExternalSessionId },
532
+ responseStyle: "data",
533
+ throwOnError: true,
534
+ signal: this.defaultRequestSignal(),
535
+ }),
536
+ );
537
+ const run = this.ensureBackgroundRunForSessionInfo(
538
+ sessionInfo,
539
+ "created",
540
+ );
541
+ if (run && status) {
542
+ return this.applyOpencodeBackgroundStatus(run, status);
543
+ }
544
+ return run;
545
+ } catch (error) {
546
+ logger.warnWithCause("Background run hydration failed", error, {
547
+ childExternalSessionId: normalizedChildExternalSessionId,
548
+ });
549
+ return null;
550
+ } finally {
551
+ this["backgroundHydrationInFlight"].delete(
552
+ normalizedChildExternalSessionId,
553
+ );
554
+ }
555
+ },
556
+
557
+ async announceBackgroundRunIfNeeded(this: OpencodeRuntime, runId) {
558
+ const normalizedRunId = runId.trim();
559
+ if (!normalizedRunId) return false;
560
+ if (this["backgroundAnnouncementInFlight"].has(normalizedRunId))
561
+ return false;
562
+ const current = getBackgroundRunById(normalizedRunId);
563
+ if (!current || current.status !== "completed") return false;
564
+ if (current.resultSummary && current.resultSummary.trim()) return false;
565
+ this["backgroundAnnouncementInFlight"].add(normalizedRunId);
566
+ try {
567
+ const latest = getBackgroundRunById(normalizedRunId);
568
+ if (!latest || latest.status !== "completed") return false;
569
+ if (latest.resultSummary && latest.resultSummary.trim()) return false;
570
+ const messages = unwrapSdkData<
571
+ Array<{ info: Message; parts: Array<Part> }>
572
+ >(
573
+ await this.getClient().session.messages({
574
+ path: { id: latest.childExternalSessionId },
575
+ query: { limit: 50 },
576
+ responseStyle: "data",
577
+ throwOnError: true,
578
+ signal: this.defaultRequestSignal(),
579
+ }),
580
+ );
581
+ await this.syncBackgroundSessionMessages(latest, true, messages);
582
+ const latestAssistant = [...messages]
583
+ .reverse()
584
+ .find((entry) => entry.info.role === "assistant");
585
+ const assistantText = latestAssistant
586
+ ? this.mapOpencodeMessageContent(
587
+ latestAssistant.info,
588
+ latestAssistant.parts,
589
+ ).trim()
590
+ : "";
591
+ const resultSummary = this.summarizeBackgroundResult(assistantText);
592
+ const childSessionId = getLocalSessionIdByRuntimeBinding(
593
+ OPENCODE_RUNTIME_ID,
594
+ latest.childExternalSessionId,
595
+ );
596
+ const summaryLine =
597
+ resultSummary && resultSummary !== "Background run completed."
598
+ ? resultSummary
599
+ : "run completed.";
600
+ const announcementText = `[Background ${latest.id}] ${summaryLine}\nChild session: ${childSessionId ?? latest.childExternalSessionId}`;
601
+ const appended = appendAssistantMessage({
602
+ sessionId: latest.parentSessionId,
603
+ content: announcementText,
604
+ source: "runtime",
605
+ });
606
+ if (!appended) {
607
+ const failed =
608
+ setBackgroundRunStatus({
609
+ runId: latest.id,
610
+ status: "failed",
611
+ completedAt: Date.now(),
612
+ error: `Unable to append announcement to parent session ${latest.parentSessionId}.`,
613
+ }) ?? latest;
614
+ this.emitBackgroundRunUpdated(failed);
615
+ return false;
616
+ }
617
+ const updated = setBackgroundRunStatus({
618
+ runId: latest.id,
619
+ status: "completed",
620
+ resultSummary,
621
+ error: null,
622
+ });
623
+ if (updated) this.emitBackgroundRunUpdated(updated);
624
+ this.emit(
625
+ createSessionMessageCreatedEvent(
626
+ { sessionId: appended.session.id, message: appended.message },
627
+ "runtime",
628
+ ),
629
+ );
630
+ this.emit(createSessionStateUpdatedEvent(appended.session, "runtime"));
631
+ this.emit(createUsageUpdatedEvent(appended.usage, "runtime"));
632
+ this.emit(createHeartbeatUpdatedEvent(appended.heartbeat, "runtime"));
633
+ return Boolean(updated);
634
+ } catch (error) {
635
+ const failed = setBackgroundRunStatus({
636
+ runId: normalizedRunId,
637
+ status: "failed",
638
+ completedAt: Date.now(),
639
+ error: this.normalizeRuntimeError(error).message,
640
+ });
641
+ if (failed) this.emitBackgroundRunUpdated(failed);
642
+ return false;
643
+ } finally {
644
+ this["backgroundAnnouncementInFlight"].delete(normalizedRunId);
645
+ }
646
+ },
647
+
648
+ applyBackgroundStatusBySessionId(
649
+ this: OpencodeRuntime,
650
+ opencodeSessionId,
651
+ status,
652
+ ) {
653
+ const run = getBackgroundRunByChildExternalSessionId(
654
+ OPENCODE_RUNTIME_ID,
655
+ opencodeSessionId,
656
+ );
657
+ if (run) {
658
+ this.applyOpencodeBackgroundStatus(run, status);
659
+ return;
660
+ }
661
+ void this.hydrateBackgroundRunFromSessionId(opencodeSessionId, status);
662
+ },
663
+
664
+ applyOpencodeBackgroundStatus(this: OpencodeRuntime, run, status) {
665
+ if (run.status === "aborted" || run.status === "failed") return run;
666
+ const now = Date.now();
667
+ const hasStarted = Boolean(run.startedAt);
668
+ let nextStatus: BackgroundRunStatus | null = null;
669
+ let startedAt: number | undefined;
670
+ let completedAt: number | null | undefined;
671
+ let error: string | null | undefined;
672
+ if (status.type === "busy") {
673
+ nextStatus = "running";
674
+ startedAt = hasStarted ? undefined : now;
675
+ completedAt = null;
676
+ error = null;
677
+ } else if (status.type === "retry") {
678
+ nextStatus = "retrying";
679
+ startedAt = hasStarted ? undefined : now;
680
+ completedAt = null;
681
+ error = this.normalizeProviderMessage(status.message) || status.message;
682
+ } else if (status.type === "idle") {
683
+ if (!hasStarted && run.status === "created") {
684
+ nextStatus = "idle";
685
+ } else if (run.status !== "completed") {
686
+ nextStatus = "completed";
687
+ completedAt = now;
688
+ error = null;
689
+ }
690
+ }
691
+ if (
692
+ !nextStatus ||
693
+ (nextStatus === run.status &&
694
+ typeof completedAt === "undefined" &&
695
+ typeof error === "undefined")
696
+ ) {
697
+ return run;
698
+ }
699
+ const updated =
700
+ setBackgroundRunStatus({
701
+ runId: run.id,
702
+ status: nextStatus,
703
+ startedAt,
704
+ completedAt,
705
+ error,
706
+ }) ?? run;
707
+ this.emitBackgroundRunUpdated(updated);
708
+ if (updated.status === "completed") {
709
+ void this.announceBackgroundRunIfNeeded(updated.id);
710
+ }
711
+ void this.maybeDrainSessionQueue(updated.parentSessionId);
712
+ return updated;
713
+ },
714
+
715
+ inFlightBackgroundChildRunCount(
716
+ this: OpencodeRuntime,
717
+ parentSessionId: string,
718
+ ) {
719
+ const runs = listBackgroundRunsForParentSession(parentSessionId, 200);
720
+ return runs.filter(
721
+ (run) =>
722
+ run.status === "created" ||
723
+ run.status === "running" ||
724
+ run.status === "retrying" ||
725
+ run.status === "idle",
726
+ ).length;
727
+ },
728
+
729
+ async maybeDrainSessionQueue(this: OpencodeRuntime, sessionId) {
730
+ if (this["busySessions"].has(sessionId)) return;
731
+ if (this["drainingSessions"].has(sessionId)) return;
732
+ if (this.inFlightBackgroundChildRunCount(sessionId) > 0) return;
733
+ try {
734
+ const queue = getLaneQueue();
735
+ if (queue.depth(sessionId) === 0) return;
736
+ this["drainingSessions"].add(sessionId);
737
+ while (queue.depth(sessionId) > 0) {
738
+ await queue.drainAndExecute(sessionId);
739
+ }
740
+ } catch (error) {
741
+ logger.warnWithCause("Queue drain failed", error, { sessionId });
742
+ } finally {
743
+ this["drainingSessions"].delete(sessionId);
744
+ }
745
+ },
746
+
747
+ markBackgroundRunFailed(this: OpencodeRuntime, opencodeSessionId, message) {
748
+ const run = getBackgroundRunByChildExternalSessionId(
749
+ OPENCODE_RUNTIME_ID,
750
+ opencodeSessionId,
751
+ );
752
+ if (!run) {
753
+ void this.hydrateBackgroundRunFromSessionId(opencodeSessionId);
754
+ return;
755
+ }
756
+ if (run.status === "aborted" || run.status === "completed") return;
757
+ const updated = setBackgroundRunStatus({
758
+ runId: run.id,
759
+ status: "failed",
760
+ completedAt: Date.now(),
761
+ error: message,
762
+ });
763
+ if (updated) this.emitBackgroundRunUpdated(updated);
764
+ },
765
+ };