Skip to content

Commit c6a0b57

Browse files
committed
fix(sessions): route diagnose transcript tail through accessor
1 parent 935c461 commit c6a0b57

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/config/sessions/session-accessor.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,31 @@ export async function loadTranscriptEvents(
607607
return events;
608608
}
609609

610+
/** Reads a bounded transcript tail through the session accessor boundary. */
611+
export async function readTranscriptTailLines(
612+
scope: SessionTranscriptReadScope & { maxLines: number },
613+
): Promise<{ lines: string[]; totalLines: number } | null> {
614+
const transcript = await resolveTranscriptReadAccess(scope);
615+
const maxLines = Math.max(1, Math.floor(scope.maxLines));
616+
const lines: string[] = [];
617+
let totalLines = 0;
618+
try {
619+
for await (const line of streamSessionTranscriptLines(transcript.sessionFile)) {
620+
if (!line.trim()) {
621+
continue;
622+
}
623+
totalLines += 1;
624+
lines.push(line);
625+
if (lines.length > maxLines) {
626+
lines.shift();
627+
}
628+
}
629+
} catch {
630+
return null;
631+
}
632+
return { lines, totalLines };
633+
}
634+
610635
/**
611636
* Appends a non-message transcript record such as session or metadata events.
612637
* Message records must use appendTranscriptMessage so parent links, idempotency,

src/gateway/server-methods/sessions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { resolveAgentMainSessionKey } from "../../config/sessions/main-session.j
6666
import {
6767
applySessionPatchProjection,
6868
createSessionEntryWithTranscript,
69+
readTranscriptTailLines,
6970
trimSessionTranscriptForManualCompact,
7071
} from "../../config/sessions/session-accessor.js";
7172
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -107,7 +108,6 @@ import {
107108
import { reactivateCompletedSubagentSession } from "../session-subagent-reactivation.js";
108109
import {
109110
readRecentSessionMessagesWithStatsAsync,
110-
readRecentSessionTranscriptLines,
111111
readSessionMessageCountAsync,
112112
readSessionPreviewItemsFromTranscript,
113113
} from "../session-transcript-readers.js";
@@ -1385,12 +1385,12 @@ function summarizeDiagnose(params: {
13851385
};
13861386
}
13871387

1388-
function buildDiagnoseResult(params: {
1388+
async function buildDiagnoseResult(params: {
13891389
cfg: OpenClawConfig;
13901390
context: GatewayRequestContext;
13911391
p: DiagnoseParams;
13921392
target: DiagnoseTarget;
1393-
}): SessionsDiagnoseResult {
1393+
}): Promise<SessionsDiagnoseResult> {
13941394
const { cfg, context, p, target } = params;
13951395
const now = Date.now();
13961396
const defaultAgentId = resolveDefaultAgentId(cfg);
@@ -1451,7 +1451,7 @@ function buildDiagnoseResult(params: {
14511451
};
14521452
const lane = getCommandLaneSnapshot(resolveSessionLane(target.key));
14531453
const tail = target.entry.sessionId
1454-
? readRecentSessionTranscriptLines({
1454+
? await readTranscriptTailLines({
14551455
sessionId: target.entry.sessionId,
14561456
storePath: target.storePath,
14571457
sessionFile: target.entry.sessionFile,
@@ -1863,7 +1863,7 @@ export const sessionsHandlers: GatewayRequestHandlers = {
18631863
);
18641864
return;
18651865
}
1866-
respond(true, buildDiagnoseResult({ cfg, context, p, target }), undefined);
1866+
respond(true, await buildDiagnoseResult({ cfg, context, p, target }), undefined);
18671867
} catch (error) {
18681868
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, formatErrorMessage(error)));
18691869
}

0 commit comments

Comments
 (0)