shadok-ai 0.19.89 → 0.19.90
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
CHANGED
|
@@ -1678,7 +1678,7 @@ HTTP endpoints (same auth): `/usage`, `/live`, `/sessions`, `/recover`,
|
|
|
1678
1678
|
`/channels/glance` (GET — each visible row's last-message time and context %,
|
|
1679
1679
|
read from the transcript's tail so the list fills before the agents resume),
|
|
1680
1680
|
`/channel` (DELETE), `/groups`, `/groups/ssh` (GET — the SSH **public** key to
|
|
1681
|
-
register per group, filtered like `/groups`), `/crons` (+ POST `/crons/:id/run` — fire a schedule now, its next slot unchanged), `/webhooks` (GET; POST create, PATCH/DELETE `/webhooks/:id`, POST `/webhooks/:id/rotate` — writes browser person only, the token is returned only by create/rotate; the call itself, `POST /hooks/:id`, sits before the password gate and authenticates with the webhook's own token), `/timezone`, `/title` (GET/PUT — the
|
|
1681
|
+
register per group, filtered like `/groups`), `/crons` (+ POST `/crons/:id/run` — fire a schedule now, its next slot unchanged; GET `/crons/env?session=<id>` — the variable NAMES a guard for that session would receive, values never included, so an agent can check its own guard before trusting it), `/webhooks` (GET; POST create, PATCH/DELETE `/webhooks/:id`, POST `/webhooks/:id/rotate` — writes browser person only, the token is returned only by create/rotate; the call itself, `POST /hooks/:id`, sits before the password gate and authenticates with the webhook's own token), `/timezone`, `/title` (GET/PUT — the
|
|
1682
1682
|
cockpit's name, per launch directory: click the header brand to rename it, so
|
|
1683
1683
|
several cockpits stay apart in the tab bar; an empty PUT reverts to the default),
|
|
1684
1684
|
`/theme` (GET/PUT — the cockpit's colour palette, per launch directory: an accent
|
|
@@ -36,10 +36,15 @@ omit the check — the prompt runs every time.
|
|
|
36
36
|
### What a check can see — secrets and cwd
|
|
37
37
|
|
|
38
38
|
A check does **not** run inside this agent's process. The server runs it with
|
|
39
|
-
`sh -c`, in the CHANNEL's directory, with the secrets
|
|
40
|
-
|
|
39
|
+
`sh -c`, in the CHANNEL's directory, with the vault secrets that resolve for
|
|
40
|
+
this agent injected as environment variables. On a multi-tenant instance it
|
|
41
41
|
runs as this agent's own OS user, exactly like the agent.
|
|
42
42
|
|
|
43
|
+
A secret reaches it two ways, and they are stored in different places: the
|
|
44
|
+
channel's **profile** lists it, OR the secret itself is bound to that profile —
|
|
45
|
+
or to “any profile” — from the web Secrets panel. The second kind reaches a
|
|
46
|
+
guard that has no profile at all, so never conclude "no profile, no secrets".
|
|
47
|
+
|
|
43
48
|
Its environment is otherwise SMALL on purpose: `PATH`, the locale and time
|
|
44
49
|
zone, proxy/CA settings, and `SHADOK_SESSION_ID` / `SHADOK_PORT` /
|
|
45
50
|
`SHADOK_SESSION_KEY` (send the key as `x-shadok-session-key` to call the local
|
|
@@ -55,14 +60,33 @@ server answers 403. A check-less cron on a sibling is fine.
|
|
|
55
60
|
So reference a secret by name (`$MY_API_KEY`) and trust it: never hardcode a
|
|
56
61
|
value into a check script, and never try to source a shell profile to get one.
|
|
57
62
|
|
|
58
|
-
The sharp edge is the other direction. A
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
is skipped just as silently. Your own shell having the key proves nothing
|
|
62
|
-
the guard's.
|
|
63
|
+
The sharp edge is the other direction. A name nothing binds to this agent is
|
|
64
|
+
simply **absent** at run time — no error, and you find out at 6am rather than
|
|
65
|
+
now. A name that isn't in the vault, or that lives in a group this agent cannot
|
|
66
|
+
reach, is skipped just as silently. Your own shell having the key proves nothing
|
|
67
|
+
about the guard's.
|
|
68
|
+
|
|
69
|
+
So ask, before writing a check that needs a secret:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
node ~/.claude/skills/shadok-scheduler/scripts/schedule.mjs env
|
|
73
|
+
```
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
It asks the server what it would inject and prints the variable NAMES (never a
|
|
76
|
+
value): the secrets that resolve, the ones the profile asks for that do not, and
|
|
77
|
+
the whole environment. Two things follow from it being the server's answer and
|
|
78
|
+
not a guess:
|
|
79
|
+
|
|
80
|
+
- **Trust it over your own shell, and over this file.** It reads both halves of
|
|
81
|
+
the secret↔profile link, which no client can.
|
|
82
|
+
- **Nothing in it waits for a reload.** The server re-reads the profiles and the
|
|
83
|
+
vault at every tick, so a secret attached a minute ago is already there. A
|
|
84
|
+
*Reload agent* changes only **your** process environment, not the guard's.
|
|
85
|
+
|
|
86
|
+
And if a guard still behaves as though a variable were missing, test the guard
|
|
87
|
+
itself — a one-minute schedule whose check is
|
|
88
|
+
`[ -n "$MY_KEY" ] && echo present` costs one wake-up and settles it. Printing a
|
|
89
|
+
presence test is fine; printing the value is not.
|
|
66
90
|
|
|
67
91
|
## Commands
|
|
68
92
|
|
|
@@ -51,6 +51,35 @@ async function api(ctx, method, path, body) {
|
|
|
51
51
|
return raw ? JSON.parse(raw) : {};
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Like `api`, but a 404 answers `null` instead of dying.
|
|
56
|
+
*
|
|
57
|
+
* Skills are re-seeded from the running build at every boot, so normally this
|
|
58
|
+
* file and the server move together. They do NOT when several cockpits share
|
|
59
|
+
* one machine: the machine-wide skill tier is written by whichever instance
|
|
60
|
+
* booted last, so an agent can be running this script against an older server.
|
|
61
|
+
* A missing route then has to degrade, not abort.
|
|
62
|
+
*/
|
|
63
|
+
async function apiMaybe(ctx, method, path) {
|
|
64
|
+
let res;
|
|
65
|
+
try {
|
|
66
|
+
res = await fetch(ctx.base + path, {
|
|
67
|
+
method,
|
|
68
|
+
headers: {
|
|
69
|
+
"content-type": "application/json",
|
|
70
|
+
...(ctx.key ? { "x-shadok-session-key": ctx.key } : {}),
|
|
71
|
+
},
|
|
72
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
73
|
+
});
|
|
74
|
+
} catch (e) {
|
|
75
|
+
die(`cannot reach the cockpit on ${ctx.base}: ${e.message}`);
|
|
76
|
+
}
|
|
77
|
+
if (res.status === 404) return null;
|
|
78
|
+
const raw = await res.text();
|
|
79
|
+
if (!res.ok) die(`API error ${res.status}: ${raw.slice(0, 200)}`);
|
|
80
|
+
return raw ? JSON.parse(raw) : {};
|
|
81
|
+
}
|
|
82
|
+
|
|
54
83
|
/** `every:30m` · `every:2h` · `every:45` · `daily:09:00` · `daily:09:00@mon-fri`
|
|
55
84
|
* · `once:2026-08-25T08:42` → the API's schedule. */
|
|
56
85
|
export function parseSchedule(spec) {
|
|
@@ -191,10 +220,52 @@ async function cmdTz(ctx, zone) {
|
|
|
191
220
|
}
|
|
192
221
|
|
|
193
222
|
async function cmdEnv(ctx) {
|
|
194
|
-
// A guard runs server-side, not in this agent's process
|
|
195
|
-
//
|
|
196
|
-
//
|
|
197
|
-
//
|
|
223
|
+
// A guard runs server-side, not in this agent's process, so this ASKS the
|
|
224
|
+
// server what it would inject instead of deriving it here.
|
|
225
|
+
//
|
|
226
|
+
// Deriving it is what this command used to do, and it was wrong in the
|
|
227
|
+
// expensive direction. The secret↔profile link has TWO writable homes — the
|
|
228
|
+
// profile's `secrets` list and the secret's own `profiles` list (the vault
|
|
229
|
+
// panel's chip row) — and a secret bound from the second one reaches the
|
|
230
|
+
// guard while a reader of the first reports it absent. A `*` ("any profile")
|
|
231
|
+
// binding reaches an agent with no profile at all, which this command used to
|
|
232
|
+
// answer with a flat "NONE". Both are false negatives, and a correct setup
|
|
233
|
+
// reported as broken costs more than no answer: it sends someone hunting a
|
|
234
|
+
// fault that is not there. `/crons/env` is built by the same function the
|
|
235
|
+
// real tick uses, so it cannot drift from it.
|
|
236
|
+
const view = await apiMaybe(ctx, "GET", `/crons/env?session=${encodeURIComponent(ctx.sid)}`);
|
|
237
|
+
if (!view) return cmdEnvHalfBlind(ctx);
|
|
238
|
+
if (!view.known) {
|
|
239
|
+
// Not "no secrets at all": a “any profile” binding still reaches a guard
|
|
240
|
+
// with no profile, and the list below proves it when there is one.
|
|
241
|
+
console.log("this channel is not registered server-side: a guard would run from the");
|
|
242
|
+
console.log("server's own directory, with no profile — so only a secret bound to");
|
|
243
|
+
console.log("“any profile” can reach it.");
|
|
244
|
+
}
|
|
245
|
+
console.log(`guard cwd: ${view.cwd || "(server default)"}`);
|
|
246
|
+
if (view.profileMissing)
|
|
247
|
+
console.log(`note: the channel points at profile '${view.profile}', which no longer exists.`);
|
|
248
|
+
const names = view.secrets ?? [];
|
|
249
|
+
const where = view.profile ? `profile '${view.profile}'` : "no profile";
|
|
250
|
+
console.log(`guard secrets (${where}): ${names.length ? names.join(", ") : "NONE"}`);
|
|
251
|
+
if (!names.length) {
|
|
252
|
+
console.log(" A secret reaches a guard through the channel's profile, or by being bound");
|
|
253
|
+
console.log(" to this profile (or to “any profile”) from the web Secrets panel.");
|
|
254
|
+
}
|
|
255
|
+
// A name the profile asks for that does not resolve for THIS agent: a typo,
|
|
256
|
+
// or a secret owned by a group it neither belongs to nor was lent. The server
|
|
257
|
+
// is the only place that can tell — it resolves by the agent's real group.
|
|
258
|
+
if (view.unresolved?.length)
|
|
259
|
+
console.log(` listed by the profile but NOT resolvable, so absent at run time: ${view.unresolved.join(", ")}`);
|
|
260
|
+
console.log(`guard env (names only): ${(view.env ?? []).join(", ")}`);
|
|
261
|
+
console.log("This is what the NEXT run gets: the server re-reads the profiles and the");
|
|
262
|
+
console.log("vault at every tick, so nothing above is waiting for a reload.");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The pre-`/crons/env` answer, for an older cockpit (see `apiMaybe`). It sees
|
|
266
|
+
* only the PROFILE side of the link, so it can report a bound secret as
|
|
267
|
+
* absent — it says so rather than sounding certain. */
|
|
268
|
+
async function cmdEnvHalfBlind(ctx) {
|
|
198
269
|
const channels = await api(ctx, "GET", "/channels");
|
|
199
270
|
const ch = channels.find((c) => c.sessionId === ctx.sid);
|
|
200
271
|
if (!ch) {
|
|
@@ -204,29 +275,19 @@ async function cmdEnv(ctx) {
|
|
|
204
275
|
}
|
|
205
276
|
console.log(`guard cwd: ${ch.cwd || "(server default)"}`);
|
|
206
277
|
const pname = ch.profile;
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
console.log(" (web UI, the agent's profile picker) that lists the names you need.");
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const profiles = await api(ctx, "GET", "/profiles");
|
|
214
|
-
const prof = profiles.find((p) => p.name === pname);
|
|
215
|
-
if (!prof) {
|
|
216
|
-
// Same end result as "no secrets", but a very different cause: say which
|
|
217
|
-
// one, or the report reads as a working setup.
|
|
218
|
-
console.log(`guard secrets: NONE — the channel points at profile '${pname}', which no longer exists.`);
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
const wanted = prof.secrets ?? [];
|
|
222
|
-
if (!wanted.length) return console.log(`guard secrets: NONE — profile '${pname}' lists no secret.`);
|
|
278
|
+
const profiles = pname ? await api(ctx, "GET", "/profiles") : [];
|
|
279
|
+
const prof = pname ? profiles.find((p) => p.name === pname) : undefined;
|
|
280
|
+
const wanted = prof?.secrets ?? [];
|
|
223
281
|
const vault = new Set((await api(ctx, "GET", "/secrets")).names ?? []);
|
|
224
282
|
const present = wanted.filter((n) => vault.has(n));
|
|
225
283
|
const missing = wanted.filter((n) => !vault.has(n));
|
|
226
|
-
console.log(`guard secrets (profile '${pname}'): ${present.length ? present.join(", ") : "NONE"}`);
|
|
284
|
+
console.log(`guard secrets (profile '${pname ?? "none"}'): ${present.length ? present.join(", ") : "NONE"}`);
|
|
227
285
|
// `secretsFor` skips an unknown name without a word, so a typo here looks
|
|
228
286
|
// exactly like a working guard until the day it runs.
|
|
229
287
|
if (missing.length) console.log(` referenced but NOT in the vault, so absent at run time: ${missing.join(", ")}`);
|
|
288
|
+
console.log("This cockpit is older than this skill, so the list above reads ONLY the");
|
|
289
|
+
console.log("profile's own list: a secret bound to this profile from the Secrets panel");
|
|
290
|
+
console.log("reaches the guard and is missing here. Test the guard itself if it matters.");
|
|
230
291
|
}
|
|
231
292
|
|
|
232
293
|
/** The cron of THIS channel an id (or the 8-character prefix `list` prints)
|
|
@@ -125,24 +125,82 @@ test("del sends the 8-char prefix the listing prints, url-encoded", async () =>
|
|
|
125
125
|
assert.match(res.out, /deleted abcdef01/);
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
-
test("env
|
|
128
|
+
test("env reports what the SERVER resolves, not what the profile lists", async () => {
|
|
129
|
+
// CHIPPED is bound to the profile from the SECRET's side, so it is absent
|
|
130
|
+
// from `profiles[].secrets` and present in the guard. Deriving the answer
|
|
131
|
+
// here reported it missing; asking the server is the fix.
|
|
129
132
|
const srv = await fakeCockpit({
|
|
130
|
-
"GET /
|
|
131
|
-
|
|
132
|
-
|
|
133
|
+
"GET /crons/env": {
|
|
134
|
+
body: {
|
|
135
|
+
cwd: "/w/t", known: true, profile: "P",
|
|
136
|
+
secrets: ["CHIPPED", "HAVE"], unresolved: ["ELSEWHERE"],
|
|
137
|
+
env: ["PATH", "SHADOK_PORT", "TZ"],
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
"GET /profiles": { body: [{ name: "P", secrets: ["HAVE", "ELSEWHERE"] }] },
|
|
133
141
|
});
|
|
134
142
|
const res = await run(["env"], AGENT(srv.port));
|
|
135
143
|
await srv.close();
|
|
144
|
+
assert.equal(srv.seen[0].query, "session=sess-1");
|
|
136
145
|
assert.match(res.out, /guard cwd: \/w\/t/);
|
|
137
|
-
assert.match(res.out, /guard secrets \(profile 'P'\): HAVE/);
|
|
138
|
-
assert.match(res.out, /NOT
|
|
146
|
+
assert.match(res.out, /guard secrets \(profile 'P'\): CHIPPED, HAVE/);
|
|
147
|
+
assert.match(res.out, /NOT resolvable[^\n]*ELSEWHERE/);
|
|
148
|
+
assert.match(res.out, /guard env \(names only\): PATH, SHADOK_PORT, TZ/);
|
|
149
|
+
// No reload is involved: saying so stops the next agent inventing one.
|
|
150
|
+
assert.match(res.out, /next run/i);
|
|
151
|
+
// The profile list must not be consulted at all any more.
|
|
152
|
+
assert.ok(!srv.seen.some((r) => r.path === "/profiles"), "still reads /profiles");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("env lists an 'any profile' secret even with no profile at all", async () => {
|
|
156
|
+
// The old answer was a flat "NONE — this channel has no profile", which is
|
|
157
|
+
// false: a `*` binding reaches a profile-less agent.
|
|
158
|
+
const srv = await fakeCockpit({
|
|
159
|
+
"GET /crons/env": { body: { cwd: "/w/t", known: true, profile: null, secrets: ["WIDE"], env: ["PATH"] } },
|
|
160
|
+
});
|
|
161
|
+
const res = await run(["env"], AGENT(srv.port));
|
|
162
|
+
await srv.close();
|
|
163
|
+
assert.match(res.out, /guard secrets \(no profile\): WIDE/);
|
|
139
164
|
});
|
|
140
165
|
|
|
141
|
-
test("env
|
|
142
|
-
|
|
166
|
+
test("env on an unregistered channel does not claim 'no secrets at all'", async () => {
|
|
167
|
+
// It used to say exactly that, which a `*` binding contradicts on the very
|
|
168
|
+
// next line.
|
|
169
|
+
const srv = await fakeCockpit({
|
|
170
|
+
"GET /crons/env": { body: { cwd: "/srv", known: false, profile: null, secrets: ["WIDE"], env: ["PATH"] } },
|
|
171
|
+
});
|
|
143
172
|
const res = await run(["env"], AGENT(srv.port));
|
|
144
173
|
await srv.close();
|
|
145
|
-
assert.match(res.out, /
|
|
174
|
+
assert.match(res.out, /not registered server-side/);
|
|
175
|
+
assert.match(res.out, /guard secrets \(no profile\): WIDE/);
|
|
176
|
+
assert.doesNotMatch(res.out, /no profile secrets at all/);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("env with nothing bound explains BOTH ways a secret can arrive", async () => {
|
|
180
|
+
const srv = await fakeCockpit({
|
|
181
|
+
"GET /crons/env": { body: { cwd: "/w/t", known: true, profile: "P", secrets: [], env: ["PATH"] } },
|
|
182
|
+
});
|
|
183
|
+
const res = await run(["env"], AGENT(srv.port));
|
|
184
|
+
await srv.close();
|
|
185
|
+
assert.match(res.out, /guard secrets \(profile 'P'\): NONE/);
|
|
186
|
+
assert.match(res.out, /any profile/);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("env on an older cockpit degrades, and says it is half-blind", async () => {
|
|
190
|
+
// Several cockpits on one machine share the skill tier, so this file can run
|
|
191
|
+
// against a server that predates /crons/env. A 404 must not abort the command.
|
|
192
|
+
const srv = await fakeCockpit({
|
|
193
|
+
"GET /crons/env": { status: 404, body: { error: "not found" } },
|
|
194
|
+
"GET /channels": { body: [{ sessionId: "sess-1", cwd: "/w/t", profile: "P" }] },
|
|
195
|
+
"GET /profiles": { body: [{ name: "P", secrets: ["HAVE", "MISSING"] }] },
|
|
196
|
+
"GET /secrets": { body: { names: ["HAVE"] } },
|
|
197
|
+
});
|
|
198
|
+
const res = await run(["env"], AGENT(srv.port));
|
|
199
|
+
await srv.close();
|
|
200
|
+
assert.equal(res.code, 0);
|
|
201
|
+
assert.match(res.out, /guard secrets \(profile 'P'\): HAVE/);
|
|
202
|
+
assert.match(res.out, /NOT in the vault[^\n]*MISSING/);
|
|
203
|
+
assert.match(res.out, /older than this skill/);
|
|
146
204
|
});
|
|
147
205
|
|
|
148
206
|
test("an API error is reported, not swallowed", async () => {
|
package/dist/server.js
CHANGED
|
@@ -439,6 +439,61 @@ const cronsFiring = new Set();
|
|
|
439
439
|
function cronTag(c) {
|
|
440
440
|
return `cron: ${c.id.slice(0, 8)}`;
|
|
441
441
|
}
|
|
442
|
+
/**
|
|
443
|
+
* Build the environment a cron guard runs with, for ONE session. Extracted so
|
|
444
|
+
* that `runCronCheck` and the `/crons/env` preview cannot disagree about it.
|
|
445
|
+
*
|
|
446
|
+
* That split is the whole point. The scheduler skill used to answer "what will
|
|
447
|
+
* my guard see?" by reading the PROFILE's `secrets` list, which is only half of
|
|
448
|
+
* a two-homed link: a secret bound from the SECRET's side (`PUT /secrets/profiles`,
|
|
449
|
+
* the vault panel's chip row) reaches the guard and was reported absent — and a
|
|
450
|
+
* `*` binding reached a profile-less agent the tool told to expect nothing. Both
|
|
451
|
+
* are false negatives, which is the expensive direction: an operator shown a
|
|
452
|
+
* correct setup as broken goes looking for a fault that is not there. There is
|
|
453
|
+
* now one builder, and the preview calls it instead of re-deriving it.
|
|
454
|
+
*/
|
|
455
|
+
function cronGuardEnv(sessionId, target) {
|
|
456
|
+
const profile = target.profile ? getProfile(target.profile) : undefined;
|
|
457
|
+
// Resolve the guard's secrets by the agent's REAL group (own → shared-in →
|
|
458
|
+
// Main), not always Main (T9 carry). Mono-tenant leaves `group` null → Main.
|
|
459
|
+
const secrets = secretsForProfile(target.profile, profile?.secrets, target.group ?? MAIN_GROUP_ID, commonsGroupIds());
|
|
460
|
+
// WHO the guard runs as (invariant 60). It used to be the SERVER — root, in
|
|
461
|
+
// multi-tenant — with the server's whole environment: a root shell for
|
|
462
|
+
// anyone able to write a cron, holding the GUI password and every token the
|
|
463
|
+
// server has. Now it runs as the TARGET agent's own OS user (the same
|
|
464
|
+
// `setpriv` drop as its spawn) with an allow-listed environment plus the
|
|
465
|
+
// profile's secrets. Mono-tenant keeps the server's user — there is only one
|
|
466
|
+
// — but gets the same clean environment.
|
|
467
|
+
let user = null;
|
|
468
|
+
if (DEPLOYMENT_MODE === "multi-tenant") {
|
|
469
|
+
if (!target.known)
|
|
470
|
+
return { ok: false, detail: "no channel for this session — refusing to guess whose rights to run the check with" };
|
|
471
|
+
const who = osUserForChannelGroup(target.group, loadGroups(), commonsOsUser({ migrated: groupsMigrated, mode: DEPLOYMENT_MODE }));
|
|
472
|
+
if (!who.ok)
|
|
473
|
+
return { ok: false, detail: "the agent's group no longer exists — check not run" };
|
|
474
|
+
user = who.user;
|
|
475
|
+
}
|
|
476
|
+
const env = cronCheckEnv({
|
|
477
|
+
base: process.env,
|
|
478
|
+
secrets,
|
|
479
|
+
session: {
|
|
480
|
+
id: sessionId,
|
|
481
|
+
port: String(boundPort || START_PORT),
|
|
482
|
+
key: sessionKeyFor(sessionId),
|
|
483
|
+
// The TARGET agent's own registry, resolved exactly like its spawn's.
|
|
484
|
+
peersFile: peersOutFileFor(process.cwd(), agentStateHome(user)),
|
|
485
|
+
},
|
|
486
|
+
user,
|
|
487
|
+
home: user ? homeForUser(user, GROUP_HOME_BASE) : null,
|
|
488
|
+
// The same `gh` token the agent itself is spawned with (`makePilot`), and
|
|
489
|
+
// for the same reason: a DROPPED guard reads `GH_CONFIG_DIR` out of its
|
|
490
|
+
// own empty home, so without this every `gh` call in it fails — silently,
|
|
491
|
+
// because guards swallow a failed call to survive a network hiccup. Only
|
|
492
|
+
// when dropping; the server's own user reads its config dir directly.
|
|
493
|
+
ghToken: user ? serverGhToken() : undefined,
|
|
494
|
+
});
|
|
495
|
+
return { ok: true, env, user, secrets };
|
|
496
|
+
}
|
|
442
497
|
/**
|
|
443
498
|
* Run a cron's optional guard command WITHOUT the LLM (in the channel's cwd,
|
|
444
499
|
* with the profile's secrets). stdout is the only content signal: whatever it
|
|
@@ -448,45 +503,10 @@ function runCronCheck(c, target) {
|
|
|
448
503
|
if (!c.check?.trim())
|
|
449
504
|
return Promise.resolve({ kind: "quiet" }); // no guard → the agent always runs
|
|
450
505
|
return new Promise((resolve) => {
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const
|
|
455
|
-
// WHO the guard runs as (invariant 60). It used to be the SERVER — root, in
|
|
456
|
-
// multi-tenant — with the server's whole environment: a root shell for
|
|
457
|
-
// anyone able to write a cron, holding the GUI password and every token the
|
|
458
|
-
// server has. Now it runs as the TARGET agent's own OS user (the same
|
|
459
|
-
// `setpriv` drop as its spawn) with an allow-listed environment plus the
|
|
460
|
-
// profile's secrets. Mono-tenant keeps the server's user — there is only one
|
|
461
|
-
// — but gets the same clean environment.
|
|
462
|
-
let user = null;
|
|
463
|
-
if (DEPLOYMENT_MODE === "multi-tenant") {
|
|
464
|
-
if (!target.known)
|
|
465
|
-
return resolve({ kind: "failed", detail: "no channel for this session — refusing to guess whose rights to run the check with" });
|
|
466
|
-
const who = osUserForChannelGroup(target.group, loadGroups(), commonsOsUser({ migrated: groupsMigrated, mode: DEPLOYMENT_MODE }));
|
|
467
|
-
if (!who.ok)
|
|
468
|
-
return resolve({ kind: "failed", detail: "the agent's group no longer exists — check not run" });
|
|
469
|
-
user = who.user;
|
|
470
|
-
}
|
|
471
|
-
const env = cronCheckEnv({
|
|
472
|
-
base: process.env,
|
|
473
|
-
secrets: secretEnv,
|
|
474
|
-
session: {
|
|
475
|
-
id: c.sessionId,
|
|
476
|
-
port: String(boundPort || START_PORT),
|
|
477
|
-
key: sessionKeyFor(c.sessionId),
|
|
478
|
-
// The TARGET agent's own registry, resolved exactly like its spawn's.
|
|
479
|
-
peersFile: peersOutFileFor(process.cwd(), agentStateHome(user)),
|
|
480
|
-
},
|
|
481
|
-
user,
|
|
482
|
-
home: user ? homeForUser(user, GROUP_HOME_BASE) : null,
|
|
483
|
-
// The same `gh` token the agent itself is spawned with (`makePilot`), and
|
|
484
|
-
// for the same reason: a DROPPED guard reads `GH_CONFIG_DIR` out of its
|
|
485
|
-
// own empty home, so without this every `gh` call in it fails — silently,
|
|
486
|
-
// because guards swallow a failed call to survive a network hiccup. Only
|
|
487
|
-
// when dropping; the server's own user reads its config dir directly.
|
|
488
|
-
ghToken: user ? serverGhToken() : undefined,
|
|
489
|
-
});
|
|
506
|
+
const plan = cronGuardEnv(c.sessionId, target);
|
|
507
|
+
if (!plan.ok)
|
|
508
|
+
return resolve({ kind: "failed", detail: plan.detail });
|
|
509
|
+
const { env, user, secrets: secretEnv } = plan;
|
|
490
510
|
const run = cronCheckCommand(user, c.check);
|
|
491
511
|
recordSecretAudit({
|
|
492
512
|
action: "cron-check",
|
|
@@ -3275,6 +3295,52 @@ app.get("/instance/stats", async (req, res) => {
|
|
|
3275
3295
|
// Scoped like every other per-session read (invariant 60): a cron carries its
|
|
3276
3296
|
// prompt and its guard command, i.e. another group's monitoring logic.
|
|
3277
3297
|
app.get("/crons", (req, res) => res.json(loadCrons().filter((c) => mayActFor(req, c.sessionId))));
|
|
3298
|
+
/**
|
|
3299
|
+
* What a guard for this session would ACTUALLY receive — variable names only,
|
|
3300
|
+
* never a value. Built by the same `cronGuardEnv` the real tick uses, so the
|
|
3301
|
+
* preview cannot drift from the thing it previews.
|
|
3302
|
+
*
|
|
3303
|
+
* This exists because the honest answer is not computable client-side. The
|
|
3304
|
+
* secret↔profile link has two writable homes — the profile's `secrets` list and
|
|
3305
|
+
* the secret's own `profiles` list — and `GET /secrets` withholds the second
|
|
3306
|
+
* from an agent for commons secrets, so the scheduler skill could only ever see
|
|
3307
|
+
* half of it. There is no reload or propagation delay to account for either:
|
|
3308
|
+
* `cronGuardEnv` reads `profiles.json` and the vault on every call, so this is
|
|
3309
|
+
* what the NEXT tick gets.
|
|
3310
|
+
*/
|
|
3311
|
+
app.get("/crons/env", (req, res) => {
|
|
3312
|
+
const sessionId = String(req.query.session ?? "").trim();
|
|
3313
|
+
if (!sessionId)
|
|
3314
|
+
return res.status(400).json({ error: "session required" });
|
|
3315
|
+
// Scoped exactly like GET /crons (invariant 60): the guard command of another
|
|
3316
|
+
// group is already gated there, and its secret NAMES are no more public.
|
|
3317
|
+
if (!mayActFor(req, sessionId))
|
|
3318
|
+
return res.status(403).json({ error: "not your agent" });
|
|
3319
|
+
const target = resolveSessionTarget(loadChannels(), sessionId, process.cwd());
|
|
3320
|
+
const plan = cronGuardEnv(sessionId, target);
|
|
3321
|
+
if (!plan.ok)
|
|
3322
|
+
return res.status(409).json({ error: plan.detail });
|
|
3323
|
+
// Declared by the profile but NOT resolvable: a typo, or a secret owned by a
|
|
3324
|
+
// group this agent neither belongs to nor was lent. The caller cannot work
|
|
3325
|
+
// this out for itself — `GET /secrets` lists what the CALLER sees, which is
|
|
3326
|
+
// not what the guard resolves (own → shared-in → commons, by the AGENT's
|
|
3327
|
+
// group). `secretsFor` skips an unknown name silently, so without this a typo
|
|
3328
|
+
// looks exactly like a working guard until the day it runs.
|
|
3329
|
+
const declared = (target.profile ? getProfile(target.profile)?.secrets : undefined) ?? [];
|
|
3330
|
+
res.json({
|
|
3331
|
+
cwd: target.cwd,
|
|
3332
|
+
known: target.known,
|
|
3333
|
+
profile: target.profile,
|
|
3334
|
+
profileMissing: !!target.profile && !getProfile(target.profile),
|
|
3335
|
+
secrets: Object.keys(plan.secrets).sort(),
|
|
3336
|
+
unresolved: [...new Set(declared.filter((n) => !Object.hasOwn(plan.secrets, n)))].sort(),
|
|
3337
|
+
// The whole key list, plumbing included: the allow-list is short and
|
|
3338
|
+
// invisible from the agent's own shell, so "does my guard even have TZ?" is
|
|
3339
|
+
// a real question — and one the agent used to answer by guessing.
|
|
3340
|
+
env: Object.keys(plan.env).sort(),
|
|
3341
|
+
user: plan.user,
|
|
3342
|
+
});
|
|
3343
|
+
});
|
|
3278
3344
|
// Create (no id) or update (id present). Body: { id?, sessionId, prompt,
|
|
3279
3345
|
// schedule:{kind,...}, enabled? }.
|
|
3280
3346
|
app.post("/crons", (req, res) => {
|