@evomap/evolver-cli 2.0.0-beta.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/dist/antiGeneBenchmark.d.ts +9 -0
- package/dist/antiGeneBenchmark.js +173 -0
- package/dist/antiGeneRollout.d.ts +14 -0
- package/dist/antiGeneRollout.js +276 -0
- package/dist/atp.d.ts +122 -0
- package/dist/atp.js +413 -0
- package/dist/atpAutoDeliver.d.ts +53 -0
- package/dist/atpAutoDeliver.js +197 -0
- package/dist/atpAutobuyPrompt.d.ts +48 -0
- package/dist/atpAutobuyPrompt.js +109 -0
- package/dist/autoDistillAntiGene.d.ts +63 -0
- package/dist/autoDistillAntiGene.js +396 -0
- package/dist/autoDistillLlm.d.ts +94 -0
- package/dist/autoDistillLlm.js +510 -0
- package/dist/autoDistillTranscript.d.ts +87 -0
- package/dist/autoDistillTranscript.js +364 -0
- package/dist/autoRecall.d.ts +25 -0
- package/dist/autoRecall.js +80 -0
- package/dist/autoexec.d.ts +237 -0
- package/dist/autoexec.js +1041 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +7 -0
- package/dist/cliContracts.d.ts +72 -0
- package/dist/cliContracts.js +815 -0
- package/dist/commands.d.ts +9 -0
- package/dist/commands.js +10 -0
- package/dist/cursorRewrite.d.ts +42 -0
- package/dist/cursorRewrite.js +65 -0
- package/dist/cycleConsumer.d.ts +42 -0
- package/dist/cycleConsumer.js +519 -0
- package/dist/daemonLoop.d.ts +33 -0
- package/dist/daemonLoop.js +72 -0
- package/dist/dispatch.d.ts +10 -0
- package/dist/dispatch.js +75 -0
- package/dist/distillObserver.d.ts +33 -0
- package/dist/distillObserver.js +146 -0
- package/dist/distillPrimitives.d.ts +48 -0
- package/dist/distillPrimitives.js +142 -0
- package/dist/doctor.d.ts +54 -0
- package/dist/doctor.js +931 -0
- package/dist/geneValue.d.ts +37 -0
- package/dist/geneValue.js +97 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +1135 -0
- package/dist/lifecycle.d.ts +75 -0
- package/dist/lifecycle.js +859 -0
- package/dist/login.d.ts +10 -0
- package/dist/login.js +38 -0
- package/dist/migrate/divergenceAudit.d.ts +29 -0
- package/dist/migrate/divergenceAudit.js +37 -0
- package/dist/migrate/fieldMap.d.ts +18 -0
- package/dist/migrate/fieldMap.js +68 -0
- package/dist/migrate/v1Import.d.ts +16 -0
- package/dist/migrate/v1Import.js +118 -0
- package/dist/phub.d.ts +18 -0
- package/dist/phub.js +284 -0
- package/dist/recall.d.ts +26 -0
- package/dist/recall.js +162 -0
- package/dist/recipe.d.ts +61 -0
- package/dist/recipe.js +564 -0
- package/dist/reflectionObserver.d.ts +8 -0
- package/dist/reflectionObserver.js +6 -0
- package/dist/reuse.d.ts +48 -0
- package/dist/reuse.js +300 -0
- package/dist/reuseReport.d.ts +20 -0
- package/dist/reuseReport.js +255 -0
- package/dist/reviewFilter.d.ts +4 -0
- package/dist/reviewFilter.js +6 -0
- package/dist/runtimeSessionSource.d.ts +9 -0
- package/dist/runtimeSessionSource.js +29 -0
- package/dist/setupHooks.d.ts +2 -0
- package/dist/setupHooks.js +410 -0
- package/dist/skill2gep.d.ts +144 -0
- package/dist/skill2gep.js +321 -0
- package/dist/skillDistill.d.ts +75 -0
- package/dist/skillDistill.js +342 -0
- package/dist/solo/breaker.d.ts +14 -0
- package/dist/solo/breaker.js +21 -0
- package/dist/solo/gitGuard.d.ts +17 -0
- package/dist/solo/gitGuard.js +62 -0
- package/dist/solo/mode.d.ts +12 -0
- package/dist/solo/mode.js +55 -0
- package/dist/thesis.d.ts +43 -0
- package/dist/thesis.js +179 -0
- package/dist/trajectoryExport.d.ts +1 -0
- package/dist/trajectoryExport.js +895 -0
- package/dist/valueDigest.d.ts +33 -0
- package/dist/valueDigest.js +94 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +14 -0
- package/package.json +38 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { dirname, join, resolve } from 'node:path';
|
|
2
|
+
import { assetstore, algo, events, exec, material as materialNs, personality, schema, signals, util } from '@evomap/evolver-core';
|
|
3
|
+
import { parseRuntimeSessionSources } from './runtimeSessionSource.js';
|
|
4
|
+
import { signalTokens } from './distillPrimitives.js';
|
|
5
|
+
import { readEvents, showCycle } from './commands.js';
|
|
6
|
+
const CYCLE_GROUP = 'cycle';
|
|
7
|
+
const DEFAULT_LIMIT = 5;
|
|
8
|
+
function parseFlags(argv) {
|
|
9
|
+
const out = {};
|
|
10
|
+
for (let i = 0; i < argv.length; i++) {
|
|
11
|
+
const arg = argv[i];
|
|
12
|
+
if (!arg?.startsWith('--'))
|
|
13
|
+
continue;
|
|
14
|
+
const next = argv[i + 1];
|
|
15
|
+
if (next !== undefined && !next.startsWith('--')) {
|
|
16
|
+
out[arg.slice(2)] = next;
|
|
17
|
+
i++;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
out[arg.slice(2)] = '';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
function parsePositiveInt(value, fallback) {
|
|
26
|
+
if (value === undefined || value.trim() === '')
|
|
27
|
+
return fallback;
|
|
28
|
+
const n = Number(value);
|
|
29
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
30
|
+
}
|
|
31
|
+
function parseRunner(value) {
|
|
32
|
+
return value === 'codex' || value === 'cursor' ? value : 'claude';
|
|
33
|
+
}
|
|
34
|
+
function resolveMaterialCycleDeps(deps = {}) {
|
|
35
|
+
const materialStore = deps.materialStore ?? new materialNs.MaterialStore({ path: events.materialStorePath() });
|
|
36
|
+
const consumer = deps.consumer ?? new materialNs.ConsumerGroups({
|
|
37
|
+
store: materialStore,
|
|
38
|
+
path: join(dirname(events.materialStorePath()), 'cycle-consumer.json'),
|
|
39
|
+
});
|
|
40
|
+
const store = deps.store ?? new assetstore.LocalJsonlProvider(events.assetsDir());
|
|
41
|
+
const assetDir = store instanceof assetstore.LocalJsonlProvider ? store.baseDir : events.assetsDir();
|
|
42
|
+
const provenance = deps.provenance ?? new assetstore.ProvenanceStore(assetDir);
|
|
43
|
+
const review = deps.review ?? new assetstore.ReviewLedger(assetDir);
|
|
44
|
+
const ingestor = deps.ingestor ?? new events.Ingestor({ path: events.rootEventsPath() });
|
|
45
|
+
const personalityStore = deps.personality ?? new personality.PersonalityStore();
|
|
46
|
+
const engine = deps.engine ?? new algo.CycleEngine({
|
|
47
|
+
ingestor,
|
|
48
|
+
selection: algo.makeGeneSelectionPoint(),
|
|
49
|
+
store,
|
|
50
|
+
now: () => Date.now(),
|
|
51
|
+
personality: personalityStore,
|
|
52
|
+
});
|
|
53
|
+
return { materialStore, consumer, store, provenance, review, ingestor, engine, personality: personalityStore };
|
|
54
|
+
}
|
|
55
|
+
export function cycleIdForMaterial(materialId) {
|
|
56
|
+
return `autoexec-material-${materialId}`;
|
|
57
|
+
}
|
|
58
|
+
function consumedEventExists(rootEvents, materialId) {
|
|
59
|
+
return rootEvents.some((event) => {
|
|
60
|
+
if (event.type !== 'cycle.consumed')
|
|
61
|
+
return false;
|
|
62
|
+
const payload = event.payload;
|
|
63
|
+
return payload?.materialId === materialId;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function terminalCycleFor(rootEvents, cycleId) {
|
|
67
|
+
for (const event of rootEvents) {
|
|
68
|
+
if (event.type !== 'cycle.solidified' && event.type !== 'cycle.failed' && event.type !== 'cycle.aborted')
|
|
69
|
+
continue;
|
|
70
|
+
const payload = event.payload;
|
|
71
|
+
if (payload?.cycleId !== cycleId)
|
|
72
|
+
continue;
|
|
73
|
+
if (event.type === 'cycle.solidified')
|
|
74
|
+
return { type: event.type, finalStage: 'solidified' };
|
|
75
|
+
if (event.type === 'cycle.failed')
|
|
76
|
+
return { type: event.type, finalStage: 'failed' };
|
|
77
|
+
return { type: event.type, finalStage: 'aborted' };
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
function consumedPayloadFor(rootEvents, materialId) {
|
|
82
|
+
for (const event of rootEvents) {
|
|
83
|
+
if (event.type !== 'cycle.consumed')
|
|
84
|
+
continue;
|
|
85
|
+
const payload = event.payload;
|
|
86
|
+
if (payload?.['materialId'] === materialId)
|
|
87
|
+
return payload;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
function inFlightCycleExists(rootEvents, cycleId) {
|
|
92
|
+
let started = false;
|
|
93
|
+
for (const event of rootEvents) {
|
|
94
|
+
const payload = event.payload;
|
|
95
|
+
if (payload?.cycleId !== cycleId)
|
|
96
|
+
continue;
|
|
97
|
+
if (event.type === 'cycle.solidified' || event.type === 'cycle.failed' || event.type === 'cycle.aborted')
|
|
98
|
+
return false;
|
|
99
|
+
if (event.type === 'cycle.started')
|
|
100
|
+
started = true;
|
|
101
|
+
}
|
|
102
|
+
return started;
|
|
103
|
+
}
|
|
104
|
+
function failureConsumedPayload(payload) {
|
|
105
|
+
return payload['action'] === 'fail'
|
|
106
|
+
|| payload['status'] === 'parse_failed'
|
|
107
|
+
|| payload['status'] === 'no_signals'
|
|
108
|
+
|| payload['status'] === 'refused'
|
|
109
|
+
|| payload['status'] === 'failed';
|
|
110
|
+
}
|
|
111
|
+
function statusForMaterial(material, rootEvents) {
|
|
112
|
+
const consumed = consumedPayloadFor(rootEvents, material.materialId);
|
|
113
|
+
if (consumed)
|
|
114
|
+
return consumed['action'] === 'observe' || consumed['status'] === 'observed' ? 'observed' : 'consumed';
|
|
115
|
+
const cycleId = cycleIdForMaterial(material.materialId);
|
|
116
|
+
if (terminalCycleFor(rootEvents, cycleId))
|
|
117
|
+
return 'terminal_missing_consumed';
|
|
118
|
+
if (inFlightCycleExists(rootEvents, cycleId))
|
|
119
|
+
return 'in_flight';
|
|
120
|
+
return 'pending';
|
|
121
|
+
}
|
|
122
|
+
export function cycleLockPathForMaterial(materialStorePath, materialId) {
|
|
123
|
+
const safeId = materialId.replace(/[^A-Za-z0-9_.-]+/g, '_');
|
|
124
|
+
return `${materialStorePath}.cycle-${safeId}.lock`;
|
|
125
|
+
}
|
|
126
|
+
function tryAcquireCycleLock(lockPath) {
|
|
127
|
+
try {
|
|
128
|
+
util.acquireLock(lockPath, { maxTries: 3, waitMs: 1 });
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (error instanceof util.LockTimeoutError)
|
|
133
|
+
return false;
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function fallbackSignalTokens(sigs) {
|
|
138
|
+
const out = new Set();
|
|
139
|
+
for (const sig of sigs) {
|
|
140
|
+
if (sig.strength === 'agent') {
|
|
141
|
+
const token = sig.text.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 48);
|
|
142
|
+
if (token)
|
|
143
|
+
out.add(token);
|
|
144
|
+
}
|
|
145
|
+
else if (sig.strength === 'strong') {
|
|
146
|
+
out.add(sig.kind);
|
|
147
|
+
}
|
|
148
|
+
if (out.size >= 8)
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
return [...out];
|
|
152
|
+
}
|
|
153
|
+
function materialSignals(material) {
|
|
154
|
+
const sources = parseRuntimeSessionSources(material.sourcePath);
|
|
155
|
+
const sigs = sources.flatMap((source) => signals.extractSignals(source.turns));
|
|
156
|
+
const tokens = signalTokens(sigs);
|
|
157
|
+
return {
|
|
158
|
+
signals: tokens.length > 0 ? tokens : fallbackSignalTokens(sigs),
|
|
159
|
+
sourceCount: sources.length,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
async function emitConsumed(ingestor, material, item, extra = {}) {
|
|
163
|
+
await ingestor.ingest({
|
|
164
|
+
type: 'cycle.consumed',
|
|
165
|
+
human: {
|
|
166
|
+
title: `material ${material.materialId} ${item.status}`,
|
|
167
|
+
...(item.reason ? { detail: item.reason } : {}),
|
|
168
|
+
severity: item.action === 'fail' ? 'warn' : 'info',
|
|
169
|
+
},
|
|
170
|
+
payload: {
|
|
171
|
+
materialId: material.materialId,
|
|
172
|
+
sourceKind: material.sourceKind,
|
|
173
|
+
kind: material.kind,
|
|
174
|
+
sourcePath: material.sourcePath,
|
|
175
|
+
action: item.action,
|
|
176
|
+
status: item.status,
|
|
177
|
+
...(item.cycleId ? { cycleId: item.cycleId } : {}),
|
|
178
|
+
...(item.reason ? { reason: item.reason } : {}),
|
|
179
|
+
...extra,
|
|
180
|
+
},
|
|
181
|
+
actor: { kind: 'machine' },
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function ackOne(consumer, material) {
|
|
185
|
+
consumer.ack(CYCLE_GROUP, [material.materialId]);
|
|
186
|
+
}
|
|
187
|
+
async function processMaterial(material, opts, deps) {
|
|
188
|
+
const cycleId = cycleIdForMaterial(material.materialId);
|
|
189
|
+
const lockPath = cycleLockPathForMaterial(deps.materialStore.path, material.materialId);
|
|
190
|
+
if (!tryAcquireCycleLock(lockPath)) {
|
|
191
|
+
return {
|
|
192
|
+
materialId: material.materialId,
|
|
193
|
+
action: 'skip',
|
|
194
|
+
status: 'already_running',
|
|
195
|
+
cycleId,
|
|
196
|
+
reason: 'cycle consumer lock is held',
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
const rootEvents = deps.ingestor.readAll();
|
|
201
|
+
if (consumedEventExists(rootEvents, material.materialId)) {
|
|
202
|
+
const item = { materialId: material.materialId, action: 'skip', status: 'already_consumed', cycleId };
|
|
203
|
+
ackOne(deps.consumer, material);
|
|
204
|
+
return item;
|
|
205
|
+
}
|
|
206
|
+
const terminal = terminalCycleFor(rootEvents, cycleId);
|
|
207
|
+
if (terminal) {
|
|
208
|
+
const item = { materialId: material.materialId, action: 'skip', status: 'already_terminal', cycleId };
|
|
209
|
+
await emitConsumed(deps.ingestor, material, item, { terminalEvent: terminal.type, finalStage: terminal.finalStage });
|
|
210
|
+
ackOne(deps.consumer, material);
|
|
211
|
+
return item;
|
|
212
|
+
}
|
|
213
|
+
if (inFlightCycleExists(rootEvents, cycleId)) {
|
|
214
|
+
return {
|
|
215
|
+
materialId: material.materialId,
|
|
216
|
+
action: 'skip',
|
|
217
|
+
status: 'already_running',
|
|
218
|
+
cycleId,
|
|
219
|
+
reason: 'cycle already started without terminal event',
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (material.sourceKind === 'proxy_trace' || material.kind === 'llm_trace') {
|
|
223
|
+
const item = { materialId: material.materialId, action: 'observe', status: 'observed' };
|
|
224
|
+
await emitConsumed(deps.ingestor, material, item, { reason: 'proxy_trace is observation-only for cycle consumer' });
|
|
225
|
+
ackOne(deps.consumer, material);
|
|
226
|
+
return item;
|
|
227
|
+
}
|
|
228
|
+
let extracted;
|
|
229
|
+
try {
|
|
230
|
+
extracted = materialSignals(material);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
234
|
+
const item = { materialId: material.materialId, action: 'fail', status: 'parse_failed', cycleId, reason };
|
|
235
|
+
await emitConsumed(deps.ingestor, material, item);
|
|
236
|
+
ackOne(deps.consumer, material);
|
|
237
|
+
return item;
|
|
238
|
+
}
|
|
239
|
+
if (extracted.sourceCount === 0 || extracted.signals.length === 0) {
|
|
240
|
+
const reason = extracted.sourceCount === 0 ? 'no parseable runtime session source' : 'no cycle-worthy session signals';
|
|
241
|
+
const item = { materialId: material.materialId, action: 'fail', status: 'no_signals', cycleId, reason };
|
|
242
|
+
await emitConsumed(deps.ingestor, material, item, { sourceCount: extracted.sourceCount, signalCount: extracted.signals.length });
|
|
243
|
+
ackOne(deps.consumer, material);
|
|
244
|
+
return item;
|
|
245
|
+
}
|
|
246
|
+
const repo = resolve(opts.repo);
|
|
247
|
+
const task = {
|
|
248
|
+
id: `material-${material.materialId}`,
|
|
249
|
+
repo,
|
|
250
|
+
target: opts.target ?? '.',
|
|
251
|
+
expectedEffect: opts.expectedEffect ?? 'evolve from consumed session material',
|
|
252
|
+
signals: extracted.signals,
|
|
253
|
+
};
|
|
254
|
+
const safety = {
|
|
255
|
+
allowedRoots: [repo],
|
|
256
|
+
runner: opts.runner ?? 'claude',
|
|
257
|
+
...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}),
|
|
258
|
+
...opts.safety,
|
|
259
|
+
};
|
|
260
|
+
const verdict = await exec.runAutoExecTask({
|
|
261
|
+
engine: deps.engine,
|
|
262
|
+
store: deps.store,
|
|
263
|
+
provenance: deps.provenance,
|
|
264
|
+
review: deps.review,
|
|
265
|
+
personality: deps.personality,
|
|
266
|
+
...(opts.agent ? { agent: opts.agent } : {}),
|
|
267
|
+
...(opts.git ? { git: opts.git } : {}),
|
|
268
|
+
}, task, safety);
|
|
269
|
+
const item = {
|
|
270
|
+
materialId: material.materialId,
|
|
271
|
+
action: 'cycle',
|
|
272
|
+
status: verdict.status,
|
|
273
|
+
cycleId,
|
|
274
|
+
...(verdict.reason ? { reason: verdict.reason } : {}),
|
|
275
|
+
};
|
|
276
|
+
await emitConsumed(deps.ingestor, material, item, {
|
|
277
|
+
finalStage: verdict.finalStage,
|
|
278
|
+
signalCount: extracted.signals.length,
|
|
279
|
+
signals: extracted.signals,
|
|
280
|
+
});
|
|
281
|
+
ackOne(deps.consumer, material);
|
|
282
|
+
return item;
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
util.releaseLock(lockPath);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
export async function runMaterialCycleConsumer(opts, injectedDeps = {}) {
|
|
289
|
+
const deps = resolveMaterialCycleDeps(injectedDeps);
|
|
290
|
+
const limit = Math.max(1, opts.limit ?? DEFAULT_LIMIT);
|
|
291
|
+
const claimed = deps.consumer.claim(CYCLE_GROUP, limit);
|
|
292
|
+
const items = [];
|
|
293
|
+
for (const material of claimed) {
|
|
294
|
+
items.push(await processMaterial(material, opts, deps));
|
|
295
|
+
}
|
|
296
|
+
return { claimed: claimed.length, processed: items.length, items };
|
|
297
|
+
}
|
|
298
|
+
async function recoverMaterialCycleAudit(material, deps) {
|
|
299
|
+
const cycleId = cycleIdForMaterial(material.materialId);
|
|
300
|
+
const lockPath = cycleLockPathForMaterial(deps.materialStore.path, material.materialId);
|
|
301
|
+
if (!tryAcquireCycleLock(lockPath)) {
|
|
302
|
+
return {
|
|
303
|
+
materialId: material.materialId,
|
|
304
|
+
action: 'skip',
|
|
305
|
+
status: 'already_running',
|
|
306
|
+
cycleId,
|
|
307
|
+
reason: 'cycle consumer lock is held',
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
const rootEvents = deps.ingestor.readAll();
|
|
312
|
+
if (consumedEventExists(rootEvents, material.materialId)) {
|
|
313
|
+
const item = { materialId: material.materialId, action: 'skip', status: 'already_consumed', cycleId };
|
|
314
|
+
ackOne(deps.consumer, material);
|
|
315
|
+
return item;
|
|
316
|
+
}
|
|
317
|
+
const terminal = terminalCycleFor(rootEvents, cycleId);
|
|
318
|
+
if (terminal) {
|
|
319
|
+
const item = { materialId: material.materialId, action: 'skip', status: 'already_terminal', cycleId };
|
|
320
|
+
await emitConsumed(deps.ingestor, material, item, { terminalEvent: terminal.type, finalStage: terminal.finalStage });
|
|
321
|
+
ackOne(deps.consumer, material);
|
|
322
|
+
return item;
|
|
323
|
+
}
|
|
324
|
+
if (inFlightCycleExists(rootEvents, cycleId)) {
|
|
325
|
+
return {
|
|
326
|
+
materialId: material.materialId,
|
|
327
|
+
action: 'skip',
|
|
328
|
+
status: 'already_running',
|
|
329
|
+
cycleId,
|
|
330
|
+
reason: 'cycle already started without terminal event',
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
materialId: material.materialId,
|
|
335
|
+
action: 'skip',
|
|
336
|
+
status: 'pending',
|
|
337
|
+
cycleId,
|
|
338
|
+
reason: 'cycle has not reached a terminal event',
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
finally {
|
|
342
|
+
util.releaseLock(lockPath);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
async function recoverMaterialCycleConsumer(opts = {}, injectedDeps = {}) {
|
|
346
|
+
const deps = resolveMaterialCycleDeps(injectedDeps);
|
|
347
|
+
const limit = Math.max(1, opts.limit ?? DEFAULT_LIMIT);
|
|
348
|
+
const claimed = deps.consumer.claim(CYCLE_GROUP, limit);
|
|
349
|
+
const items = [];
|
|
350
|
+
for (const material of claimed) {
|
|
351
|
+
items.push(await recoverMaterialCycleAudit(material, deps));
|
|
352
|
+
}
|
|
353
|
+
const recovered = items.filter((item) => item.status === 'already_terminal').length;
|
|
354
|
+
const alreadyConsumed = items.filter((item) => item.status === 'already_consumed').length;
|
|
355
|
+
return {
|
|
356
|
+
ok: true,
|
|
357
|
+
group: CYCLE_GROUP,
|
|
358
|
+
claimed: claimed.length,
|
|
359
|
+
recovered,
|
|
360
|
+
alreadyConsumed,
|
|
361
|
+
blocked: claimed.length - recovered - alreadyConsumed,
|
|
362
|
+
items,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function summarizeCycleConsumer(injectedDeps = {}) {
|
|
366
|
+
const deps = resolveMaterialCycleDeps(injectedDeps);
|
|
367
|
+
const rootEvents = deps.ingestor.readAll();
|
|
368
|
+
const materials = deps.materialStore.readAll();
|
|
369
|
+
const cursor = deps.consumer.position(CYCLE_GROUP);
|
|
370
|
+
let pending = 0;
|
|
371
|
+
let consumed = 0;
|
|
372
|
+
let inFlight = 0;
|
|
373
|
+
let failed = 0;
|
|
374
|
+
let observed = 0;
|
|
375
|
+
let recoverable = 0;
|
|
376
|
+
let recoverLimit = null;
|
|
377
|
+
const next = [];
|
|
378
|
+
materials.forEach((material, index) => {
|
|
379
|
+
const status = statusForMaterial(material, rootEvents);
|
|
380
|
+
const consumedPayload = consumedPayloadFor(rootEvents, material.materialId);
|
|
381
|
+
if (status === 'consumed' || status === 'observed')
|
|
382
|
+
consumed += 1;
|
|
383
|
+
if (status === 'observed')
|
|
384
|
+
observed += 1;
|
|
385
|
+
if (consumedPayload && failureConsumedPayload(consumedPayload))
|
|
386
|
+
failed += 1;
|
|
387
|
+
if (status === 'in_flight')
|
|
388
|
+
inFlight += 1;
|
|
389
|
+
if (status === 'pending' || status === 'terminal_missing_consumed')
|
|
390
|
+
pending += 1;
|
|
391
|
+
if (status === 'terminal_missing_consumed') {
|
|
392
|
+
recoverable += 1;
|
|
393
|
+
if (index >= cursor && recoverLimit === null)
|
|
394
|
+
recoverLimit = index - cursor + 1;
|
|
395
|
+
}
|
|
396
|
+
if (index >= cursor && next.length < 5 && status !== 'consumed' && status !== 'observed') {
|
|
397
|
+
next.push({
|
|
398
|
+
materialId: material.materialId,
|
|
399
|
+
sourceKind: material.sourceKind,
|
|
400
|
+
kind: material.kind,
|
|
401
|
+
capturedAt: material.capturedAt,
|
|
402
|
+
status,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
return {
|
|
407
|
+
ok: true,
|
|
408
|
+
group: CYCLE_GROUP,
|
|
409
|
+
total: materials.length,
|
|
410
|
+
cursor,
|
|
411
|
+
pending,
|
|
412
|
+
consumed,
|
|
413
|
+
inFlight,
|
|
414
|
+
failed,
|
|
415
|
+
observed,
|
|
416
|
+
recoverable,
|
|
417
|
+
recoverLimit,
|
|
418
|
+
next,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
function printResult(result) {
|
|
422
|
+
if (result.claimed === 0) {
|
|
423
|
+
process.stdout.write('cycle: no pending material\n');
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
for (const item of result.items) {
|
|
427
|
+
const cycle = item.cycleId ? ` cycle=${item.cycleId}` : '';
|
|
428
|
+
const reason = item.reason ? ' reason=details-redacted' : '';
|
|
429
|
+
process.stdout.write(`cycle: material=${item.materialId} action=${item.action} status=${item.status}${cycle}${reason}\n`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function printStatus(summary) {
|
|
433
|
+
process.stdout.write(`cycle status: group=${summary.group} total=${summary.total} cursor=${summary.cursor} pending=${summary.pending} consumed=${summary.consumed} inFlight=${summary.inFlight} failed=${summary.failed} observed=${summary.observed} recoverable=${summary.recoverable}\n`);
|
|
434
|
+
if (summary.next.length === 0) {
|
|
435
|
+
process.stdout.write(' next: none\n');
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
process.stdout.write(' next:\n');
|
|
439
|
+
for (const item of summary.next) {
|
|
440
|
+
process.stdout.write(` ${item.materialId} sourceKind=${item.sourceKind} kind=${item.kind} capturedAt=${item.capturedAt} status=${item.status}\n`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (summary.recoverLimit !== null) {
|
|
444
|
+
process.stdout.write(` recovery: run \`evolver cycle recover --limit ${summary.recoverLimit}\` to emit missing cycle.consumed without running an agent\n`);
|
|
445
|
+
}
|
|
446
|
+
else if (summary.recoverable > 0) {
|
|
447
|
+
process.stdout.write(' recovery: terminal_missing_consumed exists before the current cursor; inspect cursor/root events before resetting\n');
|
|
448
|
+
}
|
|
449
|
+
if (summary.inFlight > 0) {
|
|
450
|
+
process.stdout.write(' note: in_flight material is left untouched until a terminal cycle event appears\n');
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function printRecoverResult(result) {
|
|
454
|
+
process.stdout.write(`cycle recover: group=${result.group} claimed=${result.claimed} recovered=${result.recovered} alreadyConsumed=${result.alreadyConsumed} blocked=${result.blocked}\n`);
|
|
455
|
+
if (result.claimed === 0) {
|
|
456
|
+
process.stdout.write(' items: none\n');
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
for (const item of result.items) {
|
|
460
|
+
const cycle = item.cycleId ? ` cycle=${item.cycleId}` : '';
|
|
461
|
+
const reason = item.reason ? ' reason=details-redacted' : '';
|
|
462
|
+
process.stdout.write(` material=${item.materialId} action=${item.action} status=${item.status}${cycle}${reason}\n`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function hasFailedItem(item) {
|
|
466
|
+
return item.action === 'fail'
|
|
467
|
+
|| item.status === 'refused'
|
|
468
|
+
|| item.status === 'failed'
|
|
469
|
+
|| item.status === 'already_running';
|
|
470
|
+
}
|
|
471
|
+
function materialCycleExitCode(result) {
|
|
472
|
+
return result.items.some(hasFailedItem) ? 1 : 0;
|
|
473
|
+
}
|
|
474
|
+
export async function runCycleCommand(argv, injectedDeps = {}) {
|
|
475
|
+
if (argv[0] === 'show') {
|
|
476
|
+
if (!argv[1]) {
|
|
477
|
+
process.stderr.write('usage: evolver cycle show <id>\n');
|
|
478
|
+
return 1;
|
|
479
|
+
}
|
|
480
|
+
for (const t of showCycle(readEvents(), argv[1]).timeline)
|
|
481
|
+
process.stdout.write(`#${t.seq} ${t.type} ${t.title}\n`);
|
|
482
|
+
return 0;
|
|
483
|
+
}
|
|
484
|
+
if (argv[0] === 'status') {
|
|
485
|
+
const flags = parseFlags(argv.slice(1));
|
|
486
|
+
const summary = summarizeCycleConsumer(injectedDeps);
|
|
487
|
+
if ('json' in flags)
|
|
488
|
+
process.stdout.write(`${JSON.stringify(summary)}\n`);
|
|
489
|
+
else
|
|
490
|
+
printStatus(summary);
|
|
491
|
+
return 0;
|
|
492
|
+
}
|
|
493
|
+
if (argv[0] === 'recover') {
|
|
494
|
+
const flags = parseFlags(argv.slice(1));
|
|
495
|
+
const result = await recoverMaterialCycleConsumer({
|
|
496
|
+
limit: parsePositiveInt(flags['limit'], DEFAULT_LIMIT),
|
|
497
|
+
}, injectedDeps);
|
|
498
|
+
if ('json' in flags)
|
|
499
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
500
|
+
else
|
|
501
|
+
printRecoverResult(result);
|
|
502
|
+
return 0;
|
|
503
|
+
}
|
|
504
|
+
const flags = parseFlags(argv);
|
|
505
|
+
const repo = flags['repo'];
|
|
506
|
+
if (!repo) {
|
|
507
|
+
process.stderr.write('usage: evolver cycle show <id> | evolver cycle status [--json] | evolver cycle recover [--limit N] [--json] | evolver cycle --repo <path> [--limit N] [--target <path>] [--expected-effect <text>] [--runner claude|codex|cursor]\n');
|
|
508
|
+
return 1;
|
|
509
|
+
}
|
|
510
|
+
const result = await runMaterialCycleConsumer({
|
|
511
|
+
repo,
|
|
512
|
+
limit: parsePositiveInt(flags['limit'], DEFAULT_LIMIT),
|
|
513
|
+
target: flags['target'] || '.',
|
|
514
|
+
expectedEffect: flags['expected-effect'] || 'evolve from consumed session material',
|
|
515
|
+
runner: parseRunner(flags['runner']),
|
|
516
|
+
}, injectedDeps);
|
|
517
|
+
printResult(result);
|
|
518
|
+
return materialCycleExitCode(result);
|
|
519
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { daemon } from '@evomap/evolver-core';
|
|
2
|
+
export interface Beat {
|
|
3
|
+
intensity: daemon.Intensity;
|
|
4
|
+
/** The delay until the NEXT tick (ms), after applying the idle multiplier. */
|
|
5
|
+
delayMs: number;
|
|
6
|
+
idleSeconds: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ResidentLoopDeps {
|
|
9
|
+
/** The work to run each tick. Should already be single-flight-guarded by the caller; never expected to throw. */
|
|
10
|
+
tick: (beat: Beat) => unknown | Promise<unknown>;
|
|
11
|
+
/** Base poll cadence (ms). Idle multiplier scales this: aggressive ×0.5, deep ×0.25, normal ×1. */
|
|
12
|
+
basePollMs: number;
|
|
13
|
+
/** Idle-aware pacing (default true). False → fixed `basePollMs` cadence. */
|
|
14
|
+
idleAware?: boolean;
|
|
15
|
+
idleProbe?: daemon.IdleProbe;
|
|
16
|
+
thresholds?: daemon.IdleThresholds;
|
|
17
|
+
/** Called after each tick with the next-delay decision — the daemon emits its heartbeat here. */
|
|
18
|
+
onBeat?: (beat: Beat) => void;
|
|
19
|
+
/** Run the first tick immediately (default true). False → wait one delay before the first tick. */
|
|
20
|
+
runFirstImmediately?: boolean;
|
|
21
|
+
setTimer?: (fn: () => void, ms: number) => ReturnType<typeof setTimeout>;
|
|
22
|
+
clearTimer?: (handle: ReturnType<typeof setTimeout>) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface ResidentLoopHandle {
|
|
25
|
+
/** Stop scheduling: cancel the pending tick and await any in-flight one. Idempotent. */
|
|
26
|
+
stop: () => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Start an idle-aware, self-rescheduling resident loop. Each tick is followed by a delay of
|
|
30
|
+
* `basePollMs × idleMultiplier` (idle machine → smaller delay → runs more often). `stop()` makes a graceful
|
|
31
|
+
* shutdown: no further ticks are scheduled and an in-flight tick is awaited, so there is no orphaned work.
|
|
32
|
+
*/
|
|
33
|
+
export declare function startResidentLoop(deps: ResidentLoopDeps): ResidentLoopHandle;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Resident-loop lifecycle for `evolver autoexec` (#106 daemon hardening). Extracted from the (untestable, never-
|
|
2
|
+
// resolving) runAutoExec shell so the SCHEDULING is unit-testable: an idle-aware, self-rescheduling timer that runs
|
|
3
|
+
// harder when the machine is idle and backs off when the user is active, plus a graceful stop that cancels the next
|
|
4
|
+
// tick and waits out an in-flight one. Pure of any autoexec specifics — the caller injects the (single-flight-
|
|
5
|
+
// guarded) work as `tick` and observes each beat via `onBeat` (the daemon emits a heartbeat from there).
|
|
6
|
+
import { daemon } from '@evomap/evolver-core';
|
|
7
|
+
/**
|
|
8
|
+
* Start an idle-aware, self-rescheduling resident loop. Each tick is followed by a delay of
|
|
9
|
+
* `basePollMs × idleMultiplier` (idle machine → smaller delay → runs more often). `stop()` makes a graceful
|
|
10
|
+
* shutdown: no further ticks are scheduled and an in-flight tick is awaited, so there is no orphaned work.
|
|
11
|
+
*/
|
|
12
|
+
export function startResidentLoop(deps) {
|
|
13
|
+
const setTimer = deps.setTimer ?? ((fn, ms) => setTimeout(fn, ms));
|
|
14
|
+
const clearTimer = deps.clearTimer ?? ((h) => clearTimeout(h));
|
|
15
|
+
const idleAware = deps.idleAware !== false;
|
|
16
|
+
const probe = deps.idleProbe ?? daemon.defaultIdleProbe;
|
|
17
|
+
let stopped = false;
|
|
18
|
+
let timer = null;
|
|
19
|
+
let inFlight = null;
|
|
20
|
+
const planNext = () => {
|
|
21
|
+
const rec = daemon.recommendSchedule(idleAware ? probe() : -1, {
|
|
22
|
+
enabled: idleAware,
|
|
23
|
+
...(deps.thresholds ? { thresholds: deps.thresholds } : {}),
|
|
24
|
+
});
|
|
25
|
+
return { intensity: rec.intensity, idleSeconds: rec.idleSeconds, delayMs: Math.max(1, Math.round(deps.basePollMs * rec.sleepMultiplier)) };
|
|
26
|
+
};
|
|
27
|
+
const scheduleAfter = (delayMs) => {
|
|
28
|
+
if (stopped)
|
|
29
|
+
return;
|
|
30
|
+
timer = setTimer(() => { void run(); }, delayMs);
|
|
31
|
+
};
|
|
32
|
+
const run = async () => {
|
|
33
|
+
if (stopped)
|
|
34
|
+
return;
|
|
35
|
+
const beat = planNext();
|
|
36
|
+
try {
|
|
37
|
+
inFlight = Promise.resolve(deps.tick(beat));
|
|
38
|
+
await inFlight;
|
|
39
|
+
}
|
|
40
|
+
catch { /* tick is expected to swallow its own errors */ }
|
|
41
|
+
finally {
|
|
42
|
+
inFlight = null;
|
|
43
|
+
}
|
|
44
|
+
if (stopped)
|
|
45
|
+
return;
|
|
46
|
+
deps.onBeat?.(beat);
|
|
47
|
+
scheduleAfter(beat.delayMs);
|
|
48
|
+
};
|
|
49
|
+
if (deps.runFirstImmediately !== false) {
|
|
50
|
+
void run();
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const beat = planNext();
|
|
54
|
+
deps.onBeat?.(beat);
|
|
55
|
+
scheduleAfter(beat.delayMs);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
stop: async () => {
|
|
59
|
+
stopped = true;
|
|
60
|
+
if (timer) {
|
|
61
|
+
clearTimer(timer);
|
|
62
|
+
timer = null;
|
|
63
|
+
}
|
|
64
|
+
if (inFlight) {
|
|
65
|
+
try {
|
|
66
|
+
await inFlight;
|
|
67
|
+
}
|
|
68
|
+
catch { /* ignore */ }
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type CommandHandler = (argv: string[]) => Promise<number>;
|
|
2
|
+
/** Verbs dispatched asynchronously ahead of the synchronous runCli core. cli.ts drives its dispatch from this. */
|
|
3
|
+
export declare const ASYNC_COMMANDS: Readonly<Record<string, CommandHandler>>;
|
|
4
|
+
/** Verbs handled by the synchronous runCli core (read-only views + local ops). Kept in sync with runCli's switch
|
|
5
|
+
* — the source of truth for these stays runCli; this list completes the surface for the registry contract. */
|
|
6
|
+
export declare const SYNC_COMMANDS: readonly ["status", "cycles", "trigger", "value", "narrative", "retention", "gene-value", "replay", "rebuild-views", "reset-local-secret"];
|
|
7
|
+
/** Every top-level verb `evolver` resolves to (async-dispatched ∪ runCli core). */
|
|
8
|
+
export declare const ALL_COMMANDS: ReadonlySet<string>;
|
|
9
|
+
/** Run a top-level argv against the registry: async handler if present, else the synchronous runCli core. */
|
|
10
|
+
export declare function dispatch(argv: readonly string[]): Promise<number> | number;
|