sil-openclaw 0.2.3 → 0.3.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/CHANGELOG.md +128 -0
- package/README.md +68 -4
- package/dist/index.js +2 -0
- package/dist/lib/poller.js +9 -3
- package/dist/lib/profile-store.js +426 -0
- package/dist/tools/identity.js +201 -23
- package/dist/tools/profile.js +283 -0
- package/openclaw.plugin.json +7 -3
- package/package.json +1 -1
- package/skill/SKILL.md +29 -36
- package/skill/examples/road_cycling_expert_walkthrough.md +106 -0
- package/skill/references/agent_creation_engine.md +65 -0
- package/skill/references/brainstorm_interview.md +68 -0
- package/skill/references/catalog_tools_reference.md +46 -0
- package/skill/references/expert_shopping.md +112 -0
- package/skill/references/manage_experts.md +100 -0
- package/skill/references/refine_expert.md +41 -0
- package/skill/references/search_param_mapping.md +35 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sil shopping-expert behaviour-artefact store.
|
|
3
|
+
*
|
|
4
|
+
* The agent-creation engine (the bundled `skill/SKILL.md` procedure driving
|
|
5
|
+
* the OpenClaw host CLI) registers the *wiring* of a new sil-wired agent into
|
|
6
|
+
* the host's own config — the `agents.list[]` entry, the enabled `sil` plugin,
|
|
7
|
+
* the attached `sil` skill. That host-config write is the host agent driving
|
|
8
|
+
* its own `openclaw …` CLI; the plugin never touches `~/.openclaw`.
|
|
9
|
+
*
|
|
10
|
+
* What the plugin DOES own is the agent's *behaviour* layer, materialized into
|
|
11
|
+
* its own disclosed `filesystemScope` — `$SIL_DATA_DIR` (the same directory
|
|
12
|
+
* `lib/credentials.ts` uses for tokens/identity). Per the founder steer
|
|
13
|
+
* (2026-06-22) the engine writes a fixed, minimal set of artefacts there that
|
|
14
|
+
* power the created expert's behaviour, read by the sil skill at runtime:
|
|
15
|
+
*
|
|
16
|
+
* $SIL_DATA_DIR/agents/<agentId>/
|
|
17
|
+
* ├─ persona.md the shopping persona/instructions (host-natural —
|
|
18
|
+
* │ same shape as a workspace SOUL.md; the skill copies it
|
|
19
|
+
* │ into the agent workspace SOUL.md and re-reads it at
|
|
20
|
+
* │ session start)
|
|
21
|
+
* ├─ playbook.md the generated domain sub-skill (host-natural — same
|
|
22
|
+
* │ shape as a SKILL.md body; loaded by the sil skill at
|
|
23
|
+
* │ session start). OPTIONAL — written only when supplied.
|
|
24
|
+
* └─ profile.json the strictly-typed manifest the sil skill resolves the
|
|
25
|
+
* artefacts from (no filesystem guessing)
|
|
26
|
+
*
|
|
27
|
+
* Store boundary: host config = wiring; `$SIL_DATA_DIR` = behaviour artefacts.
|
|
28
|
+
* Identity/tokens already live in `$SIL_DATA_DIR` but stay logically separate
|
|
29
|
+
* (no identity coupling — creating an expert reads/writes no token).
|
|
30
|
+
*
|
|
31
|
+
* Writes are atomic and all-or-nothing (Product invariant 7): a bad spec
|
|
32
|
+
* writes NOTHING (validate first), and a mid-write failure leaves no partial
|
|
33
|
+
* artefact directory behind. The atomic write mirrors `credentials.ts` —
|
|
34
|
+
* tmp file → write → rename over target → chmod 0600, dir 0700.
|
|
35
|
+
*/
|
|
36
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
37
|
+
import { dirname, join } from "node:path";
|
|
38
|
+
import { randomBytes } from "node:crypto";
|
|
39
|
+
import { getDataDir } from "./credentials.js";
|
|
40
|
+
/** Owner-only file/dir modes — the artefacts are user-scoped, like tokens. */
|
|
41
|
+
const FILE_MODE = 0o600;
|
|
42
|
+
const DIR_MODE = 0o700;
|
|
43
|
+
/** The sub-tree under `$SIL_DATA_DIR` that holds per-agent behaviour artefacts. */
|
|
44
|
+
const AGENTS_SUBDIR = "agents";
|
|
45
|
+
/** Artefact filenames (stable — the sil skill resolves them from profile.json,
|
|
46
|
+
* but the names are also documented so a human can find them). */
|
|
47
|
+
const PERSONA_FILE = "persona.md";
|
|
48
|
+
const PLAYBOOK_FILE = "playbook.md";
|
|
49
|
+
const PROFILE_FILE = "profile.json";
|
|
50
|
+
/** A host-agent id must be lower-kebab and is never `main` (host-reserved). */
|
|
51
|
+
const AGENT_ID_RE = /^[a-z0-9][a-z0-9-]*$/;
|
|
52
|
+
/** Resolve the per-agent artefact directory under the plugin's data dir. Never
|
|
53
|
+
* hardcoded — `getDataDir()` honors `$SIL_DATA_DIR`/`$XDG_DATA_HOME`.
|
|
54
|
+
*
|
|
55
|
+
* SAFETY: this resolver does NOT guard `agentId` — it joins it as a path segment
|
|
56
|
+
* verbatim. The path-traversal guard lives at the only caller, `materializeProfile`,
|
|
57
|
+
* which validates `agentId` against `AGENT_ID_RE` (lower-kebab, not `main`) BEFORE
|
|
58
|
+
* the join, so `../escape`, `a/../b`, `.`, etc. never reach here. The guard is
|
|
59
|
+
* placed at the caller (not here) because validation must precede ALL writes for
|
|
60
|
+
* the validate-first / write-nothing-on-bad-input invariant to hold; a guard
|
|
61
|
+
* duplicated here would be redundant for that path. A FUTURE DIRECT CALLER of this
|
|
62
|
+
* exported resolver would bypass the guard — re-run `AGENT_ID_RE` (or route through
|
|
63
|
+
* `materializeProfile`) before trusting the returned path with any filesystem op. */
|
|
64
|
+
export function getAgentArtefactDir(agentId) {
|
|
65
|
+
return join(getDataDir(), AGENTS_SUBDIR, agentId);
|
|
66
|
+
}
|
|
67
|
+
/** True when `s` is a present, non-blank string. */
|
|
68
|
+
function nonBlank(s) {
|
|
69
|
+
return typeof s === "string" && s.trim().length > 0;
|
|
70
|
+
}
|
|
71
|
+
function invalid(field, message) {
|
|
72
|
+
return { ok: false, kind: "invalid_request", field, message };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Materialize the behaviour artefacts for a created sil-wired agent.
|
|
76
|
+
*
|
|
77
|
+
* Validate-first: any invalid field returns `invalid_request` and writes
|
|
78
|
+
* NOTHING. On a write failure, the partial directory is removed and
|
|
79
|
+
* `persistence_failed` is returned with `<dir>: <cause>` — no partial artefact
|
|
80
|
+
* set ever survives (Product invariant 7, atomic outcome).
|
|
81
|
+
*/
|
|
82
|
+
export function materializeProfile(spec) {
|
|
83
|
+
// --- validate-first (nothing is written until every field is good) ---
|
|
84
|
+
if (!nonBlank(spec.agentId)) {
|
|
85
|
+
return invalid("agentId", "agentId is required and must be non-empty.");
|
|
86
|
+
}
|
|
87
|
+
if (spec.agentId === "main") {
|
|
88
|
+
return invalid("agentId", '"main" is host-reserved and cannot be a sil expert id.');
|
|
89
|
+
}
|
|
90
|
+
if (!AGENT_ID_RE.test(spec.agentId)) {
|
|
91
|
+
return invalid("agentId", "agentId must be lower-kebab (a-z, 0-9, hyphen) — got: " + JSON.stringify(spec.agentId));
|
|
92
|
+
}
|
|
93
|
+
if (!nonBlank(spec.name)) {
|
|
94
|
+
return invalid("name", "name is required and must be non-empty.");
|
|
95
|
+
}
|
|
96
|
+
if (!nonBlank(spec.persona)) {
|
|
97
|
+
return invalid("persona", "persona is required and must be non-empty.");
|
|
98
|
+
}
|
|
99
|
+
// playbook is optional, but if present it must be non-blank (an empty string
|
|
100
|
+
// would materialize a useless artefact — reject it loudly rather than write it).
|
|
101
|
+
if (spec.playbook !== undefined && !nonBlank(spec.playbook)) {
|
|
102
|
+
return invalid("playbook", "playbook, when provided, must be non-empty.");
|
|
103
|
+
}
|
|
104
|
+
const dir = getAgentArtefactDir(spec.agentId);
|
|
105
|
+
const personaPath = join(dir, PERSONA_FILE);
|
|
106
|
+
const playbookPath = spec.playbook !== undefined ? join(dir, PLAYBOOK_FILE) : undefined;
|
|
107
|
+
const profilePath = join(dir, PROFILE_FILE);
|
|
108
|
+
const manifest = {
|
|
109
|
+
agentId: spec.agentId,
|
|
110
|
+
name: spec.name,
|
|
111
|
+
personaPath,
|
|
112
|
+
...(playbookPath ? { playbookPath } : {}),
|
|
113
|
+
createdAt: new Date().toISOString(),
|
|
114
|
+
};
|
|
115
|
+
// Whether the artefact directory already existed before this call. If it did,
|
|
116
|
+
// it is not ours to delete on failure (it may hold a prior expert's artefacts,
|
|
117
|
+
// or — if the path is occupied by a non-directory — a user's file). We only
|
|
118
|
+
// remove what THIS call created (Product invariant 7: nothing partial, without
|
|
119
|
+
// destroying anything pre-existing).
|
|
120
|
+
const dirPreexisted = existsSync(dir);
|
|
121
|
+
try {
|
|
122
|
+
mkdirSync(dir, { recursive: true, mode: DIR_MODE });
|
|
123
|
+
atomicWrite(personaPath, spec.persona);
|
|
124
|
+
if (playbookPath && spec.playbook !== undefined) {
|
|
125
|
+
atomicWrite(playbookPath, spec.playbook);
|
|
126
|
+
}
|
|
127
|
+
atomicWrite(profilePath, JSON.stringify(manifest, null, 2) + "\n");
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
// Leave nothing partial behind — but only tear down the dir if WE created it.
|
|
131
|
+
if (!dirPreexisted) {
|
|
132
|
+
try {
|
|
133
|
+
rmSync(dir, { recursive: true, force: true });
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// Cleanup is best-effort; the original cause is what the caller needs.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const cause = err instanceof Error ? err.message : String(err);
|
|
140
|
+
return {
|
|
141
|
+
ok: false,
|
|
142
|
+
kind: "persistence_failed",
|
|
143
|
+
error: dir + ": " + cause,
|
|
144
|
+
message: "The shopping-expert behaviour artefacts could NOT be written to the sil"
|
|
145
|
+
+ " data directory, so the profile did not stick. Fix the data directory"
|
|
146
|
+
+ " (it must be writable — check permissions / free space / that"
|
|
147
|
+
+ " $SIL_DATA_DIR is a directory), then create the expert again.",
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
ok: true,
|
|
152
|
+
agentId: spec.agentId,
|
|
153
|
+
dir,
|
|
154
|
+
personaPath,
|
|
155
|
+
...(playbookPath ? { playbookPath } : {}),
|
|
156
|
+
profilePath,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/** Atomic single-file write: tmp sibling → write → rename over target → chmod.
|
|
160
|
+
* Mirrors the token/identity write in `credentials.ts` so a half-written
|
|
161
|
+
* artefact never appears (a reader sees the old file or the new one). */
|
|
162
|
+
function atomicWrite(path, contents) {
|
|
163
|
+
const tmp = path + "." + randomBytes(6).toString("hex") + ".tmp";
|
|
164
|
+
writeFileSync(tmp, contents, { mode: FILE_MODE });
|
|
165
|
+
renameSync(tmp, path);
|
|
166
|
+
chmodSync(path, FILE_MODE);
|
|
167
|
+
}
|
|
168
|
+
// ===========================================================================
|
|
169
|
+
// Local expert lifecycle — list / view / remove the artefact half.
|
|
170
|
+
//
|
|
171
|
+
// The manifest (`profile.json`) is the source of truth for "is this a sil
|
|
172
|
+
// expert": a directory under `agents/<id>/` with a readable profile.json IS an
|
|
173
|
+
// expert; a bare host `agents.list[]` entry without one is not ours to manage.
|
|
174
|
+
// The host-config (wiring) half of list/remove is the host agent driving its
|
|
175
|
+
// own `openclaw …` CLI — these primitives only ever touch `$SIL_DATA_DIR`.
|
|
176
|
+
//
|
|
177
|
+
// Each primitive returns a discriminated result and NEVER throws across the
|
|
178
|
+
// boundary (mirroring MaterializeResult), so one corrupt manifest or a single
|
|
179
|
+
// failed delete degrades only its own outcome — never the whole operation.
|
|
180
|
+
// ===========================================================================
|
|
181
|
+
/** Resolve the agents subtree root under the plugin's data dir — the parent of
|
|
182
|
+
* every per-agent artefact directory. The deleter asserts a target is strictly
|
|
183
|
+
* a child of this (and never this itself), so a delete can never reach the
|
|
184
|
+
* parent and wipe sibling experts. */
|
|
185
|
+
function getAgentsRoot() {
|
|
186
|
+
return join(getDataDir(), AGENTS_SUBDIR);
|
|
187
|
+
}
|
|
188
|
+
/** Re-run the writer's exact `agentId` gate at a DIRECT caller of
|
|
189
|
+
* `getAgentArtefactDir`. Returns `null` when valid; an `invalid_request`
|
|
190
|
+
* variant naming the field otherwise. The gate is identical to
|
|
191
|
+
* `materializeProfile`'s validate-first block (`AGENT_ID_RE` + non-`main`),
|
|
192
|
+
* applied BEFORE any `join`/`read`/`rm` so a `../`, `/`, `.`, or `main` never
|
|
193
|
+
* becomes a filesystem path segment. `getAgentArtefactDir`'s SAFETY note
|
|
194
|
+
* mandates exactly this for every direct caller — list does not (it never
|
|
195
|
+
* trusts a caller-supplied id; it reads only ids it discovered on disk), but
|
|
196
|
+
* read and remove DO take a caller id and so call this first. */
|
|
197
|
+
function rejectBadAgentId(agentId) {
|
|
198
|
+
if (!nonBlank(agentId)) {
|
|
199
|
+
return invalidAgentId("agentId is required and must be non-empty.");
|
|
200
|
+
}
|
|
201
|
+
if (agentId === "main") {
|
|
202
|
+
return invalidAgentId('"main" is host-reserved and is not a sil expert id.');
|
|
203
|
+
}
|
|
204
|
+
if (!AGENT_ID_RE.test(agentId)) {
|
|
205
|
+
return invalidAgentId("agentId must be lower-kebab (a-z, 0-9, hyphen) — got: " + JSON.stringify(agentId));
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
function invalidAgentId(message) {
|
|
210
|
+
return { ok: false, kind: "invalid_request", field: "agentId", message };
|
|
211
|
+
}
|
|
212
|
+
/** Read + parse one `agents/<id>/profile.json` into a typed manifest, or throw
|
|
213
|
+
* a descriptive error the caller maps to an `unreadable`/`not_found` outcome.
|
|
214
|
+
* A manifest whose JSON parses but is missing the required string fields is
|
|
215
|
+
* treated as corrupt (an interrupted or hand-edited write) — not silently
|
|
216
|
+
* coerced. */
|
|
217
|
+
function readManifestFile(profilePath) {
|
|
218
|
+
const raw = readFileSync(profilePath, "utf8");
|
|
219
|
+
const parsed = JSON.parse(raw);
|
|
220
|
+
if (typeof parsed !== "object" ||
|
|
221
|
+
parsed === null ||
|
|
222
|
+
typeof parsed.agentId !== "string" ||
|
|
223
|
+
typeof parsed.name !== "string" ||
|
|
224
|
+
typeof parsed.personaPath !== "string" ||
|
|
225
|
+
typeof parsed.createdAt !== "string") {
|
|
226
|
+
throw new Error("profile.json is missing required manifest fields");
|
|
227
|
+
}
|
|
228
|
+
return parsed;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Enumerate the materialized experts under each `$SIL_DATA_DIR/agents/<id>/`.
|
|
232
|
+
*
|
|
233
|
+
* The manifest is the source of truth — each `agents/<id>/profile.json` is read
|
|
234
|
+
* and parsed (no dirname guessing). Experts are returned `createdAt` DESC (the
|
|
235
|
+
* just-made expert first, the one the user most likely wants to act on). An
|
|
236
|
+
* absent/empty store yields a normal empty listing. One unreadable or corrupt
|
|
237
|
+
* manifest lands in `unreadable[]` and never aborts the listing — the healthy
|
|
238
|
+
* experts still list. Reads only; never writes, never reads a token.
|
|
239
|
+
*/
|
|
240
|
+
export function listAgentProfiles() {
|
|
241
|
+
const root = getAgentsRoot();
|
|
242
|
+
if (!existsSync(root)) {
|
|
243
|
+
return { ok: true, experts: [], unreadable: [] };
|
|
244
|
+
}
|
|
245
|
+
const experts = [];
|
|
246
|
+
const unreadable = [];
|
|
247
|
+
let entries;
|
|
248
|
+
try {
|
|
249
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
// The agents root exists but cannot be enumerated (e.g. EACCES). Surface it
|
|
253
|
+
// as a single degraded entry rather than throwing across the boundary.
|
|
254
|
+
unreadable.push({
|
|
255
|
+
agentId: AGENTS_SUBDIR,
|
|
256
|
+
error: errCause(err),
|
|
257
|
+
});
|
|
258
|
+
return { ok: true, experts, unreadable };
|
|
259
|
+
}
|
|
260
|
+
for (const entry of entries) {
|
|
261
|
+
if (!entry.isDirectory())
|
|
262
|
+
continue;
|
|
263
|
+
const agentId = entry.name;
|
|
264
|
+
const profilePath = join(root, agentId, PROFILE_FILE);
|
|
265
|
+
if (!existsSync(profilePath)) {
|
|
266
|
+
// A directory under agents/ with no manifest is not a (loadable) sil
|
|
267
|
+
// expert — report it degraded so the user sees the residue (e.g. a
|
|
268
|
+
// remove that cleared the manifest but left a stray file).
|
|
269
|
+
unreadable.push({ agentId, error: "no profile.json manifest" });
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
const manifest = readManifestFile(profilePath);
|
|
274
|
+
experts.push({
|
|
275
|
+
agentId: manifest.agentId,
|
|
276
|
+
name: manifest.name,
|
|
277
|
+
hasPlaybook: manifest.playbookPath !== undefined,
|
|
278
|
+
createdAt: manifest.createdAt,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
unreadable.push({ agentId, error: errCause(err) });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// Most-recently-created first (Product Flow 1). createdAt is ISO 8601, so a
|
|
286
|
+
// lexical compare is also chronological; fall back to it when Date.parse is
|
|
287
|
+
// NaN (a corrupt-but-parseable date) so the sort is total and stable-ish.
|
|
288
|
+
experts.sort((a, b) => createdAtDesc(a.createdAt, b.createdAt));
|
|
289
|
+
return { ok: true, experts, unreadable };
|
|
290
|
+
}
|
|
291
|
+
/** Compare two ISO 8601 createdAt strings, most-recent first. */
|
|
292
|
+
function createdAtDesc(a, b) {
|
|
293
|
+
const ta = Date.parse(a);
|
|
294
|
+
const tb = Date.parse(b);
|
|
295
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) {
|
|
296
|
+
// Lexical fallback keeps the sort total when a timestamp is unparseable.
|
|
297
|
+
return a < b ? 1 : a > b ? -1 : 0;
|
|
298
|
+
}
|
|
299
|
+
return tb - ta;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Read one expert's full detail — the manifest PLUS the persona/playbook bodies
|
|
303
|
+
* from the files it points at — so the skill can render a complete view.
|
|
304
|
+
*
|
|
305
|
+
* Re-runs the writer's `agentId` gate BEFORE any `join`/read (a traversal-shaped
|
|
306
|
+
* id → `invalid_request`, reading nothing). An unknown id, an absent/unreadable
|
|
307
|
+
* manifest, or an absent persona body → `not_found` (a degraded expert is, from
|
|
308
|
+
* the user's view, not viewable). Never throws across the boundary; reads only.
|
|
309
|
+
*/
|
|
310
|
+
export function readAgentProfile(agentId) {
|
|
311
|
+
const bad = rejectBadAgentId(agentId);
|
|
312
|
+
if (bad)
|
|
313
|
+
return bad;
|
|
314
|
+
const dir = getAgentArtefactDir(agentId);
|
|
315
|
+
const profilePath = join(dir, PROFILE_FILE);
|
|
316
|
+
if (!existsSync(profilePath)) {
|
|
317
|
+
return notFoundRead(agentId);
|
|
318
|
+
}
|
|
319
|
+
let manifest;
|
|
320
|
+
try {
|
|
321
|
+
manifest = readManifestFile(profilePath);
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
// A corrupt/half-written manifest is, to the viewer, an expert that cannot
|
|
325
|
+
// be loaded — surface not_found (the skill lists the healthy ones), never a
|
|
326
|
+
// raw parse error across the boundary.
|
|
327
|
+
return notFoundRead(agentId);
|
|
328
|
+
}
|
|
329
|
+
let persona;
|
|
330
|
+
try {
|
|
331
|
+
persona = readFileSync(manifest.personaPath, "utf8");
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
// Manifest present but its persona body is gone (a partial removal, or a
|
|
335
|
+
// hand-deleted file) — the expert is not viewable.
|
|
336
|
+
return notFoundRead(agentId);
|
|
337
|
+
}
|
|
338
|
+
let playbook;
|
|
339
|
+
if (manifest.playbookPath !== undefined) {
|
|
340
|
+
try {
|
|
341
|
+
playbook = readFileSync(manifest.playbookPath, "utf8");
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
// The playbook is optional detail; its absence does not make the expert
|
|
345
|
+
// unviewable. Render the rest without it.
|
|
346
|
+
playbook = undefined;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
ok: true,
|
|
351
|
+
agentId: manifest.agentId,
|
|
352
|
+
name: manifest.name,
|
|
353
|
+
persona,
|
|
354
|
+
...(playbook !== undefined ? { playbook } : {}),
|
|
355
|
+
profilePath,
|
|
356
|
+
createdAt: manifest.createdAt,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function notFoundRead(agentId) {
|
|
360
|
+
return {
|
|
361
|
+
ok: false,
|
|
362
|
+
kind: "not_found",
|
|
363
|
+
agentId,
|
|
364
|
+
message: 'No sil expert "' + agentId + '" — list your experts to see which exist.',
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Remove one expert's behaviour-artefact directory — the sil-side half of a
|
|
369
|
+
* clean removal (the host-wiring half is the skill's `openclaw` CLI step; the
|
|
370
|
+
* plugin cannot write `~/.openclaw`).
|
|
371
|
+
*
|
|
372
|
+
* Fail-closed and scoped to exactly the named expert:
|
|
373
|
+
* - re-runs the writer's `agentId` gate ITSELF (never trusts the caller) — a
|
|
374
|
+
* bad/`main`/traversal-shaped id → `invalid_request`, deletes NOTHING;
|
|
375
|
+
* - asserts the resolved target is strictly UNDER `getDataDir()/agents/` and
|
|
376
|
+
* is NOT the `agents/` parent, so a delete can never escape the subtree or
|
|
377
|
+
* wipe sibling experts;
|
|
378
|
+
* - `rmSync(dir, { recursive, force })` the single leaf dir.
|
|
379
|
+
*
|
|
380
|
+
* Absent target → `not_found` (idempotent — a re-run after a partial host-CLI
|
|
381
|
+
* failure is safe). A genuine `rmSync` failure (e.g. EACCES) →
|
|
382
|
+
* `persistence_failed` with `<dir>: <cause>`. Never throws across the boundary.
|
|
383
|
+
*/
|
|
384
|
+
export function removeAgentArtefacts(agentId) {
|
|
385
|
+
const bad = rejectBadAgentId(agentId);
|
|
386
|
+
if (bad)
|
|
387
|
+
return bad;
|
|
388
|
+
const dir = getAgentArtefactDir(agentId);
|
|
389
|
+
// Defence in depth beyond the id gate: assert the resolved path is strictly a
|
|
390
|
+
// child of the agents root and never the root itself. A `rmSync` of the
|
|
391
|
+
// parent would wipe every sibling expert — refuse it even if some future
|
|
392
|
+
// change to the gate let a separator-bearing id through.
|
|
393
|
+
const root = getAgentsRoot();
|
|
394
|
+
if (dirname(dir) !== root || dir === root) {
|
|
395
|
+
return invalidAgentId("agentId resolves outside the agents subtree — refusing to delete: " +
|
|
396
|
+
JSON.stringify(agentId));
|
|
397
|
+
}
|
|
398
|
+
if (!existsSync(dir)) {
|
|
399
|
+
return {
|
|
400
|
+
ok: false,
|
|
401
|
+
kind: "not_found",
|
|
402
|
+
agentId,
|
|
403
|
+
message: 'No sil expert "' + agentId + '" to remove (already gone) — list your'
|
|
404
|
+
+ " experts to see which exist. A re-run is safe (idempotent).",
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
try {
|
|
408
|
+
rmSync(dir, { recursive: true, force: true });
|
|
409
|
+
}
|
|
410
|
+
catch (err) {
|
|
411
|
+
return {
|
|
412
|
+
ok: false,
|
|
413
|
+
kind: "persistence_failed",
|
|
414
|
+
error: dir + ": " + errCause(err),
|
|
415
|
+
message: "The expert's behaviour artefacts could NOT be removed from the sil data"
|
|
416
|
+
+ " directory. Fix the data directory (it must be writable — check"
|
|
417
|
+
+ " permissions), then remove the expert again.",
|
|
418
|
+
recovery: "fix_data_dir",
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
return { ok: true, agentId };
|
|
422
|
+
}
|
|
423
|
+
/** Extract a human-readable cause from an unknown thrown value (never PII). */
|
|
424
|
+
function errCause(err) {
|
|
425
|
+
return err instanceof Error ? err.message : String(err);
|
|
426
|
+
}
|