mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 18:51:37 +00:00
Auto-reply: fix non-default agent session transcript path resolution (#15154)
* Auto-reply: fix non-default agent transcript path resolution * Auto-reply: harden non-default agent transcript lookups * Auto-reply: harden session path resolution across agent stores
This commit is contained in:
committed by
GitHub
parent
79a38858ae
commit
ac41176532
@@ -2,6 +2,7 @@ import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
resolveSessionFilePath,
|
||||
resolveSessionFilePathOptions,
|
||||
resolveSessionTranscriptPath,
|
||||
resolveSessionTranscriptPathInDir,
|
||||
resolveStorePath,
|
||||
@@ -75,4 +76,19 @@ describe("session path safety", () => {
|
||||
const resolved = resolveSessionTranscriptPath("sess-1", "main");
|
||||
expect(resolved.endsWith(path.join("agents", "main", "sessions", "sess-1.jsonl"))).toBe(true);
|
||||
});
|
||||
|
||||
it("prefers storePath when resolving session file options", () => {
|
||||
const opts = resolveSessionFilePathOptions({
|
||||
storePath: "/tmp/custom/agent-store/sessions.json",
|
||||
agentId: "ops",
|
||||
});
|
||||
expect(opts).toEqual({
|
||||
sessionsDir: path.resolve("/tmp/custom/agent-store"),
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to agentId when storePath is absent", () => {
|
||||
const opts = resolveSessionFilePathOptions({ agentId: "ops" });
|
||||
expect(opts).toEqual({ agentId: "ops" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,6 +33,26 @@ export function resolveDefaultSessionStorePath(agentId?: string): string {
|
||||
return path.join(resolveAgentSessionsDir(agentId), "sessions.json");
|
||||
}
|
||||
|
||||
export type SessionFilePathOptions = {
|
||||
agentId?: string;
|
||||
sessionsDir?: string;
|
||||
};
|
||||
|
||||
export function resolveSessionFilePathOptions(params: {
|
||||
agentId?: string;
|
||||
storePath?: string;
|
||||
}): SessionFilePathOptions | undefined {
|
||||
const storePath = params.storePath?.trim();
|
||||
if (storePath) {
|
||||
return { sessionsDir: path.dirname(path.resolve(storePath)) };
|
||||
}
|
||||
const agentId = params.agentId?.trim();
|
||||
if (agentId) {
|
||||
return { agentId };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export const SAFE_SESSION_ID_RE = /^[a-z0-9][a-z0-9._-]{0,127}$/i;
|
||||
|
||||
export function validateSessionId(sessionId: string): string {
|
||||
@@ -43,7 +63,7 @@ export function validateSessionId(sessionId: string): string {
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function resolveSessionsDir(opts?: { agentId?: string; sessionsDir?: string }): string {
|
||||
function resolveSessionsDir(opts?: SessionFilePathOptions): string {
|
||||
const sessionsDir = opts?.sessionsDir?.trim();
|
||||
if (sessionsDir) {
|
||||
return path.resolve(sessionsDir);
|
||||
@@ -95,7 +115,7 @@ export function resolveSessionTranscriptPath(
|
||||
export function resolveSessionFilePath(
|
||||
sessionId: string,
|
||||
entry?: { sessionFile?: string },
|
||||
opts?: { agentId?: string; sessionsDir?: string },
|
||||
opts?: SessionFilePathOptions,
|
||||
): string {
|
||||
const sessionsDir = resolveSessionsDir(opts);
|
||||
const candidate = entry?.sessionFile?.trim();
|
||||
|
||||
Reference in New Issue
Block a user