Repository navigation
the state directory belongs to the user - #134
Conversation
74f5b6d to
bc05d6e
Compare
~/.bravebot landed at the process umask: drwxr-xr-x, with history at -rw-r--r--. That file is every prompt anybody has typed, meaning the paths they were working on, the branch names, and whatever they pasted into one. On a shared build host every local account could read it. The modes were also a matter of call order. The credential store wrote 0600 and the session store 0700, each creating the parent with a bare create_dir_all, so ~/.bravebot ended up 0700 or 0755 depending on which subsystem happened to write first. One helper creates a directory under it 0700 and one writes a file into it 0600, and every writer that can reach them goes through them: the history, the model, theme, effort and editing choices, the update answer, and the session records that had their own copies. The crate that imports a subscription depends on nothing, so it keeps its own copy of the modes rather than gaining a dependency for four lines. Both narrow what is already there. Creating a directory that exists does not touch its mode, so without that this would reach no machine that has run an older build, which is every machine holding the history worth protecting. Narrowing stops at the state directory: what else is in somebody's home is not this program's business. state-directory.md is the spec #86 asked for and #111 stopped short of, covering the directory rather than one file at a time. Closes #114
bc05d6e to
c4d07e7
Compare
…side it cache_for took a home and appended `.bravebot` itself, while every caller passes home::directory(), which is that directory already. The index for a workspace landed in ~/.bravebot/.bravebot/lsp/<hash>: a directory nothing else writes to, reads, or narrows, holding an index derived from every file in the tree. The argument is the state directory now, and the name it is joined with is a constant, so the doubling cannot come back by way of a caller reading the parameter name differently. The tests passed their own scratch home, so they agreed with the function rather than with the host, which is why the path survived four clauses' worth of coverage. An index under the old path is orphaned rather than moved. Nothing reads it, and the server rebuilds one on the first question it is asked.
The cache directory was created with create_dir_all, so it landed at the process umask: drwxr-xr-x for a directory holding an index derived from every file in the workspace. Who may read that is who may read the source it was built from. It is also created recursively, so on a machine where nothing has written to ~/.bravebot yet, this is what creates the state directory, at the umask, for the prompt history that arrives later. That is the call order STATE-1 exists to settle. Narrowed as well as created, since a directory keeps the mode it was made with. Two levels and no further: the state directory above them belongs to whichever subsystem created it, and this crate has no business setting it. The modes are spelled out here rather than shared, because this crate depends on the kernel alone and reaching the helper would invert that.
The mode on the open is asked for as the file is created and says nothing about one already on disk, which a person may have restored from a backup or copied between machines. The directory around it was narrowed and the file inside it was not, so the one holding a bearer token was the one left as it was found. Narrowed between the open and the write, so the file is private while it is empty rather than from the moment it holds a token again.
…e user The installer creates ~/.bravebot to record which path it wrote the binary to, and it did so with a bare mkdir -p at the umask. On a fresh machine that is what creates the directory, so it was drwxr-xr-x before the program had run once, and the prompt history arrived into a directory an install had already opened. The record is removed and written again under umask 077 rather than written over, so its mode is the one it is created with rather than one a chmod catches up with. A directory already there is narrowed, which is the case on every machine that has run an earlier install.
The clause claimed every subsystem resolves ~/.bravebot through the same answer, and argued from the cost of having two. There are four: the agent's own, the credential store's, the settings layer's and the language server client's. Three of those crates sit below the one holding the answer, so layering forbids them the dependency and one definition is not available to be had. Stated as the rule the tests actually pin: the name, and no fallback where HOME says nothing. What has to hold across independent resolvers is that much, and each is pinned on it, so the clause is now one a reader can hold a diff against.
Three roots were in use for a test that redirects HOME: target/test-scratch, which testutil documents and five crates provide, the system temporary directory, and target/scratch. The last is the one the state directory tests introduced, and the reason testutil exists is written in its doc comment, so a second spelling of the same idea is the thing it was meant to prevent. The agent's own tests reach the helper directly, since it is a unit test in the crate that has one. The integration test cannot, so it keeps its local copy and spells the same root, and gains the Drop its siblings have: without one, a run left a scratch home behind holding a fake prompt history.
|
Reviewed and pushed the fixes onto this branch as six commits, rather than splitting any of them STATE-1 claimed more than the code delivered. The clause says a directory under
Both new writers are in STATE-1's STATE-2's first sentence was false. It claimed every subsystem resolves A path bug the spec work turned up, fixed here rather than filed. Test hygiene, all in The description was stale and is updated: seventeen tests rather than eleven, and five of six Verified on the branch head: |
Moving the index to the right path leaves the old one where it was. That path is `~/.bravebot/.bravebot/lsp/`, which nothing reads, rebuilds or narrows, holding an index derived from every file in the workspace at whatever the umask gave it. On a machine that has run an earlier build it stays there for as long as the machine does, which is the case this branch exists to close. Removed rather than narrowed: nothing will ever read it, so a private copy of it is worth no more than none, and narrowing would mean walking a tree to reach the files inside. The path is derived rather than spelled. Appending the state directory's own name is what put it there, so joining that name is what finds it, and the state directory keeps one definition. Only a directory holding what this crate would have written is removed, so one that happens to carry the same name and holds something else is left where it is.
|
[puLL-Merge] - brave/bravebot@134 DescriptionCentralizes state-directory file/dir creation in Possible Issues
|
`Servers::new` takes `~/.bravebot` itself, so passing a bare `$HOME` puts the index in the home directory rather than under the state directory: on a machine with rust-analyzer installed the test sets `CARGO_TARGET_DIR` to `$HOME/lsp/<hash>` and cargo fills it with a build tree next to the user's own files.
…ot make The index directory was created for its effect and the result thrown away, so a server handed one that could not be made created it itself, at the umask, and filled it with an index derived from every file in the workspace. Go's `GOCACHE` sat one level deeper than anything created here and was made by the server every time. Every directory a server is pointed at is now created before it starts, and a failure to create one stops the launch and says so. Dropping the variable instead would write the index into the workspace, which is what LSP-10 forbids.
…as given `set_permissions` resolves a link, and the bound on how far narrowing walks is a comparison of paths, which says where a name sits and nothing about where it leads. Somebody who keeps their sessions on a synced volume and links the directory into place had the target set to 0700, which is a directory outside anything this was given. Both copies of the rule step over a link rather than following it: the helper under the state directory, and the language server client's own.
`mkdir -p` at the umask creates the directory 0755 on a fresh machine and the chmod a syscall later closes it, which is the ordering the comment beside it says the record avoids. Asking for the mode as the directory is made leaves no window; the chmod stays, since that is what narrows a directory an earlier install left.
…he umask Two problems in the same tests. The scratch directory was removed on the last line of the test body, so a failing assertion left a tree of directories with modes on it and the next run measured what the failure left. And the mode asserted on the state directory came from `create_dir_all` at the process umask, so `cargo test` failed under any umask but 022. A guard removes the directory however the test ends, and the mode the claim is about is set by the test rather than inherited from whoever runs it.
…ames `crates/tui/src/sessions.rs` is the largest writer into the state directory and one of STATE-1's tests exists to pin it, but a diff touching the file was not reviewed against the spec. `crates/config/src/settings.rs` resolves the directory the way STATE-2 describes and nothing pinned it, so a fallback added there would have been caught by nothing. Both are now governed, and the settings resolver has the tests the other two have.
Two claims a reviewer could hold a diff against and find false. STATE-1 read as though every file in the directory were narrowed, and a file this program only reads is not: `settings.json` sits at 0644 on a machine whose umask put it there, and nothing here ever writes it. That is the directory mode's job, and it is a known cost rather than a clause. The file helper is also used for one write outside the directory, the export SESSION-17 governs, which a clause titled "everything written into it" has to account for.
A test's doc comment described the tightening an earlier implementation had, in the past tense, so a reader who has only seen this code cannot check it against anything. The scratch-directory helper this branch adds opens the same way. Both say the reason the rule holds instead.
Three tests set the variable to check what `home` makes of it, which takes a lock against every other test in this binary, a restore on the way out, and four unsafe blocks to do it. The security scan flags each of those, and rightly: an environment variable is process-wide, and setting one while other tests run is what the unsafety is about. The rule is a function of one string, so it is split from the read and checked directly. The tests keep their names and what they assert, the lock and the restore go, and they no longer depend on the order the binary runs them in.
|
commits lgtm |
Closes #114
~/.bravebotlanded at the process umask:drwxr-xr-x, withhistoryat-rw-r--r--. That fileis every prompt anybody has typed, meaning the paths they were working on, the branch names, and
whatever they pasted into one. On a shared build host every local account could read it.
The modes were also a matter of call order. The credential store wrote 0600 and the session store
0700, each creating the parent with a bare
create_dir_all, so~/.bravebotended up 0700 or 0755depending on which subsystem happened to write first.
One helper creates a directory under it 0700 and one writes a file into it 0600, and every writer
that can reach them goes through them: the history, the model, theme, effort and editing choices,
the update answer, and the session records that had their own copies. The crates that sit below the
one holding the helper keep their own copy of the modes rather than inverting a dependency for four
lines: the one that imports a subscription, and the language server client.
Both narrow what is already there. Creating a directory that exists does not touch its mode, so
without that this would reach no machine that has run an older build, which is every machine
holding the history worth protecting. Narrowing stops at the state directory: what else is in
somebody's home is not this program's business.
The writers that were outside it
Three subsystems write into
~/.bravebotwithout going through the helper, and each has to ask forthe modes itself.
create_dir_all, so a directory holding an index derivedfrom every file in the workspace landed at the umask. Because it is recursive, on a machine where
nothing had written to
~/.bravebotyet this is what created the state directory, at the umask,for the history that arrived later.
mkdir -pat the umask, before the program has run once, so a fresh installopened the directory the history would land in. The record it writes there is created under
umask 077now rather than written over.the one holding a bearer token kept whatever mode it was found with.
~/.bravebot/themesis named in the issue but nothing outside tests creates it, so there wasnothing to change there.
crates/tui/src/update.rsgained a writer after the issue was filed, andthat one was landing at the umask.
A path bug this turned up
cache_fortook a home and appended.bravebotitself, while every caller passeshome::directory(), which is that directory already. The index for a workspace was landing in~/.bravebot/.bravebot/lsp/<hash>: a directory nothing else writes to, reads or narrows. Its fourclauses' worth of tests passed their own scratch home, so they agreed with the function rather than
with the host. The argument is the state directory now, the joined name is a constant, and a test
pins that nothing sits between them. An index under the old path is orphaned rather than moved;
nothing reads it and the server rebuilds one on the first question.
Checked
Driven through
contrib/drive_tui.pyagainst a scratchHOMEwith umask 022, a real session typedinto a real terminal. Before, on the parent commit:
After:
The installer fragment run against a scratch
HOMEseeded 0755/0644 under umask 022 leavesdrwx------ .bravebotand-rw------- installed-by.Seventeen tests, writing real files and reading real modes back: sixteen pin STATE-1 and one pins
the cache path under LSP-10. With the source reverted and the tests kept, five of the six
integration tests fail; the sixth asserts that nothing above the state directory is touched, and
passes either way by design. Each of the other writers' tests fails against its own unfixed code.
Spec
New
docs/specs/state-directory.md, idSTATE. STATE-1 is the rule this change establishes, andgovernsnames every writer it covers, the installer included. STATE-2 states where the directoryis and that a missing
HOMEyields no directory rather than a guessed one: existing behaviour withexisting tests, included so the file stands alone rather than being a spec about modes with no
subject.
STATE-2 says what holds across resolvers rather than claiming there is one. There are three:
crates/agent/src/home.rs,crates/skus/src/store.rsandcrates/config/src/settings.rs. Two ofthose crates sit below the one holding the answer, so layering forbids them the dependency and one
definition is not available to be had. What has to hold across them is the name and the refusal to
guess, and each is pinned on that. The language server client resolves nothing: it is handed the
directory.