@coworker-jp/aidr 0.1.11 → 0.1.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,63 +0,0 @@
1
- // Best-effort invoker username resolution at install time.
2
- //
3
- // `aidr install --scheduled` / `--with-sentinel` writes the unix username
4
- // into `/etc/aidr/aidr.env` as `AI_SCANNER_INVOKER_USER`. The Rust daemon
5
- // reads it back to resolve git email / Apple ID / $HOME of the actual
6
- // installer (root daemons have no useful identity of their own).
7
- //
8
- // In the auto-sudo path the CLI re-execs itself via `sudo -E`, which sets
9
- // SUDO_USER. But several real-world contexts arrive here as bare root
10
- // without SUDO_USER:
11
- // - `ssh root@host` then `npx ... install --scheduled` (no sudo prefix)
12
- // - container / CI where the user is already root
13
- // - sudo policy that strips SUDO_USER
14
- //
15
- // We walk a fallback chain so the resulting daemon snapshot reports the
16
- // real user instead of `actor_id=unknown`. Each step is best-effort and
17
- // never throws — we return `null` when no signal is available, which is
18
- // the honest outcome for genuinely identity-less contexts (CI runners).
19
-
20
- import { execSync } from "node:child_process";
21
-
22
- function tryExec(cmd) {
23
- try {
24
- return execSync(cmd, { stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
25
- } catch {
26
- return "";
27
- }
28
- }
29
-
30
- function isUsable(name) {
31
- return name && name !== "root";
32
- }
33
-
34
- export function resolveInvokerUser() {
35
- if (process.env.SUDO_USER) return process.env.SUDO_USER;
36
-
37
- // `who am i` reads utmp, so it returns the actual login user even after
38
- // `sudo -i` / `su -`. Empty under non-tty / no utmp.
39
- const whoUser = tryExec("who am i").split(/\s+/)[0];
40
- if (isUsable(whoUser)) return whoUser;
41
-
42
- // POSIX `logname` gives controlling-tty owner. Slightly different code
43
- // path from `who am i` — kept as a separate fallback because they fail
44
- // independently on minimal images.
45
- const logname = tryExec("logname");
46
- if (isUsable(logname)) return logname;
47
-
48
- // systemd Linux: `loginctl list-sessions` columns are: SESSION UID USER ...
49
- const loginctl = tryExec("loginctl list-sessions --no-legend");
50
- for (const line of loginctl.split("\n")) {
51
- const fields = line.trim().split(/\s+/);
52
- if (fields.length >= 3 && isUsable(fields[2])) return fields[2];
53
- }
54
-
55
- // Env fallbacks (root-filtered) so a user-shell install without sudo at
56
- // all (rare for --scheduled, possible for plain --agent without root)
57
- // still captures the obvious answer.
58
- for (const env of ["LOGNAME", "USER"]) {
59
- if (isUsable(process.env[env])) return process.env[env];
60
- }
61
-
62
- return null;
63
- }