pi-mono/packages/coding-agent/test/session-manager
robopipi[bot] 807a5c051f
fix(coding-agent): keep session memory in line with the file when a write fails (#11486)
When writing a session entry failed (ENOSPC, EBUSY, ETIMEDOUT on network
drives), `SessionManager._appendEntry` had already added the entry to
memory and made it the current leaf. The process kept running, and the
next entry written to disk pointed to a parent that was not in the file.
On resume, pi follows the history only back to that gap: everything
before it disappears. In #11460 that also dropped the first system
message, which declares the tools, so resume turned off every built-in
tool.

`_appendEntry` now undoes the in-memory append if writing fails, then
rethrows. The error reaches the caller as before; later entries chain to
the last entry actually on disk.

Reports of this problem:
- #11460: ENOSPC; resume dropped all built-in tools.
- #10166: failure injected with EBUSY; the next entry on disk points to
a missing parent.
- #10661: ETIMEDOUT on Windows + NFS; resumed sessions showed only the
last few messages. That report also describes NUL holes in the file,
which this change does not address.

**Testing**
- Two tests in `test/session-manager/file-operations.test.ts` make
writes fail with real file permissions: a read-only session file (failed
append) and a read-only session directory (failed first file creation).
They check that the entry is not kept, the leaf is unchanged, the next
entry chains to the last written one, and the reopened session's branch
contains both entries. Both fail without the change and pass with it.
- `test/session-manager/` and `test/default-tools-setting.test.ts` pass
(140 tests).
- `npm run check` passes.

Fixes #11460

Requested by @davidbrai

Co-authored-by: triagent <triagent@localhost>
2026-10-27 15:58:08 +01:00
..
build-context.test.ts fix(coding-agent): add entry renderers for session entries 2026-07-01 11:30:26 +02:00
custom-session-id.test.ts fix(coding-agent): test forkFrom copied content instead of fs calls 2026-10-23 21:24:38 +02:00
file-operations.test.ts fix(coding-agent): keep session memory in line with the file when a write fails (#11486) 2026-10-27 15:58:08 +01:00
labels.test.ts fix(coding-agent): rechain fork paths without labels 2026-06-12 18:50:31 +02:00
load-entries.test.ts feat(coding-agent): ingest external entries in in-memory sessions (#8980) 2026-09-03 10:49:41 +02:00
migration.test.ts chore: pin dependencies and use native TypeScript 2026-05-20 12:46:17 +02:00
save-entry.test.ts chore: pin dependencies and use native TypeScript 2026-05-20 12:46:17 +02:00
tree-traversal.test.ts fix(coding-agent): save new session file at the first user message 2026-09-25 16:27:51 +02:00