2026-04-06 16:43:04 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2026 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
import type { Content } from '@google/genai';
|
|
|
|
|
|
2026-04-06 17:44:28 +00:00
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
import type { AgentChatHistory } from '../core/agentChatHistory.js';
|
|
|
|
|
import { debugLogger } from '../utils/debugLogger.js';
|
|
|
|
|
import type { Episode } from './ir/types.js';
|
|
|
|
|
|
2026-04-07 02:16:06 +00:00
|
|
|
import type { ContextEventBus } from './eventBus.js';
|
|
|
|
|
import type { ContextTracer } from './tracer.js';
|
2026-04-06 16:43:04 +00:00
|
|
|
|
2026-04-06 17:44:28 +00:00
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
|
|
|
|
|
import type { ContextEnvironment } from './sidecar/environment.js';
|
|
|
|
|
|
|
|
|
|
import type { SidecarConfig } from './sidecar/types.js';
|
2026-04-06 21:10:58 +00:00
|
|
|
|
2026-04-06 17:59:01 +00:00
|
|
|
import { PipelineOrchestrator } from './sidecar/orchestrator.js';
|
2026-04-06 18:46:21 +00:00
|
|
|
import { HistoryObserver } from './historyObserver.js';
|
2026-04-06 19:54:09 +00:00
|
|
|
|
2026-04-06 18:46:21 +00:00
|
|
|
import { generateWorkingBufferView } from './ir/graphUtils.js';
|
2026-04-06 18:01:32 +00:00
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
|
2026-04-06 21:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-06 18:46:21 +00:00
|
|
|
|
|
|
|
|
import { IrProjector } from './ir/projector.js';
|
2026-04-06 16:43:04 +00:00
|
|
|
|
2026-04-06 21:10:58 +00:00
|
|
|
import './sidecar/builtins.js';
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
export class ContextManager {
|
2026-04-06 17:44:28 +00:00
|
|
|
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
// The stateful, pristine Episodic Intermediate Representation graph.
|
|
|
|
|
// This allows the agent to remember and summarize continuously without losing data across turns.
|
|
|
|
|
private pristineEpisodes: Episode[] = [];
|
|
|
|
|
private readonly eventBus: ContextEventBus;
|
2026-04-06 17:44:28 +00:00
|
|
|
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
// Internal sub-components
|
|
|
|
|
// Synchronous processors are instantiated but effectively used as singletons within this class
|
2026-04-06 17:59:01 +00:00
|
|
|
private orchestrator: PipelineOrchestrator;
|
2026-04-06 18:46:21 +00:00
|
|
|
private historyObserver?: HistoryObserver;
|
2026-04-06 17:44:28 +00:00
|
|
|
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
|
2026-04-06 17:44:28 +00:00
|
|
|
constructor(private sidecar: SidecarConfig, private env: ContextEnvironment, private readonly tracer: ContextTracer) {
|
2026-04-06 19:18:17 +00:00
|
|
|
this.eventBus = env.eventBus;
|
2026-04-06 17:44:28 +00:00
|
|
|
|
2026-04-06 18:01:32 +00:00
|
|
|
this.orchestrator = new PipelineOrchestrator(this.sidecar, this.env, this.eventBus, this.tracer);
|
2026-04-06 16:43:04 +00:00
|
|
|
|
2026-04-06 19:54:09 +00:00
|
|
|
this.eventBus.onPristineHistoryUpdated((event) => {
|
|
|
|
|
this.pristineEpisodes = event.episodes;
|
|
|
|
|
this.evaluateTriggers();
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
this.eventBus.onVariantReady((event) => {
|
2026-04-06 17:44:28 +00:00
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
// Find the target episode in the pristine graph
|
|
|
|
|
const targetEp = this.pristineEpisodes.find(
|
|
|
|
|
(ep) => ep.id === event.targetId,
|
|
|
|
|
);
|
|
|
|
|
if (targetEp) {
|
|
|
|
|
if (!targetEp.variants) {
|
|
|
|
|
targetEp.variants = {};
|
|
|
|
|
}
|
|
|
|
|
targetEp.variants[event.variantId] = event.variant;
|
2026-04-06 17:44:28 +00:00
|
|
|
this.tracer.logEvent('ContextManager', `Received async variant [${event.variantId}] for Episode ${event.targetId}`);
|
2026-04-06 16:43:04 +00:00
|
|
|
debugLogger.log(
|
|
|
|
|
`ContextManager: Received async variant [${event.variantId}] for Episode ${event.targetId}.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Safely stops background workers and clears event listeners.
|
|
|
|
|
*/
|
|
|
|
|
shutdown() {
|
2026-04-06 17:59:01 +00:00
|
|
|
this.orchestrator.shutdown();
|
2026-04-06 18:46:21 +00:00
|
|
|
if (this.historyObserver) {
|
|
|
|
|
this.historyObserver.stop();
|
2026-04-06 16:43:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 19:54:09 +00:00
|
|
|
/**
|
|
|
|
|
* Evaluates if the current working buffer exceeds configured budget thresholds,
|
|
|
|
|
* firing consolidation events if necessary.
|
|
|
|
|
*/
|
|
|
|
|
private evaluateTriggers() {
|
|
|
|
|
if (!this.sidecar.budget) return;
|
|
|
|
|
|
|
|
|
|
const workingBuffer = this.getWorkingBufferView();
|
|
|
|
|
const currentTokens = this.env.tokenCalculator.calculateEpisodeListTokens(workingBuffer);
|
|
|
|
|
|
|
|
|
|
this.tracer.logEvent('ContextManager', 'Evaluated triggers', { currentTokens, retainedTokens: this.sidecar.budget.retainedTokens });
|
|
|
|
|
|
|
|
|
|
// 1. Eager Compute Trigger
|
|
|
|
|
this.eventBus.emitChunkReceived({ episodes: this.pristineEpisodes });
|
|
|
|
|
|
|
|
|
|
// 2. Budget Crossed Trigger
|
|
|
|
|
if (currentTokens > this.sidecar.budget.retainedTokens) {
|
|
|
|
|
const deficit = currentTokens - this.sidecar.budget.retainedTokens;
|
|
|
|
|
this.tracer.logEvent('ContextManager', 'Budget crossed. Emitting ConsolidationNeeded', { deficit });
|
|
|
|
|
this.eventBus.emitConsolidationNeeded({
|
|
|
|
|
episodes: workingBuffer,
|
|
|
|
|
targetDeficit: deficit,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 16:43:04 +00:00
|
|
|
/**
|
|
|
|
|
* Subscribes to the core AgentChatHistory to natively track all message events,
|
|
|
|
|
* converting them seamlessly into pristine Episodes.
|
|
|
|
|
*/
|
|
|
|
|
subscribeToHistory(chatHistory: AgentChatHistory) {
|
2026-04-06 18:46:21 +00:00
|
|
|
if (this.historyObserver) {
|
|
|
|
|
this.historyObserver.stop();
|
2026-04-06 16:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 18:46:21 +00:00
|
|
|
this.historyObserver = new HistoryObserver(
|
|
|
|
|
chatHistory,
|
|
|
|
|
this.eventBus,
|
|
|
|
|
this.tracer,
|
2026-04-06 19:54:09 +00:00
|
|
|
this.env.tokenCalculator,
|
2026-04-06 18:46:21 +00:00
|
|
|
);
|
|
|
|
|
this.historyObserver.start();
|
2026-04-06 16:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a computed view of the pristine log.
|
|
|
|
|
* Sweeps backwards (newest to oldest), tracking rolling tokens.
|
2026-04-06 17:44:28 +00:00
|
|
|
* When rollingTokens > retainedTokens, it injects the "best" available ready variant
|
2026-04-06 16:43:04 +00:00
|
|
|
* (snapshot > summary > masked) instead of the raw text.
|
|
|
|
|
* Handles N-to-1 variant skipping automatically.
|
|
|
|
|
*/
|
2026-04-07 02:16:06 +00:00
|
|
|
getWorkingBufferView(): Episode[] {
|
2026-04-06 18:46:21 +00:00
|
|
|
return generateWorkingBufferView(
|
|
|
|
|
this.pristineEpisodes,
|
|
|
|
|
this.sidecar.budget.retainedTokens,
|
2026-04-06 19:54:09 +00:00
|
|
|
this.tracer,
|
|
|
|
|
this.env
|
2026-04-06 18:46:21 +00:00
|
|
|
);
|
2026-04-06 16:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a temporary, compressed Content[] array to be used exclusively for the LLM request.
|
|
|
|
|
* This does NOT mutate the pristine episodic graph.
|
|
|
|
|
*/
|
|
|
|
|
async projectCompressedHistory(): Promise<Content[]> {
|
|
|
|
|
this.tracer.logEvent('ContextManager', 'Projection requested.');
|
2026-04-06 18:58:49 +00:00
|
|
|
const protectedIds = new Set<string>();
|
|
|
|
|
if (this.pristineEpisodes.length > 0) {
|
|
|
|
|
protectedIds.add(this.pristineEpisodes[0].id); // Structural invariant
|
2026-04-06 16:43:04 +00:00
|
|
|
}
|
2026-04-06 17:44:28 +00:00
|
|
|
|
2026-04-06 18:58:49 +00:00
|
|
|
return IrProjector.project(
|
|
|
|
|
this.getWorkingBufferView(),
|
|
|
|
|
this.orchestrator,
|
|
|
|
|
this.sidecar,
|
|
|
|
|
this.tracer,
|
|
|
|
|
this.env,
|
|
|
|
|
protectedIds
|
2026-04-06 16:43:04 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|