fix: land multi-agent session path fix + regressions (#15103) (#15448)

Co-authored-by: Josh Lehman <josh@martian.engineering>
This commit is contained in:
Peter Steinberger
2026-02-13 14:17:24 +01:00
committed by GitHub
parent 5d37b204c0
commit 990413534a
11 changed files with 274 additions and 37 deletions

View File

@@ -167,6 +167,7 @@ export const handleUsageCommand: CommandHandler = async (params, allowTextComman
sessionEntry: params.sessionEntry,
sessionFile: params.sessionEntry?.sessionFile,
config: params.cfg,
agentId: params.agentId,
});
const summary = await loadCostUsageSummary({ days: 30, config: params.cfg });

View File

@@ -224,6 +224,7 @@ export async function buildStatusReply(params: {
verboseDefault: agentDefaults.verboseDefault,
elevatedDefault: agentDefaults.elevatedDefault,
},
agentId: statusAgentId,
sessionEntry,
sessionKey,
sessionScope,

View File

@@ -55,12 +55,13 @@ export type SessionInitResult = {
function forkSessionFromParent(params: {
parentEntry: SessionEntry;
agentId: string;
sessionsDir: string;
}): { sessionId: string; sessionFile: string } | null {
const parentSessionFile = resolveSessionFilePath(
params.parentEntry.sessionId,
params.parentEntry,
{ sessionsDir: params.sessionsDir },
{ agentId: params.agentId, sessionsDir: params.sessionsDir },
);
if (!parentSessionFile || !fs.existsSync(parentSessionFile)) {
return null;
@@ -225,11 +226,7 @@ export async function initSessionState(params: {
? evaluateSessionFreshness({ updatedAt: entry.updatedAt, now, policy: resetPolicy }).fresh
: false;
// When this is the first user message in a thread, the session entry may already
// exist (created by recordInboundSession in prepare.ts), but we should still treat
// it as a new session so that thread context (history/starter/fork) is applied.
const forceNewForThread = Boolean(ctx.IsFirstThreadTurn) && !resetTriggered;
if (!isNewSession && freshEntry && !forceNewForThread) {
if (!isNewSession && freshEntry) {
sessionId = entry.sessionId;
systemSent = entry.systemSent ?? false;
abortedLastRun = entry.abortedLastRun ?? false;
@@ -335,6 +332,7 @@ export async function initSessionState(params: {
);
const forked = forkSessionFromParent({
parentEntry: sessionStore[parentSessionKey],
agentId,
sessionsDir: path.dirname(storePath),
});
if (forked) {
@@ -358,7 +356,6 @@ export async function initSessionState(params: {
// Clear stale token metrics from previous session so /status doesn't
// display the old session's context usage after /new or /reset.
sessionEntry.totalTokens = undefined;
sessionEntry.totalTokensFresh = false;
sessionEntry.inputTokens = undefined;
sessionEntry.outputTokens = undefined;
sessionEntry.contextTokens = undefined;

View File

@@ -258,25 +258,6 @@ describe("buildStatusMessage", () => {
expect(normalized).toContain("Queue: collect");
});
it("treats stale cached totals as unknown context usage", () => {
const text = buildStatusMessage({
agent: { model: "anthropic/claude-opus-4-5", contextTokens: 32_000 },
sessionEntry: {
sessionId: "stale-1",
updatedAt: 0,
totalTokens: 12_345,
totalTokensFresh: false,
contextTokens: 32_000,
},
sessionKey: "agent:main:main",
sessionScope: "per-sender",
queue: { mode: "collect", depth: 0 },
modelAuth: "api-key",
});
expect(normalizeTestText(text)).toContain("Context: ?/32k");
});
it("includes group activation for group sessions", () => {
const text = buildStatusMessage({
agent: {},
@@ -487,6 +468,69 @@ describe("buildStatusMessage", () => {
{ prefix: "openclaw-status-" },
);
});
it("reads transcript usage using explicit agentId when sessionKey is missing", async () => {
await withTempHome(
async (dir) => {
vi.resetModules();
const { buildStatusMessage: buildStatusMessageDynamic } = await import("./status.js");
const sessionId = "sess-worker2";
const logPath = path.join(
dir,
".openclaw",
"agents",
"worker2",
"sessions",
`${sessionId}.jsonl`,
);
fs.mkdirSync(path.dirname(logPath), { recursive: true });
fs.writeFileSync(
logPath,
[
JSON.stringify({
type: "message",
message: {
role: "assistant",
model: "claude-opus-4-5",
usage: {
input: 2,
output: 3,
cacheRead: 1200,
cacheWrite: 0,
totalTokens: 1205,
},
},
}),
].join("\n"),
"utf-8",
);
const text = buildStatusMessageDynamic({
agent: {
model: "anthropic/claude-opus-4-5",
contextTokens: 32_000,
},
agentId: "worker2",
sessionEntry: {
sessionId,
updatedAt: 0,
totalTokens: 5,
contextTokens: 32_000,
},
// Intentionally omitted: sessionKey
sessionScope: "per-sender",
queue: { mode: "collect", depth: 0 },
includeTranscriptUsage: true,
modelAuth: "api-key",
});
expect(normalizeTestText(text)).toContain("Context: 1.2k/32k");
},
{ prefix: "openclaw-status-" },
);
});
});
describe("buildCommandsMessage", () => {

View File

@@ -12,7 +12,6 @@ import { resolveSandboxRuntimeStatus } from "../agents/sandbox.js";
import { derivePromptTokens, normalizeUsage, type UsageLike } from "../agents/usage.js";
import {
resolveMainSessionKey,
resolveFreshSessionTotalTokens,
resolveSessionFilePath,
resolveSessionFilePathOptions,
type SessionEntry,
@@ -59,6 +58,7 @@ type QueueStatus = {
type StatusArgs = {
config?: OpenClawConfig;
agent: AgentConfig;
agentId?: string;
sessionEntry?: SessionEntry;
sessionKey?: string;
sessionScope?: SessionScope;
@@ -169,6 +169,7 @@ const formatQueueDetails = (queue?: QueueStatus) => {
const readUsageFromSessionLog = (
sessionId?: string,
sessionEntry?: SessionEntry,
agentId?: string,
sessionKey?: string,
storePath?: string,
):
@@ -186,11 +187,12 @@ const readUsageFromSessionLog = (
}
let logPath: string;
try {
const agentId = sessionKey ? resolveAgentIdFromSessionKey(sessionKey) : undefined;
const resolvedAgentId =
agentId ?? (sessionKey ? resolveAgentIdFromSessionKey(sessionKey) : undefined);
logPath = resolveSessionFilePath(
sessionId,
sessionEntry,
resolveSessionFilePathOptions({ agentId, storePath }),
resolveSessionFilePathOptions({ agentId: resolvedAgentId, storePath }),
);
} catch {
return undefined;
@@ -344,7 +346,7 @@ export function buildStatusMessage(args: StatusArgs): string {
let inputTokens = entry?.inputTokens;
let outputTokens = entry?.outputTokens;
let totalTokens = resolveFreshSessionTotalTokens(entry);
let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
// Prefer prompt-size tokens from the session transcript when it looks larger
// (cached prompt tokens are often missing from agent meta/store).
@@ -352,6 +354,7 @@ export function buildStatusMessage(args: StatusArgs): string {
const logUsage = readUsageFromSessionLog(
entry?.sessionId,
entry,
args.agentId,
args.sessionKey,
args.sessionStorePath,
);