Yoke.sh
Agents

Compaction

Pluggable transcript compaction — MessageCompactor, monologue swap, and agent.monologue.compacted.

Compaction reduces monologue transcript size before LLM inference. When compaction changes the message list, the processor swaps to a fresh monologue conversation and repoints the forum participant stint.

Interface

import type { InferenceMessage, MessageCompactor } from "@yoke/sdk";

const compaction: MessageCompactor = async (messages: InferenceMessage[]) => {
  if (messages.length <= 20) return messages;
  return messages.slice(-10);
};

const agentStack = defineAgentStack({ compaction });

defineProcessorActor({
  configSchema: processorConfigSchema,
  stateSchema: processorStateSchema,
  compaction: agentStack.compaction,
});

MessageCompactor receives and returns InferenceMessage[] — the same shape as ctx.llm.infer input.

Preserve body.meta (especially sourceActorId) on user messages so hydration can reuse external shadow actors ingestion created.

Flow

monologue message posted
        │
        ▼
processor reads transcript → InferenceMessage[]
        │
        ▼
compaction(messages)  [optional]
        │
        ├─ unchanged ──► ctx.llm.infer(messages)
        │
        └─ changed ──► create new monologue
                       hydrate with compacted messages
                       update forum participant.metadata.monologueId
                       emit agent.monologue.compacted (egress updates routing)
                       ctx.llm.infer(compacted)
        │
        ▼
post type: "reply" to active monologue

Storage updates on swap

LocationWhat changes
participants.metadata.monologueIdForum stint repointed to new monologue
Processor ctx.statemonologueId, agentParticipantId
IngestionResolves monologue from participant metadata; resets external-actor cache
Egress ctx.state.joinsUpdated via agent.monologue.compacted event

Canonical monologue id lives in Postgres participants.metadata.monologueId — one monologue per forum stint.

Helpers

ExportPurpose
defineAgentStackTyped holder for compaction and tools
inferenceMessagesEqualCompare before/after compaction
swapMonologueAfterCompactionCreate monologue, hydrate, update participant
hydrateMonologueWithMessagesPost compacted inference messages
collectExternalActorsGather shadow actors before swap
resolveMonologueStintRead active monologue from forum participant metadata

Example compactor

Example compactor in your package:

const agentStack = defineAgentStack({
  compaction: async (messages) => {
    if (messages.length <= 15) return messages;
    // Keep system context + recent tail
    return [messages[0], ...messages.slice(-8)];
  },
});

When to compact

ScenarioStrategy
Context window pressureTruncate/summarize old turns
Long support threadsKeep last N messages + summary line
Tool-heavy sessionsDrop intermediate tool rounds, keep finals

Compaction runs before each infer call when configured. If the compactor returns an identical list (inferenceMessagesEqual), no swap occurs.

Events

EventWhen
agent.monologue.compactedAfter swap — egress updates forumId → monologueId routing

On this page