@clinebot/core 0.0.0

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 (200) hide show
  1. package/README.md +88 -0
  2. package/dist/account/cline-account-service.d.ts +34 -0
  3. package/dist/account/index.d.ts +3 -0
  4. package/dist/account/rpc.d.ts +38 -0
  5. package/dist/account/types.d.ts +74 -0
  6. package/dist/agents/agent-config-loader.d.ts +18 -0
  7. package/dist/agents/agent-config-parser.d.ts +25 -0
  8. package/dist/agents/hooks-config-loader.d.ts +23 -0
  9. package/dist/agents/index.d.ts +11 -0
  10. package/dist/agents/plugin-config-loader.d.ts +22 -0
  11. package/dist/agents/plugin-loader.d.ts +9 -0
  12. package/dist/agents/plugin-sandbox.d.ts +12 -0
  13. package/dist/agents/unified-config-file-watcher.d.ts +77 -0
  14. package/dist/agents/user-instruction-config-loader.d.ts +63 -0
  15. package/dist/auth/client.d.ts +11 -0
  16. package/dist/auth/cline.d.ts +41 -0
  17. package/dist/auth/codex.d.ts +39 -0
  18. package/dist/auth/oca.d.ts +22 -0
  19. package/dist/auth/server.d.ts +22 -0
  20. package/dist/auth/types.d.ts +72 -0
  21. package/dist/auth/utils.d.ts +32 -0
  22. package/dist/chat/chat-schema.d.ts +145 -0
  23. package/dist/default-tools/constants.d.ts +23 -0
  24. package/dist/default-tools/definitions.d.ts +96 -0
  25. package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
  26. package/dist/default-tools/executors/apply-patch.d.ts +26 -0
  27. package/dist/default-tools/executors/bash.d.ts +49 -0
  28. package/dist/default-tools/executors/editor.d.ts +31 -0
  29. package/dist/default-tools/executors/file-read.d.ts +40 -0
  30. package/dist/default-tools/executors/index.d.ts +44 -0
  31. package/dist/default-tools/executors/search.d.ts +50 -0
  32. package/dist/default-tools/executors/web-fetch.d.ts +58 -0
  33. package/dist/default-tools/index.d.ts +57 -0
  34. package/dist/default-tools/presets.d.ts +124 -0
  35. package/dist/default-tools/schemas.d.ts +121 -0
  36. package/dist/default-tools/types.d.ts +237 -0
  37. package/dist/index.d.ts +23 -0
  38. package/dist/index.js +220 -0
  39. package/dist/input/file-indexer.d.ts +5 -0
  40. package/dist/input/index.d.ts +4 -0
  41. package/dist/input/mention-enricher.d.ts +12 -0
  42. package/dist/mcp/config-loader.d.ts +15 -0
  43. package/dist/mcp/index.d.ts +4 -0
  44. package/dist/mcp/manager.d.ts +24 -0
  45. package/dist/mcp/types.d.ts +66 -0
  46. package/dist/runtime/hook-file-hooks.d.ts +18 -0
  47. package/dist/runtime/rules.d.ts +5 -0
  48. package/dist/runtime/runtime-builder.d.ts +5 -0
  49. package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
  50. package/dist/runtime/session-runtime.d.ts +36 -0
  51. package/dist/runtime/tool-approval.d.ts +9 -0
  52. package/dist/runtime/workflows.d.ts +13 -0
  53. package/dist/server/index.d.ts +47 -0
  54. package/dist/server/index.js +641 -0
  55. package/dist/session/default-session-manager.d.ts +77 -0
  56. package/dist/session/rpc-session-service.d.ts +12 -0
  57. package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
  58. package/dist/session/session-artifacts.d.ts +19 -0
  59. package/dist/session/session-graph.d.ts +15 -0
  60. package/dist/session/session-host.d.ts +21 -0
  61. package/dist/session/session-manager.d.ts +50 -0
  62. package/dist/session/session-manifest.d.ts +30 -0
  63. package/dist/session/session-service.d.ts +113 -0
  64. package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
  65. package/dist/session/unified-session-persistence-service.d.ts +93 -0
  66. package/dist/session/workspace-manager.d.ts +28 -0
  67. package/dist/session/workspace-manifest.d.ts +25 -0
  68. package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
  69. package/dist/storage/provider-settings-manager.d.ts +20 -0
  70. package/dist/storage/sqlite-session-store.d.ts +29 -0
  71. package/dist/storage/sqlite-team-store.d.ts +31 -0
  72. package/dist/storage/team-store.d.ts +2 -0
  73. package/dist/team/index.d.ts +1 -0
  74. package/dist/team/projections.d.ts +8 -0
  75. package/dist/types/common.d.ts +10 -0
  76. package/dist/types/config.d.ts +37 -0
  77. package/dist/types/events.d.ts +54 -0
  78. package/dist/types/provider-settings.d.ts +20 -0
  79. package/dist/types/sessions.d.ts +9 -0
  80. package/dist/types/storage.d.ts +37 -0
  81. package/dist/types/workspace.d.ts +7 -0
  82. package/dist/types.d.ts +26 -0
  83. package/package.json +63 -0
  84. package/src/account/cline-account-service.test.ts +101 -0
  85. package/src/account/cline-account-service.ts +267 -0
  86. package/src/account/index.ts +20 -0
  87. package/src/account/rpc.test.ts +62 -0
  88. package/src/account/rpc.ts +172 -0
  89. package/src/account/types.ts +80 -0
  90. package/src/agents/agent-config-loader.test.ts +234 -0
  91. package/src/agents/agent-config-loader.ts +107 -0
  92. package/src/agents/agent-config-parser.ts +191 -0
  93. package/src/agents/hooks-config-loader.ts +97 -0
  94. package/src/agents/index.ts +84 -0
  95. package/src/agents/plugin-config-loader.test.ts +91 -0
  96. package/src/agents/plugin-config-loader.ts +160 -0
  97. package/src/agents/plugin-loader.test.ts +102 -0
  98. package/src/agents/plugin-loader.ts +105 -0
  99. package/src/agents/plugin-sandbox.test.ts +120 -0
  100. package/src/agents/plugin-sandbox.ts +471 -0
  101. package/src/agents/unified-config-file-watcher.test.ts +196 -0
  102. package/src/agents/unified-config-file-watcher.ts +483 -0
  103. package/src/agents/user-instruction-config-loader.test.ts +158 -0
  104. package/src/agents/user-instruction-config-loader.ts +438 -0
  105. package/src/auth/client.test.ts +40 -0
  106. package/src/auth/client.ts +25 -0
  107. package/src/auth/cline.test.ts +130 -0
  108. package/src/auth/cline.ts +414 -0
  109. package/src/auth/codex.test.ts +170 -0
  110. package/src/auth/codex.ts +466 -0
  111. package/src/auth/oca.test.ts +215 -0
  112. package/src/auth/oca.ts +546 -0
  113. package/src/auth/server.ts +216 -0
  114. package/src/auth/types.ts +78 -0
  115. package/src/auth/utils.test.ts +128 -0
  116. package/src/auth/utils.ts +247 -0
  117. package/src/chat/chat-schema.ts +82 -0
  118. package/src/default-tools/constants.ts +35 -0
  119. package/src/default-tools/definitions.test.ts +233 -0
  120. package/src/default-tools/definitions.ts +632 -0
  121. package/src/default-tools/executors/apply-patch-parser.ts +520 -0
  122. package/src/default-tools/executors/apply-patch.ts +359 -0
  123. package/src/default-tools/executors/bash.ts +205 -0
  124. package/src/default-tools/executors/editor.ts +231 -0
  125. package/src/default-tools/executors/file-read.test.ts +25 -0
  126. package/src/default-tools/executors/file-read.ts +94 -0
  127. package/src/default-tools/executors/index.ts +75 -0
  128. package/src/default-tools/executors/search.ts +278 -0
  129. package/src/default-tools/executors/web-fetch.ts +259 -0
  130. package/src/default-tools/index.ts +161 -0
  131. package/src/default-tools/presets.test.ts +63 -0
  132. package/src/default-tools/presets.ts +168 -0
  133. package/src/default-tools/schemas.ts +228 -0
  134. package/src/default-tools/types.ts +324 -0
  135. package/src/index.ts +119 -0
  136. package/src/input/file-indexer.d.ts +11 -0
  137. package/src/input/file-indexer.test.ts +87 -0
  138. package/src/input/file-indexer.ts +280 -0
  139. package/src/input/index.ts +7 -0
  140. package/src/input/mention-enricher.test.ts +82 -0
  141. package/src/input/mention-enricher.ts +119 -0
  142. package/src/mcp/config-loader.test.ts +238 -0
  143. package/src/mcp/config-loader.ts +219 -0
  144. package/src/mcp/index.ts +26 -0
  145. package/src/mcp/manager.test.ts +106 -0
  146. package/src/mcp/manager.ts +262 -0
  147. package/src/mcp/types.ts +88 -0
  148. package/src/runtime/hook-file-hooks.test.ts +106 -0
  149. package/src/runtime/hook-file-hooks.ts +736 -0
  150. package/src/runtime/index.ts +27 -0
  151. package/src/runtime/rules.ts +34 -0
  152. package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
  153. package/src/runtime/runtime-builder.test.ts +215 -0
  154. package/src/runtime/runtime-builder.ts +515 -0
  155. package/src/runtime/runtime-parity.test.ts +132 -0
  156. package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
  157. package/src/runtime/session-runtime.ts +44 -0
  158. package/src/runtime/tool-approval.ts +104 -0
  159. package/src/runtime/workflows.test.ts +119 -0
  160. package/src/runtime/workflows.ts +54 -0
  161. package/src/server/index.ts +282 -0
  162. package/src/session/default-session-manager.e2e.test.ts +354 -0
  163. package/src/session/default-session-manager.test.ts +816 -0
  164. package/src/session/default-session-manager.ts +1286 -0
  165. package/src/session/index.ts +37 -0
  166. package/src/session/rpc-session-service.ts +189 -0
  167. package/src/session/runtime-oauth-token-manager.test.ts +137 -0
  168. package/src/session/runtime-oauth-token-manager.ts +265 -0
  169. package/src/session/session-artifacts.ts +106 -0
  170. package/src/session/session-graph.ts +90 -0
  171. package/src/session/session-host.ts +190 -0
  172. package/src/session/session-manager.ts +56 -0
  173. package/src/session/session-manifest.ts +29 -0
  174. package/src/session/session-service.team-persistence.test.ts +48 -0
  175. package/src/session/session-service.ts +610 -0
  176. package/src/session/sqlite-rpc-session-backend.ts +303 -0
  177. package/src/session/unified-session-persistence-service.ts +781 -0
  178. package/src/session/workspace-manager.ts +98 -0
  179. package/src/session/workspace-manifest.ts +100 -0
  180. package/src/storage/artifact-store.ts +1 -0
  181. package/src/storage/index.ts +11 -0
  182. package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
  183. package/src/storage/provider-settings-legacy-migration.ts +637 -0
  184. package/src/storage/provider-settings-manager.test.ts +111 -0
  185. package/src/storage/provider-settings-manager.ts +129 -0
  186. package/src/storage/session-store.ts +1 -0
  187. package/src/storage/sqlite-session-store.ts +270 -0
  188. package/src/storage/sqlite-team-store.ts +443 -0
  189. package/src/storage/team-store.ts +5 -0
  190. package/src/team/index.ts +4 -0
  191. package/src/team/projections.ts +285 -0
  192. package/src/types/common.ts +14 -0
  193. package/src/types/config.ts +64 -0
  194. package/src/types/events.ts +46 -0
  195. package/src/types/index.ts +24 -0
  196. package/src/types/provider-settings.ts +43 -0
  197. package/src/types/sessions.ts +16 -0
  198. package/src/types/storage.ts +64 -0
  199. package/src/types/workspace.ts +7 -0
  200. package/src/types.ts +127 -0
