fix: session_id generated at agent load (not lazy)

This commit is contained in:
Markov 2026-02-24 11:19:24 +01:00
parent a9b2d43f84
commit 9d897621a7
2 changed files with 22 additions and 33 deletions

View File

@ -36,33 +36,6 @@ export interface AgentConfig {
allowedPaths: string[]; allowedPaths: string[];
/** Persistent session UUID — survives renames. Stored in agent.json. */ /** Persistent session UUID — survives renames. Stored in agent.json. */
sessionId: string; sessionId: string;
/** Internal: path to config file for lazy session_id write-back */
_configPath: string | null;
/** Internal: parsed config data for write-back */
_configData: Record<string, unknown>;
}
/**
* Ensure agent has a persistent session_id.
* Call lazily on first agent invocation, not at startup.
* Generates UUID and writes it back to agent.json.
*/
export function ensureSessionId(config: AgentConfig): string {
if (config.sessionId) return config.sessionId;
const id = crypto.randomUUID();
config.sessionId = id;
if (config._configPath) {
try {
config._configData.session_id = id;
fs.writeFileSync(config._configPath, JSON.stringify(config._configData, null, 2) + '\n');
} catch {
// Non-fatal
}
}
return id;
} }
const homeDir = os.homedir(); const homeDir = os.homedir();
@ -189,8 +162,24 @@ export function loadAgentConfig(): AgentConfig {
apiKey: process.env.PICOGENT_API_KEY || process.env.ANTHROPIC_API_KEY || (file.api_key as string) || '', apiKey: process.env.PICOGENT_API_KEY || process.env.ANTHROPIC_API_KEY || (file.api_key as string) || '',
heartbeatIntervalSec: (file.heartbeat_interval_sec as number) || 30, heartbeatIntervalSec: (file.heartbeat_interval_sec as number) || 30,
allowedPaths: (file.allowed_paths as string[]) || [], allowedPaths: (file.allowed_paths as string[]) || [],
sessionId: (file.session_id as string) || '', sessionId: ensureSessionId(file, configPath),
_configPath: configPath,
_configData: file,
}; };
} }
/**
* Ensure agent.json has a persistent session_id.
* Generated on first load, persisted to 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();
if (configPath) {
try {
file.session_id = id;
fs.writeFileSync(configPath, JSON.stringify(file, null, 2) + '\n');
} catch { /* non-fatal */ }
}
return id;
}

View File

@ -4,7 +4,7 @@ import { runAgent } from './agent.js';
import { TrackerClient } from './tracker/client.js'; import { TrackerClient } from './tracker/client.js';
import { createTrackerTools } from './tools/index.js'; import { createTrackerTools } from './tools/index.js';
import type { ToolDefinition } from '@mariozechner/pi-coding-agent'; import type { ToolDefinition } from '@mariozechner/pi-coding-agent';
import { ensureSessionId, type AgentConfig } from './config.js'; import type { AgentConfig } from './config.js';
import type { TrackerEvent, TrackerTask } from './tracker/types.js'; import type { TrackerEvent, TrackerTask } from './tracker/types.js';
export interface TaskTracker { export interface TaskTracker {
@ -86,7 +86,7 @@ export class EventRouter {
let collectedText = ''; let collectedText = '';
for await (const msg of runAgent(prompt, { for await (const msg of runAgent(prompt, {
workDir: this.config.workDir, workDir: this.config.workDir,
sessionId: ensureSessionId(this.config), sessionId: this.config.sessionId,
model: this.config.model, model: this.config.model,
provider: this.config.provider, provider: this.config.provider,
systemPrompt: this.config.prompt || undefined, systemPrompt: this.config.prompt || undefined,
@ -158,7 +158,7 @@ export class EventRouter {
let collectedText = ''; let collectedText = '';
for await (const msg of runAgent(content, { for await (const msg of runAgent(content, {
workDir: this.config.workDir, workDir: this.config.workDir,
sessionId: ensureSessionId(this.config), sessionId: this.config.sessionId,
model: this.config.model, model: this.config.model,
provider: this.config.provider, provider: this.config.provider,
systemPrompt: this.config.prompt || undefined, systemPrompt: this.config.prompt || undefined,