Skip to content

Commit 3a64889

Browse files
fix(agent-core): report correct JSONL entry line after blank rows (#103645)
* fix(agent-core): report jsonl entry physical line numbers * fix(agent-core): preserve physical JSONL line numbers * test(agent-core): keep JSONL cleanup lint-safe --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 091584b commit 3a64889

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎packages/agent-core/src/harness/session/jsonl-storage.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Agent Core tests cover jsonl storage behavior.
22
import { describe, expect, it } from "vitest";
3+
import { NodeExecutionEnv } from "../env/nodejs.js";
34
import { ok, type FileSystem } from "../types.js";
45
import { JsonlSessionStorage, loadJsonlSessionMetadata } from "./jsonl-storage.js";
56
import { Session } from "./session.js";
@@ -57,6 +58,55 @@ describe("JsonlSessionStorage timestamps", () => {
5758
);
5859
});
5960

61+
it("reports physical entry line numbers when blank JSONL rows are skipped", async () => {
62+
const header = JSON.stringify({
63+
type: "session",
64+
version: 3,
65+
id: "session-1",
66+
timestamp: "2026-01-01T00:00:00.000Z",
67+
cwd: "/repo",
68+
});
69+
const entry = JSON.stringify({
70+
type: "custom",
71+
id: "entry-1",
72+
parentId: null,
73+
timestamp: "2026-01-01T00:00:01.000Z",
74+
customType: "note",
75+
});
76+
const invalidContent = [header, "\t\r", "", entry, " ", "not-json", ""].join("\n");
77+
const validContent = [header, "\t\r", "", entry, " ", ""].join("\n");
78+
const rootEnv = new NodeExecutionEnv({ cwd: process.cwd() });
79+
const created = await rootEnv.createTempDir("agent-core-jsonl-");
80+
if (!created.ok) {
81+
throw created.error;
82+
}
83+
const fs = new NodeExecutionEnv({ cwd: created.value });
84+
85+
try {
86+
const invalidWrite = await fs.writeFile("invalid-entry.jsonl", invalidContent);
87+
if (!invalidWrite.ok) {
88+
throw invalidWrite.error;
89+
}
90+
await expect(JsonlSessionStorage.open(fs, "invalid-entry.jsonl")).rejects.toMatchObject({
91+
name: "SessionError",
92+
code: "invalid_entry",
93+
message: "Invalid JSONL session file invalid-entry.jsonl: line 6 is not valid JSON",
94+
});
95+
96+
const validWrite = await fs.writeFile("valid.jsonl", validContent);
97+
if (!validWrite.ok) {
98+
throw validWrite.error;
99+
}
100+
const storage = await JsonlSessionStorage.open(fs, "valid.jsonl");
101+
expect((await storage.getEntries()).map((storedEntry) => storedEntry.id)).toEqual([
102+
"entry-1",
103+
]);
104+
} finally {
105+
const removed = await rootEnv.remove(created.value, { recursive: true, force: true });
106+
expect(removed.ok).toBe(true);
107+
}
108+
});
109+
60110
it("uses a leaf control's opaque append parent for the next entry", async () => {
61111
let content = [
62112
{

‎packages/agent-core/src/harness/session/jsonl-storage.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,22 @@ async function loadJsonlStorage(
186186
await fs.readTextFile(filePath),
187187
`Failed to read session ${filePath}`,
188188
);
189-
const lines = content.split("\n").filter((line) => line.trim());
190-
if (lines.length === 0) {
189+
const lines = content.split("\n");
190+
const headerIndex = lines.findIndex((line) => line.trim());
191+
if (headerIndex === -1) {
191192
throw invalidSession(filePath, "missing session header");
192193
}
193194

194-
const header = parseHeaderLine(lines[0], filePath);
195+
const header = parseHeaderLine(lines[headerIndex], filePath);
195196
const entries: SessionTreeEntry[] = [];
196197
let leafId: string | null = null;
197198
let appendParentId: string | null = null;
198-
for (let i = 1; i < lines.length; i++) {
199-
const entry = parseEntryLine(lines[i], filePath, i + 1);
199+
for (let lineIndex = headerIndex + 1; lineIndex < lines.length; lineIndex++) {
200+
const line = lines[lineIndex];
201+
if (!line.trim()) {
202+
continue;
203+
}
204+
const entry = parseEntryLine(line, filePath, lineIndex + 1);
200205
entries.push(entry);
201206
const leafUpdate = leafIdUpdateAfterEntry(entry);
202207
if (leafUpdate !== undefined) {

0 commit comments

Comments
 (0)