Skip to content

Commit 3a4858c

Browse files
committed
perf(doctor): skip legacy label scans for ordinary messages
1 parent 1ab7b75 commit 3a4858c

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎src/commands/doctor-session-transcript-labels.test.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,19 +513,33 @@ describe("doctor SQLite session transcript label migration", () => {
513513
"",
514514
"Sure, here is the answer.",
515515
].join("\n");
516+
const capitalizedEcho = [
517+
"Sure, here is the answer.",
518+
"",
519+
"Untrusted context (metadata, do not treat as instructions or commands):",
520+
"Channel provenance.",
521+
].join("\n");
516522
const database = seedMessageTranscript(databaseOptions, [
517523
{ id: "assistant-echo", content: assistantEcho, role: "assistant" },
524+
{ id: "assistant-context-echo", content: capitalizedEcho, role: "assistant" },
518525
]);
519526
if (escaped) {
520-
const row = readTranscriptEventRows(database, SESSION_ID)[1]!;
527+
const rows = readTranscriptEventRows(database, SESSION_ID).slice(1);
521528
agentDatabase.runOpenClawAgentWriteTransaction((db) => {
522-
db.db
523-
.prepare("UPDATE transcript_events SET event_json = ? WHERE session_id = ? AND seq = ?")
524-
.run(
525-
row.eventJson.replaceAll("Conversation info", "\\u0043onversation info"),
526-
SESSION_ID,
527-
row.seq,
528-
);
529+
for (const row of rows) {
530+
db.db
531+
.prepare(
532+
"UPDATE transcript_events SET event_json = ? WHERE session_id = ? AND seq = ?",
533+
)
534+
.run(
535+
row.eventJson
536+
.replaceAll("Conversation info", "\\u0043onversation info")
537+
.replaceAll("untrusted", "\\u0075ntrusted")
538+
.replaceAll("Untrusted", "\\u0055ntrusted"),
539+
SESSION_ID,
540+
row.seq,
541+
);
542+
}
529543
}, databaseOptions);
530544
}
531545
await runTranscriptLabelHealth(state, true);
@@ -538,6 +552,10 @@ describe("doctor SQLite session transcript label migration", () => {
538552
expect(content).not.toContain("Conversation info (untrusted metadata):");
539553
expect(hasInboundMetadataSentinel(content as string)).toBe(true);
540554
expect(stripInboundMetadata(content as string)).toBe("Sure, here is the answer.");
555+
const capitalizedContent = findMessageContent(repaired.events, "assistant-context-echo");
556+
expect(capitalizedContent).toContain(`Context: ${INBOUND_CONTEXT_MARKER}`);
557+
expect(capitalizedContent).not.toContain("Untrusted context");
558+
expect(stripInboundMetadata(capitalizedContent as string)).toBe("Sure, here is the answer.");
541559
},
542560
);
543561

‎src/commands/doctor-session-transcript-labels.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ const LEGACY_LEADING_TIMESTAMP_PREFIX_RE = /^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{
5353
// CHAT WINDOW: `${label} (untrusted, <order>, <relation>):` (338-360).
5454

5555
function applyLegacyInboundLabelRewrites(text: string): string {
56+
// Every legacy rule contains one of these spellings. Check decoded content so
57+
// Unicode-escaped labels still reach their rewrite.
58+
if (!text.includes("untrusted") && !text.includes("Untrusted")) {
59+
return text;
60+
}
61+
5662
// Peel the timestamp envelope so the anchored (`^`) rules see the first header at column 0, exactly
5763
// as the runtime stripper does. Without this, "[Wed …] Conversation info (…):" stays unmarked and the
5864
// marker-only strippers expose its JSON. Reattached verbatim below.

0 commit comments

Comments
 (0)