@sanqianx/claude-ai-workbench 0.13.2 → 0.13.4

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 (122) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +156 -156
  3. package/apps/agent-terminal/app.css +652 -613
  4. package/apps/agent-terminal/app.js +143 -67
  5. package/apps/agent-terminal/conversation-composer.js +44 -0
  6. package/apps/agent-terminal/index.html +172 -170
  7. package/apps/agent-terminal/panel-host-proof.html +152 -152
  8. package/apps/agent-terminal/panel-proof-content.html +41 -41
  9. package/apps/agent-terminal/right-workspace-dock.css +189 -159
  10. package/apps/agent-terminal/right-workspace-dock.js +652 -521
  11. package/apps/agent-terminal/user-question-card.js +68 -0
  12. package/apps/ai-profile-settings-host/index.html +25 -25
  13. package/apps/ai-profile-settings-host/page.css +1 -1
  14. package/apps/ai-profile-settings-host/page.js +113 -113
  15. package/apps/minimal-test-host/README.md +15 -15
  16. package/apps/minimal-test-host/host.css +2 -2
  17. package/apps/minimal-test-host/host.js +35 -35
  18. package/apps/minimal-test-host/index.html +58 -58
  19. package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -15
  20. package/docs/sdk/FILE_LAYOUT.md +31 -31
  21. package/docs/sdk/INTEGRATION.md +126 -126
  22. package/docs/sdk/README.md +21 -21
  23. package/docs/sdk/SESSION_EVENTS.md +15 -13
  24. package/docs/sdk/openapi.yaml +16 -1
  25. package/package.json +1 -1
  26. package/packages/client/README.md +29 -29
  27. package/packages/client/index.d.ts +122 -122
  28. package/packages/client/index.js +260 -260
  29. package/packages/client/package.json +2 -2
  30. package/packages/contracts/README.md +9 -9
  31. package/packages/contracts/index.d.ts +519 -517
  32. package/packages/contracts/index.js +312 -312
  33. package/packages/contracts/package.json +1 -1
  34. package/packages/core/README.md +8 -8
  35. package/packages/core/index.d.ts +76 -76
  36. package/packages/core/index.js +222 -222
  37. package/packages/core/package.json +2 -2
  38. package/packages/server/README.md +17 -17
  39. package/packages/server/bin/agent-terminal-server.js +42 -42
  40. package/packages/server/bin/claude-workbench.js +225 -225
  41. package/packages/server/index.d.ts +171 -171
  42. package/packages/server/index.js +230 -230
  43. package/packages/server/lib/agent-discovery.js +133 -133
  44. package/packages/server/lib/agent-registry.js +143 -143
  45. package/packages/server/lib/agent-spawn-env.js +17 -17
  46. package/packages/server/lib/attachment-store.js +78 -78
  47. package/packages/server/lib/bridges/app-server-bridge.js +373 -373
  48. package/packages/server/lib/bridges/opencode-server-bridge.js +506 -506
  49. package/packages/server/lib/capture-settings-store.js +35 -35
  50. package/packages/server/lib/claude-history-recovery.js +87 -87
  51. package/packages/server/lib/cli-session-recovery.js +28 -28
  52. package/packages/server/lib/context-resolver.js +55 -55
  53. package/packages/server/lib/conversation-capture.js +51 -51
  54. package/packages/server/lib/delegation-phase-store.js +357 -357
  55. package/packages/server/lib/delegation-store.js +474 -468
  56. package/packages/server/lib/diagnostics.js +32 -32
  57. package/packages/server/lib/drivers/cli-json-runner.js +974 -974
  58. package/packages/server/lib/drivers/codex-driver.js +1461 -1442
  59. package/packages/server/lib/drivers/opencode-driver.js +1554 -1554
  60. package/packages/server/lib/drivers/zcode-driver.js +1250 -1250
  61. package/packages/server/lib/drivers/zcode-profile-runtime.js +71 -71
  62. package/packages/server/lib/event-stream.js +138 -138
  63. package/packages/server/lib/folder-picker.js +623 -623
  64. package/packages/server/lib/http-handler.js +339 -339
  65. package/packages/server/lib/json-file.js +26 -26
  66. package/packages/server/lib/knowledge-context.js +33 -33
  67. package/packages/server/lib/model-detection.js +295 -295
  68. package/packages/server/lib/parent-context.js +44 -0
  69. package/packages/server/lib/profile-store.js +283 -283
  70. package/packages/server/lib/project-store.js +102 -102
  71. package/packages/server/lib/runtime-loader.js +120 -120
  72. package/packages/server/lib/session-archive-store.js +53 -53
  73. package/packages/server/lib/session-history.js +612 -612
  74. package/packages/server/lib/system-doctor.js +125 -125
  75. package/packages/server/lib/ui-preferences-store.js +48 -48
  76. package/packages/server/lib/user-questions.js +67 -0
  77. package/packages/server/lib/workbench-service.js +1696 -1675
  78. package/packages/server/package.json +2 -2
  79. package/packages/ui/README.md +30 -30
  80. package/packages/ui/ai-profile-settings.d.ts +45 -45
  81. package/packages/ui/ai-profile-settings.js +283 -283
  82. package/packages/ui/claude-terminal.d.ts +31 -31
  83. package/packages/ui/claude-terminal.js +343 -343
  84. package/packages/ui/index.d.ts +18 -18
  85. package/packages/ui/index.js +51 -51
  86. package/packages/ui/package.json +2 -2
  87. package/packages/ui/theme.css +13 -13
  88. package/src/assets/claude-prompts.json +21 -21
  89. package/src/backend/http/ai-profile-routes.js +61 -61
  90. package/src/backend/http/claude-routes.js +170 -170
  91. package/src/backend/http/node-helpers.js +45 -45
  92. package/src/backend/index.js +21 -21
  93. package/src/backend/lib/ai-adapter.js +144 -144
  94. package/src/backend/lib/ai-profile-store.js +259 -259
  95. package/src/backend/lib/ai-vendor-presets.js +83 -83
  96. package/src/backend/lib/ai-workspace.js +140 -140
  97. package/src/backend/lib/automation-config.js +253 -253
  98. package/src/backend/lib/claude-cli-runner.js +2408 -2392
  99. package/src/backend/lib/data-dir.js +169 -169
  100. package/src/backend/lib/llm-client.js +176 -176
  101. package/src/backend/lib/model-context-windows.js +82 -82
  102. package/src/backend/lib/native-task-control.js +217 -217
  103. package/src/backend/lib/native-task-history-index.js +475 -475
  104. package/src/backend/lib/native-task-history-reader.js +526 -526
  105. package/src/backend/lib/native-task-messages.js +457 -457
  106. package/src/backend/lib/native-task-registry.js +325 -325
  107. package/src/backend/lib/native-task-runtime.js +242 -242
  108. package/src/backend/lib/native-task-trace.js +440 -440
  109. package/src/backend/lib/native-task-transcript.js +393 -393
  110. package/src/backend/lib/prompt-registry.js +166 -166
  111. package/src/backend/lib/runtime-config.js +121 -121
  112. package/src/backend/lib/sdk-session-stream.js +143 -143
  113. package/src/backend/lib/sdk-turn-stream.js +16 -16
  114. package/src/backend/lib/store-scope.js +131 -131
  115. package/src/backend/lib/task-identity.js +104 -104
  116. package/src/backend/lib/task-state-projection.js +58 -58
  117. package/src/index.js +7 -7
  118. package/src/sdk-wave-a.ts +1 -1
  119. package/src/sdk-wave-b.ts +1 -1
  120. package/src/sdk-wave-c.ts +1 -1
  121. package/src/sdk-wave-d.ts +1 -1
  122. package/src/sdk-wave-e.ts +1 -1
