@xioflow/kernel 0.3.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.
- package/README.md +174 -15
- package/dist/capability/check.d.ts +32 -0
- package/dist/capability/check.d.ts.map +1 -0
- package/dist/capability/check.js +136 -0
- package/dist/capability/check.js.map +1 -0
- package/dist/capability/index.d.ts +3 -0
- package/dist/capability/index.d.ts.map +1 -0
- package/dist/capability/index.js +3 -0
- package/dist/capability/index.js.map +1 -0
- package/dist/capability/path-utils.d.ts +11 -0
- package/dist/capability/path-utils.d.ts.map +1 -0
- package/dist/capability/path-utils.js +46 -0
- package/dist/capability/path-utils.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +94 -0
- package/dist/cli.js.map +1 -0
- package/dist/confinement/bubblewrap.d.ts +8 -0
- package/dist/confinement/bubblewrap.d.ts.map +1 -0
- package/dist/confinement/bubblewrap.js +45 -0
- package/dist/confinement/bubblewrap.js.map +1 -0
- package/dist/confinement/detector.d.ts +4 -0
- package/dist/confinement/detector.d.ts.map +1 -0
- package/dist/confinement/detector.js +35 -0
- package/dist/confinement/detector.js.map +1 -0
- package/dist/confinement/index.d.ts +5 -0
- package/dist/confinement/index.d.ts.map +1 -0
- package/dist/confinement/index.js +5 -0
- package/dist/confinement/index.js.map +1 -0
- package/dist/confinement/sandbox-exec.d.ts +8 -0
- package/dist/confinement/sandbox-exec.d.ts.map +1 -0
- package/dist/confinement/sandbox-exec.js +30 -0
- package/dist/confinement/sandbox-exec.js.map +1 -0
- package/dist/confinement/srt.d.ts +8 -0
- package/dist/confinement/srt.d.ts.map +1 -0
- package/dist/confinement/srt.js +41 -0
- package/dist/confinement/srt.js.map +1 -0
- package/dist/domain.d.ts +26 -2
- package/dist/domain.d.ts.map +1 -1
- package/dist/domain.js +192 -18
- package/dist/domain.js.map +1 -1
- package/dist/driver/node-driver.d.ts.map +1 -1
- package/dist/driver/node-driver.js +42 -61
- package/dist/driver/node-driver.js.map +1 -1
- package/dist/driver/reaper-driver.d.ts +62 -0
- package/dist/driver/reaper-driver.d.ts.map +1 -0
- package/dist/driver/reaper-driver.js +371 -0
- package/dist/driver/reaper-driver.js.map +1 -0
- package/dist/driver/spawn-support.d.ts +4 -0
- package/dist/driver/spawn-support.d.ts.map +1 -0
- package/dist/driver/spawn-support.js +31 -0
- package/dist/driver/spawn-support.js.map +1 -0
- package/dist/driver/types.d.ts +19 -2
- package/dist/driver/types.d.ts.map +1 -1
- package/dist/fault/crashpoint.d.ts +8 -0
- package/dist/fault/crashpoint.d.ts.map +1 -0
- package/dist/fault/crashpoint.js +37 -0
- package/dist/fault/crashpoint.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/mcp/server.d.ts +49 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +233 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools.d.ts +32 -0
- package/dist/mcp/tools.d.ts.map +1 -0
- package/dist/mcp/tools.js +255 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/native/darwin-arm64/xioflow-reaper +0 -0
- package/dist/native/darwin-x64/xioflow-reaper +0 -0
- package/dist/native/linux-arm64/xioflow-reaper +0 -0
- package/dist/native/linux-x64/xioflow-reaper +0 -0
- package/dist/otel/otlp.d.ts +37 -0
- package/dist/otel/otlp.d.ts.map +1 -0
- package/dist/otel/otlp.js +224 -0
- package/dist/otel/otlp.js.map +1 -0
- package/dist/quick-run.d.ts.map +1 -1
- package/dist/quick-run.js +5 -12
- package/dist/quick-run.js.map +1 -1
- package/dist/recovery/engine.d.ts +7 -0
- package/dist/recovery/engine.d.ts.map +1 -1
- package/dist/recovery/engine.js +176 -4
- package/dist/recovery/engine.js.map +1 -1
- package/dist/snapshot/git-shadow.d.ts +57 -0
- package/dist/snapshot/git-shadow.d.ts.map +1 -0
- package/dist/snapshot/git-shadow.js +364 -0
- package/dist/snapshot/git-shadow.js.map +1 -0
- package/dist/store/schema.d.ts +1 -1
- package/dist/store/schema.d.ts.map +1 -1
- package/dist/store/schema.js +31 -0
- package/dist/store/schema.js.map +1 -1
- package/dist/store/sqlite.d.ts +14 -1
- package/dist/store/sqlite.d.ts.map +1 -1
- package/dist/store/sqlite.js +150 -58
- package/dist/store/sqlite.js.map +1 -1
- package/dist/supervisor/admission.d.ts +26 -0
- package/dist/supervisor/admission.d.ts.map +1 -0
- package/dist/supervisor/admission.js +98 -0
- package/dist/supervisor/admission.js.map +1 -0
- package/dist/supervisor/drainer.d.ts +17 -0
- package/dist/supervisor/drainer.d.ts.map +1 -0
- package/dist/supervisor/drainer.js +200 -0
- package/dist/supervisor/drainer.js.map +1 -0
- package/dist/supervisor/finalize.d.ts +21 -0
- package/dist/supervisor/finalize.d.ts.map +1 -0
- package/dist/supervisor/finalize.js +66 -0
- package/dist/supervisor/finalize.js.map +1 -0
- package/dist/supervisor/fingerprint.d.ts +8 -0
- package/dist/supervisor/fingerprint.d.ts.map +1 -0
- package/dist/supervisor/fingerprint.js +54 -0
- package/dist/supervisor/fingerprint.js.map +1 -0
- package/dist/supervisor/process-monitor.d.ts +9 -0
- package/dist/supervisor/process-monitor.d.ts.map +1 -0
- package/dist/supervisor/process-monitor.js +230 -0
- package/dist/supervisor/process-monitor.js.map +1 -0
- package/dist/supervisor/process-output.d.ts +27 -0
- package/dist/supervisor/process-output.d.ts.map +1 -0
- package/dist/supervisor/process-output.js +102 -0
- package/dist/supervisor/process-output.js.map +1 -0
- package/dist/supervisor/process-run.d.ts +30 -0
- package/dist/supervisor/process-run.d.ts.map +1 -0
- package/dist/supervisor/process-run.js +191 -0
- package/dist/supervisor/process-run.js.map +1 -0
- package/dist/supervisor/replay.d.ts +14 -0
- package/dist/supervisor/replay.d.ts.map +1 -0
- package/dist/supervisor/replay.js +70 -0
- package/dist/supervisor/replay.js.map +1 -0
- package/dist/supervisor/rollback.d.ts +8 -0
- package/dist/supervisor/rollback.d.ts.map +1 -0
- package/dist/supervisor/rollback.js +153 -0
- package/dist/supervisor/rollback.js.map +1 -0
- package/dist/supervisor/service.d.ts +24 -0
- package/dist/supervisor/service.d.ts.map +1 -0
- package/dist/supervisor/service.js +588 -0
- package/dist/supervisor/service.js.map +1 -0
- package/dist/supervisor/snapshot-ops.d.ts +36 -0
- package/dist/supervisor/snapshot-ops.d.ts.map +1 -0
- package/dist/supervisor/snapshot-ops.js +243 -0
- package/dist/supervisor/snapshot-ops.js.map +1 -0
- package/dist/supervisor/supervisor.d.ts +62 -30
- package/dist/supervisor/supervisor.d.ts.map +1 -1
- package/dist/supervisor/supervisor.js +164 -842
- package/dist/supervisor/supervisor.js.map +1 -1
- package/dist/supervisor/types.d.ts +64 -0
- package/dist/supervisor/types.d.ts.map +1 -0
- package/dist/supervisor/types.js +2 -0
- package/dist/supervisor/types.js.map +1 -0
- package/dist/testing/contract-suite.d.ts.map +1 -1
- package/dist/testing/contract-suite.js +619 -12
- package/dist/testing/contract-suite.js.map +1 -1
- package/dist/types.d.ts +161 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -1
- package/dist/workspace/read-tracking.d.ts +23 -0
- package/dist/workspace/read-tracking.d.ts.map +1 -0
- package/dist/workspace/read-tracking.js +66 -0
- package/dist/workspace/read-tracking.js.map +1 -0
- package/dist/workspace/transactions.d.ts +89 -0
- package/dist/workspace/transactions.d.ts.map +1 -0
- package/dist/workspace/transactions.js +277 -0
- package/dist/workspace/transactions.js.map +1 -0
- package/package.json +8 -2
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ManagedProcessHandle } from '../driver/types.js';
|
|
2
|
+
import { ProcessOperationResult } from '../types.js';
|
|
3
|
+
import { StreamDrainer } from './drainer.js';
|
|
4
|
+
import type { ProcessRunContext, ProcessRunPlan } from './process-run.js';
|
|
5
|
+
export type ExitFacts = {
|
|
6
|
+
exitCode: number | null;
|
|
7
|
+
signal: NodeJS.Signals | null;
|
|
8
|
+
};
|
|
9
|
+
export interface OutputCapture {
|
|
10
|
+
stdout: StreamDrainer;
|
|
11
|
+
stderr: StreamDrainer;
|
|
12
|
+
artifactsDir: string;
|
|
13
|
+
artifactsDirError?: string;
|
|
14
|
+
callbackError(): string | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** 准备 artifacts 溢出目录,挂上有界排空器;chunk 同时计入输出预算并转发给流订阅者。 */
|
|
17
|
+
export declare function openOutputCapture(ctx: ProcessRunContext, plan: ProcessRunPlan, handle: ManagedProcessHandle): OutputCapture;
|
|
18
|
+
export declare function buildProcessResult(plan: ProcessRunPlan, capture: OutputCapture, facts: {
|
|
19
|
+
exit: ExitFacts;
|
|
20
|
+
residualProcessesReaped: boolean;
|
|
21
|
+
peaks: {
|
|
22
|
+
peakMemoryBytes: number;
|
|
23
|
+
peakCpuTimeMs: number;
|
|
24
|
+
};
|
|
25
|
+
identityVerification: ProcessOperationResult['identityVerification'];
|
|
26
|
+
}): ProcessOperationResult;
|
|
27
|
+
//# sourceMappingURL=process-output.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-output.d.ts","sourceRoot":"","sources":["../../src/supervisor/process-output.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAsB,aAAa,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAE1E,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,IAAI,CAAA;CAAE,CAAC;AAEnF,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,aAAa,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,aAAa,IAAI,MAAM,GAAG,SAAS,CAAC;CACrC;AAED,wDAAwD;AACxD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,oBAAoB,GAAG,aAAa,CAuD3H;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,aAAa,EACtB,KAAK,EAAE;IACL,IAAI,EAAE,SAAS,CAAC;IAChB,uBAAuB,EAAE,OAAO,CAAC;IACjC,KAAK,EAAE;QAAE,eAAe,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1D,oBAAoB,EAAE,sBAAsB,CAAC,sBAAsB,CAAC,CAAC;CACtE,GACA,sBAAsB,CAsDxB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { setupStreamDrainer } from './drainer.js';
|
|
4
|
+
/** 准备 artifacts 溢出目录,挂上有界排空器;chunk 同时计入输出预算并转发给流订阅者。 */
|
|
5
|
+
export function openOutputCapture(ctx, plan, handle) {
|
|
6
|
+
const { options, opState } = plan;
|
|
7
|
+
const artifactsDir = options.artifactsDir || path.join(ctx.domain.domainPath, 'artifacts');
|
|
8
|
+
let artifactsDirError;
|
|
9
|
+
if (!fs.existsSync(artifactsDir)) {
|
|
10
|
+
try {
|
|
11
|
+
fs.mkdirSync(artifactsDir, { recursive: true });
|
|
12
|
+
}
|
|
13
|
+
catch (dirErr) {
|
|
14
|
+
artifactsDirError = dirErr.message || String(dirErr);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let totalOutputBytes = 0;
|
|
18
|
+
const onChunk = (bytes) => {
|
|
19
|
+
totalOutputBytes += bytes;
|
|
20
|
+
const budget = options.resourceBudget;
|
|
21
|
+
if (budget?.enforcement === 'soft' &&
|
|
22
|
+
budget.maxOutputBytes &&
|
|
23
|
+
totalOutputBytes > budget.maxOutputBytes &&
|
|
24
|
+
!opState.terminationReason) {
|
|
25
|
+
opState.terminationReason = 'output_exceeded';
|
|
26
|
+
ctx.stop(options.opId, 'output_exceeded', 1000).catch(() => { });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
let streamCallbackError;
|
|
30
|
+
const forwardChunk = (stream) => (chunk) => {
|
|
31
|
+
for (const subscriber of opState.streamSubscribers) {
|
|
32
|
+
try {
|
|
33
|
+
subscriber(stream, chunk);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
streamCallbackError ??= err instanceof Error ? err.message : String(err);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const maxBytes = options.maxOutputBytes ?? 10 * 1024 * 1024; // 默认 10MB
|
|
41
|
+
const drainer = (stream) => setupStreamDrainer(handle[stream], maxBytes, path.join(artifactsDir, `${options.opId}-${stream}.log`), onChunk, forwardChunk(stream), artifactsDirError);
|
|
42
|
+
return {
|
|
43
|
+
stdout: drainer('stdout'),
|
|
44
|
+
stderr: drainer('stderr'),
|
|
45
|
+
artifactsDir,
|
|
46
|
+
artifactsDirError,
|
|
47
|
+
callbackError: () => streamCallbackError,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function buildProcessResult(plan, capture, facts) {
|
|
51
|
+
const { options, opState } = plan;
|
|
52
|
+
const stdoutData = capture.stdout.getResult();
|
|
53
|
+
const stderrData = capture.stderr.getResult();
|
|
54
|
+
const spillError = stdoutData.spillError || stderrData.spillError || capture.artifactsDirError;
|
|
55
|
+
const streamCallbackError = capture.callbackError();
|
|
56
|
+
const isResourceOrTimeoutStopped = opState.timedOut ||
|
|
57
|
+
opState.terminationReason === 'memory_exceeded' ||
|
|
58
|
+
opState.terminationReason === 'cpu_exceeded' ||
|
|
59
|
+
opState.terminationReason === 'pids_exceeded' ||
|
|
60
|
+
opState.terminationReason === 'output_exceeded';
|
|
61
|
+
const finalStatus = isResourceOrTimeoutStopped
|
|
62
|
+
? 'failed'
|
|
63
|
+
: opState.cancelRequested
|
|
64
|
+
? 'cancelled'
|
|
65
|
+
: facts.exit.exitCode === 0
|
|
66
|
+
? 'succeeded'
|
|
67
|
+
: 'failed';
|
|
68
|
+
return {
|
|
69
|
+
kind: 'process',
|
|
70
|
+
status: finalStatus,
|
|
71
|
+
exitCode: facts.exit.exitCode,
|
|
72
|
+
signal: facts.exit.signal,
|
|
73
|
+
stdout: stdoutData.content,
|
|
74
|
+
stderr: stderrData.content,
|
|
75
|
+
capabilityId: options.capabilityId,
|
|
76
|
+
confined: plan.confined,
|
|
77
|
+
confinementDriver: plan.confinementDriverName,
|
|
78
|
+
isTruncated: stdoutData.isTruncated || stderrData.isTruncated,
|
|
79
|
+
stdoutTruncated: stdoutData.isTruncated,
|
|
80
|
+
stderrTruncated: stderrData.isTruncated,
|
|
81
|
+
stdoutRef: !stdoutData.spillError ? stdoutData.outputRef : undefined,
|
|
82
|
+
stderrRef: !stderrData.spillError ? stderrData.outputRef : undefined,
|
|
83
|
+
stdoutBytes: stdoutData.bytesSeen,
|
|
84
|
+
stderrBytes: stderrData.bytesSeen,
|
|
85
|
+
stdoutHash: !stdoutData.spillError ? stdoutData.outputHash : undefined,
|
|
86
|
+
stderrHash: !stderrData.spillError ? stderrData.outputHash : undefined,
|
|
87
|
+
...(facts.residualProcessesReaped ? { residualProcessesReaped: true } : {}),
|
|
88
|
+
...(streamCallbackError ? { streamCallbackError } : {}),
|
|
89
|
+
...(spillError ? { spillError } : {}),
|
|
90
|
+
...(stdoutData.spillError ? { stdoutSpillError: stdoutData.spillError } : {}),
|
|
91
|
+
...(stderrData.spillError ? { stderrSpillError: stderrData.spillError } : {}),
|
|
92
|
+
outputRef: (!spillError && (stdoutData.outputRef || stderrData.outputRef)) || undefined,
|
|
93
|
+
outputHash: (!spillError && (stdoutData.outputHash || stderrData.outputHash)) || undefined,
|
|
94
|
+
terminationReason: opState.terminationReason || (finalStatus === 'succeeded' ? 'completed' : undefined),
|
|
95
|
+
peakMemoryBytes: facts.peaks.peakMemoryBytes,
|
|
96
|
+
cpuTimeMs: facts.peaks.peakCpuTimeMs,
|
|
97
|
+
identityVerification: facts.identityVerification,
|
|
98
|
+
durationMs: Date.now() - opState.startTime,
|
|
99
|
+
completedAt: new Date().toISOString(),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=process-output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-output.js","sourceRoot":"","sources":["../../src/supervisor/process-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,kBAAkB,EAAiB,MAAM,cAAc,CAAC;AAajE,wDAAwD;AACxD,MAAM,UAAU,iBAAiB,CAAC,GAAsB,EAAE,IAAoB,EAAE,MAA4B;IAC1G,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAClC,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;IAC3F,IAAI,iBAAqC,CAAC;IAC1C,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;QACjC,IAAI,CAAC;YACH,EAAE,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;QAAC,OAAO,MAAW,EAAE,CAAC;YACrB,iBAAiB,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED,IAAI,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,CAAC,EAAE,gBAAgB,EAAE,UAAU,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7E,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,UAAU,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7E,SAAS,EAAE,CAAC,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,SAAS,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC,IAAI,SAAS;QACvF,UAAU,EAAE,CAAC,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,IAAI,SAAS;QAC1F,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,IAAI,CAAC,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC;QACvG,eAAe,EAAE,KAAK,CAAC,KAAK,CAAC,eAAe;QAC5C,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa;QACpC,oBAAoB,EAAE,KAAK,CAAC,oBAAoB;QAChD,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS;QAC1C,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACtC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ExecutionDomain } from '../domain.js';
|
|
2
|
+
import { PlatformDriver, StopProcessResult, StructuredCommand } from '../driver/types.js';
|
|
3
|
+
import { Operation, ProcessOperationResult, TerminationReason } from '../types.js';
|
|
4
|
+
import { CapabilityAdmission } from './admission.js';
|
|
5
|
+
import { ActiveOperationState, ExecuteProcessOptions } from './types.js';
|
|
6
|
+
/** 执行期需要回调监督器的能力:停止流水线与在飞表清理都归监督器所有。 */
|
|
7
|
+
export interface ProcessRunContext {
|
|
8
|
+
domain: ExecutionDomain;
|
|
9
|
+
driver: PlatformDriver;
|
|
10
|
+
stop(opId: string, reason: TerminationReason, graceMs: number): Promise<StopProcessResult>;
|
|
11
|
+
forget(opId: string): void;
|
|
12
|
+
}
|
|
13
|
+
export interface ProcessRunPlan {
|
|
14
|
+
options: ExecuteProcessOptions;
|
|
15
|
+
op: Operation;
|
|
16
|
+
opState: ActiveOperationState;
|
|
17
|
+
/** 已按写入限制包装后的实际启动命令。 */
|
|
18
|
+
command: StructuredCommand;
|
|
19
|
+
confined: boolean;
|
|
20
|
+
confinementDriverName?: string;
|
|
21
|
+
capAdmission?: CapabilityAdmission;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 启动协议:资源仲裁 -> 先持久化意图 -> 请求驱动启动 -> 登记身份并转 active -> 监督运行 -> 记录结果。
|
|
25
|
+
* 任何出口都会结清 stopResolve 并把操作移出在飞表。
|
|
26
|
+
*/
|
|
27
|
+
export declare function runProcess(ctx: ProcessRunContext, plan: ProcessRunPlan): Promise<ProcessOperationResult>;
|
|
28
|
+
/** 进程启动前被取消:记录 cancelled(evidence: unobserved),不写虚假租约或意图事件。 */
|
|
29
|
+
export declare function cancelBeforeSpawn(domain: ExecutionDomain, options: ExecuteProcessOptions, opState: ActiveOperationState): ProcessOperationResult;
|
|
30
|
+
//# sourceMappingURL=process-run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-run.d.ts","sourceRoot":"","sources":["../../src/supervisor/process-run.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAwB,cAAc,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAChH,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACnF,OAAO,EAAE,mBAAmB,EAAwB,MAAM,gBAAgB,CAAC;AAI3E,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEzE,wCAAwC;AACxC,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,EAAE,cAAc,CAAC;IACvB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC3F,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,qBAAqB,CAAC;IAC/B,EAAE,EAAE,SAAS,CAAC;IACd,OAAO,EAAE,oBAAoB,CAAC;IAC9B,wBAAwB;IACxB,OAAO,EAAE,iBAAiB,CAAC;IAC3B,QAAQ,EAAE,OAAO,CAAC;IAClB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAyD9G;AAgGD,+DAA+D;AAC/D,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE,oBAAoB,GAC5B,sBAAsB,CA0CxB"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { recordCapabilityUsed } from './admission.js';
|
|
2
|
+
import { computeInputFingerprint } from './fingerprint.js';
|
|
3
|
+
import { finalizeOperation, indeterminateResult } from './finalize.js';
|
|
4
|
+
import { superviseActive } from './process-monitor.js';
|
|
5
|
+
/**
|
|
6
|
+
* 启动协议:资源仲裁 -> 先持久化意图 -> 请求驱动启动 -> 登记身份并转 active -> 监督运行 -> 记录结果。
|
|
7
|
+
* 任何出口都会结清 stopResolve 并把操作移出在飞表。
|
|
8
|
+
*/
|
|
9
|
+
export async function runProcess(ctx, plan) {
|
|
10
|
+
const { options, opState } = plan;
|
|
11
|
+
const cleanups = [];
|
|
12
|
+
try {
|
|
13
|
+
// 1. [资源分配与排队等待]
|
|
14
|
+
await ctx.domain.allocateResourcesWithWait(options.opId, options.requiredResources || [], options.waitTimeoutMs ?? 0, options.resourceBudget, () => opState.cancelRequested === true);
|
|
15
|
+
if (opState.cancelRequested) {
|
|
16
|
+
return cancelBeforeSpawn(ctx.domain, options, opState);
|
|
17
|
+
}
|
|
18
|
+
// 2. [启动协议步骤 1] 写入 SQLite (status: intent_registered)
|
|
19
|
+
try {
|
|
20
|
+
ctx.domain.getStore().registerOperationIntent(plan.op, ctx.domain.domainId);
|
|
21
|
+
opState.phase = 'intent_registered';
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
ctx.domain.internalReleaseResources(options.opId, options.requiredResources);
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
if (opState.cancelRequested) {
|
|
28
|
+
return cancelBeforeSpawn(ctx.domain, options, opState);
|
|
29
|
+
}
|
|
30
|
+
// 3. [启动协议步骤 2] 请求平台驱动启动
|
|
31
|
+
opState.phase = 'spawning';
|
|
32
|
+
let handle;
|
|
33
|
+
try {
|
|
34
|
+
handle = await ctx.driver.spawn(plan.command);
|
|
35
|
+
opState.handle = handle;
|
|
36
|
+
opState.command = plan.command;
|
|
37
|
+
}
|
|
38
|
+
catch (spawnError) {
|
|
39
|
+
return failSpawn(ctx.domain, plan, spawnError);
|
|
40
|
+
}
|
|
41
|
+
// 4. [启动协议步骤 3] 登记执行身份,状态推进为 active
|
|
42
|
+
await activateOrAbort(ctx, plan, handle);
|
|
43
|
+
// 5–6. 监督运行、有界排空、结果落盘
|
|
44
|
+
return await superviseActive(ctx, plan, handle, cleanups);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
for (const cleanup of cleanups)
|
|
48
|
+
cleanup();
|
|
49
|
+
if (opState.stopResolve) {
|
|
50
|
+
const resolveFn = opState.stopResolve;
|
|
51
|
+
opState.stopResolve = undefined;
|
|
52
|
+
resolveFn({
|
|
53
|
+
stopped: 'confirmed_stopped',
|
|
54
|
+
scope: 'direct_child',
|
|
55
|
+
errorDetails: opState.phase === 'done' ? undefined : 'Operation terminated before process activation',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
ctx.forget(options.opId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** 启动失败(如可执行文件不存在):不产生假运行状态,直接失败收尾并释放资源。 */
|
|
62
|
+
function failSpawn(domain, plan, spawnError) {
|
|
63
|
+
const { options, opState } = plan;
|
|
64
|
+
opState.phase = 'done';
|
|
65
|
+
opState.stopResolve?.({ stopped: 'confirmed_stopped', scope: 'direct_child', errorDetails: spawnError?.message });
|
|
66
|
+
opState.stopResolve = undefined;
|
|
67
|
+
const message = spawnError?.message || String(spawnError);
|
|
68
|
+
const failResult = {
|
|
69
|
+
kind: 'process',
|
|
70
|
+
status: 'failed',
|
|
71
|
+
exitCode: 127,
|
|
72
|
+
signal: null,
|
|
73
|
+
stdout: '',
|
|
74
|
+
stderr: message,
|
|
75
|
+
spawnFailure: message,
|
|
76
|
+
capabilityId: options.capabilityId,
|
|
77
|
+
confined: plan.confined,
|
|
78
|
+
confinementDriver: plan.confinementDriverName,
|
|
79
|
+
isTruncated: false,
|
|
80
|
+
identityVerification: 'not_original_process',
|
|
81
|
+
durationMs: Date.now() - opState.startTime,
|
|
82
|
+
completedAt: new Date().toISOString(),
|
|
83
|
+
};
|
|
84
|
+
return finalizeOperation(domain, options.opId, failResult, { requiredResources: options.requiredResources });
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 登记 active 并放行闸门。登记失败时(N4)进程已存在:先停掉它,再按停止是否确认
|
|
88
|
+
* 记为 failed 或 indeterminate,最后把原始登记错误抛给调用方,防止产生孤儿进程。
|
|
89
|
+
*/
|
|
90
|
+
async function activateOrAbort(ctx, plan, handle) {
|
|
91
|
+
const { options, opState } = plan;
|
|
92
|
+
try {
|
|
93
|
+
ctx.domain.getStore().updateOperationStatus(options.opId, 'active', handle.identity);
|
|
94
|
+
opState.phase = 'active';
|
|
95
|
+
handle.releaseGate?.();
|
|
96
|
+
if (options.capabilityId && plan.capAdmission) {
|
|
97
|
+
recordCapabilityUsed(ctx.domain, { runId: options.runId, opId: options.opId, capabilityId: options.capabilityId }, plan.capAdmission);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (statusError) {
|
|
101
|
+
handle.destroyGate?.();
|
|
102
|
+
opState.phase = 'stopping';
|
|
103
|
+
const stopRes = await ctx.driver.terminate(handle.identity, 2000);
|
|
104
|
+
// 有界等待退出与关闭流,防止管道悬挂
|
|
105
|
+
await Promise.race([
|
|
106
|
+
(handle.onRootExit ?? handle.onExit).catch(() => { }),
|
|
107
|
+
new Promise((resolve) => setTimeout(resolve, 500)),
|
|
108
|
+
]);
|
|
109
|
+
try {
|
|
110
|
+
handle.stdout.destroy?.();
|
|
111
|
+
handle.stderr.destroy?.();
|
|
112
|
+
}
|
|
113
|
+
catch { }
|
|
114
|
+
opState.phase = 'done';
|
|
115
|
+
if (opState.stopResolve) {
|
|
116
|
+
const resolveFn = opState.stopResolve;
|
|
117
|
+
opState.stopResolve = undefined;
|
|
118
|
+
resolveFn(stopRes);
|
|
119
|
+
}
|
|
120
|
+
const statusMessage = statusError?.message || String(statusError);
|
|
121
|
+
if (stopRes.stopped === 'confirmed_stopped') {
|
|
122
|
+
const failResult = {
|
|
123
|
+
kind: 'process',
|
|
124
|
+
status: 'failed',
|
|
125
|
+
exitCode: null,
|
|
126
|
+
signal: null,
|
|
127
|
+
stdout: '',
|
|
128
|
+
stderr: `Failed to register active status: ${statusMessage}`,
|
|
129
|
+
spawnFailure: statusMessage,
|
|
130
|
+
isTruncated: false,
|
|
131
|
+
identityVerification: 'not_original_process',
|
|
132
|
+
durationMs: Date.now() - opState.startTime,
|
|
133
|
+
completedAt: new Date().toISOString(),
|
|
134
|
+
};
|
|
135
|
+
finalizeOperation(ctx.domain, options.opId, failResult, { requiredResources: options.requiredResources });
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const indet = indeterminateResult({
|
|
139
|
+
reason: `Failed to register active status: ${statusMessage}, and process ${handle.identity.pid} could not be confirmed stopped: ${stopRes.errorDetails || 'residual processes still alive'}`,
|
|
140
|
+
startTime: opState.startTime,
|
|
141
|
+
});
|
|
142
|
+
finalizeOperation(ctx.domain, options.opId, indet, {
|
|
143
|
+
requiredResources: options.requiredResources,
|
|
144
|
+
releaseResourceLock: false,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
throw statusError;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** 进程启动前被取消:记录 cancelled(evidence: unobserved),不写虚假租约或意图事件。 */
|
|
151
|
+
export function cancelBeforeSpawn(domain, options, opState) {
|
|
152
|
+
opState.phase = 'done';
|
|
153
|
+
const stopRes = { stopped: 'confirmed_stopped', scope: 'direct_child' };
|
|
154
|
+
opState.stopResolve?.(stopRes);
|
|
155
|
+
opState.stopResolve = undefined;
|
|
156
|
+
const cancelResult = {
|
|
157
|
+
kind: 'process',
|
|
158
|
+
status: 'cancelled',
|
|
159
|
+
exitCode: null,
|
|
160
|
+
signal: null,
|
|
161
|
+
stdout: '',
|
|
162
|
+
stderr: '',
|
|
163
|
+
capabilityId: options.capabilityId,
|
|
164
|
+
isTruncated: false,
|
|
165
|
+
terminationReason: 'user_cancelled',
|
|
166
|
+
evidence: 'unobserved',
|
|
167
|
+
identityVerification: 'not_original_process',
|
|
168
|
+
durationMs: Date.now() - opState.startTime,
|
|
169
|
+
completedAt: new Date().toISOString(),
|
|
170
|
+
};
|
|
171
|
+
const stored = domain.getStore().getOperation(options.opId);
|
|
172
|
+
if (!stored) {
|
|
173
|
+
// 此时操作是在 waiting_resources 阶段就被取消,尚未获取资源租约,亦未正常注册意图。
|
|
174
|
+
// 为保证 store 中有该操作记录以便写入 cancelled 结果,直接记录预意图取消操作,
|
|
175
|
+
// 绝不向 resource_leases 写入虚假租约,亦不产生虚假的 OPERATION_INTENT_REGISTERED 事件。
|
|
176
|
+
const op = {
|
|
177
|
+
id: options.opId,
|
|
178
|
+
runId: options.runId,
|
|
179
|
+
kind: 'process',
|
|
180
|
+
name: options.name,
|
|
181
|
+
inputFingerprint: options.inputFingerprint || computeInputFingerprint(options),
|
|
182
|
+
requiredResources: options.requiredResources || [],
|
|
183
|
+
status: 'done',
|
|
184
|
+
};
|
|
185
|
+
domain.getStore().recordPreIntentCancelledOperation(op, domain.domainId, cancelResult);
|
|
186
|
+
domain.internalReleaseResources(options.opId, options.requiredResources);
|
|
187
|
+
return cancelResult;
|
|
188
|
+
}
|
|
189
|
+
return finalizeOperation(domain, options.opId, cancelResult, { requiredResources: options.requiredResources });
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=process-run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-run.js","sourceRoot":"","sources":["../../src/supervisor/process-run.ts"],"names":[],"mappings":"AAGA,OAAO,EAAuB,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAsBvD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,GAAsB,EAAE,IAAoB;IAC3E,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAClC,MAAM,QAAQ,GAAsB,EAAE,CAAC;IACvC,IAAI,CAAC;QACH,iBAAiB;QACjB,MAAM,GAAG,CAAC,MAAM,CAAC,yBAAyB,CACxC,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,iBAAiB,IAAI,EAAE,EAC/B,OAAO,CAAC,aAAa,IA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ProcessOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* 在飞操作重放(ARCHITECTURE §3.7 / 契约 #46):同指纹加入原执行并共享流投影,
|
|
6
|
+
* 不同指纹抛 OperationIdConflictError。abortSignal 只中止本次等待,不影响原操作。
|
|
7
|
+
*/
|
|
8
|
+
export declare function joinInFlight(domain: ExecutionDomain, inFlight: ActiveOperationState, options: ExecuteProcessOptions, inputFingerprint: string): Promise<ProcessOperationResult>;
|
|
9
|
+
/**
|
|
10
|
+
* 已持久化操作重放(ARCHITECTURE §3.7 / 契约 #45, #47, #48, #49)。
|
|
11
|
+
* 返回 undefined 表示该 opId 从未登记,调用方应正常执行。
|
|
12
|
+
*/
|
|
13
|
+
export declare function replayRecorded(domain: ExecutionDomain, options: ExecuteProcessOptions, inputFingerprint: string): ProcessOperationResult | undefined;
|
|
14
|
+
//# sourceMappingURL=replay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../../src/supervisor/replay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAA4B,sBAAsB,EAAyB,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEzE;;;GAGG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,oBAAoB,EAC9B,OAAO,EAAE,qBAAqB,EAC9B,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,sBAAsB,CAAC,CAiDjC;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,qBAAqB,EAC9B,gBAAgB,EAAE,MAAM,GACvB,sBAAsB,GAAG,SAAS,CA2BpC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { OperationIdConflictError, RecoveryRequiredError } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 在飞操作重放(ARCHITECTURE §3.7 / 契约 #46):同指纹加入原执行并共享流投影,
|
|
4
|
+
* 不同指纹抛 OperationIdConflictError。abortSignal 只中止本次等待,不影响原操作。
|
|
5
|
+
*/
|
|
6
|
+
export async function joinInFlight(domain, inFlight, options, inputFingerprint) {
|
|
7
|
+
if (inFlight.inputFingerprint !== inputFingerprint) {
|
|
8
|
+
throw new OperationIdConflictError(options.opId, inFlight.inputFingerprint, inputFingerprint, inFlight.phase, inFlight.runId);
|
|
9
|
+
}
|
|
10
|
+
domain.getStore().recordReplay(domain.domainId, options.opId, 'joined', options.runId);
|
|
11
|
+
const subscriber = options.onStreamChunk;
|
|
12
|
+
if (subscriber) {
|
|
13
|
+
inFlight.streamSubscribers.add(subscriber);
|
|
14
|
+
}
|
|
15
|
+
const unsubscribe = () => {
|
|
16
|
+
if (subscriber)
|
|
17
|
+
inFlight.streamSubscribers.delete(subscriber);
|
|
18
|
+
};
|
|
19
|
+
const signal = options.abortSignal;
|
|
20
|
+
if (!signal) {
|
|
21
|
+
const res = await inFlight.resultPromise;
|
|
22
|
+
unsubscribe();
|
|
23
|
+
return { ...res, replayed: true };
|
|
24
|
+
}
|
|
25
|
+
if (signal.aborted) {
|
|
26
|
+
unsubscribe();
|
|
27
|
+
throw signal.reason || new Error('Operation wait aborted');
|
|
28
|
+
}
|
|
29
|
+
return await new Promise((resolve, reject) => {
|
|
30
|
+
const onAbort = () => {
|
|
31
|
+
unsubscribe();
|
|
32
|
+
reject(signal.reason || new Error('Operation wait aborted'));
|
|
33
|
+
};
|
|
34
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
35
|
+
inFlight.resultPromise.then((res) => {
|
|
36
|
+
signal.removeEventListener('abort', onAbort);
|
|
37
|
+
unsubscribe();
|
|
38
|
+
resolve({ ...res, replayed: true });
|
|
39
|
+
}, (err) => {
|
|
40
|
+
signal.removeEventListener('abort', onAbort);
|
|
41
|
+
unsubscribe();
|
|
42
|
+
reject(err);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 已持久化操作重放(ARCHITECTURE §3.7 / 契约 #45, #47, #48, #49)。
|
|
48
|
+
* 返回 undefined 表示该 opId 从未登记,调用方应正常执行。
|
|
49
|
+
*/
|
|
50
|
+
export function replayRecorded(domain, options, inputFingerprint) {
|
|
51
|
+
const existingRecorded = domain.getStore().getOperation(options.opId);
|
|
52
|
+
if (!existingRecorded)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (existingRecorded.inputFingerprint !== inputFingerprint) {
|
|
55
|
+
throw new OperationIdConflictError(options.opId, existingRecorded.inputFingerprint, inputFingerprint, existingRecorded.status, existingRecorded.runId);
|
|
56
|
+
}
|
|
57
|
+
// 未终结状态且不在内存中 → 必须先执行恢复(崩溃残留现场)
|
|
58
|
+
if (existingRecorded.status !== 'done') {
|
|
59
|
+
throw new RecoveryRequiredError(options.opId, existingRecorded.status, existingRecorded.runId);
|
|
60
|
+
}
|
|
61
|
+
// indeterminate:绝不重跑,原样返回,保留租约;已结清终态返回已持久化结果
|
|
62
|
+
const kind = existingRecorded.result?.status === 'indeterminate' ? 'indeterminate' : 'recorded';
|
|
63
|
+
domain.getStore().recordReplay(domain.domainId, options.opId, kind, options.runId);
|
|
64
|
+
return {
|
|
65
|
+
...existingRecorded.result,
|
|
66
|
+
replayed: true,
|
|
67
|
+
runId: existingRecorded.runId,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.js","sourceRoot":"","sources":["../../src/supervisor/replay.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAA0B,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGtG;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAuB,EACvB,QAA8B,EAC9B,OAA8B,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AAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjG,CAAC;IAED,6CAA6C;IAC7C,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,MAAM,KAAK,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC;IAChG,MAAM,CAAC,QAAQ,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACnF,OAAO;QACL,GAAG,gBAAgB,CAAC,MAAM;QAC1B,QAAQ,EAAE,IAAI;QACd,KAAK,EAAE,gBAAgB,CAAC,KAAK;KACO,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RollbackOperationResult } from '../types.js';
|
|
2
|
+
import { SnapshotContext } from './snapshot-ops.js';
|
|
3
|
+
import { RollbackOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* 回滚协议 (ARCHITECTURE §3.5 / 契约 #28–#30, #56)
|
|
6
|
+
*/
|
|
7
|
+
export declare function rollback(ctx: SnapshotContext, options: RollbackOptions): Promise<RollbackOperationResult>;
|
|
8
|
+
//# sourceMappingURL=rollback.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollback.d.ts","sourceRoot":"","sources":["../../src/supervisor/rollback.ts"],"names":[],"mappings":"AAGA,OAAO,EAAoC,uBAAuB,EAAe,MAAM,aAAa,CAAC;AAGrG,OAAO,EAAyC,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C;;GAEG;AACH,wBAAsB,QAAQ,CAAC,GAAG,EAAE,eAAe,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA0E/G"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { ResourceConflictError } from '../types.js';
|
|
4
|
+
import { isPathContained } from '../capability/index.js';
|
|
5
|
+
import { activateInternalOperation, sha256Json } from './snapshot-ops.js';
|
|
6
|
+
/**
|
|
7
|
+
* 回滚协议 (ARCHITECTURE §3.5 / 契约 #28–#30, #56)
|
|
8
|
+
*/
|
|
9
|
+
export async function rollback(ctx, options) {
|
|
10
|
+
const { domain, snapshotDriver } = ctx;
|
|
11
|
+
const startTime = Date.now();
|
|
12
|
+
const snapshot = domain.getStore().getSnapshot(options.snapshotId);
|
|
13
|
+
if (!snapshot) {
|
|
14
|
+
throw new Error(`Snapshot not found: ${options.snapshotId}`);
|
|
15
|
+
}
|
|
16
|
+
let targetRoots = (options.roots || snapshot.roots).map((r) => path.resolve(r));
|
|
17
|
+
let capAdmission;
|
|
18
|
+
if (options.capabilityId) {
|
|
19
|
+
capAdmission = domain.checkCapabilityAdmission({
|
|
20
|
+
capabilityId: options.capabilityId,
|
|
21
|
+
mutationRoots: targetRoots,
|
|
22
|
+
runId: options.runId,
|
|
23
|
+
opId: options.opId,
|
|
24
|
+
});
|
|
25
|
+
targetRoots = capAdmission.effectiveMutationRoots;
|
|
26
|
+
}
|
|
27
|
+
assertRootsQuiescent(domain, targetRoots, options.opId);
|
|
28
|
+
const op = {
|
|
29
|
+
id: options.opId,
|
|
30
|
+
runId: options.runId,
|
|
31
|
+
kind: 'rollback',
|
|
32
|
+
name: `rollback:${options.snapshotId}`,
|
|
33
|
+
inputFingerprint: sha256Json({
|
|
34
|
+
kind: 'rollback',
|
|
35
|
+
snapshotId: options.snapshotId,
|
|
36
|
+
targetRoots,
|
|
37
|
+
capabilityId: options.capabilityId ?? null,
|
|
38
|
+
}),
|
|
39
|
+
requiredResources: targetRoots.map((r) => `workspace:write:${r}`),
|
|
40
|
+
mutationRoots: targetRoots,
|
|
41
|
+
capabilityId: options.capabilityId,
|
|
42
|
+
outputRef: options.snapshotId,
|
|
43
|
+
timeoutMs: options.timeoutMs,
|
|
44
|
+
status: 'intent_registered',
|
|
45
|
+
};
|
|
46
|
+
activateInternalOperation(domain, op, capAdmission);
|
|
47
|
+
try {
|
|
48
|
+
const { unrestoredPaths } = await snapshotDriver.restore(snapshot, { force: options.force });
|
|
49
|
+
const currentFp = await snapshotDriver.fingerprint(targetRoots, { against: snapshot });
|
|
50
|
+
const status = currentFp !== snapshot.treeFingerprint ? 'failed' : unrestoredPaths.length > 0 ? 'partial' : 'restored';
|
|
51
|
+
const { coverage, outOfScopeEffects } = rollbackCoverage(status, () => allEffectsConfinedToRoots(domain, snapshot, targetRoots, options.opId));
|
|
52
|
+
let result;
|
|
53
|
+
domain.getStore().transaction(() => {
|
|
54
|
+
result = {
|
|
55
|
+
kind: 'rollback',
|
|
56
|
+
status,
|
|
57
|
+
snapshotId: options.snapshotId,
|
|
58
|
+
unrestoredPaths: unrestoredPaths.length > 0 ? unrestoredPaths : undefined,
|
|
59
|
+
coverage,
|
|
60
|
+
outOfScopeEffects,
|
|
61
|
+
capabilityId: options.capabilityId,
|
|
62
|
+
durationMs: Date.now() - startTime,
|
|
63
|
+
completedAt: new Date().toISOString(),
|
|
64
|
+
};
|
|
65
|
+
domain.getStore().recordOperationResult(options.opId, result);
|
|
66
|
+
domain.getStore().updateOperationStatus(options.opId, 'done');
|
|
67
|
+
});
|
|
68
|
+
domain.internalReleaseResources(options.opId);
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
domain.internalReleaseResources(options.opId);
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** 同一根目录的两种写资源名(原路径与 realpath),和它们对应的目录。 */
|
|
77
|
+
function rootAliases(root) {
|
|
78
|
+
let realRoot = root;
|
|
79
|
+
try {
|
|
80
|
+
realRoot = fs.realpathSync(root);
|
|
81
|
+
}
|
|
82
|
+
catch { }
|
|
83
|
+
return { root, realRoot, writeRes: `workspace:write:${root}`, writeResReal: `workspace:write:${realRoot}` };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 前置条件(契约 #30):目标根目录上不存在 active / stopping / 未裁决 indeterminate 的操作。
|
|
87
|
+
* 依次检查内存租约、未结清操作、持久化租约(含 indeterminate 保留的租约)。
|
|
88
|
+
*/
|
|
89
|
+
function assertRootsQuiescent(domain, targetRoots, opId) {
|
|
90
|
+
const aliases = targetRoots.map(rootAliases);
|
|
91
|
+
for (const a of aliases) {
|
|
92
|
+
for (const res of [a.writeRes, a.writeResReal]) {
|
|
93
|
+
if (domain.isResourceLocked(res)) {
|
|
94
|
+
throw new ResourceConflictError(res, domain.getResourceOwner(res) || 'unknown', opId, 0);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (const op of domain.getStore().getUnfinishedOperations(domain.domainId)) {
|
|
99
|
+
if (op.id === opId)
|
|
100
|
+
continue;
|
|
101
|
+
for (const a of aliases) {
|
|
102
|
+
const touchesRoot = op.requiredResources.includes(a.writeRes) ||
|
|
103
|
+
op.requiredResources.includes(a.writeResReal) ||
|
|
104
|
+
op.mutationRoots?.some((mr) => {
|
|
105
|
+
const resolved = path.resolve(mr);
|
|
106
|
+
return resolved === a.root || resolved === a.realRoot;
|
|
107
|
+
});
|
|
108
|
+
if (touchesRoot) {
|
|
109
|
+
throw new ResourceConflictError(a.writeRes, op.id, opId, 0);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const lease of domain.getStore().getPersistedResourceLeases(domain.domainId)) {
|
|
114
|
+
for (const a of aliases) {
|
|
115
|
+
if ((lease.resourceId === a.writeRes || lease.resourceId === a.writeResReal) && lease.operationId !== opId) {
|
|
116
|
+
throw new ResourceConflictError(lease.resourceId, lease.operationId, opId, 0);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 快照之后的每个已记录操作都在写入限制下运行、且写入范围落在回滚根内,才可声称 complete。
|
|
123
|
+
* 快照与回滚本身不产生根外副作用。
|
|
124
|
+
*/
|
|
125
|
+
function allEffectsConfinedToRoots(domain, snapshot, targetRoots, rollbackOpId) {
|
|
126
|
+
const store = domain.getStore();
|
|
127
|
+
const eventsSince = snapshot.journalSeq !== undefined ? store.getJournalEvents(domain.domainId, snapshot.journalSeq) : [];
|
|
128
|
+
const opResults = eventsSince.filter((e) => e.type === 'OPERATION_RESULT_RECORDED' && e.operationId !== rollbackOpId);
|
|
129
|
+
return opResults.every((e) => {
|
|
130
|
+
const res = e.payload?.result || e.payload;
|
|
131
|
+
const storedOp = store.getOperation(e.operationId);
|
|
132
|
+
if (storedOp?.kind === 'snapshot' || storedOp?.kind === 'rollback') {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
if (res?.confined !== true) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const opRoots = storedOp?.mutationRoots || [];
|
|
139
|
+
if (opRoots.length === 0)
|
|
140
|
+
return true;
|
|
141
|
+
return opRoots.every((opr) => targetRoots.some((sr) => isPathContained(sr, opr)));
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function rollbackCoverage(status, allConfined) {
|
|
145
|
+
if (status !== 'restored') {
|
|
146
|
+
return { coverage: status === 'partial' ? 'declared_roots' : 'none', outOfScopeEffects: 'possible' };
|
|
147
|
+
}
|
|
148
|
+
if (allConfined()) {
|
|
149
|
+
return { coverage: 'complete', outOfScopeEffects: 'none_possible' };
|
|
150
|
+
}
|
|
151
|
+
return { coverage: 'declared_roots', outOfScopeEffects: 'possible' };
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=rollback.js.map
|