dsh-context-compression-improved 0.11.4 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/plans/improve/checklist.md +32 -0
- package/.agents/plans/improve/findings.md +102 -0
- package/.agents/plans/improve/spec.md +53 -0
- package/.agents/plans/improve/tasks.md +70 -0
- package/.gitattributes +2 -0
- package/.githooks/pre-push +37 -0
- package/.github/workflows/ci.yml +47 -0
- package/CHANGELOG.de.md +580 -0
- package/CHANGELOG.es.md +588 -0
- package/CHANGELOG.fr.md +576 -0
- package/CHANGELOG.it.md +577 -0
- package/CHANGELOG.ja.md +261 -0
- package/CHANGELOG.ko.md +254 -0
- package/CHANGELOG.md +582 -0
- package/CHANGELOG.pt.md +167 -0
- package/CHANGELOG.ru.md +554 -0
- package/CHANGELOG.zh.md +247 -0
- package/CONTRIBUTING.md +22 -0
- package/README.de.md +96 -0
- package/README.es.md +96 -0
- package/README.fr.md +96 -0
- package/README.it.md +96 -0
- package/README.ja.md +102 -0
- package/README.ko.md +102 -0
- package/README.md +121 -15
- package/README.pt.md +96 -0
- package/README.ru.md +96 -0
- package/README.zh.md +104 -14
- package/SECURITY.md +18 -0
- package/THIRD_PARTY_NOTICES.md +7 -25
- package/assets/board.png +0 -0
- package/assets/context.png +0 -0
- package/assets/dshfind-card.svg +33 -0
- package/docs/assets/context-compression-selector-profiles.jpg +0 -0
- package/docs/assets/context-compression-selector-settings.png +0 -0
- package/docs/compat-0.1.5-report.md +77 -0
- package/docs/installation.de.md +104 -0
- package/docs/installation.es.md +104 -0
- package/docs/installation.fr.md +104 -0
- package/docs/installation.it.md +104 -0
- package/docs/installation.ja.md +78 -0
- package/docs/installation.ko.md +78 -0
- package/docs/installation.md +103 -0
- package/docs/installation.ru.md +104 -0
- package/docs/installation.zh.md +100 -0
- package/docs/repair-log.md +657 -0
- package/docs/shallow-teaching-preset-template.md +46 -0
- package/eslint.config.js +30 -0
- package/package.json +112 -127
- package/packages/selector/LICENSE +21 -0
- package/packages/selector/README.md +26 -0
- package/packages/selector/README.zh.md +24 -0
- package/packages/selector/THIRD_PARTY_NOTICES.md +32 -0
- package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -0
- package/packages/selector/docs/native-tool-result-selector-spec.md +34 -0
- package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -0
- package/{dsh.plugin.json → packages/selector/dsh.plugin.json} +2 -2
- package/{lib → packages/selector/lib}/client.js +400 -0
- package/packages/selector/lib/style.css +350 -0
- package/packages/selector/package.json +180 -0
- package/packages/selector/screenshots.json +3 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +352 -0
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +89 -0
- package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -0
- package/packages/selector/src/client/EstimatorControls.tsx +277 -0
- package/packages/selector/src/client/bridge-scope.ts +125 -0
- package/packages/selector/src/client/decode.ts +68 -0
- package/packages/selector/src/client/fab-pos.ts +71 -0
- package/packages/selector/src/client/guide-steps.ts +50 -0
- package/packages/selector/src/client/guide.tsx +368 -0
- package/packages/selector/src/client/index.ts +456 -0
- package/packages/selector/src/client/locales/de.ts +167 -0
- package/packages/selector/src/client/locales/es.ts +167 -0
- package/packages/selector/src/client/locales/fr.ts +167 -0
- package/packages/selector/src/client/locales/it.ts +167 -0
- package/packages/selector/src/client/locales/ja.ts +167 -0
- package/packages/selector/src/client/locales/ko.ts +167 -0
- package/packages/selector/src/client/locales/pt.ts +167 -0
- package/packages/selector/src/client/locales/ru.ts +167 -0
- package/packages/selector/src/client/locales.ts +334 -0
- package/packages/selector/src/client/monitor-fab.ts +445 -0
- package/packages/selector/src/client/preset-options.ts +88 -0
- package/packages/selector/src/client/savings-card.tsx +269 -0
- package/packages/selector/src/client/scope-face.ts +24 -0
- package/packages/selector/src/client/settings-section.tsx +171 -0
- package/packages/selector/src/compat/host-generation.ts +96 -0
- package/packages/selector/src/compat/llm-messages.ts +50 -0
- package/packages/selector/src/compat/settings-lease.ts +108 -0
- package/packages/selector/src/compat/tool-result.ts +91 -0
- package/packages/selector/src/css-modules.d.ts +6 -0
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -0
- package/packages/selector/src/estimator-catalog.ts +104 -0
- package/packages/selector/src/index.ts +909 -0
- package/packages/selector/src/invariant.ts +113 -0
- package/packages/selector/src/preset-overlay.ts +749 -0
- package/packages/selector/src/profiles.ts +554 -0
- package/packages/selector/src/pruner/content.ts +199 -0
- package/packages/selector/src/pruner/session.ts +97 -0
- package/packages/selector/src/pruner/state.ts +61 -0
- package/packages/selector/src/pruner/tuning.ts +23 -0
- package/packages/selector/src/pruner/types.ts +78 -0
- package/packages/selector/src/pruner.ts +3222 -0
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
- package/packages/selector/src/runtime/audit.ts +321 -0
- package/packages/selector/src/runtime/config.ts +887 -0
- package/packages/selector/src/runtime/custom-policy.ts +300 -0
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -0
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -0
- package/packages/selector/src/runtime/display-prefs.ts +98 -0
- package/packages/selector/src/runtime/fold-router.ts +143 -0
- package/packages/selector/src/runtime/measurement.ts +444 -0
- package/packages/selector/src/runtime/monitor.ts +341 -0
- package/packages/selector/src/runtime/panel-command.ts +213 -0
- package/packages/selector/src/runtime/reducers.ts +1755 -0
- package/packages/selector/src/runtime/retrieve.ts +505 -0
- package/packages/selector/src/runtime/savings-integral.ts +117 -0
- package/packages/selector/src/runtime/savings-projection.ts +229 -0
- package/packages/selector/src/runtime/savings-verify.ts +446 -0
- package/packages/selector/src/runtime/savings.ts +526 -0
- package/packages/selector/src/runtime/sediment-card.ts +126 -0
- package/packages/selector/src/runtime/sediment-clock.ts +87 -0
- package/packages/selector/src/runtime/sediment-guard.ts +136 -0
- package/packages/selector/src/runtime/sediment-publish.ts +144 -0
- package/packages/selector/src/runtime/sediment-span.ts +170 -0
- package/packages/selector/src/runtime/sediment.ts +209 -0
- package/packages/selector/src/runtime/session-events.ts +30 -0
- package/packages/selector/src/runtime/session-file-decode.ts +523 -0
- package/packages/selector/src/runtime/shallow-tools.ts +342 -0
- package/packages/selector/src/runtime/stats-backfill.ts +541 -0
- package/packages/selector/src/runtime/stats-db.ts +492 -0
- package/packages/selector/src/runtime/stats-schema.ts +123 -0
- package/packages/selector/src/runtime/stats-sync.ts +307 -0
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tail-trim.ts +177 -0
- package/packages/selector/src/runtime/token-count.ts +72 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +82 -0
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -0
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- package/packages/selector/src/runtime/tokenpilot/locator.ts +129 -0
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -0
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
- package/packages/selector/src/runtime/tool-names.ts +42 -0
- package/packages/selector/src/runtime/toolclass.ts +103 -0
- package/packages/selector/src/runtime/triage/masks.ts +128 -0
- package/packages/selector/src/runtime/triage/shallow.ts +132 -0
- package/packages/selector/src/runtime/triage/signals.ts +167 -0
- package/packages/selector/src/runtime/triage/types.ts +125 -0
- package/packages/selector/src/runtime/types.ts +396 -0
- package/packages/selector/src/runtime/value.ts +48 -0
- package/packages/selector/src/settings-bridge.ts +175 -0
- package/packages/selector/src/tui-command.ts +99 -0
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -0
- package/packages/selector/tests/built/client-artifact.spec.ts +56 -0
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -0
- package/packages/selector/tests/code-skeleton.client.spec.ts +98 -0
- package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
- package/packages/selector/tests/custom-contract.client.spec.ts +226 -0
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -0
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -0
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +202 -0
- package/packages/selector/tests/fab-pos.client.spec.ts +47 -0
- package/packages/selector/tests/fixtures/sediment-pool/52adc205-cloud-5.orig.txt +231 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-25.orig.txt +236 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-38.orig.txt +220 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-42.orig.txt +140 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-53.orig.txt +117 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-local-10.orig.txt +67 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-45.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-49.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-63.orig.txt +131 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-75.orig.txt +196 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-76.orig.txt +71 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-85.orig.txt +126 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-86.orig.txt +86 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-87.orig.txt +50 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-96.orig.txt +102 -0
- package/packages/selector/tests/helpers/legacy-namespace.ts +35 -0
- package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -0
- package/packages/selector/tests/monitor-fab.client.spec.ts +240 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +192 -0
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -0
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -0
- package/packages/selector/tests/profiles.client.spec.tsx +436 -0
- package/packages/selector/tests/ptc-custom-write.client.spec.ts +86 -0
- package/packages/selector/tests/public/package-contract.client.spec.ts +91 -0
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -0
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -0
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
- package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
- package/packages/selector/tests/runtime/audit.spec.ts +275 -0
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -0
- package/packages/selector/tests/runtime/behavior-lock-profiles.spec.ts +128 -0
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -0
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -0
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -0
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -0
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
- package/packages/selector/tests/runtime/fallback-diversity.spec.ts +75 -0
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -0
- package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +46 -0
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -0
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -0
- package/packages/selector/tests/runtime/fold-router.spec.ts +140 -0
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
- package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +354 -0
- package/packages/selector/tests/runtime/panel-command.spec.ts +101 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
- package/packages/selector/tests/runtime/ptc-profile.spec.ts +133 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2795 -0
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
- package/packages/selector/tests/runtime/savings-accounting.spec.ts +291 -0
- package/packages/selector/tests/runtime/savings-integral.spec.ts +169 -0
- package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
- package/packages/selector/tests/runtime/savings-verify.spec.ts +226 -0
- package/packages/selector/tests/runtime/savings.spec.ts +139 -0
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
- package/packages/selector/tests/runtime/sediment-card.spec.ts +111 -0
- package/packages/selector/tests/runtime/sediment-config.spec.ts +56 -0
- package/packages/selector/tests/runtime/sediment-guard.spec.ts +71 -0
- package/packages/selector/tests/runtime/sediment-masks.spec.ts +95 -0
- package/packages/selector/tests/runtime/sediment-orchestrator.spec.ts +166 -0
- package/packages/selector/tests/runtime/sediment-pool-share.spec.ts +65 -0
- package/packages/selector/tests/runtime/sediment-publish.spec.ts +142 -0
- package/packages/selector/tests/runtime/sediment-real-shapes.spec.ts +158 -0
- package/packages/selector/tests/runtime/sediment-settings.spec.ts +73 -0
- package/packages/selector/tests/runtime/sediment-signals.spec.ts +124 -0
- package/packages/selector/tests/runtime/sediment-span.spec.ts +92 -0
- package/packages/selector/tests/runtime/session-events.spec.ts +27 -0
- package/packages/selector/tests/runtime/session-file-decode.spec.ts +281 -0
- package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
- package/packages/selector/tests/runtime/shallow-primitives.spec.ts +135 -0
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
- package/packages/selector/tests/runtime/source-kind-audit.spec.ts +79 -0
- package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
- package/packages/selector/tests/runtime/stats-backfill.spec.ts +350 -0
- package/packages/selector/tests/runtime/stats-db.spec.ts +361 -0
- package/packages/selector/tests/runtime/stats-privacy.spec.ts +55 -0
- package/packages/selector/tests/runtime/stats-sync.spec.ts +278 -0
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -0
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -0
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -0
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -0
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +121 -0
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -0
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
- package/packages/selector/tests/runtime/value.spec.ts +23 -0
- package/packages/selector/tests/savings-sessions.client.spec.tsx +140 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +215 -0
- package/packages/selector/tests/standing-generation.host.spec.ts +729 -0
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -0
- package/packages/selector/tests/support/cache-prefix-audit.ts +105 -0
- package/packages/selector/tests/support/mock-adapter.ts +37 -0
- package/packages/selector/tests/support/ui-primitives.tsx +34 -0
- package/packages/selector/tsconfig.json +11 -0
- package/packages/selector/tsdown.client.config.ts +102 -0
- package/packages/selector/tsdown.config.ts +20 -0
- package/pnpm-workspace.yaml +76 -0
- package/screenshots.json +6 -3
- package/scripts/capture-profile-baseline.mjs +42 -0
- package/scripts/cell-browser-e2e.mjs +354 -0
- package/scripts/generate-tokenizer-fixtures.py +81 -0
- package/scripts/generate-vision-fixtures.py +208 -0
- package/scripts/hosts.d.mts +3 -0
- package/scripts/hosts.mjs +81 -0
- package/scripts/packed-components-smoke.mjs +763 -0
- package/scripts/packed-install-e2e.mjs +1346 -0
- package/scripts/sediment-latency-report.mjs +63 -0
- package/scripts/sync-hosts.mjs +176 -0
- package/scripts/toolclass-corpus-replay.mjs +281 -0
- package/scripts/verify-release.mjs +342 -0
- package/tests/TEST_INVENTORY.md +41 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +7 -0
- package/tsconfig.tests.json +15 -0
- package/vitest.built.config.ts +9 -0
- package/vitest.config.ts +54 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer_config.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
- /package/{cordis.patch.yml → packages/selector/cordis.patch.yml} +0 -0
- /package/{lib → packages/selector/lib}/client.d.ts +0 -0
- /package/{lib → packages/selector/lib}/index.d.ts +0 -0
- /package/{lib → packages/selector/lib}/index.js +0 -0
- /package/{lib → packages/selector/lib}/invariant.d.ts +0 -0
- /package/{lib → packages/selector/lib}/invariant.js +0 -0
- /package/{lib → packages/selector/lib}/profiles.d.ts +0 -0
- /package/{lib → packages/selector/lib}/pruner.d.ts +0 -0
- /package/{lib → packages/selector/lib}/pruner.js +0 -0
- /package/{lib → packages/selector/lib}/savings-integral.js +0 -0
- /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
- /package/{lib → packages/selector/lib}/tool-result.js +0 -0
- /package/{locale → packages/selector/locale}/de.json +0 -0
- /package/{locale → packages/selector/locale}/en.json +0 -0
- /package/{locale → packages/selector/locale}/es.json +0 -0
- /package/{locale → packages/selector/locale}/fr.json +0 -0
- /package/{locale → packages/selector/locale}/it.json +0 -0
- /package/{locale → packages/selector/locale}/ja.json +0 -0
- /package/{locale → packages/selector/locale}/ko.json +0 -0
- /package/{locale → packages/selector/locale}/pt.json +0 -0
- /package/{locale → packages/selector/locale}/ru.json +0 -0
- /package/{locale → packages/selector/locale}/zh.json +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sediment commit clock (plan cci-sedimentation-triage, Phase 3, T3.4).
|
|
3
|
+
*
|
|
4
|
+
* Decides WHEN a prepared card may touch the surface. The predicate is the
|
|
5
|
+
* event-sequence form from the plan (R7/T3.4): commit only in the quiet seam
|
|
6
|
+
* after `step/end` or `turn/end`, never with an in-flight request, never
|
|
7
|
+
* inside a compaction lifecycle, never on the first batch after resume, and
|
|
8
|
+
* yielding exactly once to a freshly landed official checkpoint (spine's
|
|
9
|
+
* seen-seq cooldown, adopted verbatim in behavior).
|
|
10
|
+
*
|
|
11
|
+
* Per-session state lives in `SedimentClock` instances — one agent's cooldown
|
|
12
|
+
* must never leak into another's (same discipline as the engine's session
|
|
13
|
+
* state map).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface CommitContext {
|
|
17
|
+
/** Type of the most recent surface event, e.g. 'turn/end' | 'step/end' | 'request/header'. */
|
|
18
|
+
readonly lastEventType: string | undefined
|
|
19
|
+
/** A request was dispatched for the current step and has not settled. */
|
|
20
|
+
readonly inFlightRequest: boolean
|
|
21
|
+
/** Between `compaction/start` and `compaction/end`. */
|
|
22
|
+
readonly compactionOpen: boolean
|
|
23
|
+
/** Seq of the newest official `compact-checkpoint` on the surface, if any. */
|
|
24
|
+
readonly officialCheckpointSeq: number | undefined
|
|
25
|
+
/** Seq of the official checkpoint this session already yielded to once. */
|
|
26
|
+
readonly seenOfficialCheckpointSeq: number | undefined
|
|
27
|
+
/** First commit window after a cold resume — replay paths are still settling. */
|
|
28
|
+
readonly resumeFirstBatch: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface CommitDecision {
|
|
32
|
+
readonly ok: boolean
|
|
33
|
+
readonly reason: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Pure commit predicate; every exclusion returns a machine-readable reason. */
|
|
37
|
+
export function canCommitNow(context: CommitContext): CommitDecision {
|
|
38
|
+
if (!context.inFlightRequest
|
|
39
|
+
&& context.lastEventType !== 'turn/end'
|
|
40
|
+
&& context.lastEventType !== 'step/end') {
|
|
41
|
+
return { ok: false, reason: 'not-in-quiet-seam' }
|
|
42
|
+
}
|
|
43
|
+
if (context.inFlightRequest) return { ok: false, reason: 'request-in-flight' }
|
|
44
|
+
if (context.compactionOpen) return { ok: false, reason: 'compaction-in-progress' }
|
|
45
|
+
if (context.resumeFirstBatch) return { ok: false, reason: 'resume-first-batch' }
|
|
46
|
+
const official = context.officialCheckpointSeq
|
|
47
|
+
if (official !== undefined && official !== context.seenOfficialCheckpointSeq) {
|
|
48
|
+
return { ok: false, reason: `yield-official-checkpoint-${official}` }
|
|
49
|
+
}
|
|
50
|
+
return { ok: true, reason: 'quiet-seam' }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Mutable per-session clock: cooldown consumption + re-entrance guard. */
|
|
54
|
+
export class SedimentClock {
|
|
55
|
+
private warming = false
|
|
56
|
+
private seenOfficialSeq: number | undefined
|
|
57
|
+
|
|
58
|
+
/** True while a commit job is running for this session (no stacked cuts). */
|
|
59
|
+
get busy(): boolean {
|
|
60
|
+
return this.warming
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Claim the commit slot; false means another job owns this session's seam. */
|
|
64
|
+
begin(): boolean {
|
|
65
|
+
if (this.warming) return false
|
|
66
|
+
this.warming = true
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
end(): void {
|
|
71
|
+
this.warming = false
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Consume the one-time yield for the given official checkpoint seq. */
|
|
75
|
+
noteOfficialConsumed(seq: number): void {
|
|
76
|
+
this.seenOfficialSeq = seq
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get seenOfficial(): number | undefined {
|
|
80
|
+
return this.seenOfficialSeq
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Decide with this clock's cooldown state folded in. */
|
|
84
|
+
decide(base: Omit<CommitContext, 'seenOfficialCheckpointSeq'>): CommitDecision {
|
|
85
|
+
return canCommitNow({ ...base, seenOfficialCheckpointSeq: this.seenOfficialSeq })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PTC downgrade guard (plan cci-sedimentation-triage, Phase 3, T3.10/T3.11).
|
|
3
|
+
*
|
|
4
|
+
* Two decisions live here as pure functions, unit-tested before any wiring:
|
|
5
|
+
* 1. PTC-mode detection — registration truth via the omnibus tool names
|
|
6
|
+
* (`run_code` on 0.1.x, `code_run` on 0.2.x), never wire literal guessing;
|
|
7
|
+
* 2. the negative-feedback loop — when net savings stay negative across K
|
|
8
|
+
* request boundaries (the community 1:15 / HUD -24,035 shape), fall back
|
|
9
|
+
* to conservative mode: aging decisions OFF, only fresh large-block cuts.
|
|
10
|
+
*
|
|
11
|
+
* The WRITE_TOOLS blindness probe encodes the root cause directly: in PTC,
|
|
12
|
+
* write operations fold into run_code bodies, so the regex-based read-state
|
|
13
|
+
* predicate sees zero hits and its "superseded" verdicts are untrustworthy.
|
|
14
|
+
*/
|
|
15
|
+
import type { SedimentSettings } from './types.ts'
|
|
16
|
+
|
|
17
|
+
/** Omnibus tool names that mark a PTC/code-mode session (both host lines). */
|
|
18
|
+
export const OMNIBUS_TOOL_NAMES: readonly string[] = Object.freeze(['run_code', 'code_run'])
|
|
19
|
+
|
|
20
|
+
/** Conservative fallback mode: aging-style decisions disabled. */
|
|
21
|
+
export interface ConservativeMode {
|
|
22
|
+
readonly conservative: boolean
|
|
23
|
+
readonly reason: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** True when the registered tool surface contains an omnibus runner. */
|
|
27
|
+
export function detectPtcMode(hasTool: (name: string) => boolean): boolean {
|
|
28
|
+
return OMNIBUS_TOOL_NAMES.some(name => hasTool(name))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* read-state blindness probe: with PTC active and ZERO write-tool regex hits
|
|
33
|
+
* across the observed calls, the superseded predicate cannot see real write
|
|
34
|
+
* targets — aging decisions must be considered untrustworthy.
|
|
35
|
+
*/
|
|
36
|
+
export function readStateBlind(ptcActive: boolean, writeRegexHits: number, observedCalls: number): boolean {
|
|
37
|
+
if (!ptcActive) return false
|
|
38
|
+
return observedCalls > 0 && writeRegexHits === 0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface NetSample {
|
|
42
|
+
readonly grossSaved: number
|
|
43
|
+
readonly recallOffset: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Feedback loop state: consumes request-boundary samples and decides whether
|
|
48
|
+
* sediment/aging operation should run in conservative mode. K consecutive
|
|
49
|
+
* negative-net samples arm it; a positive net sample disarms immediately.
|
|
50
|
+
*/
|
|
51
|
+
export class FeedbackLoop {
|
|
52
|
+
private negativeStreak = 0
|
|
53
|
+
private armed = false
|
|
54
|
+
|
|
55
|
+
constructor(private readonly k: number = 5) {}
|
|
56
|
+
|
|
57
|
+
get conservative(): boolean {
|
|
58
|
+
return this.armed
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
observe(sample: NetSample): ConservativeMode {
|
|
62
|
+
const net = sample.grossSaved - sample.recallOffset
|
|
63
|
+
if (net < 0) {
|
|
64
|
+
this.negativeStreak += 1
|
|
65
|
+
if (this.negativeStreak >= this.k) this.armed = true
|
|
66
|
+
} else if (net > 0) {
|
|
67
|
+
this.negativeStreak = 0
|
|
68
|
+
this.armed = false
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
conservative: this.armed,
|
|
72
|
+
reason: this.armed ? `net-negative-x${this.negativeStreak}` : 'ok',
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Watcher adapter (T3.11 consumption point): the savings ledger reports
|
|
79
|
+
* CUMULATIVE gross/offsets; the feedback loop needs per-boundary NET deltas.
|
|
80
|
+
* This wrapper remembers the last reading and feeds the loop the difference.
|
|
81
|
+
*/
|
|
82
|
+
export class SedimentFeedbackWatcher {
|
|
83
|
+
private last: { gross: number; offsets: number } = { gross: 0, offsets: 0 }
|
|
84
|
+
|
|
85
|
+
constructor(private readonly loop: FeedbackLoop = new FeedbackLoop(5)) {}
|
|
86
|
+
|
|
87
|
+
observe(totals: { gross: { exact: number, estimated: number }, offsets: { exact: number, estimated: number } }): ConservativeMode {
|
|
88
|
+
const gross = totals.gross.exact + totals.gross.estimated
|
|
89
|
+
const offsets = totals.offsets.exact + totals.offsets.estimated
|
|
90
|
+
const sample = {
|
|
91
|
+
grossSaved: gross - this.last.gross,
|
|
92
|
+
recallOffset: offsets - this.last.offsets,
|
|
93
|
+
}
|
|
94
|
+
this.last = { gross, offsets }
|
|
95
|
+
return this.loop.observe(sample)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get conservative(): boolean {
|
|
99
|
+
return this.loop.conservative
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Effective sediment behavior for one session after the PTC guard is folded
|
|
105
|
+
* in: in PTC mode, aging-style decisions short-circuit and the rewrite guard
|
|
106
|
+
* ratio tightens; the card pipeline itself (mask/pool/pointer) is unaffected.
|
|
107
|
+
*/
|
|
108
|
+
export function effectiveSedimentMode(
|
|
109
|
+
settings: SedimentSettings,
|
|
110
|
+
ptcActive: boolean,
|
|
111
|
+
blindReadState: boolean,
|
|
112
|
+
feedback: ConservativeMode,
|
|
113
|
+
): {
|
|
114
|
+
readonly enabled: boolean
|
|
115
|
+
readonly agingAllowed: boolean
|
|
116
|
+
readonly rewriteGuardRatio: number
|
|
117
|
+
readonly reason: string
|
|
118
|
+
} {
|
|
119
|
+
if (!settings.enabled) {
|
|
120
|
+
return { enabled: false, agingAllowed: false, rewriteGuardRatio: settings.rewriteGuardRatio, reason: 'disabled' }
|
|
121
|
+
}
|
|
122
|
+
const agingAllowed = !(ptcActive && blindReadState) && !feedback.conservative
|
|
123
|
+
const rewriteGuardRatio = ptcActive || feedback.conservative
|
|
124
|
+
? Math.max(settings.rewriteGuardRatio, 5)
|
|
125
|
+
: settings.rewriteGuardRatio
|
|
126
|
+
return {
|
|
127
|
+
enabled: true,
|
|
128
|
+
agingAllowed,
|
|
129
|
+
rewriteGuardRatio,
|
|
130
|
+
reason: feedback.conservative
|
|
131
|
+
? `conservative:${feedback.reason}`
|
|
132
|
+
: ptcActive && blindReadState
|
|
133
|
+
? 'ptc-blind-aging-off'
|
|
134
|
+
: 'normal',
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sediment publication (plan cci-sedimentation-triage, Phase 3, T3.5/T3.6).
|
|
3
|
+
*
|
|
4
|
+
* The commit transaction mirrors the proven TailTrim publication shape
|
|
5
|
+
* (pruner.ts L2386-2406): a `compaction/prune` manifest event, then ONE
|
|
6
|
+
* `user/message` replacement carrying `surfaceOp: replace` and
|
|
7
|
+
* `sourceEventSeqs`, through the generation-gated message builder so 0.1.x
|
|
8
|
+
* and 0.2.x hosts accept the same code path. The shortening proof (M7) runs
|
|
9
|
+
* BEFORE any append — a card that does not shrink the span is refused, and
|
|
10
|
+
* nothing half-committed can exist.
|
|
11
|
+
*
|
|
12
|
+
* The session is injected as a minimal interface so the transaction is
|
|
13
|
+
* unit-testable without a host; the wiring site passes the real session.
|
|
14
|
+
*/
|
|
15
|
+
import { tailTrimMessage } from './tail-trim.ts'
|
|
16
|
+
import { renderTurnCard } from './sediment-card.ts'
|
|
17
|
+
import type { TurnCardDraft } from './triage/types.ts'
|
|
18
|
+
|
|
19
|
+
export interface PublishTarget {
|
|
20
|
+
append(type: 'compaction/prune', data: Record<string, unknown>): { seq: number }
|
|
21
|
+
append(type: 'user/message', message: unknown, options?: Record<string, unknown>): { seq: number }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PublishResult {
|
|
25
|
+
readonly ok: boolean
|
|
26
|
+
readonly reason: string
|
|
27
|
+
readonly manifestSeq?: number
|
|
28
|
+
readonly replacementSeq?: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface PublishSpan {
|
|
32
|
+
readonly startSeq: number
|
|
33
|
+
readonly endSeq: number
|
|
34
|
+
readonly shadowedSeqs: readonly number[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Commit one card over the span. Never throws; every failure is a reason code. */
|
|
38
|
+
export function publishSettledCard(
|
|
39
|
+
session: PublishTarget,
|
|
40
|
+
draft: TurnCardDraft,
|
|
41
|
+
span: PublishSpan,
|
|
42
|
+
): PublishResult {
|
|
43
|
+
if (draft.retainedTokens >= draft.sourceTokens) {
|
|
44
|
+
return { ok: false, reason: `shortening-proof-failed:${draft.retainedTokens}>=${draft.sourceTokens}` }
|
|
45
|
+
}
|
|
46
|
+
if (span.shadowedSeqs.length === 0) {
|
|
47
|
+
return { ok: false, reason: 'empty-span' }
|
|
48
|
+
}
|
|
49
|
+
const manifest = session.append('compaction/prune', {
|
|
50
|
+
shadowedRange: { start: span.startSeq, end: span.endSeq },
|
|
51
|
+
shadowedSeqs: [...span.shadowedSeqs],
|
|
52
|
+
shadowedTokenCount: draft.sourceTokens,
|
|
53
|
+
})
|
|
54
|
+
let replacementSeq: number
|
|
55
|
+
try {
|
|
56
|
+
const message = tailTrimMessage(renderTurnCard(draft))
|
|
57
|
+
replacementSeq = session.append('user/message', message, {
|
|
58
|
+
surfaceOp: { op: 'replace', startSeq: span.startSeq, endSeq: span.endSeq },
|
|
59
|
+
sourceEventSeqs: [manifest.seq, ...span.shadowedSeqs],
|
|
60
|
+
}).seq
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// The manifest stands alone (same tolerated state as TailTrim publication
|
|
63
|
+
// failure: an append without replacement is inert on the surface).
|
|
64
|
+
return { ok: false, reason: `publication-failed:${error instanceof Error ? error.message : String(error)}`, manifestSeq: manifest.seq }
|
|
65
|
+
}
|
|
66
|
+
return { ok: true, reason: 'landed', manifestSeq: manifest.seq, replacementSeq }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Rewrite guard (T3.3 accounting, findings F2 community 1:15 evidence):
|
|
71
|
+
* a cut may only land when its saving clears the estimated cached-tail
|
|
72
|
+
* rewrite cost by the configured ratio. Pure function over token counts.
|
|
73
|
+
*/
|
|
74
|
+
export function passesRewriteGuard(
|
|
75
|
+
draft: TurnCardDraft,
|
|
76
|
+
rewriteTailTokens: number,
|
|
77
|
+
ratio: number,
|
|
78
|
+
): boolean {
|
|
79
|
+
const saving = draft.sourceTokens - draft.retainedTokens
|
|
80
|
+
return saving > 0 && saving >= rewriteTailTokens * ratio
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Project a host session event into the minimal `SurfaceEvent` the span
|
|
85
|
+
* planner consumes. Kept separate from the planner so the planner stays
|
|
86
|
+
* host-type-free; the wiring site supplies the call-id → name/args map.
|
|
87
|
+
*/
|
|
88
|
+
export function projectSurfaceEvent(
|
|
89
|
+
seq: number,
|
|
90
|
+
event: {
|
|
91
|
+
type: string
|
|
92
|
+
data?: {
|
|
93
|
+
message?: { content?: unknown; toolCallId?: string; isError?: boolean }
|
|
94
|
+
content?: unknown
|
|
95
|
+
reason?: { kind?: string }
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
callInfo: { name: string; argsJson: string } | undefined,
|
|
99
|
+
): import('./sediment-span.ts').SurfaceEvent | null {
|
|
100
|
+
const text = flattenText(event.data?.message?.content ?? event.data?.content)
|
|
101
|
+
switch (event.type) {
|
|
102
|
+
case 'user/message':
|
|
103
|
+
return { seq, type: 'user/message', text }
|
|
104
|
+
case 'assistant/message': {
|
|
105
|
+
const toolCalls = countToolCallBlocks(event.data?.message?.content)
|
|
106
|
+
return { seq, type: 'assistant/message', text: stripToolCallBlocks(text), toolCalls }
|
|
107
|
+
}
|
|
108
|
+
case 'tool/result':
|
|
109
|
+
return {
|
|
110
|
+
seq,
|
|
111
|
+
type: 'tool/result',
|
|
112
|
+
text,
|
|
113
|
+
...(callInfo === undefined ? {} : { toolName: callInfo.name, argsJson: callInfo.argsJson }),
|
|
114
|
+
isError: event.data?.message?.isError === true,
|
|
115
|
+
}
|
|
116
|
+
case 'turn/end': {
|
|
117
|
+
const reasonKind = event.data?.reason?.kind
|
|
118
|
+
return { seq, type: 'turn/end', text: '', ...(reasonKind === undefined ? {} : { reasonKind }) }
|
|
119
|
+
}
|
|
120
|
+
default:
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function blockText(block: unknown): string {
|
|
126
|
+
if (block === null || typeof block !== 'object') return ''
|
|
127
|
+
const record = block as Record<string, unknown>
|
|
128
|
+
return typeof record.text === 'string' ? record.text : ''
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function flattenText(content: unknown): string {
|
|
132
|
+
if (typeof content === 'string') return content
|
|
133
|
+
if (Array.isArray(content)) return content.map(blockText).filter(entry => entry !== '').join('\n')
|
|
134
|
+
return ''
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function countToolCallBlocks(content: unknown): number {
|
|
138
|
+
if (!Array.isArray(content)) return 0
|
|
139
|
+
return content.filter(block => (block as Record<string, unknown> | null)?.type === 'tool-call').length
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function stripToolCallBlocks(text: string): string {
|
|
143
|
+
return text // blocks already filtered by blockText (tool-call blocks carry no .text)
|
|
144
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whole-turn span planning (plan cci-sedimentation-triage, Phase 3, T3.3).
|
|
3
|
+
*
|
|
4
|
+
* Pure function over a minimal projected event list (the wiring site adapts
|
|
5
|
+
* real session events to `SurfaceEvent`). Decisions made here:
|
|
6
|
+
* - reason gate (R13①): only turns that ended `completed` are replaceable;
|
|
7
|
+
* - tail-kept region (R13③): the last `tailRoundsKept` tool rounds stay
|
|
8
|
+
* verbatim on the surface — the replacement span ends before them;
|
|
9
|
+
* - closing extraction: the last tool-call-free assistant text is the
|
|
10
|
+
* candidate closing body for the qualification gate;
|
|
11
|
+
* - the span covers contiguous assistant/tool events after the turn's user
|
|
12
|
+
* message; a span with no maskable/replaceable block returns null.
|
|
13
|
+
*
|
|
14
|
+
* Turn boundaries make cross-turn pairing checks unnecessary (rounds close
|
|
15
|
+
* inside their turn), which is the whole-turn granularity's main simplification.
|
|
16
|
+
*/
|
|
17
|
+
import type { TriageResult } from './triage/types.ts'
|
|
18
|
+
import { blockFailureVerdict, linePool, type LineRule } from './triage/signals.ts'
|
|
19
|
+
import { manifestFromCall, skeletonFromRead } from './triage/masks.ts'
|
|
20
|
+
import { estimateTokens } from './sediment-card.ts'
|
|
21
|
+
|
|
22
|
+
/** Minimal projected event the wiring site must provide per surface node. */
|
|
23
|
+
export interface SurfaceEvent {
|
|
24
|
+
readonly seq: number
|
|
25
|
+
readonly type: 'user/message' | 'assistant/message' | 'tool/result' | 'turn/end'
|
|
26
|
+
readonly text: string
|
|
27
|
+
/** tool-call block count on an assistant message (0 otherwise). */
|
|
28
|
+
readonly toolCalls?: number
|
|
29
|
+
/** Resolved tool name for a tool/result (call-id lookup done by the wiring site). */
|
|
30
|
+
readonly toolName?: string
|
|
31
|
+
/** Raw arguments JSON for the call owning a tool/result. */
|
|
32
|
+
readonly argsJson?: string
|
|
33
|
+
readonly isError?: boolean
|
|
34
|
+
/** `turn/end` reason kind, e.g. 'completed' | 'interrupted'. */
|
|
35
|
+
readonly reasonKind?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TurnSpanPlan {
|
|
39
|
+
readonly completed: boolean
|
|
40
|
+
/** Inclusive replacement span; null when nothing is replaceable. */
|
|
41
|
+
readonly startSeq: number | null
|
|
42
|
+
readonly endSeq: number | null
|
|
43
|
+
/** Events inside the span (assistant/tool), in order. */
|
|
44
|
+
readonly blocks: readonly SurfaceEvent[]
|
|
45
|
+
/** The turn's closing body (last tool-call-free assistant text after the span). */
|
|
46
|
+
readonly closingText: string
|
|
47
|
+
/** Kept-verbatim seqs: user messages, tail rounds, closing — never in the span. */
|
|
48
|
+
readonly keptSeqs: readonly number[]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface TurnSpanOptions {
|
|
52
|
+
readonly tailRoundsKept?: number
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface Round { readonly start: number; readonly end: number }
|
|
56
|
+
|
|
57
|
+
/** Close tool rounds over the projected events (open-stack, same shape as the replay harness). */
|
|
58
|
+
function closeRounds(events: readonly SurfaceEvent[]): Round[] {
|
|
59
|
+
const rounds: Round[] = []
|
|
60
|
+
let open = 0
|
|
61
|
+
let start = -1
|
|
62
|
+
for (const event of events) {
|
|
63
|
+
const calls = event.type === 'assistant/message' ? event.toolCalls ?? 0 : 0
|
|
64
|
+
if (calls > 0) {
|
|
65
|
+
if (open === 0) start = events.indexOf(event)
|
|
66
|
+
open += calls
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
if (event.type !== 'tool/result') continue
|
|
70
|
+
if (open <= 0) continue
|
|
71
|
+
open -= 1
|
|
72
|
+
if (open === 0 && start >= 0) {
|
|
73
|
+
rounds.push({ start, end: events.indexOf(event) })
|
|
74
|
+
start = -1
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return rounds
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Plan the replacement span for one turn's projected events. */
|
|
81
|
+
export function planTurnSpan(events: readonly SurfaceEvent[], options: TurnSpanOptions = {}): TurnSpanPlan {
|
|
82
|
+
const tailRoundsKept = options.tailRoundsKept ?? 1
|
|
83
|
+
const turnEnd = events.find(event => event.type === 'turn/end')
|
|
84
|
+
const completed = turnEnd?.reasonKind === 'completed'
|
|
85
|
+
const body = events.filter(event => event.type !== 'turn/end')
|
|
86
|
+
const keptSeqs: number[] = body.filter(event => event.type === 'user/message').map(event => event.seq)
|
|
87
|
+
|
|
88
|
+
const rounds = closeRounds(body)
|
|
89
|
+
// Closing body: last assistant text without tool-calls, after the last closed round.
|
|
90
|
+
const lastRoundEnd = rounds.length > 0 ? (rounds[rounds.length - 1] as Round).end : -1
|
|
91
|
+
let closingText = ''
|
|
92
|
+
let closingIndex = -1
|
|
93
|
+
for (let index = body.length - 1; index > lastRoundEnd; index -= 1) {
|
|
94
|
+
const event = body[index]
|
|
95
|
+
if (event === undefined) continue
|
|
96
|
+
if (event.type === 'assistant/message' && (event.toolCalls ?? 0) === 0 && event.text.trim() !== '') {
|
|
97
|
+
closingText = event.text.trim()
|
|
98
|
+
closingIndex = index
|
|
99
|
+
keptSeqs.push(event.seq)
|
|
100
|
+
break
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Tail-kept rounds: last `tailRoundsKept` rounds stay verbatim.
|
|
105
|
+
const keptRounds = tailRoundsKept > 0 ? rounds.slice(Math.max(0, rounds.length - tailRoundsKept)) : []
|
|
106
|
+
for (const round of keptRounds) {
|
|
107
|
+
for (let index = round.start; index <= round.end; index += 1) {
|
|
108
|
+
const event = body[index]
|
|
109
|
+
if (event !== undefined) keptSeqs.push(event.seq)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Replacement span: from the first replaceable event up to the kept boundary.
|
|
114
|
+
// The closing body is never inside the span (it stays verbatim on the surface).
|
|
115
|
+
const firstIndex = body.findIndex(event => event.type === 'assistant/message' || event.type === 'tool/result')
|
|
116
|
+
const keptStartIndex = keptRounds.length > 0
|
|
117
|
+
? (keptRounds[0] as Round).start
|
|
118
|
+
: closingIndex >= 0 ? closingIndex : body.length
|
|
119
|
+
const spanEndIndex = Math.max(firstIndex, keptStartIndex) - 1
|
|
120
|
+
const replaceable = firstIndex >= 0 && spanEndIndex >= firstIndex
|
|
121
|
+
const blocks = replaceable ? body.slice(firstIndex, spanEndIndex + 1) : []
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
completed,
|
|
125
|
+
startSeq: replaceable ? (blocks[0] as SurfaceEvent).seq : null,
|
|
126
|
+
endSeq: replaceable ? (blocks[blocks.length - 1] as SurfaceEvent).seq : null,
|
|
127
|
+
blocks,
|
|
128
|
+
closingText,
|
|
129
|
+
keptSeqs,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Triage every block of a planned span into the frozen TriageResult shape. */
|
|
134
|
+
export function triageSpanBlocks(
|
|
135
|
+
plan: TurnSpanPlan,
|
|
136
|
+
rules: readonly LineRule[],
|
|
137
|
+
): TriageResult[] {
|
|
138
|
+
return plan.blocks.map(event => {
|
|
139
|
+
const sourceTokens = estimateTokens(event.text)
|
|
140
|
+
if (event.type === 'tool/result') {
|
|
141
|
+
const verdict = blockFailureVerdict(event.text, event.isError === true)
|
|
142
|
+
const poolLines = linePool(event.text, {
|
|
143
|
+
seq: event.seq,
|
|
144
|
+
role: 'tool',
|
|
145
|
+
hardFailure: verdict.hard,
|
|
146
|
+
extraPatterns: rules,
|
|
147
|
+
})
|
|
148
|
+
const mask = manifestFromCall(event.toolName ?? '', event.argsJson ?? '', event.text, event.seq)
|
|
149
|
+
?? (event.toolName !== undefined && /^(read|read_file|view)$/i.test(event.toolName)
|
|
150
|
+
? skeletonFromRead(event.text, event.seq)
|
|
151
|
+
: null)
|
|
152
|
+
const residualNarrative = poolLines.length === 0 && mask === null && event.text.trim() !== ''
|
|
153
|
+
? [{ seq: event.seq, role: 'tool' as const, tokens: sourceTokens, text: event.text }]
|
|
154
|
+
: []
|
|
155
|
+
return { poolLines, ...(mask === null ? {} : { mask }), residualNarrative, sourceTokens }
|
|
156
|
+
}
|
|
157
|
+
// Assistant text inside the span: pool-check it (assertion lines occur here too).
|
|
158
|
+
const verdict = blockFailureVerdict(event.text, false)
|
|
159
|
+
const poolLines = linePool(event.text, {
|
|
160
|
+
seq: event.seq,
|
|
161
|
+
role: 'assistant',
|
|
162
|
+
hardFailure: verdict.hard,
|
|
163
|
+
extraPatterns: rules,
|
|
164
|
+
})
|
|
165
|
+
const residualNarrative = poolLines.length === 0
|
|
166
|
+
? [{ seq: event.seq, role: 'assistant' as const, tokens: sourceTokens, text: event.text }]
|
|
167
|
+
: []
|
|
168
|
+
return { poolLines, residualNarrative, sourceTokens }
|
|
169
|
+
})
|
|
170
|
+
}
|