Reviewed PR details and linked issues.
UnderstandProposed solution to rewrite corrupted session files.
BuildRan tests to confirm fix.
VerifyAdded changelog entry documenting fix.
EditYou are given one or more GitHub PR URLs: https://github.com/badlogic/pi-mono/pull/933
For each PR URL, do the following in order:
packages/*/CHANGELOG.md files. Report whether an entry exists. If missing, state that a changelog entry is required before merge and that you will add it if the user decides to merge. Follow the changelog format rules in AGENTS.md. Verify:
### Breaking Changes, ### Added, ### Fixed, etc.)Fixed foo ([#123](https://github.com/badlogic/pi-mono/pull/123) by [@user](https://github.com/user))### Breaking Changes, not just ### FixedOutput format per PR: PR: Changelog:
If no issues are found, say so under Bad and Ugly.
>gh pr view 933 --repo badlogic/pi-mono --json title,body,comments,commits,files>gh issue view 932 --repo badlogic/pi-mono --json title,body,comments,labels,state>gh pr diff 933 --repo badlogic/pi-monoPR: https://github.com/badlogic/pi-mono/pull/933 Changelog:
Fixed session recovery when loading empty or corrupted session files ([#933](https://github.com/badlogic/pi-mono/pull/933) by [@armanddp](https://github.com/armanddp)) before merge.Good:
Bad:
Ugly:
Questions or Assumptions:
Change summary:
Tests:
what's the correct solution to this?
The recovery should ensure the existing file is rewritten so the first line is a valid session header. Otherwise the file stays corrupted and loadEntriesFromFile keeps returning [].
A correct solution is:
This makes the recovery durable and ensures subsequent loads see a valid header.
that's a terrible solution. if someone accidentially passes a wrong file to --session that file gets blown away
Understood. A safer approach is to avoid touching the existing file and instead create a new session file when the header is invalid. That preserves accidental input files. This means:
If you want a different behavior, tell me the policy you want for invalid files.
ok, i think we should:
pi --session test.jsonl doesn't immediately create the session file i think or would it? check
>rg -n "--session" packages/coding-agent/src>rg -n "session" packages/coding-agent/src/cli>rg -n "sessionDir|session" packages/coding-agent/src/main.ts packages/coding-agent/src/modes -g"*.ts">rg -n "createSessionManager" packages/coding-agent/srcpi --session test.jsonl does not create the file immediately.
Flow:
SessionManager.open() calls setSessionFile().newSession() runs and sessionFile is set to the explicit path, but nothing is written._persist() after an assistant message exists. _persist() skips until there is at least one assistant message, then appends the header and entries.So with the current code, the session file appears only after the first assistant response. If we implement the “truncate and add header” behavior for existing invalid files, that would be the only case where the file is rewritten immediately.
valid solution you think?
Yes, that policy is consistent and solves the corruption case. It still avoids creating new files when --session points to a nonexistent path, and only rewrites when the path exists but is invalid. It does mean --session can overwrite a non session file that already exists. That is the tradeoff you specified.
If you want me to implement, say the word.
implement
continue
I see the PR's proposed fix. The issue is that it calls newSession() but doesn't rewrite the file. Let me implement the correct solution: if file exists but has no valid session, truncate and write a fresh header.
The PR diff shows the proposed code, but the current main branch doesn't have it yet. Let me check the current state of setSessionFile on main:
Good, this is the current main branch code without the PR changes. Let me implement the correct fix:
Now let me update the test file to add a test that verifies the file is actually rewritten with a valid header:
Now let me run the tests to verify the implementation:
>cd /Users/badlogic/workspaces/pi-mono && npm run check 2>&1>cd /Users/badlogic/workspaces/pi-mono && npm test -- packages/coding-agent/test/session-manager/file-operations.test.ts 2>&1The tests passed for tui package but coding-agent tests weren't found. Let me run them with the correct path:
>cd /Users/badlogic/workspaces/pi-mono/packages/coding-agent && npx vitest --run test/session-manager/file-operations.test.ts 2>&1All tests pass. Now let me add the changelog entry:
>cd /Users/badlogic/workspaces/pi-mono && npm run check 2>&1Implementation complete. Summary of changes:
packages/coding-agent/src/core/session-manager.ts:
setSessionFile(), when an existing file has no valid session entries (empty or corrupted), the file is now truncated and rewritten with a fresh session header immediately via _rewriteFile(), and flushed is set to true.packages/coding-agent/test/session-manager/file-operations.test.ts:
SessionManager.setSessionFile with corrupted files test suite with 4 tests:
truncates and rewrites empty file with valid headertruncates and rewrites file without valid headerpreserves explicit session file path when recovering from corrupted filesubsequent loads of recovered file work correctlypackages/coding-agent/CHANGELOG.md:
### Fixed crediting the original reporter.