@dailephd/my-centcom 0.1.0 → 0.3.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 +10 -5
- package/apps/daemon/dist/artifacts/returnSnapshots.d.ts +12 -0
- package/apps/daemon/dist/artifacts/returnSnapshots.js +41 -0
- package/apps/daemon/dist/bootstrap/app.d.ts +21 -0
- package/apps/daemon/dist/bootstrap/app.js +27 -0
- package/apps/daemon/dist/bootstrap/installLayout.d.ts +81 -0
- package/apps/daemon/dist/bootstrap/installLayout.js +164 -0
- package/apps/daemon/dist/bootstrap/main.d.ts +14 -0
- package/apps/daemon/dist/bootstrap/main.js +141 -0
- package/apps/daemon/dist/bootstrap/operatorConsole.d.ts +75 -0
- package/apps/daemon/dist/bootstrap/operatorConsole.js +293 -0
- package/apps/daemon/dist/bootstrap/privateDataRoot.d.ts +38 -0
- package/apps/daemon/dist/bootstrap/privateDataRoot.js +204 -0
- package/apps/daemon/dist/bootstrap/secureMain.d.ts +118 -0
- package/apps/daemon/dist/bootstrap/secureMain.js +403 -0
- package/apps/daemon/dist/bootstrap/setup.d.ts +93 -0
- package/apps/daemon/dist/bootstrap/setup.js +738 -0
- package/apps/daemon/dist/context/changeProposal.d.ts +97 -0
- package/apps/daemon/dist/context/changeProposal.js +259 -0
- package/apps/daemon/dist/context/gitEvidence.d.ts +88 -0
- package/apps/daemon/dist/context/gitEvidence.js +332 -0
- package/apps/daemon/dist/context/gitObservation.d.ts +30 -0
- package/apps/daemon/dist/context/gitObservation.js +113 -0
- package/apps/daemon/dist/context/projectReadIssuer.d.ts +68 -0
- package/apps/daemon/dist/context/projectReadIssuer.js +158 -0
- package/apps/daemon/dist/context/projectReadService.d.ts +153 -0
- package/apps/daemon/dist/context/projectReadService.js +652 -0
- package/apps/daemon/dist/context/projectViewService.d.ts +85 -0
- package/apps/daemon/dist/context/projectViewService.js +332 -0
- package/apps/daemon/dist/context/proposalRecorder.d.ts +11 -0
- package/apps/daemon/dist/context/proposalRecorder.js +19 -0
- package/apps/daemon/dist/plugins/drivers/codexMcpConfig.d.ts +40 -0
- package/apps/daemon/dist/plugins/drivers/codexMcpConfig.js +73 -0
- package/apps/daemon/dist/plugins/drivers/codexModelCompatibility.d.ts +49 -0
- package/apps/daemon/dist/plugins/drivers/codexModelCompatibility.js +70 -0
- package/apps/daemon/dist/plugins/drivers/fakePluginDriver.d.ts +16 -0
- package/apps/daemon/dist/plugins/drivers/fakePluginDriver.js +115 -0
- package/apps/daemon/dist/plugins/drivers/finiteCliDriver.d.ts +153 -0
- package/apps/daemon/dist/plugins/drivers/finiteCliDriver.js +505 -0
- package/apps/daemon/dist/plugins/registry/operationCatalog.d.ts +23 -0
- package/apps/daemon/dist/plugins/registry/operationCatalog.js +41 -0
- package/apps/daemon/dist/plugins/registry/registryOperationCatalog.d.ts +14 -0
- package/apps/daemon/dist/plugins/registry/registryOperationCatalog.js +29 -0
- package/apps/daemon/dist/plugins/registry/verifiedCapabilityRegistry.d.ts +151 -0
- package/apps/daemon/dist/plugins/registry/verifiedCapabilityRegistry.js +579 -0
- package/apps/daemon/dist/projects/codingAuthority.d.ts +102 -0
- package/apps/daemon/dist/projects/codingAuthority.js +244 -0
- package/apps/daemon/dist/projects/fixtureAuthorization.d.ts +54 -0
- package/apps/daemon/dist/projects/fixtureAuthorization.js +172 -0
- package/apps/daemon/dist/projects/pairedAuthority.d.ts +158 -0
- package/apps/daemon/dist/projects/pairedAuthority.js +273 -0
- package/apps/daemon/dist/projects/trustedCliConsent.d.ts +47 -0
- package/apps/daemon/dist/projects/trustedCliConsent.js +84 -0
- package/apps/daemon/dist/runs/changeApplier.d.ts +59 -0
- package/apps/daemon/dist/runs/changeApplier.js +319 -0
- package/apps/daemon/dist/runs/codingWorkflowService.d.ts +283 -0
- package/apps/daemon/dist/runs/codingWorkflowService.js +785 -0
- package/apps/daemon/dist/runs/durableIntentCoordinator.d.ts +72 -0
- package/apps/daemon/dist/runs/durableIntentCoordinator.js +160 -0
- package/apps/daemon/dist/runs/inMemoryCoordinator.d.ts +55 -0
- package/apps/daemon/dist/runs/inMemoryCoordinator.js +375 -0
- package/apps/daemon/dist/runs/runExecutor.d.ts +154 -0
- package/apps/daemon/dist/runs/runExecutor.js +295 -0
- package/apps/daemon/dist/runs/workflowService.d.ts +185 -0
- package/apps/daemon/dist/runs/workflowService.js +300 -0
- package/apps/daemon/dist/runtime/boundedProcessHost.d.ts +111 -0
- package/apps/daemon/dist/runtime/boundedProcessHost.js +752 -0
- package/apps/daemon/dist/runtime/evidenceRedaction.d.ts +30 -0
- package/apps/daemon/dist/runtime/evidenceRedaction.js +79 -0
- package/apps/daemon/dist/runtime/executionDiagnostics.d.ts +134 -0
- package/apps/daemon/dist/runtime/executionDiagnostics.js +213 -0
- package/apps/daemon/dist/runtime/modelExecutionPolicy.d.ts +22 -0
- package/apps/daemon/dist/runtime/modelExecutionPolicy.js +19 -0
- package/apps/daemon/dist/runtime/mxcExecution.d.ts +12 -0
- package/apps/daemon/dist/runtime/mxcExecution.js +163 -0
- package/apps/daemon/dist/runtime/mxcIntegrity.d.ts +24 -0
- package/apps/daemon/dist/runtime/mxcIntegrity.js +81 -0
- package/apps/daemon/dist/runtime/mxcWorker.d.ts +2 -0
- package/apps/daemon/dist/runtime/mxcWorker.js +45 -0
- package/apps/daemon/dist/runtime/provisionedExecution.d.ts +150 -0
- package/apps/daemon/dist/runtime/provisionedExecution.js +653 -0
- package/apps/daemon/dist/runtime/testExecution.d.ts +76 -0
- package/apps/daemon/dist/runtime/testExecution.js +106 -0
- package/apps/daemon/dist/runtime/testProfiles.d.ts +10 -0
- package/apps/daemon/dist/runtime/testProfiles.js +4 -0
- package/apps/daemon/dist/runtime/trustedAgentStaging.d.ts +89 -0
- package/apps/daemon/dist/runtime/trustedAgentStaging.js +262 -0
- package/apps/daemon/dist/runtime/trustedCaBundle.d.ts +77 -0
- package/apps/daemon/dist/runtime/trustedCaBundle.js +125 -0
- package/apps/daemon/dist/runtime/windows/windowsJobSupervisor.d.ts +166 -0
- package/apps/daemon/dist/runtime/windows/windowsJobSupervisor.js +521 -0
- package/apps/daemon/dist/storage/diagnosticLog.d.ts +63 -0
- package/apps/daemon/dist/storage/diagnosticLog.js +136 -0
- package/apps/daemon/dist/storage/executionTelemetry.d.ts +63 -0
- package/apps/daemon/dist/storage/executionTelemetry.js +247 -0
- package/apps/daemon/dist/storage/protectedSecrets.d.ts +39 -0
- package/apps/daemon/dist/storage/protectedSecrets.js +106 -0
- package/apps/daemon/dist/storage/sqliteLedger.d.ts +786 -0
- package/apps/daemon/dist/storage/sqliteLedger.js +1303 -0
- package/apps/daemon/dist/transports/http/dispatch.d.ts +43 -0
- package/apps/daemon/dist/transports/http/dispatch.js +405 -0
- package/apps/daemon/dist/transports/http/secureRoutes.d.ts +40 -0
- package/apps/daemon/dist/transports/http/secureRoutes.js +332 -0
- package/apps/daemon/dist/transports/http/workflowRoutes.d.ts +41 -0
- package/apps/daemon/dist/transports/http/workflowRoutes.js +473 -0
- package/apps/daemon/dist/transports/mcp/localProjectReadServer.d.ts +182 -0
- package/apps/daemon/dist/transports/mcp/localProjectReadServer.js +221 -0
- package/apps/daemon/dist/transports/native/nativeHost.d.ts +24 -0
- package/apps/daemon/dist/transports/native/nativeHost.js +344 -0
- package/apps/daemon/package.json +24 -0
- package/apps/daemon/src/runtime/benign/referenceEcho.mjs +27 -0
- package/apps/daemon/src/runtime/windows/buildSupervisor.mjs +87 -0
- package/apps/daemon/src/runtime/windows/supervisorMain.cs +676 -0
- package/apps/extension/manifest.secure.json +25 -0
- package/apps/extension/scripts/build.mjs +173 -0
- package/apps/extension/src/background/secureWorker.ts +1595 -0
- package/apps/extension/src/background/serviceWorker.ts +892 -0
- package/apps/extension/src/content/captureSelection.ts +64 -0
- package/apps/extension/src/content/secureInsert.ts +207 -0
- package/apps/extension/src/probe/index.html +41 -0
- package/apps/extension/src/probe/main.ts +201 -0
- package/apps/extension/src/sidepanel/index.html +203 -0
- package/apps/extension/src/sidepanel/main.ts +903 -0
- package/package.json +19 -3
- package/packages/contracts/dist/artifacts.d.ts +66 -0
- package/packages/contracts/dist/artifacts.js +64 -0
- package/packages/contracts/dist/automation.d.ts +540 -0
- package/packages/contracts/dist/automation.js +191 -0
- package/packages/contracts/dist/browser.d.ts +251 -0
- package/packages/contracts/dist/browser.js +131 -0
- package/packages/contracts/dist/context.d.ts +71 -0
- package/packages/contracts/dist/context.js +73 -0
- package/packages/contracts/dist/diagnostics.d.ts +99 -0
- package/packages/contracts/dist/diagnostics.js +284 -0
- package/packages/contracts/dist/effects.d.ts +42 -0
- package/packages/contracts/dist/effects.js +42 -0
- package/packages/contracts/dist/errors.d.ts +75 -0
- package/packages/contracts/dist/errors.js +70 -0
- package/packages/contracts/dist/events.d.ts +50 -0
- package/packages/contracts/dist/events.js +34 -0
- package/packages/contracts/dist/executionDiagnostics.d.ts +212 -0
- package/packages/contracts/dist/executionDiagnostics.js +101 -0
- package/packages/contracts/dist/identity.d.ts +54 -0
- package/packages/contracts/dist/identity.js +40 -0
- package/packages/contracts/dist/index.d.ts +20 -0
- package/packages/contracts/dist/index.js +20 -0
- package/packages/contracts/dist/invocation.d.ts +90 -0
- package/packages/contracts/dist/invocation.js +77 -0
- package/packages/contracts/dist/lifecycle.d.ts +130 -0
- package/packages/contracts/dist/lifecycle.js +135 -0
- package/packages/contracts/dist/plugin.d.ts +214 -0
- package/packages/contracts/dist/plugin.js +117 -0
- package/packages/contracts/dist/projectRead.d.ts +205 -0
- package/packages/contracts/dist/projectRead.js +162 -0
- package/packages/contracts/dist/secure.d.ts +128 -0
- package/packages/contracts/dist/secure.js +99 -0
- package/packages/contracts/package.json +32 -0
- package/packages/contracts/src/artifacts.ts +83 -0
- package/packages/contracts/src/automation.ts +238 -0
- package/packages/contracts/src/browser.ts +164 -0
- package/packages/contracts/src/context.ts +91 -0
- package/packages/contracts/src/diagnostics.ts +318 -0
- package/packages/contracts/src/effects.ts +49 -0
- package/packages/contracts/src/errors.ts +83 -0
- package/packages/contracts/src/events.ts +46 -0
- package/packages/contracts/src/executionDiagnostics.ts +116 -0
- package/packages/contracts/src/identity.ts +68 -0
- package/packages/contracts/src/index.ts +20 -0
- package/packages/contracts/src/invocation.ts +96 -0
- package/packages/contracts/src/lifecycle.ts +169 -0
- package/packages/contracts/src/plugin.ts +174 -0
- package/packages/contracts/src/projectRead.ts +179 -0
- package/packages/contracts/src/secure.ts +136 -0
- package/provenance.json +179 -9
- package/source.zip +0 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { PrepareIntentRequestV1, RunLifecycleState } from '../../../../packages/contracts/dist/index.js';
|
|
2
|
+
import { LifecycleStateName, RunRow, SqliteLedger } from '../storage/sqliteLedger.js';
|
|
3
|
+
import type { ApprovalRow, RunDetail } from '../storage/sqliteLedger.js';
|
|
4
|
+
/**
|
|
5
|
+
* C05 durable intent coordinator. Decides lifecycle, idempotency, approval and cancellation;
|
|
6
|
+
* persists only via C09. No driver, process, shell or executor is reachable from this module.
|
|
7
|
+
* A QUEUED run is never started here: launching belongs to a later (B03) component.
|
|
8
|
+
*/
|
|
9
|
+
export declare class IdempotencyConflictError extends Error {
|
|
10
|
+
constructor();
|
|
11
|
+
}
|
|
12
|
+
/** Narrow policy seam to C03; the coordinator never sees tokens, roots or the resolver. */
|
|
13
|
+
export interface ApprovalPolicy {
|
|
14
|
+
authorize(principalId: string, projectId: string, pluginId: string, operationId: string): boolean;
|
|
15
|
+
verifyApprovalProof(run: RunDetail, approval: ApprovalRow, presentedCode: string, requireUnconsumed: boolean): boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface PrepareOutcome {
|
|
18
|
+
runId: string;
|
|
19
|
+
duplicate: boolean;
|
|
20
|
+
createdAt: string;
|
|
21
|
+
}
|
|
22
|
+
export type DecisionOutcome = {
|
|
23
|
+
kind: 'NOT_FOUND';
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'DENIED';
|
|
26
|
+
code: 'APPROVAL_DENIED' | 'GRANT_DENIED';
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'CONFLICT';
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'OK';
|
|
31
|
+
runId: string;
|
|
32
|
+
lifecycleState: 'QUEUED' | 'CANCELLED';
|
|
33
|
+
updatedAt: string;
|
|
34
|
+
duplicate: boolean;
|
|
35
|
+
};
|
|
36
|
+
/** States that imply a process may have been launched; uncertain after a restart. */
|
|
37
|
+
export declare const RESTART_UNCERTAIN_STATES: readonly LifecycleStateName[];
|
|
38
|
+
export declare function computeRequestFingerprint(principalId: string, req: PrepareIntentRequestV1): string;
|
|
39
|
+
/** Validates each hop through the shared lifecycle table and returns the ordered states to record. */
|
|
40
|
+
export declare function validatedPath(from: RunLifecycleState, hops: ReadonlyArray<{
|
|
41
|
+
state: RunLifecycleState;
|
|
42
|
+
reason: string;
|
|
43
|
+
}>): Array<{
|
|
44
|
+
state: LifecycleStateName;
|
|
45
|
+
reason: string;
|
|
46
|
+
}>;
|
|
47
|
+
export declare class DurableIntentCoordinator {
|
|
48
|
+
private readonly ledger;
|
|
49
|
+
private readonly now;
|
|
50
|
+
private readonly policy?;
|
|
51
|
+
constructor(ledger: SqliteLedger, now?: () => number, policy?: ApprovalPolicy | undefined);
|
|
52
|
+
prepareIntent(principalId: string, req: PrepareIntentRequestV1): PrepareOutcome;
|
|
53
|
+
getStatus(principalId: string, runId: string): RunRow | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* One-time approval: consumes a trusted-challenge proof and records QUEUED, atomically.
|
|
56
|
+
* Nothing is launched; the proof (never the request's own flag) is the approval decision.
|
|
57
|
+
*/
|
|
58
|
+
approveRun(principalId: string, runId: string, approvalId: string, approvalCode: string, reason?: 'TRUSTED_LOCAL_CHALLENGE' | 'USER_RUN_CLICK' | 'LOCAL_RISK_CONFIRMATION'): DecisionOutcome;
|
|
59
|
+
/**
|
|
60
|
+
* Cancel a pending or never-started queued run. No process host exists, so no termination is
|
|
61
|
+
* claimed. Anything that may have been executing fails closed (409) instead of "cancelled".
|
|
62
|
+
*/
|
|
63
|
+
cancelRun(principalId: string, runId: string): DecisionOutcome;
|
|
64
|
+
private mapDecision;
|
|
65
|
+
/**
|
|
66
|
+
* Startup reconciliation. Possibly-executing states become UNKNOWN once, each hop validated by the
|
|
67
|
+
* shared lifecycle table; nothing is relaunched, queued, completed or failed. Pending and
|
|
68
|
+
* never-started queued runs are left exactly as they are.
|
|
69
|
+
*/
|
|
70
|
+
recoverOnStartup(): number;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=durableIntentCoordinator.d.ts.map
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { assertValidLifecycleTransition, } from '../../../../packages/contracts/dist/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* C05 durable intent coordinator. Decides lifecycle, idempotency, approval and cancellation;
|
|
5
|
+
* persists only via C09. No driver, process, shell or executor is reachable from this module.
|
|
6
|
+
* A QUEUED run is never started here: launching belongs to a later (B03) component.
|
|
7
|
+
*/
|
|
8
|
+
export class IdempotencyConflictError extends Error {
|
|
9
|
+
constructor() {
|
|
10
|
+
super('IDEMPOTENCY_CONFLICT');
|
|
11
|
+
this.name = 'IdempotencyConflictError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** States that imply a process may have been launched; uncertain after a restart. */
|
|
15
|
+
export const RESTART_UNCERTAIN_STATES = [
|
|
16
|
+
'STARTING', 'RUNNING', 'WAITING_AGENT_APPROVAL', 'CANCEL_REQUESTED',
|
|
17
|
+
];
|
|
18
|
+
export function computeRequestFingerprint(principalId, req) {
|
|
19
|
+
const promptB64 = Buffer.from(req.params.prompt, 'utf8').toString('base64');
|
|
20
|
+
return createHash('sha256')
|
|
21
|
+
.update(JSON.stringify([principalId, req.projectId, req.pluginId, req.operationId, req.requestId, req.idempotencyKey, promptB64]))
|
|
22
|
+
.digest('hex');
|
|
23
|
+
}
|
|
24
|
+
/** Validates each hop through the shared lifecycle table and returns the ordered states to record. */
|
|
25
|
+
export function validatedPath(from, hops) {
|
|
26
|
+
let cur = from;
|
|
27
|
+
for (const h of hops) {
|
|
28
|
+
assertValidLifecycleTransition(cur, h.state);
|
|
29
|
+
cur = h.state;
|
|
30
|
+
}
|
|
31
|
+
return hops.map((h) => ({ state: h.state, reason: h.reason }));
|
|
32
|
+
}
|
|
33
|
+
export class DurableIntentCoordinator {
|
|
34
|
+
ledger;
|
|
35
|
+
now;
|
|
36
|
+
policy;
|
|
37
|
+
constructor(ledger, now = Date.now, policy) {
|
|
38
|
+
this.ledger = ledger;
|
|
39
|
+
this.now = now;
|
|
40
|
+
this.policy = policy;
|
|
41
|
+
}
|
|
42
|
+
prepareIntent(principalId, req) {
|
|
43
|
+
// Lifecycle is validated against the frozen transition table before anything is persisted.
|
|
44
|
+
const states = validatedPath('CREATED', [
|
|
45
|
+
{ state: 'AWAITING_USER_APPROVAL', reason: 'USER_APPROVAL_REQUIRED' },
|
|
46
|
+
]);
|
|
47
|
+
const nowIso = new Date(this.now()).toISOString();
|
|
48
|
+
const result = this.ledger.reserveIntent({
|
|
49
|
+
runId: `run.${randomBytes(16).toString('hex')}`,
|
|
50
|
+
principalId,
|
|
51
|
+
requestId: req.requestId,
|
|
52
|
+
idempotencyKey: req.idempotencyKey,
|
|
53
|
+
fingerprint: computeRequestFingerprint(principalId, req),
|
|
54
|
+
projectId: req.projectId,
|
|
55
|
+
pluginId: req.pluginId,
|
|
56
|
+
operationId: req.operationId,
|
|
57
|
+
promptText: req.params.prompt,
|
|
58
|
+
states: [{ state: 'CREATED', reason: 'INTENT_RESERVED' }, ...states],
|
|
59
|
+
nowIso,
|
|
60
|
+
});
|
|
61
|
+
if (result.kind === 'CONFLICT')
|
|
62
|
+
throw new IdempotencyConflictError();
|
|
63
|
+
return {
|
|
64
|
+
runId: result.run.runId,
|
|
65
|
+
duplicate: result.kind === 'DUPLICATE',
|
|
66
|
+
createdAt: result.run.createdAt,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
getStatus(principalId, runId) {
|
|
70
|
+
return this.ledger.getRunForPrincipal(principalId, runId);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* One-time approval: consumes a trusted-challenge proof and records QUEUED, atomically.
|
|
74
|
+
* Nothing is launched; the proof (never the request's own flag) is the approval decision.
|
|
75
|
+
*/
|
|
76
|
+
approveRun(principalId, runId, approvalId, approvalCode, reason = 'TRUSTED_LOCAL_CHALLENGE') {
|
|
77
|
+
const policy = this.policy;
|
|
78
|
+
if (!policy)
|
|
79
|
+
return { kind: 'DENIED', code: 'APPROVAL_DENIED' };
|
|
80
|
+
const nowIso = new Date(this.now()).toISOString();
|
|
81
|
+
const res = this.ledger.applyDecision(principalId, runId, approvalId, nowIso, (run, approval) => {
|
|
82
|
+
if (run.lifecycleState === 'QUEUED') {
|
|
83
|
+
// Exact replay of an already-consumed proof for this same principal and run.
|
|
84
|
+
if (approval && approval.consumedAt !== null && policy.verifyApprovalProof(run, approval, approvalCode, false)) {
|
|
85
|
+
return { kind: 'REPLAY' };
|
|
86
|
+
}
|
|
87
|
+
return { kind: 'REJECT', code: 'CONFLICT' };
|
|
88
|
+
}
|
|
89
|
+
if (run.lifecycleState !== 'AWAITING_USER_APPROVAL')
|
|
90
|
+
return { kind: 'REJECT', code: 'CONFLICT' };
|
|
91
|
+
if (!approval || !policy.verifyApprovalProof(run, approval, approvalCode, true)) {
|
|
92
|
+
return { kind: 'REJECT', code: 'APPROVAL_DENIED' };
|
|
93
|
+
}
|
|
94
|
+
if (!policy.authorize(run.principalId, run.projectId, run.pluginId, run.operationId)) {
|
|
95
|
+
return { kind: 'REJECT', code: 'GRANT_DENIED' };
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
kind: 'APPLY',
|
|
99
|
+
consumeApproval: true,
|
|
100
|
+
states: validatedPath(run.lifecycleState, [{ state: 'QUEUED', reason }]),
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
return this.mapDecision(res, 'QUEUED');
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Cancel a pending or never-started queued run. No process host exists, so no termination is
|
|
107
|
+
* claimed. Anything that may have been executing fails closed (409) instead of "cancelled".
|
|
108
|
+
*/
|
|
109
|
+
cancelRun(principalId, runId) {
|
|
110
|
+
const nowIso = new Date(this.now()).toISOString();
|
|
111
|
+
const res = this.ledger.applyDecision(principalId, runId, undefined, nowIso, (run) => {
|
|
112
|
+
switch (run.lifecycleState) {
|
|
113
|
+
case 'CANCELLED':
|
|
114
|
+
return { kind: 'REPLAY' };
|
|
115
|
+
case 'CREATED':
|
|
116
|
+
case 'AWAITING_USER_APPROVAL':
|
|
117
|
+
return { kind: 'APPLY', states: validatedPath(run.lifecycleState, [{ state: 'CANCELLED', reason: 'USER_CANCELLED_BEFORE_START' }]) };
|
|
118
|
+
case 'QUEUED':
|
|
119
|
+
return {
|
|
120
|
+
kind: 'APPLY',
|
|
121
|
+
states: validatedPath('QUEUED', [
|
|
122
|
+
{ state: 'CANCEL_REQUESTED', reason: 'USER_CANCEL_REQUESTED' },
|
|
123
|
+
{ state: 'CANCELLED', reason: 'CANCELLED_NO_PROCESS_STARTED' },
|
|
124
|
+
]),
|
|
125
|
+
};
|
|
126
|
+
default:
|
|
127
|
+
return { kind: 'REJECT', code: 'CONFLICT' };
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return this.mapDecision(res, 'CANCELLED');
|
|
131
|
+
}
|
|
132
|
+
mapDecision(res, expected) {
|
|
133
|
+
switch (res.kind) {
|
|
134
|
+
case 'NOT_FOUND':
|
|
135
|
+
return { kind: 'NOT_FOUND' };
|
|
136
|
+
case 'REJECTED':
|
|
137
|
+
return res.code === 'APPROVAL_DENIED' || res.code === 'GRANT_DENIED'
|
|
138
|
+
? { kind: 'DENIED', code: res.code }
|
|
139
|
+
: { kind: 'CONFLICT' };
|
|
140
|
+
case 'APPLIED':
|
|
141
|
+
case 'REPLAY':
|
|
142
|
+
return {
|
|
143
|
+
kind: 'OK',
|
|
144
|
+
runId: res.run.runId,
|
|
145
|
+
lifecycleState: expected,
|
|
146
|
+
updatedAt: res.run.updatedAt,
|
|
147
|
+
duplicate: res.kind === 'REPLAY',
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Startup reconciliation. Possibly-executing states become UNKNOWN once, each hop validated by the
|
|
153
|
+
* shared lifecycle table; nothing is relaunched, queued, completed or failed. Pending and
|
|
154
|
+
* never-started queued runs are left exactly as they are.
|
|
155
|
+
*/
|
|
156
|
+
recoverOnStartup() {
|
|
157
|
+
return this.ledger.recoverUncertainRuns(RESTART_UNCERTAIN_STATES, (from, to) => assertValidLifecycleTransition(from, to), 'RESTART_UNCERTAIN', new Date(this.now()).toISOString());
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=durableIntentCoordinator.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PluginDriver, AuthorizedInvocation, NormalizedResult, NormalizedEvent, CancelReceipt, RunLifecycleRecordV1, RunId, BrowserRequestId } from '../../../../packages/contracts/dist/index.js';
|
|
2
|
+
export interface CoordinatorDispatchResult {
|
|
3
|
+
runId: RunId;
|
|
4
|
+
result: NormalizedResult;
|
|
5
|
+
events: NormalizedEvent[];
|
|
6
|
+
lifecycleRecord: RunLifecycleRecordV1;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Optional, test-scoped coordinator options (M04). Inert when omitted: normal M03 execution
|
|
10
|
+
* moves QUEUED -> STARTING and launches the driver synchronously, exactly as before.
|
|
11
|
+
*/
|
|
12
|
+
export interface CoordinatorOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Pauses an accepted operation in QUEUED, before driver.invoke, until the returned promise settles.
|
|
15
|
+
* If the run was canceled while paused, the driver is never invoked.
|
|
16
|
+
*/
|
|
17
|
+
beforeLaunch?: (context: {
|
|
18
|
+
runId: RunId;
|
|
19
|
+
requestId: BrowserRequestId;
|
|
20
|
+
}) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export interface M03Coordinator {
|
|
23
|
+
/**
|
|
24
|
+
* Resolves only for a COMPLETED run. FAILED / UNKNOWN / CANCELLED runs reject with a
|
|
25
|
+
* ContractError (NATIVE_CLI_FAILURE / UNKNOWN_PROCESS_OUTCOME / CANCELLED) whose details carry lifecycleState.
|
|
26
|
+
*/
|
|
27
|
+
dispatch(invocation: AuthorizedInvocation, requestId: BrowserRequestId): Promise<CoordinatorDispatchResult>;
|
|
28
|
+
/** In-memory, test-scoped cancellation (M04). Not a production process-tree termination. */
|
|
29
|
+
cancel(runId: RunId, reason: string): Promise<CancelReceipt>;
|
|
30
|
+
getRunRecord(runId: RunId): RunLifecycleRecordV1 | undefined;
|
|
31
|
+
getRunResult(runId: RunId): NormalizedResult | undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Synchronous in-memory reservation for one logical operation.
|
|
35
|
+
* Invariant: created before the first asynchronous boundary. In-memory only; this is NOT a
|
|
36
|
+
* durable exactly-once guarantee (durable run-intent reservation is K06 / M05).
|
|
37
|
+
*/
|
|
38
|
+
export interface ReservedAttempt {
|
|
39
|
+
requestId: BrowserRequestId;
|
|
40
|
+
fingerprint: string;
|
|
41
|
+
runId: RunId;
|
|
42
|
+
promise: Promise<CoordinatorDispatchResult>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Fingerprint of the logical operation. Deliberately excludes runId, grantId and timestamps,
|
|
46
|
+
* which are minted per attempt.
|
|
47
|
+
*/
|
|
48
|
+
export declare function computeOperationFingerprint(invocation: AuthorizedInvocation, requestId: BrowserRequestId): string;
|
|
49
|
+
/**
|
|
50
|
+
* In-memory execution coordinator (M03 skeleton, hardened in M04).
|
|
51
|
+
* Enforces canonical lifecycle transitions, synchronous idempotency reservation,
|
|
52
|
+
* truthful failure/unknown classification and in-memory cancellation.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createInMemoryCoordinator(driver: PluginDriver, options?: CoordinatorOptions): M03Coordinator;
|
|
55
|
+
//# sourceMappingURL=inMemoryCoordinator.d.ts.map
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { NormalizedResultSchema, NormalizedEventV1Schema, CancelReceiptSchema, RunLifecycleRecordV1Schema, assertValidLifecycleTransition, isTerminalLifecycleState, ContractError, } from '../../../../packages/contracts/dist/index.js';
|
|
3
|
+
import { isFixtureIssuedInvocation } from '../projects/fixtureAuthorization.js';
|
|
4
|
+
function canonicalize(value) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map(canonicalize);
|
|
7
|
+
}
|
|
8
|
+
if (value !== null && typeof value === 'object') {
|
|
9
|
+
const record = value;
|
|
10
|
+
return Object.fromEntries(Object.keys(record)
|
|
11
|
+
.sort()
|
|
12
|
+
.map((key) => [key, canonicalize(record[key])]));
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Fingerprint of the logical operation. Deliberately excludes runId, grantId and timestamps,
|
|
18
|
+
* which are minted per attempt.
|
|
19
|
+
*/
|
|
20
|
+
export function computeOperationFingerprint(invocation, requestId) {
|
|
21
|
+
const canonical = JSON.stringify([
|
|
22
|
+
invocation.projectId,
|
|
23
|
+
invocation.pluginId,
|
|
24
|
+
invocation.operationId,
|
|
25
|
+
requestId,
|
|
26
|
+
invocation.idempotencyKey,
|
|
27
|
+
canonicalize(invocation.params),
|
|
28
|
+
]);
|
|
29
|
+
return createHash('sha256').update(canonical, 'utf8').digest('hex');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* In-memory execution coordinator (M03 skeleton, hardened in M04).
|
|
33
|
+
* Enforces canonical lifecycle transitions, synchronous idempotency reservation,
|
|
34
|
+
* truthful failure/unknown classification and in-memory cancellation.
|
|
35
|
+
*/
|
|
36
|
+
export function createInMemoryCoordinator(driver, options = {}) {
|
|
37
|
+
const reservations = new Map();
|
|
38
|
+
const requestKeys = new Map();
|
|
39
|
+
const runs = new Map();
|
|
40
|
+
function transition(run, to, reason) {
|
|
41
|
+
assertValidLifecycleTransition(run.state, to);
|
|
42
|
+
run.state = to;
|
|
43
|
+
run.history.push({
|
|
44
|
+
state: to,
|
|
45
|
+
timestamp: new Date().toISOString(),
|
|
46
|
+
...(reason !== undefined ? { reason } : {}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function buildRecord(run) {
|
|
50
|
+
return RunLifecycleRecordV1Schema.parse({
|
|
51
|
+
schemaVersion: 1,
|
|
52
|
+
runId: run.invocation.runId,
|
|
53
|
+
projectId: run.invocation.projectId,
|
|
54
|
+
pluginId: run.invocation.pluginId,
|
|
55
|
+
operationId: run.invocation.operationId,
|
|
56
|
+
idempotencyKey: run.invocation.idempotencyKey,
|
|
57
|
+
currentState: run.state,
|
|
58
|
+
history: run.history,
|
|
59
|
+
cancellationRacingWithCompletion: run.cancellationRacingWithCompletion,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function runError(run, errorCode, message, details) {
|
|
63
|
+
return new ContractError({
|
|
64
|
+
errorCode,
|
|
65
|
+
message,
|
|
66
|
+
correlationId: run.requestId,
|
|
67
|
+
runId: run.invocation.runId,
|
|
68
|
+
projectId: run.invocation.projectId,
|
|
69
|
+
details: { lifecycleState: run.state, ...details },
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** Moves a run to a terminal state exactly once and settles the shared reservation promise. */
|
|
73
|
+
function settle(run, to, options = {}) {
|
|
74
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const racing = to === 'COMPLETED' && run.state === 'CANCEL_REQUESTED';
|
|
78
|
+
transition(run, to, options.reason);
|
|
79
|
+
if (racing) {
|
|
80
|
+
run.cancellationRacingWithCompletion = true;
|
|
81
|
+
}
|
|
82
|
+
if (to === 'COMPLETED') {
|
|
83
|
+
run.resolve({
|
|
84
|
+
runId: run.invocation.runId,
|
|
85
|
+
result: run.result,
|
|
86
|
+
events: run.events,
|
|
87
|
+
lifecycleRecord: buildRecord(run),
|
|
88
|
+
});
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const phase = options.phase ?? 'unspecified';
|
|
92
|
+
if (to === 'UNKNOWN') {
|
|
93
|
+
run.reject(runError(run, 'UNKNOWN_PROCESS_OUTCOME', 'Run outcome could not be established', { phase }));
|
|
94
|
+
}
|
|
95
|
+
else if (to === 'CANCELLED') {
|
|
96
|
+
// Distinct classification: a confirmed cancellation is neither a native CLI failure nor a transport fault.
|
|
97
|
+
run.reject(runError(run, 'CANCELLED', 'Run was cancelled before completion', {
|
|
98
|
+
phase,
|
|
99
|
+
cancellation: { outcome: 'CANCELLED', preLaunch: !run.launched },
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
run.reject(runError(run, options.errorCode ?? 'NATIVE_CLI_FAILURE', 'Run failed', {
|
|
104
|
+
phase,
|
|
105
|
+
...(options.exitCode !== undefined ? { exitCode: options.exitCode } : {}),
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** Classifies a driver exception into FAILED (definite) or UNKNOWN (indeterminate). */
|
|
110
|
+
function settleFromDriverError(run, err, phase) {
|
|
111
|
+
if (err instanceof ContractError) {
|
|
112
|
+
if (err.errorCode === 'NATIVE_CLI_FAILURE') {
|
|
113
|
+
settle(run, 'FAILED', { phase, reason: `driver reported definite failure during ${phase}` });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (err.errorCode === 'TIMEOUT' && err.details?.processTerminated === true) {
|
|
117
|
+
settle(run, 'FAILED', {
|
|
118
|
+
phase,
|
|
119
|
+
errorCode: 'TIMEOUT',
|
|
120
|
+
reason: `driver reported timeout with confirmed termination during ${phase}`,
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
settle(run, 'UNKNOWN', { phase, reason: `indeterminate driver outcome during ${phase}` });
|
|
126
|
+
}
|
|
127
|
+
async function execute(run) {
|
|
128
|
+
const invocation = run.invocation;
|
|
129
|
+
try {
|
|
130
|
+
try {
|
|
131
|
+
run.launched = true;
|
|
132
|
+
await driver.invoke(invocation);
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
settleFromDriverError(run, err, 'invoke');
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (run.state === 'STARTING') {
|
|
142
|
+
transition(run, 'RUNNING');
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
for await (const rawEvent of driver.observe(invocation.runId)) {
|
|
146
|
+
const event = NormalizedEventV1Schema.parse(rawEvent);
|
|
147
|
+
if (event.runId !== invocation.runId) {
|
|
148
|
+
throw new Error('observed event belongs to a different run');
|
|
149
|
+
}
|
|
150
|
+
run.events.push(event);
|
|
151
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
settleFromDriverError(run, err, 'observe');
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
let result;
|
|
164
|
+
try {
|
|
165
|
+
result = NormalizedResultSchema.parse(await driver.collect(invocation.runId));
|
|
166
|
+
if (result.runId !== invocation.runId) {
|
|
167
|
+
throw new Error('collected result belongs to a different run');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
settleFromDriverError(run, err, 'collect');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
run.result = result;
|
|
178
|
+
if (result.exitCode !== 0 || run.events.some((e) => e.eventKind === 'run_failed')) {
|
|
179
|
+
settle(run, 'FAILED', {
|
|
180
|
+
phase: 'collect',
|
|
181
|
+
exitCode: result.exitCode,
|
|
182
|
+
reason: 'process reported unsuccessful result',
|
|
183
|
+
});
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
settle(run, 'COMPLETED', { phase: 'collect' });
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
// Defensive: any coordinator-internal fault leaves the outcome indeterminate, never COMPLETED.
|
|
190
|
+
settle(run, 'UNKNOWN', { phase: 'coordinator', reason: 'coordinator internal fault' });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Test-scoped pre-launch gate: the run stays QUEUED until the hook settles. */
|
|
194
|
+
async function launchAfterHook(run) {
|
|
195
|
+
try {
|
|
196
|
+
await options.beforeLaunch({ runId: run.invocation.runId, requestId: run.requestId });
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
if (run.state === 'QUEUED') {
|
|
200
|
+
transition(run, 'STARTING');
|
|
201
|
+
settle(run, 'FAILED', {
|
|
202
|
+
phase: 'pre-launch',
|
|
203
|
+
errorCode: 'INTERNAL_ERROR',
|
|
204
|
+
reason: 'pre-launch hook failed; driver was not invoked',
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (run.state !== 'QUEUED') {
|
|
210
|
+
// Cancellation intent was recorded while pending: never launch.
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
transition(run, 'STARTING');
|
|
214
|
+
await execute(run);
|
|
215
|
+
}
|
|
216
|
+
async function performCancel(run, reason) {
|
|
217
|
+
let receipt;
|
|
218
|
+
try {
|
|
219
|
+
receipt = CancelReceiptSchema.parse(await driver.cancel(run.invocation.runId, reason));
|
|
220
|
+
if (receipt.runId !== run.invocation.runId) {
|
|
221
|
+
throw new Error('cancel receipt belongs to a different run');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
settle(run, 'UNKNOWN', { phase: 'cancel', reason: 'driver cancel failed' });
|
|
226
|
+
return CancelReceiptSchema.parse({
|
|
227
|
+
runId: run.invocation.runId,
|
|
228
|
+
cancelledAt: new Date().toISOString(),
|
|
229
|
+
acknowledged: false,
|
|
230
|
+
processTerminated: false,
|
|
231
|
+
reason: 'Driver cancel failed; process termination not confirmed',
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (receipt.acknowledged && receipt.processTerminated) {
|
|
235
|
+
// No-op when completion already won the race (state is terminal).
|
|
236
|
+
settle(run, 'CANCELLED', { phase: 'cancel', reason: receipt.reason });
|
|
237
|
+
}
|
|
238
|
+
// Unconfirmed termination: remain CANCEL_REQUESTED; never claim CANCELLED.
|
|
239
|
+
return receipt;
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
dispatch(invocation, requestId) {
|
|
243
|
+
// --- Synchronous section: no await before the reservation is recorded. ---
|
|
244
|
+
if (!isFixtureIssuedInvocation(invocation)) {
|
|
245
|
+
return Promise.reject(new ContractError({
|
|
246
|
+
errorCode: 'UNAUTHORIZED_PROJECT_OR_OPERATION',
|
|
247
|
+
message: 'Invocation was not issued by the project authorization authority',
|
|
248
|
+
correlationId: requestId,
|
|
249
|
+
projectId: invocation.projectId,
|
|
250
|
+
}));
|
|
251
|
+
}
|
|
252
|
+
const fingerprint = computeOperationFingerprint(invocation, requestId);
|
|
253
|
+
const existing = reservations.get(invocation.idempotencyKey);
|
|
254
|
+
if (existing) {
|
|
255
|
+
if (existing.requestId === requestId && existing.fingerprint === fingerprint) {
|
|
256
|
+
// Identical logical operation: share pending or completed result, never re-invoke.
|
|
257
|
+
return existing.promise;
|
|
258
|
+
}
|
|
259
|
+
return Promise.reject(new ContractError({
|
|
260
|
+
errorCode: 'IDEMPOTENCY_CONFLICT',
|
|
261
|
+
message: 'Idempotency key was already used for a different request or payload',
|
|
262
|
+
correlationId: requestId,
|
|
263
|
+
projectId: invocation.projectId,
|
|
264
|
+
runId: existing.runId,
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
267
|
+
const existingKey = requestKeys.get(requestId);
|
|
268
|
+
if (existingKey !== undefined && existingKey !== invocation.idempotencyKey) {
|
|
269
|
+
return Promise.reject(new ContractError({
|
|
270
|
+
errorCode: 'DUPLICATE_REQUEST',
|
|
271
|
+
message: 'Request ID was already used with a different idempotency key',
|
|
272
|
+
correlationId: requestId,
|
|
273
|
+
projectId: invocation.projectId,
|
|
274
|
+
runId: reservations.get(existingKey)?.runId,
|
|
275
|
+
}));
|
|
276
|
+
}
|
|
277
|
+
let resolve;
|
|
278
|
+
let reject;
|
|
279
|
+
const promise = new Promise((res, rej) => {
|
|
280
|
+
resolve = res;
|
|
281
|
+
reject = rej;
|
|
282
|
+
});
|
|
283
|
+
// Retries may await later; a failed reservation must never surface as an unhandled rejection.
|
|
284
|
+
promise.catch(() => undefined);
|
|
285
|
+
const run = {
|
|
286
|
+
invocation,
|
|
287
|
+
requestId,
|
|
288
|
+
state: 'CREATED',
|
|
289
|
+
history: [{ state: 'CREATED', timestamp: new Date().toISOString() }],
|
|
290
|
+
cancellationRacingWithCompletion: false,
|
|
291
|
+
launched: false,
|
|
292
|
+
events: [],
|
|
293
|
+
resolve,
|
|
294
|
+
reject,
|
|
295
|
+
};
|
|
296
|
+
transition(run, 'QUEUED');
|
|
297
|
+
if (!options.beforeLaunch) {
|
|
298
|
+
transition(run, 'STARTING');
|
|
299
|
+
}
|
|
300
|
+
reservations.set(invocation.idempotencyKey, {
|
|
301
|
+
requestId,
|
|
302
|
+
fingerprint,
|
|
303
|
+
runId: invocation.runId,
|
|
304
|
+
promise,
|
|
305
|
+
});
|
|
306
|
+
requestKeys.set(requestId, invocation.idempotencyKey);
|
|
307
|
+
runs.set(invocation.runId, run);
|
|
308
|
+
if (options.beforeLaunch) {
|
|
309
|
+
void launchAfterHook(run);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
void execute(run);
|
|
313
|
+
}
|
|
314
|
+
return promise;
|
|
315
|
+
},
|
|
316
|
+
cancel(runId, reason) {
|
|
317
|
+
const run = runs.get(runId);
|
|
318
|
+
if (!run) {
|
|
319
|
+
return Promise.reject(new ContractError({
|
|
320
|
+
errorCode: 'INVALID_PAYLOAD',
|
|
321
|
+
message: 'Cancellation target run does not exist',
|
|
322
|
+
correlationId: String(runId),
|
|
323
|
+
details: { reason: 'RUN_NOT_FOUND' },
|
|
324
|
+
}));
|
|
325
|
+
}
|
|
326
|
+
if (typeof reason !== 'string' || reason.trim().length === 0) {
|
|
327
|
+
return Promise.reject(new ContractError({
|
|
328
|
+
errorCode: 'INVALID_PAYLOAD',
|
|
329
|
+
message: 'Cancellation reason must be a non-empty string',
|
|
330
|
+
correlationId: String(runId),
|
|
331
|
+
runId,
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
if (run.cancelPromise) {
|
|
335
|
+
// Repeated cancellation: single driver.cancel invocation.
|
|
336
|
+
return run.cancelPromise;
|
|
337
|
+
}
|
|
338
|
+
if (isTerminalLifecycleState(run.state)) {
|
|
339
|
+
return Promise.resolve(CancelReceiptSchema.parse({
|
|
340
|
+
runId,
|
|
341
|
+
cancelledAt: new Date().toISOString(),
|
|
342
|
+
acknowledged: false,
|
|
343
|
+
processTerminated: false,
|
|
344
|
+
reason: `Run already terminal (${run.state}); no cancellation performed`,
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
const preLaunch = run.state === 'QUEUED' && !run.launched;
|
|
348
|
+
transition(run, 'CANCEL_REQUESTED', reason);
|
|
349
|
+
if (preLaunch) {
|
|
350
|
+
// Pending dispatch that never reached driver.invoke: no process exists, so no driver
|
|
351
|
+
// cancel is issued and termination is not claimed. Cancellation is nevertheless certain.
|
|
352
|
+
const receipt = CancelReceiptSchema.parse({
|
|
353
|
+
runId,
|
|
354
|
+
cancelledAt: new Date().toISOString(),
|
|
355
|
+
acknowledged: true,
|
|
356
|
+
processTerminated: false,
|
|
357
|
+
reason: `Canceled before launch; no process was started (${reason})`,
|
|
358
|
+
});
|
|
359
|
+
settle(run, 'CANCELLED', { phase: 'pre-launch', reason: receipt.reason });
|
|
360
|
+
run.cancelPromise = Promise.resolve(receipt);
|
|
361
|
+
return run.cancelPromise;
|
|
362
|
+
}
|
|
363
|
+
run.cancelPromise = performCancel(run, reason);
|
|
364
|
+
return run.cancelPromise;
|
|
365
|
+
},
|
|
366
|
+
getRunRecord(runId) {
|
|
367
|
+
const run = runs.get(runId);
|
|
368
|
+
return run ? buildRecord(run) : undefined;
|
|
369
|
+
},
|
|
370
|
+
getRunResult(runId) {
|
|
371
|
+
return runs.get(runId)?.result;
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=inMemoryCoordinator.js.map
|