@clinebot/core 0.0.21 → 0.0.23
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/dist/ClineCore.d.ts +110 -0
- package/dist/ClineCore.d.ts.map +1 -0
- package/dist/account/cline-account-service.d.ts +2 -1
- package/dist/account/cline-account-service.d.ts.map +1 -1
- package/dist/account/index.d.ts +1 -1
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/rpc.d.ts +3 -1
- package/dist/account/rpc.d.ts.map +1 -1
- package/dist/account/types.d.ts +3 -0
- package/dist/account/types.d.ts.map +1 -1
- package/dist/agents/plugin-loader.d.ts.map +1 -1
- package/dist/agents/plugin-sandbox-bootstrap.js +17 -17
- package/dist/auth/client.d.ts +1 -1
- package/dist/auth/client.d.ts.map +1 -1
- package/dist/auth/cline.d.ts +1 -1
- package/dist/auth/cline.d.ts.map +1 -1
- package/dist/auth/codex.d.ts +1 -1
- package/dist/auth/codex.d.ts.map +1 -1
- package/dist/auth/oca.d.ts +1 -1
- package/dist/auth/oca.d.ts.map +1 -1
- package/dist/auth/utils.d.ts +2 -2
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/index.d.ts +50 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +949 -0
- package/dist/providers/local-provider-service.d.ts +4 -4
- package/dist/providers/local-provider-service.d.ts.map +1 -1
- package/dist/runtime/runtime-builder.d.ts +1 -0
- package/dist/runtime/runtime-builder.d.ts.map +1 -1
- package/dist/runtime/session-runtime.d.ts +2 -1
- package/dist/runtime/session-runtime.d.ts.map +1 -1
- package/dist/runtime/team-runtime-registry.d.ts +13 -0
- package/dist/runtime/team-runtime-registry.d.ts.map +1 -0
- package/dist/session/default-session-manager.d.ts +2 -2
- package/dist/session/default-session-manager.d.ts.map +1 -1
- package/dist/session/rpc-runtime-ensure.d.ts +53 -0
- package/dist/session/rpc-runtime-ensure.d.ts.map +1 -0
- package/dist/session/session-config-builder.d.ts +2 -3
- package/dist/session/session-config-builder.d.ts.map +1 -1
- package/dist/session/session-host.d.ts +8 -18
- package/dist/session/session-host.d.ts.map +1 -1
- package/dist/session/session-manager.d.ts +1 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manifest.d.ts +1 -2
- package/dist/session/session-manifest.d.ts.map +1 -1
- package/dist/session/unified-session-persistence-service.d.ts +2 -2
- package/dist/session/unified-session-persistence-service.d.ts.map +1 -1
- package/dist/session/utils/helpers.d.ts +1 -1
- package/dist/session/utils/helpers.d.ts.map +1 -1
- package/dist/session/utils/types.d.ts +1 -1
- package/dist/session/utils/types.d.ts.map +1 -1
- package/dist/storage/provider-settings-legacy-migration.d.ts.map +1 -1
- package/dist/telemetry/OpenTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/distinct-id.d.ts +2 -0
- package/dist/telemetry/distinct-id.d.ts.map +1 -0
- package/dist/telemetry/{opentelemetry.d.ts → index.d.ts} +1 -1
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +28 -0
- package/dist/tools/constants.d.ts +1 -1
- package/dist/tools/constants.d.ts.map +1 -1
- package/dist/tools/definitions.d.ts +3 -3
- package/dist/tools/definitions.d.ts.map +1 -1
- package/dist/tools/executors/apply-patch.d.ts +1 -1
- package/dist/tools/executors/apply-patch.d.ts.map +1 -1
- package/dist/tools/executors/bash.d.ts +1 -1
- package/dist/tools/executors/bash.d.ts.map +1 -1
- package/dist/tools/executors/editor.d.ts +1 -1
- package/dist/tools/executors/editor.d.ts.map +1 -1
- package/dist/tools/executors/file-read.d.ts +1 -1
- package/dist/tools/executors/file-read.d.ts.map +1 -1
- package/dist/tools/executors/index.d.ts +14 -14
- package/dist/tools/executors/index.d.ts.map +1 -1
- package/dist/tools/executors/search.d.ts +1 -1
- package/dist/tools/executors/search.d.ts.map +1 -1
- package/dist/tools/executors/web-fetch.d.ts +1 -1
- package/dist/tools/executors/web-fetch.d.ts.map +1 -1
- package/dist/tools/helpers.d.ts +1 -1
- package/dist/tools/helpers.d.ts.map +1 -1
- package/dist/tools/index.d.ts +10 -10
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/model-tool-routing.d.ts +1 -1
- package/dist/tools/model-tool-routing.d.ts.map +1 -1
- package/dist/tools/presets.d.ts +1 -1
- package/dist/tools/presets.d.ts.map +1 -1
- package/dist/types/common.d.ts +17 -8
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/config.d.ts +4 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/provider-settings.d.ts +1 -1
- package/dist/types/provider-settings.d.ts.map +1 -1
- package/dist/types.d.ts +5 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +44 -38
- package/src/ClineCore.ts +137 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +300 -0
- package/src/account/featurebase-token.test.ts +175 -0
- package/src/account/index.ts +23 -0
- package/src/account/rpc.test.ts +63 -0
- package/src/account/rpc.ts +185 -0
- package/src/account/types.ts +102 -0
- package/src/agents/agent-config-loader.test.ts +236 -0
- package/src/agents/agent-config-loader.ts +108 -0
- package/src/agents/agent-config-parser.ts +198 -0
- package/src/agents/hooks-config-loader.test.ts +20 -0
- package/src/agents/hooks-config-loader.ts +118 -0
- package/src/agents/index.ts +85 -0
- package/src/agents/plugin-config-loader.test.ts +140 -0
- package/src/agents/plugin-config-loader.ts +97 -0
- package/src/agents/plugin-loader.test.ts +210 -0
- package/src/agents/plugin-loader.ts +175 -0
- package/src/agents/plugin-sandbox-bootstrap.ts +448 -0
- package/src/agents/plugin-sandbox.test.ts +296 -0
- package/src/agents/plugin-sandbox.ts +341 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +420 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +491 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +573 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +81 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/index.ts +479 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +127 -0
- package/src/input/file-indexer.ts +327 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +85 -0
- package/src/input/mention-enricher.ts +122 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/providers/local-provider-registry.ts +232 -0
- package/src/providers/local-provider-service.test.ts +783 -0
- package/src/providers/local-provider-service.ts +471 -0
- package/src/runtime/commands.test.ts +98 -0
- package/src/runtime/commands.ts +83 -0
- package/src/runtime/hook-file-hooks.test.ts +237 -0
- package/src/runtime/hook-file-hooks.ts +859 -0
- package/src/runtime/index.ts +37 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +245 -0
- package/src/runtime/runtime-builder.test.ts +371 -0
- package/src/runtime/runtime-builder.ts +631 -0
- package/src/runtime/runtime-parity.test.ts +143 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +231 -0
- package/src/runtime/session-runtime.ts +49 -0
- package/src/runtime/skills.ts +44 -0
- package/src/runtime/team-runtime-registry.ts +46 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +45 -0
- package/src/session/default-session-manager.e2e.test.ts +384 -0
- package/src/session/default-session-manager.test.ts +1931 -0
- package/src/session/default-session-manager.ts +1422 -0
- package/src/session/file-session-service.ts +280 -0
- package/src/session/index.ts +45 -0
- package/src/session/rpc-runtime-ensure.ts +521 -0
- package/src/session/rpc-session-service.ts +107 -0
- package/src/session/rpc-spawn-lease.test.ts +49 -0
- package/src/session/rpc-spawn-lease.ts +122 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +272 -0
- package/src/session/session-agent-events.ts +248 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-config-builder.ts +113 -0
- package/src/session/session-graph.ts +92 -0
- package/src/session/session-host.test.ts +89 -0
- package/src/session/session-host.ts +205 -0
- package/src/session/session-manager.ts +69 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +673 -0
- package/src/session/session-team-coordination.ts +229 -0
- package/src/session/session-telemetry.ts +100 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.test.ts +85 -0
- package/src/session/unified-session-persistence-service.ts +994 -0
- package/src/session/utils/helpers.ts +139 -0
- package/src/session/utils/types.ts +57 -0
- package/src/session/utils/usage.ts +32 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/file-team-store.ts +257 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +424 -0
- package/src/storage/provider-settings-legacy-migration.ts +826 -0
- package/src/storage/provider-settings-manager.test.ts +191 -0
- package/src/storage/provider-settings-manager.ts +152 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +275 -0
- package/src/storage/sqlite-team-store.ts +454 -0
- package/src/storage/team-store.ts +40 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/telemetry/ITelemetryAdapter.ts +94 -0
- package/src/telemetry/LoggerTelemetryAdapter.test.ts +42 -0
- package/src/telemetry/LoggerTelemetryAdapter.ts +114 -0
- package/src/telemetry/OpenTelemetryAdapter.test.ts +157 -0
- package/src/telemetry/OpenTelemetryAdapter.ts +348 -0
- package/src/telemetry/OpenTelemetryProvider.test.ts +113 -0
- package/src/telemetry/OpenTelemetryProvider.ts +325 -0
- package/src/telemetry/TelemetryService.test.ts +134 -0
- package/src/telemetry/TelemetryService.ts +141 -0
- package/src/telemetry/core-events.ts +400 -0
- package/src/telemetry/distinct-id.test.ts +57 -0
- package/src/telemetry/distinct-id.ts +58 -0
- package/src/telemetry/index.ts +20 -0
- package/src/tools/constants.ts +35 -0
- package/src/tools/definitions.test.ts +704 -0
- package/src/tools/definitions.ts +709 -0
- package/src/tools/executors/apply-patch-parser.ts +520 -0
- package/src/tools/executors/apply-patch.ts +359 -0
- package/src/tools/executors/bash.test.ts +87 -0
- package/src/tools/executors/bash.ts +207 -0
- package/src/tools/executors/editor.test.ts +35 -0
- package/src/tools/executors/editor.ts +219 -0
- package/src/tools/executors/file-read.test.ts +49 -0
- package/src/tools/executors/file-read.ts +110 -0
- package/src/tools/executors/index.ts +87 -0
- package/src/tools/executors/search.ts +278 -0
- package/src/tools/executors/web-fetch.ts +259 -0
- package/src/tools/helpers.ts +130 -0
- package/src/tools/index.ts +169 -0
- package/src/tools/model-tool-routing.test.ts +86 -0
- package/src/tools/model-tool-routing.ts +132 -0
- package/src/tools/presets.test.ts +62 -0
- package/src/tools/presets.ts +168 -0
- package/src/tools/schemas.ts +327 -0
- package/src/tools/types.ts +329 -0
- package/src/types/common.ts +26 -0
- package/src/types/config.ts +86 -0
- package/src/types/events.ts +74 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +132 -0
- package/src/version.ts +3 -0
- package/dist/index.node.d.ts +0 -47
- package/dist/index.node.d.ts.map +0 -1
- package/dist/index.node.js +0 -948
- package/dist/telemetry/opentelemetry.d.ts.map +0 -1
- package/dist/telemetry/opentelemetry.js +0 -27
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type { AgentResult, TeamEvent } from "@clinebot/agents";
|
|
2
|
+
import { formatUserInputBlock } from "@clinebot/shared";
|
|
3
|
+
import {
|
|
4
|
+
buildTeamProgressSummary,
|
|
5
|
+
toTeamProgressLifecycleEvent,
|
|
6
|
+
} from "../team";
|
|
7
|
+
import type { CoreSessionEvent } from "../types/events";
|
|
8
|
+
import type { ActiveSession, TeamRunUpdate } from "./utils/types";
|
|
9
|
+
|
|
10
|
+
export function trackTeamRunState(
|
|
11
|
+
session: ActiveSession,
|
|
12
|
+
event: TeamEvent,
|
|
13
|
+
): void {
|
|
14
|
+
switch (event.type) {
|
|
15
|
+
case "run_queued":
|
|
16
|
+
case "run_started":
|
|
17
|
+
session.activeTeamRunIds.add(event.run.id);
|
|
18
|
+
break;
|
|
19
|
+
case "run_completed":
|
|
20
|
+
case "run_failed":
|
|
21
|
+
case "run_cancelled":
|
|
22
|
+
case "run_interrupted": {
|
|
23
|
+
let runError: string | undefined;
|
|
24
|
+
if (event.type === "run_failed") {
|
|
25
|
+
runError = event.run.error;
|
|
26
|
+
} else if (
|
|
27
|
+
event.type === "run_cancelled" ||
|
|
28
|
+
event.type === "run_interrupted"
|
|
29
|
+
) {
|
|
30
|
+
runError = event.run.error ?? event.reason;
|
|
31
|
+
}
|
|
32
|
+
session.activeTeamRunIds.delete(event.run.id);
|
|
33
|
+
session.pendingTeamRunUpdates.push({
|
|
34
|
+
runId: event.run.id,
|
|
35
|
+
agentId: event.run.agentId,
|
|
36
|
+
taskId: event.run.taskId,
|
|
37
|
+
status: event.type.replace("run_", "") as TeamRunUpdate["status"],
|
|
38
|
+
error: runError,
|
|
39
|
+
iterations: event.run.result?.iterations,
|
|
40
|
+
});
|
|
41
|
+
notifyTeamRunWaiters(session);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
default:
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function dispatchTeamEventToBackend(
|
|
50
|
+
rootSessionId: string,
|
|
51
|
+
event: TeamEvent,
|
|
52
|
+
invokeOptional: (method: string, ...args: unknown[]) => Promise<void>,
|
|
53
|
+
): Promise<void> {
|
|
54
|
+
switch (event.type) {
|
|
55
|
+
case "run_progress":
|
|
56
|
+
await invokeOptional(
|
|
57
|
+
"onTeamTaskProgress",
|
|
58
|
+
rootSessionId,
|
|
59
|
+
event.run.agentId,
|
|
60
|
+
event.message,
|
|
61
|
+
{ kind: event.message === "heartbeat" ? "heartbeat" : "progress" },
|
|
62
|
+
);
|
|
63
|
+
break;
|
|
64
|
+
case "agent_event":
|
|
65
|
+
if (
|
|
66
|
+
event.event.type === "content_start" &&
|
|
67
|
+
event.event.contentType === "text" &&
|
|
68
|
+
typeof event.event.text === "string"
|
|
69
|
+
) {
|
|
70
|
+
const snippet = event.event.text
|
|
71
|
+
.replace(/\s+/g, " ")
|
|
72
|
+
.trim()
|
|
73
|
+
.slice(0, 120);
|
|
74
|
+
if (snippet) {
|
|
75
|
+
await invokeOptional(
|
|
76
|
+
"onTeamTaskProgress",
|
|
77
|
+
rootSessionId,
|
|
78
|
+
event.agentId,
|
|
79
|
+
snippet,
|
|
80
|
+
{ kind: "text" },
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
case "task_start":
|
|
86
|
+
await invokeOptional(
|
|
87
|
+
"onTeamTaskStart",
|
|
88
|
+
rootSessionId,
|
|
89
|
+
event.agentId,
|
|
90
|
+
event.message,
|
|
91
|
+
);
|
|
92
|
+
break;
|
|
93
|
+
case "task_end": {
|
|
94
|
+
if (event.error) {
|
|
95
|
+
await invokeOptional(
|
|
96
|
+
"onTeamTaskEnd",
|
|
97
|
+
rootSessionId,
|
|
98
|
+
event.agentId,
|
|
99
|
+
"failed",
|
|
100
|
+
`[error] ${event.error.message}`,
|
|
101
|
+
event.messages,
|
|
102
|
+
);
|
|
103
|
+
} else if (event.result?.finishReason === "aborted") {
|
|
104
|
+
await invokeOptional(
|
|
105
|
+
"onTeamTaskEnd",
|
|
106
|
+
rootSessionId,
|
|
107
|
+
event.agentId,
|
|
108
|
+
"cancelled",
|
|
109
|
+
"[done] aborted",
|
|
110
|
+
event.result.messages,
|
|
111
|
+
);
|
|
112
|
+
} else {
|
|
113
|
+
await invokeOptional(
|
|
114
|
+
"onTeamTaskEnd",
|
|
115
|
+
rootSessionId,
|
|
116
|
+
event.agentId,
|
|
117
|
+
"completed",
|
|
118
|
+
`[done] ${event.result?.finishReason ?? "completed"}`,
|
|
119
|
+
event.result?.messages,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
default:
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function emitTeamProgress(
|
|
130
|
+
session: ActiveSession,
|
|
131
|
+
rootSessionId: string,
|
|
132
|
+
event: TeamEvent,
|
|
133
|
+
emit: (event: CoreSessionEvent) => void,
|
|
134
|
+
): void {
|
|
135
|
+
if (!session.runtime.teamRuntime) return;
|
|
136
|
+
const teamName = session.config.teamName?.trim() || "team";
|
|
137
|
+
emit({
|
|
138
|
+
type: "team_progress",
|
|
139
|
+
payload: {
|
|
140
|
+
sessionId: rootSessionId,
|
|
141
|
+
teamName,
|
|
142
|
+
lifecycle: toTeamProgressLifecycleEvent({
|
|
143
|
+
teamName,
|
|
144
|
+
sessionId: rootSessionId,
|
|
145
|
+
event,
|
|
146
|
+
}),
|
|
147
|
+
summary: buildTeamProgressSummary(
|
|
148
|
+
teamName,
|
|
149
|
+
session.runtime.teamRuntime.exportState(),
|
|
150
|
+
),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function hasPendingTeamRunWork(session: ActiveSession): boolean {
|
|
156
|
+
return (
|
|
157
|
+
session.activeTeamRunIds.size > 0 ||
|
|
158
|
+
session.pendingTeamRunUpdates.length > 0
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function shouldAutoContinueTeamRuns(
|
|
163
|
+
session: ActiveSession,
|
|
164
|
+
finishReason: AgentResult["finishReason"],
|
|
165
|
+
): boolean {
|
|
166
|
+
if (session.aborting) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const canAutoContinue =
|
|
170
|
+
finishReason === "completed" || finishReason === "max_iterations";
|
|
171
|
+
if (!canAutoContinue) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return (
|
|
175
|
+
session.config.enableAgentTeams === true && hasPendingTeamRunWork(session)
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function notifyTeamRunWaiters(session: ActiveSession): void {
|
|
180
|
+
const waiters = session.teamRunWaiters.splice(0);
|
|
181
|
+
for (const resolve of waiters) resolve();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function waitForTeamRunUpdates(
|
|
185
|
+
session: ActiveSession,
|
|
186
|
+
): Promise<TeamRunUpdate[]> {
|
|
187
|
+
while (true) {
|
|
188
|
+
if (session.aborting) return [];
|
|
189
|
+
if (session.pendingTeamRunUpdates.length > 0) {
|
|
190
|
+
const updates = [...session.pendingTeamRunUpdates];
|
|
191
|
+
session.pendingTeamRunUpdates.length = 0;
|
|
192
|
+
return updates;
|
|
193
|
+
}
|
|
194
|
+
if (session.activeTeamRunIds.size === 0) return [];
|
|
195
|
+
await new Promise<void>((resolve) => {
|
|
196
|
+
session.teamRunWaiters.push(resolve);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function buildTeamRunContinuationPrompt(
|
|
202
|
+
session: ActiveSession,
|
|
203
|
+
updates: TeamRunUpdate[],
|
|
204
|
+
): string {
|
|
205
|
+
const lines = updates.map((u) => {
|
|
206
|
+
const parts = [`- ${u.runId} (${u.agentId}) -> ${u.status}`];
|
|
207
|
+
if (u.taskId) parts.push(` task=${u.taskId}`);
|
|
208
|
+
if (typeof u.iterations === "number")
|
|
209
|
+
parts.push(` iterations=${u.iterations}`);
|
|
210
|
+
if (u.error) parts.push(` error=${u.error}`);
|
|
211
|
+
return parts.join("");
|
|
212
|
+
});
|
|
213
|
+
const remaining = session.activeTeamRunIds.size;
|
|
214
|
+
const instruction =
|
|
215
|
+
remaining > 0
|
|
216
|
+
? `There are still ${remaining} teammate run(s) in progress. Continue coordination and decide whether to wait for more updates.`
|
|
217
|
+
: "No teammate runs are currently in progress. Continue coordination using these updates.";
|
|
218
|
+
return formatModePrompt(
|
|
219
|
+
`System-delivered teammate async run updates:\n${lines.join("\n")}\n\n${instruction}`,
|
|
220
|
+
session.config.mode,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function formatModePrompt(
|
|
225
|
+
prompt: string,
|
|
226
|
+
mode: "act" | "plan" | undefined,
|
|
227
|
+
): string {
|
|
228
|
+
return formatUserInputBlock(prompt, mode === "plan" ? "plan" : "act");
|
|
229
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { ITelemetryService } from "@clinebot/shared";
|
|
2
|
+
import {
|
|
3
|
+
listHookConfigFiles,
|
|
4
|
+
resolveDocumentsHooksDirectoryPath,
|
|
5
|
+
} from "../agents/hooks-config-loader";
|
|
6
|
+
import type { enrichPromptWithMentions } from "../input";
|
|
7
|
+
import {
|
|
8
|
+
captureHookDiscovery,
|
|
9
|
+
captureMentionFailed,
|
|
10
|
+
captureMentionSearchResults,
|
|
11
|
+
captureMentionUsed,
|
|
12
|
+
captureTaskCreated,
|
|
13
|
+
captureTaskRestarted,
|
|
14
|
+
type TelemetryAgentIdentityProperties,
|
|
15
|
+
} from "../telemetry/core-events";
|
|
16
|
+
import type { SessionSource } from "../types/common";
|
|
17
|
+
import type { CoreSessionConfig } from "../types/config";
|
|
18
|
+
|
|
19
|
+
export function emitSessionCreationTelemetry(
|
|
20
|
+
config: CoreSessionConfig,
|
|
21
|
+
sessionId: string,
|
|
22
|
+
source: SessionSource,
|
|
23
|
+
isRestart: boolean,
|
|
24
|
+
workspacePath: string,
|
|
25
|
+
agentIdentity?: Partial<TelemetryAgentIdentityProperties>,
|
|
26
|
+
): void {
|
|
27
|
+
if (isRestart) {
|
|
28
|
+
captureTaskRestarted(config.telemetry, {
|
|
29
|
+
ulid: sessionId,
|
|
30
|
+
apiProvider: config.providerId,
|
|
31
|
+
...agentIdentity,
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
captureTaskCreated(config.telemetry, {
|
|
35
|
+
ulid: sessionId,
|
|
36
|
+
apiProvider: config.providerId,
|
|
37
|
+
...agentIdentity,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
captureHookDiscoveryTelemetry(config.telemetry, { workspacePath });
|
|
41
|
+
config.telemetry?.capture({
|
|
42
|
+
event: "session.started",
|
|
43
|
+
properties: {
|
|
44
|
+
sessionId,
|
|
45
|
+
source,
|
|
46
|
+
providerId: config.providerId,
|
|
47
|
+
modelId: config.modelId,
|
|
48
|
+
enableTools: config.enableTools,
|
|
49
|
+
enableSpawnAgent: config.enableSpawnAgent,
|
|
50
|
+
enableAgentTeams: config.enableAgentTeams,
|
|
51
|
+
...agentIdentity,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function captureHookDiscoveryTelemetry(
|
|
57
|
+
telemetry: ITelemetryService | undefined,
|
|
58
|
+
options: { workspacePath: string },
|
|
59
|
+
): void {
|
|
60
|
+
const globalHooksDir = resolveDocumentsHooksDirectoryPath();
|
|
61
|
+
const entries = listHookConfigFiles(options.workspacePath);
|
|
62
|
+
const counts = new Map<string, { global: number; workspace: number }>();
|
|
63
|
+
for (const entry of entries) {
|
|
64
|
+
const hookName = entry.hookEventName ?? "unknown";
|
|
65
|
+
const current = counts.get(hookName) ?? { global: 0, workspace: 0 };
|
|
66
|
+
if (
|
|
67
|
+
entry.path === globalHooksDir ||
|
|
68
|
+
entry.path.startsWith(`${globalHooksDir}/`)
|
|
69
|
+
) {
|
|
70
|
+
current.global += 1;
|
|
71
|
+
} else {
|
|
72
|
+
current.workspace += 1;
|
|
73
|
+
}
|
|
74
|
+
counts.set(hookName, current);
|
|
75
|
+
}
|
|
76
|
+
for (const [hookName, count] of counts.entries()) {
|
|
77
|
+
captureHookDiscovery(telemetry, hookName, count.global, count.workspace);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function emitMentionTelemetry(
|
|
82
|
+
telemetry: ITelemetryService | undefined,
|
|
83
|
+
enriched: Awaited<ReturnType<typeof enrichPromptWithMentions>>,
|
|
84
|
+
): void {
|
|
85
|
+
for (const mention of enriched.mentions) {
|
|
86
|
+
captureMentionSearchResults(
|
|
87
|
+
telemetry,
|
|
88
|
+
mention,
|
|
89
|
+
enriched.matchedFiles.includes(mention) ? 1 : 0,
|
|
90
|
+
"file",
|
|
91
|
+
!enriched.matchedFiles.includes(mention),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
for (const matched of enriched.matchedFiles) {
|
|
95
|
+
captureMentionUsed(telemetry, "file", matched.length);
|
|
96
|
+
}
|
|
97
|
+
for (const ignored of enriched.ignoredMentions) {
|
|
98
|
+
captureMentionFailed(telemetry, "file", "not_found", ignored);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RpcSessionBackend,
|
|
3
|
+
RpcSessionRow,
|
|
4
|
+
RpcSessionStatus,
|
|
5
|
+
RpcSessionUpdateInput,
|
|
6
|
+
RpcSpawnQueueItem,
|
|
7
|
+
} from "@clinebot/rpc";
|
|
8
|
+
import {
|
|
9
|
+
asBool,
|
|
10
|
+
asOptionalString,
|
|
11
|
+
asString,
|
|
12
|
+
nowIso,
|
|
13
|
+
toBoolInt,
|
|
14
|
+
} from "@clinebot/shared/db";
|
|
15
|
+
import { SqliteSessionStore } from "../storage/sqlite-session-store";
|
|
16
|
+
|
|
17
|
+
export interface SqliteRpcSessionBackendOptions {
|
|
18
|
+
sessionsDir?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class SqliteRpcSessionBackend implements RpcSessionBackend {
|
|
22
|
+
private readonly store: SqliteSessionStore;
|
|
23
|
+
|
|
24
|
+
constructor(options: SqliteRpcSessionBackendOptions = {}) {
|
|
25
|
+
this.store = new SqliteSessionStore({ sessionsDir: options.sessionsDir });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public init(): void {
|
|
29
|
+
this.store.init();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public upsertSession(row: RpcSessionRow): void {
|
|
33
|
+
this.store.run(
|
|
34
|
+
`INSERT OR REPLACE INTO sessions (
|
|
35
|
+
session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
|
|
36
|
+
provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
|
|
37
|
+
parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
|
|
38
|
+
metadata_json, transcript_path, hook_path, messages_path, updated_at
|
|
39
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
40
|
+
[
|
|
41
|
+
row.sessionId,
|
|
42
|
+
row.source,
|
|
43
|
+
row.pid,
|
|
44
|
+
row.startedAt,
|
|
45
|
+
row.endedAt ?? null,
|
|
46
|
+
row.exitCode ?? null,
|
|
47
|
+
row.status,
|
|
48
|
+
row.statusLock,
|
|
49
|
+
toBoolInt(row.interactive),
|
|
50
|
+
row.provider,
|
|
51
|
+
row.model,
|
|
52
|
+
row.cwd,
|
|
53
|
+
row.workspaceRoot,
|
|
54
|
+
row.teamName ?? null,
|
|
55
|
+
toBoolInt(row.enableTools),
|
|
56
|
+
toBoolInt(row.enableSpawn),
|
|
57
|
+
toBoolInt(row.enableTeams),
|
|
58
|
+
row.parentSessionId ?? null,
|
|
59
|
+
row.parentAgentId ?? null,
|
|
60
|
+
row.agentId ?? null,
|
|
61
|
+
row.conversationId ?? null,
|
|
62
|
+
toBoolInt(row.isSubagent),
|
|
63
|
+
row.prompt ?? null,
|
|
64
|
+
row.metadata ? JSON.stringify(row.metadata) : null,
|
|
65
|
+
row.transcriptPath,
|
|
66
|
+
row.hookPath,
|
|
67
|
+
row.messagesPath ?? null,
|
|
68
|
+
row.updatedAt || nowIso(),
|
|
69
|
+
],
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public getSession(sessionId: string): RpcSessionRow | undefined {
|
|
74
|
+
const row = this.store.queryOne<Record<string, unknown>>(
|
|
75
|
+
`SELECT session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
|
|
76
|
+
provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
|
|
77
|
+
parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
|
|
78
|
+
metadata_json, transcript_path, hook_path, messages_path, updated_at
|
|
79
|
+
FROM sessions WHERE session_id = ?`,
|
|
80
|
+
[sessionId],
|
|
81
|
+
);
|
|
82
|
+
if (!row) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
sessionId: asString(row.session_id),
|
|
87
|
+
source: asString(row.source),
|
|
88
|
+
pid: Number(row.pid ?? 0),
|
|
89
|
+
startedAt: asString(row.started_at),
|
|
90
|
+
endedAt: (row.ended_at as string | null | undefined) ?? null,
|
|
91
|
+
exitCode: (row.exit_code as number | null | undefined) ?? null,
|
|
92
|
+
status: asString(row.status) as RpcSessionStatus,
|
|
93
|
+
statusLock: Number(row.status_lock ?? 0),
|
|
94
|
+
interactive: asBool(row.interactive),
|
|
95
|
+
provider: asString(row.provider),
|
|
96
|
+
model: asString(row.model),
|
|
97
|
+
cwd: asString(row.cwd),
|
|
98
|
+
workspaceRoot: asString(row.workspace_root),
|
|
99
|
+
teamName: asOptionalString(row.team_name),
|
|
100
|
+
enableTools: asBool(row.enable_tools),
|
|
101
|
+
enableSpawn: asBool(row.enable_spawn),
|
|
102
|
+
enableTeams: asBool(row.enable_teams),
|
|
103
|
+
parentSessionId: asOptionalString(row.parent_session_id),
|
|
104
|
+
parentAgentId: asOptionalString(row.parent_agent_id),
|
|
105
|
+
agentId: asOptionalString(row.agent_id),
|
|
106
|
+
conversationId: asOptionalString(row.conversation_id),
|
|
107
|
+
isSubagent: asBool(row.is_subagent),
|
|
108
|
+
prompt: asOptionalString(row.prompt),
|
|
109
|
+
metadata: (() => {
|
|
110
|
+
const raw = asOptionalString(row.metadata_json);
|
|
111
|
+
if (!raw) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
116
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
117
|
+
return parsed as Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
} catch {
|
|
120
|
+
// Ignore malformed metadata payloads.
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
})(),
|
|
124
|
+
transcriptPath: asString(row.transcript_path),
|
|
125
|
+
hookPath: asString(row.hook_path),
|
|
126
|
+
messagesPath: asOptionalString(row.messages_path),
|
|
127
|
+
updatedAt: asString(row.updated_at) || nowIso(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public listSessions(options: {
|
|
132
|
+
limit: number;
|
|
133
|
+
parentSessionId?: string;
|
|
134
|
+
status?: string;
|
|
135
|
+
}): RpcSessionRow[] {
|
|
136
|
+
const where: string[] = [];
|
|
137
|
+
const params: unknown[] = [];
|
|
138
|
+
if (options.parentSessionId) {
|
|
139
|
+
where.push("parent_session_id = ?");
|
|
140
|
+
params.push(options.parentSessionId);
|
|
141
|
+
}
|
|
142
|
+
if (options.status) {
|
|
143
|
+
where.push("status = ?");
|
|
144
|
+
params.push(options.status);
|
|
145
|
+
}
|
|
146
|
+
const whereClause = where.length > 0 ? `WHERE ${where.join(" AND ")}` : "";
|
|
147
|
+
const limit = Math.max(1, Math.floor(options.limit));
|
|
148
|
+
const rows = this.store.queryAll<{ session_id?: string }>(
|
|
149
|
+
`SELECT session_id FROM sessions ${whereClause} ORDER BY started_at DESC LIMIT ?`,
|
|
150
|
+
[...params, limit],
|
|
151
|
+
);
|
|
152
|
+
const out: RpcSessionRow[] = [];
|
|
153
|
+
for (const row of rows) {
|
|
154
|
+
if (!row.session_id) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const item = this.getSession(row.session_id);
|
|
158
|
+
if (item) {
|
|
159
|
+
out.push(item);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public updateSession(input: RpcSessionUpdateInput): {
|
|
166
|
+
updated: boolean;
|
|
167
|
+
statusLock: number;
|
|
168
|
+
} {
|
|
169
|
+
const existing = this.getSession(input.sessionId);
|
|
170
|
+
if (!existing) {
|
|
171
|
+
return { updated: false, statusLock: 0 };
|
|
172
|
+
}
|
|
173
|
+
if (
|
|
174
|
+
typeof input.expectedStatusLock === "number" &&
|
|
175
|
+
existing.statusLock !== input.expectedStatusLock
|
|
176
|
+
) {
|
|
177
|
+
return { updated: false, statusLock: existing.statusLock };
|
|
178
|
+
}
|
|
179
|
+
const nextLock = existing.statusLock + 1;
|
|
180
|
+
const nextStatus = input.setRunning
|
|
181
|
+
? "running"
|
|
182
|
+
: (input.status ?? existing.status);
|
|
183
|
+
const nextEndedAt =
|
|
184
|
+
input.setRunning === true
|
|
185
|
+
? null
|
|
186
|
+
: input.endedAt !== undefined
|
|
187
|
+
? input.endedAt
|
|
188
|
+
: (existing.endedAt ?? null);
|
|
189
|
+
const nextExitCode =
|
|
190
|
+
input.setRunning === true
|
|
191
|
+
? null
|
|
192
|
+
: input.exitCode !== undefined
|
|
193
|
+
? input.exitCode
|
|
194
|
+
: (existing.exitCode ?? null);
|
|
195
|
+
const nextPrompt =
|
|
196
|
+
input.prompt !== undefined
|
|
197
|
+
? (input.prompt ?? undefined)
|
|
198
|
+
: existing.prompt;
|
|
199
|
+
const nextMetadata =
|
|
200
|
+
input.metadata !== undefined
|
|
201
|
+
? (input.metadata ?? undefined)
|
|
202
|
+
: existing.metadata;
|
|
203
|
+
|
|
204
|
+
this.store.run(
|
|
205
|
+
`UPDATE sessions
|
|
206
|
+
SET status = ?, ended_at = ?, exit_code = ?, prompt = ?, metadata_json = ?,
|
|
207
|
+
parent_session_id = ?, parent_agent_id = ?, agent_id = ?, conversation_id = ?,
|
|
208
|
+
status_lock = ?, updated_at = ?
|
|
209
|
+
WHERE session_id = ?`,
|
|
210
|
+
[
|
|
211
|
+
nextStatus,
|
|
212
|
+
nextEndedAt,
|
|
213
|
+
nextExitCode,
|
|
214
|
+
nextPrompt ?? null,
|
|
215
|
+
nextMetadata ? JSON.stringify(nextMetadata) : null,
|
|
216
|
+
input.parentSessionId !== undefined
|
|
217
|
+
? (input.parentSessionId ?? null)
|
|
218
|
+
: (existing.parentSessionId ?? null),
|
|
219
|
+
input.parentAgentId !== undefined
|
|
220
|
+
? (input.parentAgentId ?? null)
|
|
221
|
+
: (existing.parentAgentId ?? null),
|
|
222
|
+
input.agentId !== undefined
|
|
223
|
+
? (input.agentId ?? null)
|
|
224
|
+
: (existing.agentId ?? null),
|
|
225
|
+
input.conversationId !== undefined
|
|
226
|
+
? (input.conversationId ?? null)
|
|
227
|
+
: (existing.conversationId ?? null),
|
|
228
|
+
nextLock,
|
|
229
|
+
nowIso(),
|
|
230
|
+
input.sessionId,
|
|
231
|
+
],
|
|
232
|
+
);
|
|
233
|
+
return { updated: true, statusLock: nextLock };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
public deleteSession(sessionId: string): boolean {
|
|
237
|
+
const changes =
|
|
238
|
+
this.store.run("DELETE FROM sessions WHERE session_id = ?", [sessionId])
|
|
239
|
+
.changes ?? 0;
|
|
240
|
+
return changes > 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public deleteSessionsByParent(parentSessionId: string): void {
|
|
244
|
+
this.store.run("DELETE FROM sessions WHERE parent_session_id = ?", [
|
|
245
|
+
parentSessionId,
|
|
246
|
+
]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public enqueueSpawnRequest(input: {
|
|
250
|
+
rootSessionId: string;
|
|
251
|
+
parentAgentId: string;
|
|
252
|
+
task?: string;
|
|
253
|
+
systemPrompt?: string;
|
|
254
|
+
}): void {
|
|
255
|
+
this.store.run(
|
|
256
|
+
`INSERT INTO subagent_spawn_queue (root_session_id, parent_agent_id, task, system_prompt, created_at, consumed_at)
|
|
257
|
+
VALUES (?, ?, ?, ?, ?, NULL)`,
|
|
258
|
+
[
|
|
259
|
+
input.rootSessionId,
|
|
260
|
+
input.parentAgentId,
|
|
261
|
+
input.task ?? null,
|
|
262
|
+
input.systemPrompt ?? null,
|
|
263
|
+
nowIso(),
|
|
264
|
+
],
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public claimSpawnRequest(
|
|
269
|
+
rootSessionId: string,
|
|
270
|
+
parentAgentId: string,
|
|
271
|
+
): RpcSpawnQueueItem | undefined {
|
|
272
|
+
const row = this.store.queryOne<Record<string, unknown>>(
|
|
273
|
+
`SELECT id, root_session_id, parent_agent_id, task, system_prompt, created_at, consumed_at
|
|
274
|
+
FROM subagent_spawn_queue
|
|
275
|
+
WHERE root_session_id = ? AND parent_agent_id = ? AND consumed_at IS NULL
|
|
276
|
+
ORDER BY id ASC LIMIT 1`,
|
|
277
|
+
[rootSessionId, parentAgentId],
|
|
278
|
+
);
|
|
279
|
+
if (!row || typeof row.id !== "number") {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
const consumedAt = nowIso();
|
|
283
|
+
this.store.run(
|
|
284
|
+
"UPDATE subagent_spawn_queue SET consumed_at = ? WHERE id = ?",
|
|
285
|
+
[consumedAt, row.id],
|
|
286
|
+
);
|
|
287
|
+
return {
|
|
288
|
+
id: row.id,
|
|
289
|
+
rootSessionId: asString(row.root_session_id),
|
|
290
|
+
parentAgentId: asString(row.parent_agent_id),
|
|
291
|
+
task: asOptionalString(row.task),
|
|
292
|
+
systemPrompt: asOptionalString(row.system_prompt),
|
|
293
|
+
createdAt: asString(row.created_at),
|
|
294
|
+
consumedAt,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function createSqliteRpcSessionBackend(
|
|
300
|
+
options: SqliteRpcSessionBackendOptions = {},
|
|
301
|
+
): RpcSessionBackend {
|
|
302
|
+
return new SqliteRpcSessionBackend(options);
|
|
303
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
import { SqliteSessionStore } from "../storage/sqlite-session-store";
|
|
6
|
+
import { SessionSource } from "../types/common";
|
|
7
|
+
import { CoreSessionService } from "./session-service";
|
|
8
|
+
|
|
9
|
+
describe("UnifiedSessionPersistenceService", () => {
|
|
10
|
+
const tempDirs: string[] = [];
|
|
11
|
+
const stores: Array<SqliteSessionStore> = [];
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
for (const store of stores.splice(0)) {
|
|
15
|
+
store.close();
|
|
16
|
+
}
|
|
17
|
+
for (const dir of tempDirs.splice(0)) {
|
|
18
|
+
rmSync(dir, { recursive: true, force: true });
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("reconciles dead running sessions into failed manifests with terminal markers", async () => {
|
|
23
|
+
const sessionsDir = mkdtempSync(join(tmpdir(), "stale-session-reconcile-"));
|
|
24
|
+
tempDirs.push(sessionsDir);
|
|
25
|
+
|
|
26
|
+
const store = new SqliteSessionStore({ sessionsDir });
|
|
27
|
+
stores.push(store);
|
|
28
|
+
const service = new CoreSessionService(store);
|
|
29
|
+
const sessionId = "stale-root-session";
|
|
30
|
+
const artifacts = await service.createRootSessionWithArtifacts({
|
|
31
|
+
sessionId,
|
|
32
|
+
source: SessionSource.CLI,
|
|
33
|
+
pid: 999_999_999,
|
|
34
|
+
interactive: false,
|
|
35
|
+
provider: "mock-provider",
|
|
36
|
+
model: "mock-model",
|
|
37
|
+
cwd: "/tmp/project",
|
|
38
|
+
workspaceRoot: "/tmp/project",
|
|
39
|
+
enableTools: true,
|
|
40
|
+
enableSpawn: true,
|
|
41
|
+
enableTeams: false,
|
|
42
|
+
prompt: "hello",
|
|
43
|
+
startedAt: "2026-01-01T00:00:00.000Z",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const reconciled = await service.reconcileDeadSessions();
|
|
47
|
+
expect(reconciled).toBe(1);
|
|
48
|
+
|
|
49
|
+
const rows = await service.listSessions(10);
|
|
50
|
+
expect(rows).toHaveLength(1);
|
|
51
|
+
expect(rows[0]).toMatchObject({
|
|
52
|
+
sessionId,
|
|
53
|
+
status: "failed",
|
|
54
|
+
exitCode: 1,
|
|
55
|
+
});
|
|
56
|
+
expect(rows[0]?.endedAt).toBeTruthy();
|
|
57
|
+
|
|
58
|
+
const manifest = JSON.parse(
|
|
59
|
+
readFileSync(artifacts.manifestPath, "utf8"),
|
|
60
|
+
) as Record<string, unknown>;
|
|
61
|
+
expect(manifest.status).toBe("failed");
|
|
62
|
+
expect(manifest.exit_code).toBe(1);
|
|
63
|
+
expect(manifest.ended_at).toBeTruthy();
|
|
64
|
+
expect(manifest.metadata).toMatchObject({
|
|
65
|
+
terminal_marker: "failed_external_process_exit",
|
|
66
|
+
terminal_marker_pid: 999_999_999,
|
|
67
|
+
terminal_marker_source: "stale_session_reconciler",
|
|
68
|
+
});
|
|
69
|
+
expect(
|
|
70
|
+
(manifest.metadata as Record<string, unknown>).terminal_marker_at,
|
|
71
|
+
).toBeTruthy();
|
|
72
|
+
|
|
73
|
+
expect(existsSync(artifacts.hookPath)).toBe(true);
|
|
74
|
+
expect(existsSync(artifacts.transcriptPath)).toBe(true);
|
|
75
|
+
expect(readFileSync(artifacts.hookPath, "utf8")).toContain(
|
|
76
|
+
'"hookName":"session_shutdown"',
|
|
77
|
+
);
|
|
78
|
+
expect(readFileSync(artifacts.hookPath, "utf8")).toContain(
|
|
79
|
+
'"reason":"failed_external_process_exit"',
|
|
80
|
+
);
|
|
81
|
+
expect(readFileSync(artifacts.transcriptPath, "utf8")).toContain(
|
|
82
|
+
"[shutdown] failed_external_process_exit",
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
});
|