@causyne/agent 1.20.0 → 1.20.7
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 +13 -9
- package/SECURITY.md +1 -4
- package/package.json +1 -1
- package/src/acp/client.js +176 -26
- package/src/acp/client.ts +167 -25
- package/src/acp/protocol.js +116 -0
- package/src/acp/protocol.ts +163 -0
- package/src/acp/server.js +5 -6
- package/src/acp/server.ts +4 -6
- package/src/agentLoop.d.ts +1 -0
- package/src/agentLoop.js +681 -245
- package/src/agentLoop.ts +708 -262
- package/src/agentPolicy.js +9 -0
- package/src/agentPolicy.ts +9 -0
- package/src/approvalPolicy.js +55 -0
- package/src/approvalPolicy.ts +66 -0
- package/src/browser/browserRuntime.js +217 -14
- package/src/browser/browserRuntime.ts +255 -14
- package/src/browser/causyneBrowser.js +1215 -68
- package/src/browser/causyneBrowser.ts +1340 -72
- package/src/cliMode.js +52 -29
- package/src/commands.js +141 -17
- package/src/config.js +22 -12
- package/src/continuum/ui/continuumPicker.js +18 -1
- package/src/continuum/ui/continuumPicker.ts +19 -1
- package/src/dynamicCatalogue.js +87 -20
- package/src/dynamicCatalogue.ts +85 -15
- package/src/launchIntent.js +40 -0
- package/src/launchIntent.ts +49 -0
- package/src/memoryManager.js +26 -2
- package/src/modelCatalogue.js +2 -0
- package/src/planCache.js +320 -11
- package/src/planCache.ts +402 -13
- package/src/providers/acp.js +173 -18
- package/src/providers/acp.ts +169 -19
- package/src/providers/antigravity.d.ts +5 -1
- package/src/providers/antigravity.js +674 -34
- package/src/providers/antigravity.ts +707 -35
- package/src/providers/base.d.ts +10 -0
- package/src/providers/factory.js +2 -0
- package/src/providers/pi.js +161 -0
- package/src/providers/pi.ts +148 -0
- package/src/provisioning/capabilityProvisioner.js +17 -0
- package/src/provisioning/capabilityTools.js +1 -1
- package/src/sandbox.js +30 -39
- package/src/sessionJournal.js +53 -10
- package/src/sessionJournal.ts +64 -10
- package/src/taskShape.js +9 -9
- package/src/taskShape.ts +9 -9
- package/src/timeMachine.js +124 -0
- package/src/timeMachine.ts +141 -0
- package/src/toolPacks.js +4 -10
- package/src/tools/archive.js +196 -59
- package/src/tools/archive.ts +220 -0
- package/src/tools/browserAutomator.js +78 -9
- package/src/tools/browserAutomator.ts +90 -9
- package/src/tools/desktopOpen.js +114 -31
- package/src/tools/desktopOpen.ts +144 -0
- package/src/tools/gitExec.js +5 -3
- package/src/tools/nativeEngine.js +4 -3
- package/src/tools/nativeEngine.ts +5 -3
- package/src/tools/openSourceToolProvisioner.js +5 -4
- package/src/tools/openSourceToolProvisioner.ts +5 -4
- package/src/tools/registry.js +3 -0
- package/src/tools/screenshot.js +55 -71
- package/src/tools/screenshot.ts +70 -0
- package/src/tools/sysRun.js +284 -117
- package/src/tools/sysRun.ts +347 -0
- package/src/tools/toolSelfRepair.js +109 -2
- package/src/tools/toolSelfRepair.ts +100 -2
- package/src/tools/updatePlan.js +49 -1
- package/src/tools/updatePlan.ts +43 -1
- package/src/tools/viewImage.js +93 -78
- package/src/tools/viewImage.ts +116 -0
- package/src/tools/webFetch.js +20 -6
- package/src/tools/webFetch.ts +26 -6
- package/src/tui/activityLayout.js +9 -0
- package/src/tui/activityLayout.ts +10 -0
- package/src/tui/block.d.ts +1 -0
- package/src/tui/chatRenderer.d.ts +23 -0
- package/src/tui/chatRenderer.js +49 -49
- package/src/tui/clipboardSync.js +28 -1
- package/src/tui/clipboardSync.ts +26 -1
- package/src/tui/codeProgress.js +299 -0
- package/src/tui/codeProgress.ts +304 -0
- package/src/tui/codeStreamRenderer.js +132 -0
- package/src/tui/codeStreamRenderer.ts +141 -0
- package/src/tui/commandPresentation.js +11 -0
- package/src/tui/commandPresentation.ts +18 -0
- package/src/tui/completion.js +45 -13
- package/src/tui/composerControls.js +131 -0
- package/src/tui/composerControls.ts +184 -0
- package/src/tui/diff.d.ts +2 -0
- package/src/tui/home.js +4 -2
- package/src/tui/homeTransition.js +21 -0
- package/src/tui/homeTransition.ts +27 -0
- package/src/tui/homeView.js +62 -0
- package/src/tui/homeView.ts +91 -0
- package/src/tui/layout.js +46 -0
- package/src/tui/layout.ts +52 -0
- package/src/tui/messageActionPlacement.js +16 -0
- package/src/tui/messageActionPlacement.ts +25 -0
- package/src/tui/messageInteraction.js +221 -0
- package/src/tui/messageInteraction.ts +275 -0
- package/src/tui/modelPicker.js +4 -24
- package/src/tui/modelPicker.ts +4 -20
- package/src/tui/mouseHover.js +6 -0
- package/src/tui/mouseHover.ts +5 -0
- package/src/tui/prompt.js +175 -101
- package/src/tui/resizeRecovery.js +15 -0
- package/src/tui/resizeRecovery.ts +27 -0
- package/src/tui/scrollInteraction.js +97 -0
- package/src/tui/scrollInteraction.ts +189 -0
- package/src/tui/sessionLayout.js +12 -0
- package/src/tui/sessionLayout.ts +12 -0
- package/src/tui/sessionPicker.js +111 -16
- package/src/tui/sessionPicker.ts +121 -16
- package/src/tui/sessionSidebar.js +78 -0
- package/src/tui/sessionSidebar.ts +95 -0
- package/src/tui/setup.js +1 -1
- package/src/tui/setup.ts +1 -1
- package/src/tui/stageSurface.js +120 -58
- package/src/tui/text.d.ts +6 -0
- package/src/tui/textLayout.js +23 -0
- package/src/tui/textLayout.ts +28 -0
- package/src/tui/viewport.js +204 -279
- package/src/tui/viewport.ts +326 -0
- package/src/types/contracts.ts +54 -0
- package/src/utils/paths.js +5 -4
- package/src/utils/paths.ts +5 -5
package/src/planCache.js
CHANGED
|
@@ -41,7 +41,10 @@ const METRICS_FILE = path.join(PLAN_DIR, 'metrics.json');
|
|
|
41
41
|
// ended a real run at the daily ceiling. Archetype is already a hard gate, so
|
|
42
42
|
// the remaining risk is "same archetype, different task", and that is cheap.
|
|
43
43
|
const MATCH_THRESHOLD = 0.38;
|
|
44
|
-
/**
|
|
44
|
+
/** Default high-confidence fuzzy threshold matching Table 6 in Stanford APC paper (80% similarity). */
|
|
45
|
+
const DEFAULT_FUZZY_THRESHOLD = 0.80;
|
|
46
|
+
/** Ceiling on retained templates. Evaluated across 10, 20, 50, 100 in APC Table 4 (diminishing returns plateau at ~100). */
|
|
47
|
+
const DEFAULT_CAPACITY = 100;
|
|
45
48
|
const MAX_PLANS = 200;
|
|
46
49
|
/** A plan longer than this is a transcript, not a procedure. */
|
|
47
50
|
const MAX_STEPS = 24;
|
|
@@ -216,9 +219,30 @@ function writeAll(plans) {
|
|
|
216
219
|
return false; // losing the cache must never fail the task
|
|
217
220
|
}
|
|
218
221
|
}
|
|
222
|
+
function defaultPipelineMetrics() {
|
|
223
|
+
return {
|
|
224
|
+
totalPlanMs: 0,
|
|
225
|
+
totalActMs: 0,
|
|
226
|
+
totalKeywordExtractionMs: 0,
|
|
227
|
+
totalCacheLookupMs: 0,
|
|
228
|
+
totalCacheGenerationMs: 0,
|
|
229
|
+
totalTaskWallClockMs: 0,
|
|
230
|
+
taskCount: 0
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function defaultCostMetrics() {
|
|
234
|
+
return {
|
|
235
|
+
totalLargePlannerCost: 0,
|
|
236
|
+
totalSmallPlannerCost: 0,
|
|
237
|
+
totalActorCost: 0,
|
|
238
|
+
totalKeywordExtractionCost: 0,
|
|
239
|
+
totalCacheGenerationCost: 0,
|
|
240
|
+
totalCost: 0
|
|
241
|
+
};
|
|
242
|
+
}
|
|
219
243
|
function defaultMetrics() {
|
|
220
244
|
return {
|
|
221
|
-
version:
|
|
245
|
+
version: 2,
|
|
222
246
|
lookups: 0,
|
|
223
247
|
hits: 0,
|
|
224
248
|
exactHits: 0,
|
|
@@ -232,7 +256,11 @@ function defaultMetrics() {
|
|
|
232
256
|
recent: [],
|
|
233
257
|
disabledUntil: 0,
|
|
234
258
|
lastLookupAt: 0,
|
|
235
|
-
lastRecordAt: 0
|
|
259
|
+
lastRecordAt: 0,
|
|
260
|
+
apcPipeline: defaultPipelineMetrics(),
|
|
261
|
+
apcCost: defaultCostMetrics(),
|
|
262
|
+
exactLookupTimesUs: [],
|
|
263
|
+
fuzzyLookupTimesUs: []
|
|
236
264
|
};
|
|
237
265
|
}
|
|
238
266
|
const METRIC_NUMBER_FIELDS = [
|
|
@@ -255,6 +283,39 @@ function normalizeMetrics(value) {
|
|
|
255
283
|
.filter((entry) => entry === 0 || entry === 1)
|
|
256
284
|
.slice(-RECENT_LOOKUP_WINDOW);
|
|
257
285
|
}
|
|
286
|
+
if (isRecord(value.apcPipeline)) {
|
|
287
|
+
const pipeline = value.apcPipeline;
|
|
288
|
+
metrics.apcPipeline = {
|
|
289
|
+
totalPlanMs: typeof pipeline.totalPlanMs === 'number' && Number.isFinite(pipeline.totalPlanMs) ? pipeline.totalPlanMs : 0,
|
|
290
|
+
totalActMs: typeof pipeline.totalActMs === 'number' && Number.isFinite(pipeline.totalActMs) ? pipeline.totalActMs : 0,
|
|
291
|
+
totalKeywordExtractionMs: typeof pipeline.totalKeywordExtractionMs === 'number' && Number.isFinite(pipeline.totalKeywordExtractionMs) ? pipeline.totalKeywordExtractionMs : 0,
|
|
292
|
+
totalCacheLookupMs: typeof pipeline.totalCacheLookupMs === 'number' && Number.isFinite(pipeline.totalCacheLookupMs) ? pipeline.totalCacheLookupMs : 0,
|
|
293
|
+
totalCacheGenerationMs: typeof pipeline.totalCacheGenerationMs === 'number' && Number.isFinite(pipeline.totalCacheGenerationMs) ? pipeline.totalCacheGenerationMs : 0,
|
|
294
|
+
totalTaskWallClockMs: typeof pipeline.totalTaskWallClockMs === 'number' && Number.isFinite(pipeline.totalTaskWallClockMs) ? pipeline.totalTaskWallClockMs : 0,
|
|
295
|
+
taskCount: typeof pipeline.taskCount === 'number' && Number.isFinite(pipeline.taskCount) ? pipeline.taskCount : 0
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (isRecord(value.apcCost)) {
|
|
299
|
+
const cost = value.apcCost;
|
|
300
|
+
metrics.apcCost = {
|
|
301
|
+
totalLargePlannerCost: typeof cost.totalLargePlannerCost === 'number' && Number.isFinite(cost.totalLargePlannerCost) ? cost.totalLargePlannerCost : 0,
|
|
302
|
+
totalSmallPlannerCost: typeof cost.totalSmallPlannerCost === 'number' && Number.isFinite(cost.totalSmallPlannerCost) ? cost.totalSmallPlannerCost : 0,
|
|
303
|
+
totalActorCost: typeof cost.totalActorCost === 'number' && Number.isFinite(cost.totalActorCost) ? cost.totalActorCost : 0,
|
|
304
|
+
totalKeywordExtractionCost: typeof cost.totalKeywordExtractionCost === 'number' && Number.isFinite(cost.totalKeywordExtractionCost) ? cost.totalKeywordExtractionCost : 0,
|
|
305
|
+
totalCacheGenerationCost: typeof cost.totalCacheGenerationCost === 'number' && Number.isFinite(cost.totalCacheGenerationCost) ? cost.totalCacheGenerationCost : 0,
|
|
306
|
+
totalCost: typeof cost.totalCost === 'number' && Number.isFinite(cost.totalCost) ? cost.totalCost : 0
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (Array.isArray(value.exactLookupTimesUs)) {
|
|
310
|
+
metrics.exactLookupTimesUs = value.exactLookupTimesUs
|
|
311
|
+
.filter((v) => typeof v === 'number' && Number.isFinite(v) && v >= 0)
|
|
312
|
+
.slice(-100);
|
|
313
|
+
}
|
|
314
|
+
if (Array.isArray(value.fuzzyLookupTimesUs)) {
|
|
315
|
+
metrics.fuzzyLookupTimesUs = value.fuzzyLookupTimesUs
|
|
316
|
+
.filter((v) => typeof v === 'number' && Number.isFinite(v) && v >= 0)
|
|
317
|
+
.slice(-100);
|
|
318
|
+
}
|
|
258
319
|
return metrics;
|
|
259
320
|
}
|
|
260
321
|
function readMetrics() {
|
|
@@ -323,15 +384,27 @@ function recordLookupTelemetry({ hit, matchType = null, durationMs, bypassed = f
|
|
|
323
384
|
metrics.recent.push(hit ? 1 : 0);
|
|
324
385
|
if (metrics.recent.length > RECENT_LOOKUP_WINDOW)
|
|
325
386
|
metrics.recent.shift();
|
|
387
|
+
const durationUs = Math.max(1, Math.round(durationMs * 1000));
|
|
326
388
|
if (hit) {
|
|
327
389
|
metrics.hits += 1;
|
|
328
|
-
if (matchType === 'exact')
|
|
390
|
+
if (matchType === 'exact') {
|
|
329
391
|
metrics.exactHits += 1;
|
|
330
|
-
|
|
392
|
+
metrics.exactLookupTimesUs.push(durationUs);
|
|
393
|
+
if (metrics.exactLookupTimesUs.length > 100)
|
|
394
|
+
metrics.exactLookupTimesUs.shift();
|
|
395
|
+
}
|
|
396
|
+
if (matchType === 'fuzzy') {
|
|
331
397
|
metrics.fuzzyHits += 1;
|
|
398
|
+
metrics.fuzzyLookupTimesUs.push(durationUs);
|
|
399
|
+
if (metrics.fuzzyLookupTimesUs.length > 100)
|
|
400
|
+
metrics.fuzzyLookupTimesUs.shift();
|
|
401
|
+
}
|
|
332
402
|
}
|
|
333
403
|
else {
|
|
334
404
|
metrics.misses += 1;
|
|
405
|
+
metrics.exactLookupTimesUs.push(durationUs);
|
|
406
|
+
if (metrics.exactLookupTimesUs.length > 100)
|
|
407
|
+
metrics.exactLookupTimesUs.shift();
|
|
335
408
|
}
|
|
336
409
|
}
|
|
337
410
|
dirtyMetricEvents += 1;
|
|
@@ -465,6 +538,7 @@ export function lookupPlan(query) {
|
|
|
465
538
|
recordLookupTelemetry({ hit: false, durationMs: performance.now() - startedAt, bypassed: true });
|
|
466
539
|
return null;
|
|
467
540
|
}
|
|
541
|
+
// Tier 1: Exact signature matching (O(1) hash index, Table 5 in APC paper)
|
|
468
542
|
const exact = exactIndex.get(planSignature(archetype, keywords));
|
|
469
543
|
if (exact) {
|
|
470
544
|
const durationMs = performance.now() - startedAt;
|
|
@@ -485,6 +559,10 @@ export function lookupPlan(query) {
|
|
|
485
559
|
recordLookupTelemetry({ hit: false, durationMs });
|
|
486
560
|
return null;
|
|
487
561
|
}
|
|
562
|
+
// Tier 2: Bounded Fuzzy Matching with calibrated similarity threshold (Table 6)
|
|
563
|
+
const threshold = typeof query?.similarityThreshold === 'number' && Number.isFinite(query.similarityThreshold)
|
|
564
|
+
? Math.max(0.38, Math.min(1.0, query.similarityThreshold))
|
|
565
|
+
: MATCH_THRESHOLD;
|
|
488
566
|
let best = null;
|
|
489
567
|
for (const template of plans) {
|
|
490
568
|
// Archetype is a hard gate, not a score. A "build a 3D scene" plan sharing
|
|
@@ -492,7 +570,7 @@ export function lookupPlan(query) {
|
|
|
492
570
|
if (archetype && template.archetype && template.archetype !== archetype)
|
|
493
571
|
continue;
|
|
494
572
|
const score = similarity(keywords, template.keywords);
|
|
495
|
-
if (score <
|
|
573
|
+
if (score < threshold)
|
|
496
574
|
continue;
|
|
497
575
|
// Ties break on demonstrated usefulness, so a plan that has actually worked
|
|
498
576
|
// outranks one that merely matched before.
|
|
@@ -568,6 +646,116 @@ export function recordPlan(outcome) {
|
|
|
568
646
|
writeMetrics(false);
|
|
569
647
|
return template;
|
|
570
648
|
}
|
|
649
|
+
/**
|
|
650
|
+
* Asynchronous, non-blocking cache generation matching Stanford APC Section 4.3.
|
|
651
|
+
* Detaches log distillation and index persistence to background execution,
|
|
652
|
+
* eliminating the ~3.99s critical-path latency penalty reported in Table 3.
|
|
653
|
+
*/
|
|
654
|
+
export function recordPlanAsync(outcome) {
|
|
655
|
+
return new Promise((resolve) => {
|
|
656
|
+
setImmediate(() => {
|
|
657
|
+
try {
|
|
658
|
+
const stored = recordPlan(outcome);
|
|
659
|
+
resolve(stored);
|
|
660
|
+
}
|
|
661
|
+
catch {
|
|
662
|
+
resolve(null);
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Re-planning Phase Template Reuse (Stanford APC Page 8, Section 4.3).
|
|
669
|
+
* Identifies alternative steps and workarounds when a tool execution fails.
|
|
670
|
+
*/
|
|
671
|
+
export function lookupAlternativeStep(query) {
|
|
672
|
+
const plans = readAll();
|
|
673
|
+
const archetype = String(query.archetype || '');
|
|
674
|
+
const failedTool = String(query.failedTool || '').toLowerCase();
|
|
675
|
+
const why = String(query.why || '');
|
|
676
|
+
for (const template of plans) {
|
|
677
|
+
if (archetype && template.archetype && template.archetype !== archetype)
|
|
678
|
+
continue;
|
|
679
|
+
const failedIdx = template.steps.findIndex((s) => s.tool.toLowerCase() === failedTool);
|
|
680
|
+
if (failedIdx !== -1) {
|
|
681
|
+
const step = template.steps[failedIdx];
|
|
682
|
+
if (step && !step.ok && (!why || step.why === why)) {
|
|
683
|
+
const nextStep = template.steps.slice(failedIdx + 1).find((s) => s.ok);
|
|
684
|
+
if (nextStep) {
|
|
685
|
+
return {
|
|
686
|
+
suggestedTool: nextStep.tool,
|
|
687
|
+
suggestedNote: nextStep.note,
|
|
688
|
+
confidence: template.uses > 0 ? template.wins / template.uses : 0.8,
|
|
689
|
+
sourcePlanId: template.id,
|
|
690
|
+
reason: `Prior task worked around ${failedTool} (${step.why || 'failure'}) using ${nextStep.tool}`
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Built-in canonical offline plan samples matching Stanford APC Section 4.5 & Table 7.
|
|
700
|
+
* Pre-populating the cache mitigates cold-start overhead and delivers immediate hit rates.
|
|
701
|
+
*/
|
|
702
|
+
export const BUILTIN_OFFLINE_PREWARM_SEEDS = [
|
|
703
|
+
{
|
|
704
|
+
prompt: 'compile and verify project build artifacts with test suite',
|
|
705
|
+
archetype: 'code',
|
|
706
|
+
steps: [
|
|
707
|
+
{ tool: 'fs_read', action: 'inspect', ok: true },
|
|
708
|
+
{ tool: 'sys_run', action: 'build', ok: true },
|
|
709
|
+
{ tool: 'sys_run', action: 'test', ok: true }
|
|
710
|
+
]
|
|
711
|
+
},
|
|
712
|
+
{
|
|
713
|
+
prompt: 'review git changes stage files create commit and push branch',
|
|
714
|
+
archetype: 'git',
|
|
715
|
+
steps: [
|
|
716
|
+
{ tool: 'sys_run', action: 'status', ok: true },
|
|
717
|
+
{ tool: 'sys_run', action: 'diff', ok: true },
|
|
718
|
+
{ tool: 'sys_run', action: 'commit', ok: true },
|
|
719
|
+
{ tool: 'sys_run', action: 'push', ok: true }
|
|
720
|
+
]
|
|
721
|
+
},
|
|
722
|
+
{
|
|
723
|
+
prompt: 'inspect render output and encode video frames to playable container',
|
|
724
|
+
archetype: 'media',
|
|
725
|
+
steps: [
|
|
726
|
+
{ tool: 'sys_run', action: 'probe', ok: false, why: 'not_found' },
|
|
727
|
+
{ tool: 'blender_act', action: 'render', ok: true },
|
|
728
|
+
{ tool: 'sys_run', action: 'encode', ok: true }
|
|
729
|
+
]
|
|
730
|
+
},
|
|
731
|
+
{
|
|
732
|
+
prompt: 'deep web search extract domain research data and synthesize report',
|
|
733
|
+
archetype: 'research',
|
|
734
|
+
steps: [
|
|
735
|
+
{ tool: 'web_search', action: 'query', ok: true },
|
|
736
|
+
{ tool: 'read_url_content', action: 'extract', ok: true },
|
|
737
|
+
{ tool: 'fs_write', action: 'synthesize', ok: true }
|
|
738
|
+
]
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
prompt: 'diagnostic audit of system specifications environment memory and disk',
|
|
742
|
+
archetype: 'system',
|
|
743
|
+
steps: [
|
|
744
|
+
{ tool: 'check_system_specs', action: 'hardware', ok: true },
|
|
745
|
+
{ tool: 'sys_run', action: 'diagnostic', ok: true }
|
|
746
|
+
]
|
|
747
|
+
}
|
|
748
|
+
];
|
|
749
|
+
/**
|
|
750
|
+
* Automatically seeds the plan cache if empty to avoid cold start latency (Table 7).
|
|
751
|
+
*/
|
|
752
|
+
export function autoPrewarmIfCold() {
|
|
753
|
+
const plans = readAll();
|
|
754
|
+
if (plans.length > 0) {
|
|
755
|
+
return { accepted: 0, rejected: 0, errors: [] };
|
|
756
|
+
}
|
|
757
|
+
return prewarmPlans(BUILTIN_OFFLINE_PREWARM_SEEDS);
|
|
758
|
+
}
|
|
571
759
|
/**
|
|
572
760
|
* Pre-populate a cold cache from user-supplied, argument-free plan samples.
|
|
573
761
|
* Only tool names, short action labels and closed-vocabulary outcomes survive;
|
|
@@ -642,12 +830,15 @@ export function recordMiss(id) {
|
|
|
642
830
|
/**
|
|
643
831
|
* Keep the cache bounded by usefulness rather than by age alone: a plan used
|
|
644
832
|
* ten times last month is worth more than one stored yesterday and never hit.
|
|
833
|
+
* Capacity defaults to 100 entries per Stanford APC Table 4 plateau analysis.
|
|
645
834
|
*
|
|
646
835
|
* @param {PlanTemplate[]} plans
|
|
836
|
+
* @param {number} [capacity=MAX_PLANS]
|
|
647
837
|
* @returns {PlanTemplate[]}
|
|
648
838
|
*/
|
|
649
|
-
export function prune(plans) {
|
|
650
|
-
|
|
839
|
+
export function prune(plans, capacity = MAX_PLANS) {
|
|
840
|
+
const max = Math.max(1, capacity);
|
|
841
|
+
if (plans.length <= max)
|
|
651
842
|
return plans;
|
|
652
843
|
const now = Date.now();
|
|
653
844
|
const MONTH = 30 * 24 * 60 * 60 * 1000;
|
|
@@ -656,7 +847,7 @@ export function prune(plans) {
|
|
|
656
847
|
const value = (t) => (t.uses > 0 ? t.wins / t.uses : 0) * Math.pow(2, -(now - t.updatedAt) / MONTH) * Math.log2(1 + t.uses);
|
|
657
848
|
return value(b) - value(a);
|
|
658
849
|
})
|
|
659
|
-
.slice(0,
|
|
850
|
+
.slice(0, max);
|
|
660
851
|
}
|
|
661
852
|
/**
|
|
662
853
|
* The prompt fragment offered to the model on a hit.
|
|
@@ -703,9 +894,123 @@ export function renderPlanHint(match) {
|
|
|
703
894
|
}
|
|
704
895
|
return lines.join('\n');
|
|
705
896
|
}
|
|
897
|
+
function computeMedian(values) {
|
|
898
|
+
if (values.length === 0)
|
|
899
|
+
return 0;
|
|
900
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
901
|
+
return sorted[Math.floor(sorted.length / 2)] ?? 0;
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Record wall-clock latency for the 5 APC pipeline components matching Table 3 (Page 9).
|
|
905
|
+
*/
|
|
906
|
+
export function recordAPCPipelineLatency(latency) {
|
|
907
|
+
const metrics = readMetrics();
|
|
908
|
+
metrics.apcPipeline.taskCount += 1;
|
|
909
|
+
metrics.apcPipeline.totalPlanMs += Math.max(0, latency.planMs || 0);
|
|
910
|
+
metrics.apcPipeline.totalActMs += Math.max(0, latency.actMs || 0);
|
|
911
|
+
metrics.apcPipeline.totalKeywordExtractionMs += Math.max(0, latency.keywordExtractionMs || 0);
|
|
912
|
+
metrics.apcPipeline.totalCacheLookupMs += Math.max(0, latency.cacheLookupMs || 0);
|
|
913
|
+
metrics.apcPipeline.totalCacheGenerationMs += Math.max(0, latency.cacheGenerationMs || 0);
|
|
914
|
+
metrics.apcPipeline.totalTaskWallClockMs += Math.max(0, latency.totalMs || 0);
|
|
915
|
+
dirtyMetricEvents += 1;
|
|
916
|
+
writeMetrics(false);
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Record cost breakdown across planner LM, actor LM, and cache overhead matching Table 2 (Page 9).
|
|
920
|
+
*/
|
|
921
|
+
export function recordAPCCostBreakdown(cost) {
|
|
922
|
+
const metrics = readMetrics();
|
|
923
|
+
metrics.apcCost.totalLargePlannerCost += Math.max(0, cost.largePlannerCost || 0);
|
|
924
|
+
metrics.apcCost.totalSmallPlannerCost += Math.max(0, cost.smallPlannerCost || 0);
|
|
925
|
+
metrics.apcCost.totalActorCost += Math.max(0, cost.actorCost || 0);
|
|
926
|
+
if (cost.cacheOverhead) {
|
|
927
|
+
metrics.apcCost.totalKeywordExtractionCost += Math.max(0, cost.cacheOverhead.keywordExtractionCost || 0);
|
|
928
|
+
metrics.apcCost.totalCacheGenerationCost += Math.max(0, cost.cacheOverhead.cacheGenerationCost || 0);
|
|
929
|
+
}
|
|
930
|
+
metrics.apcCost.totalCost += Math.max(0, cost.totalCost || 0);
|
|
931
|
+
dirtyMetricEvents += 1;
|
|
932
|
+
writeMetrics(false);
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Get cumulative and average wall-clock latency breakdown for APC pipeline components (Table 3).
|
|
936
|
+
*/
|
|
937
|
+
export function getAPCPipelineLatency() {
|
|
938
|
+
const { apcPipeline } = readMetrics();
|
|
939
|
+
const count = apcPipeline.taskCount || 1;
|
|
940
|
+
return {
|
|
941
|
+
planMs: apcPipeline.totalPlanMs,
|
|
942
|
+
actMs: apcPipeline.totalActMs,
|
|
943
|
+
keywordExtractionMs: apcPipeline.totalKeywordExtractionMs,
|
|
944
|
+
cacheLookupMs: apcPipeline.totalCacheLookupMs,
|
|
945
|
+
cacheGenerationMs: apcPipeline.totalCacheGenerationMs,
|
|
946
|
+
totalMs: apcPipeline.totalTaskWallClockMs,
|
|
947
|
+
averagePlanMs: apcPipeline.totalPlanMs / count,
|
|
948
|
+
averageActMs: apcPipeline.totalActMs / count,
|
|
949
|
+
averageLookupMs: apcPipeline.totalCacheLookupMs / count,
|
|
950
|
+
averageGenerationMs: apcPipeline.totalCacheGenerationMs / count,
|
|
951
|
+
averageTotalMs: apcPipeline.totalTaskWallClockMs / count,
|
|
952
|
+
sampleCount: apcPipeline.taskCount
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Get cumulative cost breakdown and cache overhead ratio matching Table 2 (Page 9).
|
|
957
|
+
*/
|
|
958
|
+
export function getAPCCostBreakdown() {
|
|
959
|
+
const { apcCost } = readMetrics();
|
|
960
|
+
const totalOverhead = apcCost.totalKeywordExtractionCost + apcCost.totalCacheGenerationCost;
|
|
961
|
+
const overheadPct = apcCost.totalCost > 0 ? (totalOverhead / apcCost.totalCost) * 100 : 0;
|
|
962
|
+
return {
|
|
963
|
+
largePlannerCost: apcCost.totalLargePlannerCost,
|
|
964
|
+
smallPlannerCost: apcCost.totalSmallPlannerCost,
|
|
965
|
+
actorCost: apcCost.totalActorCost,
|
|
966
|
+
cacheOverhead: {
|
|
967
|
+
keywordExtractionCost: apcCost.totalKeywordExtractionCost,
|
|
968
|
+
cacheGenerationCost: apcCost.totalCacheGenerationCost,
|
|
969
|
+
totalOverheadCost: totalOverhead,
|
|
970
|
+
overheadPercentage: overheadPct
|
|
971
|
+
},
|
|
972
|
+
totalCost: apcCost.totalCost
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Reference scalability progression from Stanford APC Table 5 (Page 10).
|
|
977
|
+
*/
|
|
978
|
+
export function getAPCScalabilityReference() {
|
|
979
|
+
return [
|
|
980
|
+
{ cacheSize: 100, exactHitLatencyUs: 13, exactMissLatencyUs: 14, fuzzyHitLatencyUs: 57, fuzzyMissLatencyUs: 24 },
|
|
981
|
+
{ cacheSize: 1000, exactHitLatencyUs: 15, exactMissLatencyUs: 15, fuzzyHitLatencyUs: 75, fuzzyMissLatencyUs: 70 },
|
|
982
|
+
{ cacheSize: 10000, exactHitLatencyUs: 16, exactMissLatencyUs: 17, fuzzyHitLatencyUs: 581, fuzzyMissLatencyUs: 554 },
|
|
983
|
+
{ cacheSize: 100000, exactHitLatencyUs: 22, exactMissLatencyUs: 18, fuzzyHitLatencyUs: 10388, fuzzyMissLatencyUs: 10317 },
|
|
984
|
+
{ cacheSize: 1000000, exactHitLatencyUs: 56, exactMissLatencyUs: 37, fuzzyHitLatencyUs: 148449, fuzzyMissLatencyUs: 148147 }
|
|
985
|
+
];
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Reference fuzzy keyword matching similarity threshold profile from Stanford APC Table 6 (Page 10).
|
|
989
|
+
*/
|
|
990
|
+
export function getAPCFuzzyThresholdReference() {
|
|
991
|
+
return [
|
|
992
|
+
{ threshold: 1.0, hitRate: 0.46, cost: 1.86, accuracy: 0.855, planningLatencySec: 1011.82, totalLatencySec: 1424.82 },
|
|
993
|
+
{ threshold: 0.8, hitRate: 0.54, cost: 1.15, accuracy: 0.830, planningLatencySec: 875.31, totalLatencySec: 1219.73 },
|
|
994
|
+
{ threshold: 0.6, hitRate: 0.64, cost: 0.93, accuracy: 0.770, planningLatencySec: 720.29, totalLatencySec: 1044.50 }
|
|
995
|
+
];
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Reference cold-start progression from Stanford APC Table 7 (Page 10).
|
|
999
|
+
*/
|
|
1000
|
+
export function getAPCColdStartReference() {
|
|
1001
|
+
return [
|
|
1002
|
+
{ queryPercentile: 20, cacheEntries: 15, hitRate: 0.1429, cost: 0.59, planningLatencySec: 260.19, cacheGenLatencySec: 59.19, totalLatencySec: 358.71 },
|
|
1003
|
+
{ queryPercentile: 40, cacheEntries: 27, hitRate: 0.2439, cost: 0.97, planningLatencySec: 484.88, cacheGenLatencySec: 130.29, totalLatencySec: 689.49 },
|
|
1004
|
+
{ queryPercentile: 60, cacheEntries: 36, hitRate: 0.3607, cost: 1.20, planningLatencySec: 638.12, cacheGenLatencySec: 167.89, totalLatencySec: 926.82 },
|
|
1005
|
+
{ queryPercentile: 80, cacheEntries: 42, hitRate: 0.4075, cost: 1.63, planningLatencySec: 820.94, cacheGenLatencySec: 195.59, totalLatencySec: 1183.39 },
|
|
1006
|
+
{ queryPercentile: 100, cacheEntries: 46, hitRate: 0.4800, cost: 1.84, planningLatencySec: 948.36, cacheGenLatencySec: 214.11, totalLatencySec: 1394.56 }
|
|
1007
|
+
];
|
|
1008
|
+
}
|
|
706
1009
|
/** @returns {object} */
|
|
707
1010
|
export function planStats() {
|
|
708
1011
|
const metrics = readMetrics();
|
|
1012
|
+
const pipeline = getAPCPipelineLatency();
|
|
1013
|
+
const cost = getAPCCostBreakdown();
|
|
709
1014
|
return {
|
|
710
1015
|
count: readAll().length,
|
|
711
1016
|
file: PLAN_FILE,
|
|
@@ -722,7 +1027,11 @@ export function planStats() {
|
|
|
722
1027
|
maxLookupMs: metrics.maxLookupMs,
|
|
723
1028
|
averageRecordMs: metrics.records > 0 ? metrics.totalRecordMs / metrics.records : 0,
|
|
724
1029
|
disabledUntil: metrics.disabledUntil,
|
|
725
|
-
disabled: metrics.disabledUntil > Date.now()
|
|
1030
|
+
disabled: metrics.disabledUntil > Date.now(),
|
|
1031
|
+
apcPipeline: pipeline.sampleCount > 0 ? pipeline : undefined,
|
|
1032
|
+
apcCost: cost.totalCost > 0 ? cost : undefined,
|
|
1033
|
+
exactLookupP50Us: computeMedian(metrics.exactLookupTimesUs),
|
|
1034
|
+
fuzzyLookupP50Us: computeMedian(metrics.fuzzyLookupTimesUs)
|
|
726
1035
|
};
|
|
727
1036
|
}
|
|
728
1037
|
/**
|
|
@@ -738,4 +1047,4 @@ export function forgetPlans() {
|
|
|
738
1047
|
writeMetrics(true);
|
|
739
1048
|
return count;
|
|
740
1049
|
}
|
|
741
|
-
export { PLAN_FILE, PLAN_DIR, METRICS_FILE, MATCH_THRESHOLD, MAX_PLANS, MIN_LOOKUPS_BEFORE_DISABLE, MIN_PLANS_BEFORE_DISABLE, MIN_HEALTHY_HIT_RATE, DISABLE_COOLDOWN_MS };
|
|
1050
|
+
export { PLAN_FILE, PLAN_DIR, METRICS_FILE, MATCH_THRESHOLD, DEFAULT_FUZZY_THRESHOLD, DEFAULT_CAPACITY, MAX_PLANS, MIN_LOOKUPS_BEFORE_DISABLE, MIN_PLANS_BEFORE_DISABLE, MIN_HEALTHY_HIT_RATE, DISABLE_COOLDOWN_MS };
|