@veda-hq/niyam 0.6.22 → 0.6.24
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/README.md +130 -13
- package/dist/agent.js +33 -12
- package/dist/board-audit.js +177 -0
- package/dist/board.js +30 -4
- package/dist/cloud-key.js +219 -0
- package/dist/cloud.js +122 -4
- package/dist/doctor.js +77 -16
- package/dist/onboard.js +152 -28
- package/dist/pr-autocreate.js +87 -12
- package/dist/run.js +23 -4
- package/dist/secrets.js +21 -0
- package/dist/session-start.js +31 -4
- package/dist/work.js +78 -4
- package/package.json +1 -1
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { API, account, agentFrom, checkToken, keyBody, signingKeys, tokenInfo } from './agent.js';
|
|
2
|
+
import { SIGNING_KEY_VAR } from './cloud.js';
|
|
3
|
+
import { CLAUDE_CODE_WEB } from './onboard.js';
|
|
4
|
+
import { agentSession } from './push-identity.js';
|
|
5
|
+
import { tokenStore, unsupported } from './token-store.js';
|
|
6
|
+
import { GITHUB_WEB } from './host.js';
|
|
7
|
+
/** The title the cloud signing keys have on GitHub, so the agent's can be told apart from a Mac's. */
|
|
8
|
+
export const CLOUD_KEY_TITLE = 'niyam cloud signing';
|
|
9
|
+
/** Where the agent's signing keys are deleted, signed in as the agent. */
|
|
10
|
+
const KEYS_PAGE = `${GITHUB_WEB}/settings/keys`;
|
|
11
|
+
/** cloud-key's arguments: the agent, then `--yes`. */
|
|
12
|
+
export function cloudKeyArgs(args) {
|
|
13
|
+
let named;
|
|
14
|
+
let yes = false;
|
|
15
|
+
for (const arg of args) {
|
|
16
|
+
if (arg === '--yes')
|
|
17
|
+
yes = true;
|
|
18
|
+
else if (arg.startsWith('--'))
|
|
19
|
+
throw new Error(`unknown option ${arg}`);
|
|
20
|
+
else if (named === undefined)
|
|
21
|
+
named = arg;
|
|
22
|
+
else
|
|
23
|
+
throw new Error(`unexpected argument "${arg}"`);
|
|
24
|
+
}
|
|
25
|
+
return { ...(named === undefined ? {} : { named }), ...(yes ? { yes: true } : {}) };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Sets up the agent's cloud signing key from the owner's Mac (#405): a new
|
|
29
|
+
* ed25519 key, in a private temporary folder, registered as the agent's
|
|
30
|
+
* signing key with the token `machine` stored, titled CLOUD_KEY_TITLE; once
|
|
31
|
+
* GitHub lists it, `NIYAM_SIGNING_KEY=<the private key, base64>` goes on the
|
|
32
|
+
* clipboard, for the cloud environment's variables, and the folder is
|
|
33
|
+
* removed, a Ctrl-C included. The key is never printed. When the clipboard
|
|
34
|
+
* does not take it, the key just registered is deleted again, since no one
|
|
35
|
+
* holds it. It names the agent's other cloud signing keys, for the owner to
|
|
36
|
+
* delete one no environment uses; it deletes none itself. Refuses in an agent
|
|
37
|
+
* session or a cloud session, where the token is not the owner's to use, and
|
|
38
|
+
* asks once before it changes anything (`--yes` skips the question).
|
|
39
|
+
*/
|
|
40
|
+
export async function cloudKey(sys, io, args = []) {
|
|
41
|
+
const { named, yes } = cloudKeyArgs(args);
|
|
42
|
+
const refusal = cloudKeyRefusal(sys);
|
|
43
|
+
const store = tokenStore(sys);
|
|
44
|
+
if (refusal !== undefined || !store) {
|
|
45
|
+
io.err(`cloud-key: ${refusal ?? unsupported(sys)}`);
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
const { login: agent } = await account(sys, agentFrom(sys, named));
|
|
49
|
+
const token = store.read(sys, agent);
|
|
50
|
+
if (!token) {
|
|
51
|
+
io.err(`cloud-key: ${store.where} has no ${agent} token; run niyam machine ${agent} first`);
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
const check = checkToken(token, await tokenInfo(sys, token), agent, sys.now());
|
|
55
|
+
if (check.problem !== undefined) {
|
|
56
|
+
io.err(`cloud-key: the stored ${agent} token cannot be used: ${check.problem}; run niyam machine ${agent}`);
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
const before = await listedKeys(sys, agent, token);
|
|
60
|
+
if ('problem' in before) {
|
|
61
|
+
io.err(`cloud-key: ${before.problem}`);
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
io.out(`cloud-key: a new signing key for ${agent}'s Claude cloud sessions, registered on GitHub as ${agent} ("${CLOUD_KEY_TITLE}"),`);
|
|
65
|
+
io.out(` its private key put on the clipboard as ${SIGNING_KEY_VAR}=…, for a cloud environment's variables. Nothing is printed.`);
|
|
66
|
+
const others = before.keys.filter((one) => one.title === CLOUD_KEY_TITLE);
|
|
67
|
+
if (!yes) {
|
|
68
|
+
if (!sys.tty) {
|
|
69
|
+
io.err('cloud-key: no terminal to ask in; pass --yes to go ahead without asking');
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
if (!(await sys.choose('Go ahead?'))) {
|
|
73
|
+
io.out('Nothing changed.');
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return makeCloudKey(sys, io, agent, token, { others, paste: true });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Why cloud-key cannot run here, or undefined: in an agent session or a cloud
|
|
81
|
+
* session the token is not the owner's to use, and a machine with no token
|
|
82
|
+
* store holds no token to register the key with.
|
|
83
|
+
*/
|
|
84
|
+
export function cloudKeyRefusal(sys) {
|
|
85
|
+
if (agentSession(sys) || sys.env.CLAUDE_CODE_REMOTE === 'true')
|
|
86
|
+
return 'run it in your own terminal on your Mac, not in an agent session; it registers a key as the agent';
|
|
87
|
+
return tokenStore(sys) ? undefined : unsupported(sys);
|
|
88
|
+
}
|
|
89
|
+
/** What the owner does with the line on the clipboard: paste it into `where`'s variables, and what it does then. */
|
|
90
|
+
export function pasteLines(agent, where = "the cloud environment's") {
|
|
91
|
+
return [
|
|
92
|
+
`Paste it as one line into ${where} variables: ${CLAUDE_CODE_WEB} → the environment's settings.`,
|
|
93
|
+
`A new session there makes ${agent}'s commits its own; its first niyam line says so.`,
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Makes the agent's cloud signing key in a private temporary folder,
|
|
98
|
+
* registers it, puts its line on the clipboard, and removes the folder, a
|
|
99
|
+
* Ctrl-C included; each step one line, the key never printed. `others` are
|
|
100
|
+
* the agent's cloud signing keys GitHub already lists, named for the owner
|
|
101
|
+
* to delete once unused; `paste` says what to do with the line, for a caller
|
|
102
|
+
* that does not say it itself. 0 once the line is on the clipboard.
|
|
103
|
+
*/
|
|
104
|
+
export async function makeCloudKey(sys, io, agent, token, { others = [], paste = false } = {}) {
|
|
105
|
+
const dir = sys.tempDir();
|
|
106
|
+
let removed = false;
|
|
107
|
+
const cleanUp = () => {
|
|
108
|
+
if (!removed) {
|
|
109
|
+
try {
|
|
110
|
+
sys.remove(dir);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// fail-open: said below, with the command that removes it.
|
|
114
|
+
}
|
|
115
|
+
removed = !sys.exists(dir);
|
|
116
|
+
}
|
|
117
|
+
return removed;
|
|
118
|
+
};
|
|
119
|
+
let interrupted = false;
|
|
120
|
+
const off = sys.onInterrupt(() => {
|
|
121
|
+
interrupted = true;
|
|
122
|
+
cleanUp();
|
|
123
|
+
});
|
|
124
|
+
let code = 1;
|
|
125
|
+
try {
|
|
126
|
+
code = await makeAndRegister(sys, io, { agent, token, key: `${dir}/key`, others, paste }, () => interrupted);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
off();
|
|
130
|
+
if (cleanUp()) {
|
|
131
|
+
io.out('✓ deleted the key files');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
io.out(`✗ could not delete ${dir}, which holds the private key → rm -rf ${dir}`);
|
|
135
|
+
code = 1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return code;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Makes the key at `key`, registers it, and puts it on the clipboard; each
|
|
142
|
+
* step one line. The key is read before it is registered, so a Ctrl-C that
|
|
143
|
+
* removes the files leaves it to delete from GitHub again, which it is.
|
|
144
|
+
*/
|
|
145
|
+
async function makeAndRegister(sys, io, { agent, token, key, others, paste }, interrupted) {
|
|
146
|
+
const made = sys.exec('ssh-keygen', ['-q', '-t', 'ed25519', '-C', `${agent} cloud signing`, '-N', '', '-f', key], { input: '' });
|
|
147
|
+
if (interrupted())
|
|
148
|
+
return stopped(io);
|
|
149
|
+
if (made.code !== 0) {
|
|
150
|
+
io.out(`✗ ssh-keygen failed: ${made.stderr.trim() || `exit ${made.code}`}`);
|
|
151
|
+
return 1;
|
|
152
|
+
}
|
|
153
|
+
const publicKey = sys.read(`${key}.pub`).trim();
|
|
154
|
+
const line = `${SIGNING_KEY_VAR}=${Buffer.from(`${sys.read(key).trim()}\n`).toString('base64')}`;
|
|
155
|
+
const posted = await sys.http(`${API}/user/ssh_signing_keys`, { method: 'POST', token, body: { title: CLOUD_KEY_TITLE, key: publicKey } });
|
|
156
|
+
const id = posted.json?.id;
|
|
157
|
+
if (posted.status !== 201 || typeof id !== 'number') {
|
|
158
|
+
io.out(`✗ GitHub did not register the key as ${agent}'s signing key (HTTP ${posted.status}); nothing is on the clipboard`);
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
let listed = false;
|
|
162
|
+
try {
|
|
163
|
+
listed = (await signingKeys(sys, agent, token)).includes(keyBody(publicKey));
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
// fail-open: a list GitHub does not give counts as not listing it, and the key is deleted again.
|
|
167
|
+
}
|
|
168
|
+
if (interrupted()) {
|
|
169
|
+
await unregister(sys, io, token, id, 'stopped by Ctrl-C');
|
|
170
|
+
return 130;
|
|
171
|
+
}
|
|
172
|
+
if (!listed) {
|
|
173
|
+
await unregister(sys, io, token, id, 'GitHub does not list it');
|
|
174
|
+
return 1;
|
|
175
|
+
}
|
|
176
|
+
io.out(`✓ registered as ${agent}'s signing key "${CLOUD_KEY_TITLE}" (id ${id})`);
|
|
177
|
+
const copied = sys.exec('/usr/bin/pbcopy', [], { input: line });
|
|
178
|
+
if (copied.code !== 0) {
|
|
179
|
+
await unregister(sys, io, token, id, `the clipboard did not take the key (${copied.stderr.trim() || `exit ${copied.code}`})`);
|
|
180
|
+
return 1;
|
|
181
|
+
}
|
|
182
|
+
io.out(`✓ ${SIGNING_KEY_VAR}=… is on the clipboard`);
|
|
183
|
+
if (paste)
|
|
184
|
+
for (const line of pasteLines(agent))
|
|
185
|
+
io.out(` ${line}`);
|
|
186
|
+
for (const other of others) {
|
|
187
|
+
io.out(`▲ ${agent} has another cloud signing key, added ${other.created.slice(0, 10)} (id ${other.id}); delete it once no environment uses it → ${KEYS_PAGE}, signed in as ${agent}`);
|
|
188
|
+
}
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
function stopped(io) {
|
|
192
|
+
io.out('✗ stopped by Ctrl-C; nothing registered, and nothing on the clipboard');
|
|
193
|
+
return 130;
|
|
194
|
+
}
|
|
195
|
+
/** Deletes the key just registered, which no one holds, and says why. */
|
|
196
|
+
async function unregister(sys, io, token, id, why) {
|
|
197
|
+
let status = 0;
|
|
198
|
+
try {
|
|
199
|
+
({ status } = await sys.http(`${API}/user/ssh_signing_keys/${id}`, { method: 'DELETE', token }));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// fail-open: said below, with where to delete the key by hand.
|
|
203
|
+
}
|
|
204
|
+
io.out(status === 204
|
|
205
|
+
? `✗ ${why}, so the key was deleted from GitHub again; nothing is on the clipboard`
|
|
206
|
+
: `✗ ${why}, and deleting the key from GitHub again failed (HTTP ${status}) → delete signing key ${id} at ${KEYS_PAGE}`);
|
|
207
|
+
}
|
|
208
|
+
/** The agent's signing keys as its own list gives them, with their titles, or why they cannot be had. */
|
|
209
|
+
export async function listedKeys(sys, agent, token) {
|
|
210
|
+
const { status, json } = await sys.http(`${API}/user/ssh_signing_keys?per_page=100`, { token });
|
|
211
|
+
if (status !== 200 || !Array.isArray(json))
|
|
212
|
+
return { problem: `GitHub answered HTTP ${status} when asked for ${agent}'s signing keys` };
|
|
213
|
+
return {
|
|
214
|
+
keys: json.flatMap((entry) => {
|
|
215
|
+
const { id, title, created_at: created } = (entry ?? {});
|
|
216
|
+
return typeof id === 'number' ? [{ id, title: typeof title === 'string' ? title : '', created: typeof created === 'string' ? created : '' }] : [];
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
}
|
package/dist/cloud.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { API, isNoToken } from './agent.js';
|
|
1
|
+
import { API, BASE_ENV, authorVars, configVars, isNoToken, keyBody, noreplyEmail, pathsFor, signingConfig } from './agent.js';
|
|
2
2
|
import { CLAUDE_CODE_WEB, SETUP_FIRST, workflowPins } from './onboard.js';
|
|
3
3
|
import { PRODUCT, tarballName } from './names.js';
|
|
4
4
|
import { CLOUD_SETUP_PATH, WORKFLOW_PATH } from './onboard-pull.js';
|
|
@@ -14,17 +14,135 @@ export function cloudToken(sys) {
|
|
|
14
14
|
const token = sys.env.GH_TOKEN;
|
|
15
15
|
return token && !isNoToken(token) ? token : PROXY_TOKEN;
|
|
16
16
|
}
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* The account a cloud session reaches GitHub as, through its proxy, or why it
|
|
19
|
+
* cannot be told; with the account's id from the same answer, when it names
|
|
20
|
+
* a valid one, for the agent's no-reply address.
|
|
21
|
+
*/
|
|
18
22
|
export async function cloudLogin(sys) {
|
|
19
23
|
try {
|
|
20
24
|
const { status, json } = await sys.http(`${API}/user`, { token: cloudToken(sys) });
|
|
21
|
-
const login = json
|
|
22
|
-
|
|
25
|
+
const { login, id } = json ?? {};
|
|
26
|
+
if (status !== 200 || typeof login !== 'string')
|
|
27
|
+
return { problem: `GitHub answered HTTP ${status} when asked who this session is` };
|
|
28
|
+
return typeof id === 'number' && Number.isSafeInteger(id) && id > 0 ? { login, id } : { login };
|
|
23
29
|
}
|
|
24
30
|
catch (error) {
|
|
25
31
|
return { problem: `GitHub could not be asked who this session is (${error instanceof Error ? error.message : String(error)})` };
|
|
26
32
|
}
|
|
27
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* The cloud environment's variable that holds the agent's cloud signing key:
|
|
36
|
+
* an OpenSSH private key, base64 on one line or as is. A key of its own, not
|
|
37
|
+
* a Mac's, registered as one of the agent's signing keys, so it is revoked
|
|
38
|
+
* on its own.
|
|
39
|
+
*/
|
|
40
|
+
export const SIGNING_KEY_VAR = 'NIYAM_SIGNING_KEY';
|
|
41
|
+
/** The message of the probe commit `cloudSigning` signs. */
|
|
42
|
+
const PROBE_MESSAGE = 'niyam: a probe of the agent\'s signing, never on a branch';
|
|
43
|
+
/**
|
|
44
|
+
* What makes a cloud session's commits the agent's: its author and committer
|
|
45
|
+
* variables, with the address of the id GitHub gave with the login, and SSH
|
|
46
|
+
* signing with the key in SIGNING_KEY_VAR, over the platform's own signing
|
|
47
|
+
* (Claude's, through a program of its own: `gpg.ssh.program` goes back to
|
|
48
|
+
* ssh-keygen). All of them or none, since a commit that claims the agent
|
|
49
|
+
* must be Verified as the agent (PR identity) and a signing setup that fails
|
|
50
|
+
* breaks every commit: each is given only when the key is one GitHub lists
|
|
51
|
+
* for the agent and a probe commit signed with exactly these settings, in
|
|
52
|
+
* the clone at `root`, verifies as that key's. The probe is an object on no
|
|
53
|
+
* branch, which git's housekeeping drops. Otherwise why not, and the session
|
|
54
|
+
* keeps the cloud image's identity.
|
|
55
|
+
*/
|
|
56
|
+
export async function cloudSigning(sys, root, login, id) {
|
|
57
|
+
const given = sys.env[SIGNING_KEY_VAR]?.trim();
|
|
58
|
+
if (!given)
|
|
59
|
+
return { problem: `the cloud environment's variables have no ${SIGNING_KEY_VAR}, ${login}'s cloud signing key (niyam's README, "Claude cloud sessions", says how to add one)` };
|
|
60
|
+
if (id === undefined)
|
|
61
|
+
return { problem: `GitHub named no account id for ${login}` };
|
|
62
|
+
const text = (given.includes(PEM_BEGIN) ? given.replaceAll('\\n', '\n') : Buffer.from(given, 'base64').toString('utf8')).trim();
|
|
63
|
+
if (!text.startsWith(`${PEM_BEGIN}\n`) || !text.endsWith(`\n${PEM_END}`)) {
|
|
64
|
+
return { problem: `${SIGNING_KEY_VAR} is not an OpenSSH private key, base64 on one line or as is` };
|
|
65
|
+
}
|
|
66
|
+
const { dir, key } = pathsFor(sys.home, login);
|
|
67
|
+
const pub = `${key}.pub`;
|
|
68
|
+
const signers = `${dir}/allowed-signers`;
|
|
69
|
+
const email = noreplyEmail(id, login);
|
|
70
|
+
try {
|
|
71
|
+
sys.mkdir(dir);
|
|
72
|
+
sys.mkdir(key.slice(0, key.lastIndexOf('/')));
|
|
73
|
+
sys.write(key, `${text}\n`, 0o600);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
// fail-open: the session keeps the image's identity, which PR identity passes, and says why.
|
|
77
|
+
return { problem: `writing ${SIGNING_KEY_VAR} to ${key} failed (${messageOf(error)})` };
|
|
78
|
+
}
|
|
79
|
+
const derived = sys.exec('ssh-keygen', ['-y', '-f', key], { input: '' });
|
|
80
|
+
if (derived.code !== 0)
|
|
81
|
+
return { problem: `ssh-keygen cannot read ${SIGNING_KEY_VAR} (${lastLine(derived.stderr) || `exit ${derived.code}`})` };
|
|
82
|
+
const body = keyBody(derived.stdout);
|
|
83
|
+
const listed = await listedSigningKeys(sys, login);
|
|
84
|
+
if ('problem' in listed)
|
|
85
|
+
return listed;
|
|
86
|
+
if (!listed.keys.includes(body))
|
|
87
|
+
return { problem: `GitHub does not list ${SIGNING_KEY_VAR}'s public half as one of ${login}'s signing keys` };
|
|
88
|
+
try {
|
|
89
|
+
sys.write(pub, `${body} ${login} cloud signing\n`, 0o644);
|
|
90
|
+
sys.write(signers, `${email} namespaces="git" ${body}\n`, 0o644);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
// fail-open: the session keeps the image's identity, which PR identity passes, and says why.
|
|
94
|
+
return { problem: `writing ${pub} failed (${messageOf(error)})` };
|
|
95
|
+
}
|
|
96
|
+
const vars = [...authorVars(login, id), ...configVars([['gpg.ssh.program', 'ssh-keygen'], ...signingConfig(pub)])];
|
|
97
|
+
const probe = probeSignature(sys, root, vars, signers);
|
|
98
|
+
return probe ? { problem: `a probe commit signed with ${SIGNING_KEY_VAR} did not verify as that key (${probe})` } : { vars };
|
|
99
|
+
}
|
|
100
|
+
const PEM_BEGIN = '-----BEGIN OPENSSH PRIVATE KEY-----';
|
|
101
|
+
const PEM_END = '-----END OPENSSH PRIVATE KEY-----';
|
|
102
|
+
/** The signing keys GitHub's public list names for `login`, asked through the proxy, or why they cannot be had. */
|
|
103
|
+
async function listedSigningKeys(sys, login) {
|
|
104
|
+
let answer;
|
|
105
|
+
try {
|
|
106
|
+
answer = await sys.http(`${API}/users/${encodeURIComponent(login)}/ssh_signing_keys?per_page=100`, { token: cloudToken(sys) });
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
// fail-open: the session keeps the image's identity, which PR identity passes, and says why.
|
|
110
|
+
return { problem: `GitHub could not be asked for ${login}'s signing keys (${messageOf(error)})` };
|
|
111
|
+
}
|
|
112
|
+
if (answer.status !== 200 || !Array.isArray(answer.json))
|
|
113
|
+
return { problem: `GitHub answered HTTP ${answer.status} when asked for ${login}'s signing keys` };
|
|
114
|
+
return {
|
|
115
|
+
keys: answer.json.flatMap((entry) => {
|
|
116
|
+
const key = entry?.key;
|
|
117
|
+
return typeof key === 'string' ? [keyBody(key)] : [];
|
|
118
|
+
}),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Makes a commit object on no branch with the environment `vars` give, and
|
|
123
|
+
* verifies its signature against `signers`, the agent's key alone: undefined
|
|
124
|
+
* when it verifies, else what failed.
|
|
125
|
+
*/
|
|
126
|
+
function probeSignature(sys, root, vars, signers) {
|
|
127
|
+
// The session's commands get the env file's lines, which unset the lock first: the probe does the same.
|
|
128
|
+
const base = Object.fromEntries(Object.entries(sys.env).filter(([name]) => !(name in BASE_ENV) && name !== SIGNING_KEY_VAR));
|
|
129
|
+
const env = { ...base, ...Object.fromEntries(vars) };
|
|
130
|
+
const tree = sys.exec('git', ['mktree'], { cwd: root, env, input: '' });
|
|
131
|
+
if (tree.code !== 0)
|
|
132
|
+
return `git mktree: ${lastLine(tree.stderr) || `exit ${tree.code}`}`;
|
|
133
|
+
// commit-tree reads no commit.gpgsign: -S signs as `git commit` would, with the key and program the settings name.
|
|
134
|
+
const commit = sys.exec('git', ['commit-tree', '-S', tree.stdout.trim(), '-m', PROBE_MESSAGE], { cwd: root, env, input: '' });
|
|
135
|
+
if (commit.code !== 0)
|
|
136
|
+
return `git commit-tree: ${lastLine(commit.stderr) || `exit ${commit.code}`}`;
|
|
137
|
+
const verified = sys.exec('git', ['-c', `gpg.ssh.allowedSignersFile=${signers}`, 'verify-commit', commit.stdout.trim()], { cwd: root, env, input: '' });
|
|
138
|
+
return verified.code === 0 ? undefined : `git verify-commit: ${lastLine(verified.stderr) || 'no signature by that key'}`;
|
|
139
|
+
}
|
|
140
|
+
function lastLine(text) {
|
|
141
|
+
return text.trim().split('\n').at(-1) ?? '';
|
|
142
|
+
}
|
|
143
|
+
function messageOf(error) {
|
|
144
|
+
return error instanceof Error ? error.message : String(error);
|
|
145
|
+
}
|
|
28
146
|
/** The Node a Claude cloud session runs the CLI with: its image's own is older than the CLI needs. */
|
|
29
147
|
export const CLOUD_NODE = '24.21.0';
|
|
30
148
|
/** The sha256 of CLOUD_NODE's Linux builds, from nodejs.org's SHASUMS256.txt for that release. */
|
package/dist/doctor.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { EXPIRY_WARNING_DAYS, SETTINGS_PATH, account, agentFrom, agentIn, anyLogin, either, hasSettings, loadSettings, repoAt, sameLogin, checkToken, keyBody, keyPairProblem, launcherPath, pathsFor, sessionFileIs, signingKeys, tokenInfo, } from './agent.js';
|
|
2
2
|
import { LOCKED, clientHooks, hooksHittingLock } from './as-agent.js';
|
|
3
|
-
import { FROM_SETUP, cloudLogin, fileFromSetup, ranFromSetup, repoReleases } from './cloud.js';
|
|
3
|
+
import { FROM_SETUP, SIGNING_KEY_VAR, cloudLogin, fileFromSetup, ranFromSetup, repoReleases } from './cloud.js';
|
|
4
|
+
import { GITHUB_NOREPLY } from './host.js';
|
|
4
5
|
import { featuresIn } from './features.js';
|
|
5
6
|
import { typesDrift } from './issue-types.js';
|
|
6
7
|
import { closing, findingLines, heading, painter, tilde } from './layout.js';
|
|
7
8
|
import { exposure, exposureFindings, movedFindings, readDeclined, setAside } from './lockout.js';
|
|
8
9
|
import { CLOUD_SETUP_PATH, HOOKS_PATH, SARATHI_PATH } from './onboard-pull.js';
|
|
9
10
|
import { leftoverDir, leftoverToken } from './machine.js';
|
|
10
|
-
import { GUARD_COMMAND, HOOK_COMMAND, OLDER_GUARD_COMMANDS, OLDER_HOOK_COMMANDS, missingAutoFixRules, sarathiRunner } from './onboard.js';
|
|
11
|
+
import { BOARD_SECRETS, GUARD_COMMAND, HOOK_COMMAND, OLDER_GUARD_COMMANDS, OLDER_HOOK_COMMANDS, missingAutoFixRules, sarathiRunner } from './onboard.js';
|
|
12
|
+
import { secretNames } from './secrets.js';
|
|
11
13
|
import { isOrganisation } from './owner-login.js';
|
|
12
14
|
import { leftBehind } from './owner-step.js';
|
|
13
15
|
import { repoRoot } from './session-start.js';
|
|
@@ -88,6 +90,7 @@ export async function doctor(sys, io, named) {
|
|
|
88
90
|
return registered ? { mark: '✓', text: 'signing key registered' } : { mark: '✗', text: 'signing key not registered', fix: MACHINE };
|
|
89
91
|
}),
|
|
90
92
|
...(await typesFindings(sys, token)),
|
|
93
|
+
...(await boardAppFindings(sys, token)),
|
|
91
94
|
];
|
|
92
95
|
return report(sys, io, agent, [
|
|
93
96
|
['This Mac', mac],
|
|
@@ -134,30 +137,36 @@ function leftoverFindings(sys, store, agent) {
|
|
|
134
137
|
];
|
|
135
138
|
}
|
|
136
139
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* Approvals issue: checked in a repository whose settings turn the board on,
|
|
140
|
-
* in an organisation, since onboard keeps them so there.
|
|
140
|
+
* The repository doctor runs in and its settings, parsed, where they turn
|
|
141
|
+
* the board on and doctor has a token to ask GitHub with; else undefined.
|
|
141
142
|
*/
|
|
142
|
-
|
|
143
|
-
let slug;
|
|
144
|
-
let approvals;
|
|
143
|
+
function boardRepo(sys, token) {
|
|
145
144
|
try {
|
|
146
145
|
const found = repoAt(sys);
|
|
147
146
|
const path = `${found.root}/${SETTINGS_PATH}`;
|
|
148
147
|
if (!token || !sys.exists(path))
|
|
149
|
-
return
|
|
148
|
+
return undefined;
|
|
150
149
|
const settings = JSON.parse(sys.read(path));
|
|
151
|
-
|
|
152
|
-
return [];
|
|
153
|
-
slug = found.slug;
|
|
154
|
-
const named = settings?.approvals;
|
|
155
|
-
approvals = typeof named === 'number' ? named : undefined;
|
|
150
|
+
return featuresIn(settings).includes('board') ? { slug: found.slug, settings } : undefined;
|
|
156
151
|
}
|
|
157
152
|
catch {
|
|
158
153
|
// Outside a client's clone, or settings doctor cannot read: onboard says what is wrong with them.
|
|
159
|
-
return
|
|
154
|
+
return undefined;
|
|
160
155
|
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Whether the organisation's enabled issue types are the work model's, no
|
|
159
|
+
* issue here holds another, and every open one here has one but the
|
|
160
|
+
* Approvals issue: checked in a repository whose settings turn the board on,
|
|
161
|
+
* in an organisation, since onboard keeps them so there.
|
|
162
|
+
*/
|
|
163
|
+
async function typesFindings(sys, token) {
|
|
164
|
+
const here = boardRepo(sys, token);
|
|
165
|
+
if (!here || !token)
|
|
166
|
+
return [];
|
|
167
|
+
const { slug, settings } = here;
|
|
168
|
+
const named = settings?.approvals;
|
|
169
|
+
const approvals = typeof named === 'number' ? named : undefined;
|
|
161
170
|
const org = slug.split('/')[0];
|
|
162
171
|
try {
|
|
163
172
|
return (await isOrganisation(sys, org)) ? [await typesDrift(sys, token, org, slug, approvals)] : [];
|
|
@@ -166,6 +175,34 @@ async function typesFindings(sys, token) {
|
|
|
166
175
|
return [{ mark: '✗', text: `issue types: cannot ask GitHub (${message(error)})`, fix: 'niyam doctor again' }];
|
|
167
176
|
}
|
|
168
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Whether the repository has the board app's secrets, which the Slice started
|
|
180
|
+
* job moves a slice to In Progress with when its pull request opens (P-70):
|
|
181
|
+
* checked where the settings turn the board on, in an organisation. A missing
|
|
182
|
+
* one is worth fixing, never broken, since the desktop push still moves the
|
|
183
|
+
* board; secrets GitHub will not list say nothing.
|
|
184
|
+
*/
|
|
185
|
+
async function boardAppFindings(sys, token) {
|
|
186
|
+
const here = boardRepo(sys, token);
|
|
187
|
+
if (!here || !token)
|
|
188
|
+
return [];
|
|
189
|
+
const { slug } = here;
|
|
190
|
+
try {
|
|
191
|
+
if (!(await isOrganisation(sys, slug.split('/')[0])))
|
|
192
|
+
return [];
|
|
193
|
+
const names = await secretNames(sys, slug, token);
|
|
194
|
+
if (names === undefined)
|
|
195
|
+
return [];
|
|
196
|
+
const missing = BOARD_SECRETS.filter((name) => !names.includes(name));
|
|
197
|
+
return missing.length === 0
|
|
198
|
+
? [{ mark: '✓', text: 'board app secrets' }]
|
|
199
|
+
: [{ mark: '▲', text: `no ${missing.join(' or ')} secret, so a slice moves to In Progress only from a desktop push`, fix: `${ONBOARD}, which says how to set up the board app` }];
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
// fail-open: a read that failed is the one ▲ here, as a missing secret breaks nothing.
|
|
203
|
+
return [{ mark: '▲', text: `board app secrets: cannot ask GitHub (${message(error)})`, fix: 'niyam doctor again' }];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
169
206
|
/**
|
|
170
207
|
* Client hooks that reach GitHub outside `as-agent` where doctor runs, a Mac
|
|
171
208
|
* or a cloud session, and the Auto-fix rules an onboarded repo's Claude Code
|
|
@@ -343,6 +380,9 @@ async function cloudDoctor(sys, io, named) {
|
|
|
343
380
|
text: 'session-start did not give this session the agent identity, so gh and git stay locked out',
|
|
344
381
|
fix: 'a new session; its first niyam line says why',
|
|
345
382
|
});
|
|
383
|
+
const agent = given ?? ('login' in asked ? asked.login : undefined);
|
|
384
|
+
if (agent !== undefined)
|
|
385
|
+
github.push(cloudCommits(sys, agent));
|
|
346
386
|
const about = 'login' in asked ? asked.login : agents !== undefined ? either(agents) : 'this cloud session';
|
|
347
387
|
return report(sys, io, about, [
|
|
348
388
|
['This cloud session', session],
|
|
@@ -350,6 +390,27 @@ async function cloudDoctor(sys, io, named) {
|
|
|
350
390
|
['This repo', repoFindings(sys)],
|
|
351
391
|
]);
|
|
352
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Whose a cloud session's commits are: the agent's when session-start gave it
|
|
395
|
+
* the agent's author and signing (the key in SIGNING_KEY_VAR), else Claude's,
|
|
396
|
+
* which PR identity passes, so a warning only, with what makes them the
|
|
397
|
+
* agent's.
|
|
398
|
+
*/
|
|
399
|
+
function cloudCommits(sys, agent) {
|
|
400
|
+
const name = sys.env.GIT_AUTHOR_NAME;
|
|
401
|
+
const email = sys.env.GIT_AUTHOR_EMAIL?.toLowerCase() ?? '';
|
|
402
|
+
if (name !== undefined && sameLogin(name, agent) && /^\d+\+/.test(email) && email.endsWith(`+${agent.toLowerCase()}@${GITHUB_NOREPLY}`)) {
|
|
403
|
+
return { mark: '✓', text: `this session's commits are ${agent}'s, signed with ${SIGNING_KEY_VAR}` };
|
|
404
|
+
}
|
|
405
|
+
if (sys.env[SIGNING_KEY_VAR]) {
|
|
406
|
+
return { mark: '▲', text: `${SIGNING_KEY_VAR} is set, but session-start could not use it, so this session's commits are Claude's`, fix: 'a new session; its first niyam line says why' };
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
mark: '▲',
|
|
410
|
+
text: `this session's commits are Claude's: the cloud environment's variables have no ${SIGNING_KEY_VAR}`,
|
|
411
|
+
fix: `niyam cloud-key ${agent} on your Mac, then paste it into the environment's variables`,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
353
414
|
function message(error) {
|
|
354
415
|
return error instanceof Error ? error.message : String(error);
|
|
355
416
|
}
|