open-multi-agent-kit 0.90.0 → 0.90.4
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 +56 -0
- package/README.md +22 -31
- package/dist/cli/args.d.ts +0 -2
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +1 -7
- package/dist/cli/args.js.map +1 -1
- package/dist/config.d.ts +3 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +27 -19
- package/dist/config.js.map +1 -1
- package/dist/core/adaptorch-bridge.d.ts +257 -0
- package/dist/core/adaptorch-bridge.d.ts.map +1 -0
- package/dist/core/adaptorch-bridge.js +473 -0
- package/dist/core/adaptorch-bridge.js.map +1 -0
- package/dist/core/agent-session.d.ts +132 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +327 -5
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/bang-skill-invocation.d.ts +26 -0
- package/dist/core/bang-skill-invocation.d.ts.map +1 -0
- package/dist/core/bang-skill-invocation.js +208 -0
- package/dist/core/bang-skill-invocation.js.map +1 -0
- package/dist/core/compaction/utils.d.ts.map +1 -1
- package/dist/core/compaction/utils.js +15 -2
- package/dist/core/compaction/utils.js.map +1 -1
- package/dist/core/compactor.d.ts.map +1 -1
- package/dist/core/compactor.js +0 -3
- package/dist/core/compactor.js.map +1 -1
- package/dist/core/context-budget-governor-v2.d.ts +2 -1
- package/dist/core/context-budget-governor-v2.d.ts.map +1 -1
- package/dist/core/context-budget-governor-v2.js +1 -0
- package/dist/core/context-budget-governor-v2.js.map +1 -1
- package/dist/core/context-budget-headroom-types.d.ts +8 -0
- package/dist/core/context-budget-headroom-types.d.ts.map +1 -1
- package/dist/core/context-budget-headroom-types.js.map +1 -1
- package/dist/core/context-budget-headroom.d.ts +1 -1
- package/dist/core/context-budget-headroom.d.ts.map +1 -1
- package/dist/core/context-budget-headroom.js +17 -4
- package/dist/core/context-budget-headroom.js.map +1 -1
- package/dist/core/context-budget-system-prompt.d.ts +2 -1
- package/dist/core/context-budget-system-prompt.d.ts.map +1 -1
- package/dist/core/context-budget-system-prompt.js +6 -1
- package/dist/core/context-budget-system-prompt.js.map +1 -1
- package/dist/core/context-budget-v2-cache-hash.d.ts +3 -0
- package/dist/core/context-budget-v2-cache-hash.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache-hash.js +16 -0
- package/dist/core/context-budget-v2-cache-hash.js.map +1 -0
- package/dist/core/context-budget-v2-cache-keys.d.ts +58 -0
- package/dist/core/context-budget-v2-cache-keys.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache-keys.js +128 -0
- package/dist/core/context-budget-v2-cache-keys.js.map +1 -0
- package/dist/core/context-budget-v2-cache-provider.d.ts +23 -0
- package/dist/core/context-budget-v2-cache-provider.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache-provider.js +29 -0
- package/dist/core/context-budget-v2-cache-provider.js.map +1 -0
- package/dist/core/context-budget-v2-cache-telemetry.d.ts +36 -0
- package/dist/core/context-budget-v2-cache-telemetry.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache-telemetry.js +64 -0
- package/dist/core/context-budget-v2-cache-telemetry.js.map +1 -0
- package/dist/core/context-budget-v2-cache-validation.d.ts +36 -0
- package/dist/core/context-budget-v2-cache-validation.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache-validation.js +98 -0
- package/dist/core/context-budget-v2-cache-validation.js.map +1 -0
- package/dist/core/context-budget-v2-cache.d.ts +36 -0
- package/dist/core/context-budget-v2-cache.d.ts.map +1 -0
- package/dist/core/context-budget-v2-cache.js +50 -0
- package/dist/core/context-budget-v2-cache.js.map +1 -0
- package/dist/core/context-budget-v2-plan-cache-keys.d.ts +16 -0
- package/dist/core/context-budget-v2-plan-cache-keys.d.ts.map +1 -0
- package/dist/core/context-budget-v2-plan-cache-keys.js +49 -0
- package/dist/core/context-budget-v2-plan-cache-keys.js.map +1 -0
- package/dist/core/context-budget-v2-planner.d.ts.map +1 -1
- package/dist/core/context-budget-v2-planner.js +55 -2
- package/dist/core/context-budget-v2-planner.js.map +1 -1
- package/dist/core/context-budget-v2-representation-cache.d.ts +27 -0
- package/dist/core/context-budget-v2-representation-cache.d.ts.map +1 -0
- package/dist/core/context-budget-v2-representation-cache.js +173 -0
- package/dist/core/context-budget-v2-representation-cache.js.map +1 -0
- package/dist/core/context-budget-v2-selection.d.ts +2 -0
- package/dist/core/context-budget-v2-selection.d.ts.map +1 -1
- package/dist/core/context-budget-v2-selection.js +12 -2
- package/dist/core/context-budget-v2-selection.js.map +1 -1
- package/dist/core/context-budget-v2-types.d.ts +107 -1
- package/dist/core/context-budget-v2-types.d.ts.map +1 -1
- package/dist/core/context-budget-v2-types.js.map +1 -1
- package/dist/core/domain-loadouts.d.ts.map +1 -1
- package/dist/core/domain-loadouts.js +1 -1
- package/dist/core/domain-loadouts.js.map +1 -1
- package/dist/core/headroom.d.ts.map +1 -1
- package/dist/core/headroom.js +0 -4
- package/dist/core/headroom.js.map +1 -1
- package/dist/core/hook-inventory.d.ts.map +1 -1
- package/dist/core/hook-inventory.js +6 -0
- package/dist/core/hook-inventory.js.map +1 -1
- package/dist/core/mcp-computer-use-presets.d.ts +64 -0
- package/dist/core/mcp-computer-use-presets.d.ts.map +1 -0
- package/dist/core/mcp-computer-use-presets.js +158 -0
- package/dist/core/mcp-computer-use-presets.js.map +1 -0
- package/dist/core/mcp-public-presets.d.ts +578 -0
- package/dist/core/mcp-public-presets.d.ts.map +1 -0
- package/dist/core/mcp-public-presets.js +647 -0
- package/dist/core/mcp-public-presets.js.map +1 -0
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +1 -0
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/pi-package-intake-candidates.d.ts +14 -1
- package/dist/core/pi-package-intake-candidates.d.ts.map +1 -1
- package/dist/core/pi-package-intake-candidates.js +182 -0
- package/dist/core/pi-package-intake-candidates.js.map +1 -1
- package/dist/core/pi-package-intake.d.ts +8 -1
- package/dist/core/pi-package-intake.d.ts.map +1 -1
- package/dist/core/pi-package-intake.js +14 -4
- package/dist/core/pi-package-intake.js.map +1 -1
- package/dist/core/reasoning-router-bias.d.ts +119 -0
- package/dist/core/reasoning-router-bias.d.ts.map +1 -0
- package/dist/core/reasoning-router-bias.js +287 -0
- package/dist/core/reasoning-router-bias.js.map +1 -0
- package/dist/core/reasoning-router-v2.d.ts +94 -0
- package/dist/core/reasoning-router-v2.d.ts.map +1 -0
- package/dist/core/reasoning-router-v2.js +288 -0
- package/dist/core/reasoning-router-v2.js.map +1 -0
- package/dist/core/reasoning-router-v3.d.ts +15 -0
- package/dist/core/reasoning-router-v3.d.ts.map +1 -0
- package/dist/core/reasoning-router-v3.js +193 -0
- package/dist/core/reasoning-router-v3.js.map +1 -0
- package/dist/core/reasoning-router-v4-weights.d.ts +124 -0
- package/dist/core/reasoning-router-v4-weights.d.ts.map +1 -0
- package/dist/core/reasoning-router-v4-weights.js +112 -0
- package/dist/core/reasoning-router-v4-weights.js.map +1 -0
- package/dist/core/reasoning-router-v4.d.ts +163 -0
- package/dist/core/reasoning-router-v4.d.ts.map +1 -0
- package/dist/core/reasoning-router-v4.js +683 -0
- package/dist/core/reasoning-router-v4.js.map +1 -0
- package/dist/core/reasoning-router-weights.d.ts +155 -0
- package/dist/core/reasoning-router-weights.d.ts.map +1 -0
- package/dist/core/reasoning-router-weights.js +204 -0
- package/dist/core/reasoning-router-weights.js.map +1 -0
- package/dist/core/reasoning-router.d.ts +44 -0
- package/dist/core/reasoning-router.d.ts.map +1 -0
- package/dist/core/reasoning-router.js +138 -0
- package/dist/core/reasoning-router.js.map +1 -0
- package/dist/core/reverse-skill.d.ts +2 -0
- package/dist/core/reverse-skill.d.ts.map +1 -0
- package/dist/core/reverse-skill.js +7 -0
- package/dist/core/reverse-skill.js.map +1 -0
- package/dist/core/router-feedback-collector.d.ts +101 -0
- package/dist/core/router-feedback-collector.d.ts.map +1 -0
- package/dist/core/router-feedback-collector.js +239 -0
- package/dist/core/router-feedback-collector.js.map +1 -0
- package/dist/core/settings-manager.d.ts +31 -3
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +16 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +4 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/system-prompt.d.ts +2 -0
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +58 -5
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migrations.d.ts +19 -0
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +39 -15
- package/dist/migrations.js.map +1 -1
- package/dist/modes/interactive/components/control-panel-box.d.ts +3 -3
- package/dist/modes/interactive/components/control-panel-box.d.ts.map +1 -1
- package/dist/modes/interactive/components/control-panel-box.js +10 -10
- package/dist/modes/interactive/components/control-panel-box.js.map +1 -1
- package/dist/modes/interactive/components/control-panel-gradient-motion.d.ts.map +1 -1
- package/dist/modes/interactive/components/control-panel-gradient-motion.js +2 -1
- package/dist/modes/interactive/components/control-panel-gradient-motion.js.map +1 -1
- package/dist/modes/interactive/components/control-panel-layout.d.ts +15 -1
- package/dist/modes/interactive/components/control-panel-layout.d.ts.map +1 -1
- package/dist/modes/interactive/components/control-panel-layout.js +212 -68
- package/dist/modes/interactive/components/control-panel-layout.js.map +1 -1
- package/dist/modes/interactive/components/control-panel-runtime-status.d.ts +15 -0
- package/dist/modes/interactive/components/control-panel-runtime-status.d.ts.map +1 -1
- package/dist/modes/interactive/components/control-panel-runtime-status.js +105 -5
- package/dist/modes/interactive/components/control-panel-runtime-status.js.map +1 -1
- package/dist/modes/interactive/components/control-panel-sparkles.d.ts +27 -0
- package/dist/modes/interactive/components/control-panel-sparkles.d.ts.map +1 -0
- package/dist/modes/interactive/components/control-panel-sparkles.js +42 -0
- package/dist/modes/interactive/components/control-panel-sparkles.js.map +1 -0
- package/dist/modes/interactive/components/control-panel.d.ts +8 -0
- package/dist/modes/interactive/components/control-panel.d.ts.map +1 -1
- package/dist/modes/interactive/components/control-panel.js +40 -8
- package/dist/modes/interactive/components/control-panel.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +2 -1
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/thinking-selector.js +2 -1
- package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +6 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +206 -32
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme.d.ts +1 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +2 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/docs/loadout-domains/README.md +8 -6
- package/docs/loadout-domains/ai-agent-ops.md +3 -2
- package/docs/loadout-domains/backend-api.md +3 -2
- package/docs/loadout-domains/data-science.md +3 -2
- package/docs/loadout-domains/devops-infra.md +2 -1
- package/docs/loadout-domains/docs-writing.md +7 -2
- package/docs/loadout-domains/frontend-ui.md +42 -11
- package/docs/loadout-domains/korean-document.md +83 -0
- package/docs/loadout-domains/mobile.md +1 -1
- package/docs/loadout-domains/qa-testing.md +4 -2
- package/docs/loadout-domains/security-audit.md +2 -1
- package/docs/loadout-domains/visual-qa.md +94 -0
- package/docs/skills.md +11 -4
- package/docs/usage.md +39 -4
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/preset.ts +2 -2
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +27 -34
- package/package.json +5 -5
- package/dist/agents/jailbreak/index.d.ts +0 -7
- package/dist/agents/jailbreak/index.d.ts.map +0 -1
- package/dist/agents/jailbreak/index.js +0 -7
- package/dist/agents/jailbreak/index.js.map +0 -1
- package/dist/agents/jailbreak/subagent-f.d.ts +0 -44
- package/dist/agents/jailbreak/subagent-f.d.ts.map +0 -1
- package/dist/agents/jailbreak/subagent-f.js +0 -60
- package/dist/agents/jailbreak/subagent-f.js.map +0 -1
- package/dist/cli/jailbreak-args.d.ts +0 -26
- package/dist/cli/jailbreak-args.d.ts.map +0 -1
- package/dist/cli/jailbreak-args.js +0 -86
- package/dist/cli/jailbreak-args.js.map +0 -1
- package/dist/commands/jailbreak.d.ts +0 -49
- package/dist/commands/jailbreak.d.ts.map +0 -1
- package/dist/commands/jailbreak.js +0 -201
- package/dist/commands/jailbreak.js.map +0 -1
- package/dist/encoding/index.d.ts +0 -6
- package/dist/encoding/index.d.ts.map +0 -1
- package/dist/encoding/index.js +0 -6
- package/dist/encoding/index.js.map +0 -1
- package/dist/encoding/l4-encoder.d.ts +0 -96
- package/dist/encoding/l4-encoder.d.ts.map +0 -1
- package/dist/encoding/l4-encoder.js +0 -474
- package/dist/encoding/l4-encoder.js.map +0 -1
- package/dist/fuzzing/index.d.ts +0 -6
- package/dist/fuzzing/index.d.ts.map +0 -1
- package/dist/fuzzing/index.js +0 -6
- package/dist/fuzzing/index.js.map +0 -1
- package/dist/fuzzing/mutation-engine.d.ts +0 -93
- package/dist/fuzzing/mutation-engine.d.ts.map +0 -1
- package/dist/fuzzing/mutation-engine.js +0 -244
- package/dist/fuzzing/mutation-engine.js.map +0 -1
- package/dist/jailbreak-extension.d.ts +0 -48
- package/dist/jailbreak-extension.d.ts.map +0 -1
- package/dist/jailbreak-extension.js +0 -47
- package/dist/jailbreak-extension.js.map +0 -1
- package/dist/modules/index.d.ts +0 -8
- package/dist/modules/index.d.ts.map +0 -1
- package/dist/modules/index.js +0 -7
- package/dist/modules/index.js.map +0 -1
- package/dist/modules/lrl-bypass.d.ts +0 -40
- package/dist/modules/lrl-bypass.d.ts.map +0 -1
- package/dist/modules/lrl-bypass.js +0 -84
- package/dist/modules/lrl-bypass.js.map +0 -1
- package/dist/modules/lrl-deps.d.ts +0 -38
- package/dist/modules/lrl-deps.d.ts.map +0 -1
- package/dist/modules/lrl-deps.js +0 -128
- package/dist/modules/lrl-deps.js.map +0 -1
- package/dist/multiturn/easl-chain.d.ts +0 -113
- package/dist/multiturn/easl-chain.d.ts.map +0 -1
- package/dist/multiturn/easl-chain.js +0 -351
- package/dist/multiturn/easl-chain.js.map +0 -1
- package/dist/multiturn/index.d.ts +0 -6
- package/dist/multiturn/index.d.ts.map +0 -1
- package/dist/multiturn/index.js +0 -6
- package/dist/multiturn/index.js.map +0 -1
- package/dist/routing/fallback-router.d.ts +0 -123
- package/dist/routing/fallback-router.d.ts.map +0 -1
- package/dist/routing/fallback-router.js +0 -355
- package/dist/routing/fallback-router.js.map +0 -1
- package/dist/routing/index.d.ts +0 -6
- package/dist/routing/index.d.ts.map +0 -1
- package/dist/routing/index.js +0 -6
- package/dist/routing/index.js.map +0 -1
- package/dist/types/jailbreak.d.ts +0 -246
- package/dist/types/jailbreak.d.ts.map +0 -1
- package/dist/types/jailbreak.js +0 -8
- package/dist/types/jailbreak.js.map +0 -1
- package/dist/utils/jailbreak/common.d.ts +0 -63
- package/dist/utils/jailbreak/common.d.ts.map +0 -1
- package/dist/utils/jailbreak/common.js +0 -130
- package/dist/utils/jailbreak/common.js.map +0 -1
- package/dist/utils/jailbreak/index.d.ts +0 -7
- package/dist/utils/jailbreak/index.d.ts.map +0 -1
- package/dist/utils/jailbreak/index.js +0 -7
- package/dist/utils/jailbreak/index.js.map +0 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { sha256Canonical, sha256Hex } from "./context-budget-v2-cache-hash.js";
|
|
2
|
+
const DEFAULT_CACHE_NAMESPACE = "context-budget-v2";
|
|
3
|
+
const DEFAULT_TOKENIZER_ID = "heuristic-v1";
|
|
4
|
+
const DEFAULT_REDACTION_POLICY_HASH = "none";
|
|
5
|
+
const DEFAULT_SAFETY_PROFILE_HASH = "default";
|
|
6
|
+
const NO_COMPRESSOR_ID = "none";
|
|
7
|
+
export const DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2 = "context-budget-v2-default-compressor";
|
|
8
|
+
export const DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2 = "context-budget-v2-default-quality";
|
|
9
|
+
const MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE = 100;
|
|
10
|
+
export function createContextBudgetCacheKeyBaseV2(input) {
|
|
11
|
+
const queryIntentHash = input.queryIntentHash ?? computeContextBudgetQueryIntentHashV2(input.query);
|
|
12
|
+
return {
|
|
13
|
+
namespace: input.namespace ?? DEFAULT_CACHE_NAMESPACE,
|
|
14
|
+
modelId: input.modelId,
|
|
15
|
+
tokenizerId: input.tokenizerId ?? DEFAULT_TOKENIZER_ID,
|
|
16
|
+
policyVersion: input.policyVersion,
|
|
17
|
+
queryIntentHash,
|
|
18
|
+
queryIntentCluster: input.queryIntentCluster ?? queryIntentHash,
|
|
19
|
+
budgetBucket: input.budgetBucket ?? "unknown",
|
|
20
|
+
redactionPolicyHash: input.redactionPolicyHash ?? DEFAULT_REDACTION_POLICY_HASH,
|
|
21
|
+
safetyProfileHash: input.safetyProfileHash ?? DEFAULT_SAFETY_PROFILE_HASH,
|
|
22
|
+
compressorPolicyHash: input.compressorPolicyHash ?? DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2,
|
|
23
|
+
qualityPolicyHash: input.qualityPolicyHash ?? DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function computeContextBudgetQueryIntentHashV2(query) {
|
|
27
|
+
const normalized = (query ?? "")
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.split(/[\s,.;:!?()[\]{}"'`]+/u)
|
|
30
|
+
.map((term) => term.trim())
|
|
31
|
+
.filter((term) => term.length > 0)
|
|
32
|
+
.sort()
|
|
33
|
+
.join(" ");
|
|
34
|
+
return sha256Hex(normalized.length > 0 ? normalized : "none");
|
|
35
|
+
}
|
|
36
|
+
export function buildContextBudgetRepresentationCacheKeyV2(input) {
|
|
37
|
+
return buildContextBudgetExactRepresentationCacheKeyV2(input);
|
|
38
|
+
}
|
|
39
|
+
export function buildContextBudgetExactRepresentationCacheKeyV2(input) {
|
|
40
|
+
return `context-representation-exact:${sha256Canonical({
|
|
41
|
+
budgetBucket: input.budgetBucket,
|
|
42
|
+
compressorId: input.compressorId ?? NO_COMPRESSOR_ID,
|
|
43
|
+
modelId: input.modelId,
|
|
44
|
+
namespace: input.namespace,
|
|
45
|
+
policyVersion: input.policyVersion,
|
|
46
|
+
queryIntentHash: input.queryIntentHash,
|
|
47
|
+
redactionPolicyHash: input.redactionPolicyHash,
|
|
48
|
+
representationFingerprint: input.representationFingerprint ?? "none",
|
|
49
|
+
representationKind: input.representationKind,
|
|
50
|
+
safetyProfileHash: input.safetyProfileHash,
|
|
51
|
+
sourceHash: input.sourceHash,
|
|
52
|
+
tokenizerId: input.tokenizerId,
|
|
53
|
+
})}`;
|
|
54
|
+
}
|
|
55
|
+
export function buildContextBudgetMaterializedRepresentationCacheKeyV2(input) {
|
|
56
|
+
return `context-representation-materialized:${sha256Canonical({
|
|
57
|
+
budgetBucket: input.budgetBucket,
|
|
58
|
+
compressorId: input.compressorId ?? NO_COMPRESSOR_ID,
|
|
59
|
+
compressorPolicyHash: input.compressorPolicyHash ?? DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2,
|
|
60
|
+
modelId: input.modelId,
|
|
61
|
+
namespace: input.namespace,
|
|
62
|
+
policyVersion: input.policyVersion,
|
|
63
|
+
qualityPolicyHash: input.qualityPolicyHash ?? DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2,
|
|
64
|
+
queryIntentCluster: input.queryIntentCluster ?? input.queryIntentHash,
|
|
65
|
+
queryIntentHash: input.queryIntentHash,
|
|
66
|
+
redactionPolicyHash: input.redactionPolicyHash,
|
|
67
|
+
representationKind: input.representationKind,
|
|
68
|
+
safetyProfileHash: input.safetyProfileHash,
|
|
69
|
+
sourceHash: input.sourceHash,
|
|
70
|
+
targetTokenBucket: input.targetTokenBucket,
|
|
71
|
+
tokenizerId: input.tokenizerId,
|
|
72
|
+
})}`;
|
|
73
|
+
}
|
|
74
|
+
export function computeContextBudgetRepresentationFingerprintV2(candidate) {
|
|
75
|
+
return sha256Canonical({
|
|
76
|
+
compressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,
|
|
77
|
+
estimatedTokens: candidate.estimatedTokens,
|
|
78
|
+
fidelity: candidate.fidelity,
|
|
79
|
+
kind: candidate.kind,
|
|
80
|
+
sourceRef: fingerprintSourceRef(candidate.sourceRef),
|
|
81
|
+
summaryHash: candidate.summaryHash ?? null,
|
|
82
|
+
textHash: sha256Hex(candidate.text),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export function buildContextBudgetRepresentationCandidateKeyV2(keyBase, candidate, sourceHash) {
|
|
86
|
+
return buildContextBudgetExactRepresentationCacheKeyV2({
|
|
87
|
+
...keyBase,
|
|
88
|
+
compressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,
|
|
89
|
+
representationFingerprint: computeContextBudgetRepresentationFingerprintV2(candidate),
|
|
90
|
+
representationKind: candidate.kind,
|
|
91
|
+
sourceHash,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export function buildContextBudgetMaterializedRepresentationCandidateKeyV2(keyBase, candidate, sourceHash) {
|
|
95
|
+
return buildContextBudgetMaterializedRepresentationCacheKeyV2({
|
|
96
|
+
...keyBase,
|
|
97
|
+
compressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,
|
|
98
|
+
representationKind: candidate.kind,
|
|
99
|
+
sourceHash,
|
|
100
|
+
targetTokenBucket: computeContextBudgetTargetTokenBucketV2(candidate.estimatedTokens),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export function computeContextBudgetTargetTokenBucketV2(tokens) {
|
|
104
|
+
if (!Number.isFinite(tokens) || tokens <= 0) {
|
|
105
|
+
return MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE;
|
|
106
|
+
}
|
|
107
|
+
return MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE * Math.ceil(tokens / MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE);
|
|
108
|
+
}
|
|
109
|
+
export function computeContextBudgetOutputTextHashV2(text) {
|
|
110
|
+
return sha256Hex(text);
|
|
111
|
+
}
|
|
112
|
+
export function fingerprintSourceRef(sourceRef) {
|
|
113
|
+
if (!sourceRef)
|
|
114
|
+
return null;
|
|
115
|
+
return {
|
|
116
|
+
contentHash: sourceRef.contentHash,
|
|
117
|
+
range: sourceRef.range
|
|
118
|
+
? {
|
|
119
|
+
endLine: sourceRef.range.endLine,
|
|
120
|
+
startLine: sourceRef.range.startLine,
|
|
121
|
+
}
|
|
122
|
+
: null,
|
|
123
|
+
retrievable: sourceRef.retrievable,
|
|
124
|
+
symbol: sourceRef.symbol ?? null,
|
|
125
|
+
uri: sourceRef.uri,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=context-budget-v2-cache-keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-keys.js","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-keys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eA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I;QAClC,UAAU;KACV,CAAC,CAAC;AAAA,CACH;AAED,MAAM,UAAU,0DAA0D,CACzE,OAAoC,EACpC,SAAwD,EACxD,UAAkB,EACT;IACT,OAAO,sDAAsD,CAAC;QAC7D,GAAG,OAAO;QACV,YAAY,EAAE,SAAS,CAAC,YAAY,IAAI,gBAAgB;QACxD,kBAAkB,EAAE,SAAS,CAAC,IAAI;QAClC,UAAU;QACV,iBAAiB,EAAE,uCAAuC,CAAC,SAAS,CAAC,eAAe,CAAC;KACrF,CAAC,CAAC;AAAA,CACH;AAED,MAAM,UAAU,uCAAuC,CAAC,MAAc,EAAU;IAC/E,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAC7C,OAAO,qCAAqC,CAAC;IAC9C,CAAC;IACD,OAAO,qCAAqC,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,qCAAqC,CAAC,CAAC;AAAA,CACzG;AAED,MAAM,UAAU,oCAAoC,CAAC,IAAY,EAAU;IAC1E,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;AAAA,CACvB;AAED,MAAM,UAAU,oBAAoB,CAAC,SAAwD,EAAW;IACvG,IAAI,CAAC,SAAS;QAAE,OAAO,IAAI,CAAC;IAC5B,OAAO;QACN,WAAW,EAAE,SAAS,CAAC,WAAW;QAClC,KAAK,EAAE,SAAS,CAAC,KAAK;YACrB,CAAC,CAAC;gBACA,OAAO,EAAE,SAAS,CAAC,KAAK,CAAC,OAAO;gBAChC,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,SAAS;aACpC;YACF,CAAC,CAAC,IAAI;QACP,WAAW,EAAE,SAAS,CAAC,WAAW;QAClC,MAAM,EAAE,SAAS,CAAC,MAAM,IAAI,IAAI;QAChC,GAAG,EAAE,SAAS,CAAC,GAAG;KAClB,CAAC;AAAA,CACF","sourcesContent":["import type { ContextRepresentationCandidateV2, ContextRepresentationKindV2 } from \"./context-budget-headroom.ts\";\nimport { sha256Canonical, sha256Hex } from \"./context-budget-v2-cache-hash.ts\";\n\nconst DEFAULT_CACHE_NAMESPACE = \"context-budget-v2\";\nconst DEFAULT_TOKENIZER_ID = \"heuristic-v1\";\nconst DEFAULT_REDACTION_POLICY_HASH = \"none\";\nconst DEFAULT_SAFETY_PROFILE_HASH = \"default\";\nconst NO_COMPRESSOR_ID = \"none\";\nexport const DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2 = \"context-budget-v2-default-compressor\";\nexport const DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2 = \"context-budget-v2-default-quality\";\nconst MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE = 100;\n\ntype ContextBudgetRepresentationFingerprintInputV2 = Pick<\n\tContextRepresentationCandidateV2,\n\t\"compressorId\" | \"estimatedTokens\" | \"fidelity\" | \"kind\" | \"sourceRef\" | \"summaryHash\" | \"text\"\n>;\n\nexport interface ContextBudgetCacheKeyBaseV2 {\n\treadonly namespace: string;\n\treadonly modelId: string;\n\treadonly tokenizerId: string;\n\treadonly policyVersion: string;\n\treadonly queryIntentHash: string;\n\treadonly queryIntentCluster?: string;\n\treadonly budgetBucket: string;\n\treadonly redactionPolicyHash: string;\n\treadonly safetyProfileHash: string;\n\treadonly compressorPolicyHash?: string;\n\treadonly qualityPolicyHash?: string;\n}\n\nexport function createContextBudgetCacheKeyBaseV2(input: {\n\treadonly namespace?: string;\n\treadonly modelId: string;\n\treadonly tokenizerId?: string;\n\treadonly policyVersion: string;\n\treadonly query?: string;\n\treadonly queryIntentHash?: string;\n\treadonly queryIntentCluster?: string;\n\treadonly budgetBucket?: string;\n\treadonly redactionPolicyHash?: string;\n\treadonly safetyProfileHash?: string;\n\treadonly compressorPolicyHash?: string;\n\treadonly qualityPolicyHash?: string;\n}): ContextBudgetCacheKeyBaseV2 {\n\tconst queryIntentHash = input.queryIntentHash ?? computeContextBudgetQueryIntentHashV2(input.query);\n\treturn {\n\t\tnamespace: input.namespace ?? DEFAULT_CACHE_NAMESPACE,\n\t\tmodelId: input.modelId,\n\t\ttokenizerId: input.tokenizerId ?? DEFAULT_TOKENIZER_ID,\n\t\tpolicyVersion: input.policyVersion,\n\t\tqueryIntentHash,\n\t\tqueryIntentCluster: input.queryIntentCluster ?? queryIntentHash,\n\t\tbudgetBucket: input.budgetBucket ?? \"unknown\",\n\t\tredactionPolicyHash: input.redactionPolicyHash ?? DEFAULT_REDACTION_POLICY_HASH,\n\t\tsafetyProfileHash: input.safetyProfileHash ?? DEFAULT_SAFETY_PROFILE_HASH,\n\t\tcompressorPolicyHash: input.compressorPolicyHash ?? DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2,\n\t\tqualityPolicyHash: input.qualityPolicyHash ?? DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2,\n\t};\n}\n\nexport function computeContextBudgetQueryIntentHashV2(query: string | undefined): string {\n\tconst normalized = (query ?? \"\")\n\t\t.toLowerCase()\n\t\t.split(/[\\s,.;:!?()[\\]{}\"'`]+/u)\n\t\t.map((term) => term.trim())\n\t\t.filter((term) => term.length > 0)\n\t\t.sort()\n\t\t.join(\" \");\n\treturn sha256Hex(normalized.length > 0 ? normalized : \"none\");\n}\n\nexport function buildContextBudgetRepresentationCacheKeyV2(\n\tinput: ContextBudgetCacheKeyBaseV2 & {\n\t\treadonly representationKind: ContextRepresentationKindV2;\n\t\treadonly sourceHash: string;\n\t\treadonly representationFingerprint?: string;\n\t\treadonly compressorId?: string;\n\t},\n): string {\n\treturn buildContextBudgetExactRepresentationCacheKeyV2(input);\n}\n\nexport function buildContextBudgetExactRepresentationCacheKeyV2(\n\tinput: ContextBudgetCacheKeyBaseV2 & {\n\t\treadonly representationKind: ContextRepresentationKindV2;\n\t\treadonly sourceHash: string;\n\t\treadonly representationFingerprint?: string;\n\t\treadonly compressorId?: string;\n\t},\n): string {\n\treturn `context-representation-exact:${sha256Canonical({\n\t\tbudgetBucket: input.budgetBucket,\n\t\tcompressorId: input.compressorId ?? NO_COMPRESSOR_ID,\n\t\tmodelId: input.modelId,\n\t\tnamespace: input.namespace,\n\t\tpolicyVersion: input.policyVersion,\n\t\tqueryIntentHash: input.queryIntentHash,\n\t\tredactionPolicyHash: input.redactionPolicyHash,\n\t\trepresentationFingerprint: input.representationFingerprint ?? \"none\",\n\t\trepresentationKind: input.representationKind,\n\t\tsafetyProfileHash: input.safetyProfileHash,\n\t\tsourceHash: input.sourceHash,\n\t\ttokenizerId: input.tokenizerId,\n\t})}`;\n}\n\nexport function buildContextBudgetMaterializedRepresentationCacheKeyV2(\n\tinput: ContextBudgetCacheKeyBaseV2 & {\n\t\treadonly representationKind: ContextRepresentationKindV2;\n\t\treadonly sourceHash: string;\n\t\treadonly compressorId?: string;\n\t\treadonly targetTokenBucket: number;\n\t},\n): string {\n\treturn `context-representation-materialized:${sha256Canonical({\n\t\tbudgetBucket: input.budgetBucket,\n\t\tcompressorId: input.compressorId ?? NO_COMPRESSOR_ID,\n\t\tcompressorPolicyHash: input.compressorPolicyHash ?? DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2,\n\t\tmodelId: input.modelId,\n\t\tnamespace: input.namespace,\n\t\tpolicyVersion: input.policyVersion,\n\t\tqualityPolicyHash: input.qualityPolicyHash ?? DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2,\n\t\tqueryIntentCluster: input.queryIntentCluster ?? input.queryIntentHash,\n\t\tqueryIntentHash: input.queryIntentHash,\n\t\tredactionPolicyHash: input.redactionPolicyHash,\n\t\trepresentationKind: input.representationKind,\n\t\tsafetyProfileHash: input.safetyProfileHash,\n\t\tsourceHash: input.sourceHash,\n\t\ttargetTokenBucket: input.targetTokenBucket,\n\t\ttokenizerId: input.tokenizerId,\n\t})}`;\n}\n\nexport function computeContextBudgetRepresentationFingerprintV2(\n\tcandidate: ContextBudgetRepresentationFingerprintInputV2,\n): string {\n\treturn sha256Canonical({\n\t\tcompressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,\n\t\testimatedTokens: candidate.estimatedTokens,\n\t\tfidelity: candidate.fidelity,\n\t\tkind: candidate.kind,\n\t\tsourceRef: fingerprintSourceRef(candidate.sourceRef),\n\t\tsummaryHash: candidate.summaryHash ?? null,\n\t\ttextHash: sha256Hex(candidate.text),\n\t});\n}\n\nexport function buildContextBudgetRepresentationCandidateKeyV2(\n\tkeyBase: ContextBudgetCacheKeyBaseV2,\n\tcandidate: ContextBudgetRepresentationFingerprintInputV2,\n\tsourceHash: string,\n): string {\n\treturn buildContextBudgetExactRepresentationCacheKeyV2({\n\t\t...keyBase,\n\t\tcompressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,\n\t\trepresentationFingerprint: computeContextBudgetRepresentationFingerprintV2(candidate),\n\t\trepresentationKind: candidate.kind,\n\t\tsourceHash,\n\t});\n}\n\nexport function buildContextBudgetMaterializedRepresentationCandidateKeyV2(\n\tkeyBase: ContextBudgetCacheKeyBaseV2,\n\tcandidate: ContextBudgetRepresentationFingerprintInputV2,\n\tsourceHash: string,\n): string {\n\treturn buildContextBudgetMaterializedRepresentationCacheKeyV2({\n\t\t...keyBase,\n\t\tcompressorId: candidate.compressorId ?? NO_COMPRESSOR_ID,\n\t\trepresentationKind: candidate.kind,\n\t\tsourceHash,\n\t\ttargetTokenBucket: computeContextBudgetTargetTokenBucketV2(candidate.estimatedTokens),\n\t});\n}\n\nexport function computeContextBudgetTargetTokenBucketV2(tokens: number): number {\n\tif (!Number.isFinite(tokens) || tokens <= 0) {\n\t\treturn MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE;\n\t}\n\treturn MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE * Math.ceil(tokens / MATERIALIZED_TARGET_TOKEN_BUCKET_SIZE);\n}\n\nexport function computeContextBudgetOutputTextHashV2(text: string): string {\n\treturn sha256Hex(text);\n}\n\nexport function fingerprintSourceRef(sourceRef: ContextRepresentationCandidateV2[\"sourceRef\"]): unknown {\n\tif (!sourceRef) return null;\n\treturn {\n\t\tcontentHash: sourceRef.contentHash,\n\t\trange: sourceRef.range\n\t\t\t? {\n\t\t\t\t\tendLine: sourceRef.range.endLine,\n\t\t\t\t\tstartLine: sourceRef.range.startLine,\n\t\t\t\t}\n\t\t\t: null,\n\t\tretrievable: sourceRef.retrievable,\n\t\tsymbol: sourceRef.symbol ?? null,\n\t\turi: sourceRef.uri,\n\t};\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ContextBudgetCacheProviderV2, ContextBudgetNegativeCacheEntryV2, ContextBudgetPlanCacheEntryV2, ContextBudgetPlanCacheReadV2, ContextBudgetRepresentationCacheEntryV2, ContextBudgetRepresentationCacheReadV2 } from "./context-budget-v2-types.ts";
|
|
2
|
+
export declare function createMemoryContextBudgetCacheProviderV2(): ContextBudgetCacheProviderV2;
|
|
3
|
+
export declare class MemoryContextBudgetCacheProviderV2 implements ContextBudgetCacheProviderV2 {
|
|
4
|
+
private readonly representations;
|
|
5
|
+
private readonly negatives;
|
|
6
|
+
private readonly plans;
|
|
7
|
+
readRepresentation(key: string): ContextBudgetRepresentationCacheReadV2 | undefined;
|
|
8
|
+
writeRepresentation(input: {
|
|
9
|
+
readonly key: string;
|
|
10
|
+
readonly entry: ContextBudgetRepresentationCacheEntryV2;
|
|
11
|
+
}): void;
|
|
12
|
+
readNegativeRepresentation(key: string): ContextBudgetNegativeCacheEntryV2 | undefined;
|
|
13
|
+
writeNegativeRepresentation(input: {
|
|
14
|
+
readonly key: string;
|
|
15
|
+
readonly reason: string;
|
|
16
|
+
}): void;
|
|
17
|
+
readPlan(key: string): ContextBudgetPlanCacheReadV2 | undefined;
|
|
18
|
+
writePlan(input: {
|
|
19
|
+
readonly key: string;
|
|
20
|
+
readonly entry: ContextBudgetPlanCacheEntryV2;
|
|
21
|
+
}): void;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=context-budget-v2-cache-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-provider.d.ts","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,4BAA4B,EAC5B,iCAAiC,EACjC,6BAA6B,EAC7B,4BAA4B,EAC5B,uCAAuC,EACvC,sCAAsC,EACtC,MAAM,8BAA8B,CAAC;AAEtC,wBAAgB,wCAAwC,IAAI,4BAA4B,CAEvF;AAED,qBAAa,kCAAmC,YAAW,4BAA4B;IACtF,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA8D;IAC9F,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAwD;IAClF,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoD;IAE1E,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,sCAAsC,GAAG,SAAS,CAGlF;IAED,mBAAmB,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,KAAK,EAAE,uCAAuC,CAAA;KAAE,GAAG,IAAI,CAElH;IAED,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,iCAAiC,GAAG,SAAS,CAErF;IAED,2BAA2B,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAE1F;IAED,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,4BAA4B,GAAG,SAAS,CAG9D;IAED,SAAS,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,KAAK,EAAE,6BAA6B,CAAA;KAAE,GAAG,IAAI,CAE9F;CACD","sourcesContent":["import type {\n\tContextBudgetCacheProviderV2,\n\tContextBudgetNegativeCacheEntryV2,\n\tContextBudgetPlanCacheEntryV2,\n\tContextBudgetPlanCacheReadV2,\n\tContextBudgetRepresentationCacheEntryV2,\n\tContextBudgetRepresentationCacheReadV2,\n} from \"./context-budget-v2-types.ts\";\n\nexport function createMemoryContextBudgetCacheProviderV2(): ContextBudgetCacheProviderV2 {\n\treturn new MemoryContextBudgetCacheProviderV2();\n}\n\nexport class MemoryContextBudgetCacheProviderV2 implements ContextBudgetCacheProviderV2 {\n\tprivate readonly representations = new Map<string, ContextBudgetRepresentationCacheEntryV2>();\n\tprivate readonly negatives = new Map<string, ContextBudgetNegativeCacheEntryV2>();\n\tprivate readonly plans = new Map<string, ContextBudgetPlanCacheEntryV2>();\n\n\treadRepresentation(key: string): ContextBudgetRepresentationCacheReadV2 | undefined {\n\t\tconst entry = this.representations.get(key);\n\t\treturn entry ? { entry, layer: \"turn\" } : undefined;\n\t}\n\n\twriteRepresentation(input: { readonly key: string; readonly entry: ContextBudgetRepresentationCacheEntryV2 }): void {\n\t\tthis.representations.set(input.key, input.entry);\n\t}\n\n\treadNegativeRepresentation(key: string): ContextBudgetNegativeCacheEntryV2 | undefined {\n\t\treturn this.negatives.get(key);\n\t}\n\n\twriteNegativeRepresentation(input: { readonly key: string; readonly reason: string }): void {\n\t\tthis.negatives.set(input.key, { reason: input.reason, layer: \"turn\" });\n\t}\n\n\treadPlan(key: string): ContextBudgetPlanCacheReadV2 | undefined {\n\t\tconst entry = this.plans.get(key);\n\t\treturn entry ? { entry, layer: \"turn\" } : undefined;\n\t}\n\n\twritePlan(input: { readonly key: string; readonly entry: ContextBudgetPlanCacheEntryV2 }): void {\n\t\tthis.plans.set(input.key, input.entry);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function createMemoryContextBudgetCacheProviderV2() {
|
|
2
|
+
return new MemoryContextBudgetCacheProviderV2();
|
|
3
|
+
}
|
|
4
|
+
export class MemoryContextBudgetCacheProviderV2 {
|
|
5
|
+
representations = new Map();
|
|
6
|
+
negatives = new Map();
|
|
7
|
+
plans = new Map();
|
|
8
|
+
readRepresentation(key) {
|
|
9
|
+
const entry = this.representations.get(key);
|
|
10
|
+
return entry ? { entry, layer: "turn" } : undefined;
|
|
11
|
+
}
|
|
12
|
+
writeRepresentation(input) {
|
|
13
|
+
this.representations.set(input.key, input.entry);
|
|
14
|
+
}
|
|
15
|
+
readNegativeRepresentation(key) {
|
|
16
|
+
return this.negatives.get(key);
|
|
17
|
+
}
|
|
18
|
+
writeNegativeRepresentation(input) {
|
|
19
|
+
this.negatives.set(input.key, { reason: input.reason, layer: "turn" });
|
|
20
|
+
}
|
|
21
|
+
readPlan(key) {
|
|
22
|
+
const entry = this.plans.get(key);
|
|
23
|
+
return entry ? { entry, layer: "turn" } : undefined;
|
|
24
|
+
}
|
|
25
|
+
writePlan(input) {
|
|
26
|
+
this.plans.set(input.key, input.entry);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=context-budget-v2-cache-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-provider.js","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-provider.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,wCAAwC,GAAiC;IACxF,OAAO,IAAI,kCAAkC,EAAE,CAAC;AAAA,CAChD;AAED,MAAM,OAAO,kCAAkC;IAC7B,eAAe,GAAG,IAAI,GAAG,EAAmD,CAAC;IAC7E,SAAS,GAAG,IAAI,GAAG,EAA6C,CAAC;IACjE,KAAK,GAAG,IAAI,GAAG,EAAyC,CAAC;IAE1E,kBAAkB,CAAC,GAAW,EAAsD;QACnF,MAAM,KAAK,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC5C,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IAAA,CACpD;IAED,mBAAmB,CAAC,KAAwF,EAAQ;QACnH,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IAAA,CACjD;IAED,0BAA0B,CAAC,GAAW,EAAiD;QACtF,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAAA,CAC/B;IAED,2BAA2B,CAAC,KAAwD,EAAQ;QAC3F,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IAAA,CACvE;IAED,QAAQ,CAAC,GAAW,EAA4C;QAC/D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IAAA,CACpD;IAED,SAAS,CAAC,KAA8E,EAAQ;QAC/F,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IAAA,CACvC;CACD","sourcesContent":["import type {\n\tContextBudgetCacheProviderV2,\n\tContextBudgetNegativeCacheEntryV2,\n\tContextBudgetPlanCacheEntryV2,\n\tContextBudgetPlanCacheReadV2,\n\tContextBudgetRepresentationCacheEntryV2,\n\tContextBudgetRepresentationCacheReadV2,\n} from \"./context-budget-v2-types.ts\";\n\nexport function createMemoryContextBudgetCacheProviderV2(): ContextBudgetCacheProviderV2 {\n\treturn new MemoryContextBudgetCacheProviderV2();\n}\n\nexport class MemoryContextBudgetCacheProviderV2 implements ContextBudgetCacheProviderV2 {\n\tprivate readonly representations = new Map<string, ContextBudgetRepresentationCacheEntryV2>();\n\tprivate readonly negatives = new Map<string, ContextBudgetNegativeCacheEntryV2>();\n\tprivate readonly plans = new Map<string, ContextBudgetPlanCacheEntryV2>();\n\n\treadRepresentation(key: string): ContextBudgetRepresentationCacheReadV2 | undefined {\n\t\tconst entry = this.representations.get(key);\n\t\treturn entry ? { entry, layer: \"turn\" } : undefined;\n\t}\n\n\twriteRepresentation(input: { readonly key: string; readonly entry: ContextBudgetRepresentationCacheEntryV2 }): void {\n\t\tthis.representations.set(input.key, input.entry);\n\t}\n\n\treadNegativeRepresentation(key: string): ContextBudgetNegativeCacheEntryV2 | undefined {\n\t\treturn this.negatives.get(key);\n\t}\n\n\twriteNegativeRepresentation(input: { readonly key: string; readonly reason: string }): void {\n\t\tthis.negatives.set(input.key, { reason: input.reason, layer: \"turn\" });\n\t}\n\n\treadPlan(key: string): ContextBudgetPlanCacheReadV2 | undefined {\n\t\tconst entry = this.plans.get(key);\n\t\treturn entry ? { entry, layer: \"turn\" } : undefined;\n\t}\n\n\twritePlan(input: { readonly key: string; readonly entry: ContextBudgetPlanCacheEntryV2 }): void {\n\t\tthis.plans.set(input.key, input.entry);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ContextRepresentationKindV2 } from "./context-budget-headroom.ts";
|
|
2
|
+
import type { ContextBudgetCacheLayerV2, ContextBudgetRepresentationCacheKeyKindV2, PromptContextBudgetPlanV2, TokenCacheTelemetryV2 } from "./context-budget-v2-types.ts";
|
|
3
|
+
export interface MutableTokenCacheTelemetryV2 {
|
|
4
|
+
planCache: {
|
|
5
|
+
hit: boolean;
|
|
6
|
+
key: string;
|
|
7
|
+
layer?: ContextBudgetCacheLayerV2;
|
|
8
|
+
rejectedReason?: string;
|
|
9
|
+
};
|
|
10
|
+
representationCache: {
|
|
11
|
+
exactHits: number;
|
|
12
|
+
semanticHits: number;
|
|
13
|
+
pointerHits: number;
|
|
14
|
+
misses: number;
|
|
15
|
+
staleRejects: number;
|
|
16
|
+
poisonRejects: number;
|
|
17
|
+
negativeHits: number;
|
|
18
|
+
writes: number;
|
|
19
|
+
};
|
|
20
|
+
tokens: {
|
|
21
|
+
raw: number;
|
|
22
|
+
rendered: number;
|
|
23
|
+
savedByCache: number;
|
|
24
|
+
savedByCompression: number;
|
|
25
|
+
savedByOmission: number;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export declare function createMutableContextBudgetCacheTelemetryV2(planKey: string): MutableTokenCacheTelemetryV2;
|
|
29
|
+
export declare function finalizeContextBudgetCacheTelemetryV2(telemetry: MutableTokenCacheTelemetryV2, input: {
|
|
30
|
+
readonly rawTokens: number;
|
|
31
|
+
readonly usedTokens: number;
|
|
32
|
+
readonly omittedTokens: number;
|
|
33
|
+
}): TokenCacheTelemetryV2;
|
|
34
|
+
export declare function withCacheTelemetry(plan: PromptContextBudgetPlanV2, telemetry: MutableTokenCacheTelemetryV2): PromptContextBudgetPlanV2;
|
|
35
|
+
export declare function recordContextBudgetRepresentationCacheHitV2(telemetry: MutableTokenCacheTelemetryV2, kind: ContextRepresentationKindV2, keyKind?: ContextBudgetRepresentationCacheKeyKindV2): void;
|
|
36
|
+
//# sourceMappingURL=context-budget-v2-cache-telemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-telemetry.d.ts","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,KAAK,EACX,yBAAyB,EACzB,yCAAyC,EACzC,yBAAyB,EACzB,qBAAqB,EACrB,MAAM,8BAA8B,CAAC;AAEtC,MAAM,WAAW,4BAA4B;IAC5C,SAAS,EAAE;QACV,GAAG,EAAE,OAAO,CAAC;QACb,GAAG,EAAE,MAAM,CAAC;QACZ,KAAK,CAAC,EAAE,yBAAyB,CAAC;QAClC,cAAc,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,mBAAmB,EAAE;QACpB,SAAS,EAAE,MAAM,CAAC;QAClB,YAAY,EAAE,MAAM,CAAC;QACrB,WAAW,EAAE,MAAM,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,MAAM,CAAC;QACtB,YAAY,EAAE,MAAM,CAAC;QACrB,MAAM,EAAE,MAAM,CAAC;KACf,CAAC;IACF,MAAM,EAAE;QACP,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;QACjB,YAAY,EAAE,MAAM,CAAC;QACrB,kBAAkB,EAAE,MAAM,CAAC;QAC3B,eAAe,EAAE,MAAM,CAAC;KACxB,CAAC;CACF;AAED,wBAAgB,0CAA0C,CAAC,OAAO,EAAE,MAAM,GAAG,4BAA4B,CAwBxG;AAED,wBAAgB,qCAAqC,CACpD,SAAS,EAAE,4BAA4B,EACvC,KAAK,EAAE;IACN,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAC/B,GACC,qBAAqB,CAYvB;AAED,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,yBAAyB,EAC/B,SAAS,EAAE,4BAA4B,GACrC,yBAAyB,CAa3B;AAED,wBAAgB,2CAA2C,CAC1D,SAAS,EAAE,4BAA4B,EACvC,IAAI,EAAE,2BAA2B,EACjC,OAAO,GAAE,yCAAmD,GAC1D,IAAI,CAUN","sourcesContent":["import type { ContextRepresentationKindV2 } from \"./context-budget-headroom.ts\";\nimport type {\n\tContextBudgetCacheLayerV2,\n\tContextBudgetRepresentationCacheKeyKindV2,\n\tPromptContextBudgetPlanV2,\n\tTokenCacheTelemetryV2,\n} from \"./context-budget-v2-types.ts\";\n\nexport interface MutableTokenCacheTelemetryV2 {\n\tplanCache: {\n\t\thit: boolean;\n\t\tkey: string;\n\t\tlayer?: ContextBudgetCacheLayerV2;\n\t\trejectedReason?: string;\n\t};\n\trepresentationCache: {\n\t\texactHits: number;\n\t\tsemanticHits: number;\n\t\tpointerHits: number;\n\t\tmisses: number;\n\t\tstaleRejects: number;\n\t\tpoisonRejects: number;\n\t\tnegativeHits: number;\n\t\twrites: number;\n\t};\n\ttokens: {\n\t\traw: number;\n\t\trendered: number;\n\t\tsavedByCache: number;\n\t\tsavedByCompression: number;\n\t\tsavedByOmission: number;\n\t};\n}\n\nexport function createMutableContextBudgetCacheTelemetryV2(planKey: string): MutableTokenCacheTelemetryV2 {\n\treturn {\n\t\tplanCache: {\n\t\t\thit: false,\n\t\t\tkey: planKey,\n\t\t},\n\t\trepresentationCache: {\n\t\t\texactHits: 0,\n\t\t\tsemanticHits: 0,\n\t\t\tpointerHits: 0,\n\t\t\tmisses: 0,\n\t\t\tstaleRejects: 0,\n\t\t\tpoisonRejects: 0,\n\t\t\tnegativeHits: 0,\n\t\t\twrites: 0,\n\t\t},\n\t\ttokens: {\n\t\t\traw: 0,\n\t\t\trendered: 0,\n\t\t\tsavedByCache: 0,\n\t\t\tsavedByCompression: 0,\n\t\t\tsavedByOmission: 0,\n\t\t},\n\t};\n}\n\nexport function finalizeContextBudgetCacheTelemetryV2(\n\ttelemetry: MutableTokenCacheTelemetryV2,\n\tinput: {\n\t\treadonly rawTokens: number;\n\t\treadonly usedTokens: number;\n\t\treadonly omittedTokens: number;\n\t},\n): TokenCacheTelemetryV2 {\n\treturn {\n\t\tplanCache: { ...telemetry.planCache },\n\t\trepresentationCache: { ...telemetry.representationCache },\n\t\ttokens: {\n\t\t\traw: input.rawTokens,\n\t\t\trendered: input.usedTokens,\n\t\t\tsavedByCache: telemetry.tokens.savedByCache,\n\t\t\tsavedByCompression: Math.max(0, input.rawTokens - input.usedTokens - input.omittedTokens),\n\t\t\tsavedByOmission: input.omittedTokens,\n\t\t},\n\t};\n}\n\nexport function withCacheTelemetry(\n\tplan: PromptContextBudgetPlanV2,\n\ttelemetry: MutableTokenCacheTelemetryV2,\n): PromptContextBudgetPlanV2 {\n\tconst cache = finalizeContextBudgetCacheTelemetryV2(telemetry, {\n\t\tomittedTokens: plan.omittedTokens,\n\t\trawTokens: plan.rawTokens,\n\t\tusedTokens: plan.usedTokens,\n\t});\n\treturn {\n\t\t...plan,\n\t\tobservability: {\n\t\t\t...plan.observability,\n\t\t\tcache,\n\t\t},\n\t};\n}\n\nexport function recordContextBudgetRepresentationCacheHitV2(\n\ttelemetry: MutableTokenCacheTelemetryV2,\n\tkind: ContextRepresentationKindV2,\n\tkeyKind: ContextBudgetRepresentationCacheKeyKindV2 = \"exact\",\n): void {\n\tif (keyKind === \"materialized\") {\n\t\ttelemetry.representationCache.semanticHits += 1;\n\t\treturn;\n\t}\n\tif (kind === \"pointer\") {\n\t\ttelemetry.representationCache.pointerHits += 1;\n\t\treturn;\n\t}\n\ttelemetry.representationCache.exactHits += 1;\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function createMutableContextBudgetCacheTelemetryV2(planKey) {
|
|
2
|
+
return {
|
|
3
|
+
planCache: {
|
|
4
|
+
hit: false,
|
|
5
|
+
key: planKey,
|
|
6
|
+
},
|
|
7
|
+
representationCache: {
|
|
8
|
+
exactHits: 0,
|
|
9
|
+
semanticHits: 0,
|
|
10
|
+
pointerHits: 0,
|
|
11
|
+
misses: 0,
|
|
12
|
+
staleRejects: 0,
|
|
13
|
+
poisonRejects: 0,
|
|
14
|
+
negativeHits: 0,
|
|
15
|
+
writes: 0,
|
|
16
|
+
},
|
|
17
|
+
tokens: {
|
|
18
|
+
raw: 0,
|
|
19
|
+
rendered: 0,
|
|
20
|
+
savedByCache: 0,
|
|
21
|
+
savedByCompression: 0,
|
|
22
|
+
savedByOmission: 0,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function finalizeContextBudgetCacheTelemetryV2(telemetry, input) {
|
|
27
|
+
return {
|
|
28
|
+
planCache: { ...telemetry.planCache },
|
|
29
|
+
representationCache: { ...telemetry.representationCache },
|
|
30
|
+
tokens: {
|
|
31
|
+
raw: input.rawTokens,
|
|
32
|
+
rendered: input.usedTokens,
|
|
33
|
+
savedByCache: telemetry.tokens.savedByCache,
|
|
34
|
+
savedByCompression: Math.max(0, input.rawTokens - input.usedTokens - input.omittedTokens),
|
|
35
|
+
savedByOmission: input.omittedTokens,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function withCacheTelemetry(plan, telemetry) {
|
|
40
|
+
const cache = finalizeContextBudgetCacheTelemetryV2(telemetry, {
|
|
41
|
+
omittedTokens: plan.omittedTokens,
|
|
42
|
+
rawTokens: plan.rawTokens,
|
|
43
|
+
usedTokens: plan.usedTokens,
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
...plan,
|
|
47
|
+
observability: {
|
|
48
|
+
...plan.observability,
|
|
49
|
+
cache,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function recordContextBudgetRepresentationCacheHitV2(telemetry, kind, keyKind = "exact") {
|
|
54
|
+
if (keyKind === "materialized") {
|
|
55
|
+
telemetry.representationCache.semanticHits += 1;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (kind === "pointer") {
|
|
59
|
+
telemetry.representationCache.pointerHits += 1;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
telemetry.representationCache.exactHits += 1;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=context-budget-v2-cache-telemetry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-telemetry.js","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-telemetry.ts"],"names":[],"mappings":"AAkCA,MAAM,UAAU,0CAA0C,CAAC,OAAe,EAAgC;IACzG,OAAO;QACN,SAAS,EAAE;YACV,GAAG,EAAE,KAAK;YACV,GAAG,EAAE,OAAO;SACZ;QACD,mBAAmB,EAAE;YACpB,SAAS,EAAE,CAAC;YACZ,YAAY,EAAE,CAAC;YACf,WAAW,EAAE,CAAC;YACd,MAAM,EAAE,C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AAK,cAAc,EAAE,CAAC;QAChC,SAAS,CAAC,mBAAmB,CAAC,YAAY,IAAI,CAAC,CAAC;QAChD,OAAO;IACR,CAAC;IACD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACxB,SAAS,CAAC,mBAAmB,CAAC,WAAW,IAAI,CAAC,CAAC;QAC/C,OAAO;IACR,CAAC;IACD,SAAS,CAAC,mBAAmB,CAAC,SAAS,IAAI,CAAC,CAAC;AAAA,CAC7C","sourcesContent":["import type { ContextRepresentationKindV2 } from \"./context-budget-headroom.ts\";\nimport type {\n\tContextBudgetCacheLayerV2,\n\tContextBudgetRepresentationCacheKeyKindV2,\n\tPromptContextBudgetPlanV2,\n\tTokenCacheTelemetryV2,\n} from \"./context-budget-v2-types.ts\";\n\nexport interface MutableTokenCacheTelemetryV2 {\n\tplanCache: {\n\t\thit: boolean;\n\t\tkey: string;\n\t\tlayer?: ContextBudgetCacheLayerV2;\n\t\trejectedReason?: string;\n\t};\n\trepresentationCache: {\n\t\texactHits: number;\n\t\tsemanticHits: number;\n\t\tpointerHits: number;\n\t\tmisses: number;\n\t\tstaleRejects: number;\n\t\tpoisonRejects: number;\n\t\tnegativeHits: number;\n\t\twrites: number;\n\t};\n\ttokens: {\n\t\traw: number;\n\t\trendered: number;\n\t\tsavedByCache: number;\n\t\tsavedByCompression: number;\n\t\tsavedByOmission: number;\n\t};\n}\n\nexport function createMutableContextBudgetCacheTelemetryV2(planKey: string): MutableTokenCacheTelemetryV2 {\n\treturn {\n\t\tplanCache: {\n\t\t\thit: false,\n\t\t\tkey: planKey,\n\t\t},\n\t\trepresentationCache: {\n\t\t\texactHits: 0,\n\t\t\tsemanticHits: 0,\n\t\t\tpointerHits: 0,\n\t\t\tmisses: 0,\n\t\t\tstaleRejects: 0,\n\t\t\tpoisonRejects: 0,\n\t\t\tnegativeHits: 0,\n\t\t\twrites: 0,\n\t\t},\n\t\ttokens: {\n\t\t\traw: 0,\n\t\t\trendered: 0,\n\t\t\tsavedByCache: 0,\n\t\t\tsavedByCompression: 0,\n\t\t\tsavedByOmission: 0,\n\t\t},\n\t};\n}\n\nexport function finalizeContextBudgetCacheTelemetryV2(\n\ttelemetry: MutableTokenCacheTelemetryV2,\n\tinput: {\n\t\treadonly rawTokens: number;\n\t\treadonly usedTokens: number;\n\t\treadonly omittedTokens: number;\n\t},\n): TokenCacheTelemetryV2 {\n\treturn {\n\t\tplanCache: { ...telemetry.planCache },\n\t\trepresentationCache: { ...telemetry.representationCache },\n\t\ttokens: {\n\t\t\traw: input.rawTokens,\n\t\t\trendered: input.usedTokens,\n\t\t\tsavedByCache: telemetry.tokens.savedByCache,\n\t\t\tsavedByCompression: Math.max(0, input.rawTokens - input.usedTokens - input.omittedTokens),\n\t\t\tsavedByOmission: input.omittedTokens,\n\t\t},\n\t};\n}\n\nexport function withCacheTelemetry(\n\tplan: PromptContextBudgetPlanV2,\n\ttelemetry: MutableTokenCacheTelemetryV2,\n): PromptContextBudgetPlanV2 {\n\tconst cache = finalizeContextBudgetCacheTelemetryV2(telemetry, {\n\t\tomittedTokens: plan.omittedTokens,\n\t\trawTokens: plan.rawTokens,\n\t\tusedTokens: plan.usedTokens,\n\t});\n\treturn {\n\t\t...plan,\n\t\tobservability: {\n\t\t\t...plan.observability,\n\t\t\tcache,\n\t\t},\n\t};\n}\n\nexport function recordContextBudgetRepresentationCacheHitV2(\n\ttelemetry: MutableTokenCacheTelemetryV2,\n\tkind: ContextRepresentationKindV2,\n\tkeyKind: ContextBudgetRepresentationCacheKeyKindV2 = \"exact\",\n): void {\n\tif (keyKind === \"materialized\") {\n\t\ttelemetry.representationCache.semanticHits += 1;\n\t\treturn;\n\t}\n\tif (kind === \"pointer\") {\n\t\ttelemetry.representationCache.pointerHits += 1;\n\t\treturn;\n\t}\n\ttelemetry.representationCache.exactHits += 1;\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ContextRepresentationCandidateV2 } from "./context-budget-headroom.ts";
|
|
2
|
+
import type { PlannedItemV2 } from "./context-budget-v2-scoring.ts";
|
|
3
|
+
import type { ContextBudgetPlanCacheEntryV2, ContextBudgetRepresentationCacheReadV2, ContextBudgetRepresentationCacheRejectReasonV2 } from "./context-budget-v2-types.ts";
|
|
4
|
+
interface ContextBudgetCacheValidationRuntimeV2 {
|
|
5
|
+
readonly keyBase: {
|
|
6
|
+
readonly modelId: string;
|
|
7
|
+
readonly policyVersion: string;
|
|
8
|
+
readonly tokenizerId: string;
|
|
9
|
+
readonly compressorPolicyHash?: string;
|
|
10
|
+
readonly qualityPolicyHash?: string;
|
|
11
|
+
};
|
|
12
|
+
readonly nowEpochMs: number;
|
|
13
|
+
readonly ttlMs: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function validateContextBudgetPlanCacheEntryV2(input: {
|
|
16
|
+
readonly entry: ContextBudgetPlanCacheEntryV2;
|
|
17
|
+
readonly planned: readonly PlannedItemV2[];
|
|
18
|
+
readonly availableTokens: number;
|
|
19
|
+
readonly cache: ContextBudgetCacheValidationRuntimeV2;
|
|
20
|
+
}): string | undefined;
|
|
21
|
+
export declare function validateContextBudgetRepresentationCacheEntryV2(input: {
|
|
22
|
+
readonly cached: ContextBudgetRepresentationCacheReadV2;
|
|
23
|
+
readonly candidate: ContextRepresentationCandidateV2;
|
|
24
|
+
readonly cache: ContextBudgetCacheValidationRuntimeV2;
|
|
25
|
+
readonly sourceHash: string;
|
|
26
|
+
}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined;
|
|
27
|
+
export declare function validateContextBudgetMaterializedRepresentationCacheEntryV2(input: {
|
|
28
|
+
readonly cached: ContextBudgetRepresentationCacheReadV2;
|
|
29
|
+
readonly candidate: ContextRepresentationCandidateV2;
|
|
30
|
+
readonly cache: ContextBudgetCacheValidationRuntimeV2;
|
|
31
|
+
readonly sourceHash: string;
|
|
32
|
+
}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined;
|
|
33
|
+
export declare function sourceHashesByContextBudgetItemIdV2(planned: readonly PlannedItemV2[]): Record<string, string>;
|
|
34
|
+
export declare function isContextBudgetCacheEntryStaleV2(createdAtEpochMs: number | undefined, nowEpochMs: number, ttlMs: number): boolean;
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=context-budget-v2-cache-validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-validation.d.ts","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,8BAA8B,CAAC;AAMrF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,KAAK,EACX,6BAA6B,EAC7B,sCAAsC,EACtC,8CAA8C,EAC9C,MAAM,8BAA8B,CAAC;AAEtC,UAAU,qCAAqC;IAC9C,QAAQ,CAAC,OAAO,EAAE;QACjB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;QAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;QAC7B,QAAQ,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;QACvC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;KACpC,CAAC;IACF,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,qCAAqC,CAAC,KAAK,EAAE;IAC5D,QAAQ,CAAC,KAAK,EAAE,6BAA6B,CAAC;IAC9C,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,CAAC;IAC3C,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,KAAK,EAAE,qCAAqC,CAAC;CACtD,GAAG,MAAM,GAAG,SAAS,CAarB;AAED,wBAAgB,+CAA+C,CAAC,KAAK,EAAE;IACtE,QAAQ,CAAC,MAAM,EAAE,sCAAsC,CAAC;IACxD,QAAQ,CAAC,SAAS,EAAE,gCAAgC,CAAC;IACrD,QAAQ,CAAC,KAAK,EAAE,qCAAqC,CAAC;IACtD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B,GAAG,8CAA8C,GAAG,SAAS,CAW7D;AAED,wBAAgB,2DAA2D,CAAC,KAAK,EAAE;IAClF,QAAQ,CAAC,MAAM,EAAE,sCAAsC,CAAC;IACxD,QAAQ,CAAC,SAAS,EAAE,gCAAgC,CAAC;IACrD,QAAQ,CAAC,KAAK,EAAE,qCAAqC,CAAC;IACtD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B,GAAG,8CAA8C,GAAG,SAAS,CA6B7D;AAqBD,wBAAgB,mCAAmC,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAM7G;AAED,wBAAgB,gCAAgC,CAC/C,gBAAgB,EAAE,MAAM,GAAG,SAAS,EACpC,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,GACX,OAAO,CAGT","sourcesContent":["import type { ContextRepresentationCandidateV2 } from \"./context-budget-headroom.ts\";\nimport {\n\tcomputeContextBudgetOutputTextHashV2,\n\tcomputeContextBudgetRepresentationFingerprintV2,\n\tcomputeContextBudgetTargetTokenBucketV2,\n} from \"./context-budget-v2-cache-keys.ts\";\nimport type { PlannedItemV2 } from \"./context-budget-v2-scoring.ts\";\nimport type {\n\tContextBudgetPlanCacheEntryV2,\n\tContextBudgetRepresentationCacheReadV2,\n\tContextBudgetRepresentationCacheRejectReasonV2,\n} from \"./context-budget-v2-types.ts\";\n\ninterface ContextBudgetCacheValidationRuntimeV2 {\n\treadonly keyBase: {\n\t\treadonly modelId: string;\n\t\treadonly policyVersion: string;\n\t\treadonly tokenizerId: string;\n\t\treadonly compressorPolicyHash?: string;\n\t\treadonly qualityPolicyHash?: string;\n\t};\n\treadonly nowEpochMs: number;\n\treadonly ttlMs: number;\n}\n\nexport function validateContextBudgetPlanCacheEntryV2(input: {\n\treadonly entry: ContextBudgetPlanCacheEntryV2;\n\treadonly planned: readonly PlannedItemV2[];\n\treadonly availableTokens: number;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n}): string | undefined {\n\tif (isContextBudgetCacheEntryStaleV2(input.entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs)) {\n\t\treturn \"stale\";\n\t}\n\tif (input.entry.plan.selectedRepresentations.some((representation) => representation.cache?.hit === true)) {\n\t\treturn \"cache_dependency_unsafe\";\n\t}\n\tif (input.entry.plan.usedTokens > input.availableTokens) return \"budget_mismatch\";\n\tconst currentSourceHashes = sourceHashesByContextBudgetItemIdV2(input.planned);\n\tfor (const [itemId, sourceHash] of Object.entries(currentSourceHashes)) {\n\t\tif (input.entry.sourceHashes[itemId] !== sourceHash) return \"source_hash_mismatch\";\n\t}\n\treturn undefined;\n}\n\nexport function validateContextBudgetRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly candidate: ContextRepresentationCandidateV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n\treadonly sourceHash: string;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.kind !== input.candidate.kind) return \"kind_mismatch\";\n\tif (entry.sourceHash !== input.sourceHash) return \"source_hash_mismatch\";\n\tif (entry.keyKind === \"materialized\") {\n\t\treturn validateContextBudgetMaterializedRepresentationCacheEntryV2(input);\n\t}\n\tif (entry.representationFingerprint !== computeContextBudgetRepresentationFingerprintV2(input.candidate)) {\n\t\treturn \"fingerprint_mismatch\";\n\t}\n\treturn validateSharedRepresentationCacheEntryV2(input);\n}\n\nexport function validateContextBudgetMaterializedRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly candidate: ContextRepresentationCandidateV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n\treadonly sourceHash: string;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.kind !== input.candidate.kind) return \"kind_mismatch\";\n\tif (entry.sourceHash !== input.sourceHash) return \"source_hash_mismatch\";\n\tconst sharedRejection = validateSharedRepresentationCacheEntryV2(input);\n\tif (sharedRejection) return sharedRejection;\n\tif (\n\t\tentry.compressorPolicyHash !== undefined &&\n\t\tentry.compressorPolicyHash !== input.cache.keyBase.compressorPolicyHash\n\t) {\n\t\treturn \"policy_mismatch\";\n\t}\n\tif (entry.qualityPolicyHash !== undefined && entry.qualityPolicyHash !== input.cache.keyBase.qualityPolicyHash) {\n\t\treturn \"policy_mismatch\";\n\t}\n\tif (\n\t\tentry.targetTokenBucket !== undefined &&\n\t\tentry.targetTokenBucket !== computeContextBudgetTargetTokenBucketV2(input.candidate.estimatedTokens)\n\t) {\n\t\treturn \"token_invalid\";\n\t}\n\tif (entry.outputTextHash !== computeContextBudgetOutputTextHashV2(entry.text)) return \"poisoned\";\n\tconst verification = entry.verification;\n\tif (!verification) return \"poisoned\";\n\tif ((verification.queryCoverage ?? 0) < materializedCoverageFloor(input.candidate)) return \"poisoned\";\n\tif ((verification.sourceCoverage ?? 0) < materializedCoverageFloor(input.candidate)) return \"poisoned\";\n\tif ((verification.secretLeakScore ?? 1) > 0) return \"poisoned\";\n\tif ((verification.poisonScore ?? 1) > 0) return \"poisoned\";\n\treturn undefined;\n}\n\nfunction validateSharedRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.policyVersion !== input.cache.keyBase.policyVersion) return \"policy_mismatch\";\n\tif (entry.modelId !== input.cache.keyBase.modelId) return \"model_mismatch\";\n\tif (entry.tokenizerId !== input.cache.keyBase.tokenizerId) return \"tokenizer_mismatch\";\n\tif (!Number.isFinite(entry.estimatedTokens) || entry.estimatedTokens < 0) return \"token_invalid\";\n\tif (isContextBudgetCacheEntryStaleV2(entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs))\n\t\treturn \"stale\";\n\treturn undefined;\n}\n\nfunction materializedCoverageFloor(candidate: ContextRepresentationCandidateV2): number {\n\tif (candidate.kind === \"headroom-compressed\") return 0.5;\n\treturn 0.75;\n}\n\nexport function sourceHashesByContextBudgetItemIdV2(planned: readonly PlannedItemV2[]): Record<string, string> {\n\tconst hashes: Record<string, string> = {};\n\tfor (const item of planned) {\n\t\thashes[item.item.id] = item.contentHash;\n\t}\n\treturn hashes;\n}\n\nexport function isContextBudgetCacheEntryStaleV2(\n\tcreatedAtEpochMs: number | undefined,\n\tnowEpochMs: number,\n\tttlMs: number,\n): boolean {\n\tif (createdAtEpochMs === undefined) return false;\n\treturn nowEpochMs - createdAtEpochMs > ttlMs;\n}\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { computeContextBudgetOutputTextHashV2, computeContextBudgetRepresentationFingerprintV2, computeContextBudgetTargetTokenBucketV2, } from "./context-budget-v2-cache-keys.js";
|
|
2
|
+
export function validateContextBudgetPlanCacheEntryV2(input) {
|
|
3
|
+
if (isContextBudgetCacheEntryStaleV2(input.entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs)) {
|
|
4
|
+
return "stale";
|
|
5
|
+
}
|
|
6
|
+
if (input.entry.plan.selectedRepresentations.some((representation) => representation.cache?.hit === true)) {
|
|
7
|
+
return "cache_dependency_unsafe";
|
|
8
|
+
}
|
|
9
|
+
if (input.entry.plan.usedTokens > input.availableTokens)
|
|
10
|
+
return "budget_mismatch";
|
|
11
|
+
const currentSourceHashes = sourceHashesByContextBudgetItemIdV2(input.planned);
|
|
12
|
+
for (const [itemId, sourceHash] of Object.entries(currentSourceHashes)) {
|
|
13
|
+
if (input.entry.sourceHashes[itemId] !== sourceHash)
|
|
14
|
+
return "source_hash_mismatch";
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
export function validateContextBudgetRepresentationCacheEntryV2(input) {
|
|
19
|
+
const entry = input.cached.entry;
|
|
20
|
+
if (entry.kind !== input.candidate.kind)
|
|
21
|
+
return "kind_mismatch";
|
|
22
|
+
if (entry.sourceHash !== input.sourceHash)
|
|
23
|
+
return "source_hash_mismatch";
|
|
24
|
+
if (entry.keyKind === "materialized") {
|
|
25
|
+
return validateContextBudgetMaterializedRepresentationCacheEntryV2(input);
|
|
26
|
+
}
|
|
27
|
+
if (entry.representationFingerprint !== computeContextBudgetRepresentationFingerprintV2(input.candidate)) {
|
|
28
|
+
return "fingerprint_mismatch";
|
|
29
|
+
}
|
|
30
|
+
return validateSharedRepresentationCacheEntryV2(input);
|
|
31
|
+
}
|
|
32
|
+
export function validateContextBudgetMaterializedRepresentationCacheEntryV2(input) {
|
|
33
|
+
const entry = input.cached.entry;
|
|
34
|
+
if (entry.kind !== input.candidate.kind)
|
|
35
|
+
return "kind_mismatch";
|
|
36
|
+
if (entry.sourceHash !== input.sourceHash)
|
|
37
|
+
return "source_hash_mismatch";
|
|
38
|
+
const sharedRejection = validateSharedRepresentationCacheEntryV2(input);
|
|
39
|
+
if (sharedRejection)
|
|
40
|
+
return sharedRejection;
|
|
41
|
+
if (entry.compressorPolicyHash !== undefined &&
|
|
42
|
+
entry.compressorPolicyHash !== input.cache.keyBase.compressorPolicyHash) {
|
|
43
|
+
return "policy_mismatch";
|
|
44
|
+
}
|
|
45
|
+
if (entry.qualityPolicyHash !== undefined && entry.qualityPolicyHash !== input.cache.keyBase.qualityPolicyHash) {
|
|
46
|
+
return "policy_mismatch";
|
|
47
|
+
}
|
|
48
|
+
if (entry.targetTokenBucket !== undefined &&
|
|
49
|
+
entry.targetTokenBucket !== computeContextBudgetTargetTokenBucketV2(input.candidate.estimatedTokens)) {
|
|
50
|
+
return "token_invalid";
|
|
51
|
+
}
|
|
52
|
+
if (entry.outputTextHash !== computeContextBudgetOutputTextHashV2(entry.text))
|
|
53
|
+
return "poisoned";
|
|
54
|
+
const verification = entry.verification;
|
|
55
|
+
if (!verification)
|
|
56
|
+
return "poisoned";
|
|
57
|
+
if ((verification.queryCoverage ?? 0) < materializedCoverageFloor(input.candidate))
|
|
58
|
+
return "poisoned";
|
|
59
|
+
if ((verification.sourceCoverage ?? 0) < materializedCoverageFloor(input.candidate))
|
|
60
|
+
return "poisoned";
|
|
61
|
+
if ((verification.secretLeakScore ?? 1) > 0)
|
|
62
|
+
return "poisoned";
|
|
63
|
+
if ((verification.poisonScore ?? 1) > 0)
|
|
64
|
+
return "poisoned";
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
function validateSharedRepresentationCacheEntryV2(input) {
|
|
68
|
+
const entry = input.cached.entry;
|
|
69
|
+
if (entry.policyVersion !== input.cache.keyBase.policyVersion)
|
|
70
|
+
return "policy_mismatch";
|
|
71
|
+
if (entry.modelId !== input.cache.keyBase.modelId)
|
|
72
|
+
return "model_mismatch";
|
|
73
|
+
if (entry.tokenizerId !== input.cache.keyBase.tokenizerId)
|
|
74
|
+
return "tokenizer_mismatch";
|
|
75
|
+
if (!Number.isFinite(entry.estimatedTokens) || entry.estimatedTokens < 0)
|
|
76
|
+
return "token_invalid";
|
|
77
|
+
if (isContextBudgetCacheEntryStaleV2(entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs))
|
|
78
|
+
return "stale";
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
function materializedCoverageFloor(candidate) {
|
|
82
|
+
if (candidate.kind === "headroom-compressed")
|
|
83
|
+
return 0.5;
|
|
84
|
+
return 0.75;
|
|
85
|
+
}
|
|
86
|
+
export function sourceHashesByContextBudgetItemIdV2(planned) {
|
|
87
|
+
const hashes = {};
|
|
88
|
+
for (const item of planned) {
|
|
89
|
+
hashes[item.item.id] = item.contentHash;
|
|
90
|
+
}
|
|
91
|
+
return hashes;
|
|
92
|
+
}
|
|
93
|
+
export function isContextBudgetCacheEntryStaleV2(createdAtEpochMs, nowEpochMs, ttlMs) {
|
|
94
|
+
if (createdAtEpochMs === undefined)
|
|
95
|
+
return false;
|
|
96
|
+
return nowEpochMs - createdAtEpochMs > ttlMs;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=context-budget-v2-cache-validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache-validation.js","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache-validation.ts"],"names":[],"mappings":"AACA,OAAO,EACN,oCAAoC,EACpC,+CAA+C,EAC/C,uCAAuC,GACvC,MAAM,mCAAmC,CAAC;AAoB3C,MAAM,UAAU,qCAAqC,CAAC,KAKrD,EAAsB;IACtB,IAAI,gCAAgC,CAAC,KAAK,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/G,OAAO,OAAO,CAAC;IAChB,CAAC;IACD,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC,cAAc,EAAE,EAAE,CAAC,cAAc,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC,EAAE,CAAC;QAC3G,OAAO,yBAAyB,CAAC;IAClC,CAAC;IACD,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,eAAe;QAAE,OAAO,iBAAiB,CAAC;IAClF,MAAM,mBAAmB,GAAG,mCAAmC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC/E,KAAK,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACxE,IAAI,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,M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AAK,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO;QAAE,OAAO,gBAAgB,CAAC;IAC3E,IAAI,KAAK,CAAC,WAAW,KAAK,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW;QAAE,OAAO,oBAAoB,CAAC;IACvF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,eAAe,CAAC,IAAI,KAAK,CAAC,eAAe,GAAG,CAAC;QAAE,OAAO,eAAe,CAAC;IACjG,IAAI,gCAAgC,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC;QACtG,OAAO,OAAO,CAAC;IAChB,OAAO,SAAS,CAAC;AAAA,CACjB;AAED,SAAS,yBAAyB,CAAC,SAA2C,EAAU;IACvF,IAAI,SAAS,CAAC,IAAI,KAAK,qBAAqB;QAAE,OAAO,GAAG,CAAC;IACzD,OAAO,IAAI,CAAC;AAAA,CACZ;AAED,MAAM,UAAU,mCAAmC,CAAC,OAAiC,EAA0B;IAC9G,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;QAC5B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC;IACzC,CAAC;IACD,OAAO,MAAM,CAAC;AAAA,CACd;AAED,MAAM,UAAU,gCAAgC,CAC/C,gBAAoC,EACpC,UAAkB,EAClB,KAAa,EACH;IACV,IAAI,gBAAgB,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IACjD,OAAO,UAAU,GAAG,gBAAgB,GAAG,KAAK,CAAC;AAAA,CAC7C","sourcesContent":["import type { ContextRepresentationCandidateV2 } from \"./context-budget-headroom.ts\";\nimport {\n\tcomputeContextBudgetOutputTextHashV2,\n\tcomputeContextBudgetRepresentationFingerprintV2,\n\tcomputeContextBudgetTargetTokenBucketV2,\n} from \"./context-budget-v2-cache-keys.ts\";\nimport type { PlannedItemV2 } from \"./context-budget-v2-scoring.ts\";\nimport type {\n\tContextBudgetPlanCacheEntryV2,\n\tContextBudgetRepresentationCacheReadV2,\n\tContextBudgetRepresentationCacheRejectReasonV2,\n} from \"./context-budget-v2-types.ts\";\n\ninterface ContextBudgetCacheValidationRuntimeV2 {\n\treadonly keyBase: {\n\t\treadonly modelId: string;\n\t\treadonly policyVersion: string;\n\t\treadonly tokenizerId: string;\n\t\treadonly compressorPolicyHash?: string;\n\t\treadonly qualityPolicyHash?: string;\n\t};\n\treadonly nowEpochMs: number;\n\treadonly ttlMs: number;\n}\n\nexport function validateContextBudgetPlanCacheEntryV2(input: {\n\treadonly entry: ContextBudgetPlanCacheEntryV2;\n\treadonly planned: readonly PlannedItemV2[];\n\treadonly availableTokens: number;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n}): string | undefined {\n\tif (isContextBudgetCacheEntryStaleV2(input.entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs)) {\n\t\treturn \"stale\";\n\t}\n\tif (input.entry.plan.selectedRepresentations.some((representation) => representation.cache?.hit === true)) {\n\t\treturn \"cache_dependency_unsafe\";\n\t}\n\tif (input.entry.plan.usedTokens > input.availableTokens) return \"budget_mismatch\";\n\tconst currentSourceHashes = sourceHashesByContextBudgetItemIdV2(input.planned);\n\tfor (const [itemId, sourceHash] of Object.entries(currentSourceHashes)) {\n\t\tif (input.entry.sourceHashes[itemId] !== sourceHash) return \"source_hash_mismatch\";\n\t}\n\treturn undefined;\n}\n\nexport function validateContextBudgetRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly candidate: ContextRepresentationCandidateV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n\treadonly sourceHash: string;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.kind !== input.candidate.kind) return \"kind_mismatch\";\n\tif (entry.sourceHash !== input.sourceHash) return \"source_hash_mismatch\";\n\tif (entry.keyKind === \"materialized\") {\n\t\treturn validateContextBudgetMaterializedRepresentationCacheEntryV2(input);\n\t}\n\tif (entry.representationFingerprint !== computeContextBudgetRepresentationFingerprintV2(input.candidate)) {\n\t\treturn \"fingerprint_mismatch\";\n\t}\n\treturn validateSharedRepresentationCacheEntryV2(input);\n}\n\nexport function validateContextBudgetMaterializedRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly candidate: ContextRepresentationCandidateV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n\treadonly sourceHash: string;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.kind !== input.candidate.kind) return \"kind_mismatch\";\n\tif (entry.sourceHash !== input.sourceHash) return \"source_hash_mismatch\";\n\tconst sharedRejection = validateSharedRepresentationCacheEntryV2(input);\n\tif (sharedRejection) return sharedRejection;\n\tif (\n\t\tentry.compressorPolicyHash !== undefined &&\n\t\tentry.compressorPolicyHash !== input.cache.keyBase.compressorPolicyHash\n\t) {\n\t\treturn \"policy_mismatch\";\n\t}\n\tif (entry.qualityPolicyHash !== undefined && entry.qualityPolicyHash !== input.cache.keyBase.qualityPolicyHash) {\n\t\treturn \"policy_mismatch\";\n\t}\n\tif (\n\t\tentry.targetTokenBucket !== undefined &&\n\t\tentry.targetTokenBucket !== computeContextBudgetTargetTokenBucketV2(input.candidate.estimatedTokens)\n\t) {\n\t\treturn \"token_invalid\";\n\t}\n\tif (entry.outputTextHash !== computeContextBudgetOutputTextHashV2(entry.text)) return \"poisoned\";\n\tconst verification = entry.verification;\n\tif (!verification) return \"poisoned\";\n\tif ((verification.queryCoverage ?? 0) < materializedCoverageFloor(input.candidate)) return \"poisoned\";\n\tif ((verification.sourceCoverage ?? 0) < materializedCoverageFloor(input.candidate)) return \"poisoned\";\n\tif ((verification.secretLeakScore ?? 1) > 0) return \"poisoned\";\n\tif ((verification.poisonScore ?? 1) > 0) return \"poisoned\";\n\treturn undefined;\n}\n\nfunction validateSharedRepresentationCacheEntryV2(input: {\n\treadonly cached: ContextBudgetRepresentationCacheReadV2;\n\treadonly cache: ContextBudgetCacheValidationRuntimeV2;\n}): ContextBudgetRepresentationCacheRejectReasonV2 | undefined {\n\tconst entry = input.cached.entry;\n\tif (entry.policyVersion !== input.cache.keyBase.policyVersion) return \"policy_mismatch\";\n\tif (entry.modelId !== input.cache.keyBase.modelId) return \"model_mismatch\";\n\tif (entry.tokenizerId !== input.cache.keyBase.tokenizerId) return \"tokenizer_mismatch\";\n\tif (!Number.isFinite(entry.estimatedTokens) || entry.estimatedTokens < 0) return \"token_invalid\";\n\tif (isContextBudgetCacheEntryStaleV2(entry.createdAtEpochMs, input.cache.nowEpochMs, input.cache.ttlMs))\n\t\treturn \"stale\";\n\treturn undefined;\n}\n\nfunction materializedCoverageFloor(candidate: ContextRepresentationCandidateV2): number {\n\tif (candidate.kind === \"headroom-compressed\") return 0.5;\n\treturn 0.75;\n}\n\nexport function sourceHashesByContextBudgetItemIdV2(planned: readonly PlannedItemV2[]): Record<string, string> {\n\tconst hashes: Record<string, string> = {};\n\tfor (const item of planned) {\n\t\thashes[item.item.id] = item.contentHash;\n\t}\n\treturn hashes;\n}\n\nexport function isContextBudgetCacheEntryStaleV2(\n\tcreatedAtEpochMs: number | undefined,\n\tnowEpochMs: number,\n\tttlMs: number,\n): boolean {\n\tif (createdAtEpochMs === undefined) return false;\n\treturn nowEpochMs - createdAtEpochMs > ttlMs;\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ContextBudgetCacheKeyBaseV2 } from "./context-budget-v2-cache-keys.ts";
|
|
2
|
+
import { type MutableTokenCacheTelemetryV2 } from "./context-budget-v2-cache-telemetry.ts";
|
|
3
|
+
import type { PlannedItemV2 } from "./context-budget-v2-scoring.ts";
|
|
4
|
+
import type { ContextBudgetCacheProviderV2, PromptContextBudgetPlanV2 } from "./context-budget-v2-types.ts";
|
|
5
|
+
export { buildContextBudgetExactRepresentationCacheKeyV2, buildContextBudgetMaterializedRepresentationCacheKeyV2, buildContextBudgetMaterializedRepresentationCandidateKeyV2, buildContextBudgetRepresentationCacheKeyV2, type ContextBudgetCacheKeyBaseV2, computeContextBudgetOutputTextHashV2, computeContextBudgetQueryIntentHashV2, computeContextBudgetRepresentationFingerprintV2, computeContextBudgetTargetTokenBucketV2, createContextBudgetCacheKeyBaseV2, DEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2, DEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2, } from "./context-budget-v2-cache-keys.ts";
|
|
6
|
+
export { createMemoryContextBudgetCacheProviderV2 } from "./context-budget-v2-cache-provider.ts";
|
|
7
|
+
export { finalizeContextBudgetCacheTelemetryV2, type MutableTokenCacheTelemetryV2, withCacheTelemetry, } from "./context-budget-v2-cache-telemetry.ts";
|
|
8
|
+
export { buildContextBudgetPlanCacheKeyV2 } from "./context-budget-v2-plan-cache-keys.ts";
|
|
9
|
+
export { applyRepresentationCacheV2, isMaterializableRepresentationV2, writeRepresentationCacheV2, } from "./context-budget-v2-representation-cache.ts";
|
|
10
|
+
export interface ContextBudgetSelectionCacheV2 {
|
|
11
|
+
readonly provider?: ContextBudgetCacheProviderV2;
|
|
12
|
+
readonly keyBase: ContextBudgetCacheKeyBaseV2;
|
|
13
|
+
readonly nowEpochMs: number;
|
|
14
|
+
readonly ttlMs: number;
|
|
15
|
+
readonly telemetry: MutableTokenCacheTelemetryV2;
|
|
16
|
+
}
|
|
17
|
+
export declare function createContextBudgetSelectionCacheV2(input: {
|
|
18
|
+
readonly provider?: ContextBudgetCacheProviderV2;
|
|
19
|
+
readonly keyBase: ContextBudgetCacheKeyBaseV2;
|
|
20
|
+
readonly planCacheKey: string;
|
|
21
|
+
readonly nowEpochMs?: number;
|
|
22
|
+
readonly ttlMs?: number;
|
|
23
|
+
}): ContextBudgetSelectionCacheV2;
|
|
24
|
+
export declare function readValidPlanCacheV2(input: {
|
|
25
|
+
readonly cache: ContextBudgetSelectionCacheV2;
|
|
26
|
+
readonly key: string;
|
|
27
|
+
readonly planned: readonly PlannedItemV2[];
|
|
28
|
+
readonly availableTokens: number;
|
|
29
|
+
}): PromptContextBudgetPlanV2 | undefined;
|
|
30
|
+
export declare function writePlanCacheV2(input: {
|
|
31
|
+
readonly cache: ContextBudgetSelectionCacheV2;
|
|
32
|
+
readonly key: string;
|
|
33
|
+
readonly plan: PromptContextBudgetPlanV2;
|
|
34
|
+
readonly planned: readonly PlannedItemV2[];
|
|
35
|
+
}): void;
|
|
36
|
+
//# sourceMappingURL=context-budget-v2-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-budget-v2-cache.d.ts","sourceRoot":"","sources":["../../src/core/context-budget-v2-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mCAAmC,CAAC;AACrF,OAAO,EAEN,KAAK,4BAA4B,EAEjC,MAAM,wCAAwC,CAAC;AAKhD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,KAAK,EAAE,4BAA4B,EAAE,yBAAyB,EAAE,MAAM,8BAA8B,CAAC;AAE5G,OAAO,EACN,+CAA+C,EAC/C,sDAAsD,EACtD,0DAA0D,EAC1D,0CAA0C,EAC1C,KAAK,2BAA2B,EAChC,oCAAoC,EACpC,qCAAqC,EACrC,+CAA+C,EAC/C,uCAAuC,EACvC,iCAAiC,EACjC,8CAA8C,EAC9C,2CAA2C,GAC3C,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,wCAAwC,EAAE,MAAM,uCAAuC,CAAC;AACjG,OAAO,EACN,qCAAqC,EACrC,KAAK,4BAA4B,EACjC,kBAAkB,GAClB,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,gCAAgC,EAAE,MAAM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acheKeyBaseV2 } from \"./context-budget-v2-cache-keys.ts\";\nimport {\n\tcreateMutableContextBudgetCacheTelemetryV2,\n\ttype MutableTokenCacheTelemetryV2,\n\twithCacheTelemetry,\n} from \"./context-budget-v2-cache-telemetry.ts\";\nimport {\n\tsourceHashesByContextBudgetItemIdV2,\n\tvalidateContextBudgetPlanCacheEntryV2,\n} from \"./context-budget-v2-cache-validation.ts\";\nimport type { PlannedItemV2 } from \"./context-budget-v2-scoring.ts\";\nimport type { ContextBudgetCacheProviderV2, PromptContextBudgetPlanV2 } from \"./context-budget-v2-types.ts\";\n\nexport {\n\tbuildContextBudgetExactRepresentationCacheKeyV2,\n\tbuildContextBudgetMaterializedRepresentationCacheKeyV2,\n\tbuildContextBudgetMaterializedRepresentationCandidateKeyV2,\n\tbuildContextBudgetRepresentationCacheKeyV2,\n\ttype ContextBudgetCacheKeyBaseV2,\n\tcomputeContextBudgetOutputTextHashV2,\n\tcomputeContextBudgetQueryIntentHashV2,\n\tcomputeContextBudgetRepresentationFingerprintV2,\n\tcomputeContextBudgetTargetTokenBucketV2,\n\tcreateContextBudgetCacheKeyBaseV2,\n\tDEFAULT_MATERIALIZED_COMPRESSOR_POLICY_HASH_V2,\n\tDEFAULT_MATERIALIZED_QUALITY_POLICY_HASH_V2,\n} from \"./context-budget-v2-cache-keys.ts\";\nexport { createMemoryContextBudgetCacheProviderV2 } from \"./context-budget-v2-cache-provider.ts\";\nexport {\n\tfinalizeContextBudgetCacheTelemetryV2,\n\ttype MutableTokenCacheTelemetryV2,\n\twithCacheTelemetry,\n} from \"./context-budget-v2-cache-telemetry.ts\";\nexport { buildContextBudgetPlanCacheKeyV2 } from \"./context-budget-v2-plan-cache-keys.ts\";\nexport {\n\tapplyRepresentationCacheV2,\n\tisMaterializableRepresentationV2,\n\twriteRepresentationCacheV2,\n} from \"./context-budget-v2-representation-cache.ts\";\n\nconst DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000;\n\nexport interface ContextBudgetSelectionCacheV2 {\n\treadonly provider?: ContextBudgetCacheProviderV2;\n\treadonly keyBase: ContextBudgetCacheKeyBaseV2;\n\treadonly nowEpochMs: number;\n\treadonly ttlMs: number;\n\treadonly telemetry: MutableTokenCacheTelemetryV2;\n}\n\nexport function createContextBudgetSelectionCacheV2(input: {\n\treadonly provider?: ContextBudgetCacheProviderV2;\n\treadonly keyBase: ContextBudgetCacheKeyBaseV2;\n\treadonly planCacheKey: string;\n\treadonly nowEpochMs?: number;\n\treadonly ttlMs?: number;\n}): ContextBudgetSelectionCacheV2 {\n\treturn {\n\t\tprovider: input.provider,\n\t\tkeyBase: input.keyBase,\n\t\tnowEpochMs: input.nowEpochMs ?? Date.now(),\n\t\tttlMs: input.ttlMs ?? DEFAULT_CACHE_TTL_MS,\n\t\ttelemetry: createMutableContextBudgetCacheTelemetryV2(input.planCacheKey),\n\t};\n}\n\nexport function readValidPlanCacheV2(input: {\n\treadonly cache: ContextBudgetSelectionCacheV2;\n\treadonly key: string;\n\treadonly planned: readonly PlannedItemV2[];\n\treadonly availableTokens: number;\n}): PromptContextBudgetPlanV2 | undefined {\n\tconst read = input.cache.provider?.readPlan(input.key);\n\tif (!read) return undefined;\n\tconst rejection = validateContextBudgetPlanCacheEntryV2({\n\t\tavailableTokens: input.availableTokens,\n\t\tcache: input.cache,\n\t\tentry: read.entry,\n\t\tplanned: input.planned,\n\t});\n\tif (rejection) {\n\t\tinput.cache.telemetry.planCache.rejectedReason = rejection;\n\t\treturn undefined;\n\t}\n\tinput.cache.telemetry.planCache.hit = true;\n\tinput.cache.telemetry.planCache.layer = read.layer;\n\treturn withCacheTelemetry(read.entry.plan, input.cache.telemetry);\n}\n\nexport function writePlanCacheV2(input: {\n\treadonly cache: ContextBudgetSelectionCacheV2;\n\treadonly key: string;\n\treadonly plan: PromptContextBudgetPlanV2;\n\treadonly planned: readonly PlannedItemV2[];\n}): void {\n\tif (!input.cache.provider || input.plan.emergency) return;\n\tif (input.plan.selectedRepresentations.some((representation) => representation.cache?.hit === true)) return;\n\tinput.cache.provider.writePlan({\n\t\tkey: input.key,\n\t\tentry: {\n\t\t\tcreatedAtEpochMs: input.cache.nowEpochMs,\n\t\t\tplan: input.plan,\n\t\t\tsourceHashes: sourceHashesByContextBudgetItemIdV2(input.planned),\n\t\t},\n\t});\n}\n"]}
|