@exaudeus/workrail 1.5.0 → 1.5.2
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/dist/application/services/workflow-compiler.js +14 -2
- package/dist/infrastructure/session/HttpServer.js +7 -2
- package/dist/infrastructure/session/SessionManager.d.ts +12 -4
- package/dist/infrastructure/session/SessionManager.js +30 -15
- package/dist/manifest.json +417 -217
- package/dist/mcp/error-mapper.d.ts +1 -1
- package/dist/mcp/error-mapper.js +12 -8
- package/dist/mcp/handler-factory.js +8 -1
- package/dist/mcp/handlers/session.js +36 -58
- package/dist/mcp/handlers/v2-advance-core/event-builders.d.ts +37 -0
- package/dist/mcp/handlers/v2-advance-core/event-builders.js +114 -0
- package/dist/mcp/handlers/v2-advance-core/index.d.ts +67 -0
- package/dist/mcp/handlers/v2-advance-core/index.js +119 -0
- package/dist/mcp/handlers/v2-advance-core/input-validation.d.ts +44 -0
- package/dist/mcp/handlers/v2-advance-core/input-validation.js +58 -0
- package/dist/mcp/handlers/v2-advance-core/outcome-blocked.d.ts +16 -0
- package/dist/mcp/handlers/v2-advance-core/outcome-blocked.js +64 -0
- package/dist/mcp/handlers/v2-advance-core/outcome-success.d.ts +15 -0
- package/dist/mcp/handlers/v2-advance-core/outcome-success.js +136 -0
- package/dist/mcp/handlers/v2-advance-core.d.ts +3 -45
- package/dist/mcp/handlers/v2-advance-core.js +3 -431
- package/dist/mcp/handlers/v2-advance-events.d.ts +61 -0
- package/dist/mcp/handlers/v2-advance-events.js +126 -0
- package/dist/mcp/handlers/v2-checkpoint.js +11 -14
- package/dist/mcp/handlers/v2-context-budget.js +2 -2
- package/dist/mcp/handlers/v2-error-mapping.d.ts +3 -3
- package/dist/mcp/handlers/v2-error-mapping.js +31 -27
- package/dist/mcp/handlers/v2-execution/advance.d.ts +32 -0
- package/dist/mcp/handlers/v2-execution/advance.js +50 -0
- package/dist/mcp/handlers/v2-execution/continue-advance.d.ts +29 -0
- package/dist/mcp/handlers/v2-execution/continue-advance.js +167 -0
- package/dist/mcp/handlers/v2-execution/continue-rehydrate.d.ts +22 -0
- package/dist/mcp/handlers/v2-execution/continue-rehydrate.js +145 -0
- package/dist/mcp/handlers/v2-execution/index.d.ts +31 -0
- package/dist/mcp/handlers/v2-execution/index.js +102 -0
- package/dist/mcp/handlers/v2-execution/replay.d.ts +55 -0
- package/dist/mcp/handlers/v2-execution/replay.js +225 -0
- package/dist/mcp/handlers/v2-execution/start.d.ts +52 -0
- package/dist/mcp/handlers/v2-execution/start.js +331 -0
- package/dist/mcp/handlers/v2-execution-helpers.d.ts +26 -2
- package/dist/mcp/handlers/v2-execution-helpers.js +76 -60
- package/dist/mcp/handlers/v2-execution.d.ts +1 -23
- package/dist/mcp/handlers/v2-execution.js +5 -898
- package/dist/mcp/handlers/v2-state-conversion.d.ts +0 -5
- package/dist/mcp/handlers/v2-state-conversion.js +2 -21
- package/dist/mcp/handlers/v2-token-ops.d.ts +1 -1
- package/dist/mcp/handlers/v2-workflow.js +85 -78
- package/dist/mcp/output-schemas.d.ts +197 -34
- package/dist/mcp/output-schemas.js +12 -5
- package/dist/mcp/tool-descriptions.js +22 -16
- package/dist/mcp/types.d.ts +10 -0
- package/dist/mcp/types.js +10 -0
- package/dist/mcp/v2/tools.d.ts +45 -7
- package/dist/mcp/v2/tools.js +7 -3
- package/dist/types/workflow-definition.d.ts +3 -2
- package/dist/v2/durable-core/canonical/jcs.js +8 -1
- package/dist/v2/durable-core/constants.d.ts +60 -0
- package/dist/v2/durable-core/constants.js +55 -1
- package/dist/v2/durable-core/domain/ack-advance-append-plan.d.ts +14 -13
- package/dist/v2/durable-core/domain/ack-advance-append-plan.js +143 -116
- package/dist/v2/durable-core/domain/blocked-node-builder.js +9 -3
- package/dist/v2/durable-core/domain/blocking-decision.d.ts +2 -0
- package/dist/v2/durable-core/domain/blocking-decision.js +29 -12
- package/dist/v2/durable-core/domain/bundle-builder.d.ts +1 -0
- package/dist/v2/durable-core/domain/bundle-builder.js +1 -1
- package/dist/v2/durable-core/domain/bundle-validator.js +3 -2
- package/dist/v2/durable-core/domain/decision-trace-builder.js +7 -9
- package/dist/v2/durable-core/domain/function-definition-expander.js +1 -3
- package/dist/v2/durable-core/domain/gap-builder.js +2 -1
- package/dist/v2/durable-core/domain/observation-builder.js +2 -1
- package/dist/v2/durable-core/domain/outputs.d.ts +2 -1
- package/dist/v2/durable-core/domain/outputs.js +3 -2
- package/dist/v2/durable-core/domain/reason-model.d.ts +1 -1
- package/dist/v2/durable-core/domain/reason-model.js +4 -9
- package/dist/v2/durable-core/domain/validation-criteria-validator.js +2 -2
- package/dist/v2/durable-core/domain/validation-event-builder.js +4 -6
- package/dist/v2/durable-core/domain/validation-loader.js +2 -1
- package/dist/v2/durable-core/domain/validation-requirements-extractor.js +12 -18
- package/dist/v2/durable-core/encoding/base32-lower.d.ts +13 -1
- package/dist/v2/durable-core/encoding/base32-lower.js +13 -3
- package/dist/v2/durable-core/encoding/hex-to-bytes.d.ts +6 -0
- package/dist/v2/durable-core/encoding/hex-to-bytes.js +19 -0
- package/dist/v2/durable-core/ids/attempt-id-derivation.d.ts +6 -1
- package/dist/v2/durable-core/ids/attempt-id-derivation.js +9 -19
- package/dist/v2/durable-core/ids/event-ids.d.ts +9 -0
- package/dist/v2/durable-core/ids/event-ids.js +18 -0
- package/dist/v2/durable-core/ids/index.d.ts +13 -33
- package/dist/v2/durable-core/ids/index.js +22 -63
- package/dist/v2/durable-core/ids/session-ids.d.ts +9 -0
- package/dist/v2/durable-core/ids/session-ids.js +18 -0
- package/dist/v2/durable-core/ids/snapshot-ids.d.ts +6 -0
- package/dist/v2/durable-core/ids/snapshot-ids.js +10 -0
- package/dist/v2/durable-core/ids/token-ids.d.ts +3 -0
- package/dist/v2/durable-core/ids/token-ids.js +6 -0
- package/dist/v2/durable-core/ids/workflow-hash-ref.d.ts +3 -0
- package/dist/v2/durable-core/ids/workflow-hash-ref.js +5 -4
- package/dist/v2/durable-core/ids/workflow-ids.d.ts +11 -0
- package/dist/v2/durable-core/ids/workflow-ids.js +21 -0
- package/dist/v2/durable-core/lib/utf8-byte-length.d.ts +1 -0
- package/dist/v2/durable-core/lib/utf8-byte-length.js +6 -0
- package/dist/v2/durable-core/schemas/artifacts/loop-control.d.ts +6 -6
- package/dist/v2/durable-core/schemas/artifacts/loop-control.js +2 -1
- package/dist/v2/durable-core/schemas/execution-snapshot/blocked-snapshot.d.ts +28 -28
- package/dist/v2/durable-core/schemas/execution-snapshot/blocked-snapshot.js +5 -7
- package/dist/v2/durable-core/schemas/execution-snapshot/execution-snapshot.v1.d.ts +296 -296
- package/dist/v2/durable-core/schemas/export-bundle/index.d.ts +466 -471
- package/dist/v2/durable-core/schemas/export-bundle/index.js +0 -8
- package/dist/v2/durable-core/schemas/lib/dedupe-key.d.ts +9 -1
- package/dist/v2/durable-core/schemas/lib/dedupe-key.js +4 -3
- package/dist/v2/durable-core/schemas/lib/utf8-byte-length.d.ts +1 -0
- package/dist/v2/durable-core/schemas/lib/utf8-byte-length.js +6 -0
- package/dist/v2/durable-core/schemas/session/blockers.d.ts +305 -0
- package/dist/v2/durable-core/schemas/session/blockers.js +80 -0
- package/dist/v2/durable-core/schemas/session/dag-topology.d.ts +77 -0
- package/dist/v2/durable-core/schemas/session/dag-topology.js +45 -0
- package/dist/v2/durable-core/schemas/session/events.d.ts +56 -56
- package/dist/v2/durable-core/schemas/session/events.js +11 -182
- package/dist/v2/durable-core/schemas/session/gaps.d.ts +211 -0
- package/dist/v2/durable-core/schemas/session/gaps.js +37 -0
- package/dist/v2/durable-core/schemas/session/outputs.d.ts +148 -0
- package/dist/v2/durable-core/schemas/session/outputs.js +44 -0
- package/dist/v2/durable-core/schemas/session/validation-event.js +5 -7
- package/dist/v2/durable-core/tokens/token-codec.d.ts +1 -18
- package/dist/v2/durable-core/tokens/token-codec.js +0 -67
- package/dist/v2/durable-core/tokens/token-signer.d.ts +1 -8
- package/dist/v2/durable-core/tokens/token-signer.js +0 -43
- package/dist/v2/infra/local/base32/index.js +1 -23
- package/dist/v2/infra/local/bech32m/index.js +1 -1
- package/dist/v2/infra/local/data-dir/index.d.ts +7 -6
- package/dist/v2/infra/local/data-dir/index.js +3 -3
- package/dist/v2/infra/local/directory-listing/index.d.ts +2 -2
- package/dist/v2/infra/local/session-store/index.js +198 -182
- package/dist/v2/infra/local/session-summary-provider/index.js +5 -2
- package/dist/v2/infra/local/snapshot-store/index.js +2 -2
- package/dist/v2/ports/data-dir.port.d.ts +7 -6
- package/dist/v2/ports/fs.port.d.ts +18 -7
- package/dist/v2/ports/session-event-log-store.port.d.ts +5 -2
- package/dist/v2/projections/advance-outcomes.d.ts +1 -7
- package/dist/v2/projections/advance-outcomes.js +2 -1
- package/dist/v2/projections/artifacts.js +3 -2
- package/dist/v2/projections/capabilities.d.ts +1 -7
- package/dist/v2/projections/capabilities.js +2 -1
- package/dist/v2/projections/gaps.d.ts +1 -7
- package/dist/v2/projections/gaps.js +2 -1
- package/dist/v2/projections/node-outputs.d.ts +1 -7
- package/dist/v2/projections/node-outputs.js +4 -3
- package/dist/v2/projections/preferences.d.ts +1 -7
- package/dist/v2/projections/preferences.js +2 -1
- package/dist/v2/projections/projection-error.d.ts +7 -0
- package/dist/v2/projections/projection-error.js +2 -0
- package/dist/v2/projections/resume-ranking.js +3 -3
- package/dist/v2/projections/run-context.d.ts +1 -7
- package/dist/v2/projections/run-context.js +4 -2
- package/dist/v2/projections/run-dag.d.ts +9 -9
- package/dist/v2/projections/run-dag.js +88 -65
- package/dist/v2/projections/run-status-signals.d.ts +1 -7
- package/dist/v2/projections/run-status-signals.js +3 -2
- package/dist/v2/usecases/execution-session-gate.js +2 -5
- package/dist/v2/usecases/export-session.js +4 -2
- package/dist/v2/usecases/import-session.d.ts +3 -3
- package/package.json +1 -1
- package/workflows/coding-task-workflow-agentic.json +0 -9
- package/workflows/workflow-for-workflows.json +18 -5
|
@@ -1,433 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.executeAdvanceCore =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const workflow_js_1 = require("../../types/workflow.js");
|
|
7
|
-
const snapshot_state_js_1 = require("../../v2/durable-core/projections/snapshot-state.js");
|
|
8
|
-
const run_context_js_1 = require("../../v2/projections/run-context.js");
|
|
9
|
-
const preferences_js_1 = require("../../v2/projections/preferences.js");
|
|
10
|
-
const context_merge_js_1 = require("../../v2/durable-core/domain/context-merge.js");
|
|
11
|
-
const jcs_js_1 = require("../../v2/durable-core/canonical/jcs.js");
|
|
12
|
-
const notes_markdown_js_1 = require("../../v2/durable-core/domain/notes-markdown.js");
|
|
13
|
-
const outputs_js_1 = require("../../v2/durable-core/domain/outputs.js");
|
|
14
|
-
const ack_advance_append_plan_js_1 = require("../../v2/durable-core/domain/ack-advance-append-plan.js");
|
|
15
|
-
const blocking_decision_js_1 = require("../../v2/durable-core/domain/blocking-decision.js");
|
|
16
|
-
const reason_model_js_1 = require("../../v2/durable-core/domain/reason-model.js");
|
|
17
|
-
const risk_policy_guardrails_js_1 = require("../../v2/durable-core/domain/risk-policy-guardrails.js");
|
|
18
|
-
const recommendation_warnings_js_1 = require("../../v2/durable-core/domain/recommendation-warnings.js");
|
|
19
|
-
const validation_criteria_validator_js_1 = require("../../v2/durable-core/domain/validation-criteria-validator.js");
|
|
20
|
-
const validation_event_builder_js_1 = require("../../v2/durable-core/domain/validation-event-builder.js");
|
|
21
|
-
const blocked_node_builder_js_1 = require("../../v2/durable-core/domain/blocked-node-builder.js");
|
|
22
|
-
const decision_trace_builder_js_1 = require("../../v2/durable-core/domain/decision-trace-builder.js");
|
|
23
|
-
const validation_engine_js_1 = require("../../application/services/validation-engine.js");
|
|
24
|
-
const enhanced_loop_validator_js_1 = require("../../application/services/enhanced-loop-validator.js");
|
|
25
|
-
const workflow_compiler_js_1 = require("../../application/services/workflow-compiler.js");
|
|
26
|
-
const workflow_interpreter_js_1 = require("../../application/services/workflow-interpreter.js");
|
|
27
|
-
const v2_state_conversion_js_1 = require("./v2-state-conversion.js");
|
|
28
|
-
const v2_context_budget_js_1 = require("./v2-context-budget.js");
|
|
29
|
-
const with_timeout_js_1 = require("./shared/with-timeout.js");
|
|
30
|
-
function nodeIdOf(mode) {
|
|
31
|
-
switch (mode.kind) {
|
|
32
|
-
case 'fresh': return mode.sourceNodeId;
|
|
33
|
-
case 'retry': return mode.blockedNodeId;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function snapshotOf(mode) {
|
|
37
|
-
switch (mode.kind) {
|
|
38
|
-
case 'fresh': return mode.snapshot;
|
|
39
|
-
case 'retry': return mode.blockedSnapshot;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function emitValidationOnSuccess(mode) {
|
|
43
|
-
switch (mode.kind) {
|
|
44
|
-
case 'fresh': return false;
|
|
45
|
-
case 'retry': return true;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function successNodeKind(mode) {
|
|
49
|
-
switch (mode.kind) {
|
|
50
|
-
case 'fresh': return undefined;
|
|
51
|
-
case 'retry': return 'step';
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function partialEvent(fields) {
|
|
55
|
-
return fields;
|
|
56
|
-
}
|
|
57
|
-
function executeAdvanceCore(args) {
|
|
58
|
-
const { mode, truth, sessionId, runId, attemptId, workflowHash, inputContext, inputOutput, lock, pinnedWorkflow, ports } = args;
|
|
59
|
-
const { snapshotStore, sessionStore, sha256, idFactory } = ports;
|
|
60
|
-
const currentNodeId = nodeIdOf(mode);
|
|
61
|
-
const snap = snapshotOf(mode);
|
|
62
|
-
const engineState = snap.enginePayload.engineState;
|
|
63
|
-
const pendingStep = derivePendingStepForMode(mode, engineState);
|
|
64
|
-
if (!pendingStep) {
|
|
65
|
-
return errAsync({ kind: 'no_pending_step' });
|
|
66
|
-
}
|
|
67
|
-
const validatedRes = validateAdvanceInputs({
|
|
68
|
-
truth, runId, currentNodeId, inputContext, inputOutput, pinnedWorkflow, pendingStep,
|
|
69
|
-
});
|
|
70
|
-
if (validatedRes.isErr())
|
|
71
|
-
return errAsync(validatedRes.error);
|
|
72
|
-
const v = validatedRes.value;
|
|
73
|
-
const validator = v.validationCriteria ? new validation_engine_js_1.ValidationEngine(new enhanced_loop_validator_js_1.EnhancedLoopValidator()) : null;
|
|
74
|
-
const validationRes = validator && v.notesMarkdown
|
|
75
|
-
? neverthrow_1.ResultAsync.fromPromise((0, with_timeout_js_1.withTimeout)(validator.validate(v.notesMarkdown, v.validationCriteria, v.mergedContext), 30000, 'ValidationEngine.validate'), (cause) => ({ kind: 'advance_apply_failed', message: String(cause) })).andThen((res) => {
|
|
76
|
-
if (res.isErr()) {
|
|
77
|
-
return (0, neverthrow_1.errAsync)({
|
|
78
|
-
kind: 'advance_apply_failed',
|
|
79
|
-
message: `ValidationEngineError: ${res.error.kind} (${res.error.message})`,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
return (0, neverthrow_1.okAsync)(res.value);
|
|
83
|
-
})
|
|
84
|
-
: (0, neverthrow_1.okAsync)(undefined);
|
|
85
|
-
return validationRes.andThen((validation) => {
|
|
86
|
-
const outputRequirement = (0, validation_criteria_validator_js_1.getOutputRequirementStatusWithArtifactsV1)({
|
|
87
|
-
outputContract: v.outputContract,
|
|
88
|
-
artifacts: v.artifacts,
|
|
89
|
-
validationCriteria: v.validationCriteria,
|
|
90
|
-
notesMarkdown: v.notesMarkdown,
|
|
91
|
-
validation,
|
|
92
|
-
});
|
|
93
|
-
const reasonsRes = (0, blocking_decision_js_1.detectBlockingReasonsV1)({ outputRequirement });
|
|
94
|
-
if (reasonsRes.isErr()) {
|
|
95
|
-
return errAsync({ kind: 'invariant_violation', message: reasonsRes.error.message });
|
|
96
|
-
}
|
|
97
|
-
const reasons = reasonsRes.value;
|
|
98
|
-
const { blocking: effectiveReasons } = (0, risk_policy_guardrails_js_1.applyGuardrails)(v.riskPolicy, reasons);
|
|
99
|
-
const shouldBlockNow = effectiveReasons.length > 0 && (0, reason_model_js_1.shouldBlock)(v.autonomy, effectiveReasons);
|
|
100
|
-
const ctx = { truth, sessionId, runId, currentNodeId, attemptId, workflowHash, inputOutput, pinnedWorkflow, engineState, pendingStep };
|
|
101
|
-
const computed = { reasons, effectiveReasons, outputRequirement, validation };
|
|
102
|
-
const ports = { snapshotStore, sessionStore, sha256, idFactory };
|
|
103
|
-
if (shouldBlockNow) {
|
|
104
|
-
return buildBlockedOutcome({ mode, snap, ctx, computed, lock, ports });
|
|
105
|
-
}
|
|
106
|
-
return buildSuccessOutcome({ mode, ctx, computed, v, lock, ports });
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
function derivePendingStepForMode(mode, engineState) {
|
|
110
|
-
switch (mode.kind) {
|
|
111
|
-
case 'fresh': {
|
|
112
|
-
const currentState = (0, v2_state_conversion_js_1.toV1ExecutionState)(engineState);
|
|
113
|
-
return (currentState.kind === 'running' && currentState.pendingStep) ? currentState.pendingStep : null;
|
|
114
|
-
}
|
|
115
|
-
case 'retry': {
|
|
116
|
-
if (engineState.kind !== 'blocked')
|
|
117
|
-
return null;
|
|
118
|
-
const pending = (0, snapshot_state_js_1.derivePendingStep)(engineState);
|
|
119
|
-
return pending ? {
|
|
120
|
-
stepId: String(pending.stepId),
|
|
121
|
-
loopPath: pending.loopPath.map(f => ({ loopId: String(f.loopId), iteration: f.iteration })),
|
|
122
|
-
} : null;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function validateAdvanceInputs(args) {
|
|
127
|
-
const { truth, runId, currentNodeId, inputContext, inputOutput, pinnedWorkflow, pendingStep } = args;
|
|
128
|
-
const storedContextRes = (0, run_context_js_1.projectRunContextV2)(truth.events);
|
|
129
|
-
const storedContext = storedContextRes.isOk() ? storedContextRes.value.byRunId[String(runId)]?.context : undefined;
|
|
130
|
-
const inputContextObj = inputContext && typeof inputContext === 'object' && inputContext !== null && !Array.isArray(inputContext)
|
|
131
|
-
? inputContext
|
|
132
|
-
: undefined;
|
|
133
|
-
const mergedContextRes = (0, context_merge_js_1.mergeContext)(storedContext, inputContextObj);
|
|
134
|
-
if (mergedContextRes.isErr()) {
|
|
135
|
-
return (0, neverthrow_1.err)({ kind: 'invariant_violation', message: `Context merge failed: ${mergedContextRes.error.message}` });
|
|
136
|
-
}
|
|
137
|
-
const step = (0, workflow_js_1.getStepById)(pinnedWorkflow, pendingStep.stepId);
|
|
138
|
-
const validationCriteria = step?.validationCriteria;
|
|
139
|
-
const outputContract = step?.outputContract;
|
|
140
|
-
const parentByNodeId = {};
|
|
141
|
-
for (const e of truth.events) {
|
|
142
|
-
if (e.kind !== 'node_created')
|
|
143
|
-
continue;
|
|
144
|
-
if (e.scope?.runId !== String(runId))
|
|
145
|
-
continue;
|
|
146
|
-
parentByNodeId[String(e.scope.nodeId)] = e.data.parentNodeId;
|
|
147
|
-
}
|
|
148
|
-
const prefs = (0, preferences_js_1.projectPreferencesV2)(truth.events, parentByNodeId);
|
|
149
|
-
const effectivePrefs = prefs.isOk() ? prefs.value.byNodeId[String(currentNodeId)]?.effective : undefined;
|
|
150
|
-
const rawAutonomy = effectivePrefs?.autonomy ?? 'guided';
|
|
151
|
-
const rawRiskPolicy = effectivePrefs?.riskPolicy ?? 'conservative';
|
|
152
|
-
const VALID_AUTONOMY = ['guided', 'full_auto_stop_on_user_deps', 'full_auto_never_stop'];
|
|
153
|
-
const VALID_RISK_POLICY = ['conservative', 'balanced', 'aggressive'];
|
|
154
|
-
if (!VALID_AUTONOMY.includes(rawAutonomy)) {
|
|
155
|
-
return (0, neverthrow_1.err)({ kind: 'invariant_violation', message: `Unknown autonomy mode: ${rawAutonomy}` });
|
|
156
|
-
}
|
|
157
|
-
if (!VALID_RISK_POLICY.includes(rawRiskPolicy)) {
|
|
158
|
-
return (0, neverthrow_1.err)({ kind: 'invariant_violation', message: `Unknown risk policy: ${rawRiskPolicy}` });
|
|
159
|
-
}
|
|
160
|
-
const autonomy = rawAutonomy;
|
|
161
|
-
const riskPolicy = rawRiskPolicy;
|
|
162
|
-
return (0, neverthrow_1.ok)({
|
|
163
|
-
pendingStep,
|
|
164
|
-
mergedContext: mergedContextRes.value,
|
|
165
|
-
inputContextObj,
|
|
166
|
-
validationCriteria,
|
|
167
|
-
outputContract,
|
|
168
|
-
notesMarkdown: inputOutput?.notesMarkdown,
|
|
169
|
-
artifacts: inputOutput?.artifacts ?? [],
|
|
170
|
-
autonomy,
|
|
171
|
-
riskPolicy,
|
|
172
|
-
effectivePrefs,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
function buildBlockedOutcome(args) {
|
|
176
|
-
const { mode, snap, lock, ports } = args;
|
|
177
|
-
const { truth, sessionId, runId, currentNodeId, attemptId, workflowHash } = args.ctx;
|
|
178
|
-
const { reasons, effectiveReasons, outputRequirement, validation } = args.computed;
|
|
179
|
-
const { snapshotStore, sessionStore, sha256, idFactory } = ports;
|
|
180
|
-
const blockersRes = (0, reason_model_js_1.buildBlockerReport)(effectiveReasons);
|
|
181
|
-
if (blockersRes.isErr()) {
|
|
182
|
-
return errAsync({ kind: 'invariant_violation', message: blockersRes.error.message });
|
|
183
|
-
}
|
|
184
|
-
const validationEventId = idFactory.mintEventId();
|
|
185
|
-
const validationId = `validation_${String(attemptId)}`;
|
|
186
|
-
const contractRefForEvent = outputRequirement.kind !== 'not_required' ? outputRequirement.contractRef : 'none';
|
|
187
|
-
const validationForEvent = validation ??
|
|
188
|
-
(outputRequirement.kind === 'missing'
|
|
189
|
-
? { valid: false, issues: [`Missing required output for contractRef=${contractRefForEvent}`], suggestions: [], warnings: undefined }
|
|
190
|
-
: { valid: false, issues: ['Validation result missing'], suggestions: [], warnings: undefined });
|
|
191
|
-
const validationEventRes = (0, validation_event_builder_js_1.buildValidationPerformedEvent)({
|
|
192
|
-
sessionId: String(sessionId),
|
|
193
|
-
validationId,
|
|
194
|
-
attemptId: String(attemptId),
|
|
195
|
-
contractRef: contractRefForEvent,
|
|
196
|
-
scope: { runId: String(runId), nodeId: String(currentNodeId) },
|
|
197
|
-
minted: { eventId: validationEventId },
|
|
198
|
-
result: validationForEvent,
|
|
199
|
-
});
|
|
200
|
-
if (validationEventRes.isErr()) {
|
|
201
|
-
return errAsync({ kind: 'invariant_violation', message: validationEventRes.error.message });
|
|
202
|
-
}
|
|
203
|
-
const extraEventsToAppend = [validationEventRes.value];
|
|
204
|
-
const primaryReason = reasons[0];
|
|
205
|
-
if (!primaryReason) {
|
|
206
|
-
return errAsync({ kind: 'invariant_violation', message: 'shouldBlockNow=true requires at least one reason' });
|
|
207
|
-
}
|
|
208
|
-
const blockedSnapshotRes = (0, blocked_node_builder_js_1.buildBlockedNodeSnapshot)({
|
|
209
|
-
priorSnapshot: snap,
|
|
210
|
-
primaryReason,
|
|
211
|
-
attemptId,
|
|
212
|
-
validationRef: validationId,
|
|
213
|
-
blockers: blockersRes.value,
|
|
214
|
-
sha256,
|
|
215
|
-
});
|
|
216
|
-
if (blockedSnapshotRes.isErr()) {
|
|
217
|
-
return errAsync({ kind: 'invariant_violation', message: blockedSnapshotRes.error.message });
|
|
218
|
-
}
|
|
219
|
-
return snapshotStore.putExecutionSnapshotV1(blockedSnapshotRes.value).andThen((blockedSnapshotRef) => {
|
|
220
|
-
return buildAndAppendPlan({
|
|
221
|
-
truth, sessionId, runId, currentNodeId, attemptId, workflowHash,
|
|
222
|
-
extraEventsToAppend, toNodeKind: 'blocked_attempt', snapshotRef: blockedSnapshotRef,
|
|
223
|
-
outputsToAppend: [], sessionStore, idFactory, lock,
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
function buildSuccessOutcome(args) {
|
|
228
|
-
const { mode, v, lock, ports } = args;
|
|
229
|
-
const { truth, sessionId, runId, currentNodeId, attemptId, workflowHash, inputOutput, pinnedWorkflow, engineState, pendingStep } = args.ctx;
|
|
230
|
-
const { effectiveReasons, outputRequirement, validation } = args.computed;
|
|
231
|
-
const { snapshotStore, sessionStore, sha256, idFactory } = ports;
|
|
232
|
-
const compiler = new workflow_compiler_js_1.WorkflowCompiler();
|
|
233
|
-
const interpreter = new workflow_interpreter_js_1.WorkflowInterpreter();
|
|
234
|
-
const compiledWf = compiler.compile(pinnedWorkflow);
|
|
235
|
-
if (compiledWf.isErr()) {
|
|
236
|
-
return errAsync({ kind: 'advance_apply_failed', message: compiledWf.error.message });
|
|
237
|
-
}
|
|
238
|
-
const currentState = (0, v2_state_conversion_js_1.toV1ExecutionState)(engineState);
|
|
239
|
-
const event = {
|
|
240
|
-
kind: 'step_completed',
|
|
241
|
-
stepInstanceId: {
|
|
242
|
-
stepId: pendingStep.stepId,
|
|
243
|
-
loopPath: pendingStep.loopPath.map(f => ({ loopId: f.loopId, iteration: f.iteration })),
|
|
244
|
-
},
|
|
245
|
-
};
|
|
246
|
-
const advanced = interpreter.applyEvent(currentState, event);
|
|
247
|
-
if (advanced.isErr()) {
|
|
248
|
-
return errAsync({ kind: 'advance_apply_failed', message: advanced.error.message });
|
|
249
|
-
}
|
|
250
|
-
const artifactsForEval = (0, v2_context_budget_js_1.collectArtifactsForEvaluation)({
|
|
251
|
-
truthEvents: truth.events,
|
|
252
|
-
inputArtifacts: inputOutput?.artifacts ?? [],
|
|
253
|
-
});
|
|
254
|
-
const nextRes = interpreter.next(compiledWf.value, advanced.value, v.mergedContext, artifactsForEval);
|
|
255
|
-
if (nextRes.isErr()) {
|
|
256
|
-
return errAsync({ kind: 'advance_next_failed', message: nextRes.error.message });
|
|
257
|
-
}
|
|
258
|
-
const out = nextRes.value;
|
|
259
|
-
const extraEventsToAppend = [];
|
|
260
|
-
if (v.autonomy === 'full_auto_never_stop' && effectiveReasons.length > 0) {
|
|
261
|
-
for (const [idx, r] of effectiveReasons.entries()) {
|
|
262
|
-
const g = (0, reason_model_js_1.reasonToGap)(r);
|
|
263
|
-
const gapId = `gap_${String(attemptId)}_${idx}`;
|
|
264
|
-
extraEventsToAppend.push({
|
|
265
|
-
v: 1,
|
|
266
|
-
eventId: idFactory.mintEventId(),
|
|
267
|
-
kind: 'gap_recorded',
|
|
268
|
-
dedupeKey: `gap_recorded:${String(sessionId)}:${gapId}`,
|
|
269
|
-
scope: { runId: String(runId), nodeId: String(currentNodeId) },
|
|
270
|
-
data: { gapId, severity: g.severity, reason: g.reason, summary: g.summary, resolution: { kind: 'unresolved' } },
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
const workflowRecommendations = pinnedWorkflow.definition.recommendedPreferences;
|
|
275
|
-
if (workflowRecommendations && v.effectivePrefs) {
|
|
276
|
-
const warnings = (0, recommendation_warnings_js_1.checkRecommendationExceedance)({ autonomy: v.autonomy, riskPolicy: v.riskPolicy }, workflowRecommendations);
|
|
277
|
-
for (const [idx, w] of warnings.entries()) {
|
|
278
|
-
const gapId = `rec_warn_${String(currentNodeId)}_${idx}`;
|
|
279
|
-
extraEventsToAppend.push(partialEvent({
|
|
280
|
-
v: 1,
|
|
281
|
-
eventId: idFactory.mintEventId(),
|
|
282
|
-
kind: 'gap_recorded',
|
|
283
|
-
dedupeKey: `gap_recorded:${String(sessionId)}:${gapId}`,
|
|
284
|
-
scope: { runId: String(runId), nodeId: String(currentNodeId) },
|
|
285
|
-
data: { gapId, severity: 'warning', reason: w.kind, summary: w.summary, resolution: { kind: 'unresolved' } },
|
|
286
|
-
}));
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (v.inputContextObj) {
|
|
290
|
-
extraEventsToAppend.push(partialEvent({
|
|
291
|
-
v: 1,
|
|
292
|
-
eventId: idFactory.mintEventId(),
|
|
293
|
-
kind: 'context_set',
|
|
294
|
-
dedupeKey: `context_set:${String(sessionId)}:${String(runId)}:${idFactory.mintEventId()}`,
|
|
295
|
-
scope: { runId: String(runId) },
|
|
296
|
-
data: {
|
|
297
|
-
contextId: idFactory.mintEventId(),
|
|
298
|
-
context: v.mergedContext,
|
|
299
|
-
source: 'agent_delta',
|
|
300
|
-
},
|
|
301
|
-
}));
|
|
302
|
-
}
|
|
303
|
-
if (emitValidationOnSuccess(mode)) {
|
|
304
|
-
const validationId = `validation_${String(attemptId)}`;
|
|
305
|
-
const contractRefForEvent = outputRequirement.kind !== 'not_required' ? outputRequirement.contractRef : 'none';
|
|
306
|
-
const validationForEvent = validation ??
|
|
307
|
-
(outputRequirement.kind === 'missing'
|
|
308
|
-
? { valid: false, issues: [`Missing required output for contractRef=${contractRefForEvent}`], suggestions: [], warnings: undefined }
|
|
309
|
-
: { valid: true, issues: [], suggestions: [], warnings: undefined });
|
|
310
|
-
const validationEventRes = (0, validation_event_builder_js_1.buildValidationPerformedEvent)({
|
|
311
|
-
sessionId: String(sessionId),
|
|
312
|
-
validationId,
|
|
313
|
-
attemptId: String(attemptId),
|
|
314
|
-
contractRef: contractRefForEvent,
|
|
315
|
-
scope: { runId: String(runId), nodeId: String(currentNodeId) },
|
|
316
|
-
minted: { eventId: idFactory.mintEventId() },
|
|
317
|
-
result: validationForEvent,
|
|
318
|
-
});
|
|
319
|
-
if (validationEventRes.isOk()) {
|
|
320
|
-
extraEventsToAppend.push(validationEventRes.value);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
if (out.trace.length > 0) {
|
|
324
|
-
const traceId = idFactory.mintEventId();
|
|
325
|
-
const traceDataRes = (0, decision_trace_builder_js_1.buildDecisionTraceEventData)(traceId, out.trace);
|
|
326
|
-
if (traceDataRes.isOk()) {
|
|
327
|
-
extraEventsToAppend.push(partialEvent({
|
|
328
|
-
v: 1,
|
|
329
|
-
eventId: idFactory.mintEventId(),
|
|
330
|
-
kind: 'decision_trace_appended',
|
|
331
|
-
dedupeKey: `decision_trace_appended:${String(sessionId)}:${traceId}`,
|
|
332
|
-
scope: { runId: String(runId), nodeId: String(currentNodeId) },
|
|
333
|
-
data: traceDataRes.value,
|
|
334
|
-
}));
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
const newEngineState = (0, v2_state_conversion_js_1.fromV1ExecutionState)(out.state);
|
|
338
|
-
const snapshotFile = {
|
|
339
|
-
v: 1,
|
|
340
|
-
kind: 'execution_snapshot',
|
|
341
|
-
enginePayload: { v: 1, engineState: newEngineState },
|
|
342
|
-
};
|
|
343
|
-
return snapshotStore.putExecutionSnapshotV1(snapshotFile).andThen((newSnapshotRef) => {
|
|
344
|
-
const allowNotesAppend = v.validationCriteria
|
|
345
|
-
? Boolean(v.notesMarkdown && validation && validation.valid)
|
|
346
|
-
: Boolean(v.notesMarkdown);
|
|
347
|
-
const notesOutputs = buildNotesOutputs(allowNotesAppend, attemptId, inputOutput);
|
|
348
|
-
const artifactOutputsRes = buildArtifactOutputs(inputOutput?.artifacts ?? [], attemptId, sha256);
|
|
349
|
-
if (artifactOutputsRes.isErr()) {
|
|
350
|
-
return errAsync(artifactOutputsRes.error);
|
|
351
|
-
}
|
|
352
|
-
const outputsToAppend = [...notesOutputs, ...artifactOutputsRes.value];
|
|
353
|
-
return buildAndAppendPlan({
|
|
354
|
-
truth, sessionId, runId, currentNodeId, attemptId, workflowHash,
|
|
355
|
-
extraEventsToAppend, toNodeKind: successNodeKind(mode),
|
|
356
|
-
snapshotRef: newSnapshotRef, outputsToAppend,
|
|
357
|
-
sessionStore, idFactory, lock,
|
|
358
|
-
});
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
function buildAndAppendPlan(args) {
|
|
362
|
-
const { truth, sessionId, runId, currentNodeId, attemptId, workflowHash, extraEventsToAppend, toNodeKind, snapshotRef, outputsToAppend, sessionStore, idFactory, lock } = args;
|
|
363
|
-
const toNodeId = String(idFactory.mintNodeId());
|
|
364
|
-
const nextEventIndex = truth.events.length === 0 ? 0 : truth.events[truth.events.length - 1].eventIndex + 1;
|
|
365
|
-
const evtAdvanceRecorded = idFactory.mintEventId();
|
|
366
|
-
const evtNodeCreated = idFactory.mintEventId();
|
|
367
|
-
const evtEdgeCreated = idFactory.mintEventId();
|
|
368
|
-
const hasChildren = truth.events.some((e) => e.kind === 'edge_created' && e.data.fromNodeId === String(currentNodeId));
|
|
369
|
-
const causeKind = hasChildren ? 'non_tip_advance' : 'intentional_fork';
|
|
370
|
-
const normalizedOutputs = (0, outputs_js_1.normalizeOutputsForAppend)(outputsToAppend);
|
|
371
|
-
const outputEventIds = normalizedOutputs.map(() => idFactory.mintEventId());
|
|
372
|
-
const planRes = (0, ack_advance_append_plan_js_1.buildAckAdvanceAppendPlanV1)({
|
|
373
|
-
sessionId: String(sessionId),
|
|
374
|
-
runId: String(runId),
|
|
375
|
-
fromNodeId: String(currentNodeId),
|
|
376
|
-
workflowHash,
|
|
377
|
-
attemptId: String(attemptId),
|
|
378
|
-
nextEventIndex,
|
|
379
|
-
extraEventsToAppend,
|
|
380
|
-
toNodeId,
|
|
381
|
-
toNodeKind,
|
|
382
|
-
snapshotRef,
|
|
383
|
-
causeKind,
|
|
384
|
-
minted: {
|
|
385
|
-
advanceRecordedEventId: evtAdvanceRecorded,
|
|
386
|
-
nodeCreatedEventId: evtNodeCreated,
|
|
387
|
-
edgeCreatedEventId: evtEdgeCreated,
|
|
388
|
-
outputEventIds,
|
|
389
|
-
},
|
|
390
|
-
outputsToAppend: [...outputsToAppend],
|
|
391
|
-
});
|
|
392
|
-
if (planRes.isErr())
|
|
393
|
-
return (0, neverthrow_1.errAsync)({ kind: 'invariant_violation', message: planRes.error.message });
|
|
394
|
-
return sessionStore.append(lock, planRes.value);
|
|
395
|
-
}
|
|
396
|
-
function buildNotesOutputs(allowNotesAppend, attemptId, inputOutput) {
|
|
397
|
-
if (!allowNotesAppend || !inputOutput?.notesMarkdown)
|
|
398
|
-
return [];
|
|
399
|
-
return [{
|
|
400
|
-
outputId: String((0, index_js_1.asOutputId)(`out_recap_${String(attemptId)}`)),
|
|
401
|
-
outputChannel: 'recap',
|
|
402
|
-
payload: {
|
|
403
|
-
payloadKind: 'notes',
|
|
404
|
-
notesMarkdown: (0, notes_markdown_js_1.toNotesMarkdownV1)(inputOutput.notesMarkdown),
|
|
405
|
-
},
|
|
406
|
-
}];
|
|
407
|
-
}
|
|
408
|
-
function buildArtifactOutputs(inputArtifacts, attemptId, sha256) {
|
|
409
|
-
const outputs = [];
|
|
410
|
-
for (let idx = 0; idx < inputArtifacts.length; idx++) {
|
|
411
|
-
const artifact = inputArtifacts[idx];
|
|
412
|
-
const canonicalBytesRes = (0, jcs_js_1.toCanonicalBytes)(artifact);
|
|
413
|
-
if (canonicalBytesRes.isErr()) {
|
|
414
|
-
return (0, neverthrow_1.err)({ kind: 'invariant_violation', message: `Artifact canonicalization failed at index ${idx}: ${canonicalBytesRes.error.message}` });
|
|
415
|
-
}
|
|
416
|
-
const canonicalBytes = canonicalBytesRes.value;
|
|
417
|
-
outputs.push({
|
|
418
|
-
outputId: (0, index_js_1.asOutputId)(`out_artifact_${String(attemptId)}_${idx}`),
|
|
419
|
-
outputChannel: 'artifact',
|
|
420
|
-
payload: {
|
|
421
|
-
payloadKind: 'artifact_ref',
|
|
422
|
-
sha256: sha256.sha256(canonicalBytes),
|
|
423
|
-
contentType: 'application/json',
|
|
424
|
-
byteLength: canonicalBytes.length,
|
|
425
|
-
content: artifact,
|
|
426
|
-
},
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
return (0, neverthrow_1.ok)(outputs);
|
|
430
|
-
}
|
|
431
|
-
function errAsync(e) {
|
|
432
|
-
return (0, neverthrow_1.errAsync)(e);
|
|
433
|
-
}
|
|
3
|
+
exports.executeAdvanceCore = void 0;
|
|
4
|
+
var index_js_1 = require("./v2-advance-core/index.js");
|
|
5
|
+
Object.defineProperty(exports, "executeAdvanceCore", { enumerable: true, get: function () { return index_js_1.executeAdvanceCore; } });
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Result } from 'neverthrow';
|
|
2
|
+
import type { DomainEventV1 } from '../../v2/durable-core/schemas/session/index.js';
|
|
3
|
+
import type { RunId, NodeId } from '../../v2/durable-core/ids/index.js';
|
|
4
|
+
import type { AttemptId } from '../../v2/durable-core/tokens/index.js';
|
|
5
|
+
import type { JsonObject } from '../../v2/durable-core/canonical/json-types.js';
|
|
6
|
+
import type { ValidationResult } from '../../types/validation.js';
|
|
7
|
+
import type { RecommendationWarning } from '../../v2/durable-core/domain/recommendation-warnings.js';
|
|
8
|
+
import type { ReasonV1 } from '../../v2/durable-core/domain/reason-model.js';
|
|
9
|
+
import type { DecisionTraceEntry } from '../../v2/durable-core/domain/decision-trace-builder.js';
|
|
10
|
+
import type { InternalError } from './v2-error-mapping.js';
|
|
11
|
+
import type { AdvanceCorePorts } from './v2-advance-core.js';
|
|
12
|
+
type PartialEvent = Omit<DomainEventV1, 'eventIndex' | 'sessionId'>;
|
|
13
|
+
import type { OutputRequirementStatus } from '../../v2/durable-core/domain/blocking-decision.js';
|
|
14
|
+
export type AdvanceMode = {
|
|
15
|
+
readonly kind: 'fresh';
|
|
16
|
+
readonly sourceNodeId: NodeId;
|
|
17
|
+
readonly snapshot: unknown;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: 'retry';
|
|
20
|
+
readonly blockedNodeId: NodeId;
|
|
21
|
+
readonly blockedSnapshot: unknown;
|
|
22
|
+
};
|
|
23
|
+
export declare function buildGapEvents(args: {
|
|
24
|
+
readonly gaps: readonly ReasonV1[];
|
|
25
|
+
readonly sessionId: string;
|
|
26
|
+
readonly runId: RunId;
|
|
27
|
+
readonly nodeId: NodeId;
|
|
28
|
+
readonly attemptId: AttemptId;
|
|
29
|
+
readonly idFactory: AdvanceCorePorts['idFactory'];
|
|
30
|
+
}): readonly PartialEvent[];
|
|
31
|
+
export declare function buildRecommendationWarningEvents(args: {
|
|
32
|
+
readonly recommendations: readonly RecommendationWarning[];
|
|
33
|
+
readonly sessionId: string;
|
|
34
|
+
readonly runId: RunId;
|
|
35
|
+
readonly nodeId: NodeId;
|
|
36
|
+
readonly idFactory: AdvanceCorePorts['idFactory'];
|
|
37
|
+
}): readonly PartialEvent[];
|
|
38
|
+
export declare function buildContextSetEvent(args: {
|
|
39
|
+
readonly mergedContext: JsonObject;
|
|
40
|
+
readonly sessionId: string;
|
|
41
|
+
readonly runId: RunId;
|
|
42
|
+
readonly idFactory: AdvanceCorePorts['idFactory'];
|
|
43
|
+
}): PartialEvent | null;
|
|
44
|
+
export declare function buildSuccessValidationEvent(args: {
|
|
45
|
+
readonly mode: AdvanceMode;
|
|
46
|
+
readonly outputRequirement: OutputRequirementStatus;
|
|
47
|
+
readonly validation: ValidationResult | undefined;
|
|
48
|
+
readonly attemptId: AttemptId;
|
|
49
|
+
readonly sessionId: string;
|
|
50
|
+
readonly runId: RunId;
|
|
51
|
+
readonly nodeId: NodeId;
|
|
52
|
+
readonly idFactory: AdvanceCorePorts['idFactory'];
|
|
53
|
+
}): PartialEvent | null;
|
|
54
|
+
export declare function buildDecisionTraceEvent(args: {
|
|
55
|
+
readonly decisions: readonly DecisionTraceEntry[];
|
|
56
|
+
readonly sessionId: string;
|
|
57
|
+
readonly runId: RunId;
|
|
58
|
+
readonly nodeId: NodeId;
|
|
59
|
+
readonly idFactory: AdvanceCorePorts['idFactory'];
|
|
60
|
+
}): Result<PartialEvent | null, InternalError>;
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildGapEvents = buildGapEvents;
|
|
4
|
+
exports.buildRecommendationWarningEvents = buildRecommendationWarningEvents;
|
|
5
|
+
exports.buildContextSetEvent = buildContextSetEvent;
|
|
6
|
+
exports.buildSuccessValidationEvent = buildSuccessValidationEvent;
|
|
7
|
+
exports.buildDecisionTraceEvent = buildDecisionTraceEvent;
|
|
8
|
+
const neverthrow_1 = require("neverthrow");
|
|
9
|
+
const reason_model_js_1 = require("../../v2/durable-core/domain/reason-model.js");
|
|
10
|
+
const validation_event_builder_js_1 = require("../../v2/durable-core/domain/validation-event-builder.js");
|
|
11
|
+
const decision_trace_builder_js_1 = require("../../v2/durable-core/domain/decision-trace-builder.js");
|
|
12
|
+
const constants_js_1 = require("../../v2/durable-core/constants.js");
|
|
13
|
+
function partialEvent(fields) {
|
|
14
|
+
return fields;
|
|
15
|
+
}
|
|
16
|
+
function buildGapEvents(args) {
|
|
17
|
+
const { gaps, sessionId, runId, nodeId, attemptId, idFactory } = args;
|
|
18
|
+
const events = [];
|
|
19
|
+
for (const [idx, r] of gaps.entries()) {
|
|
20
|
+
const g = (0, reason_model_js_1.reasonToGap)(r);
|
|
21
|
+
const gapId = `gap_${String(attemptId)}_${idx}`;
|
|
22
|
+
events.push({
|
|
23
|
+
v: 1,
|
|
24
|
+
eventId: idFactory.mintEventId(),
|
|
25
|
+
kind: constants_js_1.EVENT_KIND.GAP_RECORDED,
|
|
26
|
+
dedupeKey: `gap_recorded:${sessionId}:${gapId}`,
|
|
27
|
+
scope: { runId: String(runId), nodeId: String(nodeId) },
|
|
28
|
+
data: {
|
|
29
|
+
gapId,
|
|
30
|
+
severity: g.severity,
|
|
31
|
+
reason: g.reason,
|
|
32
|
+
summary: g.summary,
|
|
33
|
+
resolution: { kind: 'unresolved' },
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return events;
|
|
38
|
+
}
|
|
39
|
+
function buildRecommendationWarningEvents(args) {
|
|
40
|
+
const { recommendations, sessionId, runId, nodeId, idFactory } = args;
|
|
41
|
+
const events = [];
|
|
42
|
+
for (const [idx, w] of recommendations.entries()) {
|
|
43
|
+
const gapId = `rec_warn_${String(nodeId)}_${idx}`;
|
|
44
|
+
events.push(partialEvent({
|
|
45
|
+
v: 1,
|
|
46
|
+
eventId: idFactory.mintEventId(),
|
|
47
|
+
kind: constants_js_1.EVENT_KIND.GAP_RECORDED,
|
|
48
|
+
dedupeKey: `gap_recorded:${sessionId}:${gapId}`,
|
|
49
|
+
scope: { runId: String(runId), nodeId: String(nodeId) },
|
|
50
|
+
data: {
|
|
51
|
+
gapId,
|
|
52
|
+
severity: 'warning',
|
|
53
|
+
reason: w.kind,
|
|
54
|
+
summary: w.summary,
|
|
55
|
+
resolution: { kind: 'unresolved' },
|
|
56
|
+
},
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
return events;
|
|
60
|
+
}
|
|
61
|
+
function buildContextSetEvent(args) {
|
|
62
|
+
const { mergedContext, sessionId, runId, idFactory } = args;
|
|
63
|
+
return partialEvent({
|
|
64
|
+
v: 1,
|
|
65
|
+
eventId: idFactory.mintEventId(),
|
|
66
|
+
kind: constants_js_1.EVENT_KIND.CONTEXT_SET,
|
|
67
|
+
dedupeKey: `context_set:${sessionId}:${String(runId)}:${idFactory.mintEventId()}`,
|
|
68
|
+
scope: { runId: String(runId) },
|
|
69
|
+
data: {
|
|
70
|
+
contextId: idFactory.mintEventId(),
|
|
71
|
+
context: mergedContext,
|
|
72
|
+
source: 'agent_delta',
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function buildSuccessValidationEvent(args) {
|
|
77
|
+
const { mode, outputRequirement, validation, attemptId, sessionId, runId, nodeId, idFactory } = args;
|
|
78
|
+
if (mode.kind !== 'retry') {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const validationId = `validation_${String(attemptId)}`;
|
|
82
|
+
const contractRefForEvent = outputRequirement.kind !== 'not_required' && outputRequirement.kind !== 'satisfied'
|
|
83
|
+
? outputRequirement.contractRef
|
|
84
|
+
: 'none';
|
|
85
|
+
const validationForEvent = validation ??
|
|
86
|
+
(outputRequirement.kind === 'missing'
|
|
87
|
+
? {
|
|
88
|
+
valid: false,
|
|
89
|
+
issues: [`Missing required output for contractRef=${contractRefForEvent}`],
|
|
90
|
+
suggestions: [],
|
|
91
|
+
warnings: undefined,
|
|
92
|
+
}
|
|
93
|
+
: { valid: true, issues: [], suggestions: [], warnings: undefined });
|
|
94
|
+
const validationEventRes = (0, validation_event_builder_js_1.buildValidationPerformedEvent)({
|
|
95
|
+
sessionId,
|
|
96
|
+
validationId,
|
|
97
|
+
attemptId: String(attemptId),
|
|
98
|
+
contractRef: contractRefForEvent,
|
|
99
|
+
scope: { runId: String(runId), nodeId: String(nodeId) },
|
|
100
|
+
minted: { eventId: idFactory.mintEventId() },
|
|
101
|
+
result: validationForEvent,
|
|
102
|
+
});
|
|
103
|
+
return validationEventRes.isOk() ? validationEventRes.value : null;
|
|
104
|
+
}
|
|
105
|
+
function buildDecisionTraceEvent(args) {
|
|
106
|
+
const { decisions, sessionId, runId, nodeId, idFactory } = args;
|
|
107
|
+
if (decisions.length === 0) {
|
|
108
|
+
return (0, neverthrow_1.ok)(null);
|
|
109
|
+
}
|
|
110
|
+
const traceId = idFactory.mintEventId();
|
|
111
|
+
const traceDataRes = (0, decision_trace_builder_js_1.buildDecisionTraceEventData)(traceId, decisions);
|
|
112
|
+
if (traceDataRes.isErr()) {
|
|
113
|
+
return (0, neverthrow_1.err)({
|
|
114
|
+
kind: 'invariant_violation',
|
|
115
|
+
message: `Failed to build decision trace data: ${traceDataRes.error}`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return (0, neverthrow_1.ok)(partialEvent({
|
|
119
|
+
v: 1,
|
|
120
|
+
eventId: idFactory.mintEventId(),
|
|
121
|
+
kind: constants_js_1.EVENT_KIND.DECISION_TRACE_APPENDED,
|
|
122
|
+
dedupeKey: `decision_trace_appended:${sessionId}:${traceId}`,
|
|
123
|
+
scope: { runId: String(runId), nodeId: String(nodeId) },
|
|
124
|
+
data: traceDataRes.value,
|
|
125
|
+
}));
|
|
126
|
+
}
|