@bridge4dev/runner 0.22.1 → 0.27.0
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.
- package/dist/agent-auth.d.ts +29 -0
- package/dist/agent-auth.js +136 -0
- package/dist/auth-relay.d.ts +62 -4
- package/dist/auth-relay.js +423 -25
- package/dist/environment.d.ts +186 -0
- package/dist/environment.js +433 -0
- package/dist/git.d.ts +10 -0
- package/dist/git.js +94 -5
- package/dist/index.js +466 -21
- package/dist/paths.d.ts +9 -0
- package/dist/paths.js +11 -0
- package/dist/protocol.d.ts +7 -7
- package/dist/recipe-schema.d.ts +1 -1
- package/dist/self-update.d.ts +55 -0
- package/dist/self-update.js +164 -25
- package/dist/service-unit.d.ts +13 -1
- package/dist/service-unit.js +41 -10
- package/dist/supervisor.js +35 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything that depends on WHICH USER the runner runs as.
|
|
3
|
+
*
|
|
4
|
+
* The install instructions offer a choice — root or a dedicated user — and both
|
|
5
|
+
* are legitimate. What is not legitimate is the way the second choice used to
|
|
6
|
+
* fail: silently, later, and in the agent's voice. A dedicated user typically
|
|
7
|
+
* owns none of the project directories, has an empty `$HOME` where the agent
|
|
8
|
+
* CLIs keep their login and settings, and reaches systemd only with
|
|
9
|
+
* `XDG_RUNTIME_DIR` set. Each of those produces a symptom that reads like a
|
|
10
|
+
* broken runner ("agent can't do anything", "not signed in", "service is fine"
|
|
11
|
+
* with exit code 0) while the real cause is a permission nobody was told about.
|
|
12
|
+
*
|
|
13
|
+
* This module turns each of them into a fact with a command next to it.
|
|
14
|
+
*/
|
|
15
|
+
export interface RunnerIdentity {
|
|
16
|
+
user: string;
|
|
17
|
+
uid: number;
|
|
18
|
+
gid: number;
|
|
19
|
+
home: string;
|
|
20
|
+
isRoot: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function runnerIdentity(): RunnerIdentity;
|
|
23
|
+
export interface PathAccess {
|
|
24
|
+
path: string;
|
|
25
|
+
exists: boolean;
|
|
26
|
+
isDirectory: boolean;
|
|
27
|
+
/** uid of the owner, or -1 when we could not stat it. */
|
|
28
|
+
ownerUid: number;
|
|
29
|
+
ownedByUs: boolean;
|
|
30
|
+
readable: boolean;
|
|
31
|
+
writable: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* True when we could not even look — a directory ABOVE this one denies us
|
|
34
|
+
* traversal. Distinct from `!exists` on purpose: those are opposite answers
|
|
35
|
+
* to the person reading them («create it» vs «grant access»), and `statSync`
|
|
36
|
+
* reports both by throwing.
|
|
37
|
+
*/
|
|
38
|
+
unreachable: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function inspectPath(target: string): PathAccess;
|
|
41
|
+
/**
|
|
42
|
+
* The first directory on this path the runner cannot enter.
|
|
43
|
+
*
|
|
44
|
+
* «Permission denied» on `/srv/apps/shop` is usually not about `shop` at all —
|
|
45
|
+
* it is about `/srv/apps`, and naming the wrong one sends the person to chmod
|
|
46
|
+
* a directory that was never the problem.
|
|
47
|
+
*/
|
|
48
|
+
export declare function firstUnreachableAncestor(target: string): string | null;
|
|
49
|
+
/**
|
|
50
|
+
* Git refuses to work in a repository owned by somebody else — since 2022, and
|
|
51
|
+
* with no exception for root. So the "obvious" fix for a dedicated user (chown
|
|
52
|
+
* the project to it) breaks git for the person who was committing there before,
|
|
53
|
+
* and the exception has to be added on BOTH sides.
|
|
54
|
+
*/
|
|
55
|
+
export declare function safeDirectoryCommand(repoPath: string): string;
|
|
56
|
+
export declare function looksLikeDubiousOwnership(message: string): boolean;
|
|
57
|
+
/** Is this path already excused in the current user's git config? */
|
|
58
|
+
export declare function hasSafeDirectory(repoPath: string): Promise<boolean>;
|
|
59
|
+
export declare function addSafeDirectory(repoPath: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* What the agent will find in this user's home besides a login.
|
|
62
|
+
*
|
|
63
|
+
* Copying only `.credentials.json` to a fresh user is the usual half-measure:
|
|
64
|
+
* the agent starts, and then behaves like a stranger — no permission allowlist,
|
|
65
|
+
* no slash commands, no plugins, default model. From the outside that reads as
|
|
66
|
+
* «the runner can't do anything», which is why this is worth reporting BEFORE
|
|
67
|
+
* the first session rather than diagnosing after it.
|
|
68
|
+
*/
|
|
69
|
+
export interface AgentConfigContour {
|
|
70
|
+
home: string;
|
|
71
|
+
claudeDir: boolean;
|
|
72
|
+
settings: boolean;
|
|
73
|
+
localSettings: boolean;
|
|
74
|
+
/** Number of entries in `permissions.allow`, or null when unreadable. */
|
|
75
|
+
allowRules: number | null;
|
|
76
|
+
commands: number;
|
|
77
|
+
plugins: boolean;
|
|
78
|
+
codexDir: boolean;
|
|
79
|
+
codexConfig: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* MCP servers an agent session in this home will actually see, and the ones
|
|
82
|
+
* that are configured but invisible to it.
|
|
83
|
+
*
|
|
84
|
+
* Both numbers, because the difference IS the bug. `claude mcp add` defaults
|
|
85
|
+
* to LOCAL scope, which stores the server under
|
|
86
|
+
* `~/.claude.json → projects["<cwd>"].mcpServers` — keyed by the directory it
|
|
87
|
+
* was added from. Runner sessions work in a per-session git worktree, so that
|
|
88
|
+
* key never matches and the servers are simply absent. On a live machine this
|
|
89
|
+
* read as «the agent lost Playwright and Context7», and the only cure was
|
|
90
|
+
* moving them to user scope (the top-level `mcpServers`).
|
|
91
|
+
*/
|
|
92
|
+
mcpUserScope: number;
|
|
93
|
+
mcpProjectScope: number;
|
|
94
|
+
}
|
|
95
|
+
export declare function agentConfigContour(home?: string): AgentConfigContour;
|
|
96
|
+
/**
|
|
97
|
+
* Another user's home that already has an agent set up.
|
|
98
|
+
*
|
|
99
|
+
* Only reported, never copied: a copied OAuth credential means two accounts
|
|
100
|
+
* share one refresh token, and a rotation in either one silently invalidates
|
|
101
|
+
* the other. Signing in as the runner's own user is the clean answer; the copy
|
|
102
|
+
* is the fast one, and the person choosing between them deserves to be told
|
|
103
|
+
* which is which.
|
|
104
|
+
*/
|
|
105
|
+
export declare function otherHomeWithAgents(me?: RunnerIdentity): string | null;
|
|
106
|
+
/**
|
|
107
|
+
* `systemctl --user` talks over a per-user D-Bus socket, and finds it through
|
|
108
|
+
* `XDG_RUNTIME_DIR`. Under `sudo -iu <user>` that variable is not set, and the
|
|
109
|
+
* failure is worse than an error: it prints «Failed to connect to bus» and
|
|
110
|
+
* exits **0**, so a health check reads it as success.
|
|
111
|
+
*/
|
|
112
|
+
export declare function systemdUserEnv(): NodeJS.ProcessEnv;
|
|
113
|
+
/** True when the user bus is actually reachable — `systemctl --user` lies with exit 0. */
|
|
114
|
+
export declare function systemdUserBusReachable(): Promise<boolean>;
|
|
115
|
+
/** The command form that works under `sudo -iu <user>` — printed in hints. */
|
|
116
|
+
export declare function systemctlHint(args: string): string;
|
|
117
|
+
/**
|
|
118
|
+
* Remembered from binding and from session starts, so `doctor` can check the
|
|
119
|
+
* permissions of real projects instead of asking the person to name them.
|
|
120
|
+
* Best-effort on purpose: a runner that cannot write its own state directory
|
|
121
|
+
* has bigger problems than a diagnostic list, and none of them should turn a
|
|
122
|
+
* session start into an error.
|
|
123
|
+
*/
|
|
124
|
+
export declare function rememberWorkspacePath(workspacePath: string): void;
|
|
125
|
+
export declare function knownWorkspacePaths(): string[];
|
|
126
|
+
export interface ToolCheck {
|
|
127
|
+
/** `null` when the tool is not installed at all. */
|
|
128
|
+
path: string | null;
|
|
129
|
+
version?: string;
|
|
130
|
+
/** Set when the tool is there but this user cannot use it. */
|
|
131
|
+
problem?: string;
|
|
132
|
+
}
|
|
133
|
+
export declare function whichExecutable(name: string): string | null;
|
|
134
|
+
/**
|
|
135
|
+
* Node, as THIS user sees it.
|
|
136
|
+
*
|
|
137
|
+
* A dedicated user does not automatically inherit a Node installed for
|
|
138
|
+
* somebody else — fnm, nvm and a root-only prefix are all per-user by design.
|
|
139
|
+
* The runner itself is running, so node clearly exists somewhere; the question
|
|
140
|
+
* this answers is whether it is on the daemon user's own PATH, because that is
|
|
141
|
+
* what agent tooling and `npm install -g` will look at.
|
|
142
|
+
*/
|
|
143
|
+
export declare function nodeCheck(): Promise<ToolCheck>;
|
|
144
|
+
/**
|
|
145
|
+
* Docker, as THIS user sees it.
|
|
146
|
+
*
|
|
147
|
+
* Reported rather than judged: plenty of projects never touch it. But when the
|
|
148
|
+
* project's own workflow is `docker compose`, a dedicated user without access
|
|
149
|
+
* to the socket produces a session that fails on its first command, and the
|
|
150
|
+
* error will be about a socket rather than about a group nobody was added to.
|
|
151
|
+
*
|
|
152
|
+
* Worth stating where it is stated: being in the `docker` group is equivalent
|
|
153
|
+
* to root on this machine. That is a fact for the owner to accept knowingly.
|
|
154
|
+
*/
|
|
155
|
+
export declare function dockerCheck(): Promise<ToolCheck>;
|
|
156
|
+
/**
|
|
157
|
+
* Does the service survive a logout?
|
|
158
|
+
*
|
|
159
|
+
* `loginctl enable-linger` is what keeps a user's systemd services running with
|
|
160
|
+
* nobody logged in. `install-service` turns it on, but a unit installed by hand
|
|
161
|
+
* — or a user created afterwards — can miss it, and the failure looks like
|
|
162
|
+
* «the server goes offline whenever I close the terminal».
|
|
163
|
+
*/
|
|
164
|
+
export declare function lingerEnabled(): Promise<boolean | null>;
|
|
165
|
+
/**
|
|
166
|
+
* Make the agent CLIs findable, without taking anything away.
|
|
167
|
+
*
|
|
168
|
+
* A systemd user service starts with the manager's PATH, which is the
|
|
169
|
+
* distribution default — `/usr/bin` and friends. Both agent CLIs are commonly
|
|
170
|
+
* installed somewhere else: `~/.local/bin` for a per-user install, and a
|
|
171
|
+
* node managed by fnm/nvm/volta lives under its own version directory. When
|
|
172
|
+
* `codex` sits there, the runner scans PATH, does not find it, and reports to
|
|
173
|
+
* the dashboard that this machine has no Codex at all — the agent the person
|
|
174
|
+
* installed simply never appears.
|
|
175
|
+
*
|
|
176
|
+
* Deliberately APPEND-ONLY, and deliberately in the process rather than in the
|
|
177
|
+
* unit file. Writing `Environment=PATH=…` into the unit would REPLACE whatever
|
|
178
|
+
* systemd gives the service today (`/snap/bin`, anything set through
|
|
179
|
+
* `environment.d`), trading something that works for something that is
|
|
180
|
+
* missing. Here nothing can be lost: entries are only added when they are
|
|
181
|
+
* absent and the directory actually exists.
|
|
182
|
+
*
|
|
183
|
+
* Returns what it added, so the caller can say so once at startup.
|
|
184
|
+
*/
|
|
185
|
+
export declare function ensureAgentPath(): string[];
|
|
186
|
+
//# sourceMappingURL=environment.d.ts.map
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { knownWorkspacesPath } from './paths.js';
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
export function runnerIdentity() {
|
|
9
|
+
const uid = typeof process.getuid === 'function' ? process.getuid() : -1;
|
|
10
|
+
const gid = typeof process.getgid === 'function' ? process.getgid() : -1;
|
|
11
|
+
let user = process.env['USER'] ?? process.env['LOGNAME'] ?? '';
|
|
12
|
+
if (!user) {
|
|
13
|
+
try {
|
|
14
|
+
user = os.userInfo().username;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
user = uid === 0 ? 'root' : String(uid);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return { user, uid, gid, home: os.homedir(), isRoot: uid === 0 };
|
|
21
|
+
}
|
|
22
|
+
/** As root every access check passes, which is true and worth saying out loud. */
|
|
23
|
+
function canAccess(target, mode) {
|
|
24
|
+
try {
|
|
25
|
+
fs.accessSync(target, mode);
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function inspectPath(target) {
|
|
33
|
+
const me = runnerIdentity();
|
|
34
|
+
const base = {
|
|
35
|
+
path: target,
|
|
36
|
+
exists: false,
|
|
37
|
+
isDirectory: false,
|
|
38
|
+
ownerUid: -1,
|
|
39
|
+
ownedByUs: false,
|
|
40
|
+
readable: false,
|
|
41
|
+
writable: false,
|
|
42
|
+
unreachable: false,
|
|
43
|
+
};
|
|
44
|
+
let stat;
|
|
45
|
+
try {
|
|
46
|
+
stat = fs.statSync(target);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
const code = error.code;
|
|
50
|
+
return code === 'ENOENT' || code === 'ENOTDIR' ? base : { ...base, unreachable: true };
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
path: target,
|
|
54
|
+
exists: true,
|
|
55
|
+
isDirectory: stat.isDirectory(),
|
|
56
|
+
ownerUid: stat.uid,
|
|
57
|
+
ownedByUs: me.uid < 0 || stat.uid === me.uid,
|
|
58
|
+
readable: canAccess(target, fs.constants.R_OK),
|
|
59
|
+
writable: canAccess(target, fs.constants.W_OK),
|
|
60
|
+
unreachable: false,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The first directory on this path the runner cannot enter.
|
|
65
|
+
*
|
|
66
|
+
* «Permission denied» on `/srv/apps/shop` is usually not about `shop` at all —
|
|
67
|
+
* it is about `/srv/apps`, and naming the wrong one sends the person to chmod
|
|
68
|
+
* a directory that was never the problem.
|
|
69
|
+
*/
|
|
70
|
+
export function firstUnreachableAncestor(target) {
|
|
71
|
+
const parts = path.resolve(target).split(path.sep).filter(Boolean);
|
|
72
|
+
let current = path.sep;
|
|
73
|
+
for (const part of parts) {
|
|
74
|
+
current = path.join(current, part);
|
|
75
|
+
try {
|
|
76
|
+
fs.statSync(current);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
const code = error.code;
|
|
80
|
+
// ENOENT here means the path simply ends — not a permission problem.
|
|
81
|
+
return code === 'ENOENT' || code === 'ENOTDIR' ? null : current;
|
|
82
|
+
}
|
|
83
|
+
if (!canAccess(current, fs.constants.X_OK))
|
|
84
|
+
return current;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Git refuses to work in a repository owned by somebody else — since 2022, and
|
|
90
|
+
* with no exception for root. So the "obvious" fix for a dedicated user (chown
|
|
91
|
+
* the project to it) breaks git for the person who was committing there before,
|
|
92
|
+
* and the exception has to be added on BOTH sides.
|
|
93
|
+
*/
|
|
94
|
+
export function safeDirectoryCommand(repoPath) {
|
|
95
|
+
return `git config --global --add safe.directory ${repoPath}`;
|
|
96
|
+
}
|
|
97
|
+
export function looksLikeDubiousOwnership(message) {
|
|
98
|
+
return /dubious ownership|safe\.directory/i.test(message);
|
|
99
|
+
}
|
|
100
|
+
/** Is this path already excused in the current user's git config? */
|
|
101
|
+
export async function hasSafeDirectory(repoPath) {
|
|
102
|
+
try {
|
|
103
|
+
const { stdout } = await execFileAsync('git', ['config', '--global', '--get-all', 'safe.directory'], { timeout: 10_000 });
|
|
104
|
+
const entries = stdout.split('\n').map((line) => line.trim());
|
|
105
|
+
return entries.includes(repoPath) || entries.includes('*');
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// No git config at all (exit 1) — nothing is excused.
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export async function addSafeDirectory(repoPath) {
|
|
113
|
+
await execFileAsync('git', ['config', '--global', '--add', 'safe.directory', repoPath], {
|
|
114
|
+
timeout: 10_000,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Count MCP servers in the CLI's own config file, by scope.
|
|
119
|
+
*
|
|
120
|
+
* Reads `~/.claude.json` directly rather than shelling out to `claude mcp
|
|
121
|
+
* list`: this is a diagnostic that must work when the CLI is missing, and it
|
|
122
|
+
* must not spend ~300 MB and a second of a doctor run to answer.
|
|
123
|
+
*/
|
|
124
|
+
function countMcpServers(home) {
|
|
125
|
+
try {
|
|
126
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(home, '.claude.json'), 'utf8'));
|
|
127
|
+
const user = Object.keys(parsed.mcpServers ?? {}).length;
|
|
128
|
+
let project = 0;
|
|
129
|
+
for (const entry of Object.values(parsed.projects ?? {})) {
|
|
130
|
+
project += Object.keys(entry?.mcpServers ?? {}).length;
|
|
131
|
+
}
|
|
132
|
+
return { user, project };
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return { user: 0, project: 0 };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function countAllowRules(file) {
|
|
139
|
+
try {
|
|
140
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
141
|
+
const allow = parsed.permissions?.allow;
|
|
142
|
+
return Array.isArray(allow) ? allow.length : 0;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function countCommands(dir) {
|
|
149
|
+
let total = 0;
|
|
150
|
+
const walk = (current, depth) => {
|
|
151
|
+
if (depth > 3)
|
|
152
|
+
return;
|
|
153
|
+
let entries;
|
|
154
|
+
try {
|
|
155
|
+
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
if (entry.isDirectory())
|
|
162
|
+
walk(path.join(current, entry.name), depth + 1);
|
|
163
|
+
else if (entry.name.endsWith('.md'))
|
|
164
|
+
total += 1;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
walk(dir, 0);
|
|
168
|
+
return total;
|
|
169
|
+
}
|
|
170
|
+
export function agentConfigContour(home = os.homedir()) {
|
|
171
|
+
const claudeDir = path.join(home, '.claude');
|
|
172
|
+
const settings = path.join(claudeDir, 'settings.json');
|
|
173
|
+
const localSettings = path.join(claudeDir, 'settings.local.json');
|
|
174
|
+
const commandsDir = path.join(claudeDir, 'commands');
|
|
175
|
+
const codexDir = path.join(home, '.codex');
|
|
176
|
+
const mcp = countMcpServers(home);
|
|
177
|
+
return {
|
|
178
|
+
home,
|
|
179
|
+
claudeDir: fs.existsSync(claudeDir),
|
|
180
|
+
settings: fs.existsSync(settings),
|
|
181
|
+
localSettings: fs.existsSync(localSettings),
|
|
182
|
+
allowRules: fs.existsSync(localSettings)
|
|
183
|
+
? countAllowRules(localSettings)
|
|
184
|
+
: fs.existsSync(settings)
|
|
185
|
+
? countAllowRules(settings)
|
|
186
|
+
: null,
|
|
187
|
+
commands: fs.existsSync(commandsDir) ? countCommands(commandsDir) : 0,
|
|
188
|
+
plugins: fs.existsSync(path.join(claudeDir, 'plugins')),
|
|
189
|
+
codexDir: fs.existsSync(codexDir),
|
|
190
|
+
codexConfig: fs.existsSync(path.join(codexDir, 'config.toml')),
|
|
191
|
+
mcpUserScope: mcp.user,
|
|
192
|
+
mcpProjectScope: mcp.project,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Another user's home that already has an agent set up.
|
|
197
|
+
*
|
|
198
|
+
* Only reported, never copied: a copied OAuth credential means two accounts
|
|
199
|
+
* share one refresh token, and a rotation in either one silently invalidates
|
|
200
|
+
* the other. Signing in as the runner's own user is the clean answer; the copy
|
|
201
|
+
* is the fast one, and the person choosing between them deserves to be told
|
|
202
|
+
* which is which.
|
|
203
|
+
*/
|
|
204
|
+
export function otherHomeWithAgents(me = runnerIdentity()) {
|
|
205
|
+
const candidates = me.isRoot ? [] : ['/root'];
|
|
206
|
+
for (const home of candidates) {
|
|
207
|
+
if (home === me.home)
|
|
208
|
+
continue;
|
|
209
|
+
if (fs.existsSync(path.join(home, '.claude')) || fs.existsSync(path.join(home, '.codex'))) {
|
|
210
|
+
return home;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* `systemctl --user` talks over a per-user D-Bus socket, and finds it through
|
|
217
|
+
* `XDG_RUNTIME_DIR`. Under `sudo -iu <user>` that variable is not set, and the
|
|
218
|
+
* failure is worse than an error: it prints «Failed to connect to bus» and
|
|
219
|
+
* exits **0**, so a health check reads it as success.
|
|
220
|
+
*/
|
|
221
|
+
export function systemdUserEnv() {
|
|
222
|
+
const env = { ...process.env };
|
|
223
|
+
if (!env['XDG_RUNTIME_DIR']) {
|
|
224
|
+
const uid = runnerIdentity().uid;
|
|
225
|
+
if (uid >= 0)
|
|
226
|
+
env['XDG_RUNTIME_DIR'] = `/run/user/${uid}`;
|
|
227
|
+
}
|
|
228
|
+
if (!env['DBUS_SESSION_BUS_ADDRESS'] && env['XDG_RUNTIME_DIR']) {
|
|
229
|
+
env['DBUS_SESSION_BUS_ADDRESS'] = `unix:path=${env['XDG_RUNTIME_DIR']}/bus`;
|
|
230
|
+
}
|
|
231
|
+
return env;
|
|
232
|
+
}
|
|
233
|
+
/** True when the user bus is actually reachable — `systemctl --user` lies with exit 0. */
|
|
234
|
+
export async function systemdUserBusReachable() {
|
|
235
|
+
const dir = systemdUserEnv()['XDG_RUNTIME_DIR'];
|
|
236
|
+
if (!dir || !fs.existsSync(path.join(dir, 'bus')))
|
|
237
|
+
return false;
|
|
238
|
+
try {
|
|
239
|
+
const { stdout, stderr } = await execFileAsync('systemctl', ['--user', 'is-system-running'], {
|
|
240
|
+
timeout: 10_000,
|
|
241
|
+
env: systemdUserEnv(),
|
|
242
|
+
});
|
|
243
|
+
return !/Failed to connect to bus/i.test(`${stdout}${stderr}`);
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
// A non-zero exit is normal here (`degraded`, `starting`); only a bus
|
|
247
|
+
// failure means we could not talk to systemd at all.
|
|
248
|
+
const text = String(error?.stderr ?? error);
|
|
249
|
+
return !/Failed to connect to bus|No medium found/i.test(text);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** The command form that works under `sudo -iu <user>` — printed in hints. */
|
|
253
|
+
export function systemctlHint(args) {
|
|
254
|
+
const me = runnerIdentity();
|
|
255
|
+
if (me.isRoot)
|
|
256
|
+
return `systemctl --user ${args}`;
|
|
257
|
+
return `sudo -iu ${me.user} env XDG_RUNTIME_DIR=/run/user/${me.uid} DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${me.uid}/bus systemctl --user ${args}`;
|
|
258
|
+
}
|
|
259
|
+
// ─── Which project directories this machine actually works in ────────
|
|
260
|
+
/**
|
|
261
|
+
* Remembered from binding and from session starts, so `doctor` can check the
|
|
262
|
+
* permissions of real projects instead of asking the person to name them.
|
|
263
|
+
* Best-effort on purpose: a runner that cannot write its own state directory
|
|
264
|
+
* has bigger problems than a diagnostic list, and none of them should turn a
|
|
265
|
+
* session start into an error.
|
|
266
|
+
*/
|
|
267
|
+
export function rememberWorkspacePath(workspacePath) {
|
|
268
|
+
try {
|
|
269
|
+
const file = knownWorkspacesPath();
|
|
270
|
+
const known = knownWorkspacePaths();
|
|
271
|
+
if (known.includes(workspacePath))
|
|
272
|
+
return;
|
|
273
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
274
|
+
// Newest last, capped: this is a diagnostic aid, not a registry.
|
|
275
|
+
const next = [...known, workspacePath].slice(-32);
|
|
276
|
+
fs.writeFileSync(file, JSON.stringify(next, null, 2), { mode: 0o600 });
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
/* diagnostics only */
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
export function knownWorkspacePaths() {
|
|
283
|
+
try {
|
|
284
|
+
const parsed = JSON.parse(fs.readFileSync(knownWorkspacesPath(), 'utf8'));
|
|
285
|
+
if (!Array.isArray(parsed))
|
|
286
|
+
return [];
|
|
287
|
+
return parsed.filter((entry) => typeof entry === 'string' && entry.length > 0);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
export function whichExecutable(name) {
|
|
294
|
+
for (const dir of (process.env['PATH'] ?? '').split(path.delimiter).filter(Boolean)) {
|
|
295
|
+
const candidate = path.join(dir, name);
|
|
296
|
+
try {
|
|
297
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
298
|
+
return candidate;
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
/* keep looking */
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Node, as THIS user sees it.
|
|
308
|
+
*
|
|
309
|
+
* A dedicated user does not automatically inherit a Node installed for
|
|
310
|
+
* somebody else — fnm, nvm and a root-only prefix are all per-user by design.
|
|
311
|
+
* The runner itself is running, so node clearly exists somewhere; the question
|
|
312
|
+
* this answers is whether it is on the daemon user's own PATH, because that is
|
|
313
|
+
* what agent tooling and `npm install -g` will look at.
|
|
314
|
+
*/
|
|
315
|
+
export async function nodeCheck() {
|
|
316
|
+
const found = whichExecutable('node');
|
|
317
|
+
if (!found)
|
|
318
|
+
return { path: null, problem: 'node is not on this user’s PATH' };
|
|
319
|
+
try {
|
|
320
|
+
const { stdout } = await execFileAsync(found, ['--version'], { timeout: 10_000 });
|
|
321
|
+
const version = stdout.trim();
|
|
322
|
+
const major = Number(version.replace(/^v/, '').split('.')[0]);
|
|
323
|
+
return {
|
|
324
|
+
path: found,
|
|
325
|
+
version,
|
|
326
|
+
...(Number.isFinite(major) && major < 20 ? { problem: 'Node 20 or newer is required' } : {}),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
catch (error) {
|
|
330
|
+
return { path: found, problem: String(error instanceof Error ? error.message : error) };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Docker, as THIS user sees it.
|
|
335
|
+
*
|
|
336
|
+
* Reported rather than judged: plenty of projects never touch it. But when the
|
|
337
|
+
* project's own workflow is `docker compose`, a dedicated user without access
|
|
338
|
+
* to the socket produces a session that fails on its first command, and the
|
|
339
|
+
* error will be about a socket rather than about a group nobody was added to.
|
|
340
|
+
*
|
|
341
|
+
* Worth stating where it is stated: being in the `docker` group is equivalent
|
|
342
|
+
* to root on this machine. That is a fact for the owner to accept knowingly.
|
|
343
|
+
*/
|
|
344
|
+
export async function dockerCheck() {
|
|
345
|
+
const found = whichExecutable('docker');
|
|
346
|
+
if (!found)
|
|
347
|
+
return { path: null };
|
|
348
|
+
const socket = '/var/run/docker.sock';
|
|
349
|
+
if (fs.existsSync(socket) && !canAccess(socket, fs.constants.R_OK | fs.constants.W_OK)) {
|
|
350
|
+
const me = runnerIdentity();
|
|
351
|
+
return {
|
|
352
|
+
path: found,
|
|
353
|
+
problem: `${me.user} cannot use the docker socket (membership of the docker group is equivalent to root here)`,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
await execFileAsync(found, ['info', '--format', '{{.ServerVersion}}'], { timeout: 15_000 });
|
|
358
|
+
return { path: found };
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
return {
|
|
362
|
+
path: found,
|
|
363
|
+
problem: `docker is installed but did not answer: ${String(error instanceof Error ? error.message : error).slice(0, 160)}`,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Does the service survive a logout?
|
|
369
|
+
*
|
|
370
|
+
* `loginctl enable-linger` is what keeps a user's systemd services running with
|
|
371
|
+
* nobody logged in. `install-service` turns it on, but a unit installed by hand
|
|
372
|
+
* — or a user created afterwards — can miss it, and the failure looks like
|
|
373
|
+
* «the server goes offline whenever I close the terminal».
|
|
374
|
+
*/
|
|
375
|
+
export async function lingerEnabled() {
|
|
376
|
+
const me = runnerIdentity();
|
|
377
|
+
try {
|
|
378
|
+
const { stdout } = await execFileAsync('loginctl', ['show-user', me.user, '--property=Linger'], { timeout: 10_000 });
|
|
379
|
+
return stdout.trim().endsWith('=yes');
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
// No loginctl, or the user has no session recorded — unknown, not false.
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Make the agent CLIs findable, without taking anything away.
|
|
388
|
+
*
|
|
389
|
+
* A systemd user service starts with the manager's PATH, which is the
|
|
390
|
+
* distribution default — `/usr/bin` and friends. Both agent CLIs are commonly
|
|
391
|
+
* installed somewhere else: `~/.local/bin` for a per-user install, and a
|
|
392
|
+
* node managed by fnm/nvm/volta lives under its own version directory. When
|
|
393
|
+
* `codex` sits there, the runner scans PATH, does not find it, and reports to
|
|
394
|
+
* the dashboard that this machine has no Codex at all — the agent the person
|
|
395
|
+
* installed simply never appears.
|
|
396
|
+
*
|
|
397
|
+
* Deliberately APPEND-ONLY, and deliberately in the process rather than in the
|
|
398
|
+
* unit file. Writing `Environment=PATH=…` into the unit would REPLACE whatever
|
|
399
|
+
* systemd gives the service today (`/snap/bin`, anything set through
|
|
400
|
+
* `environment.d`), trading something that works for something that is
|
|
401
|
+
* missing. Here nothing can be lost: entries are only added when they are
|
|
402
|
+
* absent and the directory actually exists.
|
|
403
|
+
*
|
|
404
|
+
* Returns what it added, so the caller can say so once at startup.
|
|
405
|
+
*/
|
|
406
|
+
export function ensureAgentPath() {
|
|
407
|
+
const entries = (process.env['PATH'] ?? '').split(path.delimiter).filter(Boolean);
|
|
408
|
+
const added = [];
|
|
409
|
+
for (const candidate of [
|
|
410
|
+
path.join(os.homedir(), '.local', 'bin'),
|
|
411
|
+
// The directory of the node running us — under fnm/nvm the agent CLIs and
|
|
412
|
+
// other globally installed tools sit next to it.
|
|
413
|
+
path.dirname(process.execPath),
|
|
414
|
+
]) {
|
|
415
|
+
if (!candidate || entries.includes(candidate))
|
|
416
|
+
continue;
|
|
417
|
+
let usable;
|
|
418
|
+
try {
|
|
419
|
+
usable = fs.statSync(candidate).isDirectory();
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
usable = false;
|
|
423
|
+
}
|
|
424
|
+
if (!usable)
|
|
425
|
+
continue;
|
|
426
|
+
entries.push(candidate);
|
|
427
|
+
added.push(candidate);
|
|
428
|
+
}
|
|
429
|
+
if (added.length > 0)
|
|
430
|
+
process.env['PATH'] = entries.join(path.delimiter);
|
|
431
|
+
return added;
|
|
432
|
+
}
|
|
433
|
+
//# sourceMappingURL=environment.js.map
|
package/dist/git.d.ts
CHANGED
|
@@ -5,6 +5,16 @@ export interface PathValidation {
|
|
|
5
5
|
branch?: string;
|
|
6
6
|
error?: string;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Can this runner actually work in this directory — as the user it runs as?
|
|
10
|
+
*
|
|
11
|
+
* Everything here answers with the fix rather than the symptom. Binding a
|
|
12
|
+
* project is the moment the two legitimate install choices (root / dedicated
|
|
13
|
+
* user) start to differ, and until now the second one failed by forwarding
|
|
14
|
+
* git's own words to a dashboard the person may have no shell behind:
|
|
15
|
+
* «git check failed: fatal: detected dubious ownership in repository at
|
|
16
|
+
* '/opt/ids'». That sentence is true and unactionable.
|
|
17
|
+
*/
|
|
8
18
|
export declare function validateWorkspacePath(workspacePath: string): Promise<PathValidation>;
|
|
9
19
|
export declare function sessionShortId(sessionId: string): string;
|
|
10
20
|
export declare function sessionWorktreePath(sessionId: string): string;
|