@@ -0,0 +1,610 @@
1
+ import {
2
+ appendFileSync,
3
+ existsSync,
4
+ mkdirSync,
5
+ readFileSync,
6
+ renameSync,
7
+ unlinkSync,
8
+ writeFileSync,
9
+ } from "node:fs";
10
+ import { join } from "node:path";
11
+ import type {
12
+ AgentTeamsRuntime,
13
+ TeamEvent,
14
+ TeamTeammateSpec,
15
+ } from "@clinebot/agents";
16
+ import { resolveTeamDataDir } from "@clinebot/shared/storage";
17
+ import type { SqliteSessionStore } from "../storage/sqlite-session-store";
18
+ import type { SessionSource, SessionStatus } from "../types/common";
19
+ import { nowIso } from "./session-artifacts";
20
+ import type { SessionManifest } from "./session-manifest";
21
+ import type {
22
+ PersistedSessionUpdateInput,
23
+ SessionPersistenceAdapter,
24
+ } from "./unified-session-persistence-service";
25
+ import { UnifiedSessionPersistenceService } from "./unified-session-persistence-service";
26
+
27
+ export interface SessionRowShape {
28
+ session_id: string;
29
+ source: string;
30
+ pid: number;
31
+ started_at: string;
32
+ ended_at?: string | null;
33
+ exit_code?: number | null;
34
+ status: SessionStatus;
35
+ status_lock?: number;
36
+ interactive: number;
37
+ provider: string;
38
+ model: string;
39
+ cwd: string;
40
+ workspace_root: string;
41
+ team_name?: string | null;
42
+ enable_tools: number;
43
+ enable_spawn: number;
44
+ enable_teams: number;
45
+ parent_session_id?: string | null;
46
+ parent_agent_id?: string | null;
47
+ agent_id?: string | null;
48
+ conversation_id?: string | null;
49
+ is_subagent: number;
50
+ prompt?: string | null;
51
+ metadata_json?: string | null;
52
+ transcript_path: string;
53
+ hook_path: string;
54
+ messages_path?: string | null;
55
+ updated_at?: string;
56
+ }
57
+
58
+ export interface CreateRootSessionInput {
59
+ sessionId: string;
60
+ source: SessionSource;
61
+ pid: number;
62
+ startedAt: string;
63
+ interactive: boolean;
64
+ provider: string;
65
+ model: string;
66
+ cwd: string;
67
+ workspaceRoot: string;
68
+ teamName?: string;
69
+ enableTools: boolean;
70
+ enableSpawn: boolean;
71
+ enableTeams: boolean;
72
+ prompt?: string;
73
+ metadata?: Record<string, unknown>;
74
+ transcriptPath: string;
75
+ hookPath: string;
76
+ messagesPath: string;
77
+ }
78
+
79
+ export interface CreateRootSessionWithArtifactsInput {
80
+ sessionId: string;
81
+ source: SessionSource;
82
+ pid: number;
83
+ interactive: boolean;
84
+ provider: string;
85
+ model: string;
86
+ cwd: string;
87
+ workspaceRoot: string;
88
+ teamName?: string;
89
+ enableTools: boolean;
90
+ enableSpawn: boolean;
91
+ enableTeams: boolean;
92
+ prompt?: string;
93
+ metadata?: Record<string, unknown>;
94
+ startedAt?: string;
95
+ }
96
+
97
+ export interface RootSessionArtifacts {
98
+ manifestPath: string;
99
+ transcriptPath: string;
100
+ hookPath: string;
101
+ messagesPath: string;
102
+ manifest: SessionManifest;
103
+ }
104
+
105
+ export interface UpsertSubagentInput {
106
+ agentId: string;
107
+ parentAgentId: string;
108
+ conversationId: string;
109
+ prompt?: string;
110
+ rootSessionId?: string;
111
+ }
112
+
113
+ function reviveTeamStateDates(state: TeamRuntimeState): TeamRuntimeState {
114
+ return {
115
+ ...state,
116
+ tasks: state.tasks.map((task) => ({
117
+ ...task,
118
+ createdAt: new Date(task.createdAt),
119
+ updatedAt: new Date(task.updatedAt),
120
+ })),
121
+ mailbox: state.mailbox.map((message) => ({
122
+ ...message,
123
+ sentAt: new Date(message.sentAt),
124
+ readAt: message.readAt ? new Date(message.readAt) : undefined,
125
+ })),
126
+ missionLog: state.missionLog.map((entry) => ({
127
+ ...entry,
128
+ ts: new Date(entry.ts),
129
+ })),
130
+ runs: (state.runs ?? []).map((run) => ({
131
+ ...run,
132
+ startedAt: new Date(run.startedAt),
133
+ endedAt: run.endedAt ? new Date(run.endedAt) : undefined,
134
+ nextAttemptAt: run.nextAttemptAt
135
+ ? new Date(run.nextAttemptAt)
136
+ : undefined,
137
+ heartbeatAt: run.heartbeatAt ? new Date(run.heartbeatAt) : undefined,
138
+ })),
139
+ outcomes: (state.outcomes ?? []).map((outcome) => ({
140
+ ...outcome,
141
+ createdAt: new Date(outcome.createdAt),
142
+ finalizedAt: outcome.finalizedAt
143
+ ? new Date(outcome.finalizedAt)
144
+ : undefined,
145
+ })),
146
+ outcomeFragments: (state.outcomeFragments ?? []).map((fragment) => ({
147
+ ...fragment,
148
+ createdAt: new Date(fragment.createdAt),
149
+ reviewedAt: fragment.reviewedAt
150
+ ? new Date(fragment.reviewedAt)
151
+ : undefined,
152
+ })),
153
+ };
154
+ }
155
+
156
+ function sanitizeTeamName(name: string): string {
157
+ return name
158
+ .toLowerCase()
159
+ .replace(/[^a-z0-9._-]+/g, "-")
160
+ .replace(/^-+|-+$/g, "");
161
+ }
162
+
163
+ interface PersistedTeamEnvelope {
164
+ version: 1;
165
+ updatedAt: string;
166
+ teamState: TeamRuntimeState;
167
+ teammates: TeamTeammateSpec[];
168
+ }
169
+
170
+ type TeamRuntimeState = ReturnType<AgentTeamsRuntime["exportState"]>;
171
+
172
+ export interface FileTeamPersistenceStoreOptions {
173
+ teamName: string;
174
+ baseDir?: string;
175
+ }
176
+
177
+ export class FileTeamPersistenceStore {
178
+ private readonly dirPath: string;
179
+ private readonly statePath: string;
180
+ private readonly taskHistoryPath: string;
181
+ private readonly teammateSpecs: Map<string, TeamTeammateSpec> = new Map();
182
+
183
+ constructor(options: FileTeamPersistenceStoreOptions) {
184
+ const safeTeamName = sanitizeTeamName(options.teamName);
185
+ const baseDir = options.baseDir?.trim() || resolveTeamDataDir();
186
+ this.dirPath = join(baseDir, safeTeamName);
187
+ this.statePath = join(this.dirPath, "state.json");
188
+ this.taskHistoryPath = join(this.dirPath, "task-history.jsonl");
189
+ }
190
+
191
+ loadState(): TeamRuntimeState | undefined {
192
+ if (!existsSync(this.statePath)) {
193
+ return undefined;
194
+ }
195
+ try {
196
+ const raw = readFileSync(this.statePath, "utf8");
197
+ const parsed = JSON.parse(raw) as PersistedTeamEnvelope;
198
+ if (parsed.version !== 1 || !parsed.teamState) {
199
+ return undefined;
200
+ }
201
+ for (const spec of parsed.teammates ?? []) {
202
+ this.teammateSpecs.set(spec.agentId, spec);
203
+ }
204
+ return reviveTeamStateDates(parsed.teamState);
205
+ } catch {
206
+ return undefined;
207
+ }
208
+ }
209
+
210
+ getTeammateSpecs(): TeamTeammateSpec[] {
211
+ return Array.from(this.teammateSpecs.values());
212
+ }
213
+
214
+ upsertTeammateSpec(spec: TeamTeammateSpec): void {
215
+ this.teammateSpecs.set(spec.agentId, spec);
216
+ }
217
+
218
+ removeTeammateSpec(agentId: string): void {
219
+ this.teammateSpecs.delete(agentId);
220
+ }
221
+
222
+ persist(runtime: AgentTeamsRuntime): void {
223
+ if (!this.hasPersistableState(runtime)) {
224
+ this.clearPersistedState();
225
+ return;
226
+ }
227
+ this.ensureDir();
228
+ const envelope: PersistedTeamEnvelope = {
229
+ version: 1,
230
+ updatedAt: new Date().toISOString(),
231
+ teamState: runtime.exportState(),
232
+ teammates: Array.from(this.teammateSpecs.values()),
233
+ };
234
+ const tmpPath = `${this.statePath}.tmp`;
235
+ writeFileSync(tmpPath, `${JSON.stringify(envelope, null, 2)}\n`, "utf8");
236
+ renameSync(tmpPath, this.statePath);
237
+ }
238
+
239
+ appendTaskHistory(event: TeamEvent): void {
240
+ let task: Record<string, unknown> = {};
241
+ switch (event.type) {
242
+ case "team_task_updated":
243
+ task = event.task as unknown as Record<string, unknown>;
244
+ break;
245
+ case "team_message":
246
+ task = {
247
+ agentId: event.message.fromAgentId,
248
+ toAgentId: event.message.toAgentId,
249
+ subject: event.message.subject,
250
+ taskId: event.message.taskId,
251
+ };
252
+ break;
253
+ case "team_mission_log":
254
+ task = {
255
+ agentId: event.entry.agentId,
256
+ kind: event.entry.kind,
257
+ summary: event.entry.summary,
258
+ taskId: event.entry.taskId,
259
+ };
260
+ break;
261
+ case "teammate_spawned":
262
+ case "teammate_shutdown":
263
+ case "task_start":
264
+ task = {
265
+ agentId: event.agentId,
266
+ message: "message" in event ? event.message : undefined,
267
+ };
268
+ break;
269
+ case "task_end":
270
+ task = {
271
+ agentId: event.agentId,
272
+ finishReason: event.result?.finishReason,
273
+ error: event.error?.message,
274
+ };
275
+ break;
276
+ case "agent_event":
277
+ task = {
278
+ agentId: event.agentId,
279
+ eventType: event.event.type,
280
+ };
281
+ break;
282
+ }
283
+ this.ensureDir();
284
+ appendFileSync(
285
+ this.taskHistoryPath,
286
+ `${JSON.stringify({
287
+ ts: new Date().toISOString(),
288
+ type: event.type,
289
+ task,
290
+ })}\n`,
291
+ "utf8",
292
+ );
293
+ }
294
+
295
+ private ensureDir(): void {
296
+ if (!existsSync(this.dirPath)) {
297
+ mkdirSync(this.dirPath, { recursive: true });
298
+ }
299
+ }
300
+
301
+ private hasPersistableState(runtime: AgentTeamsRuntime): boolean {
302
+ const state = runtime.exportState();
303
+ if (this.teammateSpecs.size > 0) {
304
+ return true;
305
+ }
306
+ if (state.members.some((member) => member.role === "teammate")) {
307
+ return true;
308
+ }
309
+ return (
310
+ state.tasks.length > 0 ||
311
+ state.mailbox.length > 0 ||
312
+ state.missionLog.length > 0
313
+ );
314
+ }
315
+
316
+ private clearPersistedState(): void {
317
+ if (existsSync(this.statePath)) {
318
+ unlinkSync(this.statePath);
319
+ }
320
+ }
321
+ }
322
+
323
+ class LocalSessionPersistenceAdapter implements SessionPersistenceAdapter {
324
+ constructor(private readonly store: SqliteSessionStore) {}
325
+
326
+ ensureSessionsDir(): string {
327
+ return this.store.ensureSessionsDir();
328
+ }
329
+
330
+ async upsertSession(row: SessionRowShape): Promise<void> {
331
+ this.store.run(
332
+ `INSERT OR REPLACE INTO sessions (
333
+ session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
334
+ provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
335
+ parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
336
+ metadata_json, transcript_path, hook_path, messages_path, updated_at
337
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
338
+ [
339
+ row.session_id,
340
+ row.source,
341
+ row.pid,
342
+ row.started_at,
343
+ row.ended_at ?? null,
344
+ row.exit_code ?? null,
345
+ row.status,
346
+ typeof row.status_lock === "number" ? row.status_lock : 0,
347
+ row.interactive,
348
+ row.provider,
349
+ row.model,
350
+ row.cwd,
351
+ row.workspace_root,
352
+ row.team_name ?? null,
353
+ row.enable_tools,
354
+ row.enable_spawn,
355
+ row.enable_teams,
356
+ row.parent_session_id ?? null,
357
+ row.parent_agent_id ?? null,
358
+ row.agent_id ?? null,
359
+ row.conversation_id ?? null,
360
+ row.is_subagent,
361
+ row.prompt ?? null,
362
+ row.metadata_json ?? null,
363
+ row.transcript_path,
364
+ row.hook_path,
365
+ row.messages_path ?? null,
366
+ row.updated_at ?? nowIso(),
367
+ ],
368
+ );
369
+ }
370
+
371
+ async getSession(sessionId: string): Promise<SessionRowShape | undefined> {
372
+ const row = this.store.queryOne<SessionRowShape>(
373
+ `SELECT session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
374
+ provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
375
+ parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
376
+ metadata_json, transcript_path, hook_path, messages_path, updated_at
377
+ FROM sessions WHERE session_id = ?`,
378
+ [sessionId],
379
+ );
380
+ return row ?? undefined;
381
+ }
382
+
383
+ async listSessions(options: {
384
+ limit: number;
385
+ parentSessionId?: string;
386
+ status?: string;
387
+ }): Promise<SessionRowShape[]> {
388
+ const whereClauses: string[] = [];
389
+ const params: unknown[] = [];
390
+ if (options.parentSessionId) {
391
+ whereClauses.push("parent_session_id = ?");
392
+ params.push(options.parentSessionId);
393
+ }
394
+ if (options.status) {
395
+ whereClauses.push("status = ?");
396
+ params.push(options.status);
397
+ }
398
+ const where =
399
+ whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
400
+ return this.store.queryAll<SessionRowShape>(
401
+ `SELECT session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
402
+ provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
403
+ parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
404
+ metadata_json, transcript_path, hook_path, messages_path, updated_at
405
+ FROM sessions
406
+ ${where}
407
+ ORDER BY started_at DESC
408
+ LIMIT ?`,
409
+ [...params, options.limit],
410
+ );
411
+ }
412
+
413
+ async updateSession(
414
+ input: PersistedSessionUpdateInput,
415
+ ): Promise<{ updated: boolean; statusLock: number }> {
416
+ if (input.setRunning) {
417
+ if (input.expectedStatusLock === undefined) {
418
+ return { updated: false, statusLock: 0 };
419
+ }
420
+ const changed = this.store.run(
421
+ `UPDATE sessions
422
+ SET status = 'running', ended_at = NULL, exit_code = NULL, updated_at = ?, status_lock = ?,
423
+ parent_session_id = ?, parent_agent_id = ?, agent_id = ?, conversation_id = ?, is_subagent = 1,
424
+ prompt = COALESCE(prompt, ?)
425
+ WHERE session_id = ? AND status_lock = ?`,
426
+ [
427
+ nowIso(),
428
+ input.expectedStatusLock + 1,
429
+ input.parentSessionId ?? null,
430
+ input.parentAgentId ?? null,
431
+ input.agentId ?? null,
432
+ input.conversationId ?? null,
433
+ input.prompt ?? null,
434
+ input.sessionId,
435
+ input.expectedStatusLock,
436
+ ],
437
+ );
438
+ return {
439
+ updated: (changed.changes ?? 0) > 0,
440
+ statusLock: input.expectedStatusLock + 1,
441
+ };
442
+ }
443
+
444
+ const fields: string[] = [];
445
+ const params: unknown[] = [];
446
+ if (input.status !== undefined) {
447
+ fields.push("status = ?");
448
+ params.push(input.status);
449
+ }
450
+ if (input.endedAt !== undefined) {
451
+ fields.push("ended_at = ?");
452
+ params.push(input.endedAt);
453
+ }
454
+ if (input.exitCode !== undefined) {
455
+ fields.push("exit_code = ?");
456
+ params.push(input.exitCode);
457
+ }
458
+ if (input.prompt !== undefined) {
459
+ fields.push("prompt = ?");
460
+ params.push(input.prompt ?? null);
461
+ }
462
+ if (input.metadataJson !== undefined) {
463
+ fields.push("metadata_json = ?");
464
+ params.push(input.metadataJson ?? null);
465
+ }
466
+ if (input.parentSessionId !== undefined) {
467
+ fields.push("parent_session_id = ?");
468
+ params.push(input.parentSessionId ?? null);
469
+ }
470
+ if (input.parentAgentId !== undefined) {
471
+ fields.push("parent_agent_id = ?");
472
+ params.push(input.parentAgentId ?? null);
473
+ }
474
+ if (input.agentId !== undefined) {
475
+ fields.push("agent_id = ?");
476
+ params.push(input.agentId ?? null);
477
+ }
478
+ if (input.conversationId !== undefined) {
479
+ fields.push("conversation_id = ?");
480
+ params.push(input.conversationId ?? null);
481
+ }
482
+ if (fields.length === 0) {
483
+ const row = await this.getSession(input.sessionId);
484
+ return { updated: !!row, statusLock: row?.status_lock ?? 0 };
485
+ }
486
+
487
+ let statusLock = 0;
488
+ if (input.expectedStatusLock !== undefined) {
489
+ statusLock = input.expectedStatusLock + 1;
490
+ fields.push("status_lock = ?");
491
+ params.push(statusLock);
492
+ }
493
+ fields.push("updated_at = ?");
494
+ params.push(nowIso());
495
+
496
+ let sql = `UPDATE sessions SET ${fields.join(", ")} WHERE session_id = ?`;
497
+ params.push(input.sessionId);
498
+ if (input.expectedStatusLock !== undefined) {
499
+ sql += " AND status_lock = ?";
500
+ params.push(input.expectedStatusLock);
501
+ }
502
+ const changed = this.store.run(sql, params);
503
+ if ((changed.changes ?? 0) === 0) {
504
+ return { updated: false, statusLock: 0 };
505
+ }
506
+ if (input.expectedStatusLock === undefined) {
507
+ const row = await this.getSession(input.sessionId);
508
+ statusLock = row?.status_lock ?? 0;
509
+ }
510
+ return { updated: true, statusLock };
511
+ }
512
+
513
+ async deleteSession(sessionId: string, cascade: boolean): Promise<boolean> {
514
+ const changed =
515
+ this.store.run(`DELETE FROM sessions WHERE session_id = ?`, [sessionId])
516
+ .changes ?? 0;
517
+ if (cascade) {
518
+ this.store.run(`DELETE FROM sessions WHERE parent_session_id = ?`, [
519
+ sessionId,
520
+ ]);
521
+ }
522
+ return changed > 0;
523
+ }
524
+
525
+ async enqueueSpawnRequest(input: {
526
+ rootSessionId: string;
527
+ parentAgentId: string;
528
+ task?: string;
529
+ systemPrompt?: string;
530
+ }): Promise<void> {
531
+ this.store.run(
532
+ `INSERT INTO subagent_spawn_queue (root_session_id, parent_agent_id, task, system_prompt, created_at, consumed_at)
533
+ VALUES (?, ?, ?, ?, ?, NULL)`,
534
+ [
535
+ input.rootSessionId,
536
+ input.parentAgentId,
537
+ input.task ?? null,
538
+ input.systemPrompt ?? null,
539
+ nowIso(),
540
+ ],
541
+ );
542
+ }
543
+
544
+ async claimSpawnRequest(
545
+ rootSessionId: string,
546
+ parentAgentId: string,
547
+ ): Promise<string | undefined> {
548
+ const row = this.store.queryOne<{ id?: number; task?: string | null }>(
549
+ `SELECT id, task FROM subagent_spawn_queue
550
+ WHERE root_session_id = ? AND parent_agent_id = ? AND consumed_at IS NULL
551
+ ORDER BY id ASC LIMIT 1`,
552
+ [rootSessionId, parentAgentId],
553
+ );
554
+ if (!row || typeof row.id !== "number") {
555
+ return undefined;
556
+ }
557
+ this.store.run(
558
+ `UPDATE subagent_spawn_queue SET consumed_at = ? WHERE id = ?`,
559
+ [nowIso(), row.id],
560
+ );
561
+ return row.task ?? undefined;
562
+ }
563
+ }
564
+
565
+ export class CoreSessionService extends UnifiedSessionPersistenceService {
566
+ constructor(private readonly store: SqliteSessionStore) {
567
+ super(new LocalSessionPersistenceAdapter(store));
568
+ }
569
+
570
+ createRootSession(input: CreateRootSessionInput): void {
571
+ this.store.run(
572
+ `INSERT OR REPLACE INTO sessions (
573
+ session_id, source, pid, started_at, ended_at, exit_code, status, status_lock, interactive,
574
+ provider, model, cwd, workspace_root, team_name, enable_tools, enable_spawn, enable_teams,
575
+ parent_session_id, parent_agent_id, agent_id, conversation_id, is_subagent, prompt,
576
+ metadata_json, transcript_path, hook_path, messages_path, updated_at
577
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
578
+ [
579
+ input.sessionId,
580
+ input.source,
581
+ input.pid,
582
+ input.startedAt,
583
+ null,
584
+ null,
585
+ "running",
586
+ 0,
587
+ input.interactive ? 1 : 0,
588
+ input.provider,
589
+ input.model,
590
+ input.cwd,
591
+ input.workspaceRoot,
592
+ input.teamName ?? null,
593
+ input.enableTools ? 1 : 0,
594
+ input.enableSpawn ? 1 : 0,
595
+ input.enableTeams ? 1 : 0,
596
+ null,
597
+ null,
598
+ null,
599
+ null,
600
+ 0,
601
+ input.prompt ?? null,
602
+ input.metadata ? JSON.stringify(input.metadata) : null,
603
+ input.transcriptPath,
604
+ input.hookPath,
605
+ input.messagesPath,
606
+ nowIso(),
607
+ ],
608
+ );
609
+ }
610
+ }