jg-agent-hq 8.0.2 → 9.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 (232) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +18 -37
  3. package/api/README.md +51 -243
  4. package/dist/actions/actions.d.ts +5 -50
  5. package/dist/actions/actions.js +49 -190
  6. package/dist/actions/reads.d.ts +3 -2
  7. package/dist/actions/reads.js +12 -5
  8. package/dist/actions/waits/index.d.ts +1 -0
  9. package/dist/actions/waits/index.js +2 -0
  10. package/dist/actions/waits/waits.d.ts +37 -0
  11. package/dist/actions/waits/waits.js +107 -0
  12. package/dist/cli/cli.js +6 -56
  13. package/dist/contracts/actions/index.d.ts +2 -4
  14. package/dist/contracts/actions/index.js +1 -3
  15. package/dist/contracts/actions/types.d.ts +135 -142
  16. package/dist/contracts/actions/types.js +12 -22
  17. package/dist/contracts/browser/types.d.ts +12 -605
  18. package/dist/contracts/browser/types.js +2 -5
  19. package/dist/contracts/evidence/types.d.ts +4 -7
  20. package/dist/contracts/evidence/types.js +2 -8
  21. package/dist/contracts/index.d.ts +5 -16
  22. package/dist/contracts/index.js +4 -15
  23. package/dist/contracts/locks/index.d.ts +2 -0
  24. package/dist/contracts/locks/index.js +2 -0
  25. package/dist/contracts/locks/types.d.ts +50 -0
  26. package/dist/contracts/locks/types.js +36 -0
  27. package/dist/contracts/providers/types.d.ts +0 -38
  28. package/dist/contracts/providers/types.js +0 -1
  29. package/dist/contracts/results/types.js +1 -1
  30. package/dist/contracts/sessions/types.d.ts +2 -0
  31. package/dist/contracts/sessions/types.js +7 -1
  32. package/dist/contracts/skills/types.d.ts +1 -0
  33. package/dist/contracts/skills/types.js +5 -0
  34. package/dist/contracts/types.d.ts +4526 -12307
  35. package/dist/contracts/types.js +13 -12
  36. package/dist/index.d.ts +0 -3
  37. package/dist/index.js +0 -2
  38. package/dist/providers/claude/adapter.js +1 -11
  39. package/dist/providers/claude/claude.d.ts +6 -3
  40. package/dist/providers/claude/claude.js +49 -9
  41. package/dist/providers/claude/control/control.d.ts +2 -20
  42. package/dist/providers/claude/control/control.js +88 -155
  43. package/dist/providers/claude/control/permissions.d.ts +0 -9
  44. package/dist/providers/claude/control/permissions.js +0 -22
  45. package/dist/providers/claude/control/skills.js +1 -0
  46. package/dist/providers/claude/control/stream.js +1 -34
  47. package/dist/providers/claude/control/types.d.ts +0 -3
  48. package/dist/providers/claude/history/history.model.d.ts +9 -0
  49. package/dist/providers/claude/history/history.model.js +77 -43
  50. package/dist/providers/claude/history/types.d.ts +284 -3045
  51. package/dist/providers/claude/history/types.js +1 -0
  52. package/dist/providers/claude/rpc.d.ts +0 -5
  53. package/dist/providers/claude/rpc.js +0 -3
  54. package/dist/providers/codex/adapter.js +2 -12
  55. package/dist/providers/codex/codex.d.ts +1 -2
  56. package/dist/providers/codex/codex.js +2 -100
  57. package/dist/providers/codex/types.d.ts +2 -4
  58. package/dist/providers/registry/registry.d.ts +0 -2
  59. package/dist/providers/registry/registry.js +0 -7
  60. package/dist/providers/types.d.ts +4 -6
  61. package/dist/providers/types.js +3 -2
  62. package/dist/runtime/runtime.js +10 -81
  63. package/dist/runtime/types.d.ts +1 -18
  64. package/dist/runtime/types.js +0 -1
  65. package/dist/sessions/sessions.js +0 -1
  66. package/dist/skills/skills.js +3 -0
  67. package/dist/snapshots/archive/archive.js +2 -23
  68. package/dist/snapshots/capture.d.ts +2 -5
  69. package/dist/snapshots/capture.js +4 -85
  70. package/dist/snapshots/constants.d.ts +0 -1
  71. package/dist/snapshots/constants.js +1 -6
  72. package/dist/snapshots/database.d.ts +2 -3
  73. package/dist/snapshots/database.js +9 -114
  74. package/dist/snapshots/fences.d.ts +3 -5
  75. package/dist/snapshots/fences.js +3 -15
  76. package/dist/snapshots/files.d.ts +3 -1
  77. package/dist/snapshots/files.js +15 -4
  78. package/dist/snapshots/snapshots.d.ts +6 -7
  79. package/dist/snapshots/snapshots.js +9 -16
  80. package/dist/snapshots/types.d.ts +5 -81
  81. package/dist/snapshots/types.js +7 -30
  82. package/dist/storage/constants.d.ts +1 -0
  83. package/dist/storage/constants.js +5 -1
  84. package/dist/storage/types.d.ts +1 -0
  85. package/dist/storage/types.js +1 -0
  86. package/dist/store/control/constants.d.ts +3 -2
  87. package/dist/store/control/constants.js +3 -1
  88. package/dist/store/control/control.model.d.ts +1 -11
  89. package/dist/store/control/control.model.js +10 -131
  90. package/dist/store/control/transformers.d.ts +6 -3
  91. package/dist/store/control/transformers.js +6 -5
  92. package/dist/store/control/types.d.ts +91 -170
  93. package/dist/store/control/types.js +5 -7
  94. package/dist/store/schema.js +1 -1
  95. package/dist/store/schema.sql +3 -360
  96. package/dist/store/sessions/sessions.model.d.ts +10 -6
  97. package/dist/store/sessions/sessions.model.js +19 -2
  98. package/dist/store/snapshot.d.ts +10 -0
  99. package/dist/store/snapshot.js +37 -0
  100. package/dist/store/store.js +1 -54
  101. package/dist/store/store.worker.js +5 -97
  102. package/dist/store/types.d.ts +241 -4802
  103. package/dist/store/types.js +1 -44
  104. package/docs/recovery.md +10 -6
  105. package/errors/README.md +29 -39
  106. package/package.json +3 -4
  107. package/dist/agent-tools/agent-tools.d.ts +0 -6
  108. package/dist/agent-tools/agent-tools.js +0 -181
  109. package/dist/agent-tools/constants.d.ts +0 -7
  110. package/dist/agent-tools/constants.js +0 -45
  111. package/dist/agent-tools/index.d.ts +0 -4
  112. package/dist/agent-tools/index.js +0 -4
  113. package/dist/agent-tools/mcp/index.d.ts +0 -1
  114. package/dist/agent-tools/mcp/index.js +0 -2
  115. package/dist/agent-tools/mcp/mcp.d.ts +0 -7
  116. package/dist/agent-tools/mcp/mcp.js +0 -50
  117. package/dist/agent-tools/types.d.ts +0 -64
  118. package/dist/agent-tools/types.js +0 -1
  119. package/dist/agent-tools/utilities.d.ts +0 -13
  120. package/dist/agent-tools/utilities.js +0 -37
  121. package/dist/collaboration/collaboration.d.ts +0 -7
  122. package/dist/collaboration/collaboration.js +0 -119
  123. package/dist/collaboration/constants.d.ts +0 -2
  124. package/dist/collaboration/constants.js +0 -3
  125. package/dist/collaboration/dispatch/dispatch.d.ts +0 -12
  126. package/dist/collaboration/dispatch/dispatch.js +0 -148
  127. package/dist/collaboration/dispatch/index.d.ts +0 -3
  128. package/dist/collaboration/dispatch/index.js +0 -4
  129. package/dist/collaboration/dispatch/types.d.ts +0 -632
  130. package/dist/collaboration/dispatch/types.js +0 -44
  131. package/dist/collaboration/grants/grants.d.ts +0 -45
  132. package/dist/collaboration/grants/grants.js +0 -131
  133. package/dist/collaboration/grants/index.d.ts +0 -1
  134. package/dist/collaboration/grants/index.js +0 -2
  135. package/dist/collaboration/identity/credentials.d.ts +0 -17
  136. package/dist/collaboration/identity/credentials.js +0 -182
  137. package/dist/collaboration/identity/index.d.ts +0 -4
  138. package/dist/collaboration/identity/index.js +0 -4
  139. package/dist/collaboration/identity/types.d.ts +0 -171
  140. package/dist/collaboration/identity/types.js +0 -87
  141. package/dist/collaboration/index.d.ts +0 -3
  142. package/dist/collaboration/index.js +0 -3
  143. package/dist/collaboration/types.d.ts +0 -25
  144. package/dist/collaboration/types.js +0 -1
  145. package/dist/contracts/agent-tools/index.d.ts +0 -2
  146. package/dist/contracts/agent-tools/index.js +0 -2
  147. package/dist/contracts/agent-tools/types.d.ts +0 -697
  148. package/dist/contracts/agent-tools/types.js +0 -125
  149. package/dist/contracts/collaboration/index.d.ts +0 -4
  150. package/dist/contracts/collaboration/index.js +0 -4
  151. package/dist/contracts/collaboration/reports/constants.d.ts +0 -1
  152. package/dist/contracts/collaboration/reports/constants.js +0 -2
  153. package/dist/contracts/collaboration/reports/index.d.ts +0 -3
  154. package/dist/contracts/collaboration/reports/index.js +0 -3
  155. package/dist/contracts/collaboration/reports/types.d.ts +0 -69
  156. package/dist/contracts/collaboration/reports/types.js +0 -53
  157. package/dist/contracts/collaboration/types.d.ts +0 -6734
  158. package/dist/contracts/collaboration/types.js +0 -696
  159. package/dist/contracts/plans/index.d.ts +0 -6
  160. package/dist/contracts/plans/index.js +0 -6
  161. package/dist/contracts/plans/types.d.ts +0 -1788
  162. package/dist/contracts/plans/types.js +0 -278
  163. package/dist/plans/admission.d.ts +0 -288
  164. package/dist/plans/admission.js +0 -393
  165. package/dist/plans/authority.d.ts +0 -21
  166. package/dist/plans/authority.js +0 -207
  167. package/dist/plans/documents.d.ts +0 -80
  168. package/dist/plans/documents.js +0 -238
  169. package/dist/plans/evidence.d.ts +0 -11
  170. package/dist/plans/evidence.js +0 -211
  171. package/dist/plans/files.d.ts +0 -55
  172. package/dist/plans/files.js +0 -264
  173. package/dist/plans/finalization.d.ts +0 -11
  174. package/dist/plans/finalization.js +0 -30
  175. package/dist/plans/index.d.ts +0 -5
  176. package/dist/plans/index.js +0 -6
  177. package/dist/plans/plans.d.ts +0 -325
  178. package/dist/plans/plans.js +0 -803
  179. package/dist/plans/projections.d.ts +0 -13
  180. package/dist/plans/projections.js +0 -67
  181. package/dist/plans/revisions.d.ts +0 -50
  182. package/dist/plans/revisions.js +0 -181
  183. package/dist/plans/types.d.ts +0 -114
  184. package/dist/plans/types.js +0 -43
  185. package/dist/plans/utilities.d.ts +0 -36
  186. package/dist/plans/utilities.js +0 -62
  187. package/dist/snapshots/artifacts.d.ts +0 -16
  188. package/dist/snapshots/artifacts.js +0 -75
  189. package/dist/store/collaboration/authority.d.ts +0 -228
  190. package/dist/store/collaboration/authority.js +0 -128
  191. package/dist/store/collaboration/collaboration.model.d.ts +0 -412
  192. package/dist/store/collaboration/collaboration.model.js +0 -254
  193. package/dist/store/collaboration/constants.d.ts +0 -13
  194. package/dist/store/collaboration/constants.js +0 -14
  195. package/dist/store/collaboration/dispatch.d.ts +0 -19
  196. package/dist/store/collaboration/dispatch.js +0 -197
  197. package/dist/store/collaboration/identity.d.ts +0 -21
  198. package/dist/store/collaboration/identity.js +0 -146
  199. package/dist/store/collaboration/index.d.ts +0 -3
  200. package/dist/store/collaboration/index.js +0 -5
  201. package/dist/store/collaboration/invocation.d.ts +0 -21
  202. package/dist/store/collaboration/invocation.js +0 -162
  203. package/dist/store/collaboration/messages.d.ts +0 -54
  204. package/dist/store/collaboration/messages.js +0 -157
  205. package/dist/store/collaboration/outcomes.d.ts +0 -11
  206. package/dist/store/collaboration/outcomes.js +0 -216
  207. package/dist/store/collaboration/queries.d.ts +0 -23
  208. package/dist/store/collaboration/queries.js +0 -259
  209. package/dist/store/collaboration/reports.d.ts +0 -37
  210. package/dist/store/collaboration/reports.js +0 -117
  211. package/dist/store/collaboration/requests.d.ts +0 -22
  212. package/dist/store/collaboration/requests.js +0 -111
  213. package/dist/store/collaboration/search.d.ts +0 -13
  214. package/dist/store/collaboration/search.js +0 -58
  215. package/dist/store/collaboration/spawn.d.ts +0 -33
  216. package/dist/store/collaboration/spawn.js +0 -96
  217. package/dist/store/collaboration/streams.d.ts +0 -13
  218. package/dist/store/collaboration/streams.js +0 -100
  219. package/dist/store/collaboration/tasks.d.ts +0 -20
  220. package/dist/store/collaboration/tasks.js +0 -169
  221. package/dist/store/collaboration/transformers.d.ts +0 -56
  222. package/dist/store/collaboration/transformers.js +0 -179
  223. package/dist/store/collaboration/types.d.ts +0 -25
  224. package/dist/store/collaboration/types.js +0 -8
  225. package/dist/store/collaboration/utilities.d.ts +0 -23
  226. package/dist/store/collaboration/utilities.js +0 -77
  227. package/dist/store/collaboration/waits.d.ts +0 -26
  228. package/dist/store/collaboration/waits.js +0 -114
  229. package/dist/store/plans/index.d.ts +0 -1
  230. package/dist/store/plans/index.js +0 -2
  231. package/dist/store/plans/plans.model.d.ts +0 -47
  232. package/dist/store/plans/plans.model.js +0 -179
