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)
This commit is contained in:
Markov 2026-02-24 10:45:48 +01:00
parent a89e9a1bb9
commit cb618a195e
2 changed files with 29 additions and 1 deletions

View File

@ -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<string, unknown>, 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;
}

View File

@ -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,