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.
- package/.agents/skills/btca-cli/SKILL.md +64 -0
- package/.agents/skills/btca-cli/agents/openai.yaml +3 -0
- package/.agents/skills/frontend-design/SKILL.md +42 -0
- package/.agents/skills/frontend-design/agents/openai.yaml +3 -0
- package/.env.example +36 -0
- package/.githooks/pre-commit +33 -0
- package/.github/workflows/ci.yml +309 -0
- package/.opencode/bun.lock +18 -0
- package/.opencode/package.json +5 -0
- package/.opencode/tools/agent_type_manager.ts +100 -0
- package/.opencode/tools/config_manager.ts +87 -0
- package/.opencode/tools/cron_manager.ts +145 -0
- package/.opencode/tools/memory_get.ts +43 -0
- package/.opencode/tools/memory_remember.ts +53 -0
- package/.opencode/tools/memory_search.ts +48 -0
- package/AGENTS.md +126 -0
- package/MEMORY.md +2 -0
- package/README.md +451 -0
- package/THIRD_PARTY_NOTICES.md +11 -0
- package/agent-mockingbird.config.example.json +135 -0
- package/apps/server/package.json +32 -0
- package/apps/server/src/backend/agents/bootstrapContext.ts +362 -0
- package/apps/server/src/backend/agents/openclawImport.test.ts +133 -0
- package/apps/server/src/backend/agents/openclawImport.ts +797 -0
- package/apps/server/src/backend/agents/opencodeConfig.ts +428 -0
- package/apps/server/src/backend/agents/service.ts +10 -0
- package/apps/server/src/backend/config/example-config.test.ts +20 -0
- package/apps/server/src/backend/config/orchestration.ts +243 -0
- package/apps/server/src/backend/config/policy.ts +158 -0
- package/apps/server/src/backend/config/schema.test.ts +15 -0
- package/apps/server/src/backend/config/schema.ts +391 -0
- package/apps/server/src/backend/config/semantic.test.ts +34 -0
- package/apps/server/src/backend/config/semantic.ts +149 -0
- package/apps/server/src/backend/config/service.test.ts +75 -0
- package/apps/server/src/backend/config/service.ts +207 -0
- package/apps/server/src/backend/config/smoke.ts +77 -0
- package/apps/server/src/backend/config/store.test.ts +123 -0
- package/apps/server/src/backend/config/store.ts +581 -0
- package/apps/server/src/backend/config/testFixtures.ts +5 -0
- package/apps/server/src/backend/config/types.ts +56 -0
- package/apps/server/src/backend/contracts/events.ts +320 -0
- package/apps/server/src/backend/contracts/runtime.ts +111 -0
- package/apps/server/src/backend/cron/executor.ts +435 -0
- package/apps/server/src/backend/cron/repository.ts +170 -0
- package/apps/server/src/backend/cron/service.ts +660 -0
- package/apps/server/src/backend/cron/storage.ts +92 -0
- package/apps/server/src/backend/cron/types.ts +138 -0
- package/apps/server/src/backend/cron/utils.ts +351 -0
- package/apps/server/src/backend/db/client.ts +20 -0
- package/apps/server/src/backend/db/migrate.ts +40 -0
- package/apps/server/src/backend/db/repository.ts +1762 -0
- package/apps/server/src/backend/db/schema.ts +113 -0
- package/apps/server/src/backend/db/usageDashboard.test.ts +102 -0
- package/apps/server/src/backend/db/wipe.ts +13 -0
- package/apps/server/src/backend/defaults.ts +32 -0
- package/apps/server/src/backend/env.ts +48 -0
- package/apps/server/src/backend/heartbeat/activeHours.ts +45 -0
- package/apps/server/src/backend/heartbeat/defaultJob.ts +88 -0
- package/apps/server/src/backend/heartbeat/heartbeat.test.ts +110 -0
- package/apps/server/src/backend/heartbeat/runtimeService.ts +190 -0
- package/apps/server/src/backend/heartbeat/service.ts +176 -0
- package/apps/server/src/backend/heartbeat/state.test.ts +63 -0
- package/apps/server/src/backend/heartbeat/state.ts +167 -0
- package/apps/server/src/backend/heartbeat/types.ts +54 -0
- package/apps/server/src/backend/http/boundedQueue.test.ts +49 -0
- package/apps/server/src/backend/http/boundedQueue.ts +92 -0
- package/apps/server/src/backend/http/parsers.ts +40 -0
- package/apps/server/src/backend/http/router.ts +61 -0
- package/apps/server/src/backend/http/routes/agentRoutes.ts +67 -0
- package/apps/server/src/backend/http/routes/backgroundRoutes.ts +203 -0
- package/apps/server/src/backend/http/routes/chatRoutes.ts +107 -0
- package/apps/server/src/backend/http/routes/configRoutes.ts +602 -0
- package/apps/server/src/backend/http/routes/cronRoutes.ts +221 -0
- package/apps/server/src/backend/http/routes/dashboardRoutes.ts +308 -0
- package/apps/server/src/backend/http/routes/eventRoutes.ts +7 -0
- package/apps/server/src/backend/http/routes/heartbeatRoutes.test.ts +41 -0
- package/apps/server/src/backend/http/routes/heartbeatRoutes.ts +28 -0
- package/apps/server/src/backend/http/routes/index.ts +101 -0
- package/apps/server/src/backend/http/routes/mcpRoutes.ts +213 -0
- package/apps/server/src/backend/http/routes/memoryRoutes.ts +154 -0
- package/apps/server/src/backend/http/routes/runRoutes.ts +310 -0
- package/apps/server/src/backend/http/routes/runtimeRoutes.ts +197 -0
- package/apps/server/src/backend/http/routes/skillRoutes.ts +112 -0
- package/apps/server/src/backend/http/routes/uiRoutes.test.ts +161 -0
- package/apps/server/src/backend/http/routes/uiRoutes.ts +177 -0
- package/apps/server/src/backend/http/routes/usageRoutes.test.ts +104 -0
- package/apps/server/src/backend/http/routes/usageRoutes.ts +767 -0
- package/apps/server/src/backend/http/schemas.ts +64 -0
- package/apps/server/src/backend/http/sse.ts +144 -0
- package/apps/server/src/backend/integration/backend-core.test.ts +2316 -0
- package/apps/server/src/backend/logging/logger.ts +64 -0
- package/apps/server/src/backend/mcp/service.ts +326 -0
- package/apps/server/src/backend/memory/cli.ts +170 -0
- package/apps/server/src/backend/memory/conceptExpansion.test.ts +28 -0
- package/apps/server/src/backend/memory/conceptExpansion.ts +80 -0
- package/apps/server/src/backend/memory/qmdPort.test.ts +54 -0
- package/apps/server/src/backend/memory/qmdPort.ts +61 -0
- package/apps/server/src/backend/memory/records.test.ts +66 -0
- package/apps/server/src/backend/memory/records.ts +229 -0
- package/apps/server/src/backend/memory/service.ts +2012 -0
- package/apps/server/src/backend/memory/sqliteVec.ts +58 -0
- package/apps/server/src/backend/memory/types.ts +104 -0
- package/apps/server/src/backend/opencode/agentMockingbirdPlugin.test.ts +396 -0
- package/apps/server/src/backend/opencode/client.ts +98 -0
- package/apps/server/src/backend/opencode/models.ts +41 -0
- package/apps/server/src/backend/opencode/systemPrompt.test.ts +146 -0
- package/apps/server/src/backend/opencode/systemPrompt.ts +284 -0
- package/apps/server/src/backend/paths.ts +57 -0
- package/apps/server/src/backend/prompts/service.ts +100 -0
- package/apps/server/src/backend/queue/queue.test.ts +189 -0
- package/apps/server/src/backend/queue/service.ts +177 -0
- package/apps/server/src/backend/queue/types.ts +39 -0
- package/apps/server/src/backend/run/service.ts +576 -0
- package/apps/server/src/backend/run/storage.ts +47 -0
- package/apps/server/src/backend/run/types.ts +44 -0
- package/apps/server/src/backend/runtime/errors.ts +61 -0
- package/apps/server/src/backend/runtime/index.ts +72 -0
- package/apps/server/src/backend/runtime/memoryPromptDedup.test.ts +153 -0
- package/apps/server/src/backend/runtime/memoryPromptDedup.ts +76 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/backgroundMethods.ts +765 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/coreMethods.ts +705 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/eventMethods.ts +503 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/memoryMethods.ts +462 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/promptMethods.ts +1167 -0
- package/apps/server/src/backend/runtime/opencodeRuntime/shared.ts +254 -0
- package/apps/server/src/backend/runtime/opencodeRuntime.test.ts +2899 -0
- package/apps/server/src/backend/runtime/opencodeRuntime.ts +135 -0
- package/apps/server/src/backend/runtime/sessionScope.ts +45 -0
- package/apps/server/src/backend/skills/service.ts +442 -0
- package/apps/server/src/backend/workspace/resolve.ts +27 -0
- package/apps/server/src/cli/agent-mockingbird.mjs +2522 -0
- package/apps/server/src/cli/agent-mockingbird.test.ts +68 -0
- package/apps/server/src/cli/runtime-assets.mjs +269 -0
- package/apps/server/src/cli/runtime-assets.test.ts +52 -0
- package/apps/server/src/cli/runtime-layout.mjs +75 -0
- package/apps/server/src/cli/standaloneBuild.test.ts +19 -0
- package/apps/server/src/cli/standaloneBuild.ts +19 -0
- package/apps/server/src/cli/standaloneCronBinary.test.ts +187 -0
- package/apps/server/src/index.ts +178 -0
- package/apps/server/tsconfig.json +12 -0
- package/backlog.md +5 -0
- package/bin/agent-mockingbird +2522 -0
- package/bin/runtime-layout.mjs +75 -0
- package/build-bin.ts +34 -0
- package/build-cli.mjs +37 -0
- package/build.ts +40 -0
- package/bun-env.d.ts +11 -0
- package/bun.lock +888 -0
- package/bunfig.toml +2 -0
- package/components.json +21 -0
- package/config.json +130 -0
- package/deploy/RELEASE_INSTALL.md +112 -0
- package/deploy/docker-compose.yml +42 -0
- package/deploy/systemd/README.md +46 -0
- package/deploy/systemd/agent-mockingbird.service +28 -0
- package/deploy/systemd/opencode.service +25 -0
- package/docs/legacy-config-ui-reference.md +51 -0
- package/docs/memory-e2e-trace-2026-03-04.md +63 -0
- package/docs/memory-ops.md +96 -0
- package/docs/memory-runtime-contract.md +42 -0
- package/docs/memory-tuning-remote-2026-03-04.md +59 -0
- package/docs/opencode-rebase-workflow-plan.md +614 -0
- package/docs/opencode-startup-sync-plan.md +94 -0
- package/docs/vendor-opencode.md +41 -0
- package/drizzle/0000_famous_turbo.sql +49 -0
- package/drizzle/0001_cron_memory_aux.sql +160 -0
- package/drizzle/0002_runtime_session_bindings.sql +28 -0
- package/drizzle/0003_background_runs.sql +27 -0
- package/drizzle/0004_memory_open_write.sql +63 -0
- package/drizzle/0005_signal_channel.sql +47 -0
- package/drizzle/0006_usage_event_dimensions.sql +7 -0
- package/drizzle/meta/0000_snapshot.json +341 -0
- package/drizzle/meta/_journal.json +55 -0
- package/drizzle.config.ts +14 -0
- package/eslint.config.mjs +77 -0
- package/knip.json +18 -0
- package/memory/2026-03-04.md +4 -0
- package/opencode.lock.json +16 -0
- package/package.json +67 -0
- package/packages/agent-mockingbird-installer/README.md +31 -0
- package/packages/agent-mockingbird-installer/bin/agent-mockingbird-installer.mjs +44 -0
- package/packages/agent-mockingbird-installer/opencode.lock.json +16 -0
- package/packages/agent-mockingbird-installer/package.json +23 -0
- package/packages/contracts/package.json +19 -0
- package/packages/contracts/src/agentTypes.ts +122 -0
- package/packages/contracts/src/cron.ts +146 -0
- package/packages/contracts/src/dashboard.ts +378 -0
- package/packages/contracts/src/index.ts +3 -0
- package/packages/contracts/tsconfig.json +4 -0
- package/patches/opencode/0001-Wafflebot-OpenCode-baseline.patch +2341 -0
- package/patches/opencode/0002-Fix-OpenCode-web-entry-and-settings-icons.patch +104 -0
- package/patches/opencode/0003-fix-app-remove-duplicate-sidebar-mount.patch +32 -0
- package/patches/opencode/0004-Add-heartbeat-settings-and-usage-nav.patch +506 -0
- package/patches/opencode/0005-Use-chart-icon-for-usage-nav.patch +38 -0
- package/patches/opencode/0006-Modernize-cron-settings.patch +399 -0
- package/patches/opencode/0007-Rename-waffle-namespaces-to-mockingbird.patch +1110 -0
- package/patches/opencode/0008-Remove-cron-contract-section.patch +178 -0
- package/patches/opencode/0009-Rework-cron-tab-as-operations-console.patch +414 -0
- package/patches/opencode/0010-Refine-heartbeat-settings-controls.patch +208 -0
- package/runtime-assets/opencode-config/opencode.jsonc +25 -0
- package/runtime-assets/opencode-config/package.json +5 -0
- package/runtime-assets/opencode-config/plugins/agent-mockingbird.ts +715 -0
- package/runtime-assets/workspace/.agents/skills/config-auditor/SKILL.md +25 -0
- package/runtime-assets/workspace/.agents/skills/config-editor/SKILL.md +24 -0
- package/runtime-assets/workspace/.agents/skills/cron-manager/SKILL.md +57 -0
- package/runtime-assets/workspace/.agents/skills/memory-ops/SKILL.md +120 -0
- package/runtime-assets/workspace/.agents/skills/runtime-diagnose/SKILL.md +25 -0
- package/runtime-assets/workspace/AGENTS.md +56 -0
- package/runtime-assets/workspace/MEMORY.md +4 -0
- package/scripts/build-release-bundle.sh +66 -0
- package/scripts/check-ship.ts +383 -0
- package/scripts/dev-opencode.sh +17 -0
- package/scripts/dev-stack-opencode.sh +15 -0
- package/scripts/dev-stack.sh +61 -0
- package/scripts/install-systemd.sh +87 -0
- package/scripts/memory-e2e.sh +76 -0
- package/scripts/memory-trace-e2e.sh +141 -0
- package/scripts/migrate-opencode-env.ts +108 -0
- package/scripts/onboard/bootstrap.sh +32 -0
- package/scripts/opencode-swap.ts +78 -0
- package/scripts/opencode-sync.ts +715 -0
- package/scripts/runtime-assets-sync.mjs +83 -0
- package/scripts/setup-git-hooks.ts +39 -0
- package/tsconfig.json +45 -0
- package/tui.json +98 -0
- package/turbo.json +36 -0
- 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
|
+
};
|