@evomap/evolver-cli 2.0.0-beta.1 → 2.0.0-beta.10
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/antiGeneBenchmark.js +12 -0
- package/dist/antiGeneEvidence.d.ts +15 -0
- package/dist/antiGeneEvidence.js +49 -0
- package/dist/antiGeneRollout.d.ts +3 -1
- package/dist/antiGeneRollout.js +16 -2
- package/dist/assetHealth.d.ts +6 -0
- package/dist/assetHealth.js +105 -0
- package/dist/assetSidecarRecovery.d.ts +14 -0
- package/dist/assetSidecarRecovery.js +171 -0
- package/dist/assetTrust.d.ts +24 -0
- package/dist/assetTrust.js +218 -0
- package/dist/atp.js +4 -2
- package/dist/autoDistillLlm.d.ts +6 -0
- package/dist/autoDistillLlm.js +44 -7
- package/dist/autoexec.d.ts +14 -12
- package/dist/autoexec.js +84 -34
- package/dist/autoexecConfig.d.ts +10 -0
- package/dist/autoexecConfig.js +68 -0
- package/dist/cli.js +12 -5
- package/dist/cliContracts.js +30 -3
- package/dist/cliLifecycle.d.ts +7 -0
- package/dist/cliLifecycle.js +9 -0
- package/dist/cycleConsumer.d.ts +61 -1
- package/dist/cycleConsumer.js +551 -16
- package/dist/dashboard.d.ts +33 -0
- package/dist/dashboard.js +352 -0
- package/dist/dispatch.d.ts +10 -1
- package/dist/dispatch.js +40 -2
- package/dist/distillAdmission.d.ts +13 -0
- package/dist/distillAdmission.js +35 -0
- package/dist/distillObserver.js +6 -8
- package/dist/distillPrimitives.d.ts +9 -7
- package/dist/distillPrimitives.js +65 -12
- package/dist/doctor.d.ts +2 -0
- package/dist/doctor.js +49 -3
- package/dist/index.d.ts +8 -2
- package/dist/index.js +350 -42
- package/dist/issueReport.d.ts +7 -0
- package/dist/issueReport.js +291 -0
- package/dist/lifecycle.d.ts +19 -1
- package/dist/lifecycle.js +226 -29
- package/dist/localMemoryGraph.d.ts +97 -0
- package/dist/localMemoryGraph.js +889 -0
- package/dist/login.d.ts +20 -4
- package/dist/login.js +58 -8
- package/dist/materialPackage.d.ts +52 -0
- package/dist/materialPackage.js +401 -0
- package/dist/materialSnapshot.d.ts +54 -0
- package/dist/materialSnapshot.js +370 -0
- package/dist/memoryEventMirror.d.ts +23 -0
- package/dist/memoryEventMirror.js +140 -0
- package/dist/migrate/fieldMap.js +25 -0
- package/dist/migrate/v1Import.d.ts +7 -1
- package/dist/migrate/v1Import.js +132 -11
- package/dist/nonGitWorkspaceNotice.d.ts +24 -0
- package/dist/nonGitWorkspaceNotice.js +122 -0
- package/dist/proxyToken.d.ts +10 -0
- package/dist/proxyToken.js +80 -0
- package/dist/recipe.d.ts +12 -2
- package/dist/recipe.js +381 -8
- package/dist/reuse.d.ts +5 -3
- package/dist/reuse.js +74 -6
- package/dist/reuseReport.js +16 -16
- package/dist/runtimeCapabilities.d.ts +18 -0
- package/dist/runtimeCapabilities.js +81 -0
- package/dist/runtimeSessionSource.d.ts +10 -0
- package/dist/runtimeSessionSource.js +32 -5
- package/dist/setupHooks.d.ts +11 -1
- package/dist/setupHooks.js +235 -16
- package/dist/skill2gep.d.ts +38 -9
- package/dist/skill2gep.js +147 -14
- package/dist/skillDistill.d.ts +1 -0
- package/dist/skillDistill.js +4 -4
- package/dist/skillFetch.d.ts +18 -0
- package/dist/skillFetch.js +391 -0
- package/dist/solo/mode.js +5 -0
- package/dist/sync.d.ts +38 -0
- package/dist/sync.js +1981 -0
- package/dist/trajectoryExport.js +23 -12
- package/dist/v1Compat.d.ts +24 -0
- package/dist/v1Compat.js +249 -0
- package/dist/workflow.d.ts +39 -0
- package/dist/workflow.js +386 -0
- package/dist/workflowRuntime.d.ts +66 -0
- package/dist/workflowRuntime.js +631 -0
- package/package.json +12 -6
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve, sep } from 'node:path';
|
|
4
|
+
import { algo, assetstore, events, exec, verify, wire, workflow } from '@evomap/evolver-core';
|
|
5
|
+
import { readAutoExecConfig } from './autoexecConfig.js';
|
|
6
|
+
const DEFAULT_MAX_CONCURRENT_RUNS = 4;
|
|
7
|
+
const MAX_MAX_CONCURRENT_RUNS = 64;
|
|
8
|
+
const MAX_PROMPT_LENGTH = 4_000;
|
|
9
|
+
const MAX_REPO_PATH_LENGTH = 4_096;
|
|
10
|
+
const MAX_SIGNALS = 32;
|
|
11
|
+
const MAX_SIGNAL_LENGTH = 160;
|
|
12
|
+
const EXECUTION_CONTEXT_KEY = 'workflowExecution';
|
|
13
|
+
const POLICY_CONTEXT_KEY = 'workflowPolicyContext';
|
|
14
|
+
const LEGACY_POLICY_CONTEXT_SCHEMA_VERSION = 1;
|
|
15
|
+
const POLICY_CONTEXT_SCHEMA_VERSION = 2;
|
|
16
|
+
const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9:._-]{0,199}$/;
|
|
17
|
+
const PROFILE_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
18
|
+
const SECRET_TEXT_RE = /(?:\bBearer\s+[A-Za-z0-9._~+/=-]{8,}|\b(?:sk|gh[oprsu])_[A-Za-z0-9_-]{8,}|-----BEGIN [A-Z ]*PRIVATE KEY-----|\b(?:password|passwd|secret|token|api[_-]?key)\s*[:=]\s*\S+)/i;
|
|
19
|
+
const EXECUTION_CONTEXT_KEYS = new Set(['repo', 'signals', 'reviewedGeneAssetId', 'validationProfile']);
|
|
20
|
+
const RECOVERY_CODES = new Set([
|
|
21
|
+
'INVALID_ID',
|
|
22
|
+
'CORRUPT_STATE',
|
|
23
|
+
'UNSUPPORTED_SCHEMA',
|
|
24
|
+
'UNSAFE_PATH',
|
|
25
|
+
'INSECURE_PERMISSIONS',
|
|
26
|
+
'STATE_NOT_FOUND',
|
|
27
|
+
'RUN_FAILED',
|
|
28
|
+
'RECOVERY_FAILED',
|
|
29
|
+
'RECOVERY_INIT_FAILED',
|
|
30
|
+
]);
|
|
31
|
+
function classified(errorClass, message) {
|
|
32
|
+
return new workflow.ClassifiedWorkflowError(errorClass, message);
|
|
33
|
+
}
|
|
34
|
+
function classifyWorkflowExecutionFailure(result) {
|
|
35
|
+
const reason = result.outcome.reason;
|
|
36
|
+
const operationalReason = reason === 'execution cancelled' || reason?.startsWith('execution proof failed:') === true;
|
|
37
|
+
if (reason !== undefined && !operationalReason) {
|
|
38
|
+
return classified('safety', 'workflow agent execution was rejected by diff policy');
|
|
39
|
+
}
|
|
40
|
+
switch (result.failureKind) {
|
|
41
|
+
case 'spawn_failed':
|
|
42
|
+
case 'timeout':
|
|
43
|
+
return classified('transient', 'workflow agent execution failed transiently');
|
|
44
|
+
case 'cancelled':
|
|
45
|
+
return classified('permanent', 'workflow agent execution was cancelled');
|
|
46
|
+
case 'permission_denied':
|
|
47
|
+
return classified('safety', 'workflow agent execution was denied by runner policy');
|
|
48
|
+
case 'non_zero_exit':
|
|
49
|
+
case 'invalid_output':
|
|
50
|
+
return classified('permanent', 'workflow agent execution failed permanently');
|
|
51
|
+
case 'runtime_error':
|
|
52
|
+
return classified('unknown', 'workflow agent execution or proof capture failed');
|
|
53
|
+
case undefined:
|
|
54
|
+
return classified('permanent', 'workflow agent execution did not produce an accepted change');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function defaultWorkflowAutoExecHome(env = process.env) {
|
|
58
|
+
return join(events.evomapHome(env), 'autoexec');
|
|
59
|
+
}
|
|
60
|
+
export function resolveWorkflowAutoExecHome(options = {}) {
|
|
61
|
+
const env = options.env ?? process.env;
|
|
62
|
+
const configured = options.autoExecHome ?? env['EVOLVER_AUTOEXEC_HOME']?.trim();
|
|
63
|
+
return resolve(configured || defaultWorkflowAutoExecHome(env));
|
|
64
|
+
}
|
|
65
|
+
function canonicalPolicyHome(path) {
|
|
66
|
+
const suffix = [];
|
|
67
|
+
let cursor = resolve(path);
|
|
68
|
+
while (true) {
|
|
69
|
+
try {
|
|
70
|
+
return join(realpathSync(cursor), ...suffix.reverse());
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
const parent = dirname(cursor);
|
|
74
|
+
if (parent === cursor)
|
|
75
|
+
return resolve(path);
|
|
76
|
+
suffix.push(basename(cursor));
|
|
77
|
+
cursor = parent;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function digestPolicy(value) {
|
|
82
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
83
|
+
}
|
|
84
|
+
function normalizedLegacyPolicy(config) {
|
|
85
|
+
const validationProfiles = Object.keys(config.workflowValidationProfiles)
|
|
86
|
+
.sort()
|
|
87
|
+
.map((name) => [name, config.workflowValidationProfiles[name]]);
|
|
88
|
+
return {
|
|
89
|
+
allowedRoots: [...new Set(config.allowedRoots.map((root) => root.trim()))].sort(),
|
|
90
|
+
runner: config.runner,
|
|
91
|
+
timeoutMs: config.timeoutMs,
|
|
92
|
+
validationProfiles,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function normalizedPolicy(config, allowedRoots) {
|
|
96
|
+
const legacy = normalizedLegacyPolicy(config);
|
|
97
|
+
return { ...legacy, allowedRoots };
|
|
98
|
+
}
|
|
99
|
+
function createWorkflowPolicyContext(home, assetsDir, config, allowedRoots) {
|
|
100
|
+
const canonicalHome = canonicalPolicyHome(home);
|
|
101
|
+
const policy = normalizedPolicy(config, allowedRoots);
|
|
102
|
+
const legacyPolicy = normalizedLegacyPolicy(config);
|
|
103
|
+
return {
|
|
104
|
+
schemaVersion: POLICY_CONTEXT_SCHEMA_VERSION,
|
|
105
|
+
homeId: digestPolicy(canonicalHome),
|
|
106
|
+
policyId: digestPolicy({ ...policy, assetsRealm: canonicalPolicyHome(assetsDir) }),
|
|
107
|
+
legacyPolicyId: digestPolicy({ home: canonicalHome, ...legacyPolicy }),
|
|
108
|
+
allowLegacyPolicyExact: legacyPolicy.allowedRoots.length === allowedRoots.length
|
|
109
|
+
&& legacyPolicy.allowedRoots.every((root, index) => root === allowedRoots[index]),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function isWorkflowPolicyContext(value) {
|
|
113
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
114
|
+
return false;
|
|
115
|
+
const record = value;
|
|
116
|
+
return Object.keys(record).length === 3
|
|
117
|
+
&& record['schemaVersion'] === POLICY_CONTEXT_SCHEMA_VERSION
|
|
118
|
+
&& typeof record['homeId'] === 'string'
|
|
119
|
+
&& /^[a-f0-9]{64}$/.test(record['homeId'])
|
|
120
|
+
&& typeof record['policyId'] === 'string'
|
|
121
|
+
&& /^[a-f0-9]{64}$/.test(record['policyId']);
|
|
122
|
+
}
|
|
123
|
+
function isLegacyWorkflowPolicyContext(value) {
|
|
124
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
125
|
+
return false;
|
|
126
|
+
const record = value;
|
|
127
|
+
return Object.keys(record).length === 2
|
|
128
|
+
&& record['schemaVersion'] === LEGACY_POLICY_CONTEXT_SCHEMA_VERSION
|
|
129
|
+
&& typeof record['policyId'] === 'string'
|
|
130
|
+
&& /^[a-f0-9]{64}$/.test(record['policyId']);
|
|
131
|
+
}
|
|
132
|
+
function persistedPolicyContext(expected) {
|
|
133
|
+
return {
|
|
134
|
+
schemaVersion: expected.schemaVersion,
|
|
135
|
+
homeId: expected.homeId,
|
|
136
|
+
policyId: expected.policyId,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function policyContextDisposition(input, expected) {
|
|
140
|
+
const persisted = input[POLICY_CONTEXT_KEY];
|
|
141
|
+
if (isWorkflowPolicyContext(persisted)) {
|
|
142
|
+
if (persisted.homeId !== expected.homeId)
|
|
143
|
+
return 'owner_unknown_or_mismatch';
|
|
144
|
+
return persisted.policyId === expected.policyId ? 'exact' : 'same_home_drift';
|
|
145
|
+
}
|
|
146
|
+
if (isLegacyWorkflowPolicyContext(persisted)
|
|
147
|
+
&& expected.allowLegacyPolicyExact
|
|
148
|
+
&& persisted.policyId === expected.legacyPolicyId)
|
|
149
|
+
return 'exact';
|
|
150
|
+
return 'owner_unknown_or_mismatch';
|
|
151
|
+
}
|
|
152
|
+
function policyContextMatches(input, expected) {
|
|
153
|
+
return policyContextDisposition(input, expected) === 'exact';
|
|
154
|
+
}
|
|
155
|
+
function containsAgentStep(steps) {
|
|
156
|
+
return steps.some((step) => {
|
|
157
|
+
if (step.kind === 'agent')
|
|
158
|
+
return true;
|
|
159
|
+
if (step.kind === 'foreach')
|
|
160
|
+
return containsAgentStep(step.body);
|
|
161
|
+
if (step.kind === 'if') {
|
|
162
|
+
return containsAgentStep(step.then) || containsAgentStep(step.else ?? []);
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function runPolicyContextMatches(state, expected, allowSafeLegacyUnbound) {
|
|
168
|
+
if (state.context.input[POLICY_CONTEXT_KEY] === undefined) {
|
|
169
|
+
return allowSafeLegacyUnbound && !containsAgentStep(state.spec.steps);
|
|
170
|
+
}
|
|
171
|
+
return policyContextMatches(state.context.input, expected);
|
|
172
|
+
}
|
|
173
|
+
function runPolicyContextDisposition(state, expected, allowSafeLegacyUnbound) {
|
|
174
|
+
if (state.context.input[POLICY_CONTEXT_KEY] === undefined) {
|
|
175
|
+
return allowSafeLegacyUnbound && !containsAgentStep(state.spec.steps)
|
|
176
|
+
? 'exact'
|
|
177
|
+
: 'owner_unknown_or_mismatch';
|
|
178
|
+
}
|
|
179
|
+
return policyContextDisposition(state.context.input, expected);
|
|
180
|
+
}
|
|
181
|
+
function resolveWorkflowAssetsDir(options) {
|
|
182
|
+
const env = options.env ?? process.env;
|
|
183
|
+
return resolve(options.assetsDir ?? events.assetsDir(env));
|
|
184
|
+
}
|
|
185
|
+
function parseExecutionContext(input) {
|
|
186
|
+
const raw = input[EXECUTION_CONTEXT_KEY];
|
|
187
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
188
|
+
throw classified('safety', `workflow input.${EXECUTION_CONTEXT_KEY} is required`);
|
|
189
|
+
}
|
|
190
|
+
const record = raw;
|
|
191
|
+
if (Object.keys(record).some((key) => !EXECUTION_CONTEXT_KEYS.has(key))) {
|
|
192
|
+
throw classified('safety', 'workflow execution context contains unsupported fields');
|
|
193
|
+
}
|
|
194
|
+
const repo = record['repo'];
|
|
195
|
+
if (typeof repo !== 'string' || !isAbsolute(repo) || repo.length === 0 || repo.length > MAX_REPO_PATH_LENGTH || repo.includes('\0') || SECRET_TEXT_RE.test(repo)) {
|
|
196
|
+
throw classified('safety', 'workflow execution repo is invalid');
|
|
197
|
+
}
|
|
198
|
+
const assetId = record['reviewedGeneAssetId'];
|
|
199
|
+
if (typeof assetId !== 'string' || !SAFE_ID_RE.test(assetId) || SECRET_TEXT_RE.test(assetId)) {
|
|
200
|
+
throw classified('safety', 'workflow execution reviewed Gene is invalid');
|
|
201
|
+
}
|
|
202
|
+
const rawSignals = record['signals'];
|
|
203
|
+
if (!Array.isArray(rawSignals) || rawSignals.length === 0 || rawSignals.length > MAX_SIGNALS) {
|
|
204
|
+
throw classified('safety', 'workflow execution signals are required');
|
|
205
|
+
}
|
|
206
|
+
const signals = rawSignals.map((signal) => {
|
|
207
|
+
if (typeof signal !== 'string' || signal.trim().length === 0 || signal.length > MAX_SIGNAL_LENGTH || SECRET_TEXT_RE.test(signal)) {
|
|
208
|
+
throw classified('safety', 'workflow execution signal is invalid');
|
|
209
|
+
}
|
|
210
|
+
return signal.trim();
|
|
211
|
+
});
|
|
212
|
+
const validationProfile = record['validationProfile'];
|
|
213
|
+
if (validationProfile !== undefined && (typeof validationProfile !== 'string' || !PROFILE_NAME_RE.test(validationProfile))) {
|
|
214
|
+
throw classified('safety', 'workflow validation profile is invalid');
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
repo,
|
|
218
|
+
signals,
|
|
219
|
+
reviewedGeneAssetId: assetId,
|
|
220
|
+
...(typeof validationProfile === 'string' ? { validationProfile } : {}),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function canonicalDirectory(path) {
|
|
224
|
+
try {
|
|
225
|
+
const canonical = realpathSync(resolve(path));
|
|
226
|
+
if (!statSync(canonical).isDirectory())
|
|
227
|
+
throw new Error('not a directory');
|
|
228
|
+
return canonical;
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
throw classified('safety', 'workflow execution repo is unavailable');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function isWithinRoot(child, root) {
|
|
235
|
+
return child === root || child.startsWith(root.endsWith(sep) ? root : `${root}${sep}`);
|
|
236
|
+
}
|
|
237
|
+
function canonicalAllowedRoots(roots) {
|
|
238
|
+
const canonical = [];
|
|
239
|
+
for (const root of roots) {
|
|
240
|
+
const normalized = root.trim();
|
|
241
|
+
if (!isAbsolute(normalized) || normalized.length > MAX_REPO_PATH_LENGTH || normalized.includes('\0'))
|
|
242
|
+
continue;
|
|
243
|
+
try {
|
|
244
|
+
const path = realpathSync(resolve(normalized));
|
|
245
|
+
if (statSync(path).isDirectory())
|
|
246
|
+
canonical.push(path);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
// Invalid configured roots stay denied; one stale entry must not disable other valid roots.
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return Object.freeze([...new Set(canonical)].sort());
|
|
253
|
+
}
|
|
254
|
+
function safeScore(value) {
|
|
255
|
+
return Number.isFinite(value) ? Math.max(0, Math.min(1, value)) : 0;
|
|
256
|
+
}
|
|
257
|
+
function diffSummary(result) {
|
|
258
|
+
const diff = result.proofOfWork?.kind === 'git_diff' ? result.proofOfWork.gitDiff : undefined;
|
|
259
|
+
return {
|
|
260
|
+
changedFiles: Number.isSafeInteger(diff?.files) && (diff?.files ?? -1) >= 0 ? diff.files : 0,
|
|
261
|
+
changedLines: Number.isSafeInteger(diff?.lines) && (diff?.lines ?? -1) >= 0 ? diff.lines : 0,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
async function requireReviewedGene(context, store, provenance, review) {
|
|
265
|
+
let gene;
|
|
266
|
+
try {
|
|
267
|
+
gene = await store.get(context.reviewedGeneAssetId);
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
throw classified('unknown', 'workflow review lookup failed');
|
|
271
|
+
}
|
|
272
|
+
if (gene?.type !== 'Gene' || gene.asset_id !== context.reviewedGeneAssetId) {
|
|
273
|
+
throw classified('safety', 'workflow reviewed Gene is unavailable');
|
|
274
|
+
}
|
|
275
|
+
if (!provenance.isTrusted(context.reviewedGeneAssetId) || !review.isExplicitlyApproved(context.reviewedGeneAssetId)) {
|
|
276
|
+
throw classified('safety', 'workflow reviewed Gene is not trusted and explicitly approved');
|
|
277
|
+
}
|
|
278
|
+
return gene;
|
|
279
|
+
}
|
|
280
|
+
function workflowMutation(prompt, context) {
|
|
281
|
+
const id = createHash('sha256')
|
|
282
|
+
.update(JSON.stringify([context.repo, context.reviewedGeneAssetId, context.signals, prompt]))
|
|
283
|
+
.digest('hex')
|
|
284
|
+
.slice(0, 24);
|
|
285
|
+
return {
|
|
286
|
+
type: 'Mutation',
|
|
287
|
+
id: `workflow_${id}`,
|
|
288
|
+
category: 'repair',
|
|
289
|
+
trigger_signals: context.signals,
|
|
290
|
+
target: prompt,
|
|
291
|
+
expected_effect: prompt,
|
|
292
|
+
risk_level: 'high',
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Compose the production workflow AgentBridge from the existing hardened execution primitive.
|
|
297
|
+
* The workflow may select only an operator-configured validation profile; raw validation commands are not accepted.
|
|
298
|
+
*/
|
|
299
|
+
export function createProductionWorkflowAgentBridge(config, options = {}, expectedPolicyContext) {
|
|
300
|
+
const assetsDir = resolveWorkflowAssetsDir(options);
|
|
301
|
+
const allowedRoots = canonicalAllowedRoots(config.allowedRoots);
|
|
302
|
+
const policyContext = expectedPolicyContext
|
|
303
|
+
?? createWorkflowPolicyContext(resolveWorkflowAutoExecHome(options), assetsDir, config, allowedRoots);
|
|
304
|
+
return createProductionWorkflowAgentBridgeFromSnapshot(config, options, policyContext, allowedRoots);
|
|
305
|
+
}
|
|
306
|
+
function createProductionWorkflowAgentBridgeFromSnapshot(config, options, policyContext, allowedRoots) {
|
|
307
|
+
const assetsDir = resolveWorkflowAssetsDir(options);
|
|
308
|
+
const store = options.store ?? new assetstore.LocalJsonlProvider(assetsDir);
|
|
309
|
+
const provenance = options.provenance ?? new assetstore.ProvenanceStore(assetsDir);
|
|
310
|
+
const review = options.review ?? new assetstore.ReviewLedger(assetsDir);
|
|
311
|
+
const runValidation = options.runValidation ?? verify.runSandboxedValidation;
|
|
312
|
+
return async (prompt, ctx, bridgeOptions) => {
|
|
313
|
+
if (typeof prompt !== 'string' || prompt.trim().length === 0 || prompt.length > MAX_PROMPT_LENGTH || SECRET_TEXT_RE.test(prompt)) {
|
|
314
|
+
throw classified('safety', 'workflow agent prompt is invalid');
|
|
315
|
+
}
|
|
316
|
+
if (!policyContextMatches(ctx.input, policyContext)) {
|
|
317
|
+
throw classified('safety', 'workflow policy context does not match the durable run');
|
|
318
|
+
}
|
|
319
|
+
const context = parseExecutionContext(ctx.input);
|
|
320
|
+
const repo = canonicalDirectory(context.repo);
|
|
321
|
+
if (allowedRoots.length === 0 || !allowedRoots.some((root) => isWithinRoot(repo, root))) {
|
|
322
|
+
throw classified('safety', 'workflow execution repo is not allowlisted');
|
|
323
|
+
}
|
|
324
|
+
const gene = await requireReviewedGene(context, store, provenance, review);
|
|
325
|
+
const logicalGeneId = typeof gene['id'] === 'string' && SAFE_ID_RE.test(gene['id'])
|
|
326
|
+
? gene['id']
|
|
327
|
+
: context.reviewedGeneAssetId;
|
|
328
|
+
const validationCommands = context.validationProfile === undefined
|
|
329
|
+
? undefined
|
|
330
|
+
: config.workflowValidationProfiles[context.validationProfile];
|
|
331
|
+
if (context.validationProfile !== undefined && validationCommands === undefined) {
|
|
332
|
+
throw classified('safety', 'workflow validation profile is not configured');
|
|
333
|
+
}
|
|
334
|
+
const validate = validationCommands
|
|
335
|
+
? async (_mutation, _decision, cwd) => {
|
|
336
|
+
const result = await runValidation(validationCommands, cwd);
|
|
337
|
+
return { passed: result.passed, score: result.score };
|
|
338
|
+
}
|
|
339
|
+
: undefined;
|
|
340
|
+
const execute = exec.makeSafeExecute(repo, store, {
|
|
341
|
+
allowedRoots,
|
|
342
|
+
runner: config.runner,
|
|
343
|
+
timeoutMs: config.timeoutMs,
|
|
344
|
+
scrubEnv: true,
|
|
345
|
+
isolation: 'worktree',
|
|
346
|
+
requireTrustedGene: true,
|
|
347
|
+
...(bridgeOptions?.signal ? { signal: bridgeOptions.signal } : {}),
|
|
348
|
+
}, {
|
|
349
|
+
provenance,
|
|
350
|
+
review,
|
|
351
|
+
...(validate ? { validate } : {}),
|
|
352
|
+
...(validationCommands ? { validationCmds: validationCommands } : {}),
|
|
353
|
+
...(options.agent ? { agent: options.agent } : {}),
|
|
354
|
+
...(options.git ? { git: options.git } : {}),
|
|
355
|
+
});
|
|
356
|
+
let result;
|
|
357
|
+
try {
|
|
358
|
+
result = await execute(workflowMutation(prompt.trim(), context), {
|
|
359
|
+
selectedGeneId: logicalGeneId,
|
|
360
|
+
selectedAssetId: context.reviewedGeneAssetId,
|
|
361
|
+
candidates: [],
|
|
362
|
+
weightsVersion: 'workflow-production-v1',
|
|
363
|
+
strategyName: 'workflow-reviewed-gene',
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
if (error instanceof workflow.ClassifiedWorkflowError)
|
|
368
|
+
throw error;
|
|
369
|
+
const name = error instanceof Error ? error.name : '';
|
|
370
|
+
if (name === 'ExecBridgeForbiddenError' || name === 'UnsandboxedFullAccessRequiresIsolationError') {
|
|
371
|
+
throw classified('safety', 'workflow agent execution was denied by safety policy');
|
|
372
|
+
}
|
|
373
|
+
throw classified('unknown', 'workflow agent execution failed');
|
|
374
|
+
}
|
|
375
|
+
if (result.outcome.status !== 'success') {
|
|
376
|
+
throw classifyWorkflowExecutionFailure(result);
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
status: 'accepted',
|
|
380
|
+
score: safeScore(result.outcome.score),
|
|
381
|
+
...diffSummary(result),
|
|
382
|
+
validation: validationCommands ? 'passed' : 'not_requested',
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
export function resolveWorkflowMaxConcurrentRuns(env = process.env) {
|
|
387
|
+
const raw = env['EVOLVER_WORKFLOW_MAX_CONCURRENT_RUNS'];
|
|
388
|
+
if (raw === undefined)
|
|
389
|
+
return DEFAULT_MAX_CONCURRENT_RUNS;
|
|
390
|
+
const normalized = raw.trim();
|
|
391
|
+
if (!/^[1-9]\d*$/.test(normalized))
|
|
392
|
+
return DEFAULT_MAX_CONCURRENT_RUNS;
|
|
393
|
+
const parsed = Number(normalized);
|
|
394
|
+
return Number.isSafeInteger(parsed) && parsed <= MAX_MAX_CONCURRENT_RUNS
|
|
395
|
+
? parsed
|
|
396
|
+
: DEFAULT_MAX_CONCURRENT_RUNS;
|
|
397
|
+
}
|
|
398
|
+
function boundedRecoveryCode(code) {
|
|
399
|
+
return typeof code === 'string' && RECOVERY_CODES.has(code) ? code : 'RECOVERY_FAILED';
|
|
400
|
+
}
|
|
401
|
+
function emitRecoveryDiagnostic(writeDiagnostic, code) {
|
|
402
|
+
try {
|
|
403
|
+
writeDiagnostic(`[evolver-autoexec] workflow recovery code=${boundedRecoveryCode(code)}\n`);
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
// Diagnostics are best-effort and must never affect daemon readiness or recovery.
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
class PolicyBoundWorkflowRuntime extends workflow.DurableWorkflowRuntime {
|
|
410
|
+
policyContext;
|
|
411
|
+
allowSafeLegacyUnbound;
|
|
412
|
+
onRecoveryFailure;
|
|
413
|
+
rejectionResumeDepth = new Map();
|
|
414
|
+
constructor(deps, policyContext, allowSafeLegacyUnbound) {
|
|
415
|
+
super(deps);
|
|
416
|
+
this.policyContext = policyContext;
|
|
417
|
+
this.allowSafeLegacyUnbound = allowSafeLegacyUnbound;
|
|
418
|
+
this.onRecoveryFailure = deps.onRecoveryFailure;
|
|
419
|
+
}
|
|
420
|
+
async start(spec, options = {}) {
|
|
421
|
+
const input = spec.input ?? {};
|
|
422
|
+
if (Object.prototype.hasOwnProperty.call(input, POLICY_CONTEXT_KEY)) {
|
|
423
|
+
throw new workflow.WorkflowRuntimeError('workflow policy context is runtime-managed', 'INVALID_STATE');
|
|
424
|
+
}
|
|
425
|
+
return await super.start({
|
|
426
|
+
...spec,
|
|
427
|
+
input: { ...input, [POLICY_CONTEXT_KEY]: persistedPolicyContext(this.policyContext) },
|
|
428
|
+
}, options);
|
|
429
|
+
}
|
|
430
|
+
async recoverPending() {
|
|
431
|
+
return await this.recoverRuns((runId) => this.preparePolicyRecovery(runId), (runId, error) => { this.reportPolicyRecoveryFailure(runId, error); });
|
|
432
|
+
}
|
|
433
|
+
async resume(runId, options) {
|
|
434
|
+
this.assertRunPolicyContext(runId);
|
|
435
|
+
return await super.resume(runId, options);
|
|
436
|
+
}
|
|
437
|
+
async operatorResume(runId, options) {
|
|
438
|
+
if (!this.isInternalRejectionResume(runId))
|
|
439
|
+
this.assertRunPolicyContext(runId);
|
|
440
|
+
return await super.operatorResume(runId, options);
|
|
441
|
+
}
|
|
442
|
+
async pause(runId, options) {
|
|
443
|
+
return await super.pause(runId, options);
|
|
444
|
+
}
|
|
445
|
+
async cancel(runId, options) {
|
|
446
|
+
return await super.cancel(runId, options);
|
|
447
|
+
}
|
|
448
|
+
async approve(runId, gateId, options) {
|
|
449
|
+
this.assertRunPolicyContext(runId);
|
|
450
|
+
return await super.approve(runId, gateId, options);
|
|
451
|
+
}
|
|
452
|
+
async reject(runId, gateId, options) {
|
|
453
|
+
this.rejectionResumeDepth.set(runId, (this.rejectionResumeDepth.get(runId) ?? 0) + 1);
|
|
454
|
+
try {
|
|
455
|
+
return await super.reject(runId, gateId, options);
|
|
456
|
+
}
|
|
457
|
+
finally {
|
|
458
|
+
const depth = (this.rejectionResumeDepth.get(runId) ?? 1) - 1;
|
|
459
|
+
if (depth === 0)
|
|
460
|
+
this.rejectionResumeDepth.delete(runId);
|
|
461
|
+
else
|
|
462
|
+
this.rejectionResumeDepth.set(runId, depth);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
isInternalRejectionResume(runId) {
|
|
466
|
+
if ((this.rejectionResumeDepth.get(runId) ?? 0) === 0)
|
|
467
|
+
return false;
|
|
468
|
+
const state = this.store.readStored(runId);
|
|
469
|
+
const executionId = state.status === 'waiting_approval' ? state.approval?.executionId : undefined;
|
|
470
|
+
return executionId !== undefined
|
|
471
|
+
&& this.store.readControl(runId).approvals[executionId]?.decision === 'rejected';
|
|
472
|
+
}
|
|
473
|
+
assertRunPolicyContext(runId) {
|
|
474
|
+
this.assertStatePolicyContext(this.store.readStored(runId));
|
|
475
|
+
}
|
|
476
|
+
assertStatePolicyContext(state) {
|
|
477
|
+
if (!runPolicyContextMatches(state, this.policyContext, this.allowSafeLegacyUnbound)) {
|
|
478
|
+
throw new workflow.WorkflowRuntimeError('workflow policy context does not match the durable run', 'RUN_NOT_RESUMABLE');
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
preparePolicyRecovery(runId) {
|
|
482
|
+
const state = this.store.readStored(runId);
|
|
483
|
+
const control = this.store.readControl(runId);
|
|
484
|
+
if (!this.isRecoveryCandidate(state, control))
|
|
485
|
+
return false;
|
|
486
|
+
const executionId = state.status === 'waiting_approval' ? state.approval?.executionId : undefined;
|
|
487
|
+
const decision = executionId === undefined ? undefined : control.approvals[executionId];
|
|
488
|
+
if (control.cancel || decision?.decision === 'rejected')
|
|
489
|
+
return true;
|
|
490
|
+
const disposition = runPolicyContextDisposition(state, this.policyContext, this.allowSafeLegacyUnbound);
|
|
491
|
+
if (disposition === 'exact')
|
|
492
|
+
return true;
|
|
493
|
+
if (disposition === 'same_home_drift')
|
|
494
|
+
return this.terminalizePolicyDrift(runId);
|
|
495
|
+
throw new workflow.WorkflowRuntimeError('workflow policy owner does not match the durable run', 'RUN_NOT_RESUMABLE');
|
|
496
|
+
}
|
|
497
|
+
terminalizePolicyDrift(runId) {
|
|
498
|
+
this.store.acquireRunLock(runId);
|
|
499
|
+
try {
|
|
500
|
+
const state = this.store.readStored(runId);
|
|
501
|
+
if (['cancelled', 'succeeded', 'failed', 'unsafe_to_resume'].includes(state.status))
|
|
502
|
+
return false;
|
|
503
|
+
if (this.store.readControl(runId).cancel)
|
|
504
|
+
return true;
|
|
505
|
+
const at = new Date().toISOString();
|
|
506
|
+
const history = this.applyPolicyDriftTerminalState(state, at);
|
|
507
|
+
try {
|
|
508
|
+
return this.store.commitUnlessCancelled(state, history) !== undefined;
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
if (!(error instanceof workflow.WorkflowStateError)
|
|
512
|
+
|| !['RESOURCE_LIMIT', 'CORRUPT_STATE', 'UNSAFE_PATH', 'INSECURE_PERMISSIONS'].includes(error.code)) {
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
return this.store.commitUnlessCancelled(state, []) !== undefined;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
finally {
|
|
519
|
+
this.store.releaseRunLock(runId);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
applyPolicyDriftTerminalState(state, at) {
|
|
523
|
+
const current = state.currentStep ? state.steps[state.currentStep] : undefined;
|
|
524
|
+
if (current?.status === 'running' && current.idempotency === 'non_idempotent') {
|
|
525
|
+
current.status = 'failed';
|
|
526
|
+
current.lastErrorClass = 'interrupted_non_idempotent';
|
|
527
|
+
current.completedAt = at;
|
|
528
|
+
state.status = 'unsafe_to_resume';
|
|
529
|
+
state.lastErrorClass = 'interrupted_non_idempotent';
|
|
530
|
+
state.completedAt = at;
|
|
531
|
+
state.updatedAt = at;
|
|
532
|
+
delete state.approval;
|
|
533
|
+
return [{
|
|
534
|
+
runId: state.runId, workflowId: state.workflowId, type: 'unsafe_to_resume',
|
|
535
|
+
status: state.status, at, executionId: current.executionId, stepId: current.stepId,
|
|
536
|
+
errorClass: 'interrupted_non_idempotent',
|
|
537
|
+
}];
|
|
538
|
+
}
|
|
539
|
+
const history = [];
|
|
540
|
+
if (current && current.status !== 'succeeded' && current.status !== 'failed') {
|
|
541
|
+
current.status = 'failed';
|
|
542
|
+
current.lastErrorClass = 'safety';
|
|
543
|
+
current.completedAt = at;
|
|
544
|
+
history.push({
|
|
545
|
+
runId: state.runId, workflowId: state.workflowId, type: 'step_failed',
|
|
546
|
+
status: 'failed', at, executionId: current.executionId, stepId: current.stepId,
|
|
547
|
+
errorClass: 'safety',
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
state.status = 'failed';
|
|
551
|
+
state.lastErrorClass = 'safety';
|
|
552
|
+
state.completedAt = at;
|
|
553
|
+
state.updatedAt = at;
|
|
554
|
+
delete state.approval;
|
|
555
|
+
history.push({
|
|
556
|
+
runId: state.runId, workflowId: state.workflowId, type: 'run_failed',
|
|
557
|
+
status: state.status, at, errorClass: 'safety',
|
|
558
|
+
});
|
|
559
|
+
return history;
|
|
560
|
+
}
|
|
561
|
+
reportPolicyRecoveryFailure(runId, error) {
|
|
562
|
+
try {
|
|
563
|
+
this.onRecoveryFailure?.({
|
|
564
|
+
runId,
|
|
565
|
+
code: error instanceof workflow.WorkflowStateError
|
|
566
|
+
? error.code
|
|
567
|
+
: error instanceof workflow.WorkflowRunFailedError
|
|
568
|
+
? 'RUN_FAILED'
|
|
569
|
+
: 'RECOVERY_FAILED',
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
catch {
|
|
573
|
+
// Startup recovery diagnostics are best-effort and must not block other runs.
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
/** Create the shared production runtime used by CLI commands and daemon recovery. */
|
|
578
|
+
export function createProductionWorkflowRuntime(options = {}) {
|
|
579
|
+
const env = options.env ?? process.env;
|
|
580
|
+
const writeDiagnostic = options.writeDiagnostic ?? ((message) => { process.stderr.write(message); });
|
|
581
|
+
const configuredStateDir = env['EVOLVER_WORKFLOW_STATE_DIR']?.trim();
|
|
582
|
+
const stateDir = resolve(options.stateDir ?? (configuredStateDir || workflow.defaultWorkflowStateDir()));
|
|
583
|
+
const autoExecHome = resolveWorkflowAutoExecHome({ ...options, env });
|
|
584
|
+
const assetsDir = resolveWorkflowAssetsDir({ ...options, env });
|
|
585
|
+
const config = options.autoExecConfig ?? readAutoExecConfig(autoExecHome);
|
|
586
|
+
const allowedRoots = canonicalAllowedRoots(config.allowedRoots);
|
|
587
|
+
const policyContext = createWorkflowPolicyContext(autoExecHome, assetsDir, config, allowedRoots);
|
|
588
|
+
const runtimeOptions = { ...options, env, stateDir, autoExecHome, assetsDir };
|
|
589
|
+
const agent = createProductionWorkflowAgentBridgeFromSnapshot(config, runtimeOptions, policyContext, allowedRoots);
|
|
590
|
+
const allowSafeLegacyUnbound = canonicalPolicyHome(autoExecHome)
|
|
591
|
+
=== canonicalPolicyHome(defaultWorkflowAutoExecHome(env));
|
|
592
|
+
const deps = {
|
|
593
|
+
agent,
|
|
594
|
+
stateDir,
|
|
595
|
+
maxConcurrentRuns: resolveWorkflowMaxConcurrentRuns(env),
|
|
596
|
+
...(options.maxQueuedRuns !== undefined ? { maxQueuedRuns: options.maxQueuedRuns } : {}),
|
|
597
|
+
onRecoveryFailure: ({ code }) => { emitRecoveryDiagnostic(writeDiagnostic, code); },
|
|
598
|
+
};
|
|
599
|
+
return new PolicyBoundWorkflowRuntime(deps, policyContext, allowSafeLegacyUnbound);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Start recovery exactly once without awaiting it. A hung workflow may occupy a scheduler slot, but it cannot delay
|
|
603
|
+
* construction of the resident loop or daemon readiness.
|
|
604
|
+
*/
|
|
605
|
+
export function scheduleWorkflowStartupRecovery(runtime, writeDiagnostic = (message) => { process.stderr.write(message); }) {
|
|
606
|
+
void Promise.resolve()
|
|
607
|
+
.then(async () => { await runtime.recoverPending(); })
|
|
608
|
+
.catch(() => { emitRecoveryDiagnostic(writeDiagnostic, 'RECOVERY_FAILED'); });
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Initialize daemon startup recovery without making workflow state availability a prerequisite for autoexec.
|
|
612
|
+
* Runtime construction can fail on corrupt state, insecure permissions, or concurrent access; those failures are
|
|
613
|
+
* reported as a bounded code while the independent task queue remains available.
|
|
614
|
+
*/
|
|
615
|
+
export function initializeWorkflowStartupRecovery(options, dependencies = {}) {
|
|
616
|
+
const writeDiagnostic = dependencies.writeDiagnostic
|
|
617
|
+
?? options.writeDiagnostic
|
|
618
|
+
?? ((message) => { process.stderr.write(message); });
|
|
619
|
+
try {
|
|
620
|
+
const runtime = (dependencies.createRuntime ?? createProductionWorkflowRuntime)({
|
|
621
|
+
...options,
|
|
622
|
+
writeDiagnostic,
|
|
623
|
+
});
|
|
624
|
+
(dependencies.scheduleRecovery ?? scheduleWorkflowStartupRecovery)(runtime, writeDiagnostic);
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
catch {
|
|
628
|
+
emitRecoveryDiagnostic(writeDiagnostic, 'RECOVERY_INIT_FAILED');
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evomap/evolver-cli",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.10",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "evolver CLI (status/watch/cycles/rebuild-views/...)",
|
|
@@ -20,11 +20,16 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@evomap/evolver-core": "2.0.0-beta.
|
|
24
|
-
"@evomap/evolver-adapter-public": "2.0.0-beta.
|
|
25
|
-
"@evomap/evolver-mcp": "2.0.0-beta.
|
|
26
|
-
"@evomap/evolver-proxy": "2.0.0-beta.
|
|
27
|
-
"@evomap/evolver-runtime-adapters": "2.0.0-beta.
|
|
23
|
+
"@evomap/evolver-core": "2.0.0-beta.10",
|
|
24
|
+
"@evomap/evolver-adapter-public": "2.0.0-beta.10",
|
|
25
|
+
"@evomap/evolver-mcp": "2.0.0-beta.10",
|
|
26
|
+
"@evomap/evolver-proxy": "2.0.0-beta.10",
|
|
27
|
+
"@evomap/evolver-runtime-adapters": "2.0.0-beta.10",
|
|
28
|
+
"@evomap/evolver-webui": "2.0.0-beta.10"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/EvoMap/evolver.git"
|
|
28
33
|
},
|
|
29
34
|
"publishConfig": {
|
|
30
35
|
"access": "public",
|
|
@@ -32,6 +37,7 @@
|
|
|
32
37
|
},
|
|
33
38
|
"files": [
|
|
34
39
|
"dist/",
|
|
40
|
+
"assets/",
|
|
35
41
|
"README.md",
|
|
36
42
|
"package.json"
|
|
37
43
|
]
|