From cb618a195e2391c5e810d501d2fb726c58ef5915 Mon Sep 17 00:00:00 2001 From: Markov Date: Tue, 24 Feb 2026 10:45:48 +0100 Subject: [PATCH] feat: persistent session UUID in agent.json - session_id auto-generated on first run, saved to agent.json - Survives agent renames (slug changes don't break session history) - Directory mode: agent works inside its folder (agentHome = workspace) --- src/config.ts | 28 ++++++++++++++++++++++++++++ src/router.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index e337253..4aa2127 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,7 @@ import path from 'path'; import os from 'os'; import fs from 'fs'; +import crypto from 'crypto'; export interface Config { port: number; @@ -33,6 +34,8 @@ export interface AgentConfig { heartbeatIntervalSec: number; /** Restrict file access to these directories. Empty = unrestricted. */ allowedPaths: string[]; + /** Persistent session UUID — survives renames. Stored in agent.json. */ + sessionId: string; } const homeDir = os.homedir(); @@ -159,5 +162,30 @@ export function loadAgentConfig(): AgentConfig { apiKey: process.env.PICOGENT_API_KEY || process.env.ANTHROPIC_API_KEY || (file.api_key as string) || '', heartbeatIntervalSec: (file.heartbeat_interval_sec as number) || 30, allowedPaths: (file.allowed_paths as string[]) || [], + sessionId: ensureSessionId(file, configPath), }; } + +/** + * Ensure agent.json has a persistent session_id. + * Generates UUID on first run and writes it back to the config file. + */ +function ensureSessionId(file: Record, configPath: string | null): string { + if (file.session_id && typeof file.session_id === 'string') { + return file.session_id; + } + + const id = crypto.randomUUID(); + + // Persist to agent.json if we have a path + if (configPath) { + try { + file.session_id = id; + fs.writeFileSync(configPath, JSON.stringify(file, null, 2) + '\n'); + } catch { + // Non-fatal — session works in memory + } + } + + return id; +} diff --git a/src/router.ts b/src/router.ts index 67eaab4..cceec3d 100644 --- a/src/router.ts +++ b/src/router.ts @@ -24,7 +24,7 @@ export class EventRouter { private client: TrackerClient, private taskTracker: TaskTracker, ) { - this.sessionId = `agent-${config.slug}`; + this.sessionId = config.sessionId; this.trackerTools = createTrackerTools({ trackerClient: client, agentSlug: config.slug,