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 quiet;
106pub(crate) mod sessions;
107#[cfg(any(test, feature = "test-support"))]
110#[doc(hidden)]
111#[allow(
112 clippy::print_stdout,
113 clippy::print_stderr,
114 reason = "it prints what the program it plays would"
115)]
116pub mod stand_in;
117pub(crate) mod store;
118
119#[cfg(feature = "test-support")]
122#[doc(hidden)]
123pub mod testing {
124 pub use crate::api::scripted::{Answer, Asked, ScriptedApi, Trouble};
125 pub use crate::context::ScratchMachine;
126 pub use crate::desktop_testing as desktop;
127 pub use crate::doctor::example::{
128 healthy_linux as doctor_on_a_healthy_linux, healthy_mac as doctor_on_a_healthy_mac,
129 healthy_pc as doctor_on_a_healthy_pc,
130 };
131 pub use crate::duty::warm::run::{Ran, Started, WarmScript};
132 pub use crate::host::fs::testing as fs;
136 pub use crate::host::memory::MemoryHost;
137 pub use crate::provider::claude::api::Renewed;
138 pub use crate::provider::claude::paths::live_service;
139 pub use crate::provider::claude::slot::{LIVE_SERVICE, dir_hash, service_for_dir};
140 pub use crate::provider::jwt::unsigned as unsigned_token;
141 #[cfg(target_os = "linux")]
142 pub use crate::secret_service_testing::SecretService;
143 pub use crate::stand_in;
146 pub use crate::store::memory::{Fault, MemoryStore};
147 pub use crate::store::vault_read;
148 pub use crate::switch::enroll::{ScriptedSignIn, SignInScript};
149 pub use crate::time::{Clock, FixedClock};
150
151 pub fn launcher(program: &std::path::Path) -> Option<std::path::PathBuf> {
154 crate::host::OS.launcher(program)
155 }
156
157 pub fn variables() -> impl Iterator<Item = &'static str> {
160 crate::context::variables()
161 }
162
163 pub fn agy_item(login: &str) -> String {
165 crate::provider::agy::document::sealed(login, crate::host::OS)
166 }
167
168 pub fn agy_login(email: &str, access: &str, refresh: &str) -> serde_json::Value {
169 crate::provider::agy::login_for_a_test(email, access, refresh)
170 }
171
172 pub fn codex_login(
173 account: &str,
174 email: &str,
175 access: &str,
176 refresh: &str,
177 ) -> serde_json::Value {
178 crate::provider::codex::login_for_a_test(account, email, access, refresh)
179 }
180
181 pub fn holders(which: crate::provider::ProviderId) -> &'static [crate::holder::Holder] {
184 use crate::provider::{Adoption, LoginKind};
185 match crate::provider::of(which) {
186 LoginKind::Document(tool) => match (tool.held_by(), tool.adoption(None)) {
187 (Some(held), _) => held.holders,
188 (None, Adoption::RestartRequired { holders, .. }) => holders,
189 (None, _) => &[],
190 },
191 LoginKind::Folder(app) => std::slice::from_ref(app.holder()),
192 }
193 }
194
195 pub fn park_unread(label: &str, path: &std::path::Path, why: &str) -> crate::error::Error {
196 crate::store::folder::unread(label, path, why)
197 }
198
199 pub fn lies_in(path: &std::path::Path, place: &std::path::Path) -> bool {
201 crate::host::lies_in(path, place)
202 }
203
204 pub fn real_login_places(
205 started_in: &crate::context::Context,
206 ) -> Result<Vec<std::path::PathBuf>, String> {
207 crate::host::real_login_places(Some(started_in))
208 }
209
210 pub fn vault_write(
213 ctx: &crate::context::Context,
214 service: &str,
215 contents: &str,
216 ) -> crate::error::Result<()> {
217 let permit = crate::permit::gate(ctx)?;
218 Ok(crate::store::vault_write(ctx, permit, service, contents)?)
219 }
220
221 pub fn may_be_running(pid: u32) -> bool {
222 crate::host::proc::may_be_running(pid)
223 }
224
225 #[cfg(windows)]
227 pub fn windows_build() -> Option<u32> {
228 crate::host::user::version().map(|(_, _, build)| build)
229 }
230
231 #[cfg(windows)]
233 pub fn processes() -> std::io::Result<Vec<(u32, u32, String)>> {
234 crate::host::proc::snapshot()
235 }
236
237 #[cfg(windows)]
239 pub mod version_resource {
240 use std::path::Path;
241
242 pub fn file_version(program: &Path) -> Option<[u16; 4]> {
243 crate::host::proc::file_version(program)
244 }
245
246 pub fn product_version(program: &Path) -> Option<[u16; 4]> {
247 crate::host::proc::product_version(program)
248 }
249
250 pub fn string(program: &Path, name: &str) -> Option<String> {
251 crate::host::proc::version_string(program, name)
252 }
253 }
254
255 #[cfg(windows)]
258 pub mod credman {
259 use crate::context::Context;
260 use crate::store::RawStore;
261
262 pub struct Item {
263 pub kind: u32,
264 pub persist: u32,
265 pub user: String,
266 pub comment: Option<String>,
267 pub attributes: u32,
268 pub bytes: usize,
269 }
270
271 fn refused(target: &str) -> std::io::Result<()> {
272 match crate::provider::names::refusal(target, &[]) {
273 Some(why) => Err(std::io::Error::other(why)),
274 None => Ok(()),
275 }
276 }
277
278 pub fn item(target: &str) -> std::io::Result<Option<Item>> {
279 refused(target)?;
280 Ok(
281 crate::host::windows_credman::item(target)?.map(|held| Item {
282 kind: held.kind,
283 persist: held.persist,
284 user: held.user,
285 comment: held.comment,
286 attributes: held.attributes,
287 bytes: held.blob.len(),
288 }),
289 )
290 }
291
292 pub fn write_as_bun(target: &str, user: &str, value: &str) -> std::io::Result<()> {
294 refused(target)?;
295 crate::host::windows_credman::write(target, user, value.as_bytes())
296 }
297
298 fn store(ctx: &Context) -> crate::provider::claude::credman::Store {
299 crate::provider::claude::credman::Store::of(ctx)
300 }
301
302 pub fn write(ctx: &Context, service: &str, contents: &str) -> crate::error::Result<()> {
303 let permit = crate::permit::gate(ctx)?;
304 Ok(store(ctx).write(permit, service, contents)?)
305 }
306
307 pub fn read(ctx: &Context, service: &str) -> crate::error::Result<Option<String>> {
308 Ok(store(ctx).read(service)?)
309 }
310
311 pub fn discard(ctx: &Context, service: &str) -> crate::error::Result<()> {
313 let permit = crate::permit::gate(ctx)?;
314 Ok(store(ctx).delete(permit, service)?)
315 }
316
317 pub fn sweep(ctx: &Context, service: &str) -> Result<(), String> {
319 crate::provider::claude::credman::swept(ctx, service)
320 }
321 }
322
323 pub fn vault_delete(ctx: &crate::context::Context, service: &str) -> crate::error::Result<()> {
325 let permit = crate::permit::gate(ctx)?;
326 Ok(crate::store::vault_delete(ctx, permit, service)?)
327 }
328
329 pub fn signed_in(
334 ctx: &crate::context::Context,
335 which: crate::provider::ProviderId,
336 document: &str,
337 ) -> crate::error::Result<crate::switch::SignIn> {
338 use crate::provider::LoginKind;
339 let document = serde_json::from_str(document).expect("a login a test wrote as JSON");
340 match crate::provider::of(which) {
341 LoginKind::Document(tool) => {
342 crate::switch::planted(ctx, crate::permit::gate(ctx)?, tool, document)
343 }
344 LoginKind::Folder(_) => Err(crate::error::Error::Usage(
345 "a Claude Desktop sign-in is played by testing::desktop".into(),
346 )),
347 }
348 }
349}