@@ -1,325 +1,325 @@
1
- 'use strict';
2
- // Native task registry - a pure in-memory index of native dispatch ownership
3
- // and real task status facts. No fs, no network, no SDK queries, no stop, no
4
- // event sequencing: runner/Service/persistent-history integration is a later
5
- // task and unit tests here are not a product gate.
6
- const { createTaskScope, createNativeTaskRef, nativeTaskKey, sameTaskScope } = require('./task-identity');
7
-
8
- const TASK_STATUSES = new Set(['pending', 'running', 'completed', 'failed', 'stopped']);
9
- const TERMINAL_STATUSES = new Set(['completed', 'failed', 'stopped']);
10
- const AGENT_TOOL_NAMES = new Set(['Agent', 'Task']);
11
- const LAUNCHER_TOOL_NAMES = new Set(['Agent', 'Task', 'Bash']);
12
- const TASK_KIND_BY_TYPE = new Map([['local_agent', 'agent'], ['local_bash', 'local_bash']]);
13
- const TASK_EVENT_SUBTYPES = new Set(['task_started', 'task_progress', 'task_updated', 'task_notification']);
14
- const MAX_DIAGNOSTICS = 200;
15
-
16
- function id(value) { return typeof value === 'string' && value.trim() !== '' ? value : null; }
17
- function ownerRunIdOf(sdkSessionId, parentNativeAgentId, launcherToolUseId) {
18
- return JSON.stringify(['sdk-launch', 1, sdkSessionId, parentNativeAgentId, launcherToolUseId]);
19
- }
20
- function tupleKey() { return JSON.stringify([...arguments]); }
21
-
22
- class NativeTaskRegistry {
23
- constructor(scopeInput) {
24
- const scope = createTaskScope(scopeInput);
25
- if (!scope) throw new Error('NativeTaskRegistry requires a valid fixed TaskScope');
26
- this._scope = Object.freeze(scope);
27
- this._tasks = new Map();
28
- this._byToolUse = new Map();
29
- this._byRefKey = new Map();
30
- this._rootLaunchers = new Map();
31
- this._nestedLaunchers = new Map();
32
- this._receipts = new Map();
33
- this._agents = new Map();
34
- this._diagnostics = [];
35
- this._diagSeen = new Set();
36
- }
37
-
38
- get scope() { return { storeScope: this._scope.storeScope, contextId: this._scope.contextId, parentSessionId: this._scope.parentSessionId }; }
39
- diagnostics() { return this._diagnostics.map(d => ({ ...d })); }
40
-
41
- // Raw SDK stream messages. Assistant messages only provide launcher facts
42
- // (real tool_use blocks); model prose never creates tasks or owners.
43
- observeSdkMessage(message) {
44
- if (!message || typeof message !== 'object') return null;
45
- if (message.type === 'assistant') return this._observeAssistant(message);
46
- if (message.type === 'system') return this._observeTaskSystem(message);
47
- return null;
48
- }
49
-
50
- _observeAssistant(message) {
51
- const sdkSessionId = id(message.session_id);
52
- if (!sdkSessionId) { this._diag('assistant-missing-session-id', {}); return null; }
53
- if (id(message.parent_tool_use_id)) return null;
54
- const raw = message.message && typeof message.message === 'object' ? message.message : message;
55
- const blocks = Array.isArray(raw.content) ? raw.content : [];
56
- let recorded = 0;
57
- for (const block of blocks) {
58
- if (!block || typeof block !== 'object' || block.type !== 'tool_use') continue;
59
- const toolUseId = id(block.id);
60
- if (!toolUseId) continue;
61
- if (LAUNCHER_TOOL_NAMES.has(block.name)) {
62
- const key = tupleKey(sdkSessionId, toolUseId);
63
- if (!this._rootLaunchers.has(key)) { this._rootLaunchers.set(key, { toolName: block.name, sdkSessionId, toolUseId }); recorded++; }
64
- }
65
- }
66
- if (recorded > 0) this._retryUnbound();
67
- return recorded;
68
- }
69
-
70
- // Normalized claude/native-trace observations (W1-08 shape).
71
- observeTrace(trace) {
72
- if (!trace || typeof trace !== 'object') return null;
73
- if (trace.type !== 'claude/native-trace' || trace.source !== 'sdk-hook') return null;
74
- const hookEvent = id(trace.hookEvent);
75
- const sdkSessionId = id(trace.sdkSessionId);
76
- const toolName = id(trace.toolName);
77
- if (!hookEvent || !sdkSessionId) { this._diag('trace-missing-identity', { hookEvent: hookEvent === null ? null : hookEvent.slice(0, 40) }); return null; }
78
- if (id(trace.parentSessionId) !== this._scope.parentSessionId) {
79
- this._diag('trace-parent-session-mismatch', { hookEvent, sdkSessionId, parentSessionId: id(trace.parentSessionId) });
80
- return null;
81
- }
82
- if (hookEvent === 'SubagentStart' || hookEvent === 'SubagentStop') { this._diag(hookEvent === 'SubagentStop' ? 'subagent-stop-not-terminal' : 'subagent-start-not-launch', { sdkSessionId }); return null; }
83
- const rawId = id(trace.rawToolUseId);
84
- const callbackId = id(trace.callbackToolUseId);
85
- const toolUseId = rawId || callbackId;
86
- if (!toolUseId) { this._diag('trace-missing-tool-use-id', { hookEvent, sdkSessionId }); return null; }
87
- if (rawId && callbackId && rawId !== callbackId) { this._diag('receipt-id-mismatch', { hookEvent, sdkSessionId, rawToolUseId: rawId, callbackToolUseId: callbackId }); return null; }
88
- const dispatcher = id(trace.nativeAgentId);
89
- if (hookEvent === 'PreToolUse') {
90
- if (!dispatcher) { this._diag('pre-hook-missing-agent-context', { sdkSessionId, toolUseId }); return null; }
91
- if (LAUNCHER_TOOL_NAMES.has(toolName)) {
92
- const key = tupleKey(sdkSessionId, dispatcher, toolUseId);
93
- if (!this._nestedLaunchers.has(key)) this._nestedLaunchers.set(key, { toolName, dispatcher, sdkSessionId, toolUseId });
94
- this._retryUnbound();
95
- }
96
- return toolUseId;
97
- }
98
- if (hookEvent === 'PostToolUseFailure') { this._diag('receipt-failure-kept', { sdkSessionId, toolUseId }); return null; }
99
- if (hookEvent !== 'PostToolUse') return null;
100
- const response = trace.toolResponse && typeof trace.toolResponse === 'object' ? trace.toolResponse : null;
101
- const agentId = response ? id(response.agentId) : null;
102
- if (!AGENT_TOOL_NAMES.has(toolName)) {
103
- if (agentId) this._diag('non-agent-receipt-ignored', { sdkSessionId, toolUseId, toolName });
104
- return null;
105
- }
106
- if (!agentId) { this._diag('receipt-missing-agent-id', { sdkSessionId, toolUseId, toolName }); return null; }
107
- this._recordReceipt(sdkSessionId, toolUseId, { agentId, dispatcher, toolName, sdkSessionId, toolUseId });
108
- this._retryUnbound();
109
- return toolUseId;
110
- }
111
-
112
- _observeTaskSystem(message) {
113
- const subtype = message.subtype;
114
- if (!TASK_EVENT_SUBTYPES.has(subtype)) return null;
115
- const taskId = id(message.task_id);
116
- const sdkSessionId = id(message.session_id);
117
- if (!taskId || !sdkSessionId) { this._diag('task-event-missing-identity', { subtype, hasTaskId: taskId !== null, hasSessionId: sdkSessionId !== null }); return null; }
118
- const task = this._ensureTask(sdkSessionId, taskId);
119
- task.observedEvents++;
120
- if (subtype === 'task_started') {
121
- task.startedSeen = true;
122
- const taskType = id(message.task_type);
123
- if (task.taskType === null) task.taskType = taskType;
124
- else if (taskType !== null && taskType !== task.taskType) {
125
- task.typeConflict = true;
126
- this._diag('task-type-conflict', { sdkSessionId, taskId, existing: task.taskType, incoming: taskType });
127
- }
128
- }
129
- const toolUseId = id(message.tool_use_id);
130
- if (toolUseId) {
131
- if (task.toolUseId === null) {
132
- task.toolUseId = toolUseId;
133
- const key = tupleKey(sdkSessionId, toolUseId);
134
- if (!this._byToolUse.has(key)) this._byToolUse.set(key, new Set());
135
- this._byToolUse.get(key).add(task.taskKey);
136
- } else if (task.toolUseId !== toolUseId) {
137
- this._diag('task-tooluse-conflict', { sdkSessionId, taskId, existing: task.toolUseId, incoming: toolUseId });
138
- }
139
- }
140
- this._applyStatus(task, message);
141
- this._tryBind(task);
142
- return task.taskKey;
143
- }
144
-
145
- _applyStatus(task, message) {
146
- let raw = null;
147
- if (message.subtype === 'task_notification') {
148
- raw = id(message.status);
149
- if (raw === null) { this._diag('notification-without-status', { sdkSessionId: task.sdkSessionId, taskId: task.taskId }); return; }
150
- } else {
151
- const patch = message.patch && typeof message.patch === 'object' ? message.patch : {};
152
- raw = id(message.status) || id(patch.status) || (message.subtype === 'task_started' ? 'running' : null);
153
- }
154
- if (raw === null) {
155
- if (message.subtype === 'task_progress' && task.status === null) { task.status = 'running'; task.statusSource = 'task_progress'; }
156
- return;
157
- }
158
- if (!TASK_STATUSES.has(raw)) { this._diag('unknown-task-status', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, subtype: message.subtype, raw: String(raw).slice(0, 40) }); return; }
159
- const prior = task.status;
160
- if (prior === null) { task.status = raw; task.statusSource = message.subtype; return; }
161
- if (TERMINAL_STATUSES.has(prior)) {
162
- if (!TERMINAL_STATUSES.has(raw)) { this._diag('terminal-no-revival', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, subtype: message.subtype }); return; }
163
- if (raw !== prior) { this._diag('terminal-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, prior, incoming: raw }); return; }
164
- return;
165
- }
166
- task.status = raw; task.statusSource = message.subtype;
167
- }
168
-
169
- _ensureTask(sdkSessionId, taskId) {
170
- const taskKey = JSON.stringify([sdkSessionId, taskId]);
171
- let task = this._tasks.get(taskKey);
172
- if (!task) {
173
- task = { taskKey, sdkSessionId, taskId, toolUseId: null, status: null, statusSource: null, taskKind: null, taskType: null, startedSeen: false, typeConflict: false, association: 'unassigned', owner: null, mainToolUseId: null, observedEvents: 0 };
174
- this._tasks.set(taskKey, task);
175
- }
176
- return task;
177
- }
178
-
179
- _recordReceipt(sdkSessionId, toolUseId, receipt) {
180
- const key = tupleKey(sdkSessionId, toolUseId);
181
- const claimants = this._byToolUse.get(key);
182
- if (claimants) {
183
- let matchesBound = false;
184
- let boundCount = 0;
185
- for (const taskKey of claimants) {
186
- const t = this._tasks.get(taskKey);
187
- if (t && t.owner) {
188
- boundCount++;
189
- if (t.owner.receiptAgentId === receipt.agentId && t.owner.parentNativeAgentId === receipt.dispatcher) matchesBound = true;
190
- }
191
- }
192
- if (boundCount > 0 && !matchesBound) this._diag('owner-conflict-kept-first', { sdkSessionId, toolUseId, agentId: receipt.agentId, dispatcher: receipt.dispatcher });
193
- }
194
- const list = this._receipts.get(key) || [];
195
- if (!list.some(r => r.agentId === receipt.agentId && r.dispatcher === receipt.dispatcher)) list.push(receipt);
196
- this._receipts.set(key, list);
197
- }
198
-
199
- _tryBind(task) {
200
- if (!task || task.owner !== null || task.toolUseId === null) return false;
201
- if (!task.startedSeen) return false;
202
- if (task.typeConflict) return false;
203
- const taskKind = TASK_KIND_BY_TYPE.get(task.taskType) || null;
204
- if (!taskKind) { this._diag('unknown-task-type', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, taskType: task.taskType }); return false; }
205
- const key = tupleKey(task.sdkSessionId, task.toolUseId);
206
- const root = this._rootLaunchers.get(key);
207
- const matches = (this._receipts.get(key) || []).filter(r => r.agentId === task.taskId);
208
- if (matches.length > 1) { this._diag('ambiguous-receipt', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId }); return false; }
209
- if (root && AGENT_TOOL_NAMES.has(root.toolName)) {
210
- if (taskKind !== 'agent') { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: root.toolName, taskType: task.taskType }); return false; }
211
- if (matches.length === 1 && matches[0].dispatcher === null) return this._bind(task, { launcherKind: 'root', parentNativeAgentId: null, taskKind: 'agent', receipt: matches[0] });
212
- return false;
213
- }
214
- if (matches.length === 1 && matches[0].dispatcher !== null) {
215
- const receipt = matches[0];
216
- const parentTaskKey = this._agents.get(tupleKey(task.sdkSessionId, receipt.dispatcher));
217
- if (!parentTaskKey) { this._diag('unknown-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: receipt.dispatcher }); return false; }
218
- const nested = this._nestedLaunchers.get(tupleKey(task.sdkSessionId, receipt.dispatcher, task.toolUseId));
219
- if (!nested) { this._diag('receipt-without-launcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: receipt.dispatcher }); return false; }
220
- const nestedKind = AGENT_TOOL_NAMES.has(nested.toolName) ? 'agent' : (nested.toolName === 'Bash' ? 'local_bash' : null);
221
- if (taskKind !== nestedKind || nestedKind === null) { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: nested.toolName, taskType: task.taskType }); return false; }
222
- return this._bind(task, { launcherKind: 'nested', parentNativeAgentId: receipt.dispatcher, taskKind: nestedKind, receipt, parentTaskKey });
223
- }
224
- if (matches.length === 0) {
225
- const candidates = [];
226
- for (const entry of this._nestedLaunchers.values()) {
227
- if (entry.sdkSessionId === task.sdkSessionId && entry.toolUseId === task.toolUseId && !candidates.some(c => c.dispatcher === entry.dispatcher)) candidates.push(entry);
228
- }
229
- if (candidates.length > 1) { this._diag('ambiguous-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId }); return false; }
230
- if (candidates.length === 1) {
231
- const candidate = candidates[0];
232
- const candidateKind = AGENT_TOOL_NAMES.has(candidate.toolName) ? 'agent' : (candidate.toolName === 'Bash' ? 'local_bash' : null);
233
- if (taskKind !== candidateKind || candidateKind === null) { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: candidate.toolName, taskType: task.taskType }); return false; }
234
- if (candidateKind === 'agent') return false;
235
- const parentTaskKey = this._agents.get(tupleKey(task.sdkSessionId, candidate.dispatcher));
236
- if (!parentTaskKey) { this._diag('unknown-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: candidate.dispatcher }); return false; }
237
- return this._bind(task, { launcherKind: 'nested', parentNativeAgentId: candidate.dispatcher, taskKind: 'local_bash', receipt: null, parentTaskKey });
238
- }
239
- }
240
- if (root && root.toolName === 'Bash') {
241
- if (taskKind !== 'local_bash') { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: root.toolName, taskType: task.taskType }); return false; }
242
- if (matches.length === 0) { task.association = 'main-command'; task.mainToolUseId = task.toolUseId; }
243
- return false;
244
- }
245
- return false;
246
- }
247
- _bind(task, opts) {
248
- const ownerRunId = ownerRunIdOf(task.sdkSessionId, opts.parentNativeAgentId, task.toolUseId);
249
- const taskRef = createNativeTaskRef({ kind: 'native-task', storeScope: this._scope.storeScope, contextId: this._scope.contextId, parentSessionId: this._scope.parentSessionId, ownerRunId, taskId: task.taskId });
250
- if (!taskRef) { this._diag('taskref-invalid', { sdkSessionId: task.sdkSessionId, taskId: task.taskId }); return false; }
251
- const refKey = nativeTaskKey(taskRef);
252
- if (this._byRefKey.has(refKey)) { this._diag('owner-ref-collision', { refKey, taskId: task.taskId }); return false; }
253
- const parent = opts.parentTaskKey ? this._tasks.get(opts.parentTaskKey) : null;
254
- const owner = { taskRef, ownerRunId, ownerBasis: 'sdk-tool-launch', launcherKind: opts.launcherKind, parentNativeAgentId: opts.parentNativeAgentId, launcherToolUseId: task.toolUseId, receiptAgentId: opts.receipt ? opts.receipt.agentId : null };
255
- if (parent) owner.resolvedParent = { taskId: parent.taskId, sdkSessionId: parent.sdkSessionId, agentId: opts.receipt ? opts.receipt.dispatcher : opts.parentNativeAgentId, status: parent.status };
256
- task.owner = owner;
257
- task.taskKind = opts.taskKind;
258
- task.association = 'bound';
259
- this._byRefKey.set(refKey, task.taskKey);
260
- if (opts.taskKind === 'agent' && opts.receipt) this._agents.set(tupleKey(task.sdkSessionId, opts.receipt.agentId), task.taskKey);
261
- this._retryUnbound(task.taskKey);
262
- return true;
263
- }
264
-
265
- _retryUnbound(exceptTaskKey) {
266
- for (const task of this._tasks.values()) {
267
- if (task.owner !== null || task.toolUseId === null) continue;
268
- if (exceptTaskKey && task.taskKey === exceptTaskKey) continue;
269
- this._tryBind(task);
270
- }
271
- }
272
-
273
- list() { return [...this._tasks.values()].map(task => this._snapshot(task)); }
274
-
275
- // Narrow trusted read-only binding query for the transcript collector:
276
- // only bound agent tasks whose identity the registry itself established
277
- // (SDK session id from the stream, receipt agent id from a real hook
278
- // receipt). Fresh copies only; no status is interpreted here.
279
- agentBindings() {
280
- const out = [];
281
- for (const task of this._tasks.values()) {
282
- if (task.association !== 'bound' || task.taskKind !== 'agent') continue;
283
- if (!task.owner || !task.owner.receiptAgentId || task.owner.receiptAgentId !== task.taskId) continue;
284
- if (typeof task.sdkSessionId !== 'string' || !task.sdkSessionId || typeof task.taskId !== 'string' || !task.taskId) continue;
285
- out.push({
286
- sdkSessionId: task.sdkSessionId,
287
- taskId: task.taskId,
288
- taskRef: task.owner.taskRef ? JSON.parse(JSON.stringify(task.owner.taskRef)) : null,
289
- ownerRunId: task.owner.ownerRunId || null,
290
- status: task.status === undefined ? null : task.status,
291
- });
292
- }
293
- return out;
294
- }
295
-
296
- getTask(ref) {
297
- const taskRef = createNativeTaskRef(ref);
298
- if (!taskRef) return null;
299
- if (!sameTaskScope(taskRef, this._scope)) return null;
300
- const taskKey = this._byRefKey.get(nativeTaskKey(taskRef));
301
- if (!taskKey) return null;
302
- const task = this._tasks.get(taskKey);
303
- return task ? this._snapshot(task) : null;
304
- }
305
-
306
- _snapshot(task) {
307
- return JSON.parse(JSON.stringify({
308
- sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId,
309
- status: task.status, statusSource: task.statusSource, taskKind: task.taskKind, taskType: task.taskType, startedSeen: task.startedSeen, typeConflict: task.typeConflict === true, association: task.association,
310
- taskRef: task.owner ? task.owner.taskRef : null, ownerRunId: task.owner ? task.owner.ownerRunId : null,
311
- owner: task.owner ? { ownerBasis: task.owner.ownerBasis, launcherKind: task.owner.launcherKind, parentNativeAgentId: task.owner.parentNativeAgentId, launcherToolUseId: task.owner.launcherToolUseId, receiptAgentId: task.owner.receiptAgentId, resolvedParent: task.owner.resolvedParent || null } : null,
312
- mainToolUseId: task.mainToolUseId, observedEvents: task.observedEvents,
313
- }));
314
- }
315
-
316
- _diag(code, ids) {
317
- const signature = code + '|' + JSON.stringify(ids || {});
318
- if (this._diagSeen.has(signature)) return;
319
- if (this._diagnostics.length >= MAX_DIAGNOSTICS) return;
320
- this._diagSeen.add(signature);
321
- this._diagnostics.push(Object.assign({ code }, ids || {}));
322
- }
323
- }
324
-
325
- module.exports = { NativeTaskRegistry };
1
+ 'use strict';
2
+ // Native task registry - a pure in-memory index of native dispatch ownership
3
+ // and real task status facts. No fs, no network, no SDK queries, no stop, no
4
+ // event sequencing: runner/Service/persistent-history integration is a later
5
+ // task and unit tests here are not a product gate.
6
+ const { createTaskScope, createNativeTaskRef, nativeTaskKey, sameTaskScope } = require('./task-identity');
7
+
8
+ const TASK_STATUSES = new Set(['pending', 'running', 'completed', 'failed', 'stopped']);
9
+ const TERMINAL_STATUSES = new Set(['completed', 'failed', 'stopped']);
10
+ const AGENT_TOOL_NAMES = new Set(['Agent', 'Task']);
11
+ const LAUNCHER_TOOL_NAMES = new Set(['Agent', 'Task', 'Bash']);
12
+ const TASK_KIND_BY_TYPE = new Map([['local_agent', 'agent'], ['local_bash', 'local_bash']]);
13
+ const TASK_EVENT_SUBTYPES = new Set(['task_started', 'task_progress', 'task_updated', 'task_notification']);
14
+ const MAX_DIAGNOSTICS = 200;
15
+
16
+ function id(value) { return typeof value === 'string' && value.trim() !== '' ? value : null; }
17
+ function ownerRunIdOf(sdkSessionId, parentNativeAgentId, launcherToolUseId) {
18
+ return JSON.stringify(['sdk-launch', 1, sdkSessionId, parentNativeAgentId, launcherToolUseId]);
19
+ }
20
+ function tupleKey() { return JSON.stringify([...arguments]); }
21
+
22
+ class NativeTaskRegistry {
23
+ constructor(scopeInput) {
24
+ const scope = createTaskScope(scopeInput);
25
+ if (!scope) throw new Error('NativeTaskRegistry requires a valid fixed TaskScope');
26
+ this._scope = Object.freeze(scope);
27
+ this._tasks = new Map();
28
+ this._byToolUse = new Map();
29
+ this._byRefKey = new Map();
30
+ this._rootLaunchers = new Map();
31
+ this._nestedLaunchers = new Map();
32
+ this._receipts = new Map();
33
+ this._agents = new Map();
34
+ this._diagnostics = [];
35
+ this._diagSeen = new Set();
36
+ }
37
+
38
+ get scope() { return { storeScope: this._scope.storeScope, contextId: this._scope.contextId, parentSessionId: this._scope.parentSessionId }; }
39
+ diagnostics() { return this._diagnostics.map(d => ({ ...d })); }
40
+
41
+ // Raw SDK stream messages. Assistant messages only provide launcher facts
42
+ // (real tool_use blocks); model prose never creates tasks or owners.
43
+ observeSdkMessage(message) {
44
+ if (!message || typeof message !== 'object') return null;
45
+ if (message.type === 'assistant') return this._observeAssistant(message);
46
+ if (message.type === 'system') return this._observeTaskSystem(message);
47
+ return null;
48
+ }
49
+
50
+ _observeAssistant(message) {
51
+ const sdkSessionId = id(message.session_id);
52
+ if (!sdkSessionId) { this._diag('assistant-missing-session-id', {}); return null; }
53
+ if (id(message.parent_tool_use_id)) return null;
54
+ const raw = message.message && typeof message.message === 'object' ? message.message : message;
55
+ const blocks = Array.isArray(raw.content) ? raw.content : [];
56
+ let recorded = 0;
57
+ for (const block of blocks) {
58
+ if (!block || typeof block !== 'object' || block.type !== 'tool_use') continue;
59
+ const toolUseId = id(block.id);
60
+ if (!toolUseId) continue;
61
+ if (LAUNCHER_TOOL_NAMES.has(block.name)) {
62
+ const key = tupleKey(sdkSessionId, toolUseId);
63
+ if (!this._rootLaunchers.has(key)) { this._rootLaunchers.set(key, { toolName: block.name, sdkSessionId, toolUseId }); recorded++; }
64
+ }
65
+ }
66
+ if (recorded > 0) this._retryUnbound();
67
+ return recorded;
68
+ }
69
+
70
+ // Normalized claude/native-trace observations (W1-08 shape).
71
+ observeTrace(trace) {
72
+ if (!trace || typeof trace !== 'object') return null;
73
+ if (trace.type !== 'claude/native-trace' || trace.source !== 'sdk-hook') return null;
74
+ const hookEvent = id(trace.hookEvent);
75
+ const sdkSessionId = id(trace.sdkSessionId);
76
+ const toolName = id(trace.toolName);
77
+ if (!hookEvent || !sdkSessionId) { this._diag('trace-missing-identity', { hookEvent: hookEvent === null ? null : hookEvent.slice(0, 40) }); return null; }
78
+ if (id(trace.parentSessionId) !== this._scope.parentSessionId) {
79
+ this._diag('trace-parent-session-mismatch', { hookEvent, sdkSessionId, parentSessionId: id(trace.parentSessionId) });
80
+ return null;
81
+ }
82
+ if (hookEvent === 'SubagentStart' || hookEvent === 'SubagentStop') { this._diag(hookEvent === 'SubagentStop' ? 'subagent-stop-not-terminal' : 'subagent-start-not-launch', { sdkSessionId }); return null; }
83
+ const rawId = id(trace.rawToolUseId);
84
+ const callbackId = id(trace.callbackToolUseId);
85
+ const toolUseId = rawId || callbackId;
86
+ if (!toolUseId) { this._diag('trace-missing-tool-use-id', { hookEvent, sdkSessionId }); return null; }
87
+ if (rawId && callbackId && rawId !== callbackId) { this._diag('receipt-id-mismatch', { hookEvent, sdkSessionId, rawToolUseId: rawId, callbackToolUseId: callbackId }); return null; }
88
+ const dispatcher = id(trace.nativeAgentId);
89
+ if (hookEvent === 'PreToolUse') {
90
+ if (!dispatcher) { this._diag('pre-hook-missing-agent-context', { sdkSessionId, toolUseId }); return null; }
91
+ if (LAUNCHER_TOOL_NAMES.has(toolName)) {
92
+ const key = tupleKey(sdkSessionId, dispatcher, toolUseId);
93
+ if (!this._nestedLaunchers.has(key)) this._nestedLaunchers.set(key, { toolName, dispatcher, sdkSessionId, toolUseId });
94
+ this._retryUnbound();
95
+ }
96
+ return toolUseId;
97
+ }
98
+ if (hookEvent === 'PostToolUseFailure') { this._diag('receipt-failure-kept', { sdkSessionId, toolUseId }); return null; }
99
+ if (hookEvent !== 'PostToolUse') return null;
100
+ const response = trace.toolResponse && typeof trace.toolResponse === 'object' ? trace.toolResponse : null;
101
+ const agentId = response ? id(response.agentId) : null;
102
+ if (!AGENT_TOOL_NAMES.has(toolName)) {
103
+ if (agentId) this._diag('non-agent-receipt-ignored', { sdkSessionId, toolUseId, toolName });
104
+ return null;
105
+ }
106
+ if (!agentId) { this._diag('receipt-missing-agent-id', { sdkSessionId, toolUseId, toolName }); return null; }
107
+ this._recordReceipt(sdkSessionId, toolUseId, { agentId, dispatcher, toolName, sdkSessionId, toolUseId });
108
+ this._retryUnbound();
109
+ return toolUseId;
110
+ }
111
+
112
+ _observeTaskSystem(message) {
113
+ const subtype = message.subtype;
114
+ if (!TASK_EVENT_SUBTYPES.has(subtype)) return null;
115
+ const taskId = id(message.task_id);
116
+ const sdkSessionId = id(message.session_id);
117
+ if (!taskId || !sdkSessionId) { this._diag('task-event-missing-identity', { subtype, hasTaskId: taskId !== null, hasSessionId: sdkSessionId !== null }); return null; }
118
+ const task = this._ensureTask(sdkSessionId, taskId);
119
+ task.observedEvents++;
120
+ if (subtype === 'task_started') {
121
+ task.startedSeen = true;
122
+ const taskType = id(message.task_type);
123
+ if (task.taskType === null) task.taskType = taskType;
124
+ else if (taskType !== null && taskType !== task.taskType) {
125
+ task.typeConflict = true;
126
+ this._diag('task-type-conflict', { sdkSessionId, taskId, existing: task.taskType, incoming: taskType });
127
+ }
128
+ }
129
+ const toolUseId = id(message.tool_use_id);
130
+ if (toolUseId) {
131
+ if (task.toolUseId === null) {
132
+ task.toolUseId = toolUseId;
133
+ const key = tupleKey(sdkSessionId, toolUseId);
134
+ if (!this._byToolUse.has(key)) this._byToolUse.set(key, new Set());
135
+ this._byToolUse.get(key).add(task.taskKey);
136
+ } else if (task.toolUseId !== toolUseId) {
137
+ this._diag('task-tooluse-conflict', { sdkSessionId, taskId, existing: task.toolUseId, incoming: toolUseId });
138
+ }
139
+ }
140
+ this._applyStatus(task, message);
141
+ this._tryBind(task);
142
+ return task.taskKey;
143
+ }
144
+
145
+ _applyStatus(task, message) {
146
+ let raw = null;
147
+ if (message.subtype === 'task_notification') {
148
+ raw = id(message.status);
149
+ if (raw === null) { this._diag('notification-without-status', { sdkSessionId: task.sdkSessionId, taskId: task.taskId }); return; }
150
+ } else {
151
+ const patch = message.patch && typeof message.patch === 'object' ? message.patch : {};
152
+ raw = id(message.status) || id(patch.status) || (message.subtype === 'task_started' ? 'running' : null);
153
+ }
154
+ if (raw === null) {
155
+ if (message.subtype === 'task_progress' && task.status === null) { task.status = 'running'; task.statusSource = 'task_progress'; }
156
+ return;
157
+ }
158
+ if (!TASK_STATUSES.has(raw)) { this._diag('unknown-task-status', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, subtype: message.subtype, raw: String(raw).slice(0, 40) }); return; }
159
+ const prior = task.status;
160
+ if (prior === null) { task.status = raw; task.statusSource = message.subtype; return; }
161
+ if (TERMINAL_STATUSES.has(prior)) {
162
+ if (!TERMINAL_STATUSES.has(raw)) { this._diag('terminal-no-revival', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, subtype: message.subtype }); return; }
163
+ if (raw !== prior) { this._diag('terminal-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, prior, incoming: raw }); return; }
164
+ return;
165
+ }
166
+ task.status = raw; task.statusSource = message.subtype;
167
+ }
168
+
169
+ _ensureTask(sdkSessionId, taskId) {
170
+ const taskKey = JSON.stringify([sdkSessionId, taskId]);
171
+ let task = this._tasks.get(taskKey);
172
+ if (!task) {
173
+ task = { taskKey, sdkSessionId, taskId, toolUseId: null, status: null, statusSource: null, taskKind: null, taskType: null, startedSeen: false, typeConflict: false, association: 'unassigned', owner: null, mainToolUseId: null, observedEvents: 0 };
174
+ this._tasks.set(taskKey, task);
175
+ }
176
+ return task;
177
+ }
178
+
179
+ _recordReceipt(sdkSessionId, toolUseId, receipt) {
180
+ const key = tupleKey(sdkSessionId, toolUseId);
181
+ const claimants = this._byToolUse.get(key);
182
+ if (claimants) {
183
+ let matchesBound = false;
184
+ let boundCount = 0;
185
+ for (const taskKey of claimants) {
186
+ const t = this._tasks.get(taskKey);
187
+ if (t && t.owner) {
188
+ boundCount++;
189
+ if (t.owner.receiptAgentId === receipt.agentId && t.owner.parentNativeAgentId === receipt.dispatcher) matchesBound = true;
190
+ }
191
+ }
192
+ if (boundCount > 0 && !matchesBound) this._diag('owner-conflict-kept-first', { sdkSessionId, toolUseId, agentId: receipt.agentId, dispatcher: receipt.dispatcher });
193
+ }
194
+ const list = this._receipts.get(key) || [];
195
+ if (!list.some(r => r.agentId === receipt.agentId && r.dispatcher === receipt.dispatcher)) list.push(receipt);
196
+ this._receipts.set(key, list);
197
+ }
198
+
199
+ _tryBind(task) {
200
+ if (!task || task.owner !== null || task.toolUseId === null) return false;
201
+ if (!task.startedSeen) return false;
202
+ if (task.typeConflict) return false;
203
+ const taskKind = TASK_KIND_BY_TYPE.get(task.taskType) || null;
204
+ if (!taskKind) { this._diag('unknown-task-type', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, taskType: task.taskType }); return false; }
205
+ const key = tupleKey(task.sdkSessionId, task.toolUseId);
206
+ const root = this._rootLaunchers.get(key);
207
+ const matches = (this._receipts.get(key) || []).filter(r => r.agentId === task.taskId);
208
+ if (matches.length > 1) { this._diag('ambiguous-receipt', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId }); return false; }
209
+ if (root && AGENT_TOOL_NAMES.has(root.toolName)) {
210
+ if (taskKind !== 'agent') { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: root.toolName, taskType: task.taskType }); return false; }
211
+ if (matches.length === 1 && matches[0].dispatcher === null) return this._bind(task, { launcherKind: 'root', parentNativeAgentId: null, taskKind: 'agent', receipt: matches[0] });
212
+ return false;
213
+ }
214
+ if (matches.length === 1 && matches[0].dispatcher !== null) {
215
+ const receipt = matches[0];
216
+ const parentTaskKey = this._agents.get(tupleKey(task.sdkSessionId, receipt.dispatcher));
217
+ if (!parentTaskKey) { this._diag('unknown-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: receipt.dispatcher }); return false; }
218
+ const nested = this._nestedLaunchers.get(tupleKey(task.sdkSessionId, receipt.dispatcher, task.toolUseId));
219
+ if (!nested) { this._diag('receipt-without-launcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: receipt.dispatcher }); return false; }
220
+ const nestedKind = AGENT_TOOL_NAMES.has(nested.toolName) ? 'agent' : (nested.toolName === 'Bash' ? 'local_bash' : null);
221
+ if (taskKind !== nestedKind || nestedKind === null) { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: nested.toolName, taskType: task.taskType }); return false; }
222
+ return this._bind(task, { launcherKind: 'nested', parentNativeAgentId: receipt.dispatcher, taskKind: nestedKind, receipt, parentTaskKey });
223
+ }
224
+ if (matches.length === 0) {
225
+ const candidates = [];
226
+ for (const entry of this._nestedLaunchers.values()) {
227
+ if (entry.sdkSessionId === task.sdkSessionId && entry.toolUseId === task.toolUseId && !candidates.some(c => c.dispatcher === entry.dispatcher)) candidates.push(entry);
228
+ }
229
+ if (candidates.length > 1) { this._diag('ambiguous-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId }); return false; }
230
+ if (candidates.length === 1) {
231
+ const candidate = candidates[0];
232
+ const candidateKind = AGENT_TOOL_NAMES.has(candidate.toolName) ? 'agent' : (candidate.toolName === 'Bash' ? 'local_bash' : null);
233
+ if (taskKind !== candidateKind || candidateKind === null) { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: candidate.toolName, taskType: task.taskType }); return false; }
234
+ if (candidateKind === 'agent') return false;
235
+ const parentTaskKey = this._agents.get(tupleKey(task.sdkSessionId, candidate.dispatcher));
236
+ if (!parentTaskKey) { this._diag('unknown-dispatcher', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, dispatcher: candidate.dispatcher }); return false; }
237
+ return this._bind(task, { launcherKind: 'nested', parentNativeAgentId: candidate.dispatcher, taskKind: 'local_bash', receipt: null, parentTaskKey });
238
+ }
239
+ }
240
+ if (root && root.toolName === 'Bash') {
241
+ if (taskKind !== 'local_bash') { this._diag('task-type-conflict', { sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId, launcherTool: root.toolName, taskType: task.taskType }); return false; }
242
+ if (matches.length === 0) { task.association = 'main-command'; task.mainToolUseId = task.toolUseId; }
243
+ return false;
244
+ }
245
+ return false;
246
+ }
247
+ _bind(task, opts) {
248
+ const ownerRunId = ownerRunIdOf(task.sdkSessionId, opts.parentNativeAgentId, task.toolUseId);
249
+ const taskRef = createNativeTaskRef({ kind: 'native-task', storeScope: this._scope.storeScope, contextId: this._scope.contextId, parentSessionId: this._scope.parentSessionId, ownerRunId, taskId: task.taskId });
250
+ if (!taskRef) { this._diag('taskref-invalid', { sdkSessionId: task.sdkSessionId, taskId: task.taskId }); return false; }
251
+ const refKey = nativeTaskKey(taskRef);
252
+ if (this._byRefKey.has(refKey)) { this._diag('owner-ref-collision', { refKey, taskId: task.taskId }); return false; }
253
+ const parent = opts.parentTaskKey ? this._tasks.get(opts.parentTaskKey) : null;
254
+ const owner = { taskRef, ownerRunId, ownerBasis: 'sdk-tool-launch', launcherKind: opts.launcherKind, parentNativeAgentId: opts.parentNativeAgentId, launcherToolUseId: task.toolUseId, receiptAgentId: opts.receipt ? opts.receipt.agentId : null };
255
+ if (parent) owner.resolvedParent = { taskId: parent.taskId, sdkSessionId: parent.sdkSessionId, agentId: opts.receipt ? opts.receipt.dispatcher : opts.parentNativeAgentId, status: parent.status };
256
+ task.owner = owner;
257
+ task.taskKind = opts.taskKind;
258
+ task.association = 'bound';
259
+ this._byRefKey.set(refKey, task.taskKey);
260
+ if (opts.taskKind === 'agent' && opts.receipt) this._agents.set(tupleKey(task.sdkSessionId, opts.receipt.agentId), task.taskKey);
261
+ this._retryUnbound(task.taskKey);
262
+ return true;
263
+ }
264
+
265
+ _retryUnbound(exceptTaskKey) {
266
+ for (const task of this._tasks.values()) {
267
+ if (task.owner !== null || task.toolUseId === null) continue;
268
+ if (exceptTaskKey && task.taskKey === exceptTaskKey) continue;
269
+ this._tryBind(task);
270
+ }
271
+ }
272
+
273
+ list() { return [...this._tasks.values()].map(task => this._snapshot(task)); }
274
+
275
+ // Narrow trusted read-only binding query for the transcript collector:
276
+ // only bound agent tasks whose identity the registry itself established
277
+ // (SDK session id from the stream, receipt agent id from a real hook
278
+ // receipt). Fresh copies only; no status is interpreted here.
279
+ agentBindings() {
280
+ const out = [];
281
+ for (const task of this._tasks.values()) {
282
+ if (task.association !== 'bound' || task.taskKind !== 'agent') continue;
283
+ if (!task.owner || !task.owner.receiptAgentId || task.owner.receiptAgentId !== task.taskId) continue;
284
+ if (typeof task.sdkSessionId !== 'string' || !task.sdkSessionId || typeof task.taskId !== 'string' || !task.taskId) continue;
285
+ out.push({
286
+ sdkSessionId: task.sdkSessionId,
287
+ taskId: task.taskId,
288
+ taskRef: task.owner.taskRef ? JSON.parse(JSON.stringify(task.owner.taskRef)) : null,
289
+ ownerRunId: task.owner.ownerRunId || null,
290
+ status: task.status === undefined ? null : task.status,
291
+ });
292
+ }
293
+ return out;
294
+ }
295
+
296
+ getTask(ref) {
297
+ const taskRef = createNativeTaskRef(ref);
298
+ if (!taskRef) return null;
299
+ if (!sameTaskScope(taskRef, this._scope)) return null;
300
+ const taskKey = this._byRefKey.get(nativeTaskKey(taskRef));
301
+ if (!taskKey) return null;
302
+ const task = this._tasks.get(taskKey);
303
+ return task ? this._snapshot(task) : null;
304
+ }
305
+
306
+ _snapshot(task) {
307
+ return JSON.parse(JSON.stringify({
308
+ sdkSessionId: task.sdkSessionId, taskId: task.taskId, toolUseId: task.toolUseId,
309
+ status: task.status, statusSource: task.statusSource, taskKind: task.taskKind, taskType: task.taskType, startedSeen: task.startedSeen, typeConflict: task.typeConflict === true, association: task.association,
310
+ taskRef: task.owner ? task.owner.taskRef : null, ownerRunId: task.owner ? task.owner.ownerRunId : null,
311
+ owner: task.owner ? { ownerBasis: task.owner.ownerBasis, launcherKind: task.owner.launcherKind, parentNativeAgentId: task.owner.parentNativeAgentId, launcherToolUseId: task.owner.launcherToolUseId, receiptAgentId: task.owner.receiptAgentId, resolvedParent: task.owner.resolvedParent || null } : null,
312
+ mainToolUseId: task.mainToolUseId, observedEvents: task.observedEvents,
313
+ }));
314
+ }
315
+
316
+ _diag(code, ids) {
317
+ const signature = code + '|' + JSON.stringify(ids || {});
318
+ if (this._diagSeen.has(signature)) return;
319
+ if (this._diagnostics.length >= MAX_DIAGNOSTICS) return;
320
+ this._diagSeen.add(signature);
321
+ this._diagnostics.push(Object.assign({ code }, ids || {}));
322
+ }
323
+ }
324
+
325
+ module.exports = { NativeTaskRegistry };