1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Storage-path derivation for the workstream store (DOC-48 §3.1).
//!
//! Why: the spec pins the filename shape (`workstreams-{project_slug}-{hash}.json`)
//! to the daemon's OWN [`ProjectBinding`] — never a caller-supplied label —
//! so two daemons bound to the same project always agree on one file, and
//! two different projects (even same-named checkouts) never collide.
//! What: [`store_filename`] derives the filesystem-safe slug + a truncated
//! SHA-256 fingerprint of the canonical root from a [`ProjectBinding`];
//! [`store_path`] joins it under a data directory; [`default_data_dir`]
//! resolves `~/.trusty-code` (mirrors trusty-mpm's `~/.trusty-mpm`
//! precedent, `crates/trusty-mpm/src/core/paths.rs`).
//! Test: `path_tests`.
use ;
use ;
use crateProjectBinding;
/// Number of hex characters kept from the SHA-256 digest (§3.1: "truncated
/// to 8 chars").
const HASH_LEN: usize = 8;
/// Fixed filename used when the daemon is projectless (`ProjectBinding::None`).
///
/// Why: DOC-48 §2.3/§3.1 only specify the filename shape for a BOUND
/// project; a projectless daemon has no path to slug or hash. Rather than
/// fabricate a fake root, this ticket resolves the ambiguity with one fixed,
/// documented filename — a projectless daemon has at most one workstream
/// store regardless of which directory `tcode serve` happened to start in.
const PROJECTLESS_FILENAME: &str = "workstreams-projectless.json";
/// Resolve `~/.trusty-code`, creating it at mode `0700` and tightening it if a
/// previous run left it permissive.
///
/// Why: mirrors `trusty-mpm`'s `~/.trusty-mpm` precedent
/// (`crates/trusty-mpm/src/core/paths.rs`) rather than inventing a new
/// resolution convention. #5426 moved the resolution itself into
/// [`crate::paths::private_state`], which also owns the `0700` mode this
/// directory must carry — a workstream store sitting beside transcripts and
/// logs is private state, not an independent location. #6999: this function is
/// the ONE place every private-state writer resolves that root — the workstream
/// store, `serve`'s router, and `agent_loop::telemetry`'s own `default_data_dir`
/// all end here — so the mode guarantee is applied here rather than at each
/// writer's own `create_dir_all`, which would apply the process umask and leave
/// `0755`. Applying it anywhere else would be a second copy of the rule.
/// What: delegates to [`crate::paths::private_state::ensure_private_state_dir`]:
/// `dirs::home_dir()/.trusty-code`, created if missing and chmod'd to `0700` if
/// any group or other bit is set. On an I/O failure it falls back to the plain
/// [`crate::paths::private_state::private_state_dir`] path, logging at `warn` —
/// a harness that cannot create its state directory should still report what it
/// tried, not panic. Never panics.
/// Test: `path_tests::default_data_dir_is_dot_trusty_code`,
/// `path_tests::ensure_or_report_creates_and_tightens_a_permissive_dir`,
/// `tests/cli_e2e.rs::a_non_import_command_tightens_a_permissive_private_state_dir`,
/// `paths::private_state::private_state_tests::private_state_dir_matches_home_when_available`.
/// Create and tighten an already-resolved private-state root, or report why not.
///
/// Why: the hermetic core of [`default_data_dir`] (#6999), mirroring
/// [`crate::paths::private_state::private_state_dir_at`]'s reason for existing —
/// a test drives it with a temp directory, so no test creates, chmods, or
/// reports on the developer's real `~/.trusty-code`.
/// What: [`crate::paths::private_state::ensure_dir`], returning `dir` either
/// way. On failure — the path is a regular file, the home directory is
/// read-only, the filesystem is full — it logs at `warn` with the path and the
/// error and hands back the same path, so a harness that cannot create its state
/// directory still runs and still says what it tried. Never panics.
/// Test: `path_tests::ensure_or_report_creates_and_tightens_a_permissive_dir`,
/// `path_tests::ensure_or_report_falls_back_when_the_path_is_a_file`,
/// `tests/cli_e2e.rs::an_unusable_private_state_path_warns_instead_of_panicking`.
pub
/// Turn an arbitrary string into a filesystem-safe, lowercase, hyphenated
/// slug.
///
/// Why: a project's directory basename may contain characters unsafe for a
/// filename component (spaces, underscores, mixed case); the slug must be
/// stable and readable.
/// What: keeps ASCII alphanumerics (lowercased), collapses any run of other
/// characters into a single `-`, and trims leading/trailing `-`. An
/// all-non-alphanumeric input slugs to `"project"` rather than an empty
/// string.
/// Test: `path_tests::slugify_*`.
/// Deterministic 8-hex-char fingerprint of a project root path.
///
/// Why: disambiguates multiple checkouts that share a directory basename
/// (§3.1: "to handle multiple checkouts of the same project").
/// What: SHA-256 of the path's platform string, truncated to
/// [`HASH_LEN`] lowercase hex characters. Deterministic for a given path,
/// unlike a per-process `RandomState`-seeded hasher.
/// Test: `path_tests::project_hash_is_deterministic`,
/// `path_tests::project_hash_differs_for_different_paths`.
/// Derive the workstream store's filename from a daemon's project binding
/// (DOC-48 §3.1).
///
/// Why: the ONE place the filename shape is assembled, so the store and any
/// future inspection tooling can never disagree on it.
/// What: for a bound project, `workstreams-{slug}-{hash}.json` where `slug`
/// comes from the root's basename and `hash` fingerprints the full root
/// path (§3.1). For [`ProjectBinding::None`], the fixed
/// [`PROJECTLESS_FILENAME`] (see its docs for why).
/// Test: `path_tests::store_filename_for_bound_project`,
/// `path_tests::store_filename_for_projectless`.
/// Join [`store_filename`] under a data directory to get the full store path.
///
/// Why: the single entry point `WorkstreamStore::load_for_binding` (and any
/// future daemon boot code) calls to resolve where the store file lives.
/// What: `data_dir.join(store_filename(binding))`.
/// Test: `path_tests::store_path_joins_data_dir_and_filename`.