fix: single session per agent instance

One agent = one session. Chat, tasks, tools — all share the same
context. Session ID: 'agent-{slug}' (stable across restarts).
This commit is contained in:
Markov 2026-02-23 22:28:57 +01:00
parent b2a620185f
commit a8f205609b

View File

@ -16,17 +16,20 @@ export class EventRouter {
private log = logger.child({ component: 'event-router' });
private activeTasks = 0;
private trackerTools: ToolDefinition[];
/** Single session ID for the entire agent lifetime. */
private readonly sessionId: string;
constructor(
private config: AgentConfig,
private client: TrackerClient,
private taskTracker: TaskTracker,
) {
this.sessionId = `agent-${config.slug}`;
this.trackerTools = createTrackerTools({
trackerClient: client,
agentSlug: config.slug,
});
this.log.info({ toolCount: this.trackerTools.length }, 'Tracker tools registered');
this.log.info({ toolCount: this.trackerTools.length, sessionId: this.sessionId }, 'Tracker tools registered');
}
async handleEvent(event: TrackerEvent): Promise<void> {
@ -87,7 +90,7 @@ export class EventRouter {
let collectedText = '';
for await (const msg of runAgent(prompt, {
workDir: this.config.workDir,
sessionId: `task-${task.id}`,
sessionId: this.sessionId,
model: this.config.model,
provider: this.config.provider,
systemPrompt: this.config.prompt || undefined,
@ -156,13 +159,10 @@ export class EventRouter {
this.log.info('│ MESSAGE from @%s: "%s"', authorSlug, content.slice(0, 200));
this.log.info('│ Context: %s | Mentioned: %s', taskId ? `task=${taskId}` : chatId ? `chat=${chatId}` : 'none', isMentioned);
// Stable session ID per chat/task context — preserves conversation history
const sessionId = chatId ? `chat-${chatId}` : taskId ? `task-${taskId}` : `msg-${Date.now()}`;
let collectedText = '';
for await (const msg of runAgent(content, {
workDir: this.config.workDir,
sessionId,
sessionId: this.sessionId,
model: this.config.model,
provider: this.config.provider,
systemPrompt: this.config.prompt || undefined,