@tangle-network/agent-eval 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/index.d.ts +440 -1
- package/dist/index.js +627 -4
- package/dist/index.js.map +1 -1
- package/package.json +10 -11
package/dist/index.js
CHANGED
|
@@ -9538,6 +9538,49 @@ function extractErrorCount(text, opts = {}) {
|
|
|
9538
9538
|
// src/reference-replay.ts
|
|
9539
9539
|
import { appendFileSync as appendFileSync2, existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync4 } from "fs";
|
|
9540
9540
|
import { dirname as dirname2 } from "path";
|
|
9541
|
+
|
|
9542
|
+
// src/concurrency.ts
|
|
9543
|
+
var Mutex = class {
|
|
9544
|
+
locked = false;
|
|
9545
|
+
waiters = [];
|
|
9546
|
+
async acquire() {
|
|
9547
|
+
if (!this.locked) {
|
|
9548
|
+
this.locked = true;
|
|
9549
|
+
return () => this.release();
|
|
9550
|
+
}
|
|
9551
|
+
return new Promise((resolve) => {
|
|
9552
|
+
this.waiters.push(() => {
|
|
9553
|
+
resolve(() => this.release());
|
|
9554
|
+
});
|
|
9555
|
+
});
|
|
9556
|
+
}
|
|
9557
|
+
release() {
|
|
9558
|
+
const next = this.waiters.shift();
|
|
9559
|
+
if (next) {
|
|
9560
|
+
next();
|
|
9561
|
+
} else {
|
|
9562
|
+
this.locked = false;
|
|
9563
|
+
}
|
|
9564
|
+
}
|
|
9565
|
+
async runExclusive(fn) {
|
|
9566
|
+
const release = await this.acquire();
|
|
9567
|
+
try {
|
|
9568
|
+
return await fn();
|
|
9569
|
+
} finally {
|
|
9570
|
+
release();
|
|
9571
|
+
}
|
|
9572
|
+
}
|
|
9573
|
+
/** True iff someone holds the lock right now. Diagnostics only. */
|
|
9574
|
+
get isLocked() {
|
|
9575
|
+
return this.locked;
|
|
9576
|
+
}
|
|
9577
|
+
/** Pending waiter count. Diagnostics only. */
|
|
9578
|
+
get pending() {
|
|
9579
|
+
return this.waiters.length;
|
|
9580
|
+
}
|
|
9581
|
+
};
|
|
9582
|
+
|
|
9583
|
+
// src/reference-replay.ts
|
|
9541
9584
|
var DEFAULT_MATCH_THRESHOLD = 0.55;
|
|
9542
9585
|
var ALL_SPLITS = ["train", "dev", "test", "holdout"];
|
|
9543
9586
|
async function runReferenceReplay(cases, options) {
|
|
@@ -9638,15 +9681,29 @@ function inMemoryReferenceReplayStore(initial = []) {
|
|
|
9638
9681
|
}
|
|
9639
9682
|
};
|
|
9640
9683
|
}
|
|
9684
|
+
var jsonlStoreLocks = /* @__PURE__ */ new Map();
|
|
9685
|
+
function getJsonlStoreLock(path) {
|
|
9686
|
+
let m = jsonlStoreLocks.get(path);
|
|
9687
|
+
if (!m) {
|
|
9688
|
+
m = new Mutex();
|
|
9689
|
+
jsonlStoreLocks.set(path, m);
|
|
9690
|
+
}
|
|
9691
|
+
return m;
|
|
9692
|
+
}
|
|
9641
9693
|
function jsonlReferenceReplayStore(path) {
|
|
9694
|
+
const lock = getJsonlStoreLock(path);
|
|
9642
9695
|
return {
|
|
9643
9696
|
async save(run) {
|
|
9644
|
-
|
|
9645
|
-
|
|
9697
|
+
await lock.runExclusive(() => {
|
|
9698
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
9699
|
+
appendFileSync2(path, JSON.stringify(run) + "\n");
|
|
9700
|
+
});
|
|
9646
9701
|
},
|
|
9647
9702
|
async list() {
|
|
9648
|
-
|
|
9649
|
-
|
|
9703
|
+
return lock.runExclusive(() => {
|
|
9704
|
+
if (!existsSync4(path)) return [];
|
|
9705
|
+
return readJsonl(path);
|
|
9706
|
+
});
|
|
9650
9707
|
}
|
|
9651
9708
|
};
|
|
9652
9709
|
}
|
|
@@ -10272,6 +10329,561 @@ function samePopulation(a, b) {
|
|
|
10272
10329
|
return b.every((id) => setA.has(id));
|
|
10273
10330
|
}
|
|
10274
10331
|
|
|
10332
|
+
// src/jsonl-trial-cache.ts
|
|
10333
|
+
import { appendFileSync as appendFileSync4, existsSync as existsSync6, mkdirSync as mkdirSync4, readFileSync as readFileSync5 } from "fs";
|
|
10334
|
+
import { dirname as dirname4 } from "path";
|
|
10335
|
+
|
|
10336
|
+
// src/locked-jsonl-appender.ts
|
|
10337
|
+
import { appendFileSync as appendFileSync3, existsSync as existsSync5, mkdirSync as mkdirSync3 } from "fs";
|
|
10338
|
+
import { dirname as dirname3 } from "path";
|
|
10339
|
+
var mutexes = /* @__PURE__ */ new Map();
|
|
10340
|
+
function getMutex(path) {
|
|
10341
|
+
let m = mutexes.get(path);
|
|
10342
|
+
if (!m) {
|
|
10343
|
+
m = new Mutex();
|
|
10344
|
+
mutexes.set(path, m);
|
|
10345
|
+
}
|
|
10346
|
+
return m;
|
|
10347
|
+
}
|
|
10348
|
+
var LockedJsonlAppender = class {
|
|
10349
|
+
constructor(path) {
|
|
10350
|
+
this.path = path;
|
|
10351
|
+
this.mutex = getMutex(path);
|
|
10352
|
+
if (!existsSync5(dirname3(path))) {
|
|
10353
|
+
mkdirSync3(dirname3(path), { recursive: true });
|
|
10354
|
+
}
|
|
10355
|
+
}
|
|
10356
|
+
path;
|
|
10357
|
+
mutex;
|
|
10358
|
+
async append(entry) {
|
|
10359
|
+
const line = `${JSON.stringify(entry)}
|
|
10360
|
+
`;
|
|
10361
|
+
await this.mutex.runExclusive(() => {
|
|
10362
|
+
appendFileSync3(this.path, line);
|
|
10363
|
+
});
|
|
10364
|
+
}
|
|
10365
|
+
};
|
|
10366
|
+
function resetLockedAppendersForTesting() {
|
|
10367
|
+
mutexes.clear();
|
|
10368
|
+
}
|
|
10369
|
+
|
|
10370
|
+
// src/jsonl-trial-cache.ts
|
|
10371
|
+
var JsonlTrialCache = class {
|
|
10372
|
+
map = /* @__PURE__ */ new Map();
|
|
10373
|
+
path;
|
|
10374
|
+
appender;
|
|
10375
|
+
constructor(path) {
|
|
10376
|
+
this.path = path;
|
|
10377
|
+
if (existsSync6(path)) {
|
|
10378
|
+
for (const line of readFileSync5(path, "utf-8").split("\n")) {
|
|
10379
|
+
if (!line.trim()) continue;
|
|
10380
|
+
try {
|
|
10381
|
+
const entry = JSON.parse(line);
|
|
10382
|
+
this.map.set(entry.key, entry.result);
|
|
10383
|
+
} catch {
|
|
10384
|
+
}
|
|
10385
|
+
}
|
|
10386
|
+
} else {
|
|
10387
|
+
mkdirSync4(dirname4(path), { recursive: true });
|
|
10388
|
+
}
|
|
10389
|
+
this.appender = new LockedJsonlAppender(path);
|
|
10390
|
+
}
|
|
10391
|
+
get(key) {
|
|
10392
|
+
return this.map.get(key);
|
|
10393
|
+
}
|
|
10394
|
+
set(key, value) {
|
|
10395
|
+
this.map.set(key, value);
|
|
10396
|
+
const line = { key, result: value, writtenAt: Date.now() };
|
|
10397
|
+
void this.appender.append(line);
|
|
10398
|
+
}
|
|
10399
|
+
size() {
|
|
10400
|
+
return this.map.size;
|
|
10401
|
+
}
|
|
10402
|
+
/**
|
|
10403
|
+
* Synchronous fallback path for tests / CLI tools that want to be sure
|
|
10404
|
+
* the line is on disk before returning. Bypasses the mutex (single-
|
|
10405
|
+
* threaded callers only).
|
|
10406
|
+
*/
|
|
10407
|
+
setSync(key, value) {
|
|
10408
|
+
this.map.set(key, value);
|
|
10409
|
+
const line = { key, result: value, writtenAt: Date.now() };
|
|
10410
|
+
appendFileSync4(this.path, `${JSON.stringify(line)}
|
|
10411
|
+
`);
|
|
10412
|
+
}
|
|
10413
|
+
};
|
|
10414
|
+
|
|
10415
|
+
// src/evolution-telemetry.ts
|
|
10416
|
+
import { appendFileSync as appendFileSync5, existsSync as existsSync7, mkdirSync as mkdirSync5, readFileSync as readFileSync6, writeFileSync } from "fs";
|
|
10417
|
+
import { dirname as dirname5 } from "path";
|
|
10418
|
+
var MutationTelemetry = class {
|
|
10419
|
+
appender;
|
|
10420
|
+
constructor(path) {
|
|
10421
|
+
this.appender = new LockedJsonlAppender(path);
|
|
10422
|
+
}
|
|
10423
|
+
async record(attempt) {
|
|
10424
|
+
await this.appender.append(attempt);
|
|
10425
|
+
}
|
|
10426
|
+
};
|
|
10427
|
+
var TrialTelemetry = class {
|
|
10428
|
+
appender;
|
|
10429
|
+
constructor(path) {
|
|
10430
|
+
this.appender = new LockedJsonlAppender(path);
|
|
10431
|
+
}
|
|
10432
|
+
async record(attempt) {
|
|
10433
|
+
await this.appender.append(attempt);
|
|
10434
|
+
}
|
|
10435
|
+
};
|
|
10436
|
+
var LineageRecorder = class {
|
|
10437
|
+
path;
|
|
10438
|
+
snapshotPath;
|
|
10439
|
+
mutex = new Mutex();
|
|
10440
|
+
nodes = /* @__PURE__ */ new Map();
|
|
10441
|
+
kindOf;
|
|
10442
|
+
constructor(path, kindOf) {
|
|
10443
|
+
this.path = path;
|
|
10444
|
+
this.snapshotPath = `${path}.snapshot`;
|
|
10445
|
+
this.kindOf = kindOf ?? defaultKindOf;
|
|
10446
|
+
mkdirSync5(dirname5(path), { recursive: true });
|
|
10447
|
+
if (existsSync7(this.snapshotPath)) {
|
|
10448
|
+
try {
|
|
10449
|
+
const parsed = JSON.parse(readFileSync6(this.snapshotPath, "utf-8"));
|
|
10450
|
+
for (const n of parsed) this.nodes.set(n.id, n);
|
|
10451
|
+
} catch {
|
|
10452
|
+
}
|
|
10453
|
+
}
|
|
10454
|
+
if (existsSync7(path)) {
|
|
10455
|
+
try {
|
|
10456
|
+
for (const line of readFileSync6(path, "utf-8").split("\n")) {
|
|
10457
|
+
if (!line.trim()) continue;
|
|
10458
|
+
try {
|
|
10459
|
+
const entry = JSON.parse(line);
|
|
10460
|
+
const prev = this.nodes.get(entry.id);
|
|
10461
|
+
this.nodes.set(entry.id, { ...prev, ...entry });
|
|
10462
|
+
} catch {
|
|
10463
|
+
}
|
|
10464
|
+
}
|
|
10465
|
+
} catch {
|
|
10466
|
+
}
|
|
10467
|
+
}
|
|
10468
|
+
if (existsSync7(path) && this.nodes.size === 0) {
|
|
10469
|
+
try {
|
|
10470
|
+
const raw = readFileSync6(path, "utf-8").trim();
|
|
10471
|
+
if (raw.startsWith("[")) {
|
|
10472
|
+
const parsed = JSON.parse(raw);
|
|
10473
|
+
for (const n of parsed) this.nodes.set(n.id, n);
|
|
10474
|
+
}
|
|
10475
|
+
} catch {
|
|
10476
|
+
}
|
|
10477
|
+
}
|
|
10478
|
+
}
|
|
10479
|
+
async upsert(node) {
|
|
10480
|
+
await this.mutex.runExclusive(() => {
|
|
10481
|
+
const prev = this.nodes.get(node.id);
|
|
10482
|
+
this.nodes.set(node.id, { ...prev, ...node });
|
|
10483
|
+
try {
|
|
10484
|
+
if (existsSync7(this.path)) {
|
|
10485
|
+
const head = readFileSync6(this.path, { encoding: "utf-8", flag: "r" }).slice(0, 1);
|
|
10486
|
+
if (head === "[") {
|
|
10487
|
+
writeFileSync(this.path, "");
|
|
10488
|
+
}
|
|
10489
|
+
}
|
|
10490
|
+
} catch {
|
|
10491
|
+
}
|
|
10492
|
+
appendFileSync5(this.path, `${JSON.stringify(this.nodes.get(node.id))}
|
|
10493
|
+
`);
|
|
10494
|
+
});
|
|
10495
|
+
}
|
|
10496
|
+
async upsertVariant(variant) {
|
|
10497
|
+
await this.upsert({
|
|
10498
|
+
id: variant.id,
|
|
10499
|
+
parentId: variant.parentId ?? null,
|
|
10500
|
+
generation: variant.generation,
|
|
10501
|
+
kind: this.kindOf(variant),
|
|
10502
|
+
...variant.rationale ? { rationale: variant.rationale } : {}
|
|
10503
|
+
});
|
|
10504
|
+
}
|
|
10505
|
+
snapshot() {
|
|
10506
|
+
return [...this.nodes.values()];
|
|
10507
|
+
}
|
|
10508
|
+
/**
|
|
10509
|
+
* Write the current consolidated state to `<path>.snapshot` so external
|
|
10510
|
+
* tools can read it without replaying the event log. Idempotent.
|
|
10511
|
+
*/
|
|
10512
|
+
async compact() {
|
|
10513
|
+
await this.mutex.runExclusive(() => {
|
|
10514
|
+
writeFileSync(this.snapshotPath, JSON.stringify([...this.nodes.values()], null, 2));
|
|
10515
|
+
});
|
|
10516
|
+
}
|
|
10517
|
+
};
|
|
10518
|
+
function defaultKindOf(variant) {
|
|
10519
|
+
if (variant.parentId === void 0) return "seed";
|
|
10520
|
+
const payload = variant.payload;
|
|
10521
|
+
if (payload && typeof payload === "object" && payload.codeMutation) return "code";
|
|
10522
|
+
return "prompt";
|
|
10523
|
+
}
|
|
10524
|
+
function emptyGenBucket() {
|
|
10525
|
+
return {
|
|
10526
|
+
mutatorPromptUsd: 0,
|
|
10527
|
+
mutatorCodeUsd: 0,
|
|
10528
|
+
scorerPromptUsd: 0,
|
|
10529
|
+
scorerCodeUsd: 0,
|
|
10530
|
+
trialsCounted: 0,
|
|
10531
|
+
cachedTrials: 0
|
|
10532
|
+
};
|
|
10533
|
+
}
|
|
10534
|
+
var CostLedger = class {
|
|
10535
|
+
totals = {
|
|
10536
|
+
mutatorPromptUsd: 0,
|
|
10537
|
+
mutatorCodeUsd: 0,
|
|
10538
|
+
scorerPromptUsd: 0,
|
|
10539
|
+
scorerCodeUsd: 0,
|
|
10540
|
+
trialsCounted: 0,
|
|
10541
|
+
cachedTrials: 0,
|
|
10542
|
+
poolBusyMs: 0,
|
|
10543
|
+
poolUtilizationPct: 0,
|
|
10544
|
+
byGeneration: {}
|
|
10545
|
+
};
|
|
10546
|
+
path;
|
|
10547
|
+
mutex = new Mutex();
|
|
10548
|
+
constructor(path) {
|
|
10549
|
+
this.path = path;
|
|
10550
|
+
if (existsSync7(path)) {
|
|
10551
|
+
try {
|
|
10552
|
+
const loaded = JSON.parse(readFileSync6(path, "utf-8"));
|
|
10553
|
+
for (const k of Object.keys(this.totals)) {
|
|
10554
|
+
if (k === "byGeneration") {
|
|
10555
|
+
if (loaded.byGeneration && typeof loaded.byGeneration === "object") {
|
|
10556
|
+
this.totals.byGeneration = loaded.byGeneration;
|
|
10557
|
+
}
|
|
10558
|
+
continue;
|
|
10559
|
+
}
|
|
10560
|
+
const v = loaded[k];
|
|
10561
|
+
if (typeof v === "number" && Number.isFinite(v)) {
|
|
10562
|
+
this.totals[k] = v;
|
|
10563
|
+
}
|
|
10564
|
+
}
|
|
10565
|
+
} catch {
|
|
10566
|
+
}
|
|
10567
|
+
} else {
|
|
10568
|
+
mkdirSync5(dirname5(path), { recursive: true });
|
|
10569
|
+
}
|
|
10570
|
+
}
|
|
10571
|
+
genBucket(generation) {
|
|
10572
|
+
if (generation === void 0) return null;
|
|
10573
|
+
const key = String(generation);
|
|
10574
|
+
if (!this.totals.byGeneration[key]) {
|
|
10575
|
+
this.totals.byGeneration[key] = emptyGenBucket();
|
|
10576
|
+
}
|
|
10577
|
+
return this.totals.byGeneration[key];
|
|
10578
|
+
}
|
|
10579
|
+
async addMutation(channel, usd, opts = {}) {
|
|
10580
|
+
await this.mutex.runExclusive(() => {
|
|
10581
|
+
const bucket = this.genBucket(opts.generation);
|
|
10582
|
+
if (channel === "prompt") {
|
|
10583
|
+
this.totals.mutatorPromptUsd += usd;
|
|
10584
|
+
if (bucket) bucket.mutatorPromptUsd += usd;
|
|
10585
|
+
} else {
|
|
10586
|
+
this.totals.mutatorCodeUsd += usd;
|
|
10587
|
+
if (bucket) bucket.mutatorCodeUsd += usd;
|
|
10588
|
+
}
|
|
10589
|
+
this.persist();
|
|
10590
|
+
});
|
|
10591
|
+
}
|
|
10592
|
+
async addTrial(channel, usd, cached, opts = {}) {
|
|
10593
|
+
await this.mutex.runExclusive(() => {
|
|
10594
|
+
const bucket = this.genBucket(opts.generation);
|
|
10595
|
+
if (cached) {
|
|
10596
|
+
this.totals.cachedTrials++;
|
|
10597
|
+
this.totals.trialsCounted++;
|
|
10598
|
+
if (bucket) {
|
|
10599
|
+
bucket.cachedTrials++;
|
|
10600
|
+
bucket.trialsCounted++;
|
|
10601
|
+
}
|
|
10602
|
+
this.persist();
|
|
10603
|
+
return;
|
|
10604
|
+
}
|
|
10605
|
+
if (channel === "prompt") {
|
|
10606
|
+
this.totals.scorerPromptUsd += usd;
|
|
10607
|
+
if (bucket) bucket.scorerPromptUsd += usd;
|
|
10608
|
+
} else {
|
|
10609
|
+
this.totals.scorerCodeUsd += usd;
|
|
10610
|
+
if (bucket) bucket.scorerCodeUsd += usd;
|
|
10611
|
+
}
|
|
10612
|
+
this.totals.trialsCounted++;
|
|
10613
|
+
if (bucket) bucket.trialsCounted++;
|
|
10614
|
+
this.persist();
|
|
10615
|
+
});
|
|
10616
|
+
}
|
|
10617
|
+
async setPoolUtilization(busyMs, totalMs) {
|
|
10618
|
+
await this.mutex.runExclusive(() => {
|
|
10619
|
+
this.totals.poolBusyMs = busyMs;
|
|
10620
|
+
this.totals.poolUtilizationPct = totalMs > 0 ? 100 * busyMs / totalMs : 0;
|
|
10621
|
+
this.persist();
|
|
10622
|
+
});
|
|
10623
|
+
}
|
|
10624
|
+
snapshot() {
|
|
10625
|
+
const totalUsd = this.totals.mutatorPromptUsd + this.totals.mutatorCodeUsd + this.totals.scorerPromptUsd + this.totals.scorerCodeUsd;
|
|
10626
|
+
const byGeneration = Object.entries(this.totals.byGeneration).map(([g, b]) => ({ generation: Number(g), ...b })).sort((a, b) => a.generation - b.generation);
|
|
10627
|
+
return {
|
|
10628
|
+
totalUsd,
|
|
10629
|
+
mutatorPromptUsd: this.totals.mutatorPromptUsd,
|
|
10630
|
+
mutatorCodeUsd: this.totals.mutatorCodeUsd,
|
|
10631
|
+
scorerPromptUsd: this.totals.scorerPromptUsd,
|
|
10632
|
+
scorerCodeUsd: this.totals.scorerCodeUsd,
|
|
10633
|
+
trialsCounted: this.totals.trialsCounted,
|
|
10634
|
+
cachedTrials: this.totals.cachedTrials,
|
|
10635
|
+
poolBusyMs: this.totals.poolBusyMs,
|
|
10636
|
+
poolUtilizationPct: this.totals.poolUtilizationPct,
|
|
10637
|
+
byGeneration
|
|
10638
|
+
};
|
|
10639
|
+
}
|
|
10640
|
+
persist() {
|
|
10641
|
+
writeFileSync(this.path, JSON.stringify(this.totals, null, 2));
|
|
10642
|
+
}
|
|
10643
|
+
};
|
|
10644
|
+
|
|
10645
|
+
// src/composite-mutator.ts
|
|
10646
|
+
function createCompositeMutator(opts) {
|
|
10647
|
+
const recentScores = [];
|
|
10648
|
+
const plateauThreshold = opts.plateauThreshold ?? 0.02;
|
|
10649
|
+
const plateauPatience = opts.plateauPatience ?? 2;
|
|
10650
|
+
function pickMode(args) {
|
|
10651
|
+
recentScores.push(args.parentAggregate.meanScore);
|
|
10652
|
+
switch (opts.policy) {
|
|
10653
|
+
case "primary-only":
|
|
10654
|
+
return { mode: "primary", reason: "policy=primary-only" };
|
|
10655
|
+
case "secondary-only":
|
|
10656
|
+
if (!opts.secondary) return { mode: "primary", reason: "secondary-only requested but no secondary mutator wired" };
|
|
10657
|
+
return { mode: "secondary", reason: "policy=secondary-only" };
|
|
10658
|
+
case "alternate":
|
|
10659
|
+
if (!opts.secondary) return { mode: "primary", reason: "alternate requested but no secondary mutator wired" };
|
|
10660
|
+
return args.generation % 2 === 1 ? { mode: "secondary", reason: `alternate: gen${args.generation} odd \u2192 secondary` } : { mode: "primary", reason: `alternate: gen${args.generation} even \u2192 primary` };
|
|
10661
|
+
case "plateau": {
|
|
10662
|
+
if (!opts.secondary) return { mode: "primary", reason: "plateau requested but no secondary mutator wired" };
|
|
10663
|
+
if (recentScores.length <= plateauPatience) {
|
|
10664
|
+
return { mode: "primary", reason: "plateau: warming up with primary mutations" };
|
|
10665
|
+
}
|
|
10666
|
+
const window = recentScores.slice(-plateauPatience - 1);
|
|
10667
|
+
const deltas = window.slice(1).map((v, i) => v - window[i]);
|
|
10668
|
+
const stagnant = deltas.every((d) => d < plateauThreshold);
|
|
10669
|
+
if (stagnant) {
|
|
10670
|
+
return {
|
|
10671
|
+
mode: "split",
|
|
10672
|
+
reason: `plateau detected (${deltas.map((d) => d.toFixed(3)).join(", ")}) \u2192 split`
|
|
10673
|
+
};
|
|
10674
|
+
}
|
|
10675
|
+
return {
|
|
10676
|
+
mode: "primary",
|
|
10677
|
+
reason: `plateau: still improving (${deltas[deltas.length - 1].toFixed(3)})`
|
|
10678
|
+
};
|
|
10679
|
+
}
|
|
10680
|
+
}
|
|
10681
|
+
}
|
|
10682
|
+
return {
|
|
10683
|
+
async mutate(args) {
|
|
10684
|
+
const { mode, reason } = pickMode(args);
|
|
10685
|
+
opts.onPolicyDecision?.({ generation: args.generation, chose: mode, reason });
|
|
10686
|
+
if (mode === "primary") return opts.primary.mutate(args);
|
|
10687
|
+
if (mode === "secondary" && opts.secondary) return opts.secondary.mutate(args);
|
|
10688
|
+
if (mode === "split" && opts.secondary) {
|
|
10689
|
+
const secondaryShare = Math.ceil(args.childCount / 2);
|
|
10690
|
+
const primaryShare = args.childCount - secondaryShare;
|
|
10691
|
+
const [primaryChildren, secondaryChildren] = await Promise.all([
|
|
10692
|
+
opts.primary.mutate({ ...args, childCount: primaryShare }),
|
|
10693
|
+
opts.secondary.mutate({ ...args, childCount: secondaryShare })
|
|
10694
|
+
]);
|
|
10695
|
+
return [...primaryChildren, ...secondaryChildren];
|
|
10696
|
+
}
|
|
10697
|
+
return opts.primary.mutate(args);
|
|
10698
|
+
}
|
|
10699
|
+
};
|
|
10700
|
+
}
|
|
10701
|
+
|
|
10702
|
+
// src/sandbox-pool.ts
|
|
10703
|
+
function createSandboxPool(opts) {
|
|
10704
|
+
if (opts.size < 1) throw new Error(`sandbox pool size must be >= 1 (got ${opts.size})`);
|
|
10705
|
+
const slots = [];
|
|
10706
|
+
const waiters = [];
|
|
10707
|
+
const mutex = new Mutex();
|
|
10708
|
+
let nextSlotId = 0;
|
|
10709
|
+
let totalCheckouts = 0;
|
|
10710
|
+
let busyMs = 0;
|
|
10711
|
+
const startedAt = Date.now();
|
|
10712
|
+
async function acquireSlot() {
|
|
10713
|
+
let mintId;
|
|
10714
|
+
const ready = await mutex.runExclusive(async () => {
|
|
10715
|
+
const idle = slots.find((s) => !s.busy);
|
|
10716
|
+
if (idle) {
|
|
10717
|
+
idle.busy = true;
|
|
10718
|
+
return idle;
|
|
10719
|
+
}
|
|
10720
|
+
if (slots.length < opts.size) {
|
|
10721
|
+
mintId = `slot_${nextSlotId++}`;
|
|
10722
|
+
return null;
|
|
10723
|
+
}
|
|
10724
|
+
return null;
|
|
10725
|
+
});
|
|
10726
|
+
if (ready) return ready;
|
|
10727
|
+
if (mintId !== void 0) {
|
|
10728
|
+
const resource = await opts.factory.create(mintId);
|
|
10729
|
+
const state = {
|
|
10730
|
+
slot: { id: mintId, resource },
|
|
10731
|
+
busy: true
|
|
10732
|
+
};
|
|
10733
|
+
await mutex.runExclusive(() => {
|
|
10734
|
+
slots.push(state);
|
|
10735
|
+
});
|
|
10736
|
+
return state;
|
|
10737
|
+
}
|
|
10738
|
+
return new Promise((resolve) => {
|
|
10739
|
+
waiters.push((s) => {
|
|
10740
|
+
s.busy = true;
|
|
10741
|
+
resolve(s);
|
|
10742
|
+
});
|
|
10743
|
+
});
|
|
10744
|
+
}
|
|
10745
|
+
function releaseSlot(state) {
|
|
10746
|
+
void (async () => {
|
|
10747
|
+
try {
|
|
10748
|
+
if (opts.factory.reset) await opts.factory.reset(state.slot);
|
|
10749
|
+
} catch (err) {
|
|
10750
|
+
console.warn(`[sandbox-pool] reset failed for slot ${state.slot.id}:`, err);
|
|
10751
|
+
}
|
|
10752
|
+
state.busy = false;
|
|
10753
|
+
const next = waiters.shift();
|
|
10754
|
+
if (next) next(state);
|
|
10755
|
+
})();
|
|
10756
|
+
}
|
|
10757
|
+
async function checkout() {
|
|
10758
|
+
const state = await acquireSlot();
|
|
10759
|
+
const checkoutStart = Date.now();
|
|
10760
|
+
totalCheckouts++;
|
|
10761
|
+
return {
|
|
10762
|
+
slot: state.slot,
|
|
10763
|
+
release: () => {
|
|
10764
|
+
busyMs += Date.now() - checkoutStart;
|
|
10765
|
+
releaseSlot(state);
|
|
10766
|
+
}
|
|
10767
|
+
};
|
|
10768
|
+
}
|
|
10769
|
+
async function withSlot(fn) {
|
|
10770
|
+
const { slot, release } = await checkout();
|
|
10771
|
+
try {
|
|
10772
|
+
return await fn(slot);
|
|
10773
|
+
} finally {
|
|
10774
|
+
release();
|
|
10775
|
+
}
|
|
10776
|
+
}
|
|
10777
|
+
async function drain() {
|
|
10778
|
+
const snapshot = await mutex.runExclusive(() => {
|
|
10779
|
+
const taken = slots.splice(0, slots.length);
|
|
10780
|
+
for (const w of waiters.splice(0, waiters.length)) {
|
|
10781
|
+
void w;
|
|
10782
|
+
}
|
|
10783
|
+
return taken;
|
|
10784
|
+
});
|
|
10785
|
+
await Promise.allSettled(snapshot.map((s) => opts.factory.destroy(s.slot)));
|
|
10786
|
+
}
|
|
10787
|
+
function utilization() {
|
|
10788
|
+
return {
|
|
10789
|
+
busyMs,
|
|
10790
|
+
totalMs: Date.now() - startedAt,
|
|
10791
|
+
checkouts: totalCheckouts
|
|
10792
|
+
};
|
|
10793
|
+
}
|
|
10794
|
+
return {
|
|
10795
|
+
checkout,
|
|
10796
|
+
withSlot,
|
|
10797
|
+
drain,
|
|
10798
|
+
poolSize: () => slots.length,
|
|
10799
|
+
activeCheckouts: () => slots.filter((s) => s.busy).length,
|
|
10800
|
+
utilization
|
|
10801
|
+
};
|
|
10802
|
+
}
|
|
10803
|
+
|
|
10804
|
+
// src/code-mutator.ts
|
|
10805
|
+
function createSandboxCodeMutator(opts) {
|
|
10806
|
+
const childIdFor = opts.childIdFor ?? ((parent, generation, index) => `${parent.id}.g${generation}.code.${index}`);
|
|
10807
|
+
const labelFor = opts.labelFor ?? ((outcome, parent, _generation, index) => outcome.description?.slice(0, 80) ?? `${parent.label} \u2192 code.${index}`);
|
|
10808
|
+
return {
|
|
10809
|
+
async mutate(args) {
|
|
10810
|
+
const { parent, parentAggregate, topTrials, bottomTrials, childCount, generation } = args;
|
|
10811
|
+
const startedAt = Date.now();
|
|
10812
|
+
const outcomes = await opts.pool.withSlot(async (slot) => {
|
|
10813
|
+
try {
|
|
10814
|
+
return await opts.runner({
|
|
10815
|
+
slot,
|
|
10816
|
+
parent,
|
|
10817
|
+
parentAggregate,
|
|
10818
|
+
topTrials,
|
|
10819
|
+
bottomTrials,
|
|
10820
|
+
childCount,
|
|
10821
|
+
generation
|
|
10822
|
+
});
|
|
10823
|
+
} catch (err) {
|
|
10824
|
+
return [{
|
|
10825
|
+
ok: false,
|
|
10826
|
+
failureReason: "runner_error",
|
|
10827
|
+
description: err instanceof Error ? err.message : String(err),
|
|
10828
|
+
latencyMs: Date.now() - startedAt
|
|
10829
|
+
}];
|
|
10830
|
+
}
|
|
10831
|
+
});
|
|
10832
|
+
const variants = [];
|
|
10833
|
+
let index = 0;
|
|
10834
|
+
for (const outcome of outcomes) {
|
|
10835
|
+
const childId = outcome.childId ?? childIdFor(parent, generation, index);
|
|
10836
|
+
if (opts.mutationTelemetry) {
|
|
10837
|
+
await opts.mutationTelemetry.record({
|
|
10838
|
+
ts: Date.now(),
|
|
10839
|
+
channel: "code",
|
|
10840
|
+
generation,
|
|
10841
|
+
parentId: parent.id,
|
|
10842
|
+
childId: outcome.ok ? childId : null,
|
|
10843
|
+
ok: outcome.ok,
|
|
10844
|
+
failureReason: outcome.failureReason,
|
|
10845
|
+
description: outcome.description,
|
|
10846
|
+
latencyMs: outcome.latencyMs,
|
|
10847
|
+
diffBytes: outcome.diffBytes,
|
|
10848
|
+
filesTouched: outcome.filesTouched,
|
|
10849
|
+
agentSteps: outcome.agentSteps,
|
|
10850
|
+
costUsd: outcome.costUsd
|
|
10851
|
+
});
|
|
10852
|
+
}
|
|
10853
|
+
if (opts.costLedger && outcome.costUsd !== void 0) {
|
|
10854
|
+
await opts.costLedger.addMutation("code", outcome.costUsd, { generation });
|
|
10855
|
+
}
|
|
10856
|
+
if (outcome.ok) {
|
|
10857
|
+
const variant = {
|
|
10858
|
+
id: childId,
|
|
10859
|
+
payload: opts.toVariantPayload(outcome, parent),
|
|
10860
|
+
generation,
|
|
10861
|
+
parentId: parent.id,
|
|
10862
|
+
label: labelFor(outcome, parent, generation, index),
|
|
10863
|
+
...outcome.rationale ? { rationale: outcome.rationale } : {}
|
|
10864
|
+
};
|
|
10865
|
+
variants.push(variant);
|
|
10866
|
+
if (opts.lineage) {
|
|
10867
|
+
await opts.lineage.upsert({
|
|
10868
|
+
id: variant.id,
|
|
10869
|
+
parentId: variant.parentId ?? null,
|
|
10870
|
+
generation: variant.generation,
|
|
10871
|
+
kind: "code",
|
|
10872
|
+
...variant.rationale ? { rationale: variant.rationale } : {}
|
|
10873
|
+
});
|
|
10874
|
+
}
|
|
10875
|
+
}
|
|
10876
|
+
index++;
|
|
10877
|
+
}
|
|
10878
|
+
if (opts.costLedger) {
|
|
10879
|
+
const u = opts.pool.utilization();
|
|
10880
|
+
await opts.costLedger.setPoolUtilization(u.busyMs, u.totalMs);
|
|
10881
|
+
}
|
|
10882
|
+
return variants;
|
|
10883
|
+
}
|
|
10884
|
+
};
|
|
10885
|
+
}
|
|
10886
|
+
|
|
10275
10887
|
// src/golden-matcher.ts
|
|
10276
10888
|
function matchGoldens(goldens, candidates, options = {}) {
|
|
10277
10889
|
const extract = options.text ?? defaultExtract5;
|
|
@@ -10634,6 +11246,7 @@ export {
|
|
|
10634
11246
|
BudgetGuard,
|
|
10635
11247
|
BuilderSession,
|
|
10636
11248
|
ConvergenceTracker,
|
|
11249
|
+
CostLedger,
|
|
10637
11250
|
CostTracker,
|
|
10638
11251
|
D1ExperimentStore,
|
|
10639
11252
|
DEFAULT_AGENT_SLOS,
|
|
@@ -10664,12 +11277,17 @@ export {
|
|
|
10664
11277
|
InMemoryTraceStore,
|
|
10665
11278
|
InMemoryTrialCache,
|
|
10666
11279
|
InMemoryWorkspaceInspector,
|
|
11280
|
+
JsonlTrialCache,
|
|
10667
11281
|
JudgeRunner,
|
|
11282
|
+
LineageRecorder,
|
|
10668
11283
|
LlmCallError,
|
|
10669
11284
|
LlmClient,
|
|
11285
|
+
LockedJsonlAppender,
|
|
10670
11286
|
MODEL_PRICING,
|
|
10671
11287
|
MetricsCollector,
|
|
10672
11288
|
MultiLayerVerifier,
|
|
11289
|
+
MutationTelemetry,
|
|
11290
|
+
Mutex,
|
|
10673
11291
|
OTEL_AGENT_EVAL_SCOPE,
|
|
10674
11292
|
OptimizationLoop,
|
|
10675
11293
|
PairwiseSteeringOptimizer,
|
|
@@ -10687,6 +11305,7 @@ export {
|
|
|
10687
11305
|
TRACE_SCHEMA_VERSION,
|
|
10688
11306
|
TokenCounter,
|
|
10689
11307
|
TraceEmitter,
|
|
11308
|
+
TrialTelemetry,
|
|
10690
11309
|
UNIVERSAL_FINDERS,
|
|
10691
11310
|
adversarialJudge,
|
|
10692
11311
|
aggregateLlm,
|
|
@@ -10731,11 +11350,14 @@ export {
|
|
|
10731
11350
|
correlateLayers,
|
|
10732
11351
|
correlationStudy,
|
|
10733
11352
|
createAntiSlopJudge,
|
|
11353
|
+
createCompositeMutator,
|
|
10734
11354
|
createCustomJudge,
|
|
10735
11355
|
createDefaultReviewer,
|
|
10736
11356
|
createDomainExpertJudge,
|
|
10737
11357
|
createIntentMatchJudge,
|
|
10738
11358
|
createLlmReviewer,
|
|
11359
|
+
createSandboxCodeMutator,
|
|
11360
|
+
createSandboxPool,
|
|
10739
11361
|
createSemanticConceptJudge,
|
|
10740
11362
|
crossTraceDiff,
|
|
10741
11363
|
crowdingDistance,
|
|
@@ -10846,6 +11468,7 @@ export {
|
|
|
10846
11468
|
replayScorerOverCorpus,
|
|
10847
11469
|
replayTraceThroughJudge,
|
|
10848
11470
|
requiredSampleSize,
|
|
11471
|
+
resetLockedAppendersForTesting,
|
|
10849
11472
|
resumeBuilderSession,
|
|
10850
11473
|
rowCount,
|
|
10851
11474
|
rowWhere,
|