Skip to content

feat: implement session export, fork, and undo (Issues #91, #98) - #112

Merged
bbondy merged 7 commits into
brave:mainfrom
winshaurya:feat/session-export-fork-undo
Sep 8, 2026
Merged

bbondy merged 7 commits into
brave:mainfrom
winshaurya:feat/session-export-fork-undo

Conversation

@winshaurya

@winshaurya winshaurya commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

What this adds

Three session commands, one per commit.

  • /export [path] writes the recounted transcript as markdown, into the working directory.
    The path is confined the way a workspace write is, an existing path is refused rather than
    replaced, and the file is written 0600 like the record it came from.
  • bravebot --fork <id> copies a record into one with its own id and opens it, so a second
    approach starts from a prefix worth keeping. The audit trail comes with it. A manifest run is
    refused, and an incognito session writes no copy.
  • /undo puts the session back where it stood before the last turn: the paths that turn wrote
    go back to what they held, and the conversation, turn count, spend, timing, trust map, trusted
    programs, transcript and audit lines go back with it.

--continue, the third gap in #98, is already on main.

Follow-up commits on top of the original work

  • Export confinement. Refusing .. says nothing about where a directory inside the tree
    leads. /export escape/notes.md with escape a symlink wrote outside the working directory
    with no traversal component in the path, and /export notes.md where notes.md was a symlink
    to a missing file did too, since exists follows a symlink and reports nothing for a broken
    one. Containment is now tested against the canonical path of the deepest directory that exists,
    and a path already taken is refused on symlink_metadata.
  • A rewind names what it could not put back. Every restore was attempted with its result
    discarded, so a rewind that put nothing back reported the same success as one that put
    everything back. That leaves the transcript describing a tree that is not there, which is the
    disagreement a rewind exists to prevent.
  • A memory bound. Every file a turn wrote had its previous contents read whole into memory
    and held until the turn after it, whether or not anybody rewound. A turn now keeps at most
    MAX_REWIND_BYTES; past it the path is remembered and its contents are not, as a third state
    rather than a second, since recording it as a file that was never there would have a rewind
    delete work it merely could not hold.
  • Stale snapshots. Only a turn was closing the rewind window. /compact, /rename,
    /add-dir and /cd all change the session without starting one, so /undo afterwards undid
    that change as well under a line saying one turn had been rewound.

Also in the rebase: the branch was ~16k lines behind main and did not compile against it, the
three original commits are now one per feature so each builds and carries its own tests, and five
unrelated French strings the branch had rewritten are left as they were.

Specs

SESSION-17, SESSION-18 and SESSION-19 in sessions.md, with two
known costs on what a rewind does not cover: changes a program made rather than a file tool, and
edits the user made themselves between the turn and the rewind.

Closes #98
Closes #91

The rest of #91, the part this does not do, is #116: more than one step back, a list to choose
from, and checkpoints that survive a resume.

@winshaurya

Copy link
Copy Markdown
Contributor Author

applying Brian's warning from #86 that session data contains private code and secrets, we enforced restrictive 0600 file and 0700 directory permissions on exported transcripts, forked session records, and turn backup snapshots so that exported files and session copies never inherit world-readable permissions.

winshaurya added a commit to winshaurya/brave-bot that referenced this pull request Sep 7, 2026
winshaurya and others added 7 commits September 7, 2026 22:40
`/export` writes the recounted conversation to a markdown file under the working directory,
named on the line or defaulting to the session id. The path is confined the way a workspace
write is, an existing file is left alone rather than replaced, and the file is written 0600
like the session record it came from.

The transcript belongs to the person who had the conversation, and until now the only way to
get one out was to read the record's JSON out of the state directory by hand.

Part of brave#98
`bravebot --fork <id>` copies a record into one with its own id and opens it, so a second
approach starts from the prefix that was worth keeping instead of from nothing. The audit trail
is copied with it, since the gates that decided the shared prefix decided the fork's history too.

A manifest run is refused, for the reason SESSION-10 refuses to resume one. An incognito session
writes no copy, so forking in one opens the conversation and records nothing.

Closes brave#98
`/undo` puts the session back where it stood before the most recent turn: the paths that turn
wrote go back to what they held, the conversation returns to its pre-turn snapshot, and the turn
count, spend, timing, trust map, trusted programs and audit lines go back with it. A rewind past
the first turn removes the record rather than leaving an empty one.

The workspace keeps what a path held before the turn's first write to it, which is the only
moment that is knowable. Nothing reads those bytes: they are carried back to the path they came
from.

One turn is as far as it goes. `/clear` and a shell-mode command close the window, the latter
because the workspace never saw what the command changed.

Part of brave#91
Refusing `..`, a root and a drive prefix says nothing about where a directory inside the tree
leads. `/export escape/notes.md`, where `escape` is a symlink, wrote outside the working
directory with no traversal component anywhere in the path; so did `/export notes.md` where
`notes.md` was a symlink whose target did not exist, since `exists` follows a symlink and reports
nothing for a broken one, so the path read as free and the write followed it out.

Containment is now tested the way the workspace tests it, against the canonical path of the
deepest directory that exists, and a path already taken is refused on `symlink_metadata` rather
than on `exists`.
Every restore was attempted with its result discarded, so a rewind that put nothing back said
"rewound the session by one turn" exactly as one that put everything back did. The case that
matters is the one where a file still holds what the turn wrote while the conversation says it
never happened, which is the disagreement between disk and transcript a rewind exists to prevent.

`restore_backups` now returns the paths that would not go back, and the line reporting the rewind
names them. A file the turn created and something else has since deleted is already in the state
asked for, so it is not among them.
Every file a turn wrote had its previous contents read whole into memory and held until the turn
after it, whether or not anybody rewound. One write of a large file was therefore a large
allocation on a code path every turn takes, and nothing bounded how many of them a turn could
make.

A turn now keeps at most MAX_REWIND_BYTES. Past it the path is still remembered and what it held
is not, which is a third state rather than a second: recording it as a file that was never there
would have a rewind delete work it merely could not hold. `/undo` names such a path among the
ones that did not go back.
A snapshot describes the session as it stood before the last turn, and only a turn was closing
the window on it. `/compact`, `/rename`, `/add-dir` and `/cd` all change the session without
starting one, so `/undo` afterwards rewound that change as well, under a line saying one turn had
been rewound: a compacted conversation came back whole, a renamed session took its old name back,
and a directory stayed open with the rule that made it reachable gone from the map.

Each of those closes the window now, alongside `/clear` and a shell-mode command, which already
did.
@bbondy
bbondy force-pushed the feat/session-export-fork-undo branch from f894bd4 to ebde4e6 Compare September 8, 2026 02:53

@bbondy bbondy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice work. Rebased on main, split one commit per feature, and pushed four follow-ups: export confinement past symlinks, a rewind that names what it could not put back, a memory bound on what a turn keeps, and closing the rewind window on /compact, /rename, /add-dir and /cd. Specs and tests included. Remainder of #91 is #116.

@bbondy
bbondy merged commit 84643bb into brave:main Sep 8, 2026
6 of 8 checks passed
@winshaurya
winshaurya deleted the feat/session-export-fork-undo branch October 10, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants