Skip to content

Commit 29c8d02

Browse files
Dmatut7claude
andcommitted
fix(migration-legacy): don't classify valid-JSON non-object lines as corrupt
`analyzeContextContent` documents `corrupt` as "every non-blank line failed to parse" (disk damage / truncated write). But a line that JSON.parse accepted yet was a scalar/array (e.g. `42`, `"hi"`, `[]`) hit the object-shape `continue` before `hadParseableLine` was set, so a context of all valid-JSON non-objects was misreported as `corrupt` and surfaced as a migration failure instead of an empty/skipped session. Mark the line as parseable as soon as JSON.parse succeeds, before the shape check — matching the documented contract and the existing "at least one line parses → empty" test intent. Genuinely truncated lines still fail JSON.parse and remain `corrupt`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0e0baea commit 29c8d02

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

packages/migration-legacy/src/sessions/translator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ export function analyzeContextContent(lines: readonly string[]): ContextContent
4949
} catch {
5050
continue;
5151
}
52-
if (typeof parsed !== 'object' || parsed === null) continue;
52+
// A line that JSON.parse accepts has "parsed" per the corrupt contract
53+
// above, even when it is a scalar/array rather than an object. Mark it
54+
// before the shape check so an all-valid-JSON-but-no-object context is
55+
// classified 'empty' (cleared session), not 'corrupt' (disk damage).
5356
hadParseableLine = true;
57+
if (typeof parsed !== 'object' || parsed === null) continue;
5458
const role = (parsed as Record<string, unknown>)['role'];
5559
if (typeof role === 'string' && USABLE_ROLES.has(role)) return 'real';
5660
}

packages/migration-legacy/test/sessions/translator.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,12 @@ describe('analyzeContextContent', () => {
170170
]),
171171
).toBe('empty');
172172
});
173+
174+
it("'empty' (not 'corrupt') when lines are valid JSON scalars/arrays, not objects", () => {
175+
// These lines parse successfully, so they are not "disk damage". Per the
176+
// corrupt contract (every non-blank line *failed to parse*), they must be
177+
// classified 'empty', not 'corrupt'.
178+
expect(analyzeContextContent(['42', '"hi"', 'true'])).toBe('empty');
179+
expect(analyzeContextContent(['[]'])).toBe('empty');
180+
});
173181
});

0 commit comments

Comments
 (0)