@xioflow/kernel 0.4.0 → 0.5.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 (111) hide show
  1. package/README.md +95 -3
  2. package/dist/cli.d.ts +3 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +94 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/driver/node-driver.d.ts.map +1 -1
  7. package/dist/driver/node-driver.js +1 -30
  8. package/dist/driver/node-driver.js.map +1 -1
  9. package/dist/driver/reaper-driver.d.ts +62 -0
  10. package/dist/driver/reaper-driver.d.ts.map +1 -0
  11. package/dist/driver/reaper-driver.js +371 -0
  12. package/dist/driver/reaper-driver.js.map +1 -0
  13. package/dist/driver/spawn-support.d.ts +4 -0
  14. package/dist/driver/spawn-support.d.ts.map +1 -0
  15. package/dist/driver/spawn-support.js +31 -0
  16. package/dist/driver/spawn-support.js.map +1 -0
  17. package/dist/driver/types.d.ts +6 -2
  18. package/dist/driver/types.d.ts.map +1 -1
  19. package/dist/fault/crashpoint.d.ts +8 -0
  20. package/dist/fault/crashpoint.d.ts.map +1 -0
  21. package/dist/fault/crashpoint.js +37 -0
  22. package/dist/fault/crashpoint.js.map +1 -0
  23. package/dist/index.d.ts +6 -0
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +3 -0
  26. package/dist/index.js.map +1 -1
  27. package/dist/mcp/server.d.ts +49 -0
  28. package/dist/mcp/server.d.ts.map +1 -0
  29. package/dist/mcp/server.js +233 -0
  30. package/dist/mcp/server.js.map +1 -0
  31. package/dist/mcp/tools.d.ts +32 -0
  32. package/dist/mcp/tools.d.ts.map +1 -0
  33. package/dist/mcp/tools.js +255 -0
  34. package/dist/mcp/tools.js.map +1 -0
  35. package/dist/native/darwin-arm64/xioflow-reaper +0 -0
  36. package/dist/native/darwin-x64/xioflow-reaper +0 -0
  37. package/dist/native/linux-arm64/xioflow-reaper +0 -0
  38. package/dist/native/linux-x64/xioflow-reaper +0 -0
  39. package/dist/otel/otlp.d.ts +37 -0
  40. package/dist/otel/otlp.d.ts.map +1 -0
  41. package/dist/otel/otlp.js +224 -0
  42. package/dist/otel/otlp.js.map +1 -0
  43. package/dist/recovery/engine.d.ts.map +1 -1
  44. package/dist/recovery/engine.js +2 -0
  45. package/dist/recovery/engine.js.map +1 -1
  46. package/dist/snapshot/git-shadow.d.ts +8 -0
  47. package/dist/snapshot/git-shadow.d.ts.map +1 -1
  48. package/dist/snapshot/git-shadow.js +24 -19
  49. package/dist/snapshot/git-shadow.js.map +1 -1
  50. package/dist/store/sqlite.d.ts.map +1 -1
  51. package/dist/store/sqlite.js +5 -0
  52. package/dist/store/sqlite.js.map +1 -1
  53. package/dist/supervisor/admission.d.ts +26 -0
  54. package/dist/supervisor/admission.d.ts.map +1 -0
  55. package/dist/supervisor/admission.js +98 -0
  56. package/dist/supervisor/admission.js.map +1 -0
  57. package/dist/supervisor/finalize.d.ts +21 -0
  58. package/dist/supervisor/finalize.d.ts.map +1 -0
  59. package/dist/supervisor/finalize.js +66 -0
  60. package/dist/supervisor/finalize.js.map +1 -0
  61. package/dist/supervisor/fingerprint.d.ts +8 -0
  62. package/dist/supervisor/fingerprint.d.ts.map +1 -0
  63. package/dist/supervisor/fingerprint.js +54 -0
  64. package/dist/supervisor/fingerprint.js.map +1 -0
  65. package/dist/supervisor/process-monitor.d.ts +9 -0
  66. package/dist/supervisor/process-monitor.d.ts.map +1 -0
  67. package/dist/supervisor/process-monitor.js +230 -0
  68. package/dist/supervisor/process-monitor.js.map +1 -0
  69. package/dist/supervisor/process-output.d.ts +27 -0
  70. package/dist/supervisor/process-output.d.ts.map +1 -0
  71. package/dist/supervisor/process-output.js +102 -0
  72. package/dist/supervisor/process-output.js.map +1 -0
  73. package/dist/supervisor/process-run.d.ts +30 -0
  74. package/dist/supervisor/process-run.d.ts.map +1 -0
  75. package/dist/supervisor/process-run.js +191 -0
  76. package/dist/supervisor/process-run.js.map +1 -0
  77. package/dist/supervisor/replay.d.ts +14 -0
  78. package/dist/supervisor/replay.d.ts.map +1 -0
  79. package/dist/supervisor/replay.js +70 -0
  80. package/dist/supervisor/replay.js.map +1 -0
  81. package/dist/supervisor/rollback.d.ts +8 -0
  82. package/dist/supervisor/rollback.d.ts.map +1 -0
  83. package/dist/supervisor/rollback.js +153 -0
  84. package/dist/supervisor/rollback.js.map +1 -0
  85. package/dist/supervisor/snapshot-ops.d.ts +36 -0
  86. package/dist/supervisor/snapshot-ops.d.ts.map +1 -0
  87. package/dist/supervisor/snapshot-ops.js +243 -0
  88. package/dist/supervisor/snapshot-ops.js.map +1 -0
  89. package/dist/supervisor/supervisor.d.ts +26 -51
  90. package/dist/supervisor/supervisor.d.ts.map +1 -1
  91. package/dist/supervisor/supervisor.js +112 -1158
  92. package/dist/supervisor/supervisor.js.map +1 -1
  93. package/dist/supervisor/types.d.ts +64 -0
  94. package/dist/supervisor/types.d.ts.map +1 -0
  95. package/dist/supervisor/types.js +2 -0
  96. package/dist/supervisor/types.js.map +1 -0
  97. package/dist/testing/contract-suite.d.ts.map +1 -1
  98. package/dist/testing/contract-suite.js +49 -10
  99. package/dist/testing/contract-suite.js.map +1 -1
  100. package/dist/types.d.ts +6 -1
  101. package/dist/types.d.ts.map +1 -1
  102. package/dist/types.js.map +1 -1
  103. package/dist/workspace/read-tracking.d.ts +23 -0
  104. package/dist/workspace/read-tracking.d.ts.map +1 -0
  105. package/dist/workspace/read-tracking.js +66 -0
  106. package/dist/workspace/read-tracking.js.map +1 -0
  107. package/dist/workspace/transactions.d.ts +89 -0
  108. package/dist/workspace/transactions.d.ts.map +1 -0
  109. package/dist/workspace/transactions.js +277 -0
  110. package/dist/workspace/transactions.js.map +1 -0
  111. package/package.json +8 -2
@@ -1,63 +1,18 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import crypto from 'node:crypto';
4
- import { UnsupportedCapabilityError, OperationNotActiveError, OperationIdConflictError, RecoveryRequiredError, ResourceConflictError, } from '../types.js';
3
+ import { OperationNotActiveError, } from '../types.js';
5
4
  import { NodePlatformDriver } from '../driver/node-driver.js';
6
5
  import { GitShadowSnapshotDriver } from '../snapshot/git-shadow.js';
7
6
  import { ServiceSupervisor } from './service.js';
