Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237/** Unit tests for Advisor context serialization. @module */
import type { Api, Model,} from '@earendil-works/pi-ai';import type { SessionEntry, } from '@earendil-works/pi-coding-agent';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { type AdvisorConfig, ADVISOR_MESSAGE_TYPE, buildAdvisorContext, DEFAULT_CONFIG, maxContextCharsForAdvisorModel, truncateContext,} from '../dist/final/node/index.mjs';
//region Fixtures
/** Context truncation budget for tests. */const TRUNCATION_BUDGET = 5;
/** Stable timestamp for fixture entries. */const TIMESTAMP = '2026-05-15T00:00:00.000Z';
/** Small model context window used in budget tests. */const SMALL_CONTEXT_WINDOW = 5_000;
/** Large model context window used in budget tests. */const LARGE_CONTEXT_WINDOW = 10_000;
/** Fixture max output tokens. */const MAX_TOKENS = 1_000;
/** Focused Advisor question fixture. */const FOCUS_QUESTION = 'Which assumption is weakest?';
/** Output token budget used in context-budget tests. */const OUTPUT_TOKEN_BUDGET = 100;
/** Configured context cap used in context-budget tests. */const CONFIGURED_CONTEXT_CAP = 10;
/** Advisor config fixture with prior Advisor results omitted. */const omitPriorAdvisorConfig: AdvisorConfig = { ...DEFAULT_CONFIG, includePriorAdvisorResults: false, source: { globalPath: '/home/test/.pi/agent/extensions/pi-advisor.json', projectPath: '/repo/.pi/extensions/pi-advisor.json', globalLoaded: false, projectLoaded: false, },};
/** Advisor config fixture with no configured context cap. */const dynamicBudgetConfig: AdvisorConfig = { ...omitPriorAdvisorConfig, maxAdvisorOutputTokens: OUTPUT_TOKEN_BUDGET,};
/** Advisor config fixture with configured context cap. */const cappedBudgetConfig: AdvisorConfig = { ...dynamicBudgetConfig, maxContextChars: CONFIGURED_CONTEXT_CAP,};
/** Advisor custom message from a previous manual `/advisor` run. */const priorAdvisorMessage: SessionEntry = { type: 'custom_message', id: 'advisor-message', parentId: null, timestamp: TIMESTAMP, customType: ADVISOR_MESSAGE_TYPE, content: 'prior advisor text', display: true,};
/** Non-Advisor custom message that should remain visible. */const otherCustomMessage: SessionEntry = { type: 'custom_message', id: 'other-message', parentId: 'advisor-message', timestamp: TIMESTAMP, customType: 'other-extension', content: 'other extension text', display: true,};
/** Build fixture Advisor model. @param contextWindow - token context window @returns fixture model */function fixtureModel( contextWindow: number,): Model<Api> { return { id: 'reviewer', name: 'Reviewer', api: 'faux', provider: 'faux-provider', baseUrl: 'https://example.invalid', reasoning: false, input: ['text',], cost: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, }, contextWindow, maxTokens: MAX_TOKENS, } satisfies Model<Api>;}
//endregion Fixtures
await describe({ name: buildAdvisorContext.name, children: [ it({ name: 'omits prior Advisor custom messages when configured', fn: async () => { const context = buildAdvisorContext({ branch: [ priorAdvisorMessage, otherCustomMessage, ], config: omitPriorAdvisorConfig, advisorSystemPrompt: 'review carefully', },); expect(context.text,).not.toContain('prior advisor text',); expect(context.text,).toContain('other extension text',); }, },), it({ name: 'counts focused question in input token estimate', fn: async () => { const contextWithoutQuestion = buildAdvisorContext({ branch: [otherCustomMessage,], config: omitPriorAdvisorConfig, advisorSystemPrompt: 'review carefully', },); const contextWithQuestion = buildAdvisorContext({ branch: [otherCustomMessage,], config: omitPriorAdvisorConfig, advisorSystemPrompt: 'review carefully', question: FOCUS_QUESTION, },); expect(contextWithQuestion.estimatedInputTokens,).toBeGreaterThan( contextWithoutQuestion.estimatedInputTokens, ); }, },), ],},);
await describe({ name: truncateContext.name, children: [ it({ name: 'preserves head and tail when truncating', fn: async () => { const result = truncateContext({ text: 'abcdefghij', maxChars: TRUNCATION_BUDGET, },); expect(result.truncated,).toBe(true,); expect(result.text,).toContain( 'advisor: middle of serialized conversation omitted', ); }, },), ],},);
await describe({ name: maxContextCharsForAdvisorModel.name, children: [ it({ name: 'derives a larger budget for a larger model context window', fn: async () => { const smallBudget = maxContextCharsForAdvisorModel({ config: dynamicBudgetConfig, model: fixtureModel(SMALL_CONTEXT_WINDOW,), advisorSystemPrompt: 'review carefully', },); const largeBudget = maxContextCharsForAdvisorModel({ config: dynamicBudgetConfig, model: fixtureModel(LARGE_CONTEXT_WINDOW,), advisorSystemPrompt: 'review carefully', },); expect(largeBudget,).toBeGreaterThan(smallBudget,); }, },), it({ name: 'honors configured context cap below model-derived budget', fn: async () => { const budget = maxContextCharsForAdvisorModel({ config: cappedBudgetConfig, model: fixtureModel(LARGE_CONTEXT_WINDOW,), advisorSystemPrompt: 'review carefully', },); expect(budget,).toBe(CONFIGURED_CONTEXT_CAP,); }, },), it({ name: 'reserves context budget for focused question', fn: async () => { const budgetWithoutQuestion = maxContextCharsForAdvisorModel({ config: dynamicBudgetConfig, model: fixtureModel(LARGE_CONTEXT_WINDOW,), advisorSystemPrompt: 'review carefully', },); const budgetWithQuestion = maxContextCharsForAdvisorModel({ config: dynamicBudgetConfig, model: fixtureModel(LARGE_CONTEXT_WINDOW,), advisorSystemPrompt: 'review carefully', question: FOCUS_QUESTION, },); expect(budgetWithQuestion,).toBeLessThan(budgetWithoutQuestion,); }, },), ],},);