@@ -1,412 +0,0 @@
1
- import type { DatabaseSync } from 'node:sqlite';
2
- import { type ICollaborationRequest, type ICollaborationResponse } from '../../contracts/collaboration/index.js';
3
- import type { IAgentIdentityQuery, IAgentIdentity, INativeToolBinding, IAgentSpawnRead, IAgentSpawnRecord } from '../../collaboration/identity/index.js';
4
- import type { IDispatchScan } from '../../collaboration/dispatch/index.js';
5
- import type { IDispatchBlocked } from '../../contracts/collaboration/index.js';
6
- /** Owns short collaboration transactions and exact metadata-operation replay in the SQLite worker. */
7
- export declare class CollaborationModel {
8
- private readonly __database;
9
- private readonly __runtimeEpoch;
10
- constructor(database: DatabaseSync, runtimeEpoch: string);
11
- /**
12
- * Reconstructs current session identity and bounded task discovery without trusting caller labels.
13
- * @returns Native-bound identity and current task permissions.
14
- * @throws
15
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
16
- * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
17
- * - JG_AGENT_HQ_REQUEST_INVALID: The HQ request is invalid
18
- */
19
- identity(query: IAgentIdentityQuery): IAgentIdentity;
20
- /**
21
- * Reads the exact caller-owned native spawn relationship without replaying creation.
22
- * @returns The retained relationship or null when no action used that identity.
23
- * @throws
24
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
25
- * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
26
- */
27
- spawn(query: IAgentSpawnRead): IAgentSpawnRecord | null;
28
- /** Reads a bounded durable recovery page without loading native results. */
29
- dispatchPage(query: IDispatchScan): {
30
- data: {
31
- taskId: string;
32
- requestId: string;
33
- sequence: number;
34
- }[];
35
- boundary: number;
36
- };
37
- /** Resolves one dispatch intent against current membership, policy and native ownership. */
38
- dispatchJob(taskId: string, requestId: string): {
39
- request: {
40
- id: string;
41
- taskId: string;
42
- streamId: string;
43
- sequence: number;
44
- requester: {
45
- kind: "host";
46
- operatorRef: string;
47
- } | {
48
- kind: "agent";
49
- agentId: string;
50
- };
51
- specification: {
52
- targetAgentId: string | null;
53
- claimable: boolean;
54
- skill: {
55
- name: string;
56
- mode: string | null;
57
- } | null;
58
- grantId: string | null;
59
- grantRevision: number | null;
60
- arguments: string;
61
- effects: ("resume" | "read" | "planning-write" | "workspace-write" | "commit" | "push" | "pause")[];
62
- scopes: {
63
- workspaceRoot: string;
64
- pathPrefixes: string[];
65
- }[];
66
- plan: {
67
- planId: string;
68
- directory: string;
69
- revision: {
70
- planDigest: string;
71
- breakdownDigest: string | null;
72
- };
73
- milestoneIds: string[];
74
- } | null;
75
- targetReferences: ({
76
- kind: "repository";
77
- workspaceRoot: string;
78
- path: string;
79
- commit?: string | undefined;
80
- sha256?: string | undefined;
81
- startLine?: number | undefined;
82
- endLine?: number | undefined;
83
- } | {
84
- kind: "report";
85
- reportId: string;
86
- sha256: string;
87
- } | {
88
- kind: "native";
89
- provider: string;
90
- nativeId: string;
91
- turnId: string;
92
- itemId: string;
93
- sha256: string;
94
- } | {
95
- kind: "plan";
96
- planId: string;
97
- revision: {
98
- planDigest: string;
99
- breakdownDigest: string | null;
100
- };
101
- milestoneId: string | null;
102
- })[];
103
- returnStreamId?: string | undefined;
104
- };
105
- assigneeAgentId: string | null;
106
- parentRequestId: string | null;
107
- parentActionId: string | null;
108
- childRequestIds: string[];
109
- waitGeneration: number;
110
- continuationActionId: string | null;
111
- clarificationConsumedActionId: string | null;
112
- state: "canceled" | "interrupted" | "waiting-children" | "open" | "claimed" | "waiting-input" | "resolved";
113
- currentActionId: string | null;
114
- pendingActionId: string | null;
115
- pendingInputMessageId: string | null;
116
- manualDispatchId: string | null;
117
- lastActionId: string | null;
118
- lastReportId: string | null;
119
- dispatchCount: number;
120
- requiredAuthority: "requester" | "operator" | null;
121
- pauseAgentRevision: number | null;
122
- dispatchBlockedReason: "automatic-dispatch-disabled" | "busy" | "native-approval" | "provider-unavailable" | "skill-unavailable" | "ownership-uncertain" | "agent-paused" | "task-paused" | "grant-invalid" | "plan-stale" | "plan-authorization-required" | "capacity" | "turn-limit" | "native-uncertain" | "action-conflict" | "child-wait" | null;
123
- failureReason: "delivery-failed" | "native-failed" | "missing-response" | "invalid-response" | "wrong-request" | "capture-too-large" | "report-too-large" | "report-invalid-utf8" | "pause-not-authorized" | "invalid-child-wait" | "children-pending" | null;
124
- outcome: "completed" | "failed" | null;
125
- resultReferences: ({
126
- kind: "repository";
127
- workspaceRoot: string;
128
- path: string;
129
- commit?: string | undefined;
130
- sha256?: string | undefined;
131
- startLine?: number | undefined;
132
- endLine?: number | undefined;
133
- } | {
134
- kind: "report";
135
- reportId: string;
136
- sha256: string;
137
- } | {
138
- kind: "native";
139
- provider: string;
140
- nativeId: string;
141
- turnId: string;
142
- itemId: string;
143
- sha256: string;
144
- } | {
145
- kind: "plan";
146
- planId: string;
147
- revision: {
148
- planDigest: string;
149
- breakdownDigest: string | null;
150
- };
151
- milestoneId: string | null;
152
- })[];
153
- createdAt: number;
154
- updatedAt: number;
155
- };
156
- task: {
157
- id: string;
158
- workspaceRoot: string;
159
- title: string;
160
- ownerOperatorRef: string;
161
- plan: {
162
- planId: string;
163
- directory: string;
164
- revision: {
165
- planDigest: string;
166
- breakdownDigest: string | null;
167
- };
168
- milestoneIds: string[];
169
- } | null;
170
- policy: {
171
- providers: string[];
172
- workspaceRoots: string[];
173
- effects: ("resume" | "read" | "planning-write" | "workspace-write" | "commit" | "push" | "pause")[];
174
- canSpawn: boolean;
175
- canManagePeerSubscriptions: boolean;
176
- canExecuteRequests: boolean;
177
- automaticDispatch: boolean;
178
- concurrencyLimit: number;
179
- spawnLimit: number;
180
- automaticTurnLimit: number;
181
- };
182
- policyRevision: number;
183
- spawnCount: number;
184
- automaticTurnCount: number;
185
- state: "paused" | "active" | "stopped";
186
- lifecycleRevision: number;
187
- createdAt: number;
188
- };
189
- member: {
190
- taskId: string;
191
- agentId: string;
192
- parentAgentId: string | null;
193
- role: string | null;
194
- provider: string;
195
- workspaceRoot: string;
196
- permissions: {
197
- providers: string[];
198
- workspaceRoots: string[];
199
- effects: ("resume" | "read" | "planning-write" | "workspace-write" | "commit" | "push" | "pause")[];
200
- canSpawn: boolean;
201
- canManagePeerSubscriptions: boolean;
202
- canExecuteRequests: boolean;
203
- };
204
- revision: number;
205
- state: "paused" | "active" | "stopped";
206
- isRevoked: boolean;
207
- createdAt: number;
208
- } | null;
209
- thread: {
210
- id: string;
211
- provider: string;
212
- nativeId: string | null;
213
- nativeLineage: {
214
- sessionId: string | null;
215
- parentThreadId: string | null;
216
- forkedFromId: string | null;
217
- } | null;
218
- forkedFromThreadId: string | null;
219
- isArchived: boolean;
220
- name: string;
221
- workspaceRoot: string;
222
- ownership: "managed" | "uncertain";
223
- runtimeEpoch: string | null;
224
- activeTurnId: string | null;
225
- state: "unknown" | "idle" | "completed" | "failed" | "interrupted" | "waiting-input" | "running" | "creating" | "inactive" | "starting" | "waiting-approval" | "stopping";
226
- createdAt: number;
227
- updatedAt: number;
228
- } | null;
229
- context: {
230
- taskId: string;
231
- requestId: string;
232
- pendingActionId: string;
233
- agentId: string;
234
- specification: {
235
- targetAgentId: string | null;
236
- claimable: boolean;
237
- skill: {
238
- name: string;
239
- mode: string | null;
240
- } | null;
241
- grantId: string | null;
242
- grantRevision: number | null;
243
- arguments: string;
244
- effects: ("resume" | "read" | "planning-write" | "workspace-write" | "commit" | "push" | "pause")[];
245
- scopes: {
246
- workspaceRoot: string;
247
- pathPrefixes: string[];
248
- }[];
249
- plan: {
250
- planId: string;
251
- directory: string;
252
- revision: {
253
- planDigest: string;
254
- breakdownDigest: string | null;
255
- };
256
- milestoneIds: string[];
257
- } | null;
258
- targetReferences: ({
259
- kind: "repository";
260
- workspaceRoot: string;
261
- path: string;
262
- commit?: string | undefined;
263
- sha256?: string | undefined;
264
- startLine?: number | undefined;
265
- endLine?: number | undefined;
266
- } | {
267
- kind: "report";
268
- reportId: string;
269
- sha256: string;
270
- } | {
271
- kind: "native";
272
- provider: string;
273
- nativeId: string;
274
- turnId: string;
275
- itemId: string;
276
- sha256: string;
277
- } | {
278
- kind: "plan";
279
- planId: string;
280
- revision: {
281
- planDigest: string;
282
- breakdownDigest: string | null;
283
- };
284
- milestoneId: string | null;
285
- })[];
286
- returnStreamId?: string | undefined;
287
- };
288
- requester: {
289
- kind: "host";
290
- operatorRef: string;
291
- } | {
292
- kind: "agent";
293
- agentId: string;
294
- };
295
- inputMessageId: string | null;
296
- continuation: {
297
- generation: number;
298
- kind: "terminal-results" | "clarification";
299
- children: {
300
- requestId: string;
301
- state: "canceled" | "interrupted" | "waiting-children" | "open" | "claimed" | "waiting-input" | "resolved";
302
- outcome: "completed" | "failed" | null;
303
- actionId: string | null;
304
- report: {
305
- id: string;
306
- sha256: string;
307
- } | null;
308
- requiredAuthority: "requester" | "operator" | null;
309
- failureReason: "delivery-failed" | "native-failed" | "missing-response" | "invalid-response" | "wrong-request" | "capture-too-large" | "report-too-large" | "report-invalid-utf8" | "pause-not-authorized" | "invalid-child-wait" | "children-pending" | null;
310
- excerpt: string;
311
- isClarification: boolean;
312
- }[];
313
- } | null;
314
- isAutomatic: boolean;
315
- replyMessageId: string;
316
- interruptActionId: string;
317
- packet: string;
318
- provider: string;
319
- nativeId: string;
320
- runtimeEpoch: string;
321
- } | null;
322
- continuation: {
323
- generation: number;
324
- kind: "terminal-results" | "clarification";
325
- children: {
326
- requestId: string;
327
- state: "canceled" | "interrupted" | "waiting-children" | "open" | "claimed" | "waiting-input" | "resolved";
328
- outcome: "completed" | "failed" | null;
329
- actionId: string | null;
330
- report: {
331
- id: string;
332
- sha256: string;
333
- } | null;
334
- requiredAuthority: "requester" | "operator" | null;
335
- failureReason: "delivery-failed" | "native-failed" | "missing-response" | "invalid-response" | "wrong-request" | "capture-too-large" | "report-too-large" | "report-invalid-utf8" | "pause-not-authorized" | "invalid-child-wait" | "children-pending" | null;
336
- excerpt: string;
337
- isClarification: boolean;
338
- }[];
339
- } | null;
340
- packet: string | null;
341
- blockedReason: "automatic-dispatch-disabled" | "busy" | "native-approval" | "provider-unavailable" | "skill-unavailable" | "ownership-uncertain" | "agent-paused" | "task-paused" | "grant-invalid" | "plan-stale" | "plan-authorization-required" | "capacity" | "turn-limit" | "native-uncertain" | "action-conflict" | "child-wait" | null;
342
- };
343
- /** Stores a visible blocked reason only while the selected pending intent still matches. */
344
- blockDispatch(requestId: string, selectedActionId: string, reason: IDispatchBlocked | null): void;
345
- /** Collects saved terminal evidence without asking an agent to publish or classify its report. */
346
- collectDispatch(taskId: string, requestId: string, actionId: string): {
347
- actionId: string;
348
- requestId: string;
349
- recoveryActionId: string | null;
350
- report: {
351
- id: string;
352
- taskId: string;
353
- requestId: string;
354
- actionId: string;
355
- replyMessageId: string;
356
- authorAgentId: string;
357
- provider: string;
358
- nativeId: string;
359
- nativeTurnId: string;
360
- outcome: "completed" | "failed" | "waiting-children" | "needs-input" | "paused";
361
- sha256: string;
362
- byteCount: number;
363
- createdAt: number;
364
- } | null;
365
- reply: {
366
- id: string;
367
- sequence: number;
368
- taskId: string;
369
- streamId: string;
370
- author: {
371
- kind: "host";
372
- operatorRef: string;
373
- } | {
374
- kind: "agent";
375
- agentId: string;
376
- } | {
377
- kind: "runtime";
378
- };
379
- kind: "status" | "information" | "request" | "reply";
380
- summary: string;
381
- bodyBytes: number;
382
- referenceCount: number;
383
- replyToMessageId: string | null;
384
- createdAt: number;
385
- reportMatch?: {
386
- reportId: string;
387
- sha256: string;
388
- excerpt: string;
389
- } | undefined;
390
- };
391
- state: "canceled" | "interrupted" | "waiting-children" | "open" | "claimed" | "waiting-input" | "resolved";
392
- failureReason: "delivery-failed" | "native-failed" | "missing-response" | "invalid-response" | "wrong-request" | "capture-too-large" | "report-too-large" | "report-invalid-utf8" | "pause-not-authorized" | "invalid-child-wait" | "children-pending" | null;
393
- } | null;
394
- /**
395
- * Applies one authorized metadata command, committing its result and delivery intent atomically.
396
- * @returns The selected record or original committed operation receipt.
397
- * @throws
398
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
399
- * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
400
- * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
401
- * - JG_AGENT_HQ_REQUEST_INVALID: The HQ request is invalid
402
- */
403
- handle(request: ICollaborationRequest, binding?: INativeToolBinding): ICollaborationResponse;
404
- /**
405
- * Advances at most one subscriber batch without native calls or an in-memory queue.
406
- * @returns Whether a durable batch was processed.
407
- */
408
- recoverFanout(): boolean;
409
- private __readOperation;
410
- private __readOperationById;
411
- private __transaction;
412
- }
@@ -1,254 +0,0 @@
1
- import { hashValue } from 'jg-crypto';
2
- import { z } from 'zod';
3
- import { CollaborationOperationSchema, CollaborationResponseSchema, } from '../../contracts/collaboration/index.js';
4
- import { collaborationConflict, collaborationForbidden } from '../../collaboration/grants/index.js';
5
- import { taskAccess, requireHost } from './authority.js';
6
- import { createTask, mutateTask } from './tasks.js';
7
- import { mutateStream } from './streams.js';
8
- import { acknowledgeMessage, fanoutBatch, publishMessage } from './messages.js';
9
- import { mutateRequest } from './requests.js';
10
- import { listTasks, queryCollaboration } from './queries.js';
11
- import { assertCollaborationBudget } from './utilities.js';
12
- import { readAgentIdentity, readNativeAgent } from './identity.js';
13
- import { readAgentSpawn } from './spawn.js';
14
- import { mutateCheckpoint, readInvocation } from './invocation.js';
15
- import { collectDispatch } from './outcomes.js';
16
- import { readReport } from './reports.js';
17
- import { scanDispatch, readDispatchJob } from './dispatch.js';
18
- import { refreshWait, canReserveContinuation } from './waits.js';
19
- /** Owns short collaboration transactions and exact metadata-operation replay in the SQLite worker. */
20
- export class CollaborationModel {
21
- __database;
22
- __runtimeEpoch;
23
- constructor(database, runtimeEpoch) {
24
- this.__database = database;
25
- this.__runtimeEpoch = runtimeEpoch;
26
- }
27
- /**
28
- * Reconstructs current session identity and bounded task discovery without trusting caller labels.
29
- * @returns Native-bound identity and current task permissions.
30
- * @throws
31
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
32
- * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
33
- * - JG_AGENT_HQ_REQUEST_INVALID: The HQ request is invalid
34
- */
35
- identity(query) {
36
- return this.__transaction(() => readAgentIdentity(this.__database, query, this.__runtimeEpoch), true);
37
- }
38
- /**
39
- * Reads the exact caller-owned native spawn relationship without replaying creation.
40
- * @returns The retained relationship or null when no action used that identity.
41
- * @throws
42
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
43
- * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
44
- */
45
- spawn(query) {
46
- return this.__transaction(() => readAgentSpawn(this.__database, query, this.__runtimeEpoch), true);
47
- }
48
- /** Reads a bounded durable recovery page without loading native results. */
49
- dispatchPage(query) {
50
- const { page, ready } = this.__transaction(() => {
51
- const page = scanDispatch(this.__database, query);
52
- const ready = page.data.filter((entry) => canReserveContinuation(this.__database, entry.taskId, entry.requestId));
53
- return { page, ready };
54
- }, true);
55
- if (ready.length > 0)
56
- this.__transaction(() => {
57
- for (const entry of ready)
58
- refreshWait(this.__database, entry.taskId, entry.requestId);
59
- });
60
- return page;
61
- }
62
- /** Resolves one dispatch intent against current membership, policy and native ownership. */
63
- dispatchJob(taskId, requestId) {
64
- return this.__transaction(() => readDispatchJob(this.__database, taskId, requestId, this.__runtimeEpoch), true);
65
- }
66
- /** Stores a visible blocked reason only while the selected pending intent still matches. */
67
- blockDispatch(requestId, selectedActionId, reason) {
68
- this.__transaction(() => this.__database
69
- .prepare('UPDATE hq_request SET dispatch_blocked_reason = ? WHERE id = ? AND (pending_action_id = ? OR current_action_id = ?) AND dispatch_blocked_reason IS NOT ?')
70
- .run(reason, requestId, selectedActionId, selectedActionId, reason));
71
- }
72
- /** Collects saved terminal evidence without asking an agent to publish or classify its report. */
73
- collectDispatch(taskId, requestId, actionId) {
74
- return this.__transaction(() => collectDispatch(this.__database, taskId, requestId, actionId, this.__runtimeEpoch));
75
- }
76
- /**
77
- * Applies one authorized metadata command, committing its result and delivery intent atomically.
78
- * @returns The selected record or original committed operation receipt.
79
- * @throws
80
- * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
81
- * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
82
- * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
83
- * - JG_AGENT_HQ_REQUEST_INVALID: The HQ request is invalid
84
- */
85
- handle(request, binding) {
86
- return this.__transaction(() => {
87
- const { actor, command } = request;
88
- if (binding !== undefined) {
89
- const native = readNativeAgent(this.__database, binding, this.__runtimeEpoch);
90
- if (actor.kind !== 'agent' || actor.agentId !== native.agentId)
91
- return collaborationForbidden();
92
- }
93
- if (command.kind === 'tasks-list')
94
- return listTasks(this.__database, actor, command);
95
- const taskId = command.kind === 'task-create' ? command.id : command.taskId;
96
- const context = command.kind === 'task-create' ? null : taskAccess(this.__database, actor, taskId);
97
- let invocation = null;
98
- if (actor.kind === 'agent' && context !== null && command.kind !== 'status') {
99
- invocation = readInvocation(this.__database, context);
100
- if (invocation !== null && !invocation.request.specification.effects.includes('read'))
101
- return collaborationForbidden();
102
- if (invocation !== null &&
103
- command.kind === 'message-publish' &&
104
- command.request !== null &&
105
- command.parentRequestId !== invocation.request.id)
106
- return collaborationForbidden();
107
- if (invocation !== null &&
108
- ['request-claim', 'request-resolve', 'request-reassign', 'request-cancel'].includes(command.kind))
109
- return collaborationForbidden();
110
- }
111
- if (command.kind === 'operation-read')
112
- return {
113
- kind: 'operation',
114
- operation: this.__readOperation(taskId, command.operationId),
115
- };
116
- if (!('id' in command)) {
117
- if (context === null)
118
- return collaborationForbidden();
119
- const result = command.kind === 'invocation-read'
120
- ? {
121
- kind: 'invocation',
122
- invocation: readInvocation(this.__database, context),
123
- }
124
- : command.kind === 'report-read'
125
- ? readReport(this.__database, context, command)
126
- : queryCollaboration(this.__database, context, command);
127
- assertCollaborationBudget(result);
128
- return result;
129
- }
130
- const digest = hashValue(JSON.stringify(request));
131
- const previous = this.__readOperationById(command.id);
132
- if (previous !== null) {
133
- taskAccess(this.__database, actor, taskId);
134
- if (previous.taskId !== taskId || previous.digest !== digest)
135
- return collaborationConflict();
136
- return { kind: 'operation', operation: previous };
137
- }
138
- let result;
139
- if (command.kind === 'task-create')
140
- result = createTask(this.__database, actor, command);
141
- else {
142
- if (context === null)
143
- return collaborationForbidden();
144
- switch (command.kind) {
145
- case 'task-policy-set':
146
- case 'task-plan-set':
147
- case 'task-state-set':
148
- case 'agent-add':
149
- case 'agent-policy-set':
150
- case 'agent-state-set':
151
- case 'agent-revoke':
152
- case 'grant-issue':
153
- case 'grant-revoke':
154
- result = mutateTask(this.__database, context, command);
155
- break;
156
- case 'stream-create':
157
- case 'stream-archive':
158
- case 'subscription-set':
159
- case 'subscription-advance':
160
- result = mutateStream(this.__database, context, command);
161
- break;
162
- case 'message-publish':
163
- result = publishMessage(this.__database, context, command, invocation);
164
- break;
165
- case 'message-ack':
166
- result = acknowledgeMessage(this.__database, context, command.messageId);
167
- break;
168
- case 'request-claim':
169
- case 'request-resolve':
170
- case 'request-cancel':
171
- case 'request-reassign':
172
- result = mutateRequest(this.__database, context, command);
173
- break;
174
- case 'request-dispatch':
175
- case 'request-input':
176
- case 'request-resume':
177
- case 'request-authorize':
178
- result = mutateCheckpoint(this.__database, context, command);
179
- break;
180
- case 'request-collect':
181
- requireHost(context);
182
- result = {
183
- kind: 'collection',
184
- collection: collectDispatch(this.__database, taskId, command.requestId, command.actionId, this.__runtimeEpoch),
185
- };
186
- break;
187
- case 'fanout-recover':
188
- requireHost(context);
189
- result = { kind: 'fanout', taskId, scheduled: true };
190
- break;
191
- }
192
- }
193
- const operation = {
194
- id: command.id,
195
- taskId,
196
- actor,
197
- commandKind: command.kind,
198
- digest,
199
- result,
200
- createdAt: Date.now(),
201
- };
202
- const response = CollaborationResponseSchema.parse({ kind: 'operation', operation });
203
- assertCollaborationBudget(response);
204
- this.__database
205
- .prepare('INSERT INTO hq_collaboration_operation (id, task_id, actor, command_kind, digest, result, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)')
206
- .run(operation.id, taskId, JSON.stringify(actor), command.kind, digest, JSON.stringify(result), operation.createdAt);
207
- return response;
208
- }, !('id' in request.command));
209
- }
210
- /**
211
- * Advances at most one subscriber batch without native calls or an in-memory queue.
212
- * @returns Whether a durable batch was processed.
213
- */
214
- recoverFanout() {
215
- // Idle startup must not contend with a host writer or delay maintenance admission.
216
- if (!this.__database.prepare('SELECT 1 FROM hq_message WHERE fanout_complete = 0 LIMIT 1').get())
217
- return false;
218
- return this.__transaction(() => fanoutBatch(this.__database));
219
- }
220
- __readOperation(taskId, id) {
221
- const operation = this.__readOperationById(id);
222
- if (operation !== null && operation.taskId !== taskId)
223
- return collaborationForbidden();
224
- return operation;
225
- }
226
- __readOperationById(id) {
227
- const row = this.__database
228
- .prepare('SELECT id, task_id, actor, command_kind, digest, result, created_at FROM hq_collaboration_operation WHERE id = ?')
229
- .get(id);
230
- if (!row)
231
- return null;
232
- return CollaborationOperationSchema.parse({
233
- id: row.id,
234
- taskId: row.task_id,
235
- actor: JSON.parse(z.string().parse(row.actor)),
236
- commandKind: row.command_kind,
237
- digest: row.digest,
238
- result: JSON.parse(z.string().parse(row.result)),
239
- createdAt: row.created_at,
240
- });
241
- }
242
- __transaction(operation, isReadOnly = false) {
243
- this.__database.exec(isReadOnly ? 'BEGIN' : 'BEGIN IMMEDIATE');
244
- try {
245
- const result = operation();
246
- this.__database.exec('COMMIT');
247
- return result;
248
- }
249
- catch (error) {
250
- this.__database.exec('ROLLBACK');
251
- throw error;
252
- }
253
- }
254
- }