diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7fb23be..afcaba7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ npm ci npm run check ``` -`npm run check` validates TypeScript and the offline tests. The tests use Pi's faux provider, not a real model. +`npm run check` validates TypeScript, cyclomatic complexity (at most 10 per function, `npm run lint`), and the offline tests. Split a function that is too complex; inline `eslint-disable` comments have no effect. The tests use Pi's faux provider, not a real model. CI also installs Pi `1.0.3`, the oldest supported version, and the latest published Pi packages on Node 24, and reruns these checks. It runs weekly, even without repository changes. Keep the lockfile baseline reproducible; do not replace it with an unbounded dependency tree. Host-provided peer dependencies stay `*`. diff --git a/extensions/index.ts b/extensions/index.ts index 6eb8593..6a85379 100644 --- a/extensions/index.ts +++ b/extensions/index.ts @@ -2,11 +2,14 @@ import { type Api, type Message, type Model } from "@earendil-works/pi-ai"; import { Type } from "typebox"; import { convertToLlm, + type CompactionEntryDraft, type ExtensionAPI, type ExtensionContext, findCutPoint, generateSummaryWithUsage, getLatestCompactionEntry, + type ProjectedSessionEntry, + type SessionBeforeCompactEvent, type SessionEntry, SettingsManager, } from "@earendil-works/pi-coding-agent"; @@ -31,7 +34,7 @@ import { toDrafts, } from "../src/context.ts"; import { handoffSummary } from "../src/handoff.ts"; -import { planMaintenance } from "../src/maintain.ts"; +import { type MaintenancePlan, planMaintenance } from "../src/maintain.ts"; import { requestNote } from "../src/note.ts"; import { readEntry, searchEntries } from "../src/recall.ts"; @@ -216,18 +219,128 @@ export default function contextKit(pi: ExtensionAPI): void { : undefined; }; + /** Starts a background note shortly before the threshold, so compaction need not wait for one. */ + const speculate = ( + turn: Turn, + leafId: string, + keptEntryId: string | undefined, + messages: () => Message[], + ) => { + const { ctx, config, model, limit, result } = turn; + if (!config.asyncEnabled || !keptEntryId) return; + if (result.tokens <= limit - speculationLead(limit)) return; + if (result.tokens > limit) return; + const budget = Math.floor( + limit * config.progressRatio - keptTokens(ctx, keptEntryId), + ); + if (budget <= 0) return; + const context = messages(); + const maxTokens = Math.min(model.maxTokens, budget); + background.start( + turn.current, + leafId, + (signal) => + requestNote({ + registry: ctx.modelRegistry, + model, + sessionId: ctx.sessionManager.getSessionId(), + messages: context, + reasoning: reasoning(), + signal, + maxTokens, + }), + (error) => warn(ctx, error, "A new note is written at compaction."), + ); + }; + + /** `"waiting"` means a background note is still being written for this threshold. */ + const handoffAtTurn = async (turn: Turn) => { + const { ctx, model, limit, current, result } = turn; + const leafId = ctx.sessionManager.getLeafId(); + if (!leafId) return undefined; + const messages = () => + convertToLlm(projectedMessages(applyEdits(turn.entries, result.edits))); + const recent = recentStart(ctx, compactionSettings(model).keepRecentTokens); + speculate(turn, leafId, recent, messages); + if (result.tokens <= limit || !recent || failedEpoch === current) + return undefined; + const writing = background.writing(current); + if (writing && limit < piThreshold(model)) return "waiting" as const; + return tryHandoff(turn, recent, writing ? undefined : messages); + }; + + const tryHandoff = async ( + turn: Turn, + keptEntryId: string, + messages: (() => Message[]) | undefined, + ) => { + const { ctx, current } = turn; + const signal = ctx.signal ?? new AbortController().signal; + try { + const compaction = await handoff({ + ctx, + model: turn.model, + config: turn.config, + branch: turn.branch, + ready: await background.takeReady(current, turn.branch), + keptEntryId, + limit: turn.limit, + messages: messages?.(), + signal, + cancelBackground: () => background.cancel(), + }); + if (compaction) { + background.cancel(); + return compacted(turn.result, compaction); + } + } catch (error) { + if (!signal.aborted) + warn(ctx, error, "The remaining methods in methodOrder are tried."); + } + failedEpoch = current; + return undefined; + }; + + const softAtTurn = async (turn: Turn) => { + const { ctx, model, limit, current, result } = turn; + const due = + result.tokens > limit && + limit < piThreshold(model) && + turn.config.methodOrder.includes("soft") && + softFailedEpoch !== current; + if (!due) return undefined; + try { + const compaction = await soft({ + ctx, + model, + config: turn.config, + branch: turn.branch, + entries: applyEdits(turn.entries, result.edits), + limit, + }); + if (compaction) return compacted(result, compaction); + } catch (error) { + if (!ctx.signal?.aborted) + warn(ctx, error, "Pi compacts at its own threshold.", "Pi's summary"); + } + softFailedEpoch = current; + return undefined; + }; + pi.on("turn_end", async (event, ctx) => { const config = settings(ctx); if (!config.enabled) return undefined; + const branch = ctx.sessionManager.getBranch(); + const current = epoch(branch); + background.invalidate(current, branch); const model = ctx.model; + // Without a model the threshold is infinite, so maintenance plans no edits. + if (!model) return undefined; const limit = threshold(model, config); const methods = config.methodOrder; const entries = event.context.contextEntries; - const branch = ctx.sessionManager.getBranch(); + const tokens = currentTokens(ctx, entries); const edited = editedEntryIds(branch); - const tokens = - ctx.getContextUsage()?.tokens ?? - contextTokens(projectedMessages(entries)); const plan = (shake: boolean) => planMaintenance({ entries, @@ -237,162 +350,78 @@ export default function contextKit(pi: ExtensionAPI): void { threshold: limit, }); const shakeFirst = before(methods, "shake", "handoff"); - let result = plan(shakeFirst); - const drafts = () => toDrafts(result.edits); - // Without compaction, edits that leave the context above the threshold break the cache again next turn. - const persisted = () => - result.edits.length > 0 && result.tokens <= limit - ? { entries: drafts() } - : undefined; + const turn: Turn = { + ctx, + config, + model, + limit, + entries, + branch, + current, + result: plan(shakeFirst), + }; - const leafId = ctx.sessionManager.getLeafId(); - const current = epoch(branch); - background.invalidate(current, branch); // Try ordered strategies at Pi's threshold before its native hook can compact. - const own = result.tokens > limit; - if (model && leafId && before(methods, "handoff", "soft")) { - const messages = () => - convertToLlm(projectedMessages(applyEdits(entries, result.edits))); - const keepRecentTokens = compactionSettings(model).keepRecentTokens; - const keptEntryId = own ? recentStart(ctx, keepRecentTokens) : undefined; - const speculativeKeptId = recentStart(ctx, keepRecentTokens); - const speculativeBudget = speculativeKeptId - ? Math.floor( - limit * config.progressRatio - keptTokens(ctx, speculativeKeptId), - ) - : 0; - if ( - config.asyncEnabled && - speculativeBudget > 0 && - result.tokens > limit - speculationLead(limit) && - result.tokens <= limit - ) { - const context = messages(); - const maxTokens = Math.min(model.maxTokens, speculativeBudget); - background.start( - current, - leafId, - (signal) => - requestNote({ - registry: ctx.modelRegistry, - model, - sessionId: ctx.sessionManager.getSessionId(), - messages: context, - reasoning: reasoning(), - signal, - maxTokens, - }), - (error) => warn(ctx, error, "A new note is written at compaction."), - ); - } - if (own && keptEntryId && failedEpoch !== current) { - const writing = background.writing(current); - if (writing && limit < piThreshold(model)) return persisted(); - const signal = ctx.signal ?? new AbortController().signal; - try { - const compaction = await handoff({ - ctx, - model, - config, - branch, - ready: await background.takeReady(current, branch), - keptEntryId, - limit, - messages: writing ? undefined : messages(), - signal, - cancelBackground: () => background.cancel(), - }); - if (compaction) { - background.cancel(); - return { - entries: [...drafts(), { type: "compaction", ...compaction }], - }; - } - failedEpoch = current; - } catch (error) { - failedEpoch = current; - if (!signal.aborted) - warn(ctx, error, "The remaining methods in methodOrder are tried."); - } - } - } - const shakeAfterHandoff = !shakeFirst && before(methods, "shake", "soft"); - if (result.tokens > limit && shakeAfterHandoff) result = plan(true); - const softDue = - model && - result.tokens > limit && - limit < piThreshold(model) && - methods.includes("soft") && - softFailedEpoch !== current; - if (softDue) { - try { - const compaction = await soft({ - ctx, - model, - config, - branch, - entries: applyEdits(entries, result.edits), - limit, - }); - if (compaction) - return { - entries: [...drafts(), { type: "compaction", ...compaction }], - }; - } catch (error) { - if (!ctx.signal?.aborted) - warn(ctx, error, "Pi compacts at its own threshold.", "Pi's summary"); - } - softFailedEpoch = current; - } - return persisted(); + const handed = before(methods, "handoff", "soft") + ? await handoffAtTurn(turn) + : undefined; + if (handed === "waiting") return persisted(turn.result, limit); + if (handed) return handed; + if ( + turn.result.tokens > limit && + !shakeFirst && + before(methods, "shake", "soft") + ) + turn.result = plan(true); + return (await softAtTurn(turn)) ?? persisted(turn.result, limit); }); + const handoffBeforeCompact = async ( + event: SessionBeforeCompactEvent, + ctx: ExtensionContext, + model: Model, + config: ContextKitConfig, + ) => { + const { preparation, branchEntries, customInstructions } = event; + const current = epoch(branchEntries); + background.invalidate(current, branchEntries); + const writing = !customInstructions && background.writing(current); + const compaction = await handoff({ + ctx, + model, + config, + branch: branchEntries, + // A focused /compact needs a note written for that focus. + ready: customInstructions + ? undefined + : await background.takeReady(current, branchEntries), + keptEntryId: preparation.firstKeptEntryId, + limit: Math.min(threshold(model, config), model.contextWindow), + // The live context no longer fits the model after an overflow. + messages: + event.reason === "overflow" || writing ? undefined : liveMessages(ctx), + focus: customInstructions, + signal: event.signal, + cancelBackground: () => background.cancel(), + }); + return ( + compaction && { + compaction: { ...compaction, tokensBefore: preparation.tokensBefore }, + } + ); + }; + pi.on("session_before_compact", async (event, ctx) => { const model = ctx.model; const config = settings(ctx); - if ( - !model || - !config.enabled || - !before(config.methodOrder, "handoff", "soft") - ) - return undefined; - const { preparation, branchEntries, signal } = event; - const currentEpoch = epoch(branchEntries); - if (event.reason === "threshold" && failedEpoch === currentEpoch) - return undefined; - const limit = Math.min(threshold(model, config), model.contextWindow); + if (!model || !handoffEnabled(config)) return undefined; + const failed = failedEpoch === epoch(event.branchEntries); + if (event.reason === "threshold" && failed) return undefined; try { - const current = currentEpoch; - background.invalidate(current, branchEntries); - if (event.customInstructions) background.cancel(); - const writing = !event.customInstructions && background.writing(current); - const compaction = await handoff({ - ctx, - model, - config, - branch: branchEntries, - // A focused /compact needs a note written for that focus. - ready: event.customInstructions - ? undefined - : await background.takeReady(current, branchEntries), - keptEntryId: preparation.firstKeptEntryId, - limit, - // The live context no longer fits the model after an overflow. - messages: - event.reason === "overflow" || writing - ? undefined - : liveMessages(ctx), - focus: event.customInstructions, - signal, - cancelBackground: () => background.cancel(), - }); - return ( - compaction && { - compaction: { ...compaction, tokensBefore: preparation.tokensBefore }, - } - ); + return await handoffBeforeCompact(event, ctx, model, config); } catch (error) { - if (!signal.aborted) warn(ctx, error, "Pi's summary is used instead."); + if (!event.signal.aborted) + warn(ctx, error, "Pi's summary is used instead."); return undefined; } }); @@ -449,6 +478,50 @@ export default function contextKit(pi: ExtensionAPI): void { }); } +/** One turn boundary's inputs and its current maintenance plan. */ +interface Turn { + ctx: ExtensionContext; + config: ContextKitConfig; + model: Model; + limit: number; + entries: ProjectedSessionEntry[]; + branch: SessionEntry[]; + current: string | null; + result: MaintenancePlan; +} + +// Without compaction, edits that leave the context above the threshold break the cache again next turn. +function persisted(result: MaintenancePlan, limit: number) { + return result.edits.length > 0 && result.tokens <= limit + ? { entries: toDrafts(result.edits) } + : undefined; +} + +function compacted( + result: MaintenancePlan, + compaction: Omit, +) { + return { + entries: [ + ...toDrafts(result.edits), + { type: "compaction" as const, ...compaction }, + ], + }; +} + +function currentTokens( + ctx: ExtensionContext, + entries: readonly ProjectedSessionEntry[], +): number { + return ( + ctx.getContextUsage()?.tokens ?? contextTokens(projectedMessages(entries)) + ); +} + +function handoffEnabled(config: ContextKitConfig): boolean { + return config.enabled && before(config.methodOrder, "handoff", "soft"); +} + function liveMessages(ctx: ExtensionContext): Message[] { return convertToLlm(ctx.sessionManager.buildSessionProjection().messages); } diff --git a/package-lock.json b/package-lock.json index 3cb847b..111e622 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@earendil-works/pi-ai": "^1.5.2", "@earendil-works/pi-coding-agent": "^1.5.2", "@types/node": "^26.6.4", + "eslint": "^10.14.0", "typebox": "^1.3.27", "typescript": "^7.0.2", "vitest": "^5.0.3" @@ -415,6 +416,30 @@ "node": ">=6.9.0" } }, + "node_modules/@cacheable/memory": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@cacheable/memory/-/memory-2.2.0.tgz", + "integrity": "sha512-CTLKqLItRCEixEAewD3/j9DB3/o96gpTPD4eJ1v+DGOlxZRZncRQkGYqqnAGCscYd6RNeXfGeiuCphsPtqyIfQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cacheable/utils": "^2.5.0", + "@keyv/bigmap": "^1.3.1", + "hookified": "^1.15.1", + "keyv": "^5.6.0" + } + }, + "node_modules/@cacheable/utils": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@cacheable/utils/-/utils-2.5.0.tgz", + "integrity": "sha512-buipgOVDkkPXNR5+xBpDw7Zk2n1EvU7qBJCNUcL7rhQ//kfpOXPAvQ511Os0vpLYJ1pZnvudNytkQt2hst3wqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "hashery": "^1.5.1", + "keyv": "^5.6.0" + } + }, "node_modules/@earendil-works/pi-agent-core": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/@earendil-works/pi-agent-core/-/pi-agent-core-1.5.2.tgz", @@ -559,6 +584,130 @@ "node": ">=22.19.0" } }, + "node_modules/@eslint-community/eslint-utils": { + "version": "4.10.1", + "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.10.1.tgz", + "integrity": "sha512-cuadcxVFE8sDK6iWJbs8Sn0av2Nrh2QSGQhVlBW9AaAHqHwjWsZHT8LJ4hFGPh7ASBV2deFdM7H/DPjulmh8rg==", + "dev": true, + "license": "MIT", + "dependencies": { + "eslint-visitor-keys": "^3.4.3" + }, + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + }, + "funding": { + "url": "https://opencollective.com/eslint" + }, + "peerDependencies": { + "eslint": "^6.0.0 || ^7.0.0 || >=8.0.0" + } + }, + "node_modules/@eslint-community/eslint-utils/node_modules/eslint-visitor-keys": { + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz", + "integrity": "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + }, + "funding": { + "url": "https://opencollective.com/eslint" + } + }, + "node_modules/@eslint-community/regexpp": { + "version": "4.12.3", + "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.12.3.tgz", + "integrity": "sha512-R76UKbvsqI68DJJVTVyqFKqU98CN4+B+acxNfEA2IFarSzP0SjtKf/3+fFiXzTyv5Fj3PxLkGHe2wnVWWMxobQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.0.0 || ^14.0.0 || >=16.0.0" + } + }, + "node_modules/@eslint/config-array": { + "version": "0.23.5", + "resolved": "https://registry.npmjs.org/@eslint/config-array/-/config-array-0.23.5.tgz", + "integrity": "sha512-Y3kKLvC1dvTOT+oGlqNQ1XLqK6D1HU2YXPc52NmAlJZbMMWDzGYXMiPRJ8TYD39muD/OTjlZmNJ4ib7dvSrMBA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@eslint/object-schema": "^3.0.5", + "debug": "^4.3.1", + "minimatch": "^10.2.4" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + } + }, + "node_modules/@eslint/config-array/node_modules/minimatch": { + "version": "11.0.8", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-11.0.8.tgz", + "integrity": "sha512-nFQuYSTVrGvysVBps5Ih+0BdnYFN/Cj3C816r6+p7yK+FHrVLGE4O4N9AFwykrlNlvM2HYt+Rad1Uiro6h88Ig==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "@isaacs/nonrecursive-glob-match": "^1.1.0", + "brace-expansion": "^5.0.12" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/@eslint/config-helpers": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/@eslint/config-helpers/-/config-helpers-0.7.0.tgz", + "integrity": "sha512-DObd/KKUsU+FaFv4PLxSRenpXfQWmPXXP3pPZ6/K1PCrMu2vQpMDMuQe/BqYeoLcz8ro0bVDF1RxOJgfVEdhUw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@eslint/core": "^1.2.1" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + } + }, + "node_modules/@eslint/core": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@eslint/core/-/core-1.2.1.tgz", + "integrity": "sha512-MwcE1P+AZ4C6DWlpin/OmOA54mmIZ/+xZuJiQd4SyB29oAJjN30UW9wkKNptW2ctp4cEsvhlLY/CsQ1uoHDloQ==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@types/json-schema": "^7.0.15" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + } + }, + "node_modules/@eslint/object-schema": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/@eslint/object-schema/-/object-schema-3.0.5.tgz", + "integrity": "sha512-vqTaUEgxzm+YDSdElad6PiRoX4t8VGDjCtt05zn4nU810UIx/uNEV7/lZJ6KwFThKZOzOxzXy48da+No7HZaMw==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + } + }, + "node_modules/@eslint/plugin-kit": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/@eslint/plugin-kit/-/plugin-kit-0.7.3.tgz", + "integrity": "sha512-IkO+/KEUvwbVpiURZg+P7zF74z5Jxe0UgJxVni+RtoHQ6IZieXaO02kmadomap/q+l6bc/jdPGGqTjhuZnuz1Q==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@eslint/core": "^1.2.1", + "levn": "^0.4.1" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + } + }, "node_modules/@google/genai": { "version": "2.21.0", "resolved": "https://registry.npmjs.org/@google/genai/-/genai-2.21.0.tgz", @@ -584,6 +733,72 @@ } } }, + "node_modules/@humanfs/core": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz", + "integrity": "sha512-UhXNm+CFMWcbChXywFwkmhqjs3PRCmcSa/hfBgLIb7oQ5HNb1wS0icWsGtSAUNgefHeI+eBrA8I1fxmbHsGdvA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@humanfs/types": "^0.15.0" + }, + "engines": { + "node": ">=18.18.0" + } + }, + "node_modules/@humanfs/node": { + "version": "0.16.8", + "resolved": "https://registry.npmjs.org/@humanfs/node/-/node-0.16.8.tgz", + "integrity": "sha512-gE1eQNZ3R++kTzFUpdGlpmy8kDZD/MLyHqDwqjkVQI0JMdI1D51sy1H958PNXYkM2rAac7e5/CnIKZrHtPh3BQ==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@humanfs/core": "^0.19.2", + "@humanfs/types": "^0.15.0", + "@humanwhocodes/retry": "^0.4.0" + }, + "engines": { + "node": ">=18.18.0" + } + }, + "node_modules/@humanfs/types": { + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/@humanfs/types/-/types-0.15.0.tgz", + "integrity": "sha512-ZZ1w0aoQkwuUuC7Yf+7sdeaNfqQiiLcSRbfI08oAxqLtpXQr9AIVX7Ay7HLDuiLYAaFPu8oBYNq/QIi9URHJ3Q==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18.18.0" + } + }, + "node_modules/@humanwhocodes/module-importer": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@humanwhocodes/module-importer/-/module-importer-1.0.1.tgz", + "integrity": "sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.22" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/nzakas" + } + }, + "node_modules/@humanwhocodes/retry": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.4.3.tgz", + "integrity": "sha512-bV0Tgo9K4hfPCek+aMAn81RppFKv2ySDQeMoSZuvTASywNTnVJCArCZE2FWqpvIatKu7VMRLWlR1EazvVhDyhQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18.18" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/nzakas" + } + }, "node_modules/@isaacs/nonrecursive-glob-match": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@isaacs/nonrecursive-glob-match/-/nonrecursive-glob-match-1.1.0.tgz", @@ -619,6 +834,30 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@keyv/bigmap": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/@keyv/bigmap/-/bigmap-1.3.1.tgz", + "integrity": "sha512-WbzE9sdmQtKy8vrNPa9BRnwZh5UF4s1KTmSK0KUVLo3eff5BlQNNWDnFOouNpKfPKDnms9xynJjsMYjMaT/aFQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "hashery": "^1.4.0", + "hookified": "^1.15.0" + }, + "engines": { + "node": ">= 18" + }, + "peerDependencies": { + "keyv": "^5.6.0" + } + }, + "node_modules/@keyv/serialize": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@keyv/serialize/-/serialize-1.1.1.tgz", + "integrity": "sha512-dXn3FZhPv0US+7dtJsIi2R+c7qWYiReoEh5zUntWCf4oSpMNib8FDhSoed6m3QyZdx5hK7iLFkYk3rNxwt8vTA==", + "dev": true, + "license": "MIT" + }, "node_modules/@oxc-project/types": { "version": "0.156.0", "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.156.0.tgz", @@ -1111,6 +1350,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/esrecurse": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/@types/esrecurse/-/esrecurse-4.3.1.tgz", + "integrity": "sha512-xJBAbDifo5hpffDBuHl0Y8ywswbiAp/Wi7Y/GtAgSlZyIABppyurxVueOPE8LUQOxdlgi6Zqce7uoEpqNTeiUw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/estree": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.10.tgz", @@ -1118,6 +1364,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/json-schema": { + "version": "7.0.15", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", + "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "26.6.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.6.tgz", @@ -1513,6 +1766,29 @@ "url": "https://opencollective.com/vitest" } }, + "node_modules/acorn": { + "version": "8.19.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.19.0.tgz", + "integrity": "sha512-oJlA3XiRm7Cyk6qFD2Jn8ak9B7jSy0qy00ADO3+8dpT0LSjFihQYv4C02LFCSYJV3Q37xYwwRy5m+IpIiUzqWw==", + "dev": true, + "license": "MIT", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-jsx": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", + "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" + } + }, "node_modules/agent-base": { "version": "9.0.0", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-9.0.0.tgz", @@ -1523,6 +1799,23 @@ "node": ">= 20" } }, + "node_modules/ajv": { + "version": "6.15.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.15.0.tgz", + "integrity": "sha512-fgFx7Hfoq60ytK2c7DhnF8jIvzYgOMxfugjLOSMHjLIPgenqa7S7oaagATUq99mV6IYvN2tRmC0wnTYX6iPbMw==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, "node_modules/assertion-error": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", @@ -1601,6 +1894,20 @@ "dev": true, "license": "BSD-3-Clause" }, + "node_modules/cacheable": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/cacheable/-/cacheable-2.5.0.tgz", + "integrity": "sha512-60cyAOytib/OzBw1JNSoSV/boK1AtHryDIjvVBk7XbN4ugfkM3+Sry7fEjNgPMGgOjuaZPAp8ruZ0Cxafwyq9g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cacheable/memory": "^2.2.0", + "@cacheable/utils": "^2.5.0", + "hookified": "^1.15.0", + "keyv": "^5.6.0", + "qified": "^0.10.1" + } + }, "node_modules/chai": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/chai/-/chai-6.3.0.tgz", @@ -1667,6 +1974,13 @@ } } }, + "node_modules/deep-is": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", + "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", + "dev": true, + "license": "MIT" + }, "node_modules/detect-libc": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", @@ -1705,6 +2019,191 @@ "dev": true, "license": "MIT" }, + "node_modules/escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/eslint": { + "version": "10.14.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-10.14.0.tgz", + "integrity": "sha512-T0pNsxkb9IKmtQjhpK3d7nWOpFv+YHuMbTJnSVMoQblF0onvXfErrtp4V1jHtsQPan7dKaGVD0q1WCXnzMF31A==", + "dev": true, + "license": "MIT", + "workspaces": [ + "packages/*" + ], + "dependencies": { + "@eslint-community/eslint-utils": "^4.8.0", + "@eslint-community/regexpp": "^4.12.2", + "@eslint/config-array": "^0.23.5", + "@eslint/config-helpers": "^0.7.0", + "@eslint/core": "^1.2.1", + "@eslint/plugin-kit": "^0.7.3", + "@humanfs/node": "^0.16.8", + "@humanwhocodes/module-importer": "^1.0.1", + "@humanwhocodes/retry": "^0.4.2", + "@types/estree": "^1.0.6", + "ajv": "^6.14.0", + "cross-spawn": "^7.0.6", + "debug": "^4.3.2", + "escape-string-regexp": "^4.0.0", + "eslint-scope": "^9.1.2", + "eslint-visitor-keys": "^5.0.1", + "espree": "^11.2.0", + "esquery": "^1.7.0", + "esutils": "^2.0.2", + "fast-deep-equal": "^3.1.3", + "file-entry-cache": "11.1.5 || >11.1.6 <12", + "find-up": "^5.0.0", + "glob-parent": "^6.0.2", + "ignore": "^5.2.0", + "imurmurhash": "^0.1.4", + "is-glob": "^4.0.0", + "json-stable-stringify-without-jsonify": "^1.0.1", + "minimatch": "^10.2.6", + "natural-compare": "^1.4.0", + "optionator": "^0.9.3" + }, + "bin": { + "eslint": "bin/eslint.js" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + }, + "funding": { + "url": "https://eslint.org/donate" + }, + "peerDependencies": { + "jiti": "*" + }, + "peerDependenciesMeta": { + "jiti": { + "optional": true + } + } + }, + "node_modules/eslint-scope": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-9.1.2.tgz", + "integrity": "sha512-xS90H51cKw0jltxmvmHy2Iai1LIqrfbw57b79w/J7MfvDfkIkFZ+kj6zC3BjtUwh150HsSSdxXZcsuv72miDFQ==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "@types/esrecurse": "^4.3.1", + "@types/estree": "^1.0.8", + "esrecurse": "^4.3.0", + "estraverse": "^5.2.0" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + }, + "funding": { + "url": "https://opencollective.com/eslint" + } + }, + "node_modules/eslint-visitor-keys": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-5.0.1.tgz", + "integrity": "sha512-tD40eHxA35h0PEIZNeIjkHoDR4YjjJp34biM0mDvplBe//mB+IHCqHDGV7pxF+7MklTvighcCPPZC7ynWyjdTA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + }, + "funding": { + "url": "https://opencollective.com/eslint" + } + }, + "node_modules/eslint/node_modules/ignore": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, + "node_modules/eslint/node_modules/minimatch": { + "version": "11.0.8", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-11.0.8.tgz", + "integrity": "sha512-nFQuYSTVrGvysVBps5Ih+0BdnYFN/Cj3C816r6+p7yK+FHrVLGE4O4N9AFwykrlNlvM2HYt+Rad1Uiro6h88Ig==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "@isaacs/nonrecursive-glob-match": "^1.1.0", + "brace-expansion": "^5.0.12" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/espree": { + "version": "11.2.0", + "resolved": "https://registry.npmjs.org/espree/-/espree-11.2.0.tgz", + "integrity": "sha512-7p3DrVEIopW1B1avAGLuCSh1jubc01H2JHc8B4qqGblmg5gI9yumBgACjWo4JlIc04ufug4xJ3SQI8HkS/Rgzw==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "acorn": "^8.16.0", + "acorn-jsx": "^5.3.2", + "eslint-visitor-keys": "^5.0.1" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=24" + }, + "funding": { + "url": "https://opencollective.com/eslint" + } + }, + "node_modules/esquery": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.7.0.tgz", + "integrity": "sha512-Ap6G0WQwcU/LHsvLwON1fAQX9Zp0A2Y6Y/cJBl9r/JbW90Zyg4/zbG6zzKa2OTALELarYHmKu0GhpM5EO+7T0g==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "estraverse": "^5.1.0" + }, + "engines": { + "node": ">=0.10" + } + }, + "node_modules/esrecurse": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.3.0.tgz", + "integrity": "sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "estraverse": "^5.2.0" + }, + "engines": { + "node": ">=4.0" + } + }, + "node_modules/estraverse": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.3.0.tgz", + "integrity": "sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=4.0" + } + }, "node_modules/estree-walker": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", @@ -1715,6 +2214,16 @@ "@types/estree": "^1.0.0" } }, + "node_modules/esutils": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/expect-type": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", @@ -1732,6 +2241,27 @@ "dev": true, "license": "MIT" }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/fast-json-stable-stringify": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", + "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", + "dev": true, + "license": "MIT" + }, + "node_modules/fast-levenshtein": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", + "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", + "dev": true, + "license": "MIT" + }, "node_modules/fast-sha256": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/fast-sha256/-/fast-sha256-1.3.0.tgz", @@ -1781,6 +2311,52 @@ "node": "^12.20 || >= 14.13" } }, + "node_modules/file-entry-cache": { + "version": "11.1.5", + "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-11.1.5.tgz", + "integrity": "sha512-+PFTHITI08JIGhnNpGNI8T8inUpgZfk3GNEqfT9R2zZV2iFXg3CvqzSl/uEhs7TSGujYRELEANyDvS8Fj7+S7Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "flat-cache": "^6.1.23" + } + }, + "node_modules/find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", + "dev": true, + "license": "MIT", + "dependencies": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/flat-cache": { + "version": "6.1.23", + "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-6.1.23.tgz", + "integrity": "sha512-f++BY9pTk+983xK1FLzlLpmM0i0z+jHmx3QESGkURMXujQZz1k5wzwX6hjnQ8goaD0B+sYnDK1yZ6MTyZfUaqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "cacheable": "^2.5.0", + "flatted": "^3.4.2", + "hookified": "^1.15.0" + } + }, + "node_modules/flatted": { + "version": "3.4.4", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.4.tgz", + "integrity": "sha512-5+ybhBZANEJxaH3X5evAFatUxLfEHSr7n6kYJ+1Qd0mUqr4eu9gIf6GDbWHf8RJijHrjjO8G+la14SlL2SeS1Q==", + "dev": true, + "license": "ISC" + }, "node_modules/formdata-polyfill": { "version": "4.0.10", "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", @@ -1877,6 +2453,19 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/glob-parent": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", + "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", + "dev": true, + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.3" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/google-auth-library": { "version": "10.9.1", "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-10.9.1.tgz", @@ -1912,6 +2501,19 @@ "dev": true, "license": "ISC" }, + "node_modules/hashery": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/hashery/-/hashery-1.5.1.tgz", + "integrity": "sha512-iZyKG96/JwPz1N55vj2Ie2vXbhu440zfUfJvSwEqEbeLluk7NnapfGqa7LH0mOsnDxTF85Mx8/dyR6HfqcbmbQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "hookified": "^1.15.0" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/highlight.js": { "version": "10.7.3", "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-10.7.3.tgz", @@ -1922,6 +2524,13 @@ "node": "*" } }, + "node_modules/hookified": { + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/hookified/-/hookified-1.15.1.tgz", + "integrity": "sha512-MvG/clsADq1GPM2KGo2nyfaWVyn9naPiXrqIe4jYjXNZQt238kWyOGrsyc/DmRAQ+Re6yeo6yX/yoNCG5KAEVg==", + "dev": true, + "license": "MIT" + }, "node_modules/hosted-git-info": { "version": "9.0.3", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-9.0.3.tgz", @@ -1975,6 +2584,39 @@ "node": ">= 4" } }, + "node_modules/imurmurhash": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", + "integrity": "sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.8.19" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -2016,6 +2658,20 @@ "node": ">=16" } }, + "node_modules/json-schema-traverse": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", + "dev": true, + "license": "MIT" + }, + "node_modules/json-stable-stringify-without-jsonify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", + "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", + "dev": true, + "license": "MIT" + }, "node_modules/jwa": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.1.tgz", @@ -2039,6 +2695,30 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/keyv": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-5.6.0.tgz", + "integrity": "sha512-CYDD3SOtsHtyXeEORYRx2qBtpDJFjRTGXUtmNEMGyzYOKj1TE3tycdlho7kA1Ufx9OYWZzg52QFBGALTirzDSw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@keyv/serialize": "^1.1.1" + } + }, + "node_modules/levn": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", + "integrity": "sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "prelude-ls": "^1.2.1", + "type-check": "~0.4.0" + }, + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/lightningcss": { "version": "1.33.0", "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", @@ -2324,6 +3004,22 @@ "url": "https://opencollective.com/parcel" } }, + "node_modules/locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", + "dev": true, + "license": "MIT", + "dependencies": { + "p-locate": "^5.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/long": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", @@ -2431,6 +3127,13 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, + "node_modules/natural-compare": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", + "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", + "dev": true, + "license": "MIT" + }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -2523,6 +3226,56 @@ } } }, + "node_modules/optionator": { + "version": "0.9.4", + "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz", + "integrity": "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==", + "dev": true, + "license": "MIT", + "dependencies": { + "deep-is": "^0.1.3", + "fast-levenshtein": "^2.0.6", + "levn": "^0.4.1", + "prelude-ls": "^1.2.1", + "type-check": "^0.4.0", + "word-wrap": "^1.2.5" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "yocto-queue": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", + "dev": true, + "license": "MIT", + "dependencies": { + "p-limit": "^3.0.2" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/p-retry": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-4.6.2.tgz", @@ -2544,6 +3297,16 @@ "dev": true, "license": "MIT" }, + "node_modules/path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", @@ -2605,6 +3368,16 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/prelude-ls": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", + "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/proper-lockfile": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz", @@ -2669,6 +3442,36 @@ } } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/qified": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/qified/-/qified-0.10.1.tgz", + "integrity": "sha512-+Owyggi9IxT1ePKGafcI87ubSmxol6smwJ+RAHDQlx9+9cPwFWDiKFFCPuWhr9ignlGpZ9vDQLw67N4dcTVFEA==", + "dev": true, + "license": "MIT", + "dependencies": { + "hookified": "^2.1.1" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/qified/node_modules/hookified": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/hookified/-/hookified-2.2.0.tgz", + "integrity": "sha512-p/LgFzRN5FeoD3DLS6bkUapeye6E4SI6yJs6KetENd18S+FBthqYq2amJUWpt5z0EQwwHemidjY5OqJGEKm5uA==", + "dev": true, + "license": "MIT" + }, "node_modules/quickjs-wasi": { "version": "3.6.2", "resolved": "https://registry.npmjs.org/quickjs-wasi/-/quickjs-wasi-3.6.2.tgz", @@ -2865,6 +3668,19 @@ "dev": true, "license": "0BSD" }, + "node_modules/type-check": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", + "integrity": "sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==", + "dev": true, + "license": "MIT", + "dependencies": { + "prelude-ls": "^1.2.1" + }, + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/typebox": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/typebox/-/typebox-1.4.4.tgz", @@ -2924,6 +3740,16 @@ "dev": true, "license": "MIT" }, + "node_modules/uri-js": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", + "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "punycode": "^2.1.0" + } + }, "node_modules/vite": { "version": "8.3.5", "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.5.tgz", @@ -3125,6 +3951,16 @@ "node": ">=20.11" } }, + "node_modules/word-wrap": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/ws": { "version": "8.22.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz", @@ -3162,6 +3998,19 @@ "funding": { "url": "https://github.com/sponsors/eemeli" } + }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } } } } diff --git a/package.json b/package.json index 6b8909d..ac52feb 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ ] }, "scripts": { - "check": "tsc --noEmit && vitest --run", + "check": "tsc --noEmit && npm run lint && vitest --run", + "lint": "node scripts/lint.ts", "test": "vitest --run", "prepublishOnly": "test -n \"$GITHUB_ACTIONS\" || test \"$(npm whoami)\" = hugolopes45 || { echo 'Publish only as the npm user hugolopes45.' >&2; exit 1; }; npm run check" }, @@ -44,8 +45,17 @@ "@earendil-works/pi-ai": "^1.5.2", "@earendil-works/pi-coding-agent": "^1.5.2", "@types/node": "^26.6.4", + "eslint": "^10.14.0", "typebox": "^1.3.27", "typescript": "^7.0.2", "vitest": "^5.0.3" + }, + "overrides": { + "eslint": { + "minimatch": "^11.0.8" + }, + "@eslint/config-array": { + "minimatch": "^11.0.8" + } } } diff --git a/scripts/lint.ts b/scripts/lint.ts new file mode 100644 index 0000000..00b22bf --- /dev/null +++ b/scripts/lint.ts @@ -0,0 +1,24 @@ +// ESLint cannot parse TypeScript, and typescript-eslint does not support +// TypeScript 7. Node strips the types in place, so reported lines stay exact. +import { globSync, readFileSync } from "node:fs"; +import { stripTypeScriptTypes } from "node:module"; +import { Linter } from "eslint"; + +const linter = new Linter(); +const config: Linter.Config = { + files: ["**/*.ts"], + linterOptions: { noInlineConfig: true }, + rules: { complexity: ["error", 10] }, +}; + +let problems = 0; +for (const file of globSync("{extensions,scripts,src,test}/**/*.ts")) { + const code = stripTypeScriptTypes(readFileSync(file, "utf8")); + for (const message of linter.verify(code, config, file)) { + problems++; + console.error( + `${file}:${message.line}:${message.column} ${message.message}`, + ); + } +} +if (problems) process.exit(1); diff --git a/src/prune.ts b/src/prune.ts index 4ea5a5a..c80ff80 100644 --- a/src/prune.ts +++ b/src/prune.ts @@ -1,11 +1,6 @@ import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent"; -import { - type Edit, - editableMessage, - messageTokens, - withContent, -} from "./context.ts"; -import { toolCallsById } from "./tool-calls.ts"; +import { type Edit, messageTokens, withContent } from "./context.ts"; +import { newestToolResults, type ToolResult } from "./tool-calls.ts"; export interface PruneOptions { /** Newest tool-output tokens that are never pruned. */ @@ -22,41 +17,44 @@ export function pruneToolOutputs( edited: ReadonlySet, options: PruneOptions, ): Edit[] { - const calls = toolCallsById(entries); const edits: Edit[] = []; let seen = 0; let savings = 0; - for (let i = entries.length - 1; i >= 0; i--) { - const entry = entries[i]; - const message = entry && editableMessage(entry); - if (!entry || message?.role !== "toolResult") continue; - const tokens = messageTokens(message); + for (const result of newestToolResults(entries)) { + const tokens = messageTokens(result.message); const protectedByWindow = seen < options.protectTokens; seen += tokens; - const id = entry.sourceEntry.id; if ( protectedByWindow || tokens < options.minTokens || - edited.has(id) || - isSkillFile(calls.get(message.toolCallId)?.arguments) + edited.has(result.id) || + isSkillFile(result.args) ) { continue; } - const content = [ - { - type: "text" as const, - text: `[Output pruned: about ${tokens} tokens. Call recall with entryId "${id}" to read it.]`, - }, - ]; - const saved = tokens - messageTokens(withContent(message, content)); - if (saved <= 0) continue; - savings += saved; - edits.push({ targetId: id, content }); + const edit = pruneEdit(result, tokens); + if (!edit) continue; + savings += edit.saved; + edits.push(edit.edit); } return savings >= options.minSavings ? edits.reverse() : []; } +function pruneEdit( + { id, message }: ToolResult, + tokens: number, +): { edit: Edit; saved: number } | undefined { + const content = [ + { + type: "text" as const, + text: `[Output pruned: about ${tokens} tokens. Call recall with entryId "${id}" to read it.]`, + }, + ]; + const saved = tokens - messageTokens(withContent(message, content)); + return saved > 0 ? { edit: { targetId: id, content }, saved } : undefined; +} + function isSkillFile(args: Record | undefined): boolean { const path = args?.path; return typeof path === "string" && /(^|[\\/])SKILL\.md$/.test(path); diff --git a/src/shake.ts b/src/shake.ts index bca9114..c577a42 100644 --- a/src/shake.ts +++ b/src/shake.ts @@ -44,22 +44,30 @@ export function shake( for (const message of entry.messages) { if (message.role !== "system") seen += messageTokens(message); } - const message = editableMessage(entry); - if (isProtected || !message || message.role === "user") continue; - - const content = shakeContent(message, entry.sourceEntry.id, options); - if (content === undefined) continue; - const saved = - messageTokens(message) - messageTokens(withContent(message, content)); - if (saved <= 0) continue; - savings += saved; - edits.push({ targetId: entry.sourceEntry.id, content }); + const edit = isProtected ? undefined : shakeEdit(entry, options); + if (!edit) continue; + savings += edit.saved; + edits.push(edit.edit); } return savings >= options.minSavings && edits.length > 0 ? edits.reverse() : []; } +function shakeEdit( + entry: ProjectedSessionEntry, + options: ShakeOptions, +): { edit: Edit; saved: number } | undefined { + const message = editableMessage(entry); + if (!message || message.role === "user") return undefined; + const targetId = entry.sourceEntry.id; + const content = shakeContent(message, targetId, options); + if (content === undefined) return undefined; + const saved = + messageTokens(message) - messageTokens(withContent(message, content)); + return saved > 0 ? { edit: { targetId, content }, saved } : undefined; +} + function shakeContent( message: EditableMessage, id: string, diff --git a/src/stale.ts b/src/stale.ts index 3a5807f..0b68892 100644 --- a/src/stale.ts +++ b/src/stale.ts @@ -1,12 +1,7 @@ import type { ToolResultMessage } from "@earendil-works/pi-ai"; import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent"; -import { - type Edit, - editableMessage, - messageTokens, - withContent, -} from "./context.ts"; -import { toolCallsById } from "./tool-calls.ts"; +import { type Edit, messageTokens, withContent } from "./context.ts"; +import { newestToolResults, type ToolResult } from "./tool-calls.ts"; export const SUPERSEDED_NOTICE = "[Superseded by a newer read of this file]"; @@ -31,49 +26,57 @@ export function supersededReads( entries: readonly ProjectedSessionEntry[], edited: ReadonlySet, ): Edit[] { - const calls = toolCallsById(entries); const newer = new Map(); const edits: Edit[] = []; - for (let i = entries.length - 1; i >= 0; i--) { - const entry = entries[i]; - const message = entry && editableMessage(entry); - if (!entry || message?.role !== "toolResult" || message.toolName !== "read") - continue; - const key = readKey(calls.get(message.toolCallId)?.arguments); + for (const result of newestToolResults(entries)) { + const key = result.message.toolName === "read" && readKey(result.args); if (!key) continue; - const seen = newer.get(key.path) ?? { anyRead: false, fullSuccess: false, successRanges: new Set(), }; - const readCoversOutput = - !message.isError && !isTruncatedRead(message.content); - const superseded = message.isError - ? seen.anyRead - : seen.fullSuccess || - (key.range !== undefined && seen.successRanges.has(key.range)); - - if (superseded && !edited.has(entry.sourceEntry.id)) { - const content = [{ type: "text" as const, text: SUPERSEDED_NOTICE }]; - if ( - messageTokens(withContent(message, content)) < messageTokens(message) - ) { - edits.push({ targetId: entry.sourceEntry.id, content }); - } - } - - seen.anyRead = true; - if (readCoversOutput) { - if (key.range === undefined) seen.fullSuccess = true; - else seen.successRanges.add(key.range); + if (!edited.has(result.id) && isSuperseded(result, key, seen)) { + const edit = supersededEdit(result); + if (edit) edits.push(edit); } + recordRead(seen, result, key); newer.set(key.path, seen); } return edits.reverse(); } +function isSuperseded( + { message }: ToolResult, + key: ReadKey, + seen: NewerReads, +): boolean { + if (message.isError) return seen.anyRead; + return ( + seen.fullSuccess || + (key.range !== undefined && seen.successRanges.has(key.range)) + ); +} + +function supersededEdit({ id, message }: ToolResult): Edit | undefined { + const content = [{ type: "text" as const, text: SUPERSEDED_NOTICE }]; + return messageTokens(withContent(message, content)) < messageTokens(message) + ? { targetId: id, content } + : undefined; +} + +function recordRead( + seen: NewerReads, + { message }: ToolResult, + key: ReadKey, +): void { + seen.anyRead = true; + if (message.isError || isTruncatedRead(message.content)) return; + if (key.range === undefined) seen.fullSuccess = true; + else seen.successRanges.add(key.range); +} + function isTruncatedRead(content: ToolResultMessage["content"]): boolean { return content.some( (block) => diff --git a/src/tool-calls.ts b/src/tool-calls.ts index dcc1b4c..a272f4a 100644 --- a/src/tool-calls.ts +++ b/src/tool-calls.ts @@ -1,8 +1,15 @@ import type { ToolCall } from "@earendil-works/pi-ai"; import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent"; +import { type EditableMessage, editableMessage } from "./context.ts"; + +export interface ToolResult { + id: string; + message: Extract; + args: Record | undefined; +} /** Tool calls in the projected context, by tool call ID. */ -export function toolCallsById( +function toolCallsById( entries: readonly ProjectedSessionEntry[], ): Map { const calls = new Map(); @@ -16,3 +23,17 @@ export function toolCallsById( } return calls; } + +/** Editable tool results, newest first, with the arguments of their tool call. */ +export function* newestToolResults( + entries: readonly ProjectedSessionEntry[], +): Generator { + const calls = toolCallsById(entries); + for (let i = entries.length - 1; i >= 0; i--) { + const entry = entries[i]; + const message = entry && editableMessage(entry); + if (!entry || message?.role !== "toolResult") continue; + const args = calls.get(message.toolCallId)?.arguments; + yield { id: entry.sourceEntry.id, message, args }; + } +} diff --git a/test/extension.test.ts b/test/extension.test.ts index 9265a57..d24db88 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -511,6 +511,173 @@ describe("pi-context-kit inside Pi", () => { expect(JSON.stringify(session.messages.at(-1))).toContain("done"); }); + it("does not retry a failed note when Pi compacts at its own threshold", async () => { + const main = [ + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + fauxAssistantMessage("done"), + ]; + let notes = 0; + const reply: FauxResponseStep = (context) => { + const last = JSON.stringify(context.messages.at(-1)); + if (last.includes("handoff note")) { + notes++; + return fauxAssistantMessage("", { + stopReason: "error", + errorMessage: "note failed", + }); + } + const next = main.shift(); + if (!next) throw new Error("unexpected request"); + return next; + }; + const { session } = await start(Array(8).fill(reply), 15_000, true, { + asyncEnabled: false, + supersedeReads: false, + prune: { enabled: false }, + }); + await session.prompt(`Read b.ts. ${"history ".repeat(4_500)}`); + const compactions = session.sessionManager + .getBranch() + .filter((entry) => entry.type === "compaction"); + expect(notes).toBe(1); + expect(compactions).toHaveLength(1); + expect(compactions[0]?.fromHook).toBe(false); + }); + + it("compacts in the turn that passes an early threshold without a note in progress", async () => { + const main = [ + ...Array.from({ length: 3 }, () => + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + ), + fauxAssistantMessage("done"), + ]; + let notes = 0; + const requests: string[] = []; + const reply: FauxResponseStep = (context) => { + if (JSON.stringify(context.messages.at(-1)).includes("handoff note")) { + notes++; + return fauxAssistantMessage("## Goal\nContinue the inspection"); + } + requests.push(JSON.stringify(context.messages)); + const next = main.shift(); + if (!next) throw new Error("unexpected request"); + return next; + }; + const { session } = await start(Array(8).fill(reply), 200_000, true, { + thresholdTokens: 13_000, + asyncEnabled: true, + supersedeReads: false, + prune: { enabled: false }, + }); + await session.prompt("Read b.ts three times"); + expect(notes).toBe(1); + expect( + session.sessionManager + .getBranch() + .filter((entry) => entry.type === "compaction"), + ).toHaveLength(1); + expect(requests.at(-1)).toContain("Continue the inspection"); + }); + + it("cancels an unfinished note when /compact asks for a focus", async () => { + const main = [ + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + fauxAssistantMessage(fauxToolCall("read", { path: "a.ts" })), + fauxAssistantMessage("done"), + ]; + let notes = 0; + let noteSignal: AbortSignal | undefined; + let abortedBeforeFocus: boolean | undefined; + const reply: FauxResponseStep = (context, options) => { + if (JSON.stringify(context.messages.at(-1)).includes("handoff note")) { + notes++; + if (notes > 1) { + abortedBeforeFocus = noteSignal?.aborted; + return fauxAssistantMessage("## Goal\nFocused note"); + } + const signal = options?.signal; + if (!signal) throw new Error("missing note cancellation signal"); + noteSignal = signal; + return new Promise((resolve) => { + signal.addEventListener( + "abort", + () => resolve(fauxAssistantMessage("", { stopReason: "aborted" })), + { once: true }, + ); + }); + } + const next = main.shift(); + if (!next) throw new Error("unexpected request"); + return next; + }; + const { session } = await start(Array(12).fill(reply), 200_000, true, { + thresholdTokens: 12_000, + supersedeReads: false, + prune: { enabled: false }, + }); + await session.prompt("Read b.ts twice, then a.ts"); + expect(noteSignal?.aborted).toBe(false); + const result = await session.compact("the next step"); + expect(abortedBeforeFocus).toBe(true); + expect(notes).toBe(2); + expect(result.summary).toContain("Focused note"); + }); + + it("waits for an unfinished note at an early threshold instead of compacting", async () => { + const main = [ + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + fauxAssistantMessage(fauxToolCall("read", { path: "b.ts" })), + fauxAssistantMessage(fauxToolCall("read", { path: "a.ts" })), + fauxAssistantMessage("done"), + ]; + let notes = 0; + let summaries = 0; + let noteSignal: AbortSignal | undefined; + const reply: FauxResponseStep = (context, options) => { + const last = JSON.stringify(context.messages.at(-1)); + if (last.includes("handoff note")) { + notes++; + const signal = options?.signal; + if (!signal) throw new Error("missing note cancellation signal"); + noteSignal = signal; + return new Promise((resolve) => { + signal.addEventListener( + "abort", + () => resolve(fauxAssistantMessage("", { stopReason: "aborted" })), + { once: true }, + ); + }); + } + if (last.includes("")) { + summaries++; + return fauxAssistantMessage("## Goal\nPi summary"); + } + const next = main.shift(); + if (!next) throw new Error("unexpected request"); + return next; + }; + const { session } = await start(Array(12).fill(reply), 200_000, true, { + thresholdTokens: 12_000, + supersedeReads: false, + prune: { enabled: false }, + }); + try { + await session.prompt("Read b.ts twice, then a.ts"); + expect(notes).toBe(1); + expect(noteSignal?.aborted).toBe(false); + expect(summaries).toBe(0); + expect( + session.sessionManager + .getBranch() + .filter((entry) => entry.type === "compaction"), + ).toHaveLength(0); + expect(JSON.stringify(session.messages.at(-1))).toContain("done"); + } finally { + await session.reload(); + } + }); + it.each([ ["soft is not in methodOrder", 200_000, ["handoff", "shake"]], ["the threshold equals Pi's", 14_000, ["soft"]], diff --git a/tsconfig.json b/tsconfig.json index 90b9b3b..fa2602f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "noUnusedLocals": true, "noUnusedParameters": true }, - "include": ["extensions", "src", "test"] + "include": ["extensions", "scripts", "src", "test"] }