Skip to content

@Compaction

@Compaction ("@agboss/core") declares how completed conversation turns older than a reader’s uncompactedTurns window are summarized in multi-turn threads. A @Compaction class is bound via compaction: MyCompaction on @Application, @Workflow, @Agent, or @Router (the most specific level that sets compaction wins).

A @Compaction name must end with -compaction (and its TypeScript class name must end with Compaction). Choose one of four strategies via CompactionStrategy (SummaryQueue, SingleSummary, Custom, or None). With CompactionStrategy.Custom, your class implements CompactionHandler (compact(input: CompactionInput): Promise<readonly string[]> | readonly string[]) and can inject DI services, TenantContext, or ExecutionContext via deps: [...]:

Option Type Required Description
name string Yes Kebab-case identifier ending with -compaction; the class name must end with Compaction ([name.kind-suffix]).
description string Yes Non-empty summary of the compaction policy.
strategy CompactionStrategy Yes CompactionStrategy.SummaryQueue, CompactionStrategy.SingleSummary, CompactionStrategy.Custom, or CompactionStrategy.None.
turnsBetweenCompactions number For SummaryQueue, SingleSummary, and Custom Positive integer (>= 1) specifying how many aged-out turns accumulate before compaction runs.
deps readonly Class[] No (Custom only) Injectable dependencies (@Injectable(), TenantContext, ExecutionContext) passed to the CompactionHandler constructor.
model string For SummaryQueue and SingleSummary Chat model identifier used to write summaries (resolved against @ModelProvider({ serves })). Forbidden on Custom and None ([compaction.invalid-setting]).
summaryQueueLength number For SummaryQueue only Positive integer (>= 1) specifying how many summaries are retained in the FIFO queue. Forbidden on Custom, SingleSummary, and None.
summaryMaxTokens number | TokenLimit.MODEL_MAX For SummaryQueue and SingleSummary max_tokens limit for each summary call. Forbidden on Custom and None.
prompt / promptUrls string / readonly string[] No (SummaryQueue / SingleSummary) Custom summarization instructions (defaults to the built-in factual summarization prompt). Forbidden on Custom and None.

How CompactionHandler & CompactionInput work

Section titled “How CompactionHandler & CompactionInput work”

When strategy: CompactionStrategy.Custom is selected, the decorated class must implement CompactionHandler (enforced by TypeScript and at agboss check / assembly time via [compaction.missing-handler]):

Interface / Type Signature / Fields Description
CompactionHandler compact(input: CompactionInput): Promise<readonly string[]> | readonly string[] Receives the aged-out turns and existing summaries (both masked by active @PiiPolicy rules) and returns the complete list of summaries to store for the thread. Throws CompactionError if compact() rejects or returns a non-string / blank item.
CompactionInput { turnsToCompact: readonly CompactionTurn[]; previousSummaries: readonly string[] } Frozen input passed to compact(). previousSummaries is [] on the first compaction of a thread and the previously returned readonly string[] on subsequent runs.
CompactionTurn { startName: string; input: Readonly<Record<string, unknown>>; finishName: string; output: Readonly<Record<string, unknown>> } One completed workflow turn older than the uncompactedTurns window, with its input and output masked by the application’s and workflow’s @PiiPolicy rules.