sil-openclaw 0.4.2 → 0.4.3
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/CHANGELOG.md +42 -0
- package/dist/index.js +26 -0
- package/dist/lib/creation-entrypoint.js +111 -0
- package/dist/lib/derive-agent-id.js +36 -0
- package/dist/lib/findings.js +40 -0
- package/dist/lib/host-wiring.js +238 -0
- package/dist/lib/version-advisory.js +275 -0
- package/dist/tools/catalog.js +10 -9
- package/dist/tools/doctor.js +596 -0
- package/dist/tools/identity.js +13 -3
- package/dist/tools/profile.js +11 -1
- package/openclaw.plugin.json +4 -2
- package/package.json +1 -1
- package/scripts/create-shopper.mjs +22 -27
- package/sil-shopping/SKILL.md +9 -4
- package/sil-shopping/references/agent_creation_engine.md +46 -18
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sil_doctor` — report-first diagnosis of the plugin's `$SIL_DATA_DIR`-scoped
|
|
3
|
+
* state.
|
|
4
|
+
*
|
|
5
|
+
* The product is the findings array, not the fixes: a run that fixes nothing is
|
|
6
|
+
* a complete, successful run. Mutation is a bounded side-effect — the ONLY
|
|
7
|
+
* writes are the two safe auto-fixes (tighten a too-open mode, create the
|
|
8
|
+
* missing data dir), both under `getDataDir()`. Destructive fixes (anything that
|
|
9
|
+
* mutates the BYTES of an existing artefact, including clearing a corrupt
|
|
10
|
+
* `tokens.json`) are surfaced as `needs_confirmation` and never run: delete-first
|
|
11
|
+
* does not apply to user data.
|
|
12
|
+
*
|
|
13
|
+
* Every diagnosis is derived from local state and completes with the network
|
|
14
|
+
* down — a doctor is needed MOST when things are broken, and "broken" often
|
|
15
|
+
* includes the network. The one outbound request is the latest-version probe
|
|
16
|
+
* (bounded + fail-soft to silence); no sil-api/sil-web call, and NO token
|
|
17
|
+
* rotation — a diagnosis must have no auth side-effects.
|
|
18
|
+
*
|
|
19
|
+
* `register()` opens nothing: the probe lives inside `execute()` like every
|
|
20
|
+
* other I/O.
|
|
21
|
+
*
|
|
22
|
+
* No finding field may ever carry token bytes, a JWT claim value, or user PII.
|
|
23
|
+
* Token health is metadata only: present / mode / parses / expired.
|
|
24
|
+
*/
|
|
25
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
26
|
+
import { randomBytes } from "node:crypto";
|
|
27
|
+
import { join, relative } from "node:path";
|
|
28
|
+
import { Type } from "typebox";
|
|
29
|
+
import { buildCreationEntrypointFinding, probeCreationEntrypoint, resolveCreationEntrypoint, } from "../lib/creation-entrypoint.js";
|
|
30
|
+
import { DIR_MODE, getConfigPath, getDataDir, getTokensPath, hasTokens, readConfig, readTokens, } from "../lib/credentials.js";
|
|
31
|
+
import { sortFindings } from "../lib/findings.js";
|
|
32
|
+
import { detectWiringDrift, readHostVersion, readSilWiringFacts, } from "../lib/host-wiring.js";
|
|
33
|
+
import { readShopperIdentity, searchProfileFrontmatter, } from "../lib/profile-store.js";
|
|
34
|
+
import { jsonResult } from "../lib/tool-result.js";
|
|
35
|
+
import { buildGatewayCompatFinding, buildVersionBehindFinding, probeLatestVersion, readInstalledVersion, } from "../lib/version-advisory.js";
|
|
36
|
+
/** Owner-only file mode. `DIR_MODE` (0o700) is owned by `credentials.ts` and
|
|
37
|
+
* imported; the file mode is private per-module there and in `profile-store.ts`,
|
|
38
|
+
* so this mirrors that pattern rather than exporting a fourth copy. */
|
|
39
|
+
const FILE_MODE = 0o600;
|
|
40
|
+
/** An interrupted atomic write leaves `<path>.<hex>.tmp` behind
|
|
41
|
+
* (`profile-store.ts`'s tmp → rename). The hex length is not pinned: it is a
|
|
42
|
+
* detail of one call site's `randomBytes(n)`, and an orphan is an orphan.
|
|
43
|
+
* Bytes on disk ⇒ surfaced, never deleted. */
|
|
44
|
+
const STALE_TMP_RE = /\.[0-9a-f]+\.tmp$/;
|
|
45
|
+
const REREGISTER_HINT = "Run `sil_register` to re-register this install.";
|
|
46
|
+
export function registerDoctorTools(api,
|
|
47
|
+
// The probe's seam. Defaulted to global `fetch`, so production wiring is a
|
|
48
|
+
// bare `registerDoctorTools(api)`; a test drives up-to-date / newer /
|
|
49
|
+
// erroring / STALLING channels through it without a live network.
|
|
50
|
+
fetchImpl = fetch) {
|
|
51
|
+
api.registerTool({
|
|
52
|
+
name: "sil_doctor",
|
|
53
|
+
label: "Diagnose sil",
|
|
54
|
+
description: "Diagnose this sil install: check the local data directory, file"
|
|
55
|
+
+ " permissions, stored identity/token health, and behaviour artefacts,"
|
|
56
|
+
+ " and report whether a newer sil plugin is published. Returns a"
|
|
57
|
+
+ " machine-readable findings array. Safe permission fixes apply"
|
|
58
|
+
+ " automatically; anything that could lose data is only reported, never"
|
|
59
|
+
+ " run. Never reads out or logs token contents, and never updates"
|
|
60
|
+
+ " anything itself.",
|
|
61
|
+
parameters: Type.Object({}),
|
|
62
|
+
async execute() {
|
|
63
|
+
const dataDir = getDataDir();
|
|
64
|
+
const findings = [];
|
|
65
|
+
const dir = checkDataDir(dataDir);
|
|
66
|
+
findings.push(...dir.findings);
|
|
67
|
+
if (dir.usable) {
|
|
68
|
+
findings.push(...walkDataDir(dataDir));
|
|
69
|
+
findings.push(...checkStore());
|
|
70
|
+
}
|
|
71
|
+
findings.push(...checkIdentity());
|
|
72
|
+
// Host wiring + host compat: both read the tree already in memory, so they
|
|
73
|
+
// are local and unconditional — they still report with the network down.
|
|
74
|
+
// `sil_doctor` is the SECOND surface for the wiring advisory (it also rides
|
|
75
|
+
// every sil_* result); it is the ONLY one for compat, which answers a
|
|
76
|
+
// question nobody asked mid-search.
|
|
77
|
+
findings.push(...detectWiringDrift(api.config, readSilWiringFacts()));
|
|
78
|
+
const compat = buildGatewayCompatFinding(readHostVersion(api));
|
|
79
|
+
if (compat !== null)
|
|
80
|
+
findings.push(compat);
|
|
81
|
+
// The creation entrypoint: resolved ONCE, then both reported and probed —
|
|
82
|
+
// so the path the report hands the agent to run is, by construction, the
|
|
83
|
+
// path the verdict is about. A local stat, so it still answers offline.
|
|
84
|
+
const creationEntrypoint = resolveCreationEntrypoint();
|
|
85
|
+
findings.push(buildCreationEntrypointFinding(creationEntrypoint, probeCreationEntrypoint(creationEntrypoint)));
|
|
86
|
+
const installedVersion = readInstalledVersion();
|
|
87
|
+
const behind = buildVersionBehindFinding(installedVersion, await probeLatestVersion(fetchImpl));
|
|
88
|
+
if (behind !== null)
|
|
89
|
+
findings.push(behind);
|
|
90
|
+
return jsonResult(buildDoctorReport({
|
|
91
|
+
dataDir,
|
|
92
|
+
installedVersion,
|
|
93
|
+
creationEntrypoint,
|
|
94
|
+
findings,
|
|
95
|
+
}));
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** Roll-ups the consumer would otherwise re-derive, over a deterministic order.
|
|
100
|
+
* Pure: it never invents a finding, it only rolls up the ones it is handed. */
|
|
101
|
+
export function buildDoctorReport(input) {
|
|
102
|
+
const { dataDir, installedVersion, creationEntrypoint, findings } = input;
|
|
103
|
+
const sorted = sortFindings(findings);
|
|
104
|
+
const counts = { info: 0, warn: 0, critical: 0 };
|
|
105
|
+
for (const f of sorted)
|
|
106
|
+
counts[f.severity] += 1;
|
|
107
|
+
return {
|
|
108
|
+
status: "ok",
|
|
109
|
+
healthy: counts.warn === 0 && counts.critical === 0,
|
|
110
|
+
dataDir,
|
|
111
|
+
installedVersion,
|
|
112
|
+
creationEntrypoint,
|
|
113
|
+
counts,
|
|
114
|
+
findings: sorted,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// ===========================================================================
|
|
118
|
+
// Filesystem hygiene
|
|
119
|
+
// ===========================================================================
|
|
120
|
+
/** The data dir gates every other filesystem check: an unwritable or
|
|
121
|
+
* non-directory home means no artefact and no token can EVER persist. */
|
|
122
|
+
function checkDataDir(dataDir) {
|
|
123
|
+
const id = "fs.data_dir_writable";
|
|
124
|
+
if (!existsSync(dataDir)) {
|
|
125
|
+
// Creating a missing container dir is safe: idempotent, loses nothing.
|
|
126
|
+
try {
|
|
127
|
+
mkdirSync(dataDir, { recursive: true, mode: DIR_MODE });
|
|
128
|
+
return {
|
|
129
|
+
findings: [{
|
|
130
|
+
id,
|
|
131
|
+
severity: "info",
|
|
132
|
+
status: "fixed",
|
|
133
|
+
detected: `The sil data directory ${dataDir} was missing.`,
|
|
134
|
+
suggestedAction: null,
|
|
135
|
+
appliedAction: `Created ${dataDir} (mode 0700).`,
|
|
136
|
+
}, checkDataDirMode(dataDir)],
|
|
137
|
+
usable: true,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
return {
|
|
142
|
+
findings: [{
|
|
143
|
+
id,
|
|
144
|
+
severity: "critical",
|
|
145
|
+
status: "fix_failed",
|
|
146
|
+
detected: `The sil data directory ${dataDir} is missing and could not be`
|
|
147
|
+
+ ` created: ${causeOf(err)}`,
|
|
148
|
+
suggestedAction: `Create ${dataDir} yourself (owner-only, mode 0700), or point`
|
|
149
|
+
+ " $SIL_DATA_DIR at a writable location.",
|
|
150
|
+
appliedAction: null,
|
|
151
|
+
}],
|
|
152
|
+
usable: false,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!statSync(dataDir).isDirectory()) {
|
|
157
|
+
return {
|
|
158
|
+
findings: [{
|
|
159
|
+
id,
|
|
160
|
+
severity: "critical",
|
|
161
|
+
status: "advisory",
|
|
162
|
+
detected: `The sil data directory path ${dataDir} exists but is not a`
|
|
163
|
+
+ " directory, so no token or artefact can be stored.",
|
|
164
|
+
suggestedAction: `Move or remove the file at ${dataDir}, or point $SIL_DATA_DIR at a`
|
|
165
|
+
+ " writable directory.",
|
|
166
|
+
appliedAction: null,
|
|
167
|
+
}],
|
|
168
|
+
usable: false,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
// Prove writability the way the store actually writes — an atomic tmp file —
|
|
172
|
+
// rather than trusting the mode bits (a read-only mount passes a mode check).
|
|
173
|
+
const probe = join(dataDir, `.doctor.${randomBytes(6).toString("hex")}.tmp`);
|
|
174
|
+
try {
|
|
175
|
+
writeFileSync(probe, "", { mode: FILE_MODE });
|
|
176
|
+
unlinkSync(probe);
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
return {
|
|
180
|
+
findings: [{
|
|
181
|
+
id,
|
|
182
|
+
severity: "critical",
|
|
183
|
+
status: "advisory",
|
|
184
|
+
detected: `The sil data directory ${dataDir} is not writable, so every token`
|
|
185
|
+
+ ` and artefact write would fail: ${causeOf(err)}`,
|
|
186
|
+
suggestedAction: `Make ${dataDir} writable by its owner (mode 0700), or point`
|
|
187
|
+
+ " $SIL_DATA_DIR at a writable directory.",
|
|
188
|
+
appliedAction: null,
|
|
189
|
+
}, checkDataDirMode(dataDir)],
|
|
190
|
+
// The dir is readable, so the remaining checks still diagnose usefully —
|
|
191
|
+
// they just may not be able to fix anything.
|
|
192
|
+
usable: true,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
findings: [{
|
|
197
|
+
id,
|
|
198
|
+
severity: "info",
|
|
199
|
+
status: "ok",
|
|
200
|
+
detected: `The sil data directory ${dataDir} is present and writable.`,
|
|
201
|
+
suggestedAction: null,
|
|
202
|
+
appliedAction: null,
|
|
203
|
+
}, checkDataDirMode(dataDir)],
|
|
204
|
+
usable: true,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The data dir's OWN mode — the highest-value dir check in the tool, because this
|
|
209
|
+
* is the container holding `tokens.json`. Nothing else covers it: the enumerated
|
|
210
|
+
* walk only ever checks the *entries* it reads out of this dir, and `mkdirSync`
|
|
211
|
+
* on an ALREADY-EXISTING dir is a no-op that never chmods — so a pre-existing
|
|
212
|
+
* 0755 dir (an untarred backup, an older install's umask, another tool's mkdir)
|
|
213
|
+
* would otherwise survive forever behind a `healthy: true`.
|
|
214
|
+
*
|
|
215
|
+
* A singleton, like `fs.data_dir_writable`: one fixed path with a lifecycle, so
|
|
216
|
+
* it carries a stable id and reports `ok` when healthy — `fixed → re-run → ok`.
|
|
217
|
+
*
|
|
218
|
+
* `stat`, NOT `lstat` — deliberately the opposite of the walk, and NOT a
|
|
219
|
+
* containment hole. `chmod` follows symlinks unconditionally and Node exposes no
|
|
220
|
+
* `lchmodSync` on Linux (it is `undefined`), so `lstat` here could not prevent
|
|
221
|
+
* the follow — it would only change what we REPORT. It buys zero containment and
|
|
222
|
+
* costs convergence: a symlink's own mode is always 0777, so `lstat` would read
|
|
223
|
+
* 0777, tighten, have `chmod` follow to the target anyway, then read 0777 again
|
|
224
|
+
* next run and re-report `fixed` FOREVER — a non-idempotent lie that breaks the
|
|
225
|
+
* `fixed → re-run → ok` guarantee this singleton exists to provide. `stat` makes
|
|
226
|
+
* detect and fix agree on the same inode, so it is strictly better on both axes.
|
|
227
|
+
*
|
|
228
|
+
* Nor is following an escape: `getDataDir()` IS the sandbox root (it returns the
|
|
229
|
+
* env override verbatim, no `realpath`), so if the operator made it an
|
|
230
|
+
* indirection, the target IS the directory the plugin stores everything in. The
|
|
231
|
+
* trust boundary is `$SIL_DATA_DIR` itself — anyone who can set it can point it
|
|
232
|
+
* at the target directly, no symlink needed. The entry-walk's `lstat`+skip rule
|
|
233
|
+
* is what actually carries containment: entry symlinks are NOT operator-declared.
|
|
234
|
+
*/
|
|
235
|
+
function checkDataDirMode(dataDir) {
|
|
236
|
+
const id = "fs.data_dir_mode";
|
|
237
|
+
const mode = statSync(dataDir).mode & 0o777;
|
|
238
|
+
return tightenMode({
|
|
239
|
+
id,
|
|
240
|
+
path: dataDir,
|
|
241
|
+
mode,
|
|
242
|
+
expected: DIR_MODE,
|
|
243
|
+
detected: `The sil data directory ${dataDir} is mode ${oct(mode)} — readable beyond`
|
|
244
|
+
+ " its owner. It holds this install's stored credentials and behaviour"
|
|
245
|
+
+ ` artefacts, so it must be owner-only ${oct(DIR_MODE)}.`,
|
|
246
|
+
}) ?? {
|
|
247
|
+
id,
|
|
248
|
+
severity: "info",
|
|
249
|
+
status: "ok",
|
|
250
|
+
detected: `The sil data directory ${dataDir} is owner-only (mode ${oct(mode)}).`,
|
|
251
|
+
suggestedAction: null,
|
|
252
|
+
appliedAction: null,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Walk `$SIL_DATA_DIR` for too-open modes and orphaned tmp files.
|
|
257
|
+
*
|
|
258
|
+
* Enumerated ⇒ emits ONLY on a problem: a healthy store with 200 artefacts
|
|
259
|
+
* yields zero findings, not 200 `ok` ones.
|
|
260
|
+
*
|
|
261
|
+
* `lstat`, never `stat`: a symlink under the data dir is REPORTED, never
|
|
262
|
+
* chmod'd-through — chmod follows the link and would mutate a file outside
|
|
263
|
+
* `$SIL_DATA_DIR`.
|
|
264
|
+
*/
|
|
265
|
+
function walkDataDir(dataDir) {
|
|
266
|
+
const findings = [];
|
|
267
|
+
// tokens.json has its own stable `identity.tokens_perms` check. Without this
|
|
268
|
+
// exclusion the walk claims the same mode twice — and, running first, it
|
|
269
|
+
// would fix the file before the singleton looked, so the singleton would
|
|
270
|
+
// report `ok` on the very run that fixed it and the fix would vanish from
|
|
271
|
+
// its own `appliedAction`.
|
|
272
|
+
const tokensPath = getTokensPath();
|
|
273
|
+
const visit = (dir) => {
|
|
274
|
+
let entries;
|
|
275
|
+
try {
|
|
276
|
+
entries = readdirSync(dir);
|
|
277
|
+
}
|
|
278
|
+
catch (err) {
|
|
279
|
+
findings.push({
|
|
280
|
+
id: `fs.unreadable_dir:${rel(dataDir, dir)}`,
|
|
281
|
+
severity: "warn",
|
|
282
|
+
status: "advisory",
|
|
283
|
+
detected: `Could not list ${dir}: ${causeOf(err)}`,
|
|
284
|
+
suggestedAction: "Check the directory's permissions and ownership.",
|
|
285
|
+
appliedAction: null,
|
|
286
|
+
});
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
for (const entry of entries.sort()) {
|
|
290
|
+
const path = join(dir, entry);
|
|
291
|
+
let stats;
|
|
292
|
+
try {
|
|
293
|
+
stats = lstatSync(path);
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
// This repo MANUFACTURES this race: the store's own atomic writes create
|
|
297
|
+
// `<path>.<hex>.tmp` and rename it away, and the shopper can be writing
|
|
298
|
+
// while the agent self-diagnoses. An entry that vanished between readdir
|
|
299
|
+
// and lstat is not a finding — and must never throw out of execute().
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (stats.isSymbolicLink()) {
|
|
303
|
+
findings.push({
|
|
304
|
+
id: `fs.symlink:${rel(dataDir, path)}`,
|
|
305
|
+
severity: "warn",
|
|
306
|
+
status: "advisory",
|
|
307
|
+
detected: `${path} is a symlink. sil never creates symlinks under its data`
|
|
308
|
+
+ " directory, and its permissions are neither audited nor fixed"
|
|
309
|
+
+ " through it (a chmod would follow the link outside $SIL_DATA_DIR).",
|
|
310
|
+
suggestedAction: "Replace the symlink with a real file or directory if sil should"
|
|
311
|
+
+ " manage it.",
|
|
312
|
+
appliedAction: null,
|
|
313
|
+
});
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (stats.isDirectory()) {
|
|
317
|
+
findings.push(...checkMode(dataDir, path, stats.mode, DIR_MODE));
|
|
318
|
+
visit(path);
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (!stats.isFile())
|
|
322
|
+
continue;
|
|
323
|
+
if (path === tokensPath)
|
|
324
|
+
continue;
|
|
325
|
+
findings.push(...checkMode(dataDir, path, stats.mode, FILE_MODE));
|
|
326
|
+
if (STALE_TMP_RE.test(entry)) {
|
|
327
|
+
findings.push({
|
|
328
|
+
id: `fs.stale_tmp:${rel(dataDir, path)}`,
|
|
329
|
+
severity: "warn",
|
|
330
|
+
status: "advisory",
|
|
331
|
+
detected: `${path} is an orphaned temporary file left by an interrupted`
|
|
332
|
+
+ " atomic write.",
|
|
333
|
+
// Bytes on disk ⇒ destructive to remove ⇒ the doctor only reports it.
|
|
334
|
+
suggestedAction: "Safe to delete once you have confirmed nothing else is writing"
|
|
335
|
+
+ " it — sil does not delete artefact bytes itself.",
|
|
336
|
+
appliedAction: null,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
visit(dataDir);
|
|
342
|
+
return findings;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* The tighten-only mode fix — the one place the invariant lives, for all three
|
|
346
|
+
* mode checks (enumerated walk, tokens.json, the data dir itself).
|
|
347
|
+
*
|
|
348
|
+
* Too-open means bits set OUTSIDE the expected mask — not merely `!== expected`.
|
|
349
|
+
* That distinction IS the invariant: a stricter mode (0400 file, 0500 dir) is not
|
|
350
|
+
* a problem, and "fixing" it to 0600/0700 would WIDEN it — a security regression
|
|
351
|
+
* dressed up as a fix. The fix is `mode & expected`, which can only ever CLEAR
|
|
352
|
+
* bits.
|
|
353
|
+
*
|
|
354
|
+
* `null` = already within the mask. The caller decides what that silence means:
|
|
355
|
+
* an enumerated check emits nothing, a singleton emits its stable `ok`.
|
|
356
|
+
*/
|
|
357
|
+
function tightenMode(input) {
|
|
358
|
+
const { id, path, mode, expected, detected } = input;
|
|
359
|
+
if ((mode & ~expected & 0o777) === 0)
|
|
360
|
+
return null;
|
|
361
|
+
const tightened = mode & expected;
|
|
362
|
+
try {
|
|
363
|
+
chmodSync(path, tightened);
|
|
364
|
+
return {
|
|
365
|
+
id,
|
|
366
|
+
severity: "warn",
|
|
367
|
+
status: "fixed",
|
|
368
|
+
detected,
|
|
369
|
+
suggestedAction: null,
|
|
370
|
+
appliedAction: `Tightened ${path} from ${oct(mode)} to ${oct(tightened)}.`,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
// An un-applied fix is NEVER green-washed as `fixed`.
|
|
375
|
+
return {
|
|
376
|
+
id,
|
|
377
|
+
severity: "warn",
|
|
378
|
+
status: "fix_failed",
|
|
379
|
+
detected: `${detected} Tightening it failed: ${causeOf(err)}`,
|
|
380
|
+
suggestedAction: `Run \`chmod ${oct(tightened)} ${path}\` yourself.`,
|
|
381
|
+
appliedAction: null,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/** Enumerated ⇒ silence when healthy: 200 clean artefacts emit zero findings. */
|
|
386
|
+
function checkMode(dataDir, path, rawMode, expected) {
|
|
387
|
+
const mode = rawMode & 0o777;
|
|
388
|
+
const finding = tightenMode({
|
|
389
|
+
id: `fs.mode:${rel(dataDir, path)}`,
|
|
390
|
+
path,
|
|
391
|
+
mode,
|
|
392
|
+
expected,
|
|
393
|
+
detected: `${path} is mode ${oct(mode)}, which is more permissive than the`
|
|
394
|
+
+ ` owner-only ${oct(expected)} sil requires.`,
|
|
395
|
+
});
|
|
396
|
+
return finding === null ? [] : [finding];
|
|
397
|
+
}
|
|
398
|
+
// ===========================================================================
|
|
399
|
+
// Identity / token health — metadata only, offline, never a token byte
|
|
400
|
+
// ===========================================================================
|
|
401
|
+
function checkIdentity() {
|
|
402
|
+
const findings = [];
|
|
403
|
+
const tokensPath = getTokensPath();
|
|
404
|
+
if (!hasTokens()) {
|
|
405
|
+
// A valid state, not an error: bare `sil_search` works unregistered.
|
|
406
|
+
findings.push({
|
|
407
|
+
id: "identity.tokens_present",
|
|
408
|
+
severity: "info",
|
|
409
|
+
status: "advisory",
|
|
410
|
+
detected: "This install is not registered — no tokens.json is stored.",
|
|
411
|
+
suggestedAction: "Run `sil_register` to register, if you want personalised results.",
|
|
412
|
+
appliedAction: null,
|
|
413
|
+
});
|
|
414
|
+
return findings;
|
|
415
|
+
}
|
|
416
|
+
findings.push({
|
|
417
|
+
id: "identity.tokens_present",
|
|
418
|
+
severity: "info",
|
|
419
|
+
status: "ok",
|
|
420
|
+
detected: "This install is registered — tokens.json is present.",
|
|
421
|
+
suggestedAction: null,
|
|
422
|
+
appliedAction: null,
|
|
423
|
+
});
|
|
424
|
+
// tokens.json is a SINGLETON artefact, so its mode carries a STABLE id every
|
|
425
|
+
// run — that is what makes the fix's idempotence observable to a consumer:
|
|
426
|
+
// `fixed → re-run → ok` needs the same id to still be there. The enumerated
|
|
427
|
+
// `fs.mode:` walk could not provide that: it emits only on a problem, so it
|
|
428
|
+
// would report nothing at all on the run after the fix.
|
|
429
|
+
findings.push(...checkTokensPerms(tokensPath));
|
|
430
|
+
// A file that parses as JSON but carries no `access_token` is corrupt too:
|
|
431
|
+
// there is nothing to authenticate with, and nothing to decode an expiry from.
|
|
432
|
+
const stored = readTokens();
|
|
433
|
+
if (stored === null || typeof stored.access_token !== "string") {
|
|
434
|
+
// Clearing it would mutate bytes ⇒ destructive ⇒ surfaced, never auto-run.
|
|
435
|
+
findings.push({
|
|
436
|
+
id: "identity.tokens_parse",
|
|
437
|
+
severity: "warn",
|
|
438
|
+
status: "needs_confirmation",
|
|
439
|
+
detected: `${tokensPath} is present but could not be parsed as stored`
|
|
440
|
+
+ " credentials. Authenticated calls will fail until it is replaced.",
|
|
441
|
+
suggestedAction: `${REREGISTER_HINT} That replaces ${tokensPath}. sil does not clear or`
|
|
442
|
+
+ " overwrite it automatically — it may still be recoverable.",
|
|
443
|
+
appliedAction: null,
|
|
444
|
+
});
|
|
445
|
+
return findings;
|
|
446
|
+
}
|
|
447
|
+
findings.push({
|
|
448
|
+
id: "identity.tokens_parse",
|
|
449
|
+
severity: "info",
|
|
450
|
+
status: "ok",
|
|
451
|
+
detected: `${tokensPath} parses as stored credentials.`,
|
|
452
|
+
suggestedAction: null,
|
|
453
|
+
appliedAction: null,
|
|
454
|
+
});
|
|
455
|
+
findings.push(expiryFinding(isAccessTokenExpired(stored.access_token)));
|
|
456
|
+
findings.push(configFinding());
|
|
457
|
+
return findings;
|
|
458
|
+
}
|
|
459
|
+
/** The stable singleton counterpart to the enumerated `fs.mode:` walk — same
|
|
460
|
+
* tighten-only rule, same fix, but it reports `ok` when healthy so the
|
|
461
|
+
* `fixed → re-run → ok` transition stays observable on one id. */
|
|
462
|
+
function checkTokensPerms(tokensPath) {
|
|
463
|
+
const id = "identity.tokens_perms";
|
|
464
|
+
const mode = lstatSync(tokensPath).mode & 0o777;
|
|
465
|
+
const tightened = tightenMode({
|
|
466
|
+
id,
|
|
467
|
+
path: tokensPath,
|
|
468
|
+
mode,
|
|
469
|
+
expected: FILE_MODE,
|
|
470
|
+
detected: `${tokensPath} is mode ${oct(mode)} — readable beyond its owner. Stored`
|
|
471
|
+
+ " credentials must be owner-only.",
|
|
472
|
+
});
|
|
473
|
+
return [tightened ?? {
|
|
474
|
+
id,
|
|
475
|
+
severity: "info",
|
|
476
|
+
status: "ok",
|
|
477
|
+
detected: `${tokensPath} is owner-only (mode ${oct(mode)}).`,
|
|
478
|
+
suggestedAction: null,
|
|
479
|
+
appliedAction: null,
|
|
480
|
+
}];
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Decode-only JWT `exp`: `true` = expired, `false` = not expired, `null` =
|
|
484
|
+
* INCONCLUSIVE (not a 3-segment JWT / undecodable payload / no numeric `exp`).
|
|
485
|
+
*
|
|
486
|
+
* NO signature verification (this is a health hint, not an auth decision — real
|
|
487
|
+
* verification is server-side on the next authed call) and NO network.
|
|
488
|
+
*
|
|
489
|
+
* `StoredTokens` carries no `exp` of its own, so "expired" has to come from the
|
|
490
|
+
* token itself. The `boolean | null` return is deliberate: it is structurally
|
|
491
|
+
* incapable of carrying token bytes or any claim other than the derived answer.
|
|
492
|
+
*
|
|
493
|
+
* Never throws, and never fabricates expiry — an opaque token reads `null`.
|
|
494
|
+
*/
|
|
495
|
+
export function isAccessTokenExpired(accessToken) {
|
|
496
|
+
const segments = accessToken.split(".");
|
|
497
|
+
if (segments.length !== 3)
|
|
498
|
+
return null;
|
|
499
|
+
try {
|
|
500
|
+
const payload = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
|
|
501
|
+
const exp = payload.exp;
|
|
502
|
+
if (typeof exp !== "number" || !Number.isFinite(exp))
|
|
503
|
+
return null;
|
|
504
|
+
return exp * 1_000 <= Date.now();
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function expiryFinding(expired) {
|
|
511
|
+
const id = "identity.token_expiry";
|
|
512
|
+
if (expired === true) {
|
|
513
|
+
// `warn`, not `critical`: a stored refresh token means the next authed call
|
|
514
|
+
// self-heals. The offline doctor won't network-refresh to confirm.
|
|
515
|
+
return {
|
|
516
|
+
id,
|
|
517
|
+
severity: "warn",
|
|
518
|
+
status: "advisory",
|
|
519
|
+
detected: "The stored access token has expired.",
|
|
520
|
+
suggestedAction: "Authenticated tools refresh automatically on their next call. If they"
|
|
521
|
+
+ ` keep failing: ${REREGISTER_HINT}`,
|
|
522
|
+
appliedAction: null,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
return {
|
|
526
|
+
id,
|
|
527
|
+
severity: "info",
|
|
528
|
+
status: "ok",
|
|
529
|
+
detected: expired === false
|
|
530
|
+
? "The stored access token has not expired."
|
|
531
|
+
: "The stored access token carries no readable expiry; sil cannot tell"
|
|
532
|
+
+ " offline whether it is still valid, and will refresh it on demand.",
|
|
533
|
+
suggestedAction: null,
|
|
534
|
+
appliedAction: null,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function configFinding() {
|
|
538
|
+
const id = "identity.config_parse";
|
|
539
|
+
const configPath = getConfigPath();
|
|
540
|
+
if (existsSync(configPath) && readConfig() === null) {
|
|
541
|
+
return {
|
|
542
|
+
id,
|
|
543
|
+
severity: "warn",
|
|
544
|
+
status: "needs_confirmation",
|
|
545
|
+
detected: `${configPath} is present but could not be parsed, so the stored user`
|
|
546
|
+
+ " identity is unreadable.",
|
|
547
|
+
suggestedAction: `${REREGISTER_HINT} That rewrites ${configPath}. sil does not overwrite`
|
|
548
|
+
+ " it automatically.",
|
|
549
|
+
appliedAction: null,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
return {
|
|
553
|
+
id,
|
|
554
|
+
severity: "info",
|
|
555
|
+
status: "ok",
|
|
556
|
+
detected: existsSync(configPath)
|
|
557
|
+
? `${configPath} parses as the stored user identity.`
|
|
558
|
+
: `No ${configPath} is stored.`,
|
|
559
|
+
suggestedAction: null,
|
|
560
|
+
appliedAction: null,
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
// ===========================================================================
|
|
564
|
+
// Behaviour-artefact store
|
|
565
|
+
// ===========================================================================
|
|
566
|
+
/** Consume the store's OWN fail-closed `unreadable[]` surfacing — never re-parse
|
|
567
|
+
* the artefacts, never aggregate entries away, and never overwrite one. Each
|
|
568
|
+
* entry becomes exactly one finding. */
|
|
569
|
+
function checkStore() {
|
|
570
|
+
return [
|
|
571
|
+
...readShopperIdentity().unreadable,
|
|
572
|
+
...searchProfileFrontmatter().unreadable,
|
|
573
|
+
].map(({ id, error }) => ({
|
|
574
|
+
id: `store.unreadable:${id}`,
|
|
575
|
+
severity: "warn",
|
|
576
|
+
status: "advisory",
|
|
577
|
+
// Name the artefact AND the corruption — this is what a human reads to go
|
|
578
|
+
// repair the file. The store's own error text describes only the corruption.
|
|
579
|
+
detected: `${id}: ${error}`,
|
|
580
|
+
suggestedAction: "Inspect and repair the artefact by hand — sil never overwrites a corrupt"
|
|
581
|
+
+ " artefact, because it may still be recoverable.",
|
|
582
|
+
appliedAction: null,
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
// ===========================================================================
|
|
586
|
+
/** The OS cause, never a token or PII — mirrors the store's
|
|
587
|
+
* `persistence_failed.error` discipline. */
|
|
588
|
+
function causeOf(err) {
|
|
589
|
+
return err instanceof Error ? err.message : String(err);
|
|
590
|
+
}
|
|
591
|
+
function oct(mode) {
|
|
592
|
+
return `0${mode.toString(8).padStart(3, "0")}`;
|
|
593
|
+
}
|
|
594
|
+
function rel(dataDir, path) {
|
|
595
|
+
return relative(dataDir, path) || ".";
|
|
596
|
+
}
|
package/dist/tools/identity.js
CHANGED
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
import { Type } from "typebox";
|
|
33
33
|
import { getWebUrl, getWebUrlSource, getApiUrl } from "../lib/config.js";
|
|
34
34
|
import { clearTokens, getTokensPath, hasTokens, readConfig, readTokens, writeConfig, writeTokens, } from "../lib/credentials.js";
|
|
35
|
+
import { wiringAdvisories } from "../lib/host-wiring.js";
|
|
35
36
|
import { deriveChallenge, newSessionId, newVerifier } from "../lib/pkce.js";
|
|
36
37
|
import { POLL_DEADLINE_MS, POLL_INTERVAL_MS, startPoll, } from "../lib/poller.js";
|
|
37
38
|
import { claimSession, fetchIdentity, refreshAndRetryOnce, } from "../lib/sil-client.js";
|
|
@@ -66,6 +67,10 @@ function registerRegister(api) {
|
|
|
66
67
|
status: "already_registered",
|
|
67
68
|
user: config?.user ?? null,
|
|
68
69
|
next_step: "offer_shopper",
|
|
70
|
+
// Folded here but NOT onto `awaiting_browser`: this is a terminal
|
|
71
|
+
// result, whereas that one is a mid-flow auth handoff whose whole job
|
|
72
|
+
// is to get one link in front of the user.
|
|
73
|
+
...wiringAdvisories(api),
|
|
69
74
|
});
|
|
70
75
|
}
|
|
71
76
|
// A fresh registration attempt clears any prior in-process persist-failure
|
|
@@ -238,7 +243,7 @@ function registerWhoami(api) {
|
|
|
238
243
|
function identityOutcomeToResult(api, outcome, originBlock) {
|
|
239
244
|
switch (outcome.kind) {
|
|
240
245
|
case "ok":
|
|
241
|
-
return identityResult(outcome.identity, originBlock);
|
|
246
|
+
return identityResult(api, outcome.identity, originBlock);
|
|
242
247
|
case "forbidden":
|
|
243
248
|
// Decision B — the dead-token clear is UNIFORM across all three sil-api tools.
|
|
244
249
|
// `sil_whoami` is the tool that is legible TODAY; an agent that diagnoses the
|
|
@@ -265,8 +270,13 @@ function identityOutcomeToResult(api, outcome, originBlock) {
|
|
|
265
270
|
/** Success: the identity payload + the web-origin visibility block (FIX B) —
|
|
266
271
|
* no token, no Bearer header. The origin block (strings only) lets the agent
|
|
267
272
|
* relay the resolved origin + its source so a wrong origin is diagnosable. */
|
|
268
|
-
function identityResult(identity, originBlock) {
|
|
269
|
-
return jsonResult({
|
|
273
|
+
function identityResult(api, identity, originBlock) {
|
|
274
|
+
return jsonResult({
|
|
275
|
+
status: "ok",
|
|
276
|
+
identity,
|
|
277
|
+
...originBlock,
|
|
278
|
+
...wiringAdvisories(api),
|
|
279
|
+
});
|
|
270
280
|
}
|
|
271
281
|
/** Not registered: a distinct, actionable outcome naming the recovery tool. No
|
|
272
282
|
* identity fields (name/addresses) so the agent can't mistake it for a read. */
|