1use crate::error::Error;
2use crate::holder;
3use crate::in_use::Stored;
4use crate::provider::{Held, ProviderId};
5use crate::state::{self, Key};
6use crate::switch::{self, Recovered};
7use crate::words::capitalised;
8use std::fmt;
9
10#[derive(Debug)]
12#[non_exhaustive]
13pub enum Warning {
14 Recovered(Recovered),
16 ConfigNotUpdated(Error),
18 ParksPendingRemoval(usize),
19 ParkedLoginRefused {
21 tool: ProviderId,
22 label: String,
23 },
24 RenewalFailed(Error),
25 LockCompromised {
27 tool: ProviderId,
28 },
29 AuthOverridden {
31 tool: ProviderId,
32 names: Vec<String>,
33 },
34 FallbackLogin {
36 tool: ProviderId,
37 path: std::path::PathBuf,
38 store: crate::host::SecretStore,
39 held: Held,
40 },
41 WrittenOnTheCommandLine {
43 tool: ProviderId,
44 bytes: usize,
45 limit: usize,
46 },
47 SessionsStillRunning {
49 from: String,
50 holding: Vec<holder::Holding>,
51 },
52 SessionsKeepTheOldLogin {
54 label: String,
55 holding: Vec<holder::Holding>,
56 },
57 SessionsUnknown {
59 tool: ProviderId,
60 from: String,
61 },
62 SessionsUnknownAfterSignIn {
64 tool: ProviderId,
65 label: String,
66 },
67 SignInParkedNotInUse {
69 tool: ProviderId,
70 label: String,
71 why: String,
72 spelling: crate::words::Spelling,
73 },
74 SwitchStuck(Error),
76 LoginReplaced {
78 tool: ProviderId,
79 id: String,
80 label: String,
81 now: Stored,
82 },
83 InUseUnconfirmed {
86 tool: ProviderId,
87 last: Stored,
88 named: Stored,
89 asked: bool,
90 },
91 ConfigNamesAnother {
93 tool: ProviderId,
94 config: Stored,
95 in_use: Stored,
96 },
97 TakenAsNamed {
99 tool: ProviderId,
100 label: String,
101 recorded: Option<String>,
102 },
103 SetAside {
105 tool: ProviderId,
106 path: std::path::PathBuf,
107 label: Option<String>,
108 why: state::SetAside,
109 },
110 KeysSetAside {
112 tool: ProviderId,
113 path: std::path::PathBuf,
114 label: Option<String>,
115 },
116 LooksSignedOut {
118 tool: ProviderId,
119 label: String,
120 },
121 MoveSettled(switch::MoveSettled),
123 MoveWaiting {
125 tool: ProviderId,
126 label: String,
127 },
128 MoveNotGivenUp {
130 tool: ProviderId,
131 label: String,
132 why: Error,
133 },
134 MoveNotSettled {
136 tool: ProviderId,
137 label: String,
138 why: Error,
139 },
140 SignInAwaited {
142 tool: ProviderId,
143 label: String,
144 put_back: String,
145 since: i64,
146 again: bool,
148 },
149 ItemGone {
151 tool: ProviderId,
152 label: String,
153 item: String,
154 },
155 ReadOnly {
157 why: Option<&'static str>,
158 },
159 ReadOnlyBelowTheFloor {
161 build: Option<u32>,
162 },
163}
164
165impl Warning {
166 pub(crate) fn read_only(refused: &Error) -> Option<Warning> {
167 match *refused {
168 Error::Elevated { why } => Some(Warning::ReadOnly { why }),
169 Error::SystemTooOld { build } => Some(Warning::ReadOnlyBelowTheFloor { build }),
170 _ => None,
171 }
172 }
173
174 pub fn code(&self) -> &'static str {
176 match self {
177 Warning::Recovered(r) => r.code(),
178 Warning::LockCompromised { .. } => "lock_compromised",
179 Warning::ConfigNotUpdated(e) | Warning::RenewalFailed(e) | Warning::SwitchStuck(e) => {
180 e.code()
181 }
182 Warning::ParksPendingRemoval(_) => "parks_pending_removal",
183 Warning::ParkedLoginRefused { .. } => "parked_login_refused",
184 Warning::AuthOverridden { .. } => "auth_overridden",
185 Warning::FallbackLogin { .. } => "fallback_login",
186 Warning::WrittenOnTheCommandLine { .. } => "written_on_the_command_line",
187 Warning::SessionsStillRunning { .. } => "sessions_still_running",
188 Warning::SessionsKeepTheOldLogin { .. } => "sessions_keep_old_login",
189 Warning::SessionsUnknown { .. } | Warning::SessionsUnknownAfterSignIn { .. } => {
190 "sessions_unknown"
191 }
192 Warning::SignInParkedNotInUse { .. } => "sign_in_parked_not_in_use",
193 Warning::LoginReplaced { .. } => "login_replaced",
194 Warning::InUseUnconfirmed { .. } => "in_use_unconfirmed",
195 Warning::ConfigNamesAnother { .. } => "config_names_another",
196 Warning::TakenAsNamed { .. } => "login_taken_as_named",
197 Warning::SetAside { .. } | Warning::KeysSetAside { .. } => "set_aside",
198 Warning::LooksSignedOut { .. } => "looks_signed_out",
199 Warning::MoveSettled(moved) => match moved.ended {
200 switch::Ended::Undone => "interrupted_move_undone",
201 switch::Ended::Finished => "interrupted_move_finished",
202 },
203 Warning::MoveWaiting { .. } => "move_waiting",
204 Warning::MoveNotGivenUp { .. } => "move_not_given_up",
205 Warning::MoveNotSettled { .. } => "move_not_settled",
206 Warning::SignInAwaited { .. } => "sign_in_awaited",
207 Warning::ItemGone { .. } => "login_item_gone",
208 Warning::ReadOnly { .. } | Warning::ReadOnlyBelowTheFloor { .. } => "read_only",
209 }
210 }
211
212 pub fn tool(&self) -> Option<ProviderId> {
213 match self {
214 Warning::ParkedLoginRefused { tool, .. }
215 | Warning::LockCompromised { tool }
216 | Warning::AuthOverridden { tool, .. }
217 | Warning::FallbackLogin { tool, .. }
218 | Warning::WrittenOnTheCommandLine { tool, .. }
219 | Warning::SessionsUnknown { tool, .. }
220 | Warning::SessionsUnknownAfterSignIn { tool, .. }
221 | Warning::SignInParkedNotInUse { tool, .. }
222 | Warning::LoginReplaced { tool, .. }
223 | Warning::InUseUnconfirmed { tool, .. }
224 | Warning::ConfigNamesAnother { tool, .. }
225 | Warning::TakenAsNamed { tool, .. }
226 | Warning::SetAside { tool, .. }
227 | Warning::KeysSetAside { tool, .. }
228 | Warning::LooksSignedOut { tool, .. }
229 | Warning::MoveWaiting { tool, .. }
230 | Warning::MoveNotGivenUp { tool, .. }
231 | Warning::MoveNotSettled { tool, .. }
232 | Warning::SignInAwaited { tool, .. }
233 | Warning::ItemGone { tool, .. } => Some(*tool),
234 Warning::MoveSettled(moved) => Some(moved.tool),
235 Warning::Recovered(_)
236 | Warning::ConfigNotUpdated(_)
237 | Warning::ParksPendingRemoval(_)
238 | Warning::RenewalFailed(_)
239 | Warning::SessionsStillRunning { .. }
240 | Warning::SessionsKeepTheOldLogin { .. }
241 | Warning::SwitchStuck(_)
242 | Warning::ReadOnly { .. }
243 | Warning::ReadOnlyBelowTheFloor { .. } => None,
244 }
245 }
246}
247
248impl fmt::Display for Warning {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 match self {
251 Warning::Recovered(r) => write!(f, "{r}"),
252 Warning::LockCompromised { tool } => write!(
253 f,
254 "{} reclaimed a credential lock while this change was under way, so it may \
255 have written or renewed a login at the same time. Pitboard read the slot back \
256 and the change stood, but check with `pitboard` that the right account is \
257 signed in. A login Pitboard parked meanwhile may need signing in to again \
258 when it is next used.",
259 tool.name()
260 ),
261 Warning::ConfigNotUpdated(e) | Warning::RenewalFailed(e) | Warning::SwitchStuck(e) => {
262 write!(f, "{e}")
263 }
264 Warning::ParksPendingRemoval(count) => write!(
265 f,
266 "{count} parked login(s) no longer in use could not be removed yet; Pitboard \
267 tries again on its next change"
268 ),
269 Warning::ParkedLoginRefused { tool, label } => write!(
270 f,
271 "{} no longer accepts the parked login for `{label}`. Run `pitboard enroll \
272 {label} --sign-in` to sign in to it again.",
273 tool.service()
274 ),
275 Warning::WrittenOnTheCommandLine { tool, bytes, limit } => {
276 write!(
277 f,
278 "this login needs {bytes} bytes and `security` reads {limit} from stdin, \
279 so it was written on the argument line, where a process running as you \
280 could have read it while the call lasted."
281 )?;
282 if *tool == ProviderId::Claude {
283 write!(
284 f,
285 " Claude Code writes this same login the same way whenever it \
286 refreshes the token."
287 )?;
288 }
289 Ok(())
290 }
291 Warning::AuthOverridden { tool, names } => write!(
292 f,
293 "{} is set, so {} signs in with it and not with the login Pitboard moved. \
294 Unset it for the switch to take effect.",
295 names.join(" and "),
296 tool.name()
297 ),
298 Warning::FallbackLogin {
299 tool,
300 path,
301 store,
302 held: Held::Login,
303 } => write!(
304 f,
305 "{path} holds another {tool} login, which {reader} signs in with. No switch \
306 reaches it. While it is there, {tool} sessions already running at a switch \
307 {running}. `pitboard stow` keeps that login for its account, where Pitboard \
308 holds no other it can switch to, then deletes the file.",
309 path = path.display(),
310 tool = tool.name(),
311 reader = crate::words::reads_only_the_file(*store),
312 running = crate::words::kept_until_renewed(),
313 ),
314 Warning::FallbackLogin {
315 tool,
316 path,
317 held: Held::NoLogin,
318 ..
319 } => write!(
320 f,
321 "{path} is there with no {tool} login in it. While it is, {tool} sessions \
322 already running at a switch {running}. `pitboard stow` deletes it, which lets \
323 them follow a switch.",
324 path = path.display(),
325 tool = tool.name(),
326 running = crate::words::kept_until_renewed(),
327 ),
328 Warning::FallbackLogin {
329 tool,
330 path,
331 held: Held::Unreadable,
332 ..
333 } => write!(
334 f,
335 "{path} is there, and Pitboard could not read it. While it is, {tool} sessions \
336 already running at a switch {running}. `pitboard doctor` says why, and what to \
337 do.",
338 path = path.display(),
339 tool = tool.name(),
340 running = crate::words::kept_until_renewed(),
341 ),
342 Warning::SessionsStillRunning { from, holding } => write!(
343 f,
344 "{} started before this switch {} still running and still using `{from}`. {} \
345 Do not sign out in {}: signing out there revokes `{from}`'s login, which \
346 Pitboard has just parked.",
347 capitalised(&holder::described(holding)),
348 if holder::plural(holding) { "are" } else { "is" },
349 holder::remedies(holding, "to use the new account"),
350 if holder::plural(holding) {
351 "any of them"
352 } else {
353 "it"
354 },
355 ),
356 Warning::SessionsKeepTheOldLogin { label, holding } => write!(
357 f,
358 "{} started before this sign-in {} still running and still using `{label}`'s \
359 old login. {} Otherwise one of them can put the old login back in place of \
360 the new one when it refreshes its token.",
361 capitalised(&holder::described(holding)),
362 if holder::plural(holding) { "are" } else { "is" },
363 holder::remedies(holding, "to use the new one"),
364 ),
365 Warning::SessionsUnknown { tool, from } => write!(
366 f,
367 "Pitboard could not tell whether {} sessions started before this switch are \
368 still running, because it could not read the list of processes. Do not sign \
369 out in one that is: signing out there revokes `{from}`'s login, which \
370 Pitboard has just parked.",
371 tool.name()
372 ),
373 Warning::SessionsUnknownAfterSignIn { tool, label } => write!(
374 f,
375 "Pitboard could not tell whether {} sessions started before this sign-in are \
376 still running, because it could not read the list of processes. Quit any \
377 that are still using `{label}`'s old login and start them again. Otherwise \
378 one of them can put the old login back in place of the new one when it \
379 refreshes its token.",
380 tool.name()
381 ),
382 Warning::SignInParkedNotInUse {
383 tool,
384 label,
385 why,
386 spelling,
387 } => write!(
388 f,
389 "{} goes on with the login it has: Pitboard could not tell whose it is \
390 ({why}), so it parked the new login for `{label}` rather than write over \
391 that one. If that login no longer works, {} and sign in to `{label}` \
392 there.",
393 tool.name(),
394 crate::words::sign_in_yourself(*tool, spelling.of(*tool), false)
395 ),
396 Warning::LoginReplaced {
397 tool, label, now, ..
398 } => match (tool, now) {
399 (ProviderId::Desktop, Stored::Nothing) => write!(
400 f,
401 "`{label}` was signed out in Claude, which ends its login at claude.ai, and \
402 Pitboard holds no login for it. Run `pitboard enroll {label} --sign-in` to \
403 sign in to it again."
404 ),
405 (ProviderId::Claude | ProviderId::Codex | ProviderId::Desktop, now) => {
406 write!(
407 f,
408 "`{label}`'s login was replaced by a sign-in outside Pitboard"
409 )?;
410 let tool = tool.name();
411 match now {
412 Stored::Account(now) => {
413 write!(f, ": {tool} now has `{now}`'s login stored,")?;
414 }
415 Stored::Unenrolled(email) if email.is_empty() => write!(
416 f,
417 ": {tool} now has the login of an account Pitboard has not enrolled,"
418 )?,
419 Stored::Unenrolled(email) => {
420 write!(f, ": {tool} now has {email}'s login stored,")?;
421 }
422 Stored::Nothing => write!(f, ": {tool} now has no login stored,")?,
423 Stored::Unknown => write!(f, ",")?,
424 }
425 write!(
426 f,
427 " and Pitboard holds no login for `{label}`. Run `pitboard enroll \
428 {label} --sign-in` to sign in to it again."
429 )
430 }
431 },
432 Warning::InUseUnconfirmed {
433 tool,
434 last,
435 named,
436 asked,
437 } => match tool {
438 ProviderId::Desktop => desktop_named(f, named, last),
439 ProviderId::Claude | ProviderId::Codex if !asked => write!(
440 f,
441 "{name}'s config names {named}, and Pitboard has not asked {service} whose \
442 login {name} has stored since Pitboard was updated, so another login may be \
443 stored. The account it last switched to is {last}. `pitboard status` asks.",
444 name = tool.name(),
445 named = named.said("no account"),
446 service = tool.service(),
447 last = last.said("none"),
448 ),
449 ProviderId::Claude | ProviderId::Codex => {
450 write!(
451 f,
452 "{name}'s config has named {named} since Pitboard last asked {service} \
453 whose login {name} has stored, so another login may be stored.",
454 name = tool.name(),
455 named = named.said("no account"),
456 service = tool.service(),
457 )?;
458 match last {
459 Stored::Account(_) | Stored::Unenrolled(_) => {
460 write!(f, " The one stored then was {}'s.", last.said(""))?;
461 }
462 Stored::Nothing => write!(f, " None was stored then.")?,
463 Stored::Unknown => {}
464 }
465 write!(f, " `pitboard status` asks again.")
466 }
467 },
468 Warning::ConfigNamesAnother {
469 tool,
470 config,
471 in_use,
472 } => match tool {
473 ProviderId::Desktop => desktop_named(f, config, in_use),
474 ProviderId::Claude | ProviderId::Codex => {
475 let config = config.said("no account");
476 write!(
477 f,
478 "{name}'s config names {config}, and the login {name} has stored is \
479 {}'s, as {} said. `/status` in {name} shows {config}.",
480 in_use.said("nobody"),
481 tool.service(),
482 name = tool.name(),
483 )?;
484 write!(f, " {}", in_use.how_to_name())
485 }
486 },
487 Warning::TakenAsNamed {
488 label,
489 recorded: None,
490 ..
491 } => write!(
492 f,
493 "Pitboard had not seen the login Claude held, and took it as `{label}`'s because \
494 Claude names that account. If Claude showed another account, run `pitboard use \
495 {label}`, check in Claude, quit it, and enrol it under its own name."
496 ),
497 Warning::TakenAsNamed {
498 label,
499 recorded: Some(recorded),
500 ..
501 } => write!(
502 f,
503 "Pitboard had recorded the login Claude held as `{recorded}`'s, and filed it as \
504 `{label}`'s because Claude names that account. Open Claude and check which \
505 account it shows. If it is another, quit Claude with Command-Q, then run \
506 `pitboard enroll desktop/<label>` under that account's name."
507 ),
508 Warning::SetAside {
509 path, label, why, ..
510 } => {
511 let path = path.display();
512 match (why, label) {
513 (state::SetAside::Superseded, Some(label)) => write!(
514 f,
515 "Pitboard kept `{label}`'s earlier parked login in {path} and deleted \
516 nothing. `pitboard doctor` lists it."
517 ),
518 (state::SetAside::Superseded, None) => write!(
519 f,
520 "Pitboard kept an earlier parked login in {path} and deleted nothing. \
521 `pitboard doctor` lists it."
522 ),
523 (state::SetAside::GivenUp, Some(label)) => write!(
524 f,
525 "Pitboard moved the parts of `{label}`'s login left in Claude's folder \
526 into {path} and deleted nothing. `pitboard doctor` lists them."
527 ),
528 (state::SetAside::GivenUp, None) => write!(
529 f,
530 "Pitboard moved the parts of a login left in Claude's folder into {path} \
531 and deleted nothing. `pitboard doctor` lists them."
532 ),
533 (
534 state::SetAside::InTheWay
535 | state::SetAside::AnotherMachine
536 | state::SetAside::Orphan,
537 _,
538 ) => write!(
539 f,
540 "Pitboard moved what it found in the way into {path} and deleted nothing."
541 ),
542 }
543 }
544 Warning::KeysSetAside { path, label, .. } => {
545 let whose = label
546 .as_ref()
547 .map_or_else(|| "a login".to_string(), |label| format!("`{label}`"));
548 write!(
549 f,
550 "Claude rewrote {whose}'s keys. Pitboard kept the earlier ones in {} and \
551 deleted nothing. `pitboard doctor` lists them.",
552 path.display()
553 )
554 }
555 Warning::LooksSignedOut { label, .. } => write!(
556 f,
557 "Claude looks signed out since Pitboard last read `{label}` in use. The next \
558 change to a Claude Desktop account checks."
559 ),
560 Warning::MoveSettled(moved) => {
561 let label = Key::new(moved.tool, moved.label.clone()).typed();
562 f.write_str(&crate::words::move_settled(&label, moved.half, moved.ended))
563 }
564 Warning::MoveWaiting { label, .. } => f.write_str(&crate::words::move_waiting(label)),
565 Warning::MoveNotGivenUp { label, why, .. } => {
566 f.write_str(&crate::words::move_not_given_up(label, why))
567 }
568 Warning::MoveNotSettled { label, why, .. } => {
569 f.write_str(&crate::words::move_not_settled(label, why))
570 }
571 Warning::SignInAwaited {
572 label,
573 put_back,
574 again,
575 ..
576 } => f.write_str(&crate::words::sign_in_awaited(label, put_back, *again)),
577 Warning::ItemGone { label, item, .. } => {
578 f.write_str(&crate::words::item_gone(label, item))
579 }
580 Warning::ReadOnly { why } => {
581 read_only(f, &crate::words::elevated(crate::host::OS, *why))
582 }
583 Warning::ReadOnlyBelowTheFloor { build } => {
584 read_only(f, &crate::words::too_old(*build))
585 }
586 }
587 }
588}
589
590fn desktop_named(f: &mut fmt::Formatter<'_>, named: &Stored, last: &Stored) -> fmt::Result {
591 write!(
592 f,
593 "{}. Open Claude, check which account it shows, quit it with Command-Q, then run \
594 `pitboard enroll desktop/<label>` under that account's name.",
595 crate::words::desktop_named(named, last, |stored| stored.said("nobody"))
596 )
597}
598
599fn read_only(f: &mut fmt::Formatter<'_>, said: &crate::words::ChangesNothing) -> fmt::Result {
600 write!(
601 f,
602 "{}, so it changes nothing: these are the numbers it last measured, and it renews no \
603 parked login and asks nobody. {}",
604 said.because, said.to_ask_again
605 )
606}