Skip to content

fix(storage): poison on failed append + O(new) commit id validation - #123

Merged
ajianaz merged 3 commits into
developfrom
fix/storage-poison-perf
Sep 22, 2026
Merged

ajianaz merged 3 commits into
developfrom
fix/storage-poison-perf

Conversation

@ajianaz

@ajianaz ajianaz commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

What (description of the change)

Two storage fixes from the cora full scan, same subsystem:

  1. Poison on failed append (chore: ruleset smoke test #53) — when append_line() fails mid-write (classic ENOSPC partial write), the storage is marked poisoned: commit() rejects every later append with a fatal Corrupt("storage poisoned …; re-open or re-create the session") error. Previously the storage stayed usable and a retry glued its line onto the newline-less fragment left at EOF, bricking the session permanently. The fragment is still discarded by open()'s torn-tail rule, so recovery = drop + re-open/re-create.
  2. O(new) commit id validation (ci: add Build job (release profile) for branch-protection parity #52) — commit() no longer clones the entire by_id index per commit: parent checks use by_id.contains_key() and only the current commit's new ids go into a small local set. Long sessions were O(N²) in total commit cost.

Why (the problem you're solving)

Closes #116. Both findings are from the cora full scan on develop (post-merge hygiene pass). #53 is the same brick family as the torn-header defect fixed in 48141f2 — the last durable-data-integrity hole in the commit path. #52 is the hot-path cost that compounds exactly where tole's value proposition lives (long durable sessions).

Testing (how you verified this works)

  • failed_append_poisons_until_reopen (unit, #[cfg(unix)]): injects a REAL write failure by re-pointing the writer at /dev/full (ENOSPC), then asserts: failing commit errors → retry hits the poison gate → in-memory state untouched → reopen replays cleanly and accepts commits again. (A chmod-based injection was rejected by cora as ineffective — create() already holds a write-enabled fd, so permissions can't fail the write; direct field access is the honest injection point.)
  • intra_commit_parents_and_duplicate_ids_still_validated: intra-commit parent resolution via the local set + cross-commit duplicate-id refusal still enforced after the O(new) rewrite.
  • Full workspace suites green; mobile (--no-default-features) green; cargo clippy --workspace --all-targets -- -D warnings clean; cora review: No issues found.

…116)

- append_line() failure mid-write (classic ENOSPC partial write) now
  POISONS the storage: commit() refuses every later append with a
  fatal reopen-directed error. Previously the storage stayed usable
  and a retry glued its line onto the newline-less fragment left at
  EOF, bricking the session on every subsequent open — the same
  failure family as the torn-header fix in 48141f2.
- commit() id validation is O(new entries): parent checks use
  by_id.contains_key() and only the current commit's ids go into a
  local set — cloning the whole by_id index per commit made long
  sessions O(N^2).
- Tests: /dev/full injection drives the poison contract end-to-end
  (fail -> gate -> untouched memory -> reopen recovery; chmod-based
  injection rejected by cora as ineffective against an already-open
  fd); intra-commit parent + cross-commit duplicate assertions.
Comment thread crates/tole-core/src/storage.rs Fixed
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

}

fn commit(&mut self, c: Commit) -> Result<Vec<Entry>, StorageError> {
// Poison gate (cora full-scan #53): a previous append failed
@ajianaz
ajianaz force-pushed the fix/storage-poison-perf branch from 37dc725 to 6a5768a Compare September 22, 2026 15:21
@ajianaz
ajianaz merged commit d4337d3 into develop Sep 22, 2026
16 checks passed
@ajianaz
ajianaz deleted the fix/storage-poison-perf branch September 22, 2026 15:28
ajianaz added a commit that referenced this pull request Oct 28, 2026
…lines (#439)

Cora (alerts #122/#123): a corrupt index line whose file is not
<id>.jsonl made resume recurse without bound, and the first non-UTF-8
line ended the index read. find() now only returns an entry whose file
is <dir>/<id>.jsonl and resume only looks up a valid id, so the
recursive call always resolves locally; the index is read as raw byte
lines and an unparsable one is skipped.
ajianaz added a commit that referenced this pull request Oct 28, 2026
… dir (#454)

## What

A global session index so sessions can be found and resumed across
projects: `tole sessions --all [--grep <text>]`, and `tole resume <id>`
naming the project a session belongs to.

Closes #439.

## Why

Sessions live per cwd (`.tole/sessions/`); on a multi-project machine
there was no way to find which project a session was in, and resuming
required remembering the cwd.

## Changes

- `tole-cli/src/session_index.rs` (new):
`$CODECORA_HOME/tole/sessions-index.jsonl` (next to the trust store,
rather than XDG, so all tole state shares one root). One line per
session START — id, absolute session file, cwd, face, start time —
written in one append-mode `write`, owner-only (0600); the last line per
id wins, malformed/torn lines are skipped. **No prompt text in the
index:** preview, status and turn count are read from the session file
when listing. Recording is best effort (never fails a session);
`TOLE_NO_SESSION_INDEX=1` disables it.
- Recorded by `run`, `chat` (new sessions), `mission` and the session
host (serve / ACP / MCP).
- `tole sessions --all` — newest first, id, name, pc, turns, start time,
cwd and a sanitized one-line preview; sessions whose file is gone are
hidden. `--grep` (requires `--all`) filters case-insensitively on first
prompt, name or cwd.
- `tole resume <id>`: when the id is not in the current sessions dir but
the index knows it, resume REFUSES and prints the command to run in the
owning project (`cd <project> && tole resume <id>`, with
`--sessions-dir` when non-default). Deliberate deviation from the
issue's "cwd-independent resume" (Cora alert #124): resuming from here
would apply this directory's workspace jail, project config and trust to
another project's session. A missing local sessions dir is now "no local
match" instead of an error. The index fallback only accepts a valid id
whose indexed file is `<dir>/<id>.jsonl`, and the index is read as raw
byte lines (a non-UTF-8 line is skipped) — Cora alerts #122/#123.
- Docs: README, CHANGELOG, `docs/epics.md`, `docs/threat-model.md`.
- Not in this PR: `chat --resume` does not consult the index yet.

## Testing

- Unit: last-line-wins, newest-first, torn/foreign lines skipped,
owner-only mode; rows read from the session file, gone sessions hidden,
preview bounded and one-line, grep on prompt/cwd, `find` ignores gone
files.
- E2E (real binary, isolated `CODECORA_HOME`, mock provider): a session
started in project A is invisible to B's `sessions`, listed by `sessions
--all` with A's cwd and preview, matched by `--grep`, `resume <id>` from
B refused with the exact `cd <A> && tole resume <id>` command and
nothing written, then resumed from A; the index holds no prompt text;
`TOLE_NO_SESSION_INDEX=1` records nothing; `--grep` without `--all` is
refused.
- `cargo fmt`, clippy (workspace all-targets, bare, shell-tools, mcp),
`cargo test --workspace`: green (one run hit the known load-sensitive
`web::fetch_and_search_round_trip_against_local_mock` flake, untouched
here; green on rerun). Cora agent review (prepare + finalize): 0
findings, `should_block: false`.

Note: e2e tests elsewhere that do not isolate `CODECORA_HOME` add their
temp sessions to a developer's real index; `--all` hides them once their
files are deleted.

---------

Co-authored-by: ajianaz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage: poison-on-failed-append + O(N^2) commit id-set (cora scan majors)

2 participants