1#![warn(clippy::print_stdout, clippy::print_stderr)]
25
26#[cfg(all(
32 windows,
33 not(all(
34 target_vendor = "pc",
35 target_env = "msvc",
36 any(target_arch = "x86_64", target_arch = "aarch64")
37 ))
38))]
39compile_error!(
40 "Pitboard for Windows is built for x86_64-pc-windows-msvc and aarch64-pc-windows-msvc \
41 alone, the targets it is built and tested on."
42);
43
44#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
45compile_error!(
46 "Pitboard runs on macOS, Linux and Windows. Another system needs a host of its own in \
47 `host/`, saying where its stores, processes and scheduler are."
48);
49
50pub(crate) mod api;
51pub mod app;
52pub mod assumptions;
53pub mod audit;
54pub mod context;
55#[cfg(any(test, feature = "test-support"))]
57#[doc(hidden)]
58#[allow(
59 clippy::disallowed_methods,
60 reason = "it leaves Claude Desktop's files as Claude does"
61)]
62pub mod desktop_testing;
63pub mod doctor;
64pub mod duty;
65pub mod error;
66pub(crate) mod in_use;
67pub mod label;
68pub mod provider;
69pub mod redact;
70pub mod release;
71pub mod schedule;
72#[cfg(all(target_os = "linux", any(test, feature = "test-support")))]
75#[doc(hidden)]
76#[allow(
77 clippy::disallowed_methods,
78 reason = "it writes the bus's config and the daemons' logs in a test's own folder"
79)]
80pub mod secret_service_testing;
81pub mod service;
82pub(crate) mod settings;
83pub mod state;
84pub mod status;
85pub mod statusline;
86pub mod switch;
87pub mod time;
88pub mod usage;
89pub(crate) mod warning;
90pub mod watch_service;
91pub mod words;
92
93pub(crate) mod atomic;
94pub(crate) mod fault;
95#[cfg(test)]
96mod helper;
97pub mod holder;
98pub(crate) mod home;
99pub mod host;
100pub(crate) mod lock;
101pub(crate) mod park;
102pub(crate) mod pending;
103pub(crate) mod permit;
104pub(crate) mod proxy;
105pub(crate) mod sessions;
106#[cfg(any(test, feature = "test-support"))]
109#[doc(hidden)]
110#[allow(
111 clippy::print_stdout,
112 clippy::print_stderr,
113 reason = "it prints what the program it plays would"
114)]
115pub mod stand_in;
116pub(crate) mod store;
117
118#[cfg(feature = "test-support")]
121#[doc(hidden)]
122pub mod testing {
123 pub use crate::api::scripted::{Answer, Asked, ScriptedApi, Trouble};
124 pub use crate::context::ScratchMachine;
125 pub use crate::desktop_testing as desktop;
126 pub use crate::doctor::example::{
127 healthy_linux as doctor_on_a_healthy_linux, healthy_mac as doctor_on_a_healthy_mac,
128 healthy_pc as doctor_on_a_healthy_pc,
129 };
130 pub use crate::duty::warm::run::{Ran, Started, WarmScript};
131 pub use crate::host::fs::testing as fs;
135 pub use crate::host::memory::MemoryHost;
136 pub use crate::provider::claude::api::Renewed;
137 pub use crate::provider::claude::paths::live_service;
138 pub use crate::provider::claude::slot::{LIVE_SERVICE, dir_hash, service_for_dir};
139 pub use crate::provider::jwt::unsigned as unsigned_token;
140 #[cfg(target_os = "linux")]
141 pub use crate::secret_service_testing::SecretService;
142 pub use crate::stand_in;
145 pub use crate::store::memory::{Fault, MemoryStore};
146 pub use crate::store::vault_read;
147 pub use crate::switch::enroll::{ScriptedSignIn, SignInScript};
148 pub use crate::time::{Clock, FixedClock};
149
150 pub fn variables() -> impl Iterator<Item = &'static str> {
153 crate::context::variables()
154 }
155
156 pub fn codex_login(
157 account: &str,
158 email: &str,
159 access: &str,
160 refresh: &str,
161 ) -> serde_json::Value {
162 crate::provider::codex::login_for_a_test(account, email, access, refresh)
163 }
164
165 pub fn holders(which: crate::provider::ProviderId) -> &'static [crate::holder::Holder] {
168 use crate::provider::{Adoption, LoginKind};
169 match crate::provider::of(which) {
170 LoginKind::Document(tool) => match tool.adoption(None) {
171 Adoption::RestartRequired { holders, .. } => holders,
172 _ => &[],
173 },
174 LoginKind::Folder(app) => std::slice::from_ref(app.holder()),
175 }
176 }
177
178 pub fn park_unread(label: &str, path: &std::path::Path, why: &str) -> crate::error::Error {
179 crate::store::folder::unread(label, path, why)
180 }
181
182 pub fn lies_in(path: &std::path::Path, place: &std::path::Path) -> bool {
184 crate::host::lies_in(path, place)
185 }
186
187 pub fn real_login_places(
188 started_in: &crate::context::Context,
189 ) -> Result<Vec<std::path::PathBuf>, String> {
190 crate::host::real_login_places(Some(started_in))
191 }
192
193 pub fn vault_write(
196 ctx: &crate::context::Context,
197 service: &str,
198 contents: &str,
199 ) -> crate::error::Result<()> {
200 let permit = crate::permit::gate(ctx)?;
201 Ok(crate::store::vault_write(ctx, permit, service, contents)?)
202 }
203
204 pub fn may_be_running(pid: u32) -> bool {
205 crate::host::proc::may_be_running(pid)
206 }
207
208 #[cfg(windows)]
210 pub fn processes() -> std::io::Result<Vec<(u32, u32, String)>> {
211 crate::host::proc::snapshot()
212 }
213
214 #[cfg(windows)]
216 pub mod version_resource {
217 use std::path::Path;
218
219 pub fn file_version(program: &Path) -> Option<[u16; 4]> {
220 crate::host::proc::file_version(program)
221 }
222
223 pub fn product_version(program: &Path) -> Option<[u16; 4]> {
224 crate::host::proc::product_version(program)
225 }
226
227 pub fn string(program: &Path, name: &str) -> Option<String> {
228 crate::host::proc::version_string(program, name)
229 }
230 }
231
232 #[cfg(windows)]
235 pub mod credman {
236 use crate::context::Context;
237 use crate::store::RawStore;
238
239 pub struct Item {
240 pub kind: u32,
241 pub persist: u32,
242 pub user: String,
243 pub comment: Option<String>,
244 pub attributes: u32,
245 pub bytes: usize,
246 }
247
248 fn refused(target: &str) -> std::io::Result<()> {
249 match crate::provider::names::refusal(target, &[]) {
250 Some(why) => Err(std::io::Error::other(why)),
251 None => Ok(()),
252 }
253 }
254
255 pub fn item(target: &str) -> std::io::Result<Option<Item>> {
256 refused(target)?;
257 Ok(
258 crate::host::windows_credman::item(target)?.map(|held| Item {
259 kind: held.kind,
260 persist: held.persist,
261 user: held.user,
262 comment: held.comment,
263 attributes: held.attributes,
264 bytes: held.blob.len(),
265 }),
266 )
267 }
268
269 pub fn write_as_bun(target: &str, user: &str, value: &str) -> std::io::Result<()> {
271 refused(target)?;
272 crate::host::windows_credman::write(target, user, value.as_bytes())
273 }
274
275 fn store(ctx: &Context) -> crate::provider::claude::credman::Store {
276 crate::provider::claude::credman::Store::of(ctx)
277 }
278
279 pub fn write(ctx: &Context, service: &str, contents: &str) -> crate::error::Result<()> {
280 let permit = crate::permit::gate(ctx)?;
281 Ok(store(ctx).write(permit, service, contents)?)
282 }
283
284 pub fn read(ctx: &Context, service: &str) -> crate::error::Result<Option<String>> {
285 Ok(store(ctx).read(service)?)
286 }
287
288 pub fn discard(ctx: &Context, service: &str) -> crate::error::Result<()> {
290 let permit = crate::permit::gate(ctx)?;
291 Ok(store(ctx).delete(permit, service)?)
292 }
293
294 pub fn sweep(ctx: &Context, service: &str) -> Result<(), String> {
296 crate::provider::claude::credman::swept(ctx, service)
297 }
298 }
299
300 pub fn vault_delete(ctx: &crate::context::Context, service: &str) -> crate::error::Result<()> {
302 let permit = crate::permit::gate(ctx)?;
303 Ok(crate::store::vault_delete(ctx, permit, service)?)
304 }
305
306 pub fn signed_in(
311 ctx: &crate::context::Context,
312 which: crate::provider::ProviderId,
313 document: &str,
314 ) -> crate::error::Result<crate::switch::SignIn> {
315 use crate::provider::LoginKind;
316 let document = serde_json::from_str(document).expect("a login a test wrote as JSON");
317 match crate::provider::of(which) {
318 LoginKind::Document(tool) => {
319 crate::switch::planted(ctx, crate::permit::gate(ctx)?, tool, document)
320 }
321 LoginKind::Folder(_) => Err(crate::error::Error::Usage(
322 "a Claude Desktop sign-in is played by testing::desktop".into(),
323 )),
324 }
325 }
326}