8
- import { setupStreamDrainer } from './drainer.js';
9
- import { isPathContained } from '../capability/index.js';
10
- import { createConfinementDriver } from '../confinement/index.js';
11
- export function normalizeResourceName(res) {
12
- if (res.startsWith('workspace:write:')) {
13
- const raw = res.slice('workspace:write:'.length);
14
- try {
15
- return `workspace:write:${fs.realpathSync(raw)}`;
16
- }
17
- catch {
18
- return `workspace:write:${path.resolve(raw)}`;
19
- }
20
- }
21
- return res;
22
- }
23
- /**
24
- * 规范化计算完整输入的 SHA-256 指纹 (ARCHITECTURE §2 / P0-12)
25
- * 覆盖 execPath, args, cwd, 键排序的 envWhiteList, inheritEnv, sha256(stdin), 排序后的 requiredResources, timeoutMs, resourceBudget.
26
- */
27
- export function computeInputFingerprint(options) {
28
- const cmd = options.command;
29
- let stdinHash = null;
30
- if (cmd.stdin !== undefined && cmd.stdin !== null) {
31
- stdinHash = crypto.createHash('sha256').update(cmd.stdin).digest('hex');
32
- }
33
- let envSorted = null;
34
- if (cmd.envWhiteList) {
35
- const keys = Object.keys(cmd.envWhiteList).sort();
36
- envSorted = keys.map((k) => [k, cmd.envWhiteList[k]]);
37
- }
38
- const canonical = {
39
- args: cmd.args || [],
40
- cwd: cmd.cwd || '',
41
- envWhiteList: envSorted,
42
- execPath: cmd.execPath || '',
43
- inheritEnv: cmd.inheritEnv ?? null,
44
- requiredResources: [...(options.requiredResources || [])].sort(),
45
- resourceBudget: options.resourceBudget
46
- ? {
47
- enforcement: options.resourceBudget.enforcement ?? null,
48
- maxCpuTimeMs: options.resourceBudget.maxCpuTimeMs ?? null,
49
- maxMemoryBytes: options.resourceBudget.maxMemoryBytes ?? null,
50
- maxOutputBytes: options.resourceBudget.maxOutputBytes ?? null,
51
- maxPids: options.resourceBudget.maxPids ?? null,
52
- }
53
- : null,
54
- stdinHash,
55
- timeoutMs: options.timeoutMs ?? null,
56
- capabilityId: options.capabilityId ?? null,
57
- };
58
- const canonicalJson = JSON.stringify(canonical);
59
- return crypto.createHash('sha256').update(canonicalJson).digest('hex');
60
- }
7
+ import { admitProcessScope, assertHardBudgetSupported, assertRunAcceptsOperations, resolveConfinementDriver, } from './admission.js';
8
+ import { computeInputFingerprint } from './fingerprint.js';
9
+ import { finalizeOperation, indeterminateResult } from './finalize.js';
10
+ import { joinInFlight, replayRecorded } from './replay.js';
11
+ import { runProcess } from './process-run.js';
12
+ import * as snapshotOps from './snapshot-ops.js';
13
+ import { rollback } from './rollback.js';
14
+ import { WorkspaceTransactions, } from '../workspace/transactions.js';
15
+ export { normalizeResourceName, computeInputFingerprint } from './fingerprint.js';
61
16
  export class ProcessSupervisor {
62
17
  domain;
63
18
  driver;
@@ -65,6 +20,7 @@ export class ProcessSupervisor {
65
20
  serviceSupervisor;
66
21
  snapshotDriver;
67
22
  confinementDriver;
23
+ transactions;
68
24
  constructor(domain, driver = domain.getDriver?.() || new NodePlatformDriver(), snapshotDriver, confinementDriver) {
69
25
  this.domain = domain;
70
26
  this.driver = driver;
@@ -92,407 +48,32 @@ export class ProcessSupervisor {
92
48
  async startService(spec) {
93
49
  return this.serviceSupervisor.startService(spec);
94
50
  }
51
+ get snapshotContext() {
52
+ return { domain: this.domain, snapshotDriver: this.snapshotDriver };
53
+ }
95
54
  /**
96
55
  * 快照捕获协议 (ARCHITECTURE §3.5 / 契约 #32, #53)
97
56
  */
98
57
  async captureSnapshot(options) {
99
- const startTime = Date.now();
100
- let targetRoots = options.roots;
101
- let capAdmission;
102
- if (options.capabilityId) {
103
- capAdmission = this.domain.checkCapabilityAdmission({
104
- capabilityId: options.capabilityId,
105
- mutationRoots: targetRoots,
106
- runId: options.runId,
107
- opId: options.opId,
108
- });
109
- if (!targetRoots || targetRoots.length === 0) {
110
- targetRoots = capAdmission.effectiveMutationRoots;
111
- }
112
- }
113
- if (!targetRoots || targetRoots.length === 0) {
114
- throw new Error('captureSnapshot requires at least one root directory');
115
- }
116
- const realRoots = targetRoots.map((r) => path.resolve(r));
117
- const requiredResources = realRoots.map((r) => `workspace:write:${r}`);
118
- if (this.snapshotDriver instanceof GitShadowSnapshotDriver) {
119
- try {
120
- const { commonDir } = await this.snapshotDriver.assertGitRepo(realRoots[0]);
121
- requiredResources.push(`git:objects:${commonDir}`);
122
- }
123
- catch { }
124
- }
125
- const inputFingerprint = crypto
126
- .createHash('sha256')
127
- .update(JSON.stringify({
128
- kind: 'snapshot',
129
- roots: realRoots,
130
- includeIgnored: !!options.includeIgnored,
131
- maxTreeSizeBytes: options.maxTreeSizeBytes,
132
- capabilityId: options.capabilityId ?? null,
133
- }))
134
- .digest('hex');
135
- const op = {
136
- id: options.opId,
137
- runId: options.runId,
138
- kind: 'snapshot',
139
- name: `snapshot:${realRoots[0]}`,
140
- inputFingerprint,
141
- requiredResources,
142
- mutationRoots: realRoots,
143
- capabilityId: options.capabilityId,
144
- timeoutMs: options.timeoutMs,
145
- status: 'intent_registered',
146
- };
147
- this.domain.registerOperationIntent(op);
148
- this.domain.getStore().updateOperationStatus(options.opId, 'active');
149
- this.domain.getStore().recordEventAndTransitionState({
150
- domainId: this.domain.domainId,
151
- runId: options.runId,
152
- operationId: options.opId,
153
- type: 'OPERATION_STATUS_TRANSITION',
154
- payload: { status: 'active' },
155
- timestamp: new Date().toISOString(),
156
- });
157
- if (options.capabilityId && capAdmission) {
158
- this.domain.getStore().recordJournalEvent({
159
- domainId: this.domain.domainId,
160
- runId: options.runId,
161
- operationId: options.opId,
162
- type: 'CAPABILITY_USED',
163
- payload: {
164
- capabilityId: options.capabilityId,
165
- scope: capAdmission.capability.scope,
166
- actor: capAdmission.capability.issuedBy,
167
- },
168
- timestamp: new Date().toISOString(),
169
- });
170
- }
171
- try {
172
- const snapRef = await this.snapshotDriver.capture(realRoots, {
173
- id: options.opId,
174
- domainId: this.domain.domainId,
175
- opId: options.opId,
176
- includeIgnored: options.includeIgnored,
177
- maxTreeSizeBytes: options.maxTreeSizeBytes,
178
- });
179
- let result;
180
- this.domain.getStore().transaction(() => {
181
- const seq = this.domain.getStore().recordEventAndTransitionState({
182
- domainId: this.domain.domainId,
183
- runId: options.runId,
184
- operationId: options.opId,
185
- type: 'SNAPSHOT_CAPTURED',
186
- payload: {
187
- snapshotId: snapRef.id,
188
- roots: snapRef.roots,
189
- treeFingerprint: snapRef.treeFingerprint,
190
- commitHash: snapRef.commitHash,
191
- },
192
- timestamp: new Date().toISOString(),
193
- });
194
- snapRef.journalSeq = seq;
195
- this.domain.getStore().recordSnapshot(snapRef);
196
- result = {
197
- kind: 'snapshot',
198
- status: 'succeeded',
199
- snapshot: snapRef,
200
- capabilityId: options.capabilityId,
201
- durationMs: Date.now() - startTime,
202
- completedAt: new Date().toISOString(),
203
- };
204
- this.domain.getStore().recordOperationResult(options.opId, result);
205
- this.domain.getStore().updateOperationStatus(options.opId, 'done');
206
- this.domain.getStore().recordEventAndTransitionState({
207
- domainId: this.domain.domainId,
208
- runId: options.runId,
209
- operationId: options.opId,
210
- type: 'OPERATION_RESULT_RECORDED',
211
- payload: { status: 'succeeded', snapshotId: snapRef.id },
212
- timestamp: new Date().toISOString(),
213
- });
214
- });
215
- this.domain.internalReleaseResources(options.opId);
216
- return result;
217
- }
218
- catch (err) {
219
- const failResult = {
220
- kind: 'snapshot',
221
- status: 'failed',
222
- errorMessage: err.message,
223
- durationMs: Date.now() - startTime,
224
- completedAt: new Date().toISOString(),
225
- };
226
- let recordError;
227
- try {
228
- this.domain.getStore().recordOperationResult(options.opId, failResult);
229
- this.domain.getStore().updateOperationStatus(options.opId, 'done');
230
- }
231
- catch (recErr) {
232
- recordError = recErr;
233
- }
234
- this.domain.internalReleaseResources(options.opId);
235
- if (recordError !== undefined) {
236
- // 失败事实没能落盘时不能只抛原始错误:调用方需要知道 journal 里没有这条结果
237
- const recMsg = recordError instanceof Error ? recordError.message : String(recordError);
238
- throw new Error(`${err.message} (additionally, recording the failed snapshot result failed: ${recMsg})`, {
239
- cause: err,
240
- });
241
- }
242
- throw err;
243
- }
58
+ return snapshotOps.captureSnapshot(this.snapshotContext, options);
244
59
  }
245
60
  /**
246
61
  * 回滚协议 (ARCHITECTURE §3.5 / 契约 #28–#30, #56)
247
62
  */
248
63
  async rollback(options) {
249
- const startTime = Date.now();
250
- const snapshot = this.domain.getStore().getSnapshot(options.snapshotId);
251
- if (!snapshot) {
252
- throw new Error(`Snapshot not found: ${options.snapshotId}`);
253
- }
254
- let targetRoots = (options.roots || snapshot.roots).map((r) => path.resolve(r));
255
- let capAdmission;
256
- if (options.capabilityId) {
257
- capAdmission = this.domain.checkCapabilityAdmission({
258
- capabilityId: options.capabilityId,
259
- mutationRoots: targetRoots,
260
- runId: options.runId,
261
- opId: options.opId,
262
- });
263
- targetRoots = capAdmission.effectiveMutationRoots;
264
- }
265
- // 0. 前置条件检查(契约 #30):目标根目录上不存在 active / stopping / 未裁决 indeterminate 的操作
266
- for (const root of targetRoots) {
267
- let realRoot = root;
268
- try {
269
- realRoot = fs.realpathSync(root);
270
- }
271
- catch { }
272
- const res1 = `workspace:write:${root}`;
273
- const res2 = `workspace:write:${realRoot}`;
274
- if (this.domain.isResourceLocked(res1)) {
275
- throw new ResourceConflictError(res1, this.domain.getResourceOwner(res1) || 'unknown', options.opId, 0);
276
- }
277
- if (this.domain.isResourceLocked(res2)) {
278
- throw new ResourceConflictError(res2, this.domain.getResourceOwner(res2) || 'unknown', options.opId, 0);
279
- }
280
- }
281
- const unfinishedOps = this.domain.getStore().getUnfinishedOperations(this.domain.domainId);
282
- for (const op of unfinishedOps) {
283
- if (op.id === options.opId)
284
- continue;
285
- for (const root of targetRoots) {
286
- let realRoot = root;
287
- try {
288
- realRoot = fs.realpathSync(root);
289
- }
290
- catch { }
291
- const writeRes1 = `workspace:write:${root}`;
292
- const writeRes2 = `workspace:write:${realRoot}`;
293
- if (op.requiredResources.includes(writeRes1) ||
294
- op.requiredResources.includes(writeRes2) ||
295
- op.mutationRoots?.some((mr) => {
296
- const resolved = path.resolve(mr);
297
- return resolved === root || resolved === realRoot;
298
- })) {
299
- throw new ResourceConflictError(writeRes1, op.id, options.opId, 0);
300
- }
301
- }
302
- }
303
- // 检查是否有未裁决的 indeterminate op 占用该 root
304
- const leases = this.domain.getStore().getPersistedResourceLeases(this.domain.domainId);
305
- for (const lease of leases) {
306
- for (const root of targetRoots) {
307
- let realRoot = root;
308
- try {
309
- realRoot = fs.realpathSync(root);
310
- }
311
- catch { }
312
- const writeRes1 = `workspace:write:${root}`;
313
- const writeRes2 = `workspace:write:${realRoot}`;
314
- if ((lease.resourceId === writeRes1 || lease.resourceId === writeRes2) && lease.operationId !== options.opId) {
315
- throw new ResourceConflictError(lease.resourceId, lease.operationId, options.opId, 0);
316
- }
317
- }
318
- }
319
- const requiredResources = targetRoots.map((r) => `workspace:write:${r}`);
320
- const inputFingerprint = crypto
321
- .createHash('sha256')
322
- .update(JSON.stringify({
323
- kind: 'rollback',
324
- snapshotId: options.snapshotId,
325
- targetRoots,
326
- capabilityId: options.capabilityId ?? null,
327
- }))
328
- .digest('hex');
329
- const op = {
330
- id: options.opId,
331
- runId: options.runId,
332
- kind: 'rollback',
333
- name: `rollback:${options.snapshotId}`,
334
- inputFingerprint,
335
- requiredResources,
336
- mutationRoots: targetRoots,
337
- capabilityId: options.capabilityId,
338
- outputRef: options.snapshotId,
339
- timeoutMs: options.timeoutMs,
340
- status: 'intent_registered',
341
- };
342
- this.domain.registerOperationIntent(op);
343
- this.domain.getStore().updateOperationStatus(options.opId, 'active');
344
- this.domain.getStore().recordEventAndTransitionState({
345
- domainId: this.domain.domainId,
346
- runId: options.runId,
347
- operationId: options.opId,
348
- type: 'OPERATION_STATUS_TRANSITION',
349
- payload: { status: 'active' },
350
- timestamp: new Date().toISOString(),
351
- });
352
- if (options.capabilityId && capAdmission) {
353
- this.domain.getStore().recordJournalEvent({
354
- domainId: this.domain.domainId,
355
- runId: options.runId,
356
- operationId: options.opId,
357
- type: 'CAPABILITY_USED',
358
- payload: {
359
- capabilityId: options.capabilityId,
360
- scope: capAdmission.capability.scope,
361
- actor: capAdmission.capability.issuedBy,
362
- },
363
- timestamp: new Date().toISOString(),
364
- });
365
- }
366
- try {
367
- const { unrestoredPaths } = await this.snapshotDriver.restore(snapshot, { force: options.force });
368
- const currentFp = await this.snapshotDriver.fingerprint(targetRoots, { against: snapshot });
369
- const isMatch = currentFp === snapshot.treeFingerprint;
370
- let status;
371
- if (!isMatch) {
372
- status = 'failed';
373
- }
374
- else if (unrestoredPaths.length > 0) {
375
- status = 'partial';
376
- }
377
- else {
378
- status = 'restored';
379
- }
380
- let allOpsConfined = false;
381
- const eventsSince = snapshot.journalSeq !== undefined
382
- ? this.domain.getStore().getJournalEvents(this.domain.domainId, snapshot.journalSeq)
383
- : [];
384
- const opResults = eventsSince.filter((e) => e.type === 'OPERATION_RESULT_RECORDED' && e.operationId !== options.opId);
385
- if (opResults.length === 0) {
386
- allOpsConfined = true;
387
- }
388
- else {
389
- allOpsConfined = opResults.every((e) => {
390
- const res = e.payload?.result || e.payload;
391
- const storedOp = this.domain.getStore().getOperation(e.operationId);
392
- if (storedOp?.kind === 'snapshot' || storedOp?.kind === 'rollback') {
393
- return true;
394
- }
395
- if (res?.confined !== true) {
396
- return false;
397
- }
398
- const opRoots = storedOp?.mutationRoots || [];
399
- if (opRoots.length === 0)
400
- return true;
401
- return opRoots.every((opr) => targetRoots.some((sr) => isPathContained(sr, opr)));
402
- });
403
- }
404
- let coverage;
405
- let outOfScopeEffects;
406
- if (status !== 'restored') {
407
- coverage = status === 'partial' ? 'declared_roots' : 'none';
408
- outOfScopeEffects = 'possible';
409
- }
410
- else if (allOpsConfined) {
411
- coverage = 'complete';
412
- outOfScopeEffects = 'none_possible';
413
- }
414
- else {
415
- coverage = 'declared_roots';
416
- outOfScopeEffects = 'possible';
417
- }
418
- let result;
419
- this.domain.getStore().transaction(() => {
420
- result = {
421
- kind: 'rollback',
422
- status,
423
- snapshotId: options.snapshotId,
424
- unrestoredPaths: unrestoredPaths.length > 0 ? unrestoredPaths : undefined,
425
- coverage,
426
- outOfScopeEffects,
427
- capabilityId: options.capabilityId,
428
- durationMs: Date.now() - startTime,
429
- completedAt: new Date().toISOString(),
430
- };
431
- this.domain.getStore().recordOperationResult(options.opId, result);
432
- this.domain.getStore().updateOperationStatus(options.opId, 'done');
433
- });
434
- this.domain.internalReleaseResources(options.opId);
435
- return result;
436
- }
437
- catch (err) {
438
- this.domain.internalReleaseResources(options.opId);
439
- throw err;
440
- }
64
+ return rollback(this.snapshotContext, options);
441
65
  }
442
66
  /**
443
67
  * 分叉工作区 Materialize (ARCHITECTURE §3.5 / 契约 #54)
444
68
  */
445
69
  async materialize(snapshotId, newRoot) {
446
- const absNewRoot = path.resolve(newRoot);
447
- const writeRes = `workspace:write:${absNewRoot}`;
448
- if (this.domain.isResourceLocked(writeRes)) {
449
- throw new ResourceConflictError(writeRes, this.domain.getResourceOwner(writeRes) || 'unknown', 'materialize');
450
- }
451
- const opId = `mat-${snapshotId}-${Date.now()}`;
452
- this.domain.allocateResources(opId, [writeRes]);
453
- try {
454
- if (!this.snapshotDriver.materialize) {
455
- throw new Error(`Snapshot driver ${this.snapshotDriver.name} does not support materialize`);
456
- }
457
- const snapshot = this.domain.getStore().getSnapshot(snapshotId);
458
- const repoRoot = snapshot?.roots[0];
459
- return await this.snapshotDriver.materialize(snapshotId, absNewRoot, { repoRoot });
460
- }
461
- catch (err) {
462
- this.domain.internalReleaseResources(opId);
463
- throw err;
464
- }
70
+ return snapshotOps.materialize(this.snapshotContext, snapshotId, newRoot);
465
71
  }
466
72
  /**
467
73
  * 回收分叉工作区 Dematerialize (ARCHITECTURE §3.5 / 契约 #54)
468
74
  */
469
75
  async dematerialize(newRoot, options) {
470
- const absNewRoot = path.resolve(newRoot);
471
- let realNewRoot = absNewRoot;
472
- try {
473
- realNewRoot = fs.realpathSync(absNewRoot);
474
- }
475
- catch { }
476
- if (!this.snapshotDriver.dematerialize) {
477
- throw new Error(`Snapshot driver ${this.snapshotDriver.name} does not support dematerialize`);
478
- }
479
- await this.snapshotDriver.dematerialize(absNewRoot, options);
480
- const writeRes = `workspace:write:${absNewRoot}`;
481
- const writeResReal = `workspace:write:${realNewRoot}`;
482
- const owner1 = this.domain.getResourceOwner(writeRes);
483
- const owner2 = this.domain.getResourceOwner(writeResReal);
484
- if (owner1) {
485
- this.domain.internalReleaseResources(owner1, [writeRes]);
486
- }
487
- if (owner2 && owner2 !== owner1) {
488
- this.domain.internalReleaseResources(owner2, [writeResReal]);
489
- }
490
- const leases = this.domain.getStore().getPersistedResourceLeases(this.domain.domainId);
491
- for (const lease of leases) {
492
- if (lease.resourceId === writeRes || lease.resourceId === writeResReal) {
493
- this.domain.internalReleaseResources(lease.operationId, [lease.resourceId]);
494
- }
495
- }
76
+ return snapshotOps.dematerialize(this.snapshotContext, newRoot, options);
496
77
  }
497
78
  /**
498
79
  * 回收快照:删除驱动侧的私有 ref 与 store 记录,并写 SNAPSHOT_PRUNED。
@@ -500,195 +81,75 @@ export class ProcessSupervisor {
500
81
  * 未知 id 视为已回收(幂等);驱动删除失败直接抛出,store 记录保留,便于重试。
501
82
  */
502
83
  async pruneSnapshots(snapshotIds, options) {
503
- const store = this.domain.getStore();
504
- const pruned = [];
505
- for (const id of snapshotIds) {
506
- const snapshot = store.getSnapshot(id);
507
- if (!snapshot)
508
- continue;
509
- store.verifyEpochFencing(this.domain.domainId);
510
- const repoRoot = this.snapshotDriver instanceof GitShadowSnapshotDriver
511
- ? (await this.snapshotDriver.assertGitRepo(snapshot.roots[0])).repoRoot
512
- : undefined;
513
- await this.snapshotDriver.prune([id], { repoRoot });
514
- store.transaction(() => {
515
- store.deleteSnapshot(id);
516
- store.recordEventAndTransitionState({
517
- domainId: this.domain.domainId,
518
- runId: options?.runId,
519
- type: 'SNAPSHOT_PRUNED',
520
- payload: { snapshotId: id, roots: snapshot.roots },
521
- timestamp: new Date().toISOString(),
522
- });
523
- });
524
- pruned.push(id);
525
- }
526
- return pruned;
84
+ return snapshotOps.pruneSnapshots(this.snapshotContext, snapshotIds, options);
85
+ }
86
+ /**
87
+ * 工作区事务(ARCHITECTURE §3.9):在主工作区的基线快照上 materialize 出独立 fork,
88
+ * agent 以 `forkRoot` 为 cwd 工作;commit 时按读写集做乐观并发校验,无冲突才把写集应用回主工作区。
89
+ */
90
+ async beginWorkspaceTransaction(options) {
91
+ return this.workspaceTransactions().begin(options);
92
+ }
93
+ async inspectWorkspaceTransaction(txId) {
94
+ return this.workspaceTransactions().inspect(txId);
95
+ }
96
+ async commitWorkspaceTransaction(txId) {
97
+ return this.workspaceTransactions().commit(txId);
98
+ }
99
+ async abortWorkspaceTransaction(txId, reason) {
100
+ return this.workspaceTransactions().abort(txId, reason);
101
+ }
102
+ workspaceTransactions() {
103
+ if (this.transactions)
104
+ return this.transactions;
105
+ const snapshotDriver = this.snapshotDriver;
106
+ if (!(snapshotDriver instanceof GitShadowSnapshotDriver)) {
107
+ throw new Error(`Workspace transactions need the git-shadow snapshot driver (got ${snapshotDriver.name})`);
108
+ }
109
+ this.transactions = new WorkspaceTransactions({
110
+ domain: this.domain,
111
+ snapshotDriver,
112
+ captureSnapshot: async (runId, opId, root) => {
113
+ const res = await this.captureSnapshot({ runId, opId, roots: [root] });
114
+ if (res.status !== 'succeeded' || !res.snapshot) {
115
+ throw new Error(`Base snapshot for ${opId} failed: ${res.errorMessage ?? res.status}`);
116
+ }
117
+ return res.snapshot;
118
+ },
119
+ materialize: async (snapshotId, forkPath) => {
120
+ await this.materialize(snapshotId, forkPath);
121
+ // 租约移交:materialize 以独占租约保留 fork(§3.5);事务接管 fork 后释放它,
122
+ // fork 内 agent 操作之间的互斥由各自声明的 workspace:write:<fork> 保证。
123
+ for (const res of new Set([`workspace:write:${path.resolve(forkPath)}`, `workspace:write:${fs.realpathSync(forkPath)}`])) {
124
+ const owner = this.domain.getResourceOwner(res);
125
+ if (owner?.startsWith('mat-'))
126
+ this.domain.internalReleaseResources(owner, [res]);
127
+ }
128
+ },
129
+ dematerialize: (forkPath) => this.dematerialize(forkPath, { force: true }),
130
+ });
131
+ return this.transactions;
527
132
  }
528
133
  /**
529
134
  * 启动协议:准入检查 -> 先持久化意图 -> 请求驱动启动 -> 登记身份并转 active -> 监督运行 -> 记录结果
530
135
  */
531
136
  async executeProcess(options) {
532
- // 0. Capability 准入校验与字段推导(契约 #55)
533
- let effectiveMutationRoots = options.mutationRoots;
534
- let capAdmission;
535
- if (options.capabilityId) {
536
- let candidateRoots = options.mutationRoots ? [...options.mutationRoots] : [];
537
- if (candidateRoots.length === 0 && options.command.cwd) {
538
- candidateRoots = [options.command.cwd];
539
- }
540
- capAdmission = this.domain.checkCapabilityAdmission({
541
- capabilityId: options.capabilityId,
542
- requiredResources: options.requiredResources,
543
- mutationRoots: candidateRoots.length > 0 ? candidateRoots : undefined,
544
- runId: options.runId,
545
- opId: options.opId,
546
- });
547
- options.requiredResources = capAdmission.effectiveResources;
548
- effectiveMutationRoots = capAdmission.effectiveMutationRoots;
549
- }
550
- else {
551
- options.requiredResources = (options.requiredResources || []).map(normalizeResourceName);
552
- if (!effectiveMutationRoots || effectiveMutationRoots.length === 0) {
553
- effectiveMutationRoots = options.requiredResources
554
- .filter((r) => r.startsWith('workspace:write:'))
555
- .map((r) => r.slice('workspace:write:'.length));
556
- }
557
- }
558
- // 0. ConfinementDriver 驱动解析与包装(契约 #56)
559
- let activeConfinementDriver;
560
- if (options.confinementDriver) {
561
- activeConfinementDriver = options.confinementDriver;
562
- }
563
- else if (typeof options.confinement === 'string') {
564
- const driver = createConfinementDriver(options.confinement);
565
- if (!driver) {
566
- throw new Error(`Confinement driver '${options.confinement}' is not available on this platform`);
567
- }
568
- activeConfinementDriver = driver;
569
- }
570
- else if (options.confinement === true) {
571
- const driver = this.confinementDriver || createConfinementDriver();
572
- if (!driver) {
573
- throw new Error('Confinement was requested but no confinement driver is available on this platform');
574
- }
575
- activeConfinementDriver = driver;
576
- }
577
- else if (this.confinementDriver) {
578
- activeConfinementDriver = this.confinementDriver;
579
- }
580
- let effectiveCommand = options.command;
581
- const isConfined = activeConfinementDriver !== undefined;
582
- const confinementDriverName = activeConfinementDriver?.name;
583
- if (activeConfinementDriver) {
584
- effectiveCommand = activeConfinementDriver.wrap(options.command, effectiveMutationRoots || []);
585
- }
137
+ // 0. 准入:capability 范围、写入限制、hard 预算能力、调用 Run 状态
138
+ const { mutationRoots, capAdmission } = admitProcessScope(this.domain, options);
139
+ const confinementDriver = resolveConfinementDriver(options, this.confinementDriver);
140
+ const command = confinementDriver ? confinementDriver.wrap(options.command, mutationRoots || []) : options.command;
586
141
  const startTime = Date.now();
587
- const maxBytes = options.maxOutputBytes ?? 10 * 1024 * 1024; // 默认 10MB
588
- const drainTimeoutMs = options.drainTimeoutMs ?? 2000; // 默认 2000ms
589
- let stdoutDrainer;
590
- let stderrDrainer;
591
- // 0. [准入期强校验 Pre-admission Capability Check]
592
- if (options.resourceBudget?.enforcement === 'hard') {
593
- if (options.resourceBudget.maxMemoryBytes && !this.driver.capabilities.memoryHardLimit) {
594
- throw new UnsupportedCapabilityError('memoryHardLimit', this.driver.name, 'hard');
595
- }
596
- if (options.resourceBudget.maxPids && !this.driver.capabilities.pidsLimit) {
597
- throw new UnsupportedCapabilityError('pidsLimit', this.driver.name, 'hard');
598
- }
599
- if (options.resourceBudget.maxCpuTimeMs && !this.driver.capabilities.cpuLimit) {
600
- throw new UnsupportedCapabilityError('cpuLimit', this.driver.name, 'hard');
601
- }
602
- }
142
+ assertHardBudgetSupported(this.driver, options.resourceBudget);
603
143
  const inputFingerprint = options.inputFingerprint || computeInputFingerprint(options);
604
- // 0. 验证调用 Run 状态(N3 契约):终态 Run 禁止登记或重放操作
605
- const callingRun = this.domain.getStore().getRun(options.runId);
606
- if (!callingRun) {
607
- throw new Error(`Run "${options.runId}" is not registered in domain "${this.domain.domainId}". ` +
608
- 'Register the task and run first (store.saveTask() + store.saveRun()), then execute operations for that run.');
609
- }
610
- if (callingRun.status === 'succeeded' ||
611
- callingRun.status === 'failed' ||
612
- callingRun.status === 'cancelled' ||
613
- callingRun.status === 'indeterminate') {
614
- throw new Error(`Cannot register operation "${options.opId}" for Run "${options.runId}" because the Run is already finalized with status "${callingRun.status}".`);
144
+ assertRunAcceptsOperations(this.domain, options.runId, options.opId);
145
+ // 1–2. 幂等重放(ARCHITECTURE §3.7):在飞加入,已持久化原样返回
146
+ const inFlight = this.activeOperations.get(options.opId);
147
+ if (inFlight) {
148
+ return joinInFlight(this.domain, inFlight, options, inputFingerprint);
615
149
  }
616
- // 1. 在飞操作重放判定(ARCHITECTURE §3.7 / 契约 #46)
617
- if (this.activeOperations.has(options.opId)) {
618
- const inFlight = this.activeOperations.get(options.opId);
619
- if (inFlight.inputFingerprint !== inputFingerprint) {
620
- throw new OperationIdConflictError(options.opId, inFlight.inputFingerprint, inputFingerprint, inFlight.phase, inFlight.runId);
621
- }
622
- this.domain.getStore().recordReplay(this.domain.domainId, options.opId, 'joined', options.runId);
623
- if (options.onStreamChunk) {
624
- inFlight.streamSubscribers.add(options.onStreamChunk);
625
- }
626
- if (options.abortSignal) {
627
- const signal = options.abortSignal;
628
- if (signal.aborted) {
629
- if (options.onStreamChunk) {
630
- inFlight.streamSubscribers.delete(options.onStreamChunk);
631
- }
632
- throw signal.reason || new Error('Operation wait aborted');
633
- }
634
- return await new Promise((resolve, reject) => {
635
- const onAbort = () => {
636
- if (options.onStreamChunk) {
637
- inFlight.streamSubscribers.delete(options.onStreamChunk);
638
- }
639
- reject(signal.reason || new Error('Operation wait aborted'));
640
- };
641
- signal.addEventListener('abort', onAbort, { once: true });
642
- inFlight.resultPromise.then((res) => {
643
- signal.removeEventListener('abort', onAbort);
644
- if (options.onStreamChunk) {
645
- inFlight.streamSubscribers.delete(options.onStreamChunk);
646
- }
647
- resolve({ ...res, replayed: true });
648
- }, (err) => {
649
- signal.removeEventListener('abort', onAbort);
650
- if (options.onStreamChunk) {
651
- inFlight.streamSubscribers.delete(options.onStreamChunk);
652
- }
653
- reject(err);
654
- });
655
- });
656
- }
657
- const res = await inFlight.resultPromise;
658
- if (options.onStreamChunk) {
659
- inFlight.streamSubscribers.delete(options.onStreamChunk);
660
- }
661
- return {
662
- ...res,
663
- replayed: true,
664
- };
665
- }
666
- // 2. 已持久化操作重放判定(ARCHITECTURE §3.7 / 契约 #45, #47, #48, #49)
667
- const existingRecorded = this.domain.getStore().getOperation(options.opId);
668
- if (existingRecorded) {
669
- if (existingRecorded.inputFingerprint !== inputFingerprint) {
670
- throw new OperationIdConflictError(options.opId, existingRecorded.inputFingerprint, inputFingerprint, existingRecorded.status, existingRecorded.runId);
671
- }
672
- // 未终结状态且不在内存中 → 必须先执行恢复(崩溃残留现场)
673
- if (existingRecorded.status !== 'done') {
674
- throw new RecoveryRequiredError(options.opId, existingRecorded.status, existingRecorded.runId);
675
- }
676
- // indeterminate 状态:绝不重跑,原样返回,保留租约
677
- if (existingRecorded.result?.status === 'indeterminate') {
678
- this.domain.getStore().recordReplay(this.domain.domainId, options.opId, 'indeterminate', options.runId);
679
- return {
680
- ...existingRecorded.result,
681
- replayed: true,
682
- runId: existingRecorded.runId,
683
- };
684
- }
685
- // 已结清终态 (succeeded / failed / cancelled):返回已持久化结果
686
- this.domain.getStore().recordReplay(this.domain.domainId, options.opId, 'recorded', options.runId);
687
- return {
688
- ...existingRecorded.result,
689
- replayed: true,
690
- runId: existingRecorded.runId,
691
- };
150
+ const replayed = replayRecorded(this.domain, options, inputFingerprint);
151
+ if (replayed) {
152
+ return replayed;
692
153
  }
693
154
  const op = {
694
155
  id: options.opId,
@@ -697,12 +158,37 @@ export class ProcessSupervisor {
697
158
  name: options.name,
698
159
  inputFingerprint,
699
160
  requiredResources: options.requiredResources || [],
700
- mutationRoots: effectiveMutationRoots,
161
+ mutationRoots,
701
162
  capabilityId: options.capabilityId,
702
163
  timeoutMs: options.timeoutMs,
703
164
  resourceBudget: options.resourceBudget,
704
165
  status: 'pending',
705
166
  };
167
+ const opState = this.trackOperation(options, inputFingerprint, startTime);
168
+ const executionPromise = runProcess(this.runContext, {
169
+ options,
170
+ op,
171
+ opState,
172
+ command,
173
+ confined: confinementDriver !== undefined,
174
+ confinementDriverName: confinementDriver?.name,
175
+ capAdmission,
176
+ });
177
+ opState.resultPromise = executionPromise;
178
+ return await executionPromise;
179
+ }
180
+ get runContext() {
181
+ return {
182
+ domain: this.domain,
183
+ driver: this.driver,
184
+ stop: (opId, reason, graceMs) => this.handleStopPipeline(opId, reason, graceMs),
185
+ forget: (opId) => {
186
+ this.activeOperations.delete(opId);
187
+ },
188
+ };
189
+ }
190
+ /** 登记在飞状态;abortSignal 触发即走取消流水线(已中止则在启动前取消)。 */
191
+ trackOperation(options, inputFingerprint, startTime) {
706
192
  const streamSubscribers = new Set();
707
193
  if (options.onStreamChunk) {
708
194
  streamSubscribers.add(options.onStreamChunk);
@@ -729,450 +215,7 @@ export class ProcessSupervisor {
729
215
  options.abortSignal.addEventListener('abort', onAbort, { once: true });
730
216
  }
731
217
  }
732
- const executionPromise = (async () => {
733
- let timeoutTimer = null;
734
- let samplingInterval = null;
735
- let stdoutDrainer;
736
- let stderrDrainer;
737
- try {
738
- // 1. [资源分配与排队等待]
739
- await this.domain.allocateResourcesWithWait(options.opId, options.requiredResources || [], options.waitTimeoutMs ?? 0, options.resourceBudget, () => opState.cancelRequested === true);
740
- if (opState.cancelRequested) {
741
- return this.handlePreSpawnCancellation(options, opState, startTime);
742
- }
743
- // 2. [启动协议步骤 1] 写入 SQLite (status: intent_registered)
744
- try {
745
- this.domain.getStore().registerOperationIntent(op, this.domain.domainId);
746
- opState.phase = 'intent_registered';
747
- }
748
- catch (err) {
749
- this.domain.internalReleaseResources(options.opId, options.requiredResources);
750
- throw err;
751
- }
752
- if (opState.cancelRequested) {
753
- return this.handlePreSpawnCancellation(options, opState, startTime);
754
- }
755
- let handle;
756
- opState.phase = 'spawning';
757
- try {
758
- // 3. [启动协议步骤 2] 请求平台驱动启动
759
- handle = await this.driver.spawn(effectiveCommand);
760
- opState.handle = handle;
761
- opState.command = effectiveCommand;
762
- }
763
- catch (spawnError) {
764
- opState.phase = 'done';
765
- opState.stopResolve?.({ stopped: 'confirmed_stopped', scope: 'direct_child', errorDetails: spawnError?.message });
766
- opState.stopResolve = undefined;
767
- // 启动失败(如可执行文件不存在),不产生假运行状态,直接失败收尾并释放资源
768
- const failResult = {
769
- kind: 'process',
770
- status: 'failed',
771
- exitCode: 127,
772
- signal: null,
773
- stdout: '',
774
- stderr: spawnError?.message || String(spawnError),
775
- spawnFailure: spawnError?.message || String(spawnError),
776
- capabilityId: options.capabilityId,
777
- confined: isConfined,
778
- confinementDriver: confinementDriverName,
779
- isTruncated: false,
780
- identityVerification: 'not_original_process',
781
- durationMs: Date.now() - startTime,
782
- completedAt: new Date().toISOString(),
783
- };
784
- return this.finalizeOperation(options.opId, failResult, options.requiredResources, true);
785
- }
786
- // 4. [启动协议步骤 3] 登记执行身份,状态推进为 active
787
- try {
788
- this.domain.getStore().updateOperationStatus(options.opId, 'active', handle.identity);
789
- opState.phase = 'active';
790
- handle.releaseGate?.();
791
- if (options.capabilityId && capAdmission) {
792
- this.domain.getStore().recordJournalEvent({
793
- domainId: this.domain.domainId,
794
- runId: options.runId,
795
- operationId: options.opId,
796
- type: 'CAPABILITY_USED',
797
- payload: {
798
- capabilityId: options.capabilityId,
799
- scope: capAdmission.capability.scope,
800
- actor: capAdmission.capability.issuedBy,
801
- },
802
- timestamp: new Date().toISOString(),
803
- });
804
- }
805
- }
806
- catch (statusError) {
807
- handle.destroyGate?.();
808
- // N4: spawn 成功但登记 active 失败,防止产生孤儿进程
809
- opState.phase = 'stopping';
810
- const stopRes = await this.driver.terminate(handle.identity, 2000);
811
- // 有界等待退出与关闭流,防止管道悬挂
812
- await Promise.race([
813
- (handle.onRootExit ?? handle.onExit).catch(() => { }),
814
- new Promise((resolve) => setTimeout(resolve, 500)),
815
- ]);
816
- try {
817
- handle.stdout.destroy?.();
818
- handle.stderr.destroy?.();
819
- }
820
- catch { }
821
- opState.phase = 'done';
822
- if (opState.stopResolve) {
823
- const resolveFn = opState.stopResolve;
824
- opState.stopResolve = undefined;
825
- resolveFn(stopRes);
826
- }
827
- if (stopRes.stopped === 'confirmed_stopped') {
828
- const failResult = {
829
- kind: 'process',
830
- status: 'failed',
831
- exitCode: null,
832
- signal: null,
833
- stdout: '',
834
- stderr: `Failed to register active status: ${statusError?.message || String(statusError)}`,
835
- spawnFailure: statusError?.message || String(statusError),
836
- isTruncated: false,
837
- identityVerification: 'not_original_process',
838
- durationMs: Date.now() - startTime,
839
- completedAt: new Date().toISOString(),
840
- };
841
- this.finalizeOperation(options.opId, failResult, options.requiredResources, true);
842
- }
843
- else {
844
- const indetResult = {
845
- kind: 'indeterminate',
846
- status: 'indeterminate',
847
- reason: `Failed to register active status: ${statusError?.message || String(statusError)}, and process ${handle.identity.pid} could not be confirmed stopped: ${stopRes.errorDetails || 'residual processes still alive'}`,
848
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
849
- durationMs: Date.now() - startTime,
850
- completedAt: new Date().toISOString(),
851
- };
852
- this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
853
- }
854
- throw statusError;
855
- }
856
- // 准备 artifacts 溢出转储目录
857
- const artifactsDir = options.artifactsDir || path.join(this.domain.domainPath, 'artifacts');
858
- let artifactsDirError = undefined;
859
- if (!fs.existsSync(artifactsDir)) {
860
- try {
861
- fs.mkdirSync(artifactsDir, { recursive: true });
862
- }
863
- catch (dirErr) {
864
- artifactsDirError = dirErr.message || String(dirErr);
865
- }
866
- }
867
- const stdoutSpillPath = path.join(artifactsDir, `${options.opId}-stdout.log`);
868
- const stderrSpillPath = path.join(artifactsDir, `${options.opId}-stderr.log`);
869
- // 5. [正常监督、有界排空与流式转储 (Spill to Artifacts)]
870
- let totalOutputBytes = 0;
871
- const onChunk = (bytes) => {
872
- totalOutputBytes += bytes;
873
- if (options.resourceBudget?.enforcement === 'soft' &&
874
- options.resourceBudget.maxOutputBytes &&
875
- totalOutputBytes > options.resourceBudget.maxOutputBytes &&
876
- !opState.terminationReason) {
877
- opState.terminationReason = 'output_exceeded';
878
- this.handleStopPipeline(options.opId, 'output_exceeded', 1000).catch(() => { });
879
- }
880
- };
881
- let streamCallbackError;
882
- const forwardChunk = (stream) => (chunk) => {
883
- for (const subscriber of opState.streamSubscribers) {
884
- try {
885
- subscriber(stream, chunk);
886
- }
887
- catch (err) {
888
- streamCallbackError ??= err instanceof Error ? err.message : String(err);
889
- }
890
- }
891
- };
892
- stdoutDrainer = setupStreamDrainer(handle.stdout, maxBytes, stdoutSpillPath, onChunk, forwardChunk('stdout'), artifactsDirError);
893
- stderrDrainer = setupStreamDrainer(handle.stderr, maxBytes, stderrSpillPath, onChunk, forwardChunk('stderr'), artifactsDirError);
894
- // 检查在 spawn 途中是否已被请求取消:若已请求取消,立即触发停止流水线向底层发送终止信号
895
- if (opState.cancelRequested) {
896
- this.handleStopPipeline(options.opId, 'user_cancelled', opState.cancelGraceMs ?? 2000).catch(() => { });
897
- }
898
- const timeoutPromise = options.timeoutMs && options.timeoutMs > 0
899
- ? new Promise((resolve) => {
900
- timeoutTimer = setTimeout(() => resolve('timeout'), options.timeoutMs);
901
- })
902
- : new Promise(() => { });
903
- // Soft / Observe 模式资源采样治理
904
- let peakMemoryBytes = 0;
905
- let peakCpuTimeMs = 0;
906
- if (this.driver.sampleMetrics &&
907
- options.resourceBudget &&
908
- (options.resourceBudget.enforcement === 'soft' || options.resourceBudget.enforcement === 'observe')) {
909
- samplingInterval = setInterval(async () => {
910
- try {
911
- const metrics = await this.driver.sampleMetrics(handle.identity);
912
- peakMemoryBytes = Math.max(peakMemoryBytes, metrics.rssBytes);
913
- peakCpuTimeMs = Math.max(peakCpuTimeMs, metrics.cpuTimeMs);
914
- if (options.resourceBudget?.enforcement === 'soft') {
915
- if (options.resourceBudget.maxMemoryBytes && metrics.rssBytes > options.resourceBudget.maxMemoryBytes) {
916
- opState.terminationReason = 'memory_exceeded';
917
- if (samplingInterval)
918
- clearInterval(samplingInterval);
919
- await this.handleStopPipeline(options.opId, 'memory_exceeded', 1000);
920
- }
921
- else if (options.resourceBudget.maxCpuTimeMs && metrics.cpuTimeMs > options.resourceBudget.maxCpuTimeMs) {
922
- opState.terminationReason = 'cpu_exceeded';
923
- if (samplingInterval)
924
- clearInterval(samplingInterval);
925
- await this.handleStopPipeline(options.opId, 'cpu_exceeded', 1000);
926
- }
927
- else if (options.resourceBudget.maxPids && metrics.pidsCount > options.resourceBudget.maxPids) {
928
- opState.terminationReason = 'pids_exceeded';
929
- if (samplingInterval)
930
- clearInterval(samplingInterval);
931
- await this.handleStopPipeline(options.opId, 'pids_exceeded', 1000);
932
- }
933
- }
934
- }
935
- catch { }
936
- }, 100);
937
- }
938
- let notifyStop;
939
- const stopTriggerPromise = new Promise((resolve) => {
940
- notifyStop = resolve;
941
- });
942
- const previousResolve = opState.stopResolve;
943
- opState.stopResolve = (res) => {
944
- previousResolve?.(res);
945
- notifyStop?.(res);
946
- };
947
- // 等待根进程退出、超时触发、或外部停止完成
948
- const rootExitPromise = handle.onRootExit ?? handle.onExit;
949
- const stopCandidate = opState.stopPromise ? opState.stopPromise : stopTriggerPromise;
950
- const exitOrTimeout = await Promise.race([
951
- rootExitPromise.then((res) => ({ type: 'exit', res, stopRes: null })),
952
- timeoutPromise.then((type) => ({ type, res: null, stopRes: null })),
953
- stopCandidate.then((stopRes) => ({ type: 'stop', res: null, stopRes })),
954
- ]);
955
- if (timeoutTimer)
956
- clearTimeout(timeoutTimer);
957
- if (samplingInterval)
958
- clearInterval(samplingInterval);
959
- let exitResult;
960
- if (exitOrTimeout.type === 'stop') {
961
- const stopRes = exitOrTimeout.stopRes;
962
- if (stopRes.stopped !== 'confirmed_stopped') {
963
- if (handle.rawProcess && typeof handle.rawProcess.kill === 'function') {
964
- try {
965
- handle.rawProcess.kill('SIGKILL');
966
- }
967
- catch { }
968
- }
969
- try {
970
- handle.stdout.destroy?.();
971
- handle.stderr.destroy?.();
972
- }
973
- catch { }
974
- const stored = this.domain.getStore().getOperation(options.opId);
975
- if (stored && stored.status === 'done') {
976
- return (stored.result ?? null);
977
- }
978
- const indetResult = {
979
- kind: 'indeterminate',
980
- status: 'indeterminate',
981
- reason: `Process ${handle.identity.pid} was cancelled or stopped but could not be confirmed: ${stopRes.errorDetails || 'residual processes still alive'}`,
982
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
983
- durationMs: Date.now() - startTime,
984
- completedAt: new Date().toISOString(),
985
- };
986
- return this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
987
- }
988
- const rootExitWaitMs = 1000;
989
- const rootExitOrTimeout = await Promise.race([
990
- rootExitPromise.then((res) => ({ exited: true, res })),
991
- new Promise((resolve) => setTimeout(() => resolve({ exited: false }), rootExitWaitMs)),
992
- ]);
993
- exitResult = rootExitOrTimeout.exited ? rootExitOrTimeout.res : { exitCode: null, signal: 'SIGKILL' };
994
- }
995
- else if (exitOrTimeout.type === 'timeout') {
996
- opState.timedOut = true;
997
- opState.terminationReason = 'timed_out';
998
- const stopRes = await this.handleStopPipeline(options.opId, 'timed_out', 1500);
999
- // 超时路径:根进程已由停止流水线终止,等待根进程退出事实(避免被持有管道的逃逸后代挂死)
1000
- // 关键防护(问题 4):给等待根进程退出增加有界上限(1000ms),
1001
- // 若驱动停止未确认(stopped !== 'confirmed_stopped')或根进程超出上限仍未退出,转入 indeterminate
1002
- const rootExitWaitMs = 1000;
1003
- const rootExitOrTimeout = await Promise.race([
1004
- (handle.onRootExit ?? handle.onExit).then((res) => ({ exited: true, res })),
1005
- new Promise((resolve) => setTimeout(() => resolve({ exited: false }), rootExitWaitMs)),
1006
- ]);
1007
- if (stopRes.stopped !== 'confirmed_stopped' || !rootExitOrTimeout.exited) {
1008
- const stored = this.domain.getStore().getOperation(options.opId);
1009
- if (stored && stored.status === 'done' && stored.result?.status === 'indeterminate') {
1010
- return stored.result;
1011
- }
1012
- const indetResult = {
1013
- kind: 'indeterminate',
1014
- status: 'indeterminate',
1015
- reason: `Process ${handle.identity.pid} timed out and root process could not be confirmed exited within ${rootExitWaitMs}ms bound: ${stopRes.errorDetails || 'root process still alive'}`,
1016
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
1017
- durationMs: Date.now() - startTime,
1018
- completedAt: new Date().toISOString(),
1019
- };
1020
- return this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
1021
- }
1022
- exitResult = rootExitOrTimeout.res;
1023
- }
1024
- else {
1025
- exitResult = exitOrTimeout.res;
1026
- }
1027
- // 有界等待流排空
1028
- const drainPromise = Promise.all([stdoutDrainer.finishPromise, stderrDrainer.finishPromise]);
1029
- const drainTimeoutPromise = new Promise((resolve) => setTimeout(resolve, drainTimeoutMs));
1030
- let drained = await Promise.race([
1031
- drainPromise.then(() => true),
1032
- drainTimeoutPromise.then(() => false),
1033
- ]);
1034
- // 排空超时说明根进程已退出但仍有后代持有管道:回收整个进程组,
1035
- // 否则操作会一直挂到超时,且 root 的真实退出事实会被超时掩盖。
1036
- let residualProcessesReaped = false;
1037
- if (!drained && exitOrTimeout.type === 'exit') {
1038
- const reapResult = await this.driver.terminate(handle.identity, 1500);
1039
- if (reapResult.stopped !== 'confirmed_stopped') {
1040
- const indetResult = {
1041
- kind: 'indeterminate',
1042
- status: 'indeterminate',
1043
- reason: `Process ${handle.identity.pid} exited but residual descendants could not be confirmed stopped: ${reapResult.errorDetails}`,
1044
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
1045
- durationMs: Date.now() - startTime,
1046
- completedAt: new Date().toISOString(),
1047
- };
1048
- return this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
1049
- }
1050
- residualProcessesReaped = true;
1051
- // 被杀死的后代释放管道后,给排空最后一次机会
1052
- drained = await Promise.race([
1053
- drainPromise.then(() => true),
1054
- new Promise((resolve) => setTimeout(() => resolve(false), 250)),
1055
- ]);
1056
- }
1057
- if (!drained) {
1058
- // 排空超时路径强制调用 fsyncSync + closeSync 结清刷盘
1059
- stdoutDrainer.forceFinalize();
1060
- stderrDrainer.forceFinalize();
1061
- // 走到这里时进程组已被确认停止,管道写端却仍被持有:
1062
- // 必然存在一个驱动未观测到的逃逸进程(例如 setsid 后被 init 收养、采样器没来得及记录)。
1063
- // 无法确认隔离,不能报告 succeeded / residualProcessesReaped,必须如实判 indeterminate 并保留租约。
1064
- const indetResult = {
1065
- kind: 'indeterminate',
1066
- status: 'indeterminate',
1067
- reason: `Process ${handle.identity.pid} exited (exitCode=${exitResult.exitCode}, signal=${exitResult.signal}) and its process group was confirmed stopped, but stdout/stderr pipes are still held by an unobserved escaped process`,
1068
- recoveryGuidance: 'An escaped process outside the observed process tree still holds the output pipes. Inspect system processes manually before adjudicating and releasing resources.',
1069
- capabilityId: options.capabilityId,
1070
- durationMs: Date.now() - startTime,
1071
- completedAt: new Date().toISOString(),
1072
- };
1073
- return this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
1074
- }
1075
- const stdoutData = stdoutDrainer.getResult();
1076
- const stderrData = stderrDrainer.getResult();
1077
- const isTruncated = stdoutData.isTruncated || stderrData.isTruncated;
1078
- const spillError = stdoutData.spillError || stderrData.spillError || artifactsDirError;
1079
- const outputRef = (!spillError && (stdoutData.outputRef || stderrData.outputRef)) || undefined;
1080
- const outputHash = (!spillError && (stdoutData.outputHash || stderrData.outputHash)) || undefined;
1081
- // 校验进程身份
1082
- const idVerify = await this.driver.verifyIdentity(handle.identity);
1083
- const isResourceOrTimeoutStopped = opState.timedOut ||
1084
- opState.terminationReason === 'memory_exceeded' ||
1085
- opState.terminationReason === 'cpu_exceeded' ||
1086
- opState.terminationReason === 'pids_exceeded' ||
1087
- opState.terminationReason === 'output_exceeded';
1088
- const isSucceeded = exitResult.exitCode === 0 &&
1089
- !isResourceOrTimeoutStopped &&
1090
- !opState.cancelRequested;
1091
- const finalStatus = isResourceOrTimeoutStopped
1092
- ? 'failed'
1093
- : opState.cancelRequested
1094
- ? 'cancelled'
1095
- : isSucceeded
1096
- ? 'succeeded'
1097
- : 'failed';
1098
- const terminationReason = opState.terminationReason || (finalStatus === 'succeeded' ? 'completed' : undefined);
1099
- const result = {
1100
- kind: 'process',
1101
- status: finalStatus,
1102
- exitCode: exitResult.exitCode,
1103
- signal: exitResult.signal,
1104
- stdout: stdoutData.content,
1105
- stderr: stderrData.content,
1106
- capabilityId: options.capabilityId,
1107
- confined: isConfined,
1108
- confinementDriver: confinementDriverName,
1109
- isTruncated,
1110
- stdoutTruncated: stdoutData.isTruncated,
1111
- stderrTruncated: stderrData.isTruncated,
1112
- stdoutRef: !stdoutData.spillError ? stdoutData.outputRef : undefined,
1113
- stderrRef: !stderrData.spillError ? stderrData.outputRef : undefined,
1114
- stdoutBytes: stdoutData.bytesSeen,
1115
- stderrBytes: stderrData.bytesSeen,
1116
- stdoutHash: !stdoutData.spillError ? stdoutData.outputHash : undefined,
1117
- stderrHash: !stderrData.spillError ? stderrData.outputHash : undefined,
1118
- ...(residualProcessesReaped ? { residualProcessesReaped: true } : {}),
1119
- ...(streamCallbackError ? { streamCallbackError } : {}),
1120
- ...(spillError ? { spillError } : {}),
1121
- ...(stdoutData.spillError ? { stdoutSpillError: stdoutData.spillError } : {}),
1122
- ...(stderrData.spillError ? { stderrSpillError: stderrData.spillError } : {}),
1123
- outputRef,
1124
- outputHash,
1125
- terminationReason,
1126
- peakMemoryBytes,
1127
- cpuTimeMs: peakCpuTimeMs,
1128
- identityVerification: idVerify,
1129
- durationMs: Date.now() - startTime,
1130
- completedAt: new Date().toISOString(),
1131
- };
1132
- if (opState.stopPromise) {
1133
- const stopRes = await opState.stopPromise;
1134
- if (stopRes.stopped !== 'confirmed_stopped') {
1135
- const stored = this.domain.getStore().getOperation(options.opId);
1136
- if (stored && stored.status === 'done') {
1137
- return (stored.result ?? null);
1138
- }
1139
- const indetResult = {
1140
- kind: 'indeterminate',
1141
- status: 'indeterminate',
1142
- reason: `Process ${handle.identity.pid} was cancelled or stopped but could not be confirmed: ${stopRes.errorDetails || 'residual processes still alive'}`,
1143
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
1144
- capabilityId: options.capabilityId,
1145
- durationMs: Date.now() - startTime,
1146
- completedAt: new Date().toISOString(),
1147
- };
1148
- return this.finalizeOperation(options.opId, indetResult, options.requiredResources, false);
1149
- }
1150
- }
1151
- // 6. [事务提交结果与释放资源 - Single Writer]
1152
- // 统一由 finalizeOperation 写入事实与处理资源
1153
- return this.finalizeOperation(options.opId, result, options.requiredResources, true, artifactsDir);
1154
- }
1155
- finally {
1156
- stdoutDrainer?.forceFinalize();
1157
- stderrDrainer?.forceFinalize();
1158
- if (timeoutTimer)
1159
- clearTimeout(timeoutTimer);
1160
- if (samplingInterval)
1161
- clearInterval(samplingInterval);
1162
- if (opState.stopResolve) {
1163
- const resolveFn = opState.stopResolve;
1164
- opState.stopResolve = undefined;
1165
- resolveFn({
1166
- stopped: 'confirmed_stopped',
1167
- scope: 'direct_child',
1168
- errorDetails: opState.phase === 'done' ? undefined : 'Operation terminated before process activation',
1169
- });
1170
- }
1171
- this.activeOperations.delete(options.opId);
1172
- }
1173
- })();
1174
- opState.resultPromise = executionPromise;
1175
- return await executionPromise;
218
+ return opState;
1176
219
  }
1177
220
  /**
1178
221
  * 停止确认流水线 (Stopping Pipeline)
@@ -1222,15 +265,11 @@ export class ProcessSupervisor {
1222
265
  if (stopResult.stopped !== 'confirmed_stopped') {
1223
266
  const op = this.domain.getStore().getOperation(opId);
1224
267
  if (op && op.status !== 'done') {
1225
- const indetResult = {
1226
- kind: 'indeterminate',
1227
- status: 'indeterminate',
268
+ const indet = indeterminateResult({
1228
269
  reason: `Process ${active.handle.identity.pid} could not be confirmed stopped: ${stopResult.errorDetails}`,
1229
- recoveryGuidance: 'Residual PID detected. Inspect system processes manually before releasing resources.',
1230
- durationMs: Date.now() - active.startTime,
1231
- completedAt: new Date().toISOString(),
1232
- };
1233
- this.finalizeOperation(opId, indetResult, undefined, false);
270
+ startTime: active.startTime,
271
+ });
272
+ finalizeOperation(this.domain, opId, indet, { releaseResourceLock: false });
1234
273
  }
1235
274
  }
1236
275
  existingResolve?.(stopResult);
@@ -1256,97 +295,12 @@ export class ProcessSupervisor {
1256
295
  }
1257
296
  return active;
1258
297
  }
1259
- handlePreSpawnCancellation(options, opState, startTime) {
1260
- opState.phase = 'done';
1261
- const stopRes = { stopped: 'confirmed_stopped', scope: 'direct_child' };
1262
- opState.stopResolve?.(stopRes);
1263
- opState.stopResolve = undefined;
1264
- const cancelResult = {
1265
- kind: 'process',
1266
- status: 'cancelled',
1267
- exitCode: null,
1268
- signal: null,
1269
- stdout: '',
1270
- stderr: '',
1271
- capabilityId: options.capabilityId,
1272
- isTruncated: false,
1273
- terminationReason: 'user_cancelled',
1274
- evidence: 'unobserved',
1275
- identityVerification: 'not_original_process',
1276
- durationMs: Date.now() - startTime,
1277
- completedAt: new Date().toISOString(),
1278
- };
1279
- const stored = this.domain.getStore().getOperation(options.opId);
1280
- if (!stored) {
1281
- // 此时操作是在 waiting_resources 阶段就被取消,尚未获取资源租约,亦未正常注册意图。
1282
- // 为保证 store 中有该操作记录以便写入 cancelled 结果,直接记录预意图取消操作,
1283
- // 绝不向 resource_leases 写入虚假租约,亦不产生虚假的 OPERATION_INTENT_REGISTERED 事件。
1284
- const op = {
1285
- id: options.opId,
1286
- runId: options.runId,
1287
- kind: 'process',
1288
- name: options.name,
1289
- inputFingerprint: options.inputFingerprint || computeInputFingerprint(options),
1290
- requiredResources: options.requiredResources || [],
1291
- status: 'done',
1292
- };
1293
- this.domain.getStore().recordPreIntentCancelledOperation(op, this.domain.domainId, cancelResult);
1294
- this.domain.internalReleaseResources(options.opId, options.requiredResources);
1295
- return cancelResult;
1296
- }
1297
- return this.finalizeOperation(options.opId, cancelResult, options.requiredResources, true);
1298
- }
1299
298
  pruneArtifacts(filter) {
1300
299
  return this.domain.pruneArtifacts(filter);
1301
300
  }
301
+ /** 保留原签名:共享契约与单写者测试直接调用它验证「重复终结必须抛错」。 */
1302
302
  finalizeOperation(opId, result, requiredResources, releaseResourceLock = true, artifactsDir) {
1303
- const existing = this.domain.getStore().getOperation(opId);
1304
- if (existing && existing.status === 'done') {
1305
- throw new Error(`Cannot finalize operation "${opId}": operation is already finalized with status "${existing.status}"`);
1306
- }
1307
- if (existing) {
1308
- result.runId = existing.runId;
1309
- if (existing.capabilityId && !result.capabilityId) {
1310
- result.capabilityId = existing.capabilityId;
1311
- }
1312
- }
1313
- if (result.kind === 'process') {
1314
- const procRes = result;
1315
- const targetDir = artifactsDir || path.join(this.domain.domainPath, 'artifacts');
1316
- const stdoutSpillPath = path.join(targetDir, `${opId}-stdout.log`);
1317
- const stderrSpillPath = path.join(targetDir, `${opId}-stderr.log`);
1318
- if (!procRes.stdoutRef && fs.existsSync(stdoutSpillPath)) {
1319
- try {
1320
- fs.unlinkSync(stdoutSpillPath);
1321
- }
1322
- catch (unlinkErr) {
1323
- procRes.spillError = procRes.spillError || unlinkErr.message || String(unlinkErr);
1324
- }
1325
- }
1326
- if (!procRes.stderrRef && fs.existsSync(stderrSpillPath)) {
1327
- try {
1328
- fs.unlinkSync(stderrSpillPath);
1329
- }
1330
- catch (unlinkErr) {
1331
- procRes.spillError = procRes.spillError || unlinkErr.message || String(unlinkErr);
1332
- }
1333
- }
1334
- }
1335
- if (result.status === 'indeterminate') {
1336
- this.domain.getStore().recordOperationResult(opId, result, false);
1337
- const op = this.domain.getStore().getOperation(opId);
1338
- if (op) {
1339
- const termReason = result.terminationReason;
1340
- this.domain.getStore().updateRunStatus(op.runId, 'indeterminate', termReason);
1341
- }
1342
- }
1343
- else {
1344
- this.domain.getStore().recordOperationResult(opId, result, true);
1345
- if (releaseResourceLock) {
1346
- this.domain.internalReleaseResources(opId, requiredResources);
1347
- }
1348
- }
1349
- return result;
303
+ return finalizeOperation(this.domain, opId, result, { requiredResources, releaseResourceLock, artifactsDir });
1350
304
  }
1351
305
  }
1352
306
  //# sourceMappingURL=supervisor.js.map