@iowarp/clio-coder 0.3.3 → 0.3.6
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/CHANGELOG.md +74 -0
- package/CONTRIBUTING.md +7 -7
- package/README.md +3 -3
- package/dist/{acp-P2AQILE2.js → acp-2BEHC4DL.js} +9 -8
- package/dist/{agents-72W3BI7I.js → agents-LNNFTM53.js} +29 -24
- package/dist/assets/codewiki.json +1 -1
- package/dist/{auth-5TWEIYDN.js → auth-KXXFI2VS.js} +14 -10
- package/dist/{chunk-GGXXDWE4.js → chunk-22NAGB7X.js} +2 -2
- package/dist/{chunk-YCWGATWI.js → chunk-24I7BN55.js} +2 -2
- package/dist/{chunk-EKMEHE4H.js → chunk-33YXPOE3.js} +2 -3
- package/dist/chunk-3BPUFZDL.js +37 -0
- package/dist/chunk-43AOLP7E.js +375 -0
- package/dist/{chunk-ZDOOVTXZ.js → chunk-4OC57DA6.js} +27 -4
- package/dist/{chunk-6SGHMWE3.js → chunk-5JGRAMKL.js} +5 -5
- package/dist/{chunk-V6RTAOC2.js → chunk-6US73PDB.js} +572 -51
- package/dist/{chunk-5UFT4SUX.js → chunk-6XXKFVSN.js} +3 -3
- package/dist/{chunk-A3CYT5EX.js → chunk-AD2SYQYC.js} +55 -2
- package/dist/chunk-AOCYTWAV.js +449 -0
- package/dist/chunk-CFGTUFWB.js +67 -0
- package/dist/chunk-CJUB2JJ2.js +1478 -0
- package/dist/{chunk-FNTMWMX5.js → chunk-CKXWIANG.js} +14 -12
- package/dist/{chunk-PIWWS5BL.js → chunk-CYQKWTG3.js} +63 -78
- package/dist/{chunk-LZSJBIVT.js → chunk-DJVECN66.js} +271 -762
- package/dist/{chunk-CBCAPZAA.js → chunk-E25LMLRW.js} +2 -2
- package/dist/{chunk-ZWMF7253.js → chunk-E2ER4LJF.js} +304 -9
- package/dist/{chunk-STBPMHSX.js → chunk-EKY57CSP.js} +51 -84
- package/dist/{chunk-DUYJ5IO6.js → chunk-EYPA3EGJ.js} +12 -4
- package/dist/{chunk-M6SHUN7Q.js → chunk-FO5ZOVUY.js} +2 -2
- package/dist/chunk-FYYLNIL5.js +313 -0
- package/dist/{chunk-OQ33BKR3.js → chunk-G7MUEIGA.js} +3 -60
- package/dist/chunk-GEYXPTRF.js +613 -0
- package/dist/chunk-GOXNB3AO.js +261 -0
- package/dist/{chunk-G4BMMOKF.js → chunk-HVDIIIQW.js} +2 -2
- package/dist/chunk-HWUFFB6L.js +83 -0
- package/dist/{chunk-4XUGQOHA.js → chunk-K7T3E2SR.js} +15 -8
- package/dist/chunk-K7VKOLQQ.js +15 -0
- package/dist/{chunk-UFIIWP2H.js → chunk-KHSFENX2.js} +8 -8
- package/dist/{chunk-BMEMKKIT.js → chunk-KOHPCX4K.js} +2 -2
- package/dist/chunk-LCGCVYZ4.js +57 -0
- package/dist/chunk-LL4KHSZI.js +22 -0
- package/dist/{chunk-PAJK6MAQ.js → chunk-LYF7OHWH.js} +42 -15
- package/dist/{chunk-POHLU5DW.js → chunk-M6L6IDJG.js} +3 -3
- package/dist/{chunk-5UUP6MWO.js → chunk-MV3K5QF2.js} +5 -436
- package/dist/{chunk-X4RCMKVQ.js → chunk-NDINPTJ4.js} +2 -2
- package/dist/{chunk-TZK7PACC.js → chunk-NILBFAPG.js} +14 -8
- package/dist/chunk-ODFEOB4F.js +1082 -0
- package/dist/{chunk-AGYYIBLL.js → chunk-OH3TOQTB.js} +6 -2
- package/dist/chunk-OZNBF4L3.js +23 -0
- package/dist/{chunk-DSELYM6W.js → chunk-PBTHKCPN.js} +30 -10
- package/dist/{verify-375KUB3Y.js → chunk-PCZJO5TI.js} +127 -42
- package/dist/{chunk-ED4KHGC3.js → chunk-PPAMZ32Z.js} +9 -2
- package/dist/{chunk-SRDMMSEP.js → chunk-QM3F2GKX.js} +1063 -1645
- package/dist/{chunk-X6IAEBZR.js → chunk-QNQHSOLF.js} +7 -7
- package/dist/{chunk-OC7FIQPC.js → chunk-R46L2BIR.js} +10 -7
- package/dist/{chunk-2TLUCQVG.js → chunk-RD5U66HV.js} +3 -3
- package/dist/{chunk-6N5PTWMY.js → chunk-RY3LY4J5.js} +50 -13
- package/dist/{chunk-OKGUZO2U.js → chunk-SPULKLCF.js} +4 -3
- package/dist/{chunk-OOJYHWRB.js → chunk-TSHXZTOQ.js} +6 -5
- package/dist/chunk-TZSKNMZG.js +434 -0
- package/dist/{chunk-7MNJORFF.js → chunk-UL3WSD3F.js} +6 -1
- package/dist/{chunk-VJWL6YS5.js → chunk-UUVG37B4.js} +2 -2
- package/dist/{chunk-COU2UHX6.js → chunk-VEZEGCGW.js} +170 -2
- package/dist/chunk-W6GROXXM.js +69 -0
- package/dist/{chunk-OAO4GE4M.js → chunk-WHGPSPT5.js} +2 -2
- package/dist/chunk-WHJYKASB.js +677 -0
- package/dist/{chunk-ORBHGJC5.js → chunk-WR67VIZY.js} +3 -3
- package/dist/{chunk-YHZX5GEU.js → chunk-XAKHZX5N.js} +2 -2
- package/dist/{chunk-TZTZS7QK.js → chunk-XE2VEJHX.js} +5 -3
- package/dist/{chunk-LM5TQCJZ.js → chunk-XF5N4U5A.js} +8 -7
- package/dist/{chunk-LWLEKMDQ.js → chunk-XXQNGV4M.js} +1073 -552
- package/dist/{chunk-KZWTDYJF.js → chunk-XYDYPRZI.js} +7 -7
- package/dist/chunk-ZGVHUX3M.js +66 -0
- package/dist/{chunk-LW6DSM3M.js → chunk-ZRGEBJ4T.js} +1192 -1119
- package/dist/{chunk-2DJ2KNFG.js → chunk-ZXF4XRKW.js} +202 -40
- package/dist/chunk-ZZMN5OM4.js +122 -0
- package/dist/cli/index.js +34 -30
- package/dist/{clio-JOU4FXVA.js → clio-M2KGYUFZ.js} +7 -6
- package/dist/{code-nav-7AX6FYE6.js → code-nav-GQNL7XA6.js} +8 -6
- package/dist/codewiki/build-worker.js +4 -4
- package/dist/{components-KELWS457.js → components-5TTYYX6G.js} +3 -3
- package/dist/{config-XCDVKR23.js → config-XUUYQIWO.js} +47 -35
- package/dist/{configure-4GAP54ZW.js → configure-IHJ7YOMV.js} +18 -15
- package/dist/{context-77FM5DV5.js → context-74JLXAWD.js} +18 -10
- package/dist/{context-4UOGGLQ5.js → context-75MIWW3U.js} +41 -29
- package/dist/{context-5VKGUVJJ.js → context-ZQ7SIFJV.js} +85 -9
- package/dist/{context-clear-XXJRLCJJ.js → context-clear-GYKWNUML.js} +41 -29
- package/dist/{context-index-BZ4UYMTC.js → context-index-SSR5ECNE.js} +3 -3
- package/dist/context-working-set-UX5KEP4J.js +1553 -0
- package/dist/{dispatch-runner-QPRDDBDX.js → dispatch-runner-GIJBHNFL.js} +47 -32
- package/dist/{docs-2C2LTVT2.js → docs-6FZSCG5B.js} +3 -3
- package/dist/{doctor-HR46URBJ.js → doctor-SVJ5BZCW.js} +12 -12
- package/dist/{eval-XSSNATB4.js → eval-CG6LLBLD.js} +54 -238
- package/dist/{evidence-6HG2PY2B.js → evidence-ZYFIEN42.js} +57 -28
- package/dist/{evolve-K7YU3NCY.js → evolve-QGEXEMDW.js} +36 -25
- package/dist/{extensions-QVDOHDGJ.js → extensions-ADGNCJJD.js} +3 -3
- package/dist/{fleet-VY3HHKN6.js → fleet-S5R4ZOQY.js} +73 -44
- package/dist/{fleet-preflight-DDN536IT.js → fleet-preflight-BHSNPBMH.js} +3 -3
- package/dist/{init-JYGXI3FK.js → init-5DRU55YR.js} +49 -37
- package/dist/memory-7YKKR6UC.js +467 -0
- package/dist/{models-I5QWSEOM.js → models-ZPOLRU2C.js} +24 -21
- package/dist/{monitor-GE4ID3IA.js → monitor-US5F5YGZ.js} +73 -46
- package/dist/{orchestrator-EM5MC3HM.js → orchestrator-E2AL4T5N.js} +1624 -1007
- package/dist/{paths-UXLN5YYZ.js → paths-E7KYAQWE.js} +3 -3
- package/dist/{reset-L2FQEE3E.js → reset-KZ652EK6.js} +6 -5
- package/dist/{run-ZU3QMZPZ.js → run-SRNBKDWD.js} +76 -54
- package/dist/{share-S5BZQC5I.js → share-CGZE33UP.js} +7 -6
- package/dist/{skills-X5VXCRNQ.js → skills-S2X4DLY5.js} +4 -4
- package/dist/{skills-eval-WKIHWTHR.js → skills-eval-W2GGIC4R.js} +40 -29
- package/dist/{targets-SNCPI2NR.js → targets-54SWINWB.js} +28 -23
- package/dist/{terminal-lease-BNAHVHBS.js → terminal-lease-SAIF2OGY.js} +6 -4
- package/dist/{uninstall-FZCQCDKC.js → uninstall-BVLWXKBT.js} +3 -3
- package/dist/{upgrade-JQHHPQ4K.js → upgrade-JKAR27XC.js} +20 -19
- package/dist/{usage-OR4O5SMZ.js → usage-MSAWCLX4.js} +79 -36
- package/dist/verifiers-NCBTHHN2.js +1220 -0
- package/dist/verify-X5HDROLA.js +25 -0
- package/dist/{wiki-generate-UEXP2ARI.js → wiki-generate-GUSOQ6ZP.js} +50 -37
- package/dist/worker/entry.js +90 -70
- package/dist/{workspace-G4ZWUIPR.js → workspace-ZJ6BFM3Q.js} +4 -4
- package/docs/README.md +8 -7
- package/docs/acp.md +1 -1
- package/docs/alcf-provider.md +1 -1
- package/docs/architecture.md +2 -2
- package/docs/artifact-placement.md +1 -2
- package/docs/artifact-versions.md +1 -1
- package/docs/built-in-agents.md +1 -1
- package/docs/capacity-and-scheduling.md +1 -1
- package/docs/commands-and-modes.md +60 -26
- package/docs/config-knobs-audit.md +1 -2
- package/docs/configuration-and-targets.md +26 -1
- package/docs/context-engine.md +67 -13
- package/docs/context-working-set.md +194 -0
- package/docs/development-pipeline.md +1 -1
- package/docs/documentation-coverage.md +6 -6
- package/docs/documentation-guide.md +7 -6
- package/docs/environment-variables.md +2 -1
- package/docs/eval-runner.md +1 -1
- package/docs/evals-internal.md +4 -32
- package/docs/evidence-and-memory.md +139 -7
- package/docs/evolution.md +1 -1
- package/docs/exit-codes-and-output.md +1 -1
- package/docs/extensions-and-sharing.md +2 -2
- package/docs/fleet-dispatch.md +49 -8
- package/docs/glossary.md +21 -1
- package/docs/installation-and-lifecycle.md +2 -2
- package/docs/middleware-and-components.md +19 -2
- package/docs/model-catalog.md +7 -9
- package/docs/observability.md +4 -4
- package/docs/proactive-memory.md +26 -16
- package/docs/prompt-envelope-and-tools.md +7 -5
- package/docs/provider-adapter-cookbook.md +1 -1
- package/docs/release-cut-checklist.md +43 -40
- package/docs/safety-model.md +49 -8
- package/docs/scientific-validation.md +21 -3
- package/docs/session-lifecycle.md +3 -3
- package/docs/skills-marketplace.md +1 -1
- package/docs/tool-usage.md +79 -12
- package/docs/trace-store.md +1 -1
- package/docs/troubleshooting.md +1 -1
- package/docs/tui-design.md +38 -4
- package/docs/worker-dispatch-mechanics.md +11 -1
- package/package.json +13 -13
- package/skills/meta/clio-test/SKILL.md +20 -17
- package/skills/meta/clio-test/evals.md +3 -3
- package/skills/meta/clio-test/references/harness.md +35 -6
- package/skills/meta/clio-test/references/test-map.md +20 -10
- package/skills/registry.yaml +2 -2
- package/skills/skill-marketplace.json +1 -1
- package/src/cli/agents.ts +2 -3
- package/src/cli/argv.ts +14 -1
- package/src/cli/context-working-set.ts +513 -0
- package/src/cli/context.ts +8 -0
- package/src/cli/evidence.ts +20 -2
- package/src/cli/fleet.ts +15 -0
- package/src/cli/index.ts +5 -1
- package/src/cli/memory.ts +272 -10
- package/src/cli/modes/json-stream.ts +2 -2
- package/src/cli/modes/print.ts +12 -1
- package/src/cli/run.ts +22 -2
- package/src/cli/targets.ts +12 -3
- package/src/cli/usage.ts +55 -7
- package/src/cli/verifiers.ts +325 -0
- package/src/core/bash-exec.ts +39 -14
- package/src/core/bus-events.ts +22 -4
- package/src/core/config.ts +54 -0
- package/src/core/defaults.ts +50 -3
- package/src/core/response-model-id.ts +134 -0
- package/src/core/toml.ts +62 -0
- package/src/core/verification-scripts.ts +6 -0
- package/src/core/workspace-files.ts +0 -1
- package/src/domains/agents/builtins/architect.md +1 -1
- package/src/domains/agents/builtins/verifier.md +3 -0
- package/src/domains/agents/catalog.ts +5 -4
- package/src/domains/agents/recipe.ts +54 -14
- package/src/domains/agents/result-contract.ts +7 -4
- package/src/domains/config/classify.ts +1 -0
- package/src/domains/context/bootstrap.ts +36 -27
- package/src/domains/context/project-metadata.ts +19 -63
- package/src/domains/context/prompt-context.ts +8 -0
- package/src/domains/context/working-set/contract.ts +161 -0
- package/src/domains/context/working-set/defaults.ts +28 -0
- package/src/domains/context/working-set/engine.ts +203 -0
- package/src/domains/context/working-set/fold.ts +62 -0
- package/src/domains/context/working-set/horizon.ts +38 -0
- package/src/domains/context/working-set/marker.ts +103 -0
- package/src/domains/context/working-set/path-index.ts +436 -0
- package/src/domains/context/working-set/payload.ts +152 -0
- package/src/domains/context/working-set/policies/age-horizon.ts +55 -0
- package/src/domains/context/working-set/policies/index.ts +20 -0
- package/src/domains/context/working-set/policies/structural.ts +160 -0
- package/src/domains/context/working-set/project.ts +132 -0
- package/src/domains/context/working-set/protect.ts +109 -0
- package/src/domains/context/working-set/recall.ts +177 -0
- package/src/domains/context/working-set/replay/controls.ts +112 -0
- package/src/domains/context/working-set/replay/load-clio.ts +199 -0
- package/src/domains/context/working-set/replay/metrics.ts +185 -0
- package/src/domains/context/working-set/replay/reference-graph.ts +79 -0
- package/src/domains/context/working-set/replay/report.ts +139 -0
- package/src/domains/context/working-set/replay/runner.ts +325 -0
- package/src/domains/context/working-set/replay/synthetic.ts +422 -0
- package/src/domains/context/working-set/replay/trace.ts +21 -0
- package/src/domains/context/working-set/visible.ts +54 -0
- package/src/domains/dispatch/budget-envelope.ts +396 -0
- package/src/domains/dispatch/contract.ts +2 -0
- package/src/domains/dispatch/extension.ts +81 -27
- package/src/domains/dispatch/orphan-recovery.ts +1 -0
- package/src/domains/dispatch/receipt-integrity.ts +4 -0
- package/src/domains/dispatch/state.ts +1 -0
- package/src/domains/dispatch/types.ts +10 -3
- package/src/domains/dispatch/validation.ts +14 -0
- package/src/domains/dispatch/worker-spawn.ts +14 -3
- package/src/domains/eval/metrics/evidence.ts +0 -116
- package/src/domains/eval/metrics/invariants.ts +1 -1
- package/src/domains/eval/runners/clio-run.ts +1 -10
- package/src/domains/eval/runners/external-command.ts +2 -29
- package/src/domains/eval/schema/suite.ts +0 -7
- package/src/domains/eval/suites/run.ts +1 -7
- package/src/domains/evidence/build.ts +112 -45
- package/src/domains/evidence/eval.ts +24 -7
- package/src/domains/evidence/index.ts +53 -0
- package/src/domains/evidence/ordering.ts +12 -0
- package/src/domains/evidence/run-trust.ts +221 -0
- package/src/domains/evidence/store.ts +46 -6
- package/src/domains/evidence/trust-status.ts +854 -0
- package/src/domains/evidence/types.ts +26 -0
- package/src/domains/memory/index.ts +22 -0
- package/src/domains/memory/operations.ts +58 -1
- package/src/domains/memory/promotion.ts +281 -0
- package/src/domains/memory/prompt-section.ts +25 -5
- package/src/domains/memory/proposal.ts +51 -7
- package/src/domains/memory/task-bank.ts +3 -2
- package/src/domains/memory/task-memory-handoff.ts +181 -24
- package/src/domains/memory/task-memory-policy.ts +3 -1
- package/src/domains/memory/types.ts +37 -0
- package/src/domains/memory/validate.ts +178 -0
- package/src/domains/middleware/memory-intervention.ts +38 -25
- package/src/domains/middleware/runtime.ts +6 -0
- package/src/domains/middleware/skills-reminder.ts +19 -4
- package/src/domains/middleware/stalled-turn.ts +208 -5
- package/src/domains/middleware/types.ts +10 -0
- package/src/domains/observability/contract.ts +6 -1
- package/src/domains/observability/cost.ts +20 -4
- package/src/domains/observability/extension.ts +2 -2
- package/src/domains/providers/index.ts +3 -0
- package/src/domains/providers/model-discovery.ts +9 -0
- package/src/domains/providers/runtime-resolution.ts +38 -1
- package/src/domains/providers/runtimes/common/probe-helpers.ts +97 -16
- package/src/domains/providers/types/context-window-slots.ts +18 -0
- package/src/domains/providers/types/runtime-descriptor.ts +3 -1
- package/src/domains/safety/autonomy.ts +1 -1
- package/src/domains/safety/call-target.ts +211 -14
- package/src/domains/safety/decision-presentation.ts +268 -0
- package/src/domains/safety/default-path-policy.ts +8 -0
- package/src/domains/safety/finish-contract.ts +4 -3
- package/src/domains/safety/policy-engine.ts +48 -6
- package/src/domains/safety/redaction.ts +73 -0
- package/src/domains/session/compaction/compact.ts +23 -1
- package/src/domains/session/compaction/cut-point.ts +2 -0
- package/src/domains/session/compaction/tokens.ts +16 -1
- package/src/domains/session/context-ledger.ts +12 -1
- package/src/domains/session/decision-board.ts +4 -0
- package/src/domains/session/entries.ts +110 -1
- package/src/domains/session/history.ts +68 -19
- package/src/domains/session/manager.ts +9 -2
- package/src/domains/session/migrations/index.ts +22 -3
- package/src/domains/session/usage.ts +24 -7
- package/src/engine/acp/event-mapper.ts +7 -0
- package/src/engine/acp/server.ts +32 -2
- package/src/engine/agent.ts +18 -1
- package/src/engine/apis/lmstudio.ts +25 -4
- package/src/engine/apis/openai-completions.ts +147 -22
- package/src/engine/claude/sdk-runtime.ts +8 -2
- package/src/engine/claude/tool-safety.ts +13 -0
- package/src/engine/loop-guard.ts +27 -3
- package/src/engine/session.ts +9 -3
- package/src/engine/worker-events.ts +4 -3
- package/src/engine/worker-runtime.ts +59 -54
- package/src/entry/orchestrator.ts +34 -5
- package/src/interactive/chat-loop-messages.ts +40 -6
- package/src/interactive/chat-loop.ts +13 -0
- package/src/interactive/chat-panel.ts +17 -1
- package/src/interactive/chat-renderer.ts +49 -24
- package/src/interactive/clio-editor.ts +44 -7
- package/src/interactive/context-meter.ts +10 -0
- package/src/interactive/context-overlay.ts +120 -7
- package/src/interactive/context-recall-command.ts +110 -0
- package/src/interactive/cost-overlay.ts +39 -8
- package/src/interactive/dispatch-board.ts +212 -35
- package/src/interactive/footer/widgets.ts +13 -0
- package/src/interactive/interactive-application.ts +6 -1
- package/src/interactive/interactive-input-runtime.ts +11 -1
- package/src/interactive/interactive-presentation.ts +11 -1
- package/src/interactive/interactive-slash-runtime.ts +37 -1
- package/src/interactive/memory-overlay.ts +89 -4
- package/src/interactive/model-session-replay.ts +21 -0
- package/src/interactive/overlay-ask-user-lifecycle.ts +1 -1
- package/src/interactive/overlay-frame.ts +5 -2
- package/src/interactive/overlay-general-openers.ts +46 -1
- package/src/interactive/overlay-key-routing.ts +41 -1
- package/src/interactive/overlay-lifecycle.ts +11 -4
- package/src/interactive/overlay-permission-lifecycle.ts +23 -8
- package/src/interactive/overlay-session-lifecycle.ts +8 -4
- package/src/interactive/overlay-transitions.ts +11 -0
- package/src/interactive/overlays/ask-user.ts +74 -30
- package/src/interactive/overlays/decisions.ts +3 -1
- package/src/interactive/permission-hint.ts +35 -0
- package/src/interactive/permission-overlay.ts +95 -45
- package/src/interactive/renderers/tool-execution.ts +37 -51
- package/src/interactive/session-last-turn.ts +8 -1
- package/src/interactive/session-transcript.ts +2 -2
- package/src/interactive/session-usage-reseed.ts +36 -10
- package/src/interactive/slash-commands.ts +31 -4
- package/src/interactive/status/summary.ts +5 -0
- package/src/interactive/status/types.ts +5 -0
- package/src/interactive/terminal-lease.ts +1 -0
- package/src/interactive/turn-context.ts +333 -110
- package/src/interactive/turn-middleware.ts +7 -6
- package/src/interactive/turn-runtime.ts +37 -8
- package/src/interactive/turn-state.ts +3 -0
- package/src/interactive/worker-progress.ts +440 -0
- package/src/interactive/worker-stream.ts +51 -110
- package/src/tools/agent-tools.ts +39 -7
- package/src/tools/ask-user.ts +21 -1
- package/src/tools/bash.ts +144 -82
- package/src/tools/builtin-tool-catalog.ts +11 -5
- package/src/tools/context/index.ts +107 -5
- package/src/tools/context/surface.ts +3 -2
- package/src/tools/core-bootstrap.ts +21 -0
- package/src/tools/dispatch-arguments.ts +8 -0
- package/src/tools/dispatch-event-text.ts +19 -0
- package/src/tools/dispatch-runner.ts +9 -7
- package/src/tools/dispatch.ts +24 -1
- package/src/tools/monitor.ts +43 -20
- package/src/tools/registry.ts +72 -10
- package/src/tools/result-disposition.ts +706 -0
- package/src/tools/result-shaping.ts +321 -20
- package/src/tools/safe-exec.ts +2 -0
- package/src/tools/verify/authoring.ts +1120 -0
- package/src/tools/verify/catalog.ts +346 -0
- package/src/tools/verify/index.ts +13 -3
- package/src/tools/verify/scripts.ts +135 -37
- package/src/tools/verify/surface.ts +9 -5
- package/src/tools/worker-evidence.ts +54 -12
- package/src/worker/spec-contract.ts +43 -3
- package/dist/chunk-J7CWMCQD.js +0 -255
- package/dist/chunk-T6YILFSB.js +0 -80
- package/dist/chunk-VAKQQHWR.js +0 -434
- package/dist/chunk-VPAYEGVX.js +0 -184
- package/dist/chunk-XBXAASKX.js +0 -18
- package/dist/memory-WFZMGYHX.js +0 -236
- package/src/domains/eval/metrics/chaos-stream.ts +0 -93
|
@@ -0,0 +1,1553 @@
|
|
|
1
|
+
import { createRequire as __clioCreateRequire } from "node:module"; const require = __clioCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_KEEP_RECENT_TOKENS,
|
|
4
|
+
buildEvictionFields,
|
|
5
|
+
estimateTokens,
|
|
6
|
+
findCutPoint,
|
|
7
|
+
isProtected,
|
|
8
|
+
planEviction,
|
|
9
|
+
projectWorkingSet,
|
|
10
|
+
resolveWorkingSetPolicy,
|
|
11
|
+
selectVisibleEntries,
|
|
12
|
+
tokensFreedByEviction
|
|
13
|
+
} from "./chunk-WHJYKASB.js";
|
|
14
|
+
import {
|
|
15
|
+
parseSessionEntries
|
|
16
|
+
} from "./chunk-CJUB2JJ2.js";
|
|
17
|
+
import "./chunk-HVDIIIQW.js";
|
|
18
|
+
import {
|
|
19
|
+
EMPTY_WORKING_SET_VIEW,
|
|
20
|
+
buildPathIndex,
|
|
21
|
+
callPathsByToolCallId,
|
|
22
|
+
covers,
|
|
23
|
+
foldWorkingSet,
|
|
24
|
+
isTurnStart,
|
|
25
|
+
protectionCutoffIndex
|
|
26
|
+
} from "./chunk-TZSKNMZG.js";
|
|
27
|
+
import {
|
|
28
|
+
filterEntriesToActivePath
|
|
29
|
+
} from "./chunk-W6GROXXM.js";
|
|
30
|
+
import "./chunk-43AOLP7E.js";
|
|
31
|
+
import "./chunk-MV3K5QF2.js";
|
|
32
|
+
import "./chunk-UL3WSD3F.js";
|
|
33
|
+
import "./chunk-ECH6PKUQ.js";
|
|
34
|
+
import {
|
|
35
|
+
isSessionHeader
|
|
36
|
+
} from "./chunk-GOXNB3AO.js";
|
|
37
|
+
import "./chunk-HWUFFB6L.js";
|
|
38
|
+
import "./chunk-OZNBF4L3.js";
|
|
39
|
+
import "./chunk-CGKSTWHD.js";
|
|
40
|
+
import "./chunk-XAKHZX5N.js";
|
|
41
|
+
import "./chunk-33YXPOE3.js";
|
|
42
|
+
import "./chunk-CFGTUFWB.js";
|
|
43
|
+
import {
|
|
44
|
+
DEFAULT_WORKING_SET_SETTINGS
|
|
45
|
+
} from "./chunk-LL4KHSZI.js";
|
|
46
|
+
import "./chunk-GEYXPTRF.js";
|
|
47
|
+
import "./chunk-FO5ZOVUY.js";
|
|
48
|
+
import {
|
|
49
|
+
assertSafeId
|
|
50
|
+
} from "./chunk-R346GLFC.js";
|
|
51
|
+
import {
|
|
52
|
+
clioStatePath
|
|
53
|
+
} from "./chunk-BNAZZHFG.js";
|
|
54
|
+
import "./chunk-WEPFGWHJ.js";
|
|
55
|
+
import "./chunk-G7MUEIGA.js";
|
|
56
|
+
import "./chunk-7CR24IG7.js";
|
|
57
|
+
import "./chunk-IWHMRKLL.js";
|
|
58
|
+
import {
|
|
59
|
+
init_esm_shims
|
|
60
|
+
} from "./chunk-3R73A4XB.js";
|
|
61
|
+
|
|
62
|
+
// src/cli/context-working-set.ts
|
|
63
|
+
init_esm_shims();
|
|
64
|
+
import { execFileSync } from "node:child_process";
|
|
65
|
+
import { mkdir, readdir as readdir2, readFile as readFile2, stat as stat2, writeFile } from "node:fs/promises";
|
|
66
|
+
import { basename as basename2, dirname as dirname2, join as join2, resolve as resolve2 } from "node:path";
|
|
67
|
+
|
|
68
|
+
// src/domains/context/working-set/replay/controls.ts
|
|
69
|
+
init_esm_shims();
|
|
70
|
+
|
|
71
|
+
// src/domains/context/working-set/replay/trace.ts
|
|
72
|
+
init_esm_shims();
|
|
73
|
+
function countReplayTurns(entries) {
|
|
74
|
+
let count = 0;
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
if (isTurnStart(entry)) count += 1;
|
|
77
|
+
}
|
|
78
|
+
return count;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/domains/context/working-set/replay/controls.ts
|
|
82
|
+
function controlId(id) {
|
|
83
|
+
return id;
|
|
84
|
+
}
|
|
85
|
+
function eligibleToolResults(input) {
|
|
86
|
+
const cutoff = protectionCutoffIndex(input.entries, input.settings.protectLastTurns);
|
|
87
|
+
const index = buildPathIndex(input.entries, { cwd: input.cwd });
|
|
88
|
+
const out = [];
|
|
89
|
+
for (let entryIndex = cutoff - 1; entryIndex >= 0; entryIndex -= 1) {
|
|
90
|
+
const entry = input.entries[entryIndex];
|
|
91
|
+
if (entry?.kind !== "message" || entry.role !== "tool_result") continue;
|
|
92
|
+
if (input.view.evicted.has(entry.turnId)) continue;
|
|
93
|
+
if (isProtected(entry, { entryIndex, cutoffIndex: cutoff, input, index })) continue;
|
|
94
|
+
out.push(entry);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function takeToTarget(input, entries) {
|
|
99
|
+
let tokensNeeded = Math.max(0, input.pressure.tokens - input.pressure.target * input.pressure.contextWindow);
|
|
100
|
+
if (tokensNeeded <= 0) return [];
|
|
101
|
+
const selected = [];
|
|
102
|
+
const callPaths = callPathsByToolCallId(input.entries);
|
|
103
|
+
for (const entry of entries) {
|
|
104
|
+
const candidate = { ref: { entry: entry.turnId }, reason: "age_horizon" };
|
|
105
|
+
selected.push(candidate);
|
|
106
|
+
tokensNeeded -= tokensFreedByEviction(input.estimateTokens, entry, candidate, callPaths);
|
|
107
|
+
if (tokensNeeded <= 0) break;
|
|
108
|
+
}
|
|
109
|
+
return selected;
|
|
110
|
+
}
|
|
111
|
+
function makeOraclePolicy(graph) {
|
|
112
|
+
let lastInput = null;
|
|
113
|
+
let lastCandidateCount = 0;
|
|
114
|
+
const safeEntries = (input) => {
|
|
115
|
+
const currentTurn = countReplayTurns(input.entries) + 1;
|
|
116
|
+
return eligibleToolResults(input).filter((entry) => {
|
|
117
|
+
const futureTurns = graph.futureTurnsOf.get(entry.turnId) ?? [];
|
|
118
|
+
return futureTurns.every((turn) => turn < currentTurn);
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
id: controlId("oracle"),
|
|
123
|
+
select(input) {
|
|
124
|
+
const safe = safeEntries(input);
|
|
125
|
+
lastInput = input;
|
|
126
|
+
lastCandidateCount = safe.length;
|
|
127
|
+
return takeToTarget(input, safe);
|
|
128
|
+
},
|
|
129
|
+
replayCandidateCount(input) {
|
|
130
|
+
return input === lastInput ? lastCandidateCount : safeEntries(input).length;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function mulberry32(seed) {
|
|
135
|
+
let value = seed >>> 0;
|
|
136
|
+
return () => {
|
|
137
|
+
value = value + 1831565813 >>> 0;
|
|
138
|
+
let next = value;
|
|
139
|
+
next = Math.imul(next ^ next >>> 15, next | 1);
|
|
140
|
+
next ^= next + Math.imul(next ^ next >>> 7, next | 61);
|
|
141
|
+
return ((next ^ next >>> 14) >>> 0) / 4294967296;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function makeRandomPolicy(seed) {
|
|
145
|
+
let lastInput = null;
|
|
146
|
+
let lastCandidateCount = 0;
|
|
147
|
+
return {
|
|
148
|
+
id: controlId("random"),
|
|
149
|
+
select(input) {
|
|
150
|
+
const entries = [...eligibleToolResults(input)];
|
|
151
|
+
lastInput = input;
|
|
152
|
+
lastCandidateCount = entries.length;
|
|
153
|
+
const random = mulberry32(seed);
|
|
154
|
+
for (let index = entries.length - 1; index > 0; index -= 1) {
|
|
155
|
+
const swap = Math.floor(random() * (index + 1));
|
|
156
|
+
const value = entries[index];
|
|
157
|
+
entries[index] = entries[swap];
|
|
158
|
+
entries[swap] = value;
|
|
159
|
+
}
|
|
160
|
+
return takeToTarget(input, entries);
|
|
161
|
+
},
|
|
162
|
+
replayCandidateCount(input) {
|
|
163
|
+
return input === lastInput ? lastCandidateCount : eligibleToolResults(input).length;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
var nonePolicy = {
|
|
168
|
+
id: controlId("none"),
|
|
169
|
+
select: () => []
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// src/domains/context/working-set/replay/load-clio.ts
|
|
173
|
+
init_esm_shims();
|
|
174
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
175
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
176
|
+
|
|
177
|
+
// src/domains/context/working-set/replay/reference-graph.ts
|
|
178
|
+
init_esm_shims();
|
|
179
|
+
var READ_CLASS_OPS = /* @__PURE__ */ new Set(["read", "grep", "find", "ls", "code_nav"]);
|
|
180
|
+
var MUTATION_OPS = /* @__PURE__ */ new Set(["write", "edit"]);
|
|
181
|
+
function isToolResult(entry) {
|
|
182
|
+
return entry?.kind === "message" && entry.role === "tool_result";
|
|
183
|
+
}
|
|
184
|
+
function isReadableObservation(observation) {
|
|
185
|
+
return !observation.isError && observation.path.length > 0 && READ_CLASS_OPS.has(observation.op);
|
|
186
|
+
}
|
|
187
|
+
function edgeKey(edge) {
|
|
188
|
+
return `${edge.from}\0${edge.toTurnIndex}\0${edge.kind}`;
|
|
189
|
+
}
|
|
190
|
+
function buildReferenceGraph(trace, index) {
|
|
191
|
+
const entryById = new Map(trace.entries.map((entry) => [entry.turnId, entry]));
|
|
192
|
+
const edges = [];
|
|
193
|
+
const seenEdges = /* @__PURE__ */ new Set();
|
|
194
|
+
const future = /* @__PURE__ */ new Map();
|
|
195
|
+
const add = (edge) => {
|
|
196
|
+
const key = edgeKey(edge);
|
|
197
|
+
if (seenEdges.has(key)) return;
|
|
198
|
+
seenEdges.add(key);
|
|
199
|
+
edges.push(edge);
|
|
200
|
+
if (edge.kind === "file_rewrite") return;
|
|
201
|
+
const turns = future.get(edge.from);
|
|
202
|
+
if (turns === void 0) future.set(edge.from, /* @__PURE__ */ new Set([edge.toTurnIndex]));
|
|
203
|
+
else turns.add(edge.toTurnIndex);
|
|
204
|
+
};
|
|
205
|
+
for (const earlier of index.observations) {
|
|
206
|
+
if (!isToolResult(entryById.get(earlier.ref.entry)) || !isReadableObservation(earlier)) continue;
|
|
207
|
+
const surfaced = new Set(earlier.surfaced);
|
|
208
|
+
for (const later of index.observations) {
|
|
209
|
+
if (later.entryIndex <= earlier.entryIndex) continue;
|
|
210
|
+
if (isReadableObservation(later) && later.path === earlier.path) {
|
|
211
|
+
add({ from: earlier.ref.entry, toTurnIndex: later.turnIndex, kind: "file_reread" });
|
|
212
|
+
}
|
|
213
|
+
if (isReadableObservation(later) && surfaced.has(later.path)) {
|
|
214
|
+
add({ from: earlier.ref.entry, toTurnIndex: later.turnIndex, kind: "file_discovery" });
|
|
215
|
+
}
|
|
216
|
+
if (later.path === earlier.path && MUTATION_OPS.has(later.op)) {
|
|
217
|
+
add({ from: earlier.ref.entry, toTurnIndex: later.turnIndex, kind: "file_rewrite" });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const futureTurnsOf = /* @__PURE__ */ new Map();
|
|
222
|
+
for (const [ref, turns] of future)
|
|
223
|
+
futureTurnsOf.set(
|
|
224
|
+
ref,
|
|
225
|
+
[...turns].sort((a, b) => a - b)
|
|
226
|
+
);
|
|
227
|
+
return { edges, futureTurnsOf };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// src/domains/context/working-set/replay/load-clio.ts
|
|
231
|
+
var DEFAULT_FILTER = {
|
|
232
|
+
minTurns: 8,
|
|
233
|
+
minToolResults: 8,
|
|
234
|
+
requireFileReread: true
|
|
235
|
+
};
|
|
236
|
+
async function collectLedgerFiles(input, out) {
|
|
237
|
+
const path = resolve(input);
|
|
238
|
+
let facts;
|
|
239
|
+
try {
|
|
240
|
+
facts = await stat(path);
|
|
241
|
+
} catch {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (facts.isFile()) {
|
|
245
|
+
if (path.endsWith(".jsonl")) out.add(path);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (!facts.isDirectory()) return;
|
|
249
|
+
const direct = join(path, "current.jsonl");
|
|
250
|
+
try {
|
|
251
|
+
if ((await stat(direct)).isFile()) {
|
|
252
|
+
out.add(direct);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
} catch {
|
|
256
|
+
}
|
|
257
|
+
let children;
|
|
258
|
+
try {
|
|
259
|
+
children = await readdir(path, { withFileTypes: true });
|
|
260
|
+
} catch {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
for (const child of children.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
264
|
+
if (!child.isDirectory()) continue;
|
|
265
|
+
await collectLedgerFiles(join(path, child.name), out);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function sessionFacts(raw, source) {
|
|
269
|
+
const fallback = { id: basename(dirname(source)), cwd: null };
|
|
270
|
+
for (const line of raw.split("\n")) {
|
|
271
|
+
if (line.trim().length === 0) continue;
|
|
272
|
+
try {
|
|
273
|
+
const value = JSON.parse(line);
|
|
274
|
+
if (isSessionHeader(value)) return { id: value.id, cwd: value.cwd.length > 0 ? value.cwd : null };
|
|
275
|
+
} catch {
|
|
276
|
+
return fallback;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return fallback;
|
|
280
|
+
}
|
|
281
|
+
async function pinnedLeafTurnId(source) {
|
|
282
|
+
try {
|
|
283
|
+
const raw = await readFile(join(dirname(source), "meta.json"), "utf8");
|
|
284
|
+
const value = JSON.parse(raw);
|
|
285
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
286
|
+
const pinned = value.pinnedLeafTurnId;
|
|
287
|
+
return typeof pinned === "string" && pinned.length > 0 ? pinned : void 0;
|
|
288
|
+
} catch {
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function cleanActiveEntries(entries, activeLeafTurnId) {
|
|
293
|
+
return filterEntriesToActivePath(entries, activeLeafTurnId).filter(
|
|
294
|
+
(entry) => entry.kind !== "contextEviction" && entry.kind !== "contextRecall"
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function toolResultCount(entries) {
|
|
298
|
+
let count = 0;
|
|
299
|
+
for (const entry of entries) {
|
|
300
|
+
if (entry.kind === "message" && entry.role === "tool_result") count += 1;
|
|
301
|
+
}
|
|
302
|
+
return count;
|
|
303
|
+
}
|
|
304
|
+
async function loadClioTraces(paths, options = {}) {
|
|
305
|
+
const discovered = /* @__PURE__ */ new Set();
|
|
306
|
+
let missingInputs = 0;
|
|
307
|
+
for (const path of paths) {
|
|
308
|
+
const before = discovered.size;
|
|
309
|
+
await collectLedgerFiles(path, discovered);
|
|
310
|
+
if (discovered.size === before) {
|
|
311
|
+
try {
|
|
312
|
+
await stat(resolve(path));
|
|
313
|
+
} catch {
|
|
314
|
+
missingInputs += 1;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const filtered = {
|
|
319
|
+
turns_lt_8: 0,
|
|
320
|
+
tool_results_lt_8: 0,
|
|
321
|
+
no_file_reread: 0
|
|
322
|
+
};
|
|
323
|
+
const cascade = {
|
|
324
|
+
found: discovered.size,
|
|
325
|
+
unreadable: missingInputs,
|
|
326
|
+
filtered,
|
|
327
|
+
kept: 0
|
|
328
|
+
};
|
|
329
|
+
const traces = [];
|
|
330
|
+
const filter = options.filter === false ? null : {
|
|
331
|
+
minTurns: options.filter?.minTurns ?? DEFAULT_FILTER.minTurns,
|
|
332
|
+
minToolResults: options.filter?.minToolResults ?? DEFAULT_FILTER.minToolResults,
|
|
333
|
+
requireFileReread: options.filter?.requireFileReread ?? DEFAULT_FILTER.requireFileReread
|
|
334
|
+
};
|
|
335
|
+
for (const source of [...discovered].sort((a, b) => a.localeCompare(b))) {
|
|
336
|
+
let raw;
|
|
337
|
+
try {
|
|
338
|
+
raw = await readFile(source, "utf8");
|
|
339
|
+
} catch {
|
|
340
|
+
cascade.unreadable += 1;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const parsed = parseSessionEntries(raw, source);
|
|
344
|
+
if (parsed.errors.length > 0) {
|
|
345
|
+
cascade.unreadable += 1;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const entries = cleanActiveEntries(parsed.entries, await pinnedLeafTurnId(source));
|
|
349
|
+
const facts = sessionFacts(raw, source);
|
|
350
|
+
const trace = {
|
|
351
|
+
id: facts.id,
|
|
352
|
+
source,
|
|
353
|
+
cwd: facts.cwd,
|
|
354
|
+
entries,
|
|
355
|
+
turnCount: countReplayTurns(entries)
|
|
356
|
+
};
|
|
357
|
+
if (filter !== null) {
|
|
358
|
+
if (trace.turnCount < filter.minTurns) {
|
|
359
|
+
filtered.turns_lt_8 = (filtered.turns_lt_8 ?? 0) + 1;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (toolResultCount(entries) < filter.minToolResults) {
|
|
363
|
+
filtered.tool_results_lt_8 = (filtered.tool_results_lt_8 ?? 0) + 1;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (filter.requireFileReread) {
|
|
367
|
+
const graph = buildReferenceGraph(trace, buildPathIndex(entries, { cwd: trace.cwd }));
|
|
368
|
+
if (!graph.edges.some((edge) => edge.kind === "file_reread")) {
|
|
369
|
+
filtered.no_file_reread = (filtered.no_file_reread ?? 0) + 1;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
traces.push(trace);
|
|
375
|
+
}
|
|
376
|
+
cascade.kept = traces.length;
|
|
377
|
+
return { traces, cascade };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/domains/context/working-set/replay/metrics.ts
|
|
381
|
+
init_esm_shims();
|
|
382
|
+
function safeFraction(numerator, denominator, empty) {
|
|
383
|
+
return denominator === 0 ? empty : numerator / denominator;
|
|
384
|
+
}
|
|
385
|
+
function survivesThrough(observation, referenceTurn, evictedAtTurn) {
|
|
386
|
+
const evictedAt = evictedAtTurn.get(observation.ref.entry);
|
|
387
|
+
return evictedAt === void 0 || evictedAt > referenceTurn;
|
|
388
|
+
}
|
|
389
|
+
function hasSurvivingCoveringRead(input, ref, referenceTurn) {
|
|
390
|
+
const original = input.index.byRef.get(ref);
|
|
391
|
+
if (original?.op !== "read" || original.path.length === 0) return false;
|
|
392
|
+
return (input.index.byPath.get(original.path) ?? []).some(
|
|
393
|
+
(later) => later.op === "read" && !later.isError && later.entryIndex > original.entryIndex && later.turnIndex < referenceTurn && covers(later.range, original.range) && survivesThrough(later, referenceTurn, input.replay.evictedAtTurn)
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
function measure(input) {
|
|
397
|
+
let pairs = 0;
|
|
398
|
+
let retainedPairs = 0;
|
|
399
|
+
let coveredRetainedPairs = 0;
|
|
400
|
+
let pairsAt10 = 0;
|
|
401
|
+
let retainedPairsAt10 = 0;
|
|
402
|
+
for (const [ref, futureTurns] of input.graph.futureTurnsOf) {
|
|
403
|
+
const observationTurn = input.index.byRef.get(ref)?.turnIndex;
|
|
404
|
+
const evictedAt = input.replay.evictedAtTurn.get(ref);
|
|
405
|
+
for (const referenceTurn of futureTurns) {
|
|
406
|
+
pairs += 1;
|
|
407
|
+
const retained = evictedAt === void 0 || evictedAt > referenceTurn;
|
|
408
|
+
if (retained) retainedPairs += 1;
|
|
409
|
+
if (retained || hasSurvivingCoveringRead(input, ref, referenceTurn)) coveredRetainedPairs += 1;
|
|
410
|
+
if (observationTurn !== void 0 && referenceTurn - observationTurn <= 10) {
|
|
411
|
+
pairsAt10 += 1;
|
|
412
|
+
if (retained) retainedPairsAt10 += 1;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
let evictedItems = 0;
|
|
417
|
+
let safelyEvictedItems = 0;
|
|
418
|
+
let tokensEvicted = 0;
|
|
419
|
+
let recallTokens = 0;
|
|
420
|
+
let coldPrefixTokens2 = 0;
|
|
421
|
+
let saturatedEventCount = 0;
|
|
422
|
+
for (const event of input.replay.events) {
|
|
423
|
+
if (event.saturated) saturatedEventCount += 1;
|
|
424
|
+
coldPrefixTokens2 += event.coldPrefixTokens;
|
|
425
|
+
for (const item of event.items) {
|
|
426
|
+
evictedItems += 1;
|
|
427
|
+
tokensEvicted += item.tokensFreed;
|
|
428
|
+
const future = input.graph.futureTurnsOf.get(item.ref.entry) ?? [];
|
|
429
|
+
if (!future.some((turn) => turn > event.turnIndex)) safelyEvictedItems += 1;
|
|
430
|
+
else recallTokens += item.tokensFreed;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
metrics: {
|
|
435
|
+
traces: 1,
|
|
436
|
+
retention: safeFraction(retainedPairs, pairs, 1),
|
|
437
|
+
retentionCovered: safeFraction(coveredRetainedPairs, pairs, 1),
|
|
438
|
+
retentionAt10: safeFraction(retainedPairsAt10, pairsAt10, 1),
|
|
439
|
+
evictionPrecision: safeFraction(safelyEvictedItems, evictedItems, 1),
|
|
440
|
+
tokensEvicted,
|
|
441
|
+
recallTokens,
|
|
442
|
+
coldPrefixTokens: coldPrefixTokens2,
|
|
443
|
+
evictionEvents: input.replay.events.length,
|
|
444
|
+
saturatedEvents: safeFraction(saturatedEventCount, input.replay.events.length, 0),
|
|
445
|
+
turnsToFirstSummary: input.replay.turnsToFirstSummary,
|
|
446
|
+
summaries: input.replay.summaries
|
|
447
|
+
},
|
|
448
|
+
pairs,
|
|
449
|
+
retainedPairs,
|
|
450
|
+
coveredRetainedPairs,
|
|
451
|
+
pairsAt10,
|
|
452
|
+
retainedPairsAt10,
|
|
453
|
+
saturatedEventCount
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
function mean(values) {
|
|
457
|
+
return values.length === 0 ? 0 : values.reduce((sum, value) => sum + value, 0) / values.length;
|
|
458
|
+
}
|
|
459
|
+
function aggregateReplayMetrics(inputs) {
|
|
460
|
+
const measured = inputs.map(measure);
|
|
461
|
+
const firstSummaries = measured.map((entry) => entry.metrics.turnsToFirstSummary).filter((value) => value !== null);
|
|
462
|
+
const sum = (field) => measured.reduce((total, entry) => total + entry[field], 0);
|
|
463
|
+
const totalEvents = measured.reduce((total, entry) => total + entry.metrics.evictionEvents, 0);
|
|
464
|
+
const saturatedEvents = measured.reduce((total, entry) => total + entry.saturatedEventCount, 0);
|
|
465
|
+
return {
|
|
466
|
+
mean: {
|
|
467
|
+
traces: measured.length,
|
|
468
|
+
retention: measured.length === 0 ? 1 : mean(measured.map((entry) => entry.metrics.retention)),
|
|
469
|
+
retentionCovered: measured.length === 0 ? 1 : mean(measured.map((entry) => entry.metrics.retentionCovered)),
|
|
470
|
+
retentionAt10: measured.length === 0 ? 1 : mean(measured.map((entry) => entry.metrics.retentionAt10)),
|
|
471
|
+
evictionPrecision: measured.length === 0 ? 1 : mean(measured.map((entry) => entry.metrics.evictionPrecision)),
|
|
472
|
+
tokensEvicted: mean(measured.map((entry) => entry.metrics.tokensEvicted)),
|
|
473
|
+
recallTokens: mean(measured.map((entry) => entry.metrics.recallTokens)),
|
|
474
|
+
coldPrefixTokens: mean(measured.map((entry) => entry.metrics.coldPrefixTokens)),
|
|
475
|
+
evictionEvents: mean(measured.map((entry) => entry.metrics.evictionEvents)),
|
|
476
|
+
// Event-pooled: zero-event traces must not dilute the saturation rate.
|
|
477
|
+
saturatedEvents: safeFraction(saturatedEvents, totalEvents, 0),
|
|
478
|
+
turnsToFirstSummary: firstSummaries.length === 0 ? null : mean(firstSummaries),
|
|
479
|
+
summaries: mean(measured.map((entry) => entry.metrics.summaries))
|
|
480
|
+
},
|
|
481
|
+
turnsToFirstSummaryCount: firstSummaries.length,
|
|
482
|
+
pooledRetention: safeFraction(sum("retainedPairs"), sum("pairs"), 1),
|
|
483
|
+
pooledRetentionCovered: safeFraction(sum("coveredRetainedPairs"), sum("pairs"), 1),
|
|
484
|
+
pooledRetentionAt10: safeFraction(sum("retainedPairsAt10"), sum("pairsAt10"), 1)
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/domains/context/working-set/replay/report.ts
|
|
489
|
+
init_esm_shims();
|
|
490
|
+
function metricObject(metrics, turnsToFirstSummaryCount) {
|
|
491
|
+
return {
|
|
492
|
+
traces: metrics.traces,
|
|
493
|
+
retention: metrics.retention,
|
|
494
|
+
retentionCovered: metrics.retentionCovered,
|
|
495
|
+
retentionAt10: metrics.retentionAt10,
|
|
496
|
+
evictionPrecision: metrics.evictionPrecision,
|
|
497
|
+
tokensEvicted: metrics.tokensEvicted,
|
|
498
|
+
recallTokens: metrics.recallTokens,
|
|
499
|
+
coldPrefixTokens: metrics.coldPrefixTokens,
|
|
500
|
+
evictionEvents: metrics.evictionEvents,
|
|
501
|
+
saturatedEvents: metrics.saturatedEvents,
|
|
502
|
+
turnsToFirstSummary: metrics.turnsToFirstSummary,
|
|
503
|
+
turnsToFirstSummaryCount,
|
|
504
|
+
summaries: metrics.summaries
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function renderReplayJson(input) {
|
|
508
|
+
const filtered = Object.fromEntries(Object.entries(input.cascade.filtered).sort(([a], [b]) => a.localeCompare(b)));
|
|
509
|
+
const artifact = {
|
|
510
|
+
schema: "clio-context-replay-v2",
|
|
511
|
+
config: {
|
|
512
|
+
policies: [...input.config.policies],
|
|
513
|
+
budgets: [...input.config.budgets],
|
|
514
|
+
threshold: input.config.threshold,
|
|
515
|
+
target: input.config.target,
|
|
516
|
+
seed: input.config.seed,
|
|
517
|
+
corpus: [...input.config.corpus],
|
|
518
|
+
filter: input.config.filter,
|
|
519
|
+
settings: {
|
|
520
|
+
enabled: input.config.settings.enabled,
|
|
521
|
+
policy: input.config.settings.policy,
|
|
522
|
+
target: input.config.settings.target,
|
|
523
|
+
protectLastTurns: input.config.settings.protectLastTurns,
|
|
524
|
+
minEvictableTokens: input.config.settings.minEvictableTokens
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
provenance: {
|
|
528
|
+
gitSha: input.gitSha,
|
|
529
|
+
commandLine: [...input.commandLine]
|
|
530
|
+
},
|
|
531
|
+
cascade: {
|
|
532
|
+
found: input.cascade.found,
|
|
533
|
+
unreadable: input.cascade.unreadable,
|
|
534
|
+
filtered,
|
|
535
|
+
kept: input.cascade.kept
|
|
536
|
+
},
|
|
537
|
+
results: input.results.map((result) => ({
|
|
538
|
+
budgetTokens: result.budgetTokens,
|
|
539
|
+
policyId: result.policyId,
|
|
540
|
+
metrics: {
|
|
541
|
+
mean: metricObject(result.metrics.mean, result.metrics.turnsToFirstSummaryCount),
|
|
542
|
+
pooledRetention: result.metrics.pooledRetention,
|
|
543
|
+
pooledRetentionCovered: result.metrics.pooledRetentionCovered,
|
|
544
|
+
pooledRetentionAt10: result.metrics.pooledRetentionAt10
|
|
545
|
+
}
|
|
546
|
+
}))
|
|
547
|
+
};
|
|
548
|
+
return `${JSON.stringify(artifact, null, " ")}
|
|
549
|
+
`;
|
|
550
|
+
}
|
|
551
|
+
function ratio(value) {
|
|
552
|
+
return value.toFixed(3);
|
|
553
|
+
}
|
|
554
|
+
function quantity(value) {
|
|
555
|
+
return Number.isInteger(value) ? String(value) : value.toFixed(1);
|
|
556
|
+
}
|
|
557
|
+
function cascadeRows(cascade) {
|
|
558
|
+
return [
|
|
559
|
+
`| found | ${cascade.found} |`,
|
|
560
|
+
`| unreadable | ${cascade.unreadable} |`,
|
|
561
|
+
...Object.entries(cascade.filtered).map(([stage, count]) => `| ${stage} | ${count} |`),
|
|
562
|
+
`| kept | ${cascade.kept} |`
|
|
563
|
+
];
|
|
564
|
+
}
|
|
565
|
+
function renderReplayMarkdown(input) {
|
|
566
|
+
const lines = [
|
|
567
|
+
"# Clio working-set replay",
|
|
568
|
+
"",
|
|
569
|
+
"## Inclusion cascade",
|
|
570
|
+
"",
|
|
571
|
+
"| stage | traces |",
|
|
572
|
+
"| --- | ---: |",
|
|
573
|
+
...cascadeRows(input.cascade)
|
|
574
|
+
];
|
|
575
|
+
for (const budget of input.config.budgets) {
|
|
576
|
+
lines.push(
|
|
577
|
+
"",
|
|
578
|
+
`## Budget ${budget}`,
|
|
579
|
+
"",
|
|
580
|
+
"| policy | n | retention (mean) | retention (pooled) | retention covered (mean) | retention covered (pooled) | retention@10 (mean) | eviction precision (mean) | tokens evicted (mean) | recall tokens (mean) | cold prefix tokens (mean) | eviction events (mean) | saturated events | turns to first summary (mean) | summaries (mean) |",
|
|
581
|
+
"| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |"
|
|
582
|
+
);
|
|
583
|
+
for (const policy of input.config.policies) {
|
|
584
|
+
const result = input.results.find((entry) => entry.budgetTokens === budget && entry.policyId === policy);
|
|
585
|
+
if (result === void 0) continue;
|
|
586
|
+
const metrics = result.metrics.mean;
|
|
587
|
+
lines.push(
|
|
588
|
+
`| ${policy} | ${metrics.traces} | ${ratio(metrics.retention)} | ${ratio(result.metrics.pooledRetention)} | ${ratio(metrics.retentionCovered)} | ${ratio(result.metrics.pooledRetentionCovered)} | ${ratio(metrics.retentionAt10)} | ${ratio(metrics.evictionPrecision)} | ${quantity(metrics.tokensEvicted)} | ${quantity(metrics.recallTokens)} | ${quantity(metrics.coldPrefixTokens)} | ${quantity(metrics.evictionEvents)} | ${ratio(metrics.saturatedEvents)} | ${metrics.turnsToFirstSummary === null ? "\u2014" : quantity(metrics.turnsToFirstSummary)} (n=${result.metrics.turnsToFirstSummaryCount}) | ${quantity(metrics.summaries)} |`
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
lines.push("");
|
|
593
|
+
return lines.join("\n");
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/domains/context/working-set/replay/runner.ts
|
|
597
|
+
init_esm_shims();
|
|
598
|
+
function sumTokens(entries) {
|
|
599
|
+
let tokens = 0;
|
|
600
|
+
for (const entry of entries) tokens += estimateTokens(entry);
|
|
601
|
+
return tokens;
|
|
602
|
+
}
|
|
603
|
+
function hasCandidatePool(policy) {
|
|
604
|
+
return "replayCandidateCount" in policy && typeof policy.replayCandidateCount === "function";
|
|
605
|
+
}
|
|
606
|
+
function latestCompactionIndex(entries) {
|
|
607
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
608
|
+
if (entries[index]?.kind === "compactionSummary") return index;
|
|
609
|
+
}
|
|
610
|
+
return -1;
|
|
611
|
+
}
|
|
612
|
+
function eventSaturated(policy, input, plan) {
|
|
613
|
+
if (policy.id === "age-horizon") return true;
|
|
614
|
+
const targetTokens = input.pressure.target * input.pressure.contextWindow;
|
|
615
|
+
if (policy.id === "structural-v1") {
|
|
616
|
+
const thresholdTokens = input.pressure.threshold * input.pressure.contextWindow;
|
|
617
|
+
const usedAgeRung = plan.items.some((item) => item.reason === "age_horizon");
|
|
618
|
+
return plan.tokensAfter > targetTokens && (plan.tokensAfter > thresholdTokens || usedAgeRung);
|
|
619
|
+
}
|
|
620
|
+
if (hasCandidatePool(policy)) return plan.items.length === policy.replayCandidateCount(input);
|
|
621
|
+
return plan.tokensAfter > targetTokens;
|
|
622
|
+
}
|
|
623
|
+
function rebuildProjection(soFar, leaf, view) {
|
|
624
|
+
const raw = selectVisibleEntries(soFar, leaf ?? void 0);
|
|
625
|
+
const projected = projectWorkingSet(raw, view);
|
|
626
|
+
return {
|
|
627
|
+
raw,
|
|
628
|
+
projected,
|
|
629
|
+
indexByTurnId: new Map(raw.map((entry, index) => [entry.turnId, index])),
|
|
630
|
+
tokens: sumTokens(projected)
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
function projectAppendedEntry(entry, state, view) {
|
|
634
|
+
if (view.evictionEvents === 0) return entry;
|
|
635
|
+
const lastEventId = view.lastEvictionTurnId;
|
|
636
|
+
if (lastEventId !== null && state.indexByTurnId.has(lastEventId)) {
|
|
637
|
+
return entry;
|
|
638
|
+
}
|
|
639
|
+
return projectWorkingSet([entry], view)[0] ?? entry;
|
|
640
|
+
}
|
|
641
|
+
function appendVisibleEntry(entry, state, view) {
|
|
642
|
+
const projected = projectAppendedEntry(entry, state, view);
|
|
643
|
+
state.indexByTurnId.set(entry.turnId, state.raw.length);
|
|
644
|
+
state.raw.push(entry);
|
|
645
|
+
state.projected.push(projected);
|
|
646
|
+
state.tokens += estimateTokens(projected);
|
|
647
|
+
}
|
|
648
|
+
function applyEvictionProjection(state, synthetic, view, items) {
|
|
649
|
+
const affected = /* @__PURE__ */ new Set();
|
|
650
|
+
for (const item of items) {
|
|
651
|
+
const index = state.indexByTurnId.get(item.ref.entry);
|
|
652
|
+
if (index !== void 0) affected.add(index);
|
|
653
|
+
}
|
|
654
|
+
for (let index = 0; index < state.raw.length - 1; index += 1) {
|
|
655
|
+
const entry = state.raw[index];
|
|
656
|
+
if (entry?.kind === "message" && entry.role === "assistant") affected.add(index);
|
|
657
|
+
}
|
|
658
|
+
for (const index of affected) {
|
|
659
|
+
const source = state.raw[index];
|
|
660
|
+
const before = state.projected[index];
|
|
661
|
+
if (source === void 0 || before === void 0) continue;
|
|
662
|
+
const after = projectWorkingSet([source, synthetic], view)[0] ?? source;
|
|
663
|
+
state.projected[index] = after;
|
|
664
|
+
state.tokens += estimateTokens(after) - estimateTokens(before);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
function coldPrefixTokens(state, items) {
|
|
668
|
+
let cut = state.projected.length;
|
|
669
|
+
for (const item of items) {
|
|
670
|
+
const index = state.indexByTurnId.get(item.ref.entry);
|
|
671
|
+
if (index !== void 0 && index < cut) cut = index;
|
|
672
|
+
}
|
|
673
|
+
let tokens = 0;
|
|
674
|
+
for (let index = cut; index < state.projected.length; index += 1) {
|
|
675
|
+
const entry = state.projected[index];
|
|
676
|
+
if (entry !== void 0) tokens += estimateTokens(entry);
|
|
677
|
+
}
|
|
678
|
+
return tokens;
|
|
679
|
+
}
|
|
680
|
+
function replayTrace(trace, policy, config) {
|
|
681
|
+
const soFar = [];
|
|
682
|
+
const events = [];
|
|
683
|
+
const evictedAtTurn = /* @__PURE__ */ new Map();
|
|
684
|
+
const toolResults = new Set(
|
|
685
|
+
trace.entries.filter((entry) => entry.kind === "message" && entry.role === "tool_result").map((entry) => entry.turnId)
|
|
686
|
+
);
|
|
687
|
+
let evictionSequence = 0;
|
|
688
|
+
let summaries = 0;
|
|
689
|
+
let turnIndex = 0;
|
|
690
|
+
let turnsToFirstSummary = null;
|
|
691
|
+
let lastMessageTurnId = null;
|
|
692
|
+
let view = EMPTY_WORKING_SET_VIEW;
|
|
693
|
+
let visible = { raw: [], projected: [], indexByTurnId: /* @__PURE__ */ new Map(), tokens: 0 };
|
|
694
|
+
const pressureLimit = config.threshold * config.budgetTokens;
|
|
695
|
+
for (const entry of trace.entries) {
|
|
696
|
+
if (isTurnStart(entry)) {
|
|
697
|
+
turnIndex += 1;
|
|
698
|
+
const leaf = lastMessageTurnId;
|
|
699
|
+
const tokens = visible.tokens;
|
|
700
|
+
if (tokens > pressureLimit) {
|
|
701
|
+
const input = {
|
|
702
|
+
entries: visible.raw,
|
|
703
|
+
view,
|
|
704
|
+
cwd: trace.cwd,
|
|
705
|
+
settings: config.settings,
|
|
706
|
+
pressure: {
|
|
707
|
+
tokens,
|
|
708
|
+
contextWindow: config.budgetTokens,
|
|
709
|
+
threshold: config.threshold,
|
|
710
|
+
target: config.target
|
|
711
|
+
},
|
|
712
|
+
estimateTokens
|
|
713
|
+
};
|
|
714
|
+
const plan = planEviction(policy, input);
|
|
715
|
+
if (plan !== null) {
|
|
716
|
+
const saturated = eventSaturated(policy, input, plan);
|
|
717
|
+
evictionSequence += 1;
|
|
718
|
+
const previous = soFar[soFar.length - 1];
|
|
719
|
+
const synthetic = {
|
|
720
|
+
...buildEvictionFields(plan, {
|
|
721
|
+
trigger: "pressure",
|
|
722
|
+
pressureBefore: tokens / config.budgetTokens,
|
|
723
|
+
snapshotIdBefore: null
|
|
724
|
+
}),
|
|
725
|
+
turnId: `replay-evict-${evictionSequence}`,
|
|
726
|
+
parentTurnId: leaf,
|
|
727
|
+
timestamp: previous?.timestamp ?? entry.timestamp
|
|
728
|
+
};
|
|
729
|
+
soFar.push(synthetic);
|
|
730
|
+
appendVisibleEntry(synthetic, visible, view);
|
|
731
|
+
view = foldWorkingSet(soFar, leaf ?? void 0);
|
|
732
|
+
applyEvictionProjection(visible, synthetic, view, plan.items);
|
|
733
|
+
events.push({
|
|
734
|
+
turnIndex,
|
|
735
|
+
items: plan.items,
|
|
736
|
+
tokensBefore: plan.tokensBefore,
|
|
737
|
+
tokensAfter: plan.tokensAfter,
|
|
738
|
+
saturated,
|
|
739
|
+
coldPrefixTokens: coldPrefixTokens(visible, plan.items)
|
|
740
|
+
});
|
|
741
|
+
for (const item of plan.items) {
|
|
742
|
+
if (toolResults.has(item.ref.entry) && !evictedAtTurn.has(item.ref.entry)) {
|
|
743
|
+
evictedAtTurn.set(item.ref.entry, turnIndex);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (visible.tokens > pressureLimit) {
|
|
748
|
+
if (turnsToFirstSummary === null) turnsToFirstSummary = turnIndex;
|
|
749
|
+
const previousCompactionIndex = latestCompactionIndex(soFar);
|
|
750
|
+
const boundaryStart = previousCompactionIndex + 1;
|
|
751
|
+
const cut = config.summaries === void 0 ? boundaryStart : findCutPoint(soFar, config.summaries.keepRecentTokens, { startIndex: boundaryStart }).firstKeptEntryIndex;
|
|
752
|
+
if (config.summaries !== void 0 && cut > boundaryStart) {
|
|
753
|
+
summaries += 1;
|
|
754
|
+
const visibleBefore = new Set(visible.raw.map((candidate) => candidate.turnId));
|
|
755
|
+
const previous = soFar[soFar.length - 1];
|
|
756
|
+
const firstKeptTurnId = soFar[cut]?.turnId ?? "";
|
|
757
|
+
const synthetic = {
|
|
758
|
+
kind: "compactionSummary",
|
|
759
|
+
turnId: `replay-summary-${summaries}`,
|
|
760
|
+
parentTurnId: firstKeptTurnId || null,
|
|
761
|
+
timestamp: previous?.timestamp ?? entry.timestamp,
|
|
762
|
+
summary: "#".repeat(config.summaries.summaryTokens * 4),
|
|
763
|
+
tokensBefore: visible.tokens,
|
|
764
|
+
firstKeptTurnId
|
|
765
|
+
};
|
|
766
|
+
soFar.push(synthetic);
|
|
767
|
+
visible = rebuildProjection(soFar, lastMessageTurnId, view);
|
|
768
|
+
const visibleAfter = new Set(visible.raw.map((candidate) => candidate.turnId));
|
|
769
|
+
for (const removed of visibleBefore) {
|
|
770
|
+
if (toolResults.has(removed) && !visibleAfter.has(removed) && !evictedAtTurn.has(removed)) {
|
|
771
|
+
evictedAtTurn.set(removed, turnIndex);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
soFar.push(entry);
|
|
779
|
+
if (entry.kind === "compactionSummary") visible = rebuildProjection(soFar, lastMessageTurnId, view);
|
|
780
|
+
else appendVisibleEntry(entry, visible, view);
|
|
781
|
+
if (entry.kind === "message") lastMessageTurnId = entry.turnId;
|
|
782
|
+
}
|
|
783
|
+
return {
|
|
784
|
+
traceId: trace.id,
|
|
785
|
+
policyId: config.policyId,
|
|
786
|
+
budgetTokens: config.budgetTokens,
|
|
787
|
+
turnCount: trace.turnCount,
|
|
788
|
+
events,
|
|
789
|
+
evictedAtTurn,
|
|
790
|
+
turnsToFirstSummary,
|
|
791
|
+
summaries,
|
|
792
|
+
entries: soFar
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// src/domains/context/working-set/replay/synthetic.ts
|
|
797
|
+
init_esm_shims();
|
|
798
|
+
var SYNTHETIC_CORPORA = [
|
|
799
|
+
{
|
|
800
|
+
id: "science-long",
|
|
801
|
+
seed: 7,
|
|
802
|
+
traces: 8,
|
|
803
|
+
turns: 300,
|
|
804
|
+
files: 48,
|
|
805
|
+
returnProbability: 0.3,
|
|
806
|
+
zipfExponent: 1,
|
|
807
|
+
scripts: { explore: 1, implement: 3, validate: 2, analyze: 2 },
|
|
808
|
+
fileLines: [12, 96],
|
|
809
|
+
simulationLines: [100, 2400],
|
|
810
|
+
offloadAfterLines: 200
|
|
811
|
+
},
|
|
812
|
+
{
|
|
813
|
+
id: "refactor",
|
|
814
|
+
seed: 11,
|
|
815
|
+
traces: 8,
|
|
816
|
+
turns: 200,
|
|
817
|
+
files: 64,
|
|
818
|
+
returnProbability: 0.4,
|
|
819
|
+
zipfExponent: 1,
|
|
820
|
+
scripts: { explore: 1, implement: 5, validate: 2, analyze: 0 },
|
|
821
|
+
fileLines: [16, 110],
|
|
822
|
+
simulationLines: [100, 400],
|
|
823
|
+
offloadAfterLines: 200
|
|
824
|
+
},
|
|
825
|
+
{
|
|
826
|
+
id: "exploration",
|
|
827
|
+
seed: 13,
|
|
828
|
+
traces: 8,
|
|
829
|
+
turns: 200,
|
|
830
|
+
files: 120,
|
|
831
|
+
returnProbability: 0.15,
|
|
832
|
+
zipfExponent: 1,
|
|
833
|
+
scripts: { explore: 4, implement: 1, validate: 1, analyze: 1 },
|
|
834
|
+
fileLines: [10, 64],
|
|
835
|
+
simulationLines: [100, 800],
|
|
836
|
+
offloadAfterLines: 200
|
|
837
|
+
}
|
|
838
|
+
];
|
|
839
|
+
function syntheticCorpus(id) {
|
|
840
|
+
return SYNTHETIC_CORPORA.find((spec) => spec.id === id);
|
|
841
|
+
}
|
|
842
|
+
function mulberry322(seed) {
|
|
843
|
+
let value = seed >>> 0;
|
|
844
|
+
return () => {
|
|
845
|
+
value = value + 1831565813 >>> 0;
|
|
846
|
+
let next = value;
|
|
847
|
+
next = Math.imul(next ^ next >>> 15, next | 1);
|
|
848
|
+
next ^= next + Math.imul(next ^ next >>> 7, next | 61);
|
|
849
|
+
return ((next ^ next >>> 14) >>> 0) / 4294967296;
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
function powerLawRank(size, exponent, draw) {
|
|
853
|
+
const upper = size + 1;
|
|
854
|
+
const sample = exponent === 1 ? upper ** draw : (1 + draw * (upper ** (1 - exponent) - 1)) ** (1 / (1 - exponent));
|
|
855
|
+
return Math.min(size - 1, Math.max(0, Math.floor(sample) - 1));
|
|
856
|
+
}
|
|
857
|
+
var DIRECTORIES = ["src/sim", "src/io", "src/analysis", "src/mesh", "tests", "scripts", "configs"];
|
|
858
|
+
var STEMS = ["solver", "reader", "writer", "grid", "field", "boundary", "stepper", "metrics", "plot", "checkpoint"];
|
|
859
|
+
var EXTENSIONS = ["py", "py", "py", "c", "h", "yaml", "toml"];
|
|
860
|
+
var CONSUMED_LISTING_READ_LIMIT = 48;
|
|
861
|
+
var CONSUMED_LISTING_SAMPLE_LINES = 20;
|
|
862
|
+
var Ledger = class {
|
|
863
|
+
constructor(cwd) {
|
|
864
|
+
this.cwd = cwd;
|
|
865
|
+
}
|
|
866
|
+
cwd;
|
|
867
|
+
entries = [];
|
|
868
|
+
parent = null;
|
|
869
|
+
sequence = 0;
|
|
870
|
+
calls = 0;
|
|
871
|
+
next(kind) {
|
|
872
|
+
this.sequence += 1;
|
|
873
|
+
const turnId = `${kind}-${String(this.sequence).padStart(5, "0")}`;
|
|
874
|
+
const stamp = {
|
|
875
|
+
turnId,
|
|
876
|
+
parentTurnId: this.parent,
|
|
877
|
+
timestamp: new Date(1787e9 + this.sequence * 1e3).toISOString()
|
|
878
|
+
};
|
|
879
|
+
this.parent = turnId;
|
|
880
|
+
return stamp;
|
|
881
|
+
}
|
|
882
|
+
user(text) {
|
|
883
|
+
this.entries.push({ kind: "message", ...this.next("user"), role: "user", payload: { text } });
|
|
884
|
+
}
|
|
885
|
+
assistant(thinking, text) {
|
|
886
|
+
this.entries.push({
|
|
887
|
+
kind: "message",
|
|
888
|
+
...this.next("assistant"),
|
|
889
|
+
role: "assistant",
|
|
890
|
+
payload: {
|
|
891
|
+
content: [
|
|
892
|
+
{ type: "thinking", thinking },
|
|
893
|
+
{ type: "text", text }
|
|
894
|
+
]
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
call(name, args, text, options = {}) {
|
|
899
|
+
this.calls += 1;
|
|
900
|
+
const toolCallId = `call-${String(this.calls).padStart(5, "0")}`;
|
|
901
|
+
this.entries.push({ kind: "message", ...this.next("call"), role: "tool_call", payload: { toolCallId, name, args } });
|
|
902
|
+
const details = options.offload === true ? { resultDisposition: { offloadPath: `${this.cwd}/.clio-state/scratch/${toolCallId}.txt` } } : void 0;
|
|
903
|
+
this.entries.push({
|
|
904
|
+
kind: "message",
|
|
905
|
+
...this.next("result"),
|
|
906
|
+
role: "tool_result",
|
|
907
|
+
payload: {
|
|
908
|
+
toolCallId,
|
|
909
|
+
toolName: name,
|
|
910
|
+
result: { content: [{ type: "text", text }], ...details === void 0 ? {} : { details } },
|
|
911
|
+
isError: options.isError === true
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
function fileBody(file, offset = 0, limit = null) {
|
|
917
|
+
const end = limit === null ? file.lines : Math.min(file.lines, offset + limit);
|
|
918
|
+
const lines = [];
|
|
919
|
+
for (let line = offset + 1; line <= end; line += 1) {
|
|
920
|
+
lines.push(`${String(line).padStart(4, " ")} ${file.path}@v${file.version}: x[${line}] = f(x[${line - 1}]) + eps`);
|
|
921
|
+
}
|
|
922
|
+
return lines.join("\n");
|
|
923
|
+
}
|
|
924
|
+
function simulationOutput(lines, seed) {
|
|
925
|
+
const rnd = mulberry322(seed);
|
|
926
|
+
const out = [];
|
|
927
|
+
for (let step = 0; step < lines; step += 1) {
|
|
928
|
+
out.push(
|
|
929
|
+
`step ${String(step).padStart(5, "0")} t=${(step * 0.01).toFixed(2)} e=${(1 - rnd() * 1e-3).toFixed(6)} r=${(rnd() * 1e-6).toExponential(2)}`
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
return out.join("\n");
|
|
933
|
+
}
|
|
934
|
+
function testOutput(files, failing) {
|
|
935
|
+
const out = [
|
|
936
|
+
"============================= test session starts =============================",
|
|
937
|
+
`collected ${files.length * 4} items`,
|
|
938
|
+
""
|
|
939
|
+
];
|
|
940
|
+
for (const file of files) {
|
|
941
|
+
for (let case_ = 1; case_ <= 4; case_ += 1) {
|
|
942
|
+
out.push(
|
|
943
|
+
`${file.path}::test_${file.path.replace(/\W/g, "_")}_${case_} ${file === failing && case_ === 3 ? "FAILED" : "PASSED"} [${String(Math.round(100 * case_ / 4)).padStart(3, " ")}%]`
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
if (failing !== null) {
|
|
948
|
+
const line = Math.max(1, Math.floor(failing.lines / 2));
|
|
949
|
+
out.push(
|
|
950
|
+
"",
|
|
951
|
+
"=================================== FAILURES ===================================",
|
|
952
|
+
`____________________ test_${failing.path.replace(/\W/g, "_")}_3 ____________________`,
|
|
953
|
+
""
|
|
954
|
+
);
|
|
955
|
+
for (let frame = 0; frame < 12; frame += 1) {
|
|
956
|
+
out.push(
|
|
957
|
+
` File "${failing.path}", line ${line + frame}, in step_${frame}`,
|
|
958
|
+
` residual = advance(state, dt) # frame ${frame}`
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
out.push(
|
|
962
|
+
"",
|
|
963
|
+
"AssertionError: residual 3.2e-4 exceeds tolerance 1e-6",
|
|
964
|
+
`FAILED ${failing.path}::test_${failing.path.replace(/\W/g, "_")}_3`
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
out.push(
|
|
968
|
+
"",
|
|
969
|
+
`${failing === null ? files.length * 4 : files.length * 4 - 1} passed, ${failing === null ? 0 : 1} failed`
|
|
970
|
+
);
|
|
971
|
+
return out.join("\n");
|
|
972
|
+
}
|
|
973
|
+
function generateSyntheticTrace(spec, index) {
|
|
974
|
+
const rnd = mulberry322(spec.seed * 1e3 + index);
|
|
975
|
+
const between = (low, high) => low + Math.floor(rnd() * (high - low + 1));
|
|
976
|
+
const pick = (items) => items[Math.floor(rnd() * items.length)];
|
|
977
|
+
const cwd = `/synthetic/${spec.id}-${index}`;
|
|
978
|
+
const ledger = new Ledger(cwd);
|
|
979
|
+
const repo = [];
|
|
980
|
+
for (let file = 0; file < spec.files; file += 1) {
|
|
981
|
+
const dir = DIRECTORIES[file % DIRECTORIES.length];
|
|
982
|
+
const stem = STEMS[Math.floor(file / DIRECTORIES.length) % STEMS.length];
|
|
983
|
+
const ext = EXTENSIONS[file % EXTENSIONS.length];
|
|
984
|
+
repo.push({
|
|
985
|
+
path: `${dir}/${stem}${Math.floor(file / (DIRECTORIES.length * STEMS.length)) || ""}.${ext}`,
|
|
986
|
+
lines: between(spec.fileLines[0], spec.fileLines[1]),
|
|
987
|
+
version: 1
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
const zipf = () => repo[powerLawRank(repo.length, spec.zipfExponent, rnd())];
|
|
991
|
+
const scriptNames = Object.keys(spec.scripts).flatMap(
|
|
992
|
+
(name) => Array.from({ length: spec.scripts[name] }, () => name)
|
|
993
|
+
);
|
|
994
|
+
const history = [];
|
|
995
|
+
let treeListings = 0;
|
|
996
|
+
const readWhole = (file) => ledger.call("read", { path: file.path }, fileBody(file));
|
|
997
|
+
const readListingPath = (file) => ledger.call(
|
|
998
|
+
"read",
|
|
999
|
+
{ path: file.path, offset: 1, limit: CONSUMED_LISTING_SAMPLE_LINES },
|
|
1000
|
+
fileBody(file, 0, CONSUMED_LISTING_SAMPLE_LINES)
|
|
1001
|
+
);
|
|
1002
|
+
const readRange = (file) => {
|
|
1003
|
+
const limit = between(20, 80);
|
|
1004
|
+
const offset = between(1, Math.max(1, file.lines - limit));
|
|
1005
|
+
ledger.call("read", { path: file.path, offset, limit }, fileBody(file, offset - 1, limit));
|
|
1006
|
+
};
|
|
1007
|
+
const edit = (file) => {
|
|
1008
|
+
file.version += 1;
|
|
1009
|
+
ledger.call(
|
|
1010
|
+
"edit",
|
|
1011
|
+
{ path: file.path, edits: [{ oldText: `v${file.version - 1}`, newText: `v${file.version}` }] },
|
|
1012
|
+
`Applied 1 edit to ${file.path}`
|
|
1013
|
+
);
|
|
1014
|
+
};
|
|
1015
|
+
const runTests = (focus, failing) => ledger.call(
|
|
1016
|
+
"bash",
|
|
1017
|
+
{ command: `pytest ${focus.map((file) => file.path).join(" ")} -q` },
|
|
1018
|
+
testOutput(focus, failing),
|
|
1019
|
+
{ isError: failing !== null }
|
|
1020
|
+
);
|
|
1021
|
+
for (let turn = 0; turn < spec.turns; turn += 1) {
|
|
1022
|
+
const script = pick(scriptNames);
|
|
1023
|
+
const returning = history.length > 0 && rnd() < spec.returnProbability;
|
|
1024
|
+
const focus = returning ? pick(history) : Array.from({ length: between(1, 3) }, zipf);
|
|
1025
|
+
if (!returning) history.push(focus);
|
|
1026
|
+
const names = focus.map((file) => file.path).join(", ");
|
|
1027
|
+
ledger.user(`Episode ${turn + 1}: ${script} ${names}`);
|
|
1028
|
+
ledger.assistant(`Plan for ${script}: start from ${names} and keep the residual bounded.`, `Working on ${names}.`);
|
|
1029
|
+
switch (script) {
|
|
1030
|
+
case "explore": {
|
|
1031
|
+
const dir = pick(DIRECTORIES);
|
|
1032
|
+
const tree = rnd() < 0.6;
|
|
1033
|
+
const discovered = tree ? repo.slice(0, between(24, repo.length)) : repo.filter((file) => file.path.startsWith(`${dir}/`));
|
|
1034
|
+
const consumeListing = tree && treeListings % 7 === 0;
|
|
1035
|
+
if (tree) treeListings += 1;
|
|
1036
|
+
const listed = consumeListing ? repo.slice(0, CONSUMED_LISTING_READ_LIMIT) : discovered;
|
|
1037
|
+
const root = tree ? "." : dir;
|
|
1038
|
+
ledger.call(
|
|
1039
|
+
rnd() < 0.5 ? "find" : "ls",
|
|
1040
|
+
{ path: root },
|
|
1041
|
+
listed.map((file) => tree ? file.path : file.path.slice(dir.length + 1)).join("\n")
|
|
1042
|
+
);
|
|
1043
|
+
const term = pick(["residual", "checkpoint", "boundary", "tolerance"]);
|
|
1044
|
+
const matches = repo.filter(() => rnd() < 0.15).slice(0, 20);
|
|
1045
|
+
ledger.call(
|
|
1046
|
+
"grep",
|
|
1047
|
+
{ pattern: term, path: "src" },
|
|
1048
|
+
matches.map(
|
|
1049
|
+
(file) => `${file.path}:${between(1, file.lines)}: ${term} = compute_${term}(state, dt, tol=1e-6) # ${file.path}`
|
|
1050
|
+
).join("\n")
|
|
1051
|
+
);
|
|
1052
|
+
const walk = consumeListing ? listed : listed.slice(0, between(2, 4));
|
|
1053
|
+
for (const file of walk) (consumeListing ? readListingPath : readWhole)(file);
|
|
1054
|
+
for (const file of matches.slice(0, between(0, 2))) readWhole(file);
|
|
1055
|
+
break;
|
|
1056
|
+
}
|
|
1057
|
+
case "implement": {
|
|
1058
|
+
for (const file of focus) {
|
|
1059
|
+
readWhole(file);
|
|
1060
|
+
edit(file);
|
|
1061
|
+
if (rnd() < 0.7) readRange(file);
|
|
1062
|
+
}
|
|
1063
|
+
break;
|
|
1064
|
+
}
|
|
1065
|
+
case "validate": {
|
|
1066
|
+
const culprit = pick(focus);
|
|
1067
|
+
readWhole(culprit);
|
|
1068
|
+
runTests(focus, culprit);
|
|
1069
|
+
edit(culprit);
|
|
1070
|
+
runTests(focus, null);
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
case "analyze": {
|
|
1074
|
+
const lines = between(spec.simulationLines[0], spec.simulationLines[1]);
|
|
1075
|
+
const text = simulationOutput(lines, spec.seed + turn);
|
|
1076
|
+
const offload = lines > spec.offloadAfterLines;
|
|
1077
|
+
ledger.call(
|
|
1078
|
+
"bash",
|
|
1079
|
+
{ command: `python scripts/run_sim.py --config configs/run.yaml --steps ${lines}` },
|
|
1080
|
+
offload ? `${text.split("\n").slice(0, spec.offloadAfterLines).join("\n")}
|
|
1081
|
+
[tool result truncated]` : text,
|
|
1082
|
+
{ offload }
|
|
1083
|
+
);
|
|
1084
|
+
const results = pick(focus);
|
|
1085
|
+
readRange(results);
|
|
1086
|
+
if (rnd() < 0.5)
|
|
1087
|
+
ledger.call(
|
|
1088
|
+
"grep",
|
|
1089
|
+
{ pattern: "energy", path: results.path },
|
|
1090
|
+
`${results.path}:${between(1, results.lines)}: energy = 0.999`
|
|
1091
|
+
);
|
|
1092
|
+
break;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
ledger.assistant(`Checked ${names}; residual acceptable for this episode.`, `Done with episode ${turn + 1}.`);
|
|
1096
|
+
}
|
|
1097
|
+
const entries = ledger.entries;
|
|
1098
|
+
return {
|
|
1099
|
+
id: `${spec.id}-${String(index).padStart(2, "0")}`,
|
|
1100
|
+
source: `synthetic:${spec.id}#${index}`,
|
|
1101
|
+
cwd,
|
|
1102
|
+
entries,
|
|
1103
|
+
turnCount: countReplayTurns(entries)
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
function generateSyntheticCorpora(ids) {
|
|
1107
|
+
const traces = [];
|
|
1108
|
+
for (const id of ids) {
|
|
1109
|
+
const spec = syntheticCorpus(id);
|
|
1110
|
+
if (spec === void 0) throw new Error(`unknown synthetic corpus: ${id}`);
|
|
1111
|
+
for (let index = 0; index < spec.traces; index += 1) traces.push(generateSyntheticTrace(spec, index));
|
|
1112
|
+
}
|
|
1113
|
+
return { traces, cascade: { found: traces.length, unreadable: 0, filtered: {}, kept: traces.length } };
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
// src/cli/context-working-set.ts
|
|
1117
|
+
var REPLAY_HELP = `Usage:
|
|
1118
|
+
clio-coder context replay (--sessions <path>... | --synthetic <corpus>...) [options]
|
|
1119
|
+
|
|
1120
|
+
Options:
|
|
1121
|
+
--sessions <path>... Clio session directories, sessions roots, or ledger JSONL files
|
|
1122
|
+
--synthetic <ids> comma-separated procedural corpora: ${SYNTHETIC_CORPORA.map((spec) => spec.id).join(",")}
|
|
1123
|
+
--policies <ids> comma-separated none,random,age-horizon,structural-v1,oracle
|
|
1124
|
+
--budgets <tokens> comma-separated budgets (default: 16000,32000,64000)
|
|
1125
|
+
--threshold <ratio> pressure threshold (default: 0.8)
|
|
1126
|
+
--target <ratio> post-eviction pressure target (default: 0.6)
|
|
1127
|
+
--protect-last-turns <n>
|
|
1128
|
+
protected recent turns (default: ${DEFAULT_WORKING_SET_SETTINGS.protectLastTurns})
|
|
1129
|
+
--min-evictable-tokens <n>
|
|
1130
|
+
minimum tool-result body tokens (default: ${DEFAULT_WORKING_SET_SETTINGS.minEvictableTokens})
|
|
1131
|
+
--seed <integer> deterministic random-policy seed (default: 0)
|
|
1132
|
+
--no-filter include every readable transcript
|
|
1133
|
+
--json <path> write the stable JSON report
|
|
1134
|
+
--md <path> write Markdown instead of printing it
|
|
1135
|
+
`;
|
|
1136
|
+
var WORKING_SET_HELP = `Usage:
|
|
1137
|
+
clio-coder context working-set --session <id|path>
|
|
1138
|
+
|
|
1139
|
+
Print the durable working-set fold and path-index summary for one Clio session.
|
|
1140
|
+
`;
|
|
1141
|
+
var REPLAY_SUMMARY_TOKENS = 1500;
|
|
1142
|
+
var POLICY_IDS = ["none", "random", "age-horizon", "structural-v1", "oracle"];
|
|
1143
|
+
var CliUsageError = class extends Error {
|
|
1144
|
+
};
|
|
1145
|
+
function commaValues(value, flag) {
|
|
1146
|
+
const values = value.split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
|
|
1147
|
+
if (values.length === 0) throw new CliUsageError(`${flag} requires at least one value`);
|
|
1148
|
+
return [...new Set(values)];
|
|
1149
|
+
}
|
|
1150
|
+
function numberValue(value, flag) {
|
|
1151
|
+
const parsed = Number(value);
|
|
1152
|
+
if (!Number.isFinite(parsed)) throw new CliUsageError(`${flag} requires a finite number`);
|
|
1153
|
+
return parsed;
|
|
1154
|
+
}
|
|
1155
|
+
function requiredValue(args, index, flag) {
|
|
1156
|
+
const value = args[index + 1];
|
|
1157
|
+
if (value === void 0 || value.startsWith("--")) throw new CliUsageError(`${flag} requires a value`);
|
|
1158
|
+
return value;
|
|
1159
|
+
}
|
|
1160
|
+
function policyResolves(id) {
|
|
1161
|
+
if (id === "none" || id === "random" || id === "oracle") return true;
|
|
1162
|
+
try {
|
|
1163
|
+
resolveWorkingSetPolicy(id);
|
|
1164
|
+
return true;
|
|
1165
|
+
} catch {
|
|
1166
|
+
return false;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function defaultPolicies() {
|
|
1170
|
+
return POLICY_IDS.filter(policyResolves);
|
|
1171
|
+
}
|
|
1172
|
+
function parseReplayArgs(args) {
|
|
1173
|
+
const parsed = {
|
|
1174
|
+
sessions: [],
|
|
1175
|
+
policies: defaultPolicies(),
|
|
1176
|
+
budgets: [16e3, 32e3, 64e3],
|
|
1177
|
+
threshold: 0.8,
|
|
1178
|
+
target: 0.6,
|
|
1179
|
+
protectLastTurns: DEFAULT_WORKING_SET_SETTINGS.protectLastTurns,
|
|
1180
|
+
minEvictableTokens: DEFAULT_WORKING_SET_SETTINGS.minEvictableTokens,
|
|
1181
|
+
seed: 0,
|
|
1182
|
+
synthetic: [],
|
|
1183
|
+
noFilter: false
|
|
1184
|
+
};
|
|
1185
|
+
let policiesExplicit = false;
|
|
1186
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
1187
|
+
const arg = args[index];
|
|
1188
|
+
if (arg === "--no-filter") {
|
|
1189
|
+
parsed.noFilter = true;
|
|
1190
|
+
continue;
|
|
1191
|
+
}
|
|
1192
|
+
if (arg === "--sessions") {
|
|
1193
|
+
let consumed = 0;
|
|
1194
|
+
while (index + 1 < args.length && !args[index + 1]?.startsWith("--")) {
|
|
1195
|
+
parsed.sessions.push(args[index + 1]);
|
|
1196
|
+
index += 1;
|
|
1197
|
+
consumed += 1;
|
|
1198
|
+
}
|
|
1199
|
+
if (consumed === 0) throw new CliUsageError("--sessions requires at least one path");
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
if (arg === "--policies" || arg === "--budgets" || arg === "--synthetic" || arg === "--threshold" || arg === "--target" || arg === "--protect-last-turns" || arg === "--min-evictable-tokens" || arg === "--seed") {
|
|
1203
|
+
const value = requiredValue(args, index, arg);
|
|
1204
|
+
index += 1;
|
|
1205
|
+
if (arg === "--policies") {
|
|
1206
|
+
const values = commaValues(value, arg);
|
|
1207
|
+
const unknown = values.filter((entry) => !POLICY_IDS.includes(entry));
|
|
1208
|
+
if (unknown.length > 0) throw new CliUsageError(`unknown replay policy: ${unknown.join(", ")}`);
|
|
1209
|
+
parsed.policies = values;
|
|
1210
|
+
policiesExplicit = true;
|
|
1211
|
+
} else if (arg === "--budgets") {
|
|
1212
|
+
parsed.budgets = commaValues(value, arg).map((entry) => {
|
|
1213
|
+
const budget = numberValue(entry, arg);
|
|
1214
|
+
if (!Number.isInteger(budget) || budget <= 0) {
|
|
1215
|
+
throw new CliUsageError("--budgets values must be positive integers");
|
|
1216
|
+
}
|
|
1217
|
+
return budget;
|
|
1218
|
+
});
|
|
1219
|
+
} else if (arg === "--synthetic") {
|
|
1220
|
+
const values = commaValues(value, arg);
|
|
1221
|
+
const known = SYNTHETIC_CORPORA.map((spec) => spec.id);
|
|
1222
|
+
const unknown = values.filter((entry) => !known.includes(entry));
|
|
1223
|
+
if (unknown.length > 0) throw new CliUsageError(`unknown synthetic corpus: ${unknown.join(", ")}`);
|
|
1224
|
+
parsed.synthetic = values;
|
|
1225
|
+
} else if (arg === "--threshold") {
|
|
1226
|
+
parsed.threshold = numberValue(value, arg);
|
|
1227
|
+
if (parsed.threshold <= 0 || parsed.threshold > 1) {
|
|
1228
|
+
throw new CliUsageError("--threshold must be greater than 0 and at most 1");
|
|
1229
|
+
}
|
|
1230
|
+
} else if (arg === "--target") {
|
|
1231
|
+
parsed.target = numberValue(value, arg);
|
|
1232
|
+
if (parsed.target <= 0 || parsed.target >= 1) {
|
|
1233
|
+
throw new CliUsageError("--target must be greater than 0 and less than 1");
|
|
1234
|
+
}
|
|
1235
|
+
} else if (arg === "--protect-last-turns") {
|
|
1236
|
+
parsed.protectLastTurns = numberValue(value, arg);
|
|
1237
|
+
if (!Number.isInteger(parsed.protectLastTurns) || parsed.protectLastTurns < 1) {
|
|
1238
|
+
throw new CliUsageError("--protect-last-turns must be an integer at least 1");
|
|
1239
|
+
}
|
|
1240
|
+
} else if (arg === "--min-evictable-tokens") {
|
|
1241
|
+
parsed.minEvictableTokens = numberValue(value, arg);
|
|
1242
|
+
if (!Number.isInteger(parsed.minEvictableTokens) || parsed.minEvictableTokens < 0) {
|
|
1243
|
+
throw new CliUsageError("--min-evictable-tokens must be a non-negative integer");
|
|
1244
|
+
}
|
|
1245
|
+
} else {
|
|
1246
|
+
parsed.seed = numberValue(value, arg);
|
|
1247
|
+
if (!Number.isInteger(parsed.seed)) throw new CliUsageError("--seed must be an integer");
|
|
1248
|
+
}
|
|
1249
|
+
continue;
|
|
1250
|
+
}
|
|
1251
|
+
if (arg === "--json" || arg === "--md") {
|
|
1252
|
+
const value = requiredValue(args, index, arg);
|
|
1253
|
+
index += 1;
|
|
1254
|
+
if (arg === "--json") parsed.jsonPath = value;
|
|
1255
|
+
else parsed.markdownPath = value;
|
|
1256
|
+
continue;
|
|
1257
|
+
}
|
|
1258
|
+
throw new CliUsageError(`unknown flag ${arg}`);
|
|
1259
|
+
}
|
|
1260
|
+
if (parsed.sessions.length === 0 && parsed.synthetic.length === 0) {
|
|
1261
|
+
throw new CliUsageError("--sessions or --synthetic is required");
|
|
1262
|
+
}
|
|
1263
|
+
if (parsed.target >= parsed.threshold) throw new CliUsageError("--target must be less than --threshold");
|
|
1264
|
+
if (policiesExplicit) {
|
|
1265
|
+
const unavailable = parsed.policies.filter((id) => !policyResolves(id));
|
|
1266
|
+
if (unavailable.length > 0) {
|
|
1267
|
+
throw new CliUsageError(`replay policy is not available in this build: ${unavailable.join(", ")}`);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return parsed;
|
|
1271
|
+
}
|
|
1272
|
+
function policyForTrace(id, graph, seed) {
|
|
1273
|
+
if (id === "none") return nonePolicy;
|
|
1274
|
+
if (id === "random") return makeRandomPolicy(seed);
|
|
1275
|
+
if (id === "oracle") return makeOraclePolicy(graph);
|
|
1276
|
+
return resolveWorkingSetPolicy(id);
|
|
1277
|
+
}
|
|
1278
|
+
function gitSha() {
|
|
1279
|
+
try {
|
|
1280
|
+
return execFileSync("git", ["rev-parse", "HEAD"], {
|
|
1281
|
+
cwd: process.cwd(),
|
|
1282
|
+
encoding: "utf8",
|
|
1283
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
1284
|
+
}).trim();
|
|
1285
|
+
} catch {
|
|
1286
|
+
return null;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
function exactCommandLine() {
|
|
1290
|
+
return [process.execPath, ...process.execArgv, ...process.argv.slice(1)];
|
|
1291
|
+
}
|
|
1292
|
+
async function writeOutput(path, contents) {
|
|
1293
|
+
const output = resolve2(path);
|
|
1294
|
+
await mkdir(dirname2(output), { recursive: true });
|
|
1295
|
+
await writeFile(output, contents, "utf8");
|
|
1296
|
+
return output;
|
|
1297
|
+
}
|
|
1298
|
+
function cascadeLine(cascade) {
|
|
1299
|
+
const filtered = Object.entries(cascade.filtered).map(([stage, count]) => `${stage}=${count}`).join(" ");
|
|
1300
|
+
return `cascade found=${cascade.found} unreadable=${cascade.unreadable} ${filtered} kept=${cascade.kept}`;
|
|
1301
|
+
}
|
|
1302
|
+
async function runContextReplayCommand(args) {
|
|
1303
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
1304
|
+
process.stdout.write(REPLAY_HELP);
|
|
1305
|
+
return 0;
|
|
1306
|
+
}
|
|
1307
|
+
let parsed;
|
|
1308
|
+
try {
|
|
1309
|
+
parsed = parseReplayArgs(args);
|
|
1310
|
+
} catch (error) {
|
|
1311
|
+
process.stderr.write(
|
|
1312
|
+
`clio-coder context replay: ${error instanceof Error ? error.message : String(error)}
|
|
1313
|
+
${REPLAY_HELP}`
|
|
1314
|
+
);
|
|
1315
|
+
return 2;
|
|
1316
|
+
}
|
|
1317
|
+
try {
|
|
1318
|
+
const ledgers = await loadClioTraces(parsed.sessions, { filter: parsed.noFilter ? false : {} });
|
|
1319
|
+
const synthetic = generateSyntheticCorpora(parsed.synthetic);
|
|
1320
|
+
const loaded = {
|
|
1321
|
+
traces: [...ledgers.traces, ...synthetic.traces],
|
|
1322
|
+
cascade: {
|
|
1323
|
+
found: ledgers.cascade.found + synthetic.cascade.found,
|
|
1324
|
+
unreadable: ledgers.cascade.unreadable,
|
|
1325
|
+
filtered: ledgers.cascade.filtered,
|
|
1326
|
+
kept: ledgers.cascade.kept + synthetic.cascade.kept
|
|
1327
|
+
}
|
|
1328
|
+
};
|
|
1329
|
+
if (loaded.traces.length === 0) {
|
|
1330
|
+
process.stderr.write(`clio-coder context replay: no traces to replay (${cascadeLine(loaded.cascade)})
|
|
1331
|
+
`);
|
|
1332
|
+
return 1;
|
|
1333
|
+
}
|
|
1334
|
+
const indexed = loaded.traces.map((trace) => {
|
|
1335
|
+
const index = buildPathIndex(trace.entries, { cwd: trace.cwd });
|
|
1336
|
+
return { trace, index, graph: buildReferenceGraph(trace, index) };
|
|
1337
|
+
});
|
|
1338
|
+
const settings = {
|
|
1339
|
+
...DEFAULT_WORKING_SET_SETTINGS,
|
|
1340
|
+
target: parsed.target,
|
|
1341
|
+
protectLastTurns: parsed.protectLastTurns,
|
|
1342
|
+
minEvictableTokens: parsed.minEvictableTokens
|
|
1343
|
+
};
|
|
1344
|
+
const results = [];
|
|
1345
|
+
for (const budgetTokens of parsed.budgets) {
|
|
1346
|
+
for (const policyId of parsed.policies) {
|
|
1347
|
+
const measurements = indexed.map(({ trace, index, graph }) => ({
|
|
1348
|
+
trace,
|
|
1349
|
+
index,
|
|
1350
|
+
graph,
|
|
1351
|
+
replay: replayTrace(trace, policyForTrace(policyId, graph, parsed.seed), {
|
|
1352
|
+
policyId,
|
|
1353
|
+
budgetTokens,
|
|
1354
|
+
threshold: parsed.threshold,
|
|
1355
|
+
target: parsed.target,
|
|
1356
|
+
settings,
|
|
1357
|
+
seed: parsed.seed,
|
|
1358
|
+
summaries: { keepRecentTokens: DEFAULT_KEEP_RECENT_TOKENS, summaryTokens: REPLAY_SUMMARY_TOKENS }
|
|
1359
|
+
})
|
|
1360
|
+
}));
|
|
1361
|
+
results.push({ budgetTokens, policyId, metrics: aggregateReplayMetrics(measurements) });
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
const report = {
|
|
1365
|
+
config: {
|
|
1366
|
+
policies: parsed.policies,
|
|
1367
|
+
budgets: parsed.budgets,
|
|
1368
|
+
threshold: parsed.threshold,
|
|
1369
|
+
target: parsed.target,
|
|
1370
|
+
seed: parsed.seed,
|
|
1371
|
+
corpus: [...parsed.sessions.length > 0 ? ["ledgers"] : [], ...parsed.synthetic],
|
|
1372
|
+
filter: parsed.noFilter ? "none" : "default",
|
|
1373
|
+
settings
|
|
1374
|
+
},
|
|
1375
|
+
cascade: loaded.cascade,
|
|
1376
|
+
results,
|
|
1377
|
+
gitSha: gitSha(),
|
|
1378
|
+
commandLine: exactCommandLine()
|
|
1379
|
+
};
|
|
1380
|
+
const markdown = renderReplayMarkdown(report);
|
|
1381
|
+
if (parsed.markdownPath === void 0) process.stdout.write(markdown);
|
|
1382
|
+
else {
|
|
1383
|
+
const path = await writeOutput(parsed.markdownPath, markdown);
|
|
1384
|
+
process.stdout.write(`${cascadeLine(loaded.cascade)}
|
|
1385
|
+
markdown ${path}
|
|
1386
|
+
`);
|
|
1387
|
+
}
|
|
1388
|
+
if (parsed.jsonPath !== void 0) {
|
|
1389
|
+
const path = await writeOutput(parsed.jsonPath, renderReplayJson(report));
|
|
1390
|
+
process.stdout.write(`json ${path}
|
|
1391
|
+
`);
|
|
1392
|
+
}
|
|
1393
|
+
return 0;
|
|
1394
|
+
} catch (error) {
|
|
1395
|
+
process.stderr.write(`clio-coder context replay failed: ${error instanceof Error ? error.message : String(error)}
|
|
1396
|
+
`);
|
|
1397
|
+
return 1;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
async function sessionPath(value) {
|
|
1401
|
+
const candidate = resolve2(value);
|
|
1402
|
+
try {
|
|
1403
|
+
const facts = await stat2(candidate);
|
|
1404
|
+
if (facts.isDirectory()) return join2(candidate, "current.jsonl");
|
|
1405
|
+
if (facts.isFile()) return candidate;
|
|
1406
|
+
} catch {
|
|
1407
|
+
}
|
|
1408
|
+
try {
|
|
1409
|
+
assertSafeId(value, "session");
|
|
1410
|
+
} catch (error) {
|
|
1411
|
+
throw new CliUsageError(error instanceof Error ? error.message : String(error));
|
|
1412
|
+
}
|
|
1413
|
+
const sessionsRoot = join2(clioStatePath(), "sessions");
|
|
1414
|
+
let cwdHashes;
|
|
1415
|
+
try {
|
|
1416
|
+
cwdHashes = await readdir2(sessionsRoot);
|
|
1417
|
+
} catch {
|
|
1418
|
+
throw new Error(`session not found: ${value}`);
|
|
1419
|
+
}
|
|
1420
|
+
for (const cwdHash of cwdHashes.sort()) {
|
|
1421
|
+
const current = join2(sessionsRoot, cwdHash, value, "current.jsonl");
|
|
1422
|
+
try {
|
|
1423
|
+
if ((await stat2(current)).isFile()) return current;
|
|
1424
|
+
} catch {
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
throw new Error(`session not found: ${value}`);
|
|
1428
|
+
}
|
|
1429
|
+
async function pinnedLeafForSession(source) {
|
|
1430
|
+
try {
|
|
1431
|
+
const raw = await readFile2(join2(dirname2(source), "meta.json"), "utf8");
|
|
1432
|
+
const value = JSON.parse(raw);
|
|
1433
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1434
|
+
const pinned = value.pinnedLeafTurnId;
|
|
1435
|
+
return typeof pinned === "string" && pinned.length > 0 ? pinned : void 0;
|
|
1436
|
+
} catch {
|
|
1437
|
+
return void 0;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
async function sessionCwdForSession(source) {
|
|
1441
|
+
try {
|
|
1442
|
+
const raw = await readFile2(join2(dirname2(source), "meta.json"), "utf8");
|
|
1443
|
+
const value = JSON.parse(raw);
|
|
1444
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
1445
|
+
const cwd = value.cwd;
|
|
1446
|
+
return typeof cwd === "string" && cwd.length > 0 ? cwd : null;
|
|
1447
|
+
} catch {
|
|
1448
|
+
return null;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
function formatWorkingSet(trace) {
|
|
1452
|
+
const view = foldWorkingSet(trace.entries);
|
|
1453
|
+
const index = buildPathIndex(trace.entries, { cwd: trace.cwd });
|
|
1454
|
+
const graph = buildReferenceGraph(trace, index);
|
|
1455
|
+
const evictedTokens = [...view.evicted.values()].reduce((sum, state) => sum + state.tokensFreed, 0);
|
|
1456
|
+
const churn = view.itemsEvicted === 0 ? "n/a" : (view.recalls / view.itemsEvicted).toFixed(3);
|
|
1457
|
+
const opCounts = /* @__PURE__ */ new Map();
|
|
1458
|
+
for (const observation of index.observations) {
|
|
1459
|
+
opCounts.set(observation.op, (opCounts.get(observation.op) ?? 0) + 1);
|
|
1460
|
+
}
|
|
1461
|
+
const rewrittenPaths = /* @__PURE__ */ new Set();
|
|
1462
|
+
for (const edge of graph.edges) {
|
|
1463
|
+
if (edge.kind !== "file_rewrite") continue;
|
|
1464
|
+
const path = index.byRef.get(edge.from)?.path;
|
|
1465
|
+
if (path) rewrittenPaths.add(path);
|
|
1466
|
+
}
|
|
1467
|
+
const lines = [
|
|
1468
|
+
`session: ${trace.id}`,
|
|
1469
|
+
`source: ${trace.source}`,
|
|
1470
|
+
"working set:",
|
|
1471
|
+
` policy: ${view.lastPolicyId ?? "none"}`,
|
|
1472
|
+
` eviction events: ${view.evictionEvents}`,
|
|
1473
|
+
` evicted refs: ${view.evicted.size}`,
|
|
1474
|
+
` items evicted: ${view.itemsEvicted}`,
|
|
1475
|
+
` evicted tokens: ${evictedTokens}`,
|
|
1476
|
+
` recalls: ${view.recalls}`,
|
|
1477
|
+
` churn: ${churn}`,
|
|
1478
|
+
" refs:",
|
|
1479
|
+
...[...view.evicted.entries()].length === 0 ? [" none"] : [...view.evicted.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([ref, state]) => ` ${ref} reason=${state.reason} by=${state.by ?? "-"} tokens=${state.tokensFreed}`),
|
|
1480
|
+
"path index:",
|
|
1481
|
+
` observations: ${index.observations.length}`,
|
|
1482
|
+
` ops: ${[...opCounts.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([op, count]) => `${op}=${count}`).join(", ") || "none"}`,
|
|
1483
|
+
` paths with rewrites: ${rewrittenPaths.size}`,
|
|
1484
|
+
...[...rewrittenPaths].sort().map((path) => ` ${path}`),
|
|
1485
|
+
""
|
|
1486
|
+
];
|
|
1487
|
+
return lines.join("\n");
|
|
1488
|
+
}
|
|
1489
|
+
async function runContextWorkingSetCommand(args) {
|
|
1490
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
1491
|
+
process.stdout.write(WORKING_SET_HELP);
|
|
1492
|
+
return 0;
|
|
1493
|
+
}
|
|
1494
|
+
let session;
|
|
1495
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
1496
|
+
const arg = args[index];
|
|
1497
|
+
if (arg === "--session") {
|
|
1498
|
+
try {
|
|
1499
|
+
session = requiredValue(args, index, arg);
|
|
1500
|
+
} catch (error) {
|
|
1501
|
+
process.stderr.write(
|
|
1502
|
+
`clio-coder context working-set: ${error instanceof Error ? error.message : String(error)}
|
|
1503
|
+
${WORKING_SET_HELP}`
|
|
1504
|
+
);
|
|
1505
|
+
return 2;
|
|
1506
|
+
}
|
|
1507
|
+
index += 1;
|
|
1508
|
+
continue;
|
|
1509
|
+
}
|
|
1510
|
+
process.stderr.write(`clio-coder context working-set: unknown flag ${arg}
|
|
1511
|
+
${WORKING_SET_HELP}`);
|
|
1512
|
+
return 2;
|
|
1513
|
+
}
|
|
1514
|
+
if (session === void 0) {
|
|
1515
|
+
process.stderr.write(`clio-coder context working-set: --session is required
|
|
1516
|
+
${WORKING_SET_HELP}`);
|
|
1517
|
+
return 2;
|
|
1518
|
+
}
|
|
1519
|
+
try {
|
|
1520
|
+
const source = await sessionPath(session);
|
|
1521
|
+
const raw = await readFile2(source, "utf8");
|
|
1522
|
+
const parsed = parseSessionEntries(raw, source);
|
|
1523
|
+
if (parsed.errors.length > 0) throw new Error(parsed.errors.join("; "));
|
|
1524
|
+
const entries = filterEntriesToActivePath(parsed.entries, await pinnedLeafForSession(source));
|
|
1525
|
+
const cwd = await sessionCwdForSession(source);
|
|
1526
|
+
process.stdout.write(
|
|
1527
|
+
formatWorkingSet({
|
|
1528
|
+
id: basename2(dirname2(source)),
|
|
1529
|
+
source,
|
|
1530
|
+
cwd,
|
|
1531
|
+
entries,
|
|
1532
|
+
turnCount: buildPathIndex(entries, { cwd }).turnCount
|
|
1533
|
+
})
|
|
1534
|
+
);
|
|
1535
|
+
return 0;
|
|
1536
|
+
} catch (error) {
|
|
1537
|
+
if (error instanceof CliUsageError) {
|
|
1538
|
+
process.stderr.write(`clio-coder context working-set: ${error.message}
|
|
1539
|
+
${WORKING_SET_HELP}`);
|
|
1540
|
+
return 2;
|
|
1541
|
+
}
|
|
1542
|
+
process.stderr.write(
|
|
1543
|
+
`clio-coder context working-set failed: ${error instanceof Error ? error.message : String(error)}
|
|
1544
|
+
`
|
|
1545
|
+
);
|
|
1546
|
+
return 1;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
export {
|
|
1550
|
+
runContextReplayCommand,
|
|
1551
|
+
runContextWorkingSetCommand
|
|
1552
|
+
};
|
|
1553
|
+
//# sourceMappingURL=context-working-set-UX5KEP4J.js.map
|