mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45:22 +00:00
refactor(pi-compaction): 改为压力触发的观察遮蔽与空闲阈值整段摘要
原实现对所有工具结果(含 read)在下一次请求即替换为约 4K 的首尾存根, 不看上下文压力,导致模型看不到大文件中段;后台逐条语义摘要又反复改写 历史、打碎 prompt 缓存。参照 Anthropic 上下文编辑、Manus 与 JetBrains 观察遮蔽研究重构为: - L2:上下文达到阈值时在 turn_end 批量提交 Pi 原生 context_edit,把最近 keepRecentTurns 轮之外的旧 toolResult 换成确定性占位符;trigger/target 回差与 minClearTokens 保证少触发、一次清够;原始条目保留,可经 context_tree_query 取回 - L3:空闲 agent_settled 且超过绝对阈值时调用一次 ctx.compact - 阈值按绝对 token 计并受窗口比例约束,默认针对 1M 窗口(DeepSeek V4.1 Flash):遮蔽 200K→100K、整段摘要 400K - 删除逐条存根、语义 worker、soft/hard/target 比例与自行取消 Pi 压缩的门控; Pi 原生阈值压缩保留为最终安全网 Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent
a254d4fd68
commit
a6d8416c76
@@ -2,34 +2,82 @@ import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { DEFAULT_CONFIG, normalizeConfig } from "../src/config.ts";
|
||||
|
||||
test("normalizes one greenfield piCompaction config without legacy aliases", () => {
|
||||
test("normalizes a full pi-compaction config", () => {
|
||||
const config = normalizeConfig({
|
||||
enabled: false,
|
||||
softRatio: 0.7,
|
||||
hardRatio: 0.91,
|
||||
targetRatio: 0.5,
|
||||
deterministic: { enabled: false, maxStubTokens: 512 },
|
||||
semantic: { enabled: false, concurrency: 2, maxRetries: 0 },
|
||||
masking: {
|
||||
enabled: false,
|
||||
triggerTokens: 150_000,
|
||||
triggerRatio: 0.5,
|
||||
targetTokens: 60_000,
|
||||
targetRatio: 0.2,
|
||||
minClearTokens: 30_000,
|
||||
keepRecentTurns: 5,
|
||||
minEntryTokens: 512,
|
||||
excludeTools: ["bash", "read"],
|
||||
},
|
||||
summary: { triggerTokens: 300_000, triggerRatio: 0.8, thinking: "medium" },
|
||||
native: { enabled: false },
|
||||
});
|
||||
assert.equal(config.enabled, false);
|
||||
assert.equal(config.deterministic.enabled, false);
|
||||
assert.equal(config.deterministic.maxStubTokens, 512);
|
||||
assert.equal(config.semantic.enabled, false);
|
||||
assert.equal(config.semantic.maxRetries, 0);
|
||||
assert.deepEqual(config.masking, {
|
||||
enabled: false,
|
||||
triggerTokens: 150_000,
|
||||
triggerRatio: 0.5,
|
||||
targetTokens: 60_000,
|
||||
targetRatio: 0.2,
|
||||
minClearTokens: 30_000,
|
||||
keepRecentTurns: 5,
|
||||
minEntryTokens: 512,
|
||||
excludeTools: ["bash", "read"],
|
||||
});
|
||||
assert.equal(config.summary.triggerTokens, 300_000);
|
||||
assert.equal(config.summary.triggerRatio, 0.8);
|
||||
assert.equal(config.summary.thinking, "medium");
|
||||
assert.equal(config.native.enabled, false);
|
||||
});
|
||||
|
||||
test("invalid ratios and bounds fail safe to bounded defaults", () => {
|
||||
test("invalid fields fail safe to bounded defaults", () => {
|
||||
const config = normalizeConfig({
|
||||
softRatio: 2,
|
||||
hardRatio: -1,
|
||||
targetRatio: 0.99,
|
||||
semantic: { concurrency: 50, maxRetries: -1 },
|
||||
enabled: "yes",
|
||||
masking: {
|
||||
triggerTokens: -1,
|
||||
triggerRatio: 2,
|
||||
targetTokens: "a lot",
|
||||
targetRatio: 0,
|
||||
minClearTokens: 0,
|
||||
keepRecentTurns: 500,
|
||||
minEntryTokens: -3,
|
||||
excludeTools: "read",
|
||||
},
|
||||
summary: { triggerTokens: Number.NaN, triggerRatio: 1.5, thinking: "insane" },
|
||||
native: { enabled: 1 },
|
||||
});
|
||||
assert.equal(config.softRatio, DEFAULT_CONFIG.softRatio);
|
||||
assert.ok(config.targetRatio < config.softRatio);
|
||||
assert.ok(config.hardRatio > config.softRatio);
|
||||
assert.equal(config.semantic.concurrency, DEFAULT_CONFIG.semantic.concurrency);
|
||||
assert.equal(config.semantic.maxRetries, DEFAULT_CONFIG.semantic.maxRetries);
|
||||
assert.equal(config.enabled, DEFAULT_CONFIG.enabled);
|
||||
assert.deepEqual(config.masking, DEFAULT_CONFIG.masking);
|
||||
assert.equal(config.summary.triggerTokens, DEFAULT_CONFIG.summary.triggerTokens);
|
||||
assert.equal(config.summary.triggerRatio, DEFAULT_CONFIG.summary.triggerRatio);
|
||||
assert.equal(config.summary.thinking, DEFAULT_CONFIG.summary.thinking);
|
||||
assert.equal(config.native.enabled, DEFAULT_CONFIG.native.enabled);
|
||||
});
|
||||
|
||||
test("masking target is corrected below trigger and summary above masking", () => {
|
||||
const config = normalizeConfig({
|
||||
masking: { triggerTokens: 80_000, triggerRatio: 0.5, targetTokens: 120_000, targetRatio: 0.9 },
|
||||
summary: { triggerTokens: 50_000, triggerRatio: 0.4 },
|
||||
});
|
||||
assert.equal(config.masking.targetTokens, 40_000);
|
||||
assert.equal(config.masking.targetRatio, 0.25);
|
||||
assert.equal(config.summary.triggerTokens, 160_000);
|
||||
assert.ok(config.summary.triggerRatio > config.masking.triggerRatio);
|
||||
});
|
||||
|
||||
test("minClearTokens and excludeTools accept valid overrides", () => {
|
||||
const config = normalizeConfig({
|
||||
masking: { minClearTokens: 1, keepRecentTurns: 1, minEntryTokens: 0, excludeTools: [] },
|
||||
});
|
||||
assert.equal(config.masking.minClearTokens, 1);
|
||||
assert.equal(config.masking.keepRecentTurns, 1);
|
||||
assert.equal(config.masking.minEntryTokens, 0);
|
||||
assert.deepEqual(config.masking.excludeTools, []);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { performance } from "node:perf_hooks";
|
||||
import piCompaction from "../index.ts";
|
||||
import { getCompactionService } from "../src/service.ts";
|
||||
import { MASK_PREFIX } from "../src/masking.ts";
|
||||
|
||||
function harness() {
|
||||
const handlers = new Map<string, Function[]>();
|
||||
@@ -24,90 +25,253 @@ function harness() {
|
||||
return { pi, handlers, tools, commands, entries };
|
||||
}
|
||||
|
||||
function context(branch: any[] = []) {
|
||||
return {
|
||||
function context(branch: any[] = [], tokens = 1_000) {
|
||||
const compactCalls: any[] = [];
|
||||
const sessionId = `session-${Math.random().toString(36).slice(2)}`;
|
||||
const ctx = {
|
||||
sessionManager: {
|
||||
getSessionId: () => "session-test",
|
||||
getSessionId: () => sessionId,
|
||||
getBranch: () => branch,
|
||||
getLeafId: () => "leaf-1",
|
||||
},
|
||||
model: { provider: "test", api: "test", id: "model", contextWindow: 100_000 },
|
||||
modelRegistry: {},
|
||||
getSystemPrompt: () => "system",
|
||||
getContextUsage: () => ({ tokens: 1_000, contextWindow: 100_000, percent: 1 }),
|
||||
getContextUsage: () => ({ tokens, contextWindow: 100_000, percent: 1 }),
|
||||
compact(options: unknown) { compactCalls.push(options); },
|
||||
isIdle: () => true,
|
||||
hasUI: false,
|
||||
abort() {},
|
||||
} as any;
|
||||
return { ctx, compactCalls };
|
||||
}
|
||||
|
||||
test("one runtime projects tool evidence and recovers it through context_tree_query", async () => {
|
||||
function projectedEntry(id: string, message: any) {
|
||||
return { sourceEntry: { id, type: "message", message }, messages: [message] };
|
||||
}
|
||||
|
||||
function assistantMessage(id: string, calls: Array<{ id: string; name: string; args?: unknown }>) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: calls.map((call) => ({ type: "toolCall", id: call.id, name: call.name, arguments: call.args ?? {} })),
|
||||
timestamp: id,
|
||||
};
|
||||
}
|
||||
|
||||
function toolResultMessage(id: string, toolCallId: string, text: string, toolName = "bash") {
|
||||
return {
|
||||
role: "toolResult",
|
||||
toolCallId,
|
||||
toolName,
|
||||
content: [{ type: "text", text }],
|
||||
timestamp: id,
|
||||
};
|
||||
}
|
||||
|
||||
// Default config: masking trigger = min(200K, 60% of window); at the harness
|
||||
// 100K window that is 60K tokens and the target is 35K, so 60K used needs
|
||||
// ~25K tokens of maskable results outside the 10-turn protected window.
|
||||
function pressurizedContextEntries() {
|
||||
const entries = [
|
||||
projectedEntry("a1", assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }])),
|
||||
projectedEntry("r1", toolResultMessage("r1", "c1", `failure evidence\n${"x".repeat(120_000)}\nstack tail`)),
|
||||
];
|
||||
for (let index = 0; index < 10; index++) {
|
||||
entries.push(projectedEntry(`keep-${index}`, assistantMessage(`keep-${index}`, [])));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
test("turn_end masks old tool results under pressure and preserves prior drafts", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const ctx = context();
|
||||
const { ctx } = context([], 60_000);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
const assistant = {
|
||||
role: "assistant",
|
||||
content: [{ type: "toolCall", id: "call-1", name: "bash", arguments: { command: "npm test" } }],
|
||||
timestamp: 10,
|
||||
};
|
||||
const result = {
|
||||
role: "toolResult",
|
||||
toolCallId: "call-1",
|
||||
toolName: "bash",
|
||||
content: [{ type: "text", text: `failure evidence\n${"x".repeat(12_000)}\nstack tail` }],
|
||||
isError: true,
|
||||
timestamp: 11,
|
||||
};
|
||||
await h.handlers.get("turn_end")![0]!({ message: assistant, toolResults: [result] }, ctx);
|
||||
const projected = await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
|
||||
const prior = { type: "custom", customType: "other-extension", data: {} };
|
||||
const result = await h.handlers.get("turn_end")![0]!({
|
||||
entries: [prior],
|
||||
message: assistantMessage("a1", []),
|
||||
toolResults: [],
|
||||
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
|
||||
}, ctx);
|
||||
|
||||
assert.match(projected.messages[1].content[0].text, /pi-compaction tool=bash/);
|
||||
const cacheStart = performance.now();
|
||||
for (let iteration = 0; iteration < 1_000; iteration++) {
|
||||
await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
|
||||
}
|
||||
const cacheAverageMs = (performance.now() - cacheStart) / 1_000;
|
||||
assert.ok(cacheAverageMs < 2, `full context handler cache-hit average ${cacheAverageMs.toFixed(3)}ms exceeded 2ms`);
|
||||
assert.equal(h.entries.length, 1);
|
||||
assert.equal(h.entries[0]!.customType, "pi-compaction");
|
||||
|
||||
const query = h.tools.get("context_tree_query");
|
||||
const recovered = await query.execute("q", { toolCallIds: ["call-1"] }, new AbortController().signal, undefined, ctx);
|
||||
assert.match(recovered.content[0].text, /failure evidence/);
|
||||
assert.ok(result?.entries);
|
||||
assert.strictEqual(result.entries[0], prior);
|
||||
const edits = result.entries.slice(1);
|
||||
assert.equal(edits.length, 1);
|
||||
assert.equal(edits[0]!.type, "context_edit");
|
||||
assert.equal(edits[0]!.targetId, "r1");
|
||||
assert.ok(edits[0]!.replacement.content.startsWith(MASK_PREFIX));
|
||||
// Masking is append-only context_edit; nothing is written as custom entries.
|
||||
assert.equal(h.entries.length, 0);
|
||||
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("small tool results are passed through without a stub wrapper", async () => {
|
||||
test("turn_end below the masking trigger returns undefined", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const ctx = context();
|
||||
const { ctx } = context([], 40_000);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
const assistant = {
|
||||
role: "assistant",
|
||||
content: [{ type: "toolCall", id: "call-small", name: "bash", arguments: { command: "ls" } }],
|
||||
timestamp: 20,
|
||||
};
|
||||
const result = {
|
||||
role: "toolResult",
|
||||
toolCallId: "call-small",
|
||||
toolName: "bash",
|
||||
content: [{ type: "text", text: "ok\nfile-a.ts\nfile-b.ts" }],
|
||||
timestamp: 21,
|
||||
};
|
||||
const result = await h.handlers.get("turn_end")![0]!({
|
||||
entries: [],
|
||||
message: assistantMessage("a1", []),
|
||||
toolResults: [],
|
||||
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
|
||||
}, ctx);
|
||||
assert.equal(result, undefined);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
await h.handlers.get("turn_end")![0]!({ message: assistant, toolResults: [result] }, ctx);
|
||||
const projected = await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
|
||||
test("context hook returns messages untouched on the non-checkpoint path", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const { ctx } = context();
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
assert.strictEqual(projected.messages[1].content[0].text, "ok\nfile-a.ts\nfile-b.ts");
|
||||
assert.doesNotMatch(projected.messages[1].content[0].text, /pi-compaction tool=/);
|
||||
const messages = [assistantMessage("a1", []), toolResultMessage("r1", "c1", "visible\nfile-a.ts")];
|
||||
const projected = await h.handlers.get("context")![0]!({ messages }, ctx);
|
||||
assert.equal(projected, undefined);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("agent_settled compacts once above the summary threshold and never repeats on the same leaf", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const { ctx, compactCalls } = context([], 95_000);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
|
||||
assert.equal(compactCalls.length, 1);
|
||||
assert.equal(typeof compactCalls[0].onError, "function");
|
||||
|
||||
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
|
||||
assert.equal(compactCalls.length, 1);
|
||||
|
||||
ctx.sessionManager.getLeafId = () => "leaf-2";
|
||||
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
|
||||
assert.equal(compactCalls.length, 2);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("agent_settled stays idle below the summary threshold", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const { ctx, compactCalls } = context([], 50_000);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
|
||||
assert.equal(compactCalls.length, 0);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("context_tree_query recovers the original tool output from the branch", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const branch = [
|
||||
{ type: "message", id: "a1", parentId: null, timestamp: "t", message: assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }]) },
|
||||
{ type: "message", id: "r1", parentId: "a1", timestamp: "t", message: toolResultMessage("r1", "c1", "failure evidence\nstack tail") },
|
||||
];
|
||||
const { ctx } = context(branch);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
// Original evidence stays recoverable even when the message is passed through.
|
||||
const query = h.tools.get("context_tree_query")!;
|
||||
const recovered = await query.execute("q", { toolCallIds: ["call-small"] }, new AbortController().signal, undefined, ctx);
|
||||
assert.match(recovered.content[0].text, /file-a\.ts/);
|
||||
const recovered = await query.execute("q", { toolCallIds: ["c1"] }, new AbortController().signal, undefined, ctx);
|
||||
assert.match(recovered.content[0].text, /failure evidence/);
|
||||
assert.match(recovered.content[0].text, /stack tail/);
|
||||
assert.deepEqual(recovered.details.missing, []);
|
||||
|
||||
const missing = await query.execute("q", { toolCallIds: ["nope"] }, new AbortController().signal, undefined, ctx);
|
||||
assert.match(missing.content[0].text, /not found/);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("service query returns placeholders only for masked tool call ids", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const { ctx } = context([], 60_000);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
const sessionId = ctx.sessionManager.getSessionId();
|
||||
|
||||
await h.handlers.get("turn_end")![0]!({
|
||||
entries: [],
|
||||
message: assistantMessage("a1", []),
|
||||
toolResults: [],
|
||||
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
|
||||
}, ctx);
|
||||
|
||||
const records = getCompactionService()!.query(sessionId, ["c1", "never-masked"]);
|
||||
assert.equal(records.length, 1);
|
||||
assert.equal(records[0]!.toolCallId, "c1");
|
||||
assert.ok(records[0]!.deterministicSummary.startsWith(MASK_PREFIX));
|
||||
assert.equal(typeof records[0]!.resultTimestamp, "number");
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("hydrating a branch with this extension's context_edit rebuilds masking metrics", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const placeholder = `${MASK_PREFIX}bash result cleared to save context (~1_000 tokens). Call args: {}. Re-run the call if still needed, or recover the original with context_tree_query {"toolCallIds":["c1"]}.`;
|
||||
const branch = [
|
||||
{ type: "message", id: "a1", parentId: null, timestamp: "t", message: assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }]) },
|
||||
{ type: "message", id: "r1", parentId: "a1", timestamp: "t", message: toolResultMessage("r1", "c1", "x".repeat(8_000)) },
|
||||
{ type: "context_edit", id: "e1", parentId: "r1", timestamp: "t", targetId: "r1", replacement: { content: placeholder } },
|
||||
];
|
||||
const { ctx } = context(branch);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
const sessionId = ctx.sessionManager.getSessionId();
|
||||
|
||||
await h.handlers.get("context")![0]!({ messages: [toolResultMessage("r1", "c1", "x".repeat(8_000))] }, ctx);
|
||||
const metrics = getCompactionService()!.getMetrics(sessionId);
|
||||
assert.equal(metrics!.maskedResults, 1);
|
||||
assert.ok(metrics!.reclaimedTokens > 0);
|
||||
assert.ok(metrics!.rawTokens > metrics!.effectiveTokens);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("session_before_compact no longer cancels threshold compactions", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const { ctx } = context();
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
ctx.modelRegistry.getApiKeyAndHeaders = async () => ({ ok: false, error: "no credentials" });
|
||||
|
||||
const result = await h.handlers.get("session_before_compact")![0]!({
|
||||
reason: "threshold", branchEntries: [], preparation: { firstKeptEntryId: "u1" },
|
||||
signal: new AbortController().signal,
|
||||
}, ctx);
|
||||
assert.equal(result, undefined);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
test("native checkpoint projection applies context edits made after the checkpoint", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const details = {
|
||||
kind: "pi-compaction",
|
||||
version: 1,
|
||||
checkpoint: "native",
|
||||
modelKey: "test:test:model",
|
||||
replacementHistory: [{ type: "compaction", encrypted_content: "opaque" }],
|
||||
portableSummary: "checkpoint summary",
|
||||
sourceFingerprint: "fp",
|
||||
};
|
||||
const branch = [
|
||||
{ type: "compaction", id: "c1", parentId: null, timestamp: "t", summary: "marker", firstKeptEntryId: "r1", tokensBefore: 1_000, details },
|
||||
{ type: "message", id: "r1", parentId: "c1", timestamp: "t", message: toolResultMessage("r1", "c1", "ORIGINAL-CONTENT-HERE") },
|
||||
{ type: "context_edit", id: "e1", parentId: "r1", timestamp: "t", targetId: "r1", replacement: { content: `${MASK_PREFIX}bash result cleared to save context (~5 tokens). Call args: {}.` } },
|
||||
{ type: "message", id: "u1", parentId: "e1", timestamp: "t", message: { role: "user", content: "next question", timestamp: 30 } },
|
||||
] as any[];
|
||||
const { ctx } = context(branch);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
|
||||
const projected = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
|
||||
const serialized = JSON.stringify(projected.messages);
|
||||
assert.doesNotMatch(serialized, /ORIGINAL-CONTENT-HERE/);
|
||||
assert.match(serialized, /result cleared to save context/);
|
||||
assert.match(serialized, /next question/);
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
@@ -127,7 +291,7 @@ test("model switching after a native checkpoint projects the portable checkpoint
|
||||
{ type: "compaction", id: "c1", parentId: null, timestamp: new Date().toISOString(), summary: "marker", firstKeptEntryId: "m0", tokensBefore: 1_000, details },
|
||||
{ type: "message", id: "m1", parentId: "c1", timestamp: new Date().toISOString(), message: { role: "user", content: "continue", timestamp: 20 } },
|
||||
] as any[];
|
||||
const ctx = context(branch);
|
||||
const { ctx } = context(branch);
|
||||
await h.handlers.get("session_start")![0]!({}, ctx);
|
||||
const projected = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
|
||||
assert.match(projected.messages[0].content[0].text, /decisions and files/);
|
||||
@@ -143,7 +307,7 @@ for (const provider of ["sub-api", "openai-codex"]) {
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Remember the test decision.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
const { ctx } = context(branch);
|
||||
ctx.model = {
|
||||
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",
|
||||
id: "selected-model", input: ["text"], contextWindow: 100_000,
|
||||
@@ -231,7 +395,7 @@ test("SubAPI native failures and cancellation return control to Pi without a che
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Test only.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
const { ctx } = context(branch);
|
||||
ctx.model = { provider: "sub-api", api: "openai-responses", id: "test-model", input: ["text"], baseUrl: "https://gateway.example/v1" };
|
||||
ctx.modelRegistry.getApiKeyAndHeaders = async () => ({ ok: true, apiKey: "test-key" });
|
||||
const responses = [
|
||||
@@ -266,7 +430,7 @@ test("SubAPI native failures and cancellation return control to Pi without a che
|
||||
test("other providers and SubAPI Chat Completions keep their provider requests unchanged", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const ctx = context();
|
||||
const { ctx } = context();
|
||||
try {
|
||||
for (const model of [
|
||||
{ provider: "other", api: "openai-responses", id: "test" },
|
||||
@@ -302,7 +466,7 @@ test("native runtime delegates authentication to each provider without blocking
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Synthetic authentication test.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
const { ctx } = context(branch);
|
||||
const provider = scenario.provider ?? "sub-api";
|
||||
ctx.model = {
|
||||
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { MASK_PREFIX, planMasking, rebuildMaskedLedger } from "../src/masking.ts";
|
||||
import { effectiveThreshold } from "../src/token-accounting.ts";
|
||||
import type { MaskingConfig } from "../src/types.ts";
|
||||
|
||||
const MASKING: MaskingConfig = {
|
||||
enabled: true,
|
||||
triggerTokens: 1_000,
|
||||
triggerRatio: 0.9,
|
||||
targetTokens: 400,
|
||||
targetRatio: 0.35,
|
||||
minClearTokens: 200,
|
||||
keepRecentTurns: 2,
|
||||
minEntryTokens: 10,
|
||||
excludeTools: ["ask_user_question"],
|
||||
};
|
||||
|
||||
const estimate = (message: any): number => Math.ceil(JSON.stringify(message).length / 4);
|
||||
|
||||
function assistantMessage(id: string, calls: Array<{ id: string; name: string; args?: unknown }>) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: calls.map((call) => ({ type: "toolCall", id: call.id, name: call.name, arguments: call.args ?? {} })),
|
||||
timestamp: id,
|
||||
};
|
||||
}
|
||||
|
||||
function toolResultMessage(id: string, toolCallId: string, text: string, toolName = "bash") {
|
||||
return {
|
||||
role: "toolResult",
|
||||
toolCallId,
|
||||
toolName,
|
||||
content: [{ type: "text", text }],
|
||||
timestamp: id,
|
||||
};
|
||||
}
|
||||
|
||||
function entry(id: string, message: any, messages = [message]) {
|
||||
return { sourceEntry: { id, type: "message", message }, messages };
|
||||
}
|
||||
|
||||
function protect(turns = MASKING.keepRecentTurns) {
|
||||
return Array.from({ length: turns }, (_unused, index) =>
|
||||
entry(`recent-${index}`, { role: "assistant", content: [{ type: "text", text: "ok" }], timestamp: `recent-${index}` }));
|
||||
}
|
||||
|
||||
test("below the effective trigger plans no drafts", () => {
|
||||
const entries = [
|
||||
entry("e1", assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "ls" } }])),
|
||||
entry("e2", toolResultMessage("r1", "c1", "x".repeat(8_000))),
|
||||
...protect(),
|
||||
];
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 999, contextWindow: 0, masking: MASKING, estimate });
|
||||
assert.equal(plan.drafts.length, 0);
|
||||
assert.equal(plan.masked.length, 0);
|
||||
});
|
||||
|
||||
test("above the trigger masks oldest first until context fits under target", () => {
|
||||
const entries = [
|
||||
entry("e1", assistantMessage("a1", [
|
||||
{ id: "c1", name: "bash", args: { command: "one" } },
|
||||
{ id: "c2", name: "bash", args: { command: "two" } },
|
||||
{ id: "c3", name: "bash", args: { command: "three" } },
|
||||
])),
|
||||
entry("e2", toolResultMessage("r1", "c1", "a".repeat(4_000))),
|
||||
entry("e3", toolResultMessage("r2", "c2", "b".repeat(4_000))),
|
||||
entry("e4", toolResultMessage("r3", "c3", "c".repeat(4_000))),
|
||||
entry("e5", toolResultMessage("r4", "c4", "recent result".repeat(200))),
|
||||
...protect(),
|
||||
];
|
||||
// needed = 2_000 - 400 = 1_600; each result clears ~980 tokens.
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 2_000, contextWindow: 0, masking: MASKING, estimate });
|
||||
assert.deepEqual(plan.drafts.map((draft) => draft.targetId), ["e2", "e3"]);
|
||||
});
|
||||
|
||||
test("results inside the recent keepRecentTurns window are never masked", () => {
|
||||
const entries = [
|
||||
entry("e1", toolResultMessage("r1", "c1", "a".repeat(4_000))),
|
||||
entry("recent-a", assistantMessage("ra", [{ id: "rc", name: "bash", args: {} }])),
|
||||
entry("recent-r", toolResultMessage("rr", "rc", "b".repeat(4_000))),
|
||||
entry("recent-b", assistantMessage("rb", [])),
|
||||
];
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 3_500, contextWindow: 0, masking: MASKING, estimate });
|
||||
assert.deepEqual(plan.drafts.map((draft) => draft.targetId), ["e1"]);
|
||||
});
|
||||
|
||||
test("excluded tools, SKILL.md reads, already-masked and small results are skipped", () => {
|
||||
const entries = [
|
||||
entry("e1", assistantMessage("a1", [
|
||||
{ id: "c1", name: "ask_user_question", args: { question: "x" } },
|
||||
{ id: "c2", name: "read", args: { path: "/repo/skills/foo/SKILL.md" } },
|
||||
{ id: "c3", name: "bash", args: { command: "old" } },
|
||||
{ id: "c4", name: "bash", args: { command: "tiny" } },
|
||||
])),
|
||||
entry("e2", toolResultMessage("r1", "c1", "a".repeat(4_000), "ask_user_question")),
|
||||
entry("e3", toolResultMessage("r2", "c2", "b".repeat(4_000), "read")),
|
||||
entry("e4", toolResultMessage("r3", "c3", `${MASK_PREFIX}bash result cleared to save context (~900 tokens).`), ),
|
||||
entry("e5", toolResultMessage("r4", "c4", "tiny")),
|
||||
entry("e6", toolResultMessage("r5", "c5", "d".repeat(4_000))),
|
||||
...protect(),
|
||||
];
|
||||
const masking = { ...MASKING, minClearTokens: 100, minEntryTokens: 100 };
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 3_500, contextWindow: 0, masking, estimate });
|
||||
assert.deepEqual(plan.drafts.map((draft) => draft.targetId), ["e6"]);
|
||||
assert.equal(plan.masked[0]!.toolCallId, "c5");
|
||||
});
|
||||
|
||||
test("when clearable volume stays under minClear nothing is masked", () => {
|
||||
const entries = [
|
||||
entry("e1", toolResultMessage("r1", "c1", "a".repeat(600))),
|
||||
...protect(),
|
||||
];
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 3_500, contextWindow: 0, masking: MASKING, estimate });
|
||||
// ~130 tokens clearable, below minClearTokens (200) → not worth invalidating cache.
|
||||
assert.equal(plan.drafts.length, 0);
|
||||
});
|
||||
|
||||
test("placeholder is deterministic, names the tool and call id, and truncates args at 300 chars", () => {
|
||||
const longArgs = { command: `run ${"v".repeat(600)}` };
|
||||
const entries = [
|
||||
entry("e1", assistantMessage("a1", [{ id: "c1", name: "bash", args: longArgs }])),
|
||||
entry("e2", toolResultMessage("r1", "c1", "a".repeat(4_000))),
|
||||
...protect(),
|
||||
];
|
||||
const masking = { ...MASKING, minClearTokens: 1 };
|
||||
const first = planMasking({ contextEntries: entries, currentTokens: 3_500, contextWindow: 0, masking, estimate });
|
||||
const second = planMasking({ contextEntries: entries, currentTokens: 3_500, contextWindow: 0, masking, estimate });
|
||||
assert.equal(first.drafts.length, 1);
|
||||
const placeholder = first.masked[0]!.placeholder;
|
||||
assert.equal(placeholder, second.masked[0]!.placeholder);
|
||||
assert.ok(placeholder.startsWith(MASK_PREFIX));
|
||||
assert.match(placeholder, /bash result cleared/);
|
||||
assert.match(placeholder, /"toolCallIds":\["c1"\]/);
|
||||
const argsText = placeholder.match(/Call args: (.*)\. Re-run/)?.[1] ?? "";
|
||||
assert.ok(argsText.length <= 301, `args excerpt ${argsText.length} exceeded 300+ellipsis`);
|
||||
assert.ok(placeholder.includes("…"));
|
||||
});
|
||||
|
||||
test("the small-window ratio caps both trigger and target", () => {
|
||||
const masking: MaskingConfig = {
|
||||
...MASKING,
|
||||
triggerTokens: 200_000,
|
||||
triggerRatio: 0.6,
|
||||
targetTokens: 100_000,
|
||||
targetRatio: 0.35,
|
||||
minClearTokens: 50_000,
|
||||
};
|
||||
assert.equal(effectiveThreshold(masking.triggerTokens, masking.triggerRatio, 272_000), 163_200);
|
||||
assert.equal(effectiveThreshold(masking.targetTokens, masking.targetRatio, 272_000), 95_200);
|
||||
const entries = [
|
||||
entry("e1", toolResultMessage("r1", "c1", "a".repeat(300_000))),
|
||||
...protect(),
|
||||
];
|
||||
const below = planMasking({ contextEntries: entries, currentTokens: 163_199, contextWindow: 272_000, masking, estimate });
|
||||
assert.equal(below.drafts.length, 0);
|
||||
const above = planMasking({ contextEntries: entries, currentTokens: 163_200, contextWindow: 272_000, masking, estimate });
|
||||
assert.equal(above.drafts.length, 1);
|
||||
});
|
||||
|
||||
test("ledger rebuild counts only masked targets still inside the active context", () => {
|
||||
const placeholder = (id: string) =>
|
||||
`${MASK_PREFIX}bash result cleared to save context (~100 tokens). Call args: {}. Re-run the call if still needed, or recover the original with context_tree_query {"toolCallIds":["${id}"]}.`;
|
||||
const branch = [
|
||||
{ type: "message", id: "u1", parentId: null, timestamp: "t", message: { role: "user", content: "start", timestamp: 1 } },
|
||||
{ type: "message", id: "r-old", parentId: "u1", timestamp: "t", message: toolResultMessage("m-old", "c-old", "o".repeat(2_000)) },
|
||||
{ type: "context_edit", id: "e-old", parentId: "r-old", timestamp: "t", targetId: "r-old", replacement: { content: placeholder("c-old") } },
|
||||
{ type: "message", id: "m-keep", parentId: "e-old", timestamp: "t", message: { role: "user", content: "kept question", timestamp: 2 } },
|
||||
{ type: "compaction", id: "c1", parentId: "m-keep", timestamp: "t", summary: "summary", firstKeptEntryId: "m-keep", tokensBefore: 9_999 },
|
||||
{ type: "message", id: "a-keep", parentId: "c1", timestamp: "t", message: assistantMessage("a-keep", []) },
|
||||
{ type: "message", id: "r-new", parentId: "a-keep", timestamp: "t", message: toolResultMessage("m-new", "c-new", "n".repeat(2_000)) },
|
||||
{ type: "context_edit", id: "e-new", parentId: "r-new", timestamp: "t", targetId: "r-new", replacement: { content: placeholder("c-new") } },
|
||||
];
|
||||
const ledger = rebuildMaskedLedger(branch, "e-new", estimate);
|
||||
assert.equal(ledger.size, 1);
|
||||
assert.ok(ledger.has("c-new"));
|
||||
assert.ok(!ledger.has("c-old"));
|
||||
});
|
||||
|
||||
test("user and assistant messages are never masked", () => {
|
||||
const entries = [
|
||||
entry("e1", { role: "user", content: "u".repeat(8_000), timestamp: "u1" }),
|
||||
entry("e2", { role: "assistant", content: [{ type: "text", text: "a".repeat(8_000) }], timestamp: "a1" }),
|
||||
...protect(),
|
||||
];
|
||||
const plan = planMasking({ contextEntries: entries, currentTokens: 9_000, contextWindow: 0, masking: { ...MASKING, keepRecentTurns: 1, minClearTokens: 1 }, estimate });
|
||||
assert.equal(plan.drafts.length, 0);
|
||||
});
|
||||
@@ -10,9 +10,9 @@ function message(index: number) {
|
||||
|
||||
test("cached projection and append-only delta stay inside the hot-path budget", () => {
|
||||
const index = new SegmentIndex("perf");
|
||||
index.hydrate([], DEFAULT_CONFIG);
|
||||
index.hydrate();
|
||||
const messages = Array.from({ length: 10_000 }, (_, value) => message(value));
|
||||
const params = { environmentHash: "env", environmentTokens: 2_000, contextWindow: 300_000 };
|
||||
const params = { environmentHash: "env", environmentTokens: 2_000, contextWindow: 300_000, maskedResults: 0, reclaimedTokens: 0 };
|
||||
index.project(messages, params);
|
||||
|
||||
const cacheStart = performance.now();
|
||||
@@ -24,6 +24,7 @@ test("cached projection and append-only delta stay inside the hot-path budget",
|
||||
index.project(appended, params);
|
||||
const deltaMs = performance.now() - deltaStart;
|
||||
|
||||
console.log(`cache-hit average ${cacheAverageMs.toFixed(3)}ms; delta projection ${deltaMs.toFixed(3)}ms`);
|
||||
assert.ok(cacheAverageMs < 2, `cache-hit average ${cacheAverageMs.toFixed(3)}ms exceeded 2ms`);
|
||||
assert.ok(deltaMs < 10, `delta projection ${deltaMs.toFixed(3)}ms exceeded 10ms`);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { DEFAULT_CONFIG } from "../src/config.ts";
|
||||
import { SegmentIndex, deterministicSummary } from "../src/segment-index.ts";
|
||||
import { SegmentIndex } from "../src/segment-index.ts";
|
||||
|
||||
const assistant = {
|
||||
role: "assistant",
|
||||
@@ -17,66 +16,96 @@ const result = {
|
||||
timestamp: 11,
|
||||
};
|
||||
|
||||
test("deterministic summary is bounded and preserves head/tail evidence", () => {
|
||||
const summary = deterministicSummary({
|
||||
toolName: "bash",
|
||||
args: { command: "npm test" },
|
||||
rawText: result.content[0]!.text,
|
||||
isError: true,
|
||||
maxStubTokens: 256,
|
||||
// Regression: a ~14.5K-char read result projected under a 1M window must reach
|
||||
// the model byte-for-byte unchanged — masking only happens via context_edit
|
||||
// drafts under real pressure, never on the hot projection path.
|
||||
test("projection is byte-for-byte identity for a 300-line read result under a 1M window", () => {
|
||||
const readBody = Array.from({ length: 300 }, (_unused, index) => `line ${index + 1}: ${"y".repeat(38)}`).join("\n");
|
||||
const readResult = {
|
||||
role: "toolResult",
|
||||
toolCallId: "call-read",
|
||||
toolName: "read",
|
||||
content: [{ type: "text", text: readBody }],
|
||||
timestamp: 12,
|
||||
};
|
||||
assert.ok(readBody.length > 14_000 && readBody.length < 15_000, `fixture ${readBody.length} chars`);
|
||||
|
||||
const index = new SegmentIndex("session-1");
|
||||
index.hydrate();
|
||||
const snapshot = index.project([assistant, readResult], {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 100,
|
||||
contextWindow: 1_000_000,
|
||||
maskedResults: 0,
|
||||
reclaimedTokens: 0,
|
||||
});
|
||||
assert.match(summary, /tool=bash status=error/);
|
||||
assert.match(summary, /failure/);
|
||||
assert.match(summary, /stack tail/);
|
||||
assert.ok(summary.length <= 1_100);
|
||||
|
||||
assert.strictEqual(snapshot.messages[1], readResult);
|
||||
assert.strictEqual(snapshot.messages[1]!.content[0].text, readBody);
|
||||
assert.ok(snapshot.messages[1]!.content[0].text.includes("line 150:"));
|
||||
assert.equal(snapshot.reclaimedTokens, 0);
|
||||
assert.equal(snapshot.maskedResults, 0);
|
||||
});
|
||||
|
||||
test("projection incrementally replaces tool results and caches identical input", () => {
|
||||
const index = new SegmentIndex("session-1");
|
||||
index.hydrate([], DEFAULT_CONFIG);
|
||||
const records = index.captureTurn(assistant, [result], DEFAULT_CONFIG);
|
||||
assert.equal(records.length, 1);
|
||||
|
||||
test("projection caches identical input and handles append-only deltas", () => {
|
||||
const index = new SegmentIndex("session-2");
|
||||
index.hydrate();
|
||||
const messages = [assistant, result];
|
||||
const first = index.project(messages, {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 100,
|
||||
contextWindow: 300_000,
|
||||
});
|
||||
const second = index.project(messages, {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 100,
|
||||
contextWindow: 300_000,
|
||||
});
|
||||
|
||||
const params = { environmentHash: "env", environmentTokens: 100, contextWindow: 300_000, maskedResults: 0, reclaimedTokens: 0 };
|
||||
const first = index.project(messages, params);
|
||||
const second = index.project(messages, params);
|
||||
assert.strictEqual(first, second);
|
||||
assert.ok(first.effectiveTokens < first.rawTokens);
|
||||
assert.match(first.messages[1]!.content[0].text, /context_tree_query/);
|
||||
assert.ok(first.rawTokens > 0);
|
||||
|
||||
const record = records[0]!;
|
||||
assert.ok(index.applySemantic(record.sourceKey, record.sourceHash, "- npm test failed\n- stack tail"));
|
||||
const semantic = index.project(messages, {
|
||||
const appended = index.project([...messages, { role: "user", content: "next", timestamp: 13 }], params);
|
||||
assert.notStrictEqual(appended, first);
|
||||
assert.equal(appended.messages.length, 3);
|
||||
assert.strictEqual(appended.messages[1], result);
|
||||
});
|
||||
|
||||
test("changed masking stats publish a new snapshot without mutating the old one", () => {
|
||||
const index = new SegmentIndex("session-stats");
|
||||
index.hydrate();
|
||||
const params = {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 100,
|
||||
contextWindow: 300_000,
|
||||
});
|
||||
assert.notStrictEqual(semantic, first);
|
||||
assert.match(semantic.messages[1]!.content[0].text, /npm test failed/);
|
||||
maskedResults: 0,
|
||||
reclaimedTokens: 0,
|
||||
};
|
||||
const first = index.project([assistant, result], params);
|
||||
const same = index.project([assistant, result], params);
|
||||
assert.strictEqual(same, first);
|
||||
|
||||
const updated = index.project([assistant, result], { ...params, maskedResults: 1, reclaimedTokens: 500 });
|
||||
assert.notStrictEqual(updated, first);
|
||||
assert.equal(updated.maskedResults, 1);
|
||||
assert.equal(updated.reclaimedTokens, 500);
|
||||
assert.equal(updated.rawTokens, updated.effectiveTokens + 500);
|
||||
// The previously published object stays immutable.
|
||||
assert.equal(first.maskedResults, 0);
|
||||
assert.equal(first.reclaimedTokens, 0);
|
||||
// Unchanged stats keep returning the same cached object.
|
||||
assert.strictEqual(index.project([assistant, result], { ...params, maskedResults: 1, reclaimedTokens: 500 }), updated);
|
||||
});
|
||||
|
||||
test("usage-backed baseline is adjusted only by projected delta", () => {
|
||||
const index = new SegmentIndex("session-2");
|
||||
index.hydrate([], DEFAULT_CONFIG);
|
||||
const index = new SegmentIndex("session-3");
|
||||
index.hydrate();
|
||||
const initial = index.project([assistant], {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 50,
|
||||
contextWindow: 100_000,
|
||||
maskedResults: 0,
|
||||
reclaimedTokens: 0,
|
||||
});
|
||||
index.recordUsage(1_000, initial.messageEstimate, "env");
|
||||
const next = index.project([assistant, { role: "user", content: "next", timestamp: 12 }], {
|
||||
environmentHash: "env",
|
||||
environmentTokens: 50,
|
||||
contextWindow: 100_000,
|
||||
maskedResults: 0,
|
||||
reclaimedTokens: 0,
|
||||
});
|
||||
assert.equal(next.confidence, "usage-backed");
|
||||
assert.ok(next.effectiveTokens >= 1_000);
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { shouldCompact } from "../src/token-accounting.ts";
|
||||
import { effectiveThreshold } from "../src/token-accounting.ts";
|
||||
|
||||
test("raw pressure cannot trigger compaction while effective lower-bound stays below 90%", () => {
|
||||
const config = { hardRatio: 0.9 };
|
||||
const snapshot = { lowerBound: 231_000, contextWindow: 300_000 };
|
||||
assert.equal(shouldCompact("threshold", snapshot, config), false);
|
||||
assert.equal(shouldCompact("manual", snapshot, config), true);
|
||||
assert.equal(shouldCompact("overflow", snapshot, config), true);
|
||||
test("absolute token threshold wins on large windows", () => {
|
||||
assert.equal(effectiveThreshold(200_000, 0.6, 1_000_000), 200_000);
|
||||
});
|
||||
|
||||
test("effective lower-bound at the configured threshold compacts", () => {
|
||||
assert.equal(shouldCompact(
|
||||
"threshold",
|
||||
{ lowerBound: 270_000, contextWindow: 300_000 },
|
||||
{ hardRatio: 0.9 },
|
||||
), true);
|
||||
test("window ratio caps the threshold on small windows", () => {
|
||||
assert.equal(effectiveThreshold(200_000, 0.6, 272_000), 163_200);
|
||||
assert.equal(effectiveThreshold(100_000, 0.35, 272_000), 95_200);
|
||||
});
|
||||
|
||||
test("a missing context window falls back to the absolute threshold", () => {
|
||||
assert.equal(effectiveThreshold(200_000, 0.6, 0), 200_000);
|
||||
assert.equal(effectiveThreshold(200_000, 0.6, -1), 200_000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user