sil-openclaw 0.3.5 → 0.3.6
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 +38 -0
- package/README.md +8 -9
- package/dist/lib/profile-store.js +520 -337
- package/dist/tools/profile.js +317 -194
- package/openclaw.plugin.json +4 -4
- package/package.json +1 -1
- package/sil-shopping/SKILL.md +18 -17
- package/sil-shopping/examples/multi_domain_shopper_walkthrough.md +190 -0
- package/sil-shopping/references/agent_creation_engine.md +35 -32
- package/sil-shopping/references/brainstorm_interview.md +31 -47
- package/sil-shopping/references/manage_domains.md +56 -0
- package/sil-shopping/references/refine_shopper.md +48 -0
- package/sil-shopping/references/search_param_mapping.md +12 -12
- package/sil-shopping/references/shop_loop.md +109 -0
- package/sil-shopping/examples/road_cycling_expert_walkthrough.md +0 -150
- package/sil-shopping/references/expert_shopping.md +0 -79
- package/sil-shopping/references/manage_experts.md +0 -42
- package/sil-shopping/references/refine_expert.md +0 -43
|
@@ -1,70 +1,68 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* sil
|
|
2
|
+
* sil shopper behaviour-artefact store — the single multi-domain shopper layout.
|
|
3
3
|
*
|
|
4
4
|
* The agent-creation engine (the bundled `sil-shopping/SKILL.md` procedure driving
|
|
5
|
-
* the OpenClaw host CLI) registers the *wiring* of
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* the OpenClaw host CLI) registers the *wiring* of the one sil shopper into the
|
|
6
|
+
* host's own config — the `agents.list[]` entry, the enabled `sil` plugin, the
|
|
7
|
+
* attached `sil` skill. That host-config write is the host agent driving its own
|
|
8
|
+
* `openclaw …` CLI; the plugin never touches `~/.openclaw`.
|
|
9
9
|
*
|
|
10
|
-
* What the plugin DOES own is the
|
|
10
|
+
* What the plugin DOES own is the shopper's *behaviour* layer, materialized into
|
|
11
11
|
* its own disclosed `filesystemScope` — `$SIL_DATA_DIR` (the same directory
|
|
12
|
-
* `lib/credentials.ts` uses for tokens/identity).
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* `lib/credentials.ts` uses for tokens/identity). There is ONE persistent shopper
|
|
13
|
+
* (a SINGLETON), so its artefacts live under a fixed, compile-time-constant
|
|
14
|
+
* sub-directory — NOT a caller-supplied key. The store keeps a SHARED user spec
|
|
15
|
+
* (the one person) plus a per-domain pack that is minted LAZILY on the first shop
|
|
16
|
+
* in a niche:
|
|
15
17
|
*
|
|
16
|
-
* $SIL_DATA_DIR/
|
|
17
|
-
* ├─
|
|
18
|
-
* │
|
|
19
|
-
* │
|
|
20
|
-
* │
|
|
21
|
-
*
|
|
22
|
-
* │
|
|
23
|
-
*
|
|
24
|
-
* │
|
|
25
|
-
* │
|
|
26
|
-
*
|
|
27
|
-
* │
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
18
|
+
* $SIL_DATA_DIR/shopper/
|
|
19
|
+
* ├─ user_spec.md SHARED — the one person: addresses, sizes,
|
|
20
|
+
* │ allergy/ethics HARD constraints, budget
|
|
21
|
+
* │ psychology. Read by EVERY domain's shop loop, so a
|
|
22
|
+
* │ fact captured while shopping niche A is reused in
|
|
23
|
+
* │ niche B without being re-asked. Overwritten
|
|
24
|
+
* │ (full-body) on every materialize call.
|
|
25
|
+
* ├─ profile.json the strictly-typed manifest the sil skill resolves
|
|
26
|
+
* │ artefacts from — name + userSpecPath + a
|
|
27
|
+
* │ slug-keyed `domains` MAP (the source of truth; no
|
|
28
|
+
* │ filesystem guessing). `domains: {}` is the HEALTHY
|
|
29
|
+
* │ state of a freshly-created shopper.
|
|
30
|
+
* └─ domains/<slug>/
|
|
31
|
+
* ├─ domain_spec.md per-domain: deep researched niche expertise.
|
|
32
|
+
* ├─ intent_spec.md per-domain: the decomposition DIMENSIONS a query
|
|
33
|
+
* │ must resolve (the dimension SCHEMA only — the
|
|
34
|
+
* │ per-query intent is ephemeral, never persisted).
|
|
35
|
+
* └─ playbook.md per-domain: the user's buying TASTE for THIS niche.
|
|
34
36
|
*
|
|
35
|
-
* The persona is NOT a sil artefact: the
|
|
37
|
+
* The persona is NOT a sil artefact: the shopper's identity/voice/standing rules
|
|
36
38
|
* are the host workspace `SOUL.md`, written directly via the host CLI by the
|
|
37
|
-
* engine — there is no `persona.md`
|
|
38
|
-
*
|
|
39
|
+
* engine — there is no `persona.md` here and no copy step. This store holds only
|
|
40
|
+
* the SHARED user spec and the per-domain SDS packs.
|
|
39
41
|
*
|
|
40
|
-
*
|
|
41
|
-
* `
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* learning. All four are refine-mutable in place. The per-query intent is NOT a
|
|
48
|
-
* slot here: only the intent_spec *dimension schema* is persisted; the filled
|
|
49
|
-
* dimensions for one request are ephemeral, derived in the conversation.
|
|
42
|
+
* `sil_profile_materialize` stays ONE tool with an OPTIONAL `domain` pack:
|
|
43
|
+
* - no `domain` ⇒ CREATE the shopper: write the shared `user_spec.md` + a
|
|
44
|
+
* `profile.json` with `domains: {}`. NO `domains/` dir is created yet.
|
|
45
|
+
* - with `domain` ⇒ lazily MINT/refresh that niche: write
|
|
46
|
+
* `domains/<slug>/{domain_spec,intent_spec,playbook}.md`, overwrite the shared
|
|
47
|
+
* `user_spec.md`, and upsert `domains[slug]` — one atomic call. Persisting a
|
|
48
|
+
* cross-niche fact surfaced in the same query is therefore native.
|
|
50
49
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* never
|
|
65
|
-
* — see `readAgentProfile`). The catch block below encodes exactly this split.
|
|
50
|
+
* Write safety: a bad spec writes NOTHING (validate-first). Each artefact is
|
|
51
|
+
* written atomically — tmp file → write → rename over target → chmod 0600, dir
|
|
52
|
+
* 0700 (mirroring `credentials.ts`) — so a reader never sees a half-written file.
|
|
53
|
+
* Write order on a mint is `domains/<slug>/* → shared user_spec → profile.json
|
|
54
|
+
* LAST`, so a crash never leaves a registered-but-absent pack: the worst outcome
|
|
55
|
+
* is an ORPHANED, unreferenced `domains/<slug>/` leaf, which readers gate out via
|
|
56
|
+
* the manifest (fail-closed). On a mid-write failure the teardown is keyed on
|
|
57
|
+
* MANIFEST MEMBERSHIP, NOT filesystem pre-existence: a failed FIRST mint of a NEW
|
|
58
|
+
* domain (slug ∉ the existing `domains` map) tears down ONLY that fresh leaf — the
|
|
59
|
+
* shopper dir, the shared user_spec, and sibling domains survive; a failed re-mint
|
|
60
|
+
* of an EXISTING domain is dir-preserving (the prior pack is left intact and is
|
|
61
|
+
* never served half-refined — a referenced-but-missing body fails the read closed,
|
|
62
|
+
* see `readAgentProfile`). A leaf we did not create (the path was already occupied)
|
|
63
|
+
* is never torn down.
|
|
66
64
|
*/
|
|
67
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync,
|
|
65
|
+
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
68
66
|
import { dirname, join } from "node:path";
|
|
69
67
|
import { randomBytes } from "node:crypto";
|
|
70
68
|
import { DIR_MODE, ensureDataDir, getDataDir } from "./credentials.js";
|
|
@@ -72,33 +70,47 @@ import { DIR_MODE, ensureDataDir, getDataDir } from "./credentials.js";
|
|
|
72
70
|
* mode (`DIR_MODE`, `0o700`) is owned by `credentials.ts` (the data-dir owner)
|
|
73
71
|
* and imported, so the data-home permission lives in exactly one place. */
|
|
74
72
|
const FILE_MODE = 0o600;
|
|
75
|
-
/** The sub-tree under `$SIL_DATA_DIR` that holds
|
|
76
|
-
|
|
73
|
+
/** The fixed, compile-time-constant sub-tree under `$SIL_DATA_DIR` that holds the
|
|
74
|
+
* ONE shopper's behaviour artefacts. The product is a SINGLETON, so this is a
|
|
75
|
+
* constant — NOT caller input — which makes the shopper path segment un-spoofable
|
|
76
|
+
* (no caller-controlled `agentId` traversal vector). */
|
|
77
|
+
const SHOPPER_SUBDIR = "shopper";
|
|
78
|
+
/** The sub-tree under the shopper dir that holds the per-domain packs. */
|
|
79
|
+
const DOMAINS_SUBDIR = "domains";
|
|
77
80
|
/** Artefact filenames (stable — the sil skill resolves them from profile.json,
|
|
78
|
-
* but the names are also documented so a human can find them). The
|
|
79
|
-
*
|
|
81
|
+
* but the names are also documented so a human can find them). The shared user
|
|
82
|
+
* spec sits at the shopper level; the three SDS pack files sit under each domain. */
|
|
83
|
+
const USER_SPEC_FILE = "user_spec.md";
|
|
80
84
|
const DOMAIN_SPEC_FILE = "domain_spec.md";
|
|
81
85
|
const INTENT_SPEC_FILE = "intent_spec.md";
|
|
82
|
-
const USER_SPEC_FILE = "user_spec.md";
|
|
83
86
|
const PLAYBOOK_FILE = "playbook.md";
|
|
84
87
|
const PROFILE_FILE = "profile.json";
|
|
85
|
-
/**
|
|
88
|
+
/** The stable, case-insensitive marker the shop loop's reject-at-pick rule greps
|
|
89
|
+
* to find an appended hard constraint (e.g. `- [hard] never leather`). */
|
|
90
|
+
const HARD_MARKER = "[hard]";
|
|
91
|
+
/** A domain slug must be lower-kebab and is never `main` (host-reserved). It is
|
|
92
|
+
* joined as a filesystem path segment, so it carries this guard. (The shopper dir
|
|
93
|
+
* segment is the un-spoofable constant `SHOPPER_SUBDIR` — it needs no guard.) */
|
|
86
94
|
const AGENT_ID_RE = /^[a-z0-9][a-z0-9-]*$/;
|
|
87
|
-
/** Resolve the
|
|
88
|
-
* hardcoded — `getDataDir()` honors `$SIL_DATA_DIR`/`$XDG_DATA_HOME`.
|
|
95
|
+
/** Resolve the SINGLETON shopper artefact directory under the plugin's data dir.
|
|
96
|
+
* Never hardcoded — `getDataDir()` honors `$SIL_DATA_DIR`/`$XDG_DATA_HOME`.
|
|
97
|
+
*
|
|
98
|
+
* SAFETY: the shopper segment is the compile-time constant `SHOPPER_SUBDIR`, NOT
|
|
99
|
+
* caller input, so there is no path-traversal vector here (nothing to spoof). The
|
|
100
|
+
* only caller-supplied segment in the store is the domain `slug` — see
|
|
101
|
+
* `getDomainArtefactDir`, which every caller guards before the join. */
|
|
102
|
+
export function getShopperArtefactDir() {
|
|
103
|
+
return join(getDataDir(), SHOPPER_SUBDIR);
|
|
104
|
+
}
|
|
105
|
+
/** Resolve a per-domain pack directory under the singleton shopper dir.
|
|
89
106
|
*
|
|
90
|
-
* SAFETY:
|
|
91
|
-
* verbatim
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
* duplicated here would be redundant for that path. A FUTURE DIRECT CALLER of this
|
|
98
|
-
* exported resolver would bypass the guard — re-run `AGENT_ID_RE` (or route through
|
|
99
|
-
* `materializeProfile`) before trusting the returned path with any filesystem op. */
|
|
100
|
-
export function getAgentArtefactDir(agentId) {
|
|
101
|
-
return join(getDataDir(), AGENTS_SUBDIR, agentId);
|
|
107
|
+
* SAFETY: `slug` is the ONLY caller-supplied path segment in the store. It is
|
|
108
|
+
* joined verbatim here, so every caller MUST validate it with `rejectBadSegment`
|
|
109
|
+
* (`AGENT_ID_RE` + non-`main`) BEFORE the join. A FUTURE DIRECT CALLER would bypass
|
|
110
|
+
* that guard — re-validate `slug` before trusting the returned path with any
|
|
111
|
+
* filesystem op. */
|
|
112
|
+
export function getDomainArtefactDir(slug) {
|
|
113
|
+
return join(getShopperArtefactDir(), DOMAINS_SUBDIR, slug);
|
|
102
114
|
}
|
|
103
115
|
/** True when `s` is a present, non-blank string. */
|
|
104
116
|
function nonBlank(s) {
|
|
@@ -107,87 +119,152 @@ function nonBlank(s) {
|
|
|
107
119
|
function invalid(field, message) {
|
|
108
120
|
return { ok: false, kind: "invalid_request", field, message };
|
|
109
121
|
}
|
|
122
|
+
/** Validate the domain-slug path-segment field the SAME way at every caller:
|
|
123
|
+
* present + non-blank, never `main`, lower-kebab. Returns `null` when valid, an
|
|
124
|
+
* `invalid_request` naming the field otherwise — applied BEFORE any
|
|
125
|
+
* join/read/delete so a parent-directory traversal, an absolute path, a bare dot,
|
|
126
|
+
* or `main` never becomes a filesystem path segment. */
|
|
127
|
+
function rejectBadSegment(value, field) {
|
|
128
|
+
if (!nonBlank(value)) {
|
|
129
|
+
return invalid(field, field + " is required and must be non-empty.");
|
|
130
|
+
}
|
|
131
|
+
if (value === "main") {
|
|
132
|
+
return invalid(field, '"main" is host-reserved and cannot be a ' + field + ".");
|
|
133
|
+
}
|
|
134
|
+
if (!AGENT_ID_RE.test(value)) {
|
|
135
|
+
return invalid(field, field + " must be lower-kebab (a-z, 0-9, hyphen) — got: " + JSON.stringify(value));
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
110
139
|
/**
|
|
111
|
-
* Materialize the
|
|
140
|
+
* Materialize the shopper / a domain pack.
|
|
112
141
|
*
|
|
113
|
-
* Validate-first
|
|
114
|
-
*
|
|
115
|
-
* `
|
|
116
|
-
*
|
|
142
|
+
* Validate-first (deterministic field order: `name` → `userSpec` →, when `domain`
|
|
143
|
+
* is present, `domain.slug` → `domain.name` → `domain.domainSpec` →
|
|
144
|
+
* `domain.intentSpec` → `domain.playbook`): any invalid field returns
|
|
145
|
+
* `invalid_request` naming it and writes NOTHING.
|
|
146
|
+
*
|
|
147
|
+
* - no `domain` ⇒ write the shared `user_spec.md` + `profile.json` (`domains: {}`);
|
|
148
|
+
* no `domains/` dir.
|
|
149
|
+
* - with `domain` ⇒ write `domains/<slug>/*` → overwrite the shared `user_spec.md`
|
|
150
|
+
* → write `profile.json` LAST (upsert `domains[slug]`; preserve `createdAt` on a
|
|
151
|
+
* re-mint, advance `updatedAt`). `domain` is echoed in the ok result.
|
|
117
152
|
*/
|
|
118
153
|
export function materializeProfile(spec) {
|
|
119
154
|
// --- validate-first (nothing is written until every field is good) ---
|
|
120
|
-
if (!nonBlank(spec.agentId)) {
|
|
121
|
-
return invalid("agentId", "agentId is required and must be non-empty.");
|
|
122
|
-
}
|
|
123
|
-
if (spec.agentId === "main") {
|
|
124
|
-
return invalid("agentId", '"main" is host-reserved and cannot be a sil expert id.');
|
|
125
|
-
}
|
|
126
|
-
if (!AGENT_ID_RE.test(spec.agentId)) {
|
|
127
|
-
return invalid("agentId", "agentId must be lower-kebab (a-z, 0-9, hyphen) — got: " + JSON.stringify(spec.agentId));
|
|
128
|
-
}
|
|
129
155
|
if (!nonBlank(spec.name)) {
|
|
130
156
|
return invalid("name", "name is required and must be non-empty.");
|
|
131
157
|
}
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
// review round 2): they are seeded *partial* at creation, then augmented every
|
|
135
|
-
// query. A present-but-blank or omitted spec is rejected (validate-first; write
|
|
136
|
-
// nothing).
|
|
137
|
-
if (!nonBlank(spec.domainSpec)) {
|
|
138
|
-
return invalid("domainSpec", "domainSpec is required and must be non-empty.");
|
|
139
|
-
}
|
|
140
|
-
if (!nonBlank(spec.intentSpec)) {
|
|
141
|
-
return invalid("intentSpec", "intentSpec is required and must be non-empty.");
|
|
142
|
-
}
|
|
158
|
+
// The SHARED user spec is REQUIRED non-blank on EVERY call — create AND mint —
|
|
159
|
+
// so a mint can never blank out the one person's standing facts/constraints.
|
|
143
160
|
if (!nonBlank(spec.userSpec)) {
|
|
144
161
|
return invalid("userSpec", "userSpec is required and must be non-empty.");
|
|
145
162
|
}
|
|
146
|
-
|
|
147
|
-
|
|
163
|
+
const domain = spec.domain;
|
|
164
|
+
if (domain !== undefined) {
|
|
165
|
+
// The slug is the caller-supplied filesystem path segment — guard it BEFORE any
|
|
166
|
+
// join. All five pack fields are REQUIRED non-blank.
|
|
167
|
+
const badSlug = rejectBadSegment(domain.slug, "domain.slug");
|
|
168
|
+
if (badSlug)
|
|
169
|
+
return badSlug;
|
|
170
|
+
if (!nonBlank(domain.name)) {
|
|
171
|
+
return invalid("domain.name", "domain.name is required and must be non-empty.");
|
|
172
|
+
}
|
|
173
|
+
if (!nonBlank(domain.domainSpec)) {
|
|
174
|
+
return invalid("domain.domainSpec", "domain.domainSpec is required and must be non-empty.");
|
|
175
|
+
}
|
|
176
|
+
if (!nonBlank(domain.intentSpec)) {
|
|
177
|
+
return invalid("domain.intentSpec", "domain.intentSpec is required and must be non-empty.");
|
|
178
|
+
}
|
|
179
|
+
if (!nonBlank(domain.playbook)) {
|
|
180
|
+
return invalid("domain.playbook", "domain.playbook is required and must be non-empty.");
|
|
181
|
+
}
|
|
148
182
|
}
|
|
149
|
-
const dir =
|
|
150
|
-
const domainSpecPath = join(dir, DOMAIN_SPEC_FILE);
|
|
151
|
-
const intentSpecPath = join(dir, INTENT_SPEC_FILE);
|
|
183
|
+
const dir = getShopperArtefactDir();
|
|
152
184
|
const userSpecPath = join(dir, USER_SPEC_FILE);
|
|
153
|
-
const playbookPath = join(dir, PLAYBOOK_FILE);
|
|
154
185
|
const profilePath = join(dir, PROFILE_FILE);
|
|
186
|
+
// The manifest is the source of truth: read what is already on disk so a mint is
|
|
187
|
+
// an UPSERT (the createdAt of the shopper and of any existing domain is
|
|
188
|
+
// preserved) rather than a clobber. A corrupt/absent manifest is treated as a
|
|
189
|
+
// fresh shopper — re-materialize heals it.
|
|
190
|
+
const existing = tryReadManifest(profilePath);
|
|
191
|
+
const createdAt = existing?.createdAt ?? new Date().toISOString();
|
|
192
|
+
const domains = existing ? { ...existing.domains } : {};
|
|
193
|
+
// Per-mint state, resolved BEFORE the write so the catch block knows whether the
|
|
194
|
+
// failed leaf is ours to tear down (manifest membership) and whether the path
|
|
195
|
+
// pre-existed (never delete a path we did not create).
|
|
196
|
+
let leaf = "";
|
|
197
|
+
let entry;
|
|
198
|
+
let domainIsNew = false;
|
|
199
|
+
let leafPreexisted = false;
|
|
200
|
+
if (domain !== undefined) {
|
|
201
|
+
leaf = getDomainArtefactDir(domain.slug);
|
|
202
|
+
const prior = domains[domain.slug];
|
|
203
|
+
domainIsNew = prior === undefined;
|
|
204
|
+
leafPreexisted = existsSync(leaf);
|
|
205
|
+
const now = new Date().toISOString();
|
|
206
|
+
entry = {
|
|
207
|
+
slug: domain.slug,
|
|
208
|
+
name: domain.name,
|
|
209
|
+
domainSpecPath: join(leaf, DOMAIN_SPEC_FILE),
|
|
210
|
+
intentSpecPath: join(leaf, INTENT_SPEC_FILE),
|
|
211
|
+
playbookPath: join(leaf, PLAYBOOK_FILE),
|
|
212
|
+
createdAt: prior?.createdAt ?? now,
|
|
213
|
+
updatedAt: now,
|
|
214
|
+
};
|
|
215
|
+
domains[domain.slug] = entry;
|
|
216
|
+
}
|
|
155
217
|
const manifest = {
|
|
156
|
-
agentId: spec.agentId,
|
|
157
218
|
name: spec.name,
|
|
158
|
-
domainSpecPath,
|
|
159
|
-
intentSpecPath,
|
|
160
219
|
userSpecPath,
|
|
161
|
-
|
|
162
|
-
|
|
220
|
+
createdAt,
|
|
221
|
+
domains,
|
|
163
222
|
};
|
|
164
|
-
// Whether the
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
// destroying anything pre-existing).
|
|
223
|
+
// Whether the shopper dir already existed. A fresh CREATE that fails is torn down
|
|
224
|
+
// whole (all-or-nothing); a MINT never tears down the shopper dir (it holds the
|
|
225
|
+
// shared user_spec + sibling domains) — only the fresh leaf, and only if it is
|
|
226
|
+
// ours.
|
|
169
227
|
const dirPreexisted = existsSync(dir);
|
|
170
228
|
try {
|
|
171
|
-
// Ensure the data home (`$SIL_DATA_DIR`, 0700) exists first — its creation
|
|
172
|
-
//
|
|
173
|
-
// re-heals it if it was deleted mid-session. Then create the per-agent leaf
|
|
174
|
-
// (`agents/<id>`), which is profile-store's own concern.
|
|
229
|
+
// Ensure the data home (`$SIL_DATA_DIR`, 0700) exists first — its creation and
|
|
230
|
+
// mode are owned by credentials.ts — then the shopper leaf.
|
|
175
231
|
ensureDataDir();
|
|
176
232
|
mkdirSync(dir, { recursive: true, mode: DIR_MODE });
|
|
177
|
-
|
|
178
|
-
|
|
233
|
+
if (domain !== undefined && entry !== undefined) {
|
|
234
|
+
// Write order: the domain pack FIRST, the shared user_spec, the manifest
|
|
235
|
+
// LAST — so a crash before the manifest write leaves only an orphaned,
|
|
236
|
+
// unreferenced leaf (invisible to readers), never a registered-but-absent
|
|
237
|
+
// pack.
|
|
238
|
+
mkdirSync(leaf, { recursive: true, mode: DIR_MODE });
|
|
239
|
+
atomicWrite(entry.domainSpecPath, domain.domainSpec);
|
|
240
|
+
atomicWrite(entry.intentSpecPath, domain.intentSpec);
|
|
241
|
+
atomicWrite(entry.playbookPath, domain.playbook);
|
|
242
|
+
}
|
|
179
243
|
atomicWrite(userSpecPath, spec.userSpec);
|
|
180
|
-
atomicWrite(playbookPath, spec.playbook);
|
|
181
244
|
atomicWrite(profilePath, JSON.stringify(manifest, null, 2) + "\n");
|
|
182
245
|
}
|
|
183
246
|
catch (err) {
|
|
184
|
-
|
|
185
|
-
|
|
247
|
+
if (domain !== undefined) {
|
|
248
|
+
// MINT teardown — keyed on manifest membership, NOT filesystem pre-existence:
|
|
249
|
+
// tear down ONLY a fresh leaf this call created for a NEW domain. A re-mint of
|
|
250
|
+
// an EXISTING domain is dir-preserving (the prior pack survives), and a leaf
|
|
251
|
+
// path already occupied (the test's blocking file) is never our leaf to remove.
|
|
252
|
+
if (domainIsNew && !leafPreexisted) {
|
|
253
|
+
try {
|
|
254
|
+
rmSync(leaf, { recursive: true, force: true });
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
// Cleanup is best-effort; the original cause is what the caller needs.
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else if (!dirPreexisted) {
|
|
262
|
+
// Fresh CREATE — leave nothing partial behind, but only if WE created the dir.
|
|
186
263
|
try {
|
|
187
264
|
rmSync(dir, { recursive: true, force: true });
|
|
188
265
|
}
|
|
189
266
|
catch {
|
|
190
|
-
//
|
|
267
|
+
// best-effort
|
|
191
268
|
}
|
|
192
269
|
}
|
|
193
270
|
const cause = err instanceof Error ? err.message : String(err);
|
|
@@ -195,22 +272,16 @@ export function materializeProfile(spec) {
|
|
|
195
272
|
ok: false,
|
|
196
273
|
kind: "persistence_failed",
|
|
197
274
|
error: dir + ": " + cause,
|
|
198
|
-
message: "The
|
|
199
|
-
+ "
|
|
200
|
-
+ "
|
|
201
|
-
+ "
|
|
275
|
+
message: "The shopper behaviour artefacts could NOT be written to the sil data"
|
|
276
|
+
+ " directory, so the change did not stick. Fix the data directory (it must"
|
|
277
|
+
+ " be writable — check permissions / free space / that $SIL_DATA_DIR is a"
|
|
278
|
+
+ " directory), then try again.",
|
|
202
279
|
};
|
|
203
280
|
}
|
|
204
|
-
|
|
205
|
-
ok: true,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
domainSpecPath,
|
|
209
|
-
intentSpecPath,
|
|
210
|
-
userSpecPath,
|
|
211
|
-
playbookPath,
|
|
212
|
-
profilePath,
|
|
213
|
-
};
|
|
281
|
+
if (domain !== undefined && entry !== undefined) {
|
|
282
|
+
return { ok: true, dir, userSpecPath, profilePath, domain: entry };
|
|
283
|
+
}
|
|
284
|
+
return { ok: true, dir, userSpecPath, profilePath };
|
|
214
285
|
}
|
|
215
286
|
/** Atomic single-file write: tmp sibling → write → rename over target → chmod.
|
|
216
287
|
* Mirrors the token/identity write in `credentials.ts` so a half-written
|
|
@@ -221,269 +292,381 @@ function atomicWrite(path, contents) {
|
|
|
221
292
|
renameSync(tmp, path);
|
|
222
293
|
chmodSync(path, FILE_MODE);
|
|
223
294
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
//
|
|
227
|
-
// The manifest (`profile.json`) is the source of truth for "is this a sil
|
|
228
|
-
// expert": a directory under `agents/<id>/` with a readable profile.json IS an
|
|
229
|
-
// expert; a bare host `agents.list[]` entry without one is not ours to manage.
|
|
230
|
-
// The host-config (wiring) half of list/remove is the host agent driving its
|
|
231
|
-
// own `openclaw …` CLI — these primitives only ever touch `$SIL_DATA_DIR`.
|
|
232
|
-
//
|
|
233
|
-
// Each primitive returns a discriminated result and NEVER throws across the
|
|
234
|
-
// boundary (mirroring MaterializeResult), so one corrupt manifest or a single
|
|
235
|
-
// failed delete degrades only its own outcome — never the whole operation.
|
|
236
|
-
// ===========================================================================
|
|
237
|
-
/** Resolve the agents subtree root under the plugin's data dir — the parent of
|
|
238
|
-
* every per-agent artefact directory. The deleter asserts a target is strictly
|
|
239
|
-
* a child of this (and never this itself), so a delete can never reach the
|
|
240
|
-
* parent and wipe sibling experts. */
|
|
241
|
-
function getAgentsRoot() {
|
|
242
|
-
return join(getDataDir(), AGENTS_SUBDIR);
|
|
295
|
+
function notFoundRemember(message) {
|
|
296
|
+
return { ok: false, kind: "not_found", message };
|
|
243
297
|
}
|
|
244
|
-
/**
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Append ONE typed learning to the shopper's behaviour artefacts — the cheap
|
|
300
|
+
* per-query persist path (the card's reason to exist). Unlike `materializeProfile`
|
|
301
|
+
* (whole-doc overwrite), this NEVER reads or re-serializes the body: a `kind:"fact"`
|
|
302
|
+
* lands at the EOF of the shared `user_spec.md`, a `kind:"taste"` at the EOF of the
|
|
303
|
+
* active domain's `playbook.md`.
|
|
304
|
+
*
|
|
305
|
+
* Validate-first (deterministic field order: `text` → the routing category errors →
|
|
306
|
+
* a taste's explicit `domain` shape): any bad field returns `invalid_request`
|
|
307
|
+
* naming it and writes NOTHING.
|
|
308
|
+
* - `kind:"fact"` carrying a `domain` → `invalid_request(domain)` (a fact is
|
|
309
|
+
* shopper-level).
|
|
310
|
+
* - `kind:"taste"` carrying `hard:true` → `invalid_request(hard)` (taste is soft).
|
|
311
|
+
*
|
|
312
|
+
* Active-domain resolution (taste only): explicit `domain` slug → else the single
|
|
313
|
+
* registered domain → 0 domains → `not_found` → 2+ domains → `invalid_request(domain)`
|
|
314
|
+
* (ambiguous — never a silent write to the wrong niche).
|
|
315
|
+
*
|
|
316
|
+
* Fail-closed existence gate — append NEVER creates a file: an unknown shopper (no
|
|
317
|
+
* manifest), a missing `user_spec.md` body, an unregistered domain, or a registered
|
|
318
|
+
* domain whose `playbook.md` body is gone → `not_found` (no lone-bullet seed-less
|
|
319
|
+
* doc is ever born via append).
|
|
320
|
+
*
|
|
321
|
+
* The write is `appendFileSync(target, "\n- …", { flag: "a", mode: FILE_MODE })` —
|
|
322
|
+
* O_APPEND lands the entry at EOF atomically per write (POSIX), so concurrent
|
|
323
|
+
* appenders never lose an entry. The entry is `"\n"`-prefixed (O_APPEND does not
|
|
324
|
+
* read the prior terminator) and is ONE short learning, well under PIPE_BUF (~4 KB),
|
|
325
|
+
* so the per-write atomicity holds. A hard fact carries the `[hard]` marker so the
|
|
326
|
+
* shop loop's reject-at-pick rule can grep it. No manifest rewrite — the entry is
|
|
327
|
+
* already visible to `readAgentProfile` via `userSpecPath` / `playbookPath`. The
|
|
328
|
+
* `mode` option only applies on file creation, and the gate guarantees the target
|
|
329
|
+
* already exists, so the file's `0600` mode (set at materialize) is preserved.
|
|
330
|
+
*/
|
|
331
|
+
export function appendProfileEntry(spec) {
|
|
332
|
+
// --- validate-first (nothing is written until every field is good) ---
|
|
333
|
+
if (!nonBlank(spec.text)) {
|
|
334
|
+
return invalid("text", "text is required and must be non-empty.");
|
|
262
335
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
336
|
+
// Routing category errors — fail fast, write nothing.
|
|
337
|
+
if (spec.kind === "fact" && spec.domain !== undefined) {
|
|
338
|
+
return invalid("domain", "a fact is shopper-level (it carries across every niche) — drop `domain`, or use kind:\"taste\".");
|
|
339
|
+
}
|
|
340
|
+
if (spec.kind === "taste" && spec.hard === true) {
|
|
341
|
+
return invalid("hard", "taste is always soft/bendable — a hard taste is a contradiction; mark `hard` on FACTS only.");
|
|
342
|
+
}
|
|
343
|
+
// A taste's explicit domain slug is the caller-supplied filesystem path segment —
|
|
344
|
+
// guard its SHAPE (field `domain`) BEFORE any resolve/read.
|
|
345
|
+
if (spec.kind === "taste" && spec.domain !== undefined) {
|
|
346
|
+
const badSlug = rejectBadSegment(spec.domain, "domain");
|
|
347
|
+
if (badSlug)
|
|
348
|
+
return badSlug;
|
|
349
|
+
}
|
|
350
|
+
const profilePath = join(getShopperArtefactDir(), PROFILE_FILE);
|
|
351
|
+
const manifest = tryReadManifest(profilePath);
|
|
352
|
+
if (manifest === null) {
|
|
353
|
+
return notFoundRemember("No sil shopper to remember for — create it first with sil_profile_materialize.");
|
|
354
|
+
}
|
|
355
|
+
// Resolve the target body file (and, for a taste, the active domain).
|
|
356
|
+
let target;
|
|
357
|
+
let resolvedDomain;
|
|
358
|
+
if (spec.kind === "fact") {
|
|
359
|
+
target = manifest.userSpecPath;
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
const slugs = Object.keys(manifest.domains);
|
|
363
|
+
let slug;
|
|
364
|
+
if (spec.domain !== undefined) {
|
|
365
|
+
slug = spec.domain;
|
|
366
|
+
}
|
|
367
|
+
else if (slugs.length === 1) {
|
|
368
|
+
slug = slugs[0];
|
|
369
|
+
}
|
|
370
|
+
else if (slugs.length === 0) {
|
|
371
|
+
return notFoundRemember("The shopper has no domains yet — mint the niche first via sil_profile_materialize.");
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
return invalid("domain", "The shopper has " + slugs.length + " domains (" + slugs.join(", ")
|
|
375
|
+
+ ") — name the `domain` to remember a taste in (omitting it is ambiguous).");
|
|
376
|
+
}
|
|
377
|
+
const entry = manifest.domains[slug];
|
|
378
|
+
if (entry === undefined) {
|
|
379
|
+
return notFoundRemember('No domain "' + slug + '" on the shopper — mint it first via sil_profile_materialize.');
|
|
380
|
+
}
|
|
381
|
+
target = entry.playbookPath;
|
|
382
|
+
resolvedDomain = slug;
|
|
383
|
+
}
|
|
384
|
+
// Fail-closed existence gate — append NEVER resurrects a seed-less doc.
|
|
385
|
+
if (!existsSync(target)) {
|
|
386
|
+
return notFoundRemember("The target artefact is missing — re-materialize the shopper/domain before remembering.");
|
|
387
|
+
}
|
|
388
|
+
// O_APPEND write — one short entry at EOF, "\n"-prefixed. A hard fact is marked
|
|
389
|
+
// unambiguously so the reject-at-pick rule can grep it.
|
|
390
|
+
const mark = spec.kind === "fact" && spec.hard === true ? HARD_MARKER + " " : "";
|
|
391
|
+
const line = "\n- " + mark + spec.text.trim();
|
|
392
|
+
try {
|
|
393
|
+
appendFileSync(target, line, { flag: "a", mode: FILE_MODE });
|
|
394
|
+
}
|
|
395
|
+
catch (err) {
|
|
396
|
+
return {
|
|
397
|
+
ok: false,
|
|
398
|
+
kind: "persistence_failed",
|
|
399
|
+
error: target + ": " + errCause(err),
|
|
400
|
+
message: "The remembered entry could NOT be appended to the sil data directory, so it"
|
|
401
|
+
+ " did not stick. Fix the data directory (it must be writable — check"
|
|
402
|
+
+ " permissions / free space / that the target is a file, not a directory),"
|
|
403
|
+
+ " then try again.",
|
|
404
|
+
recovery: "fix_data_dir",
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
ok: true,
|
|
409
|
+
kind: spec.kind,
|
|
410
|
+
target,
|
|
411
|
+
...(resolvedDomain !== undefined ? { domain: resolvedDomain } : {}),
|
|
412
|
+
};
|
|
267
413
|
}
|
|
268
|
-
/** Read + parse
|
|
269
|
-
* a descriptive error the caller maps to an `unreadable`/`not_found` outcome.
|
|
270
|
-
* A manifest whose JSON parses but is missing the required
|
|
271
|
-
*
|
|
272
|
-
* coerced. */
|
|
414
|
+
/** Read + parse the singleton `shopper/profile.json` into a typed manifest, or
|
|
415
|
+
* throw a descriptive error the caller maps to an `unreadable`/`not_found` outcome.
|
|
416
|
+
* A manifest whose JSON parses but is missing the required fields (name +
|
|
417
|
+
* userSpecPath + createdAt + a `domains` object) is treated as corrupt — not
|
|
418
|
+
* silently coerced. */
|
|
273
419
|
function readManifestFile(profilePath) {
|
|
274
420
|
const raw = readFileSync(profilePath, "utf8");
|
|
275
421
|
const parsed = JSON.parse(raw);
|
|
276
422
|
if (typeof parsed !== "object" ||
|
|
277
423
|
parsed === null ||
|
|
278
|
-
typeof parsed.agentId !== "string" ||
|
|
279
424
|
typeof parsed.name !== "string" ||
|
|
280
|
-
typeof parsed.domainSpecPath !== "string" ||
|
|
281
|
-
typeof parsed.intentSpecPath !== "string" ||
|
|
282
425
|
typeof parsed.userSpecPath !== "string" ||
|
|
283
|
-
typeof parsed.
|
|
284
|
-
typeof parsed.
|
|
426
|
+
typeof parsed.createdAt !== "string" ||
|
|
427
|
+
typeof parsed.domains !== "object" ||
|
|
428
|
+
parsed.domains === null ||
|
|
429
|
+
Array.isArray(parsed.domains)) {
|
|
285
430
|
throw new Error("profile.json is missing required manifest fields");
|
|
286
431
|
}
|
|
287
432
|
return parsed;
|
|
288
433
|
}
|
|
289
|
-
/**
|
|
290
|
-
*
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
* just-made expert first, the one the user most likely wants to act on). An
|
|
295
|
-
* absent/empty store yields a normal empty listing. One unreadable or corrupt
|
|
296
|
-
* manifest lands in `unreadable[]` and never aborts the listing — the healthy
|
|
297
|
-
* experts still list. Reads only; never writes, never reads a token.
|
|
298
|
-
*/
|
|
299
|
-
export function listAgentProfiles() {
|
|
300
|
-
const root = getAgentsRoot();
|
|
301
|
-
if (!existsSync(root)) {
|
|
302
|
-
return { ok: true, experts: [], unreadable: [] };
|
|
303
|
-
}
|
|
304
|
-
const experts = [];
|
|
305
|
-
const unreadable = [];
|
|
306
|
-
let entries;
|
|
434
|
+
/** Read the manifest if it exists and parses; `null` otherwise. Used by the
|
|
435
|
+
* upsert/de-register paths that must merge with whatever is already on disk. */
|
|
436
|
+
function tryReadManifest(profilePath) {
|
|
437
|
+
if (!existsSync(profilePath))
|
|
438
|
+
return null;
|
|
307
439
|
try {
|
|
308
|
-
|
|
440
|
+
return readManifestFile(profilePath);
|
|
309
441
|
}
|
|
310
|
-
catch
|
|
311
|
-
|
|
312
|
-
// as a single degraded entry rather than throwing across the boundary.
|
|
313
|
-
unreadable.push({
|
|
314
|
-
agentId: AGENTS_SUBDIR,
|
|
315
|
-
error: errCause(err),
|
|
316
|
-
});
|
|
317
|
-
return { ok: true, experts, unreadable };
|
|
318
|
-
}
|
|
319
|
-
for (const entry of entries) {
|
|
320
|
-
if (!entry.isDirectory())
|
|
321
|
-
continue;
|
|
322
|
-
const agentId = entry.name;
|
|
323
|
-
const profilePath = join(root, agentId, PROFILE_FILE);
|
|
324
|
-
if (!existsSync(profilePath)) {
|
|
325
|
-
// A directory under agents/ with no manifest is not a (loadable) sil
|
|
326
|
-
// expert — report it degraded so the user sees the residue (e.g. a
|
|
327
|
-
// remove that cleared the manifest but left a stray file).
|
|
328
|
-
unreadable.push({ agentId, error: "no profile.json manifest" });
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
try {
|
|
332
|
-
const manifest = readManifestFile(profilePath);
|
|
333
|
-
experts.push({
|
|
334
|
-
agentId: manifest.agentId,
|
|
335
|
-
name: manifest.name,
|
|
336
|
-
createdAt: manifest.createdAt,
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
catch (err) {
|
|
340
|
-
unreadable.push({ agentId, error: errCause(err) });
|
|
341
|
-
}
|
|
442
|
+
catch {
|
|
443
|
+
return null;
|
|
342
444
|
}
|
|
343
|
-
// Most-recently-created first (Product Flow 1). createdAt is ISO 8601, so a
|
|
344
|
-
// lexical compare is also chronological; fall back to it when Date.parse is
|
|
345
|
-
// NaN (a corrupt-but-parseable date) so the sort is total and stable-ish.
|
|
346
|
-
experts.sort((a, b) => createdAtDesc(a.createdAt, b.createdAt));
|
|
347
|
-
return { ok: true, experts, unreadable };
|
|
348
445
|
}
|
|
349
|
-
/**
|
|
350
|
-
|
|
446
|
+
/** Project a manifest's `domains` map to the cheap summary index, ordered by
|
|
447
|
+
* first-mint time (then slug) so the index is deterministic. */
|
|
448
|
+
function toDomainSummaries(manifest) {
|
|
449
|
+
return Object.values(manifest.domains)
|
|
450
|
+
.map((d) => ({ slug: d.slug, name: d.name, createdAt: d.createdAt, updatedAt: d.updatedAt }))
|
|
451
|
+
.sort((a, b) => createdAtAsc(a.createdAt, b.createdAt) || (a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0));
|
|
452
|
+
}
|
|
453
|
+
/** Compare two ISO 8601 createdAt strings, oldest first. */
|
|
454
|
+
function createdAtAsc(a, b) {
|
|
351
455
|
const ta = Date.parse(a);
|
|
352
456
|
const tb = Date.parse(b);
|
|
353
457
|
if (Number.isNaN(ta) || Number.isNaN(tb)) {
|
|
354
|
-
|
|
355
|
-
return a < b ? 1 : a > b ? -1 : 0;
|
|
458
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
356
459
|
}
|
|
357
|
-
return
|
|
460
|
+
return ta - tb;
|
|
358
461
|
}
|
|
359
462
|
/**
|
|
360
|
-
* Read
|
|
361
|
-
*
|
|
463
|
+
* Read the shopper overview (Zoom A, no args) OR one domain's full pack (slug).
|
|
464
|
+
*
|
|
465
|
+
* Zoom A (no slug) is the folded listing — it ABSORBS the old standalone listing
|
|
466
|
+
* surface: an empty store, or a freshly-created shopper with `domains: {}`, reads `ok`
|
|
467
|
+
* (empty-is-healthy) with an empty `domains` index and `unreadable: []` — NEVER
|
|
468
|
+
* `not_found` (the create-collision check + the "you haven't set up a shopper yet"
|
|
469
|
+
* framing + the shop-time existence check all depend on it). A corrupt manifest, or
|
|
470
|
+
* a degraded shopper directory, is surfaced inline in `unreadable[]` and never
|
|
471
|
+
* aborts the read nor downgrades to `not_found`.
|
|
362
472
|
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
* manifest,
|
|
366
|
-
*
|
|
367
|
-
* `not_found` (
|
|
368
|
-
*
|
|
369
|
-
* any more — a referenced-but-missing body of any of the four fails the read
|
|
370
|
-
* closed. Never throws across the boundary; reads only.
|
|
473
|
+
* Per-domain (slug given) guards the `domainSlug` BEFORE any `join`/read (a
|
|
474
|
+
* traversal-shaped slug → `invalid_request(field=domainSlug)`). Fail-closed: an
|
|
475
|
+
* absent/corrupt manifest, a missing SHARED user-spec body, a slug NOT in the
|
|
476
|
+
* `domains` map (manifest-gated — an orphaned leaf is invisible), or any of the 3
|
|
477
|
+
* referenced domain bodies missing → `not_found` (never a half pack). Never throws;
|
|
478
|
+
* reads only.
|
|
371
479
|
*/
|
|
372
|
-
export function readAgentProfile(
|
|
373
|
-
const
|
|
374
|
-
if (
|
|
375
|
-
|
|
376
|
-
|
|
480
|
+
export function readAgentProfile(domainSlug) {
|
|
481
|
+
const wantsDomain = nonBlank(domainSlug);
|
|
482
|
+
if (wantsDomain) {
|
|
483
|
+
const badSlug = rejectBadSegment(domainSlug, "domainSlug");
|
|
484
|
+
if (badSlug)
|
|
485
|
+
return badSlug;
|
|
486
|
+
}
|
|
487
|
+
const dir = getShopperArtefactDir();
|
|
377
488
|
const profilePath = join(dir, PROFILE_FILE);
|
|
489
|
+
if (!wantsDomain) {
|
|
490
|
+
// --- Zoom A: the no-args overview (folded listing, empty-is-healthy) ---
|
|
491
|
+
if (!existsSync(profilePath)) {
|
|
492
|
+
// Empty store — a normal, healthy empty outcome.
|
|
493
|
+
return { ok: true, userSpec: "", domains: [], unreadable: [], profilePath };
|
|
494
|
+
}
|
|
495
|
+
let manifest;
|
|
496
|
+
try {
|
|
497
|
+
manifest = readManifestFile(profilePath);
|
|
498
|
+
}
|
|
499
|
+
catch (err) {
|
|
500
|
+
// Corrupt manifest — degraded, surfaced inline, never not_found / never throws.
|
|
501
|
+
return {
|
|
502
|
+
ok: true,
|
|
503
|
+
userSpec: "",
|
|
504
|
+
domains: [],
|
|
505
|
+
unreadable: [{ id: SHOPPER_SUBDIR, error: errCause(err) }],
|
|
506
|
+
profilePath,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
let userSpec;
|
|
510
|
+
try {
|
|
511
|
+
userSpec = readFileSync(manifest.userSpecPath, "utf8");
|
|
512
|
+
}
|
|
513
|
+
catch (err) {
|
|
514
|
+
// The shared user-spec body is gone — the shopper cannot fully load; surface
|
|
515
|
+
// the breakage as degraded rather than hiding it or aborting the read.
|
|
516
|
+
return {
|
|
517
|
+
ok: true,
|
|
518
|
+
userSpec: "",
|
|
519
|
+
domains: [],
|
|
520
|
+
unreadable: [{ id: SHOPPER_SUBDIR, error: errCause(err) }],
|
|
521
|
+
profilePath,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
ok: true,
|
|
526
|
+
name: manifest.name,
|
|
527
|
+
userSpec,
|
|
528
|
+
domains: toDomainSummaries(manifest),
|
|
529
|
+
unreadable: [],
|
|
530
|
+
profilePath,
|
|
531
|
+
createdAt: manifest.createdAt,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
// --- Per-domain read (slug given) — fail-closed ---
|
|
378
535
|
if (!existsSync(profilePath)) {
|
|
379
|
-
return notFoundRead(
|
|
536
|
+
return notFoundRead();
|
|
380
537
|
}
|
|
381
538
|
let manifest;
|
|
382
539
|
try {
|
|
383
540
|
manifest = readManifestFile(profilePath);
|
|
384
541
|
}
|
|
385
542
|
catch {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
//
|
|
398
|
-
//
|
|
543
|
+
return notFoundRead();
|
|
544
|
+
}
|
|
545
|
+
// The SHARED user-spec body gates the read fail-closed — a manifest pointing at a
|
|
546
|
+
// user_spec.md whose body is gone is, to the viewer, a shopper that cannot load.
|
|
547
|
+
let userSpec;
|
|
548
|
+
try {
|
|
549
|
+
userSpec = readFileSync(manifest.userSpecPath, "utf8");
|
|
550
|
+
}
|
|
551
|
+
catch {
|
|
552
|
+
return notFoundRead();
|
|
553
|
+
}
|
|
554
|
+
// Manifest-gated — a slug not in the map (incl. an orphaned on-disk leaf) is
|
|
555
|
+
// not_found, never a filesystem guess.
|
|
556
|
+
const entry = manifest.domains[domainSlug];
|
|
557
|
+
if (entry === undefined) {
|
|
558
|
+
return notFoundRead();
|
|
559
|
+
}
|
|
399
560
|
let domainSpec;
|
|
400
561
|
let intentSpec;
|
|
401
|
-
let userSpec;
|
|
402
562
|
let playbook;
|
|
403
563
|
try {
|
|
404
|
-
domainSpec = readFileSync(
|
|
405
|
-
intentSpec = readFileSync(
|
|
406
|
-
|
|
407
|
-
playbook = readFileSync(manifest.playbookPath, "utf8");
|
|
564
|
+
domainSpec = readFileSync(entry.domainSpecPath, "utf8");
|
|
565
|
+
intentSpec = readFileSync(entry.intentSpecPath, "utf8");
|
|
566
|
+
playbook = readFileSync(entry.playbookPath, "utf8");
|
|
408
567
|
}
|
|
409
568
|
catch {
|
|
410
|
-
return notFoundRead(
|
|
569
|
+
return notFoundRead();
|
|
411
570
|
}
|
|
412
571
|
return {
|
|
413
572
|
ok: true,
|
|
414
|
-
agentId: manifest.agentId,
|
|
415
573
|
name: manifest.name,
|
|
574
|
+
userSpec,
|
|
575
|
+
domains: toDomainSummaries(manifest),
|
|
576
|
+
unreadable: [],
|
|
577
|
+
profilePath,
|
|
578
|
+
createdAt: entry.createdAt,
|
|
579
|
+
slug: entry.slug,
|
|
416
580
|
domainSpec,
|
|
417
581
|
intentSpec,
|
|
418
|
-
userSpec,
|
|
419
582
|
playbook,
|
|
420
|
-
|
|
421
|
-
createdAt: manifest.createdAt,
|
|
583
|
+
updatedAt: entry.updatedAt,
|
|
422
584
|
};
|
|
423
585
|
}
|
|
424
|
-
function notFoundRead(
|
|
586
|
+
function notFoundRead() {
|
|
425
587
|
return {
|
|
426
588
|
ok: false,
|
|
427
589
|
kind: "not_found",
|
|
428
|
-
|
|
429
|
-
|
|
590
|
+
message: "No such domain on the sil shopper — call sil_profile_get with no domainSlug to see"
|
|
591
|
+
+ " what the shopper has (its shared user spec + the domains it has learned).",
|
|
430
592
|
};
|
|
431
593
|
}
|
|
432
594
|
/**
|
|
433
|
-
* Remove
|
|
434
|
-
*
|
|
435
|
-
* plugin cannot write `~/.openclaw`).
|
|
595
|
+
* Remove exactly ONE of the shopper's domain packs — the sil-side half of a clean
|
|
596
|
+
* domain removal (the host-wiring half, if any, is the skill's `openclaw` CLI
|
|
597
|
+
* step; the plugin cannot write `~/.openclaw`).
|
|
436
598
|
*
|
|
437
|
-
* Fail-closed and scoped to
|
|
438
|
-
* - re-runs the
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
* wipe sibling
|
|
443
|
-
* -
|
|
599
|
+
* Fail-closed and scoped to one domain leaf:
|
|
600
|
+
* - re-runs the `domainSlug` gate (never trusts the caller) — a bad/`main`/
|
|
601
|
+
* traversal/missing slug → `invalid_request` (`field:"domainSlug"`), deletes
|
|
602
|
+
* NOTHING (no omit-deletes-everything trap);
|
|
603
|
+
* - asserts the resolved leaf is strictly UNDER `<shopperDir>/domains/` and is NOT
|
|
604
|
+
* that parent, so a delete can never escape the subtree or wipe a sibling;
|
|
605
|
+
* - manifest-gated: an unregistered slug → `not_found` (idempotent);
|
|
606
|
+
* - removes the single leaf, THEN rewrites `profile.json` to de-register
|
|
607
|
+
* `domains[slug]` — the shopper and the SHARED user_spec survive.
|
|
444
608
|
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
* `persistence_failed` with `<dir>: <cause>`. Never throws across the boundary.
|
|
609
|
+
* A genuine delete/rewrite failure → `persistence_failed` with `<dir>: <cause>`.
|
|
610
|
+
* Never throws across the boundary.
|
|
448
611
|
*/
|
|
449
|
-
export function removeAgentArtefacts(
|
|
450
|
-
const
|
|
451
|
-
if (
|
|
452
|
-
return
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
//
|
|
457
|
-
//
|
|
458
|
-
|
|
459
|
-
if (dirname(
|
|
460
|
-
return
|
|
461
|
-
JSON.stringify(
|
|
462
|
-
}
|
|
463
|
-
|
|
612
|
+
export function removeAgentArtefacts(domainSlug) {
|
|
613
|
+
const badSlug = rejectBadSegment(domainSlug, "domainSlug");
|
|
614
|
+
if (badSlug)
|
|
615
|
+
return badSlug;
|
|
616
|
+
const shopperDir = getShopperArtefactDir();
|
|
617
|
+
const domainsRoot = join(shopperDir, DOMAINS_SUBDIR);
|
|
618
|
+
const leaf = getDomainArtefactDir(domainSlug);
|
|
619
|
+
// Defence in depth beyond the slug gate: the leaf must be a STRICT child of the
|
|
620
|
+
// domains root and never the root itself — deleting the parent would wipe every
|
|
621
|
+
// sibling domain.
|
|
622
|
+
if (dirname(leaf) !== domainsRoot || leaf === domainsRoot) {
|
|
623
|
+
return invalid("domainSlug", "domainSlug resolves outside the domains subtree — refusing to delete: " +
|
|
624
|
+
JSON.stringify(domainSlug));
|
|
625
|
+
}
|
|
626
|
+
const profilePath = join(shopperDir, PROFILE_FILE);
|
|
627
|
+
const manifest = tryReadManifest(profilePath);
|
|
628
|
+
// Manifest-gated: an unregistered (or absent) domain is already gone.
|
|
629
|
+
if (manifest === null || !(domainSlug in manifest.domains)) {
|
|
464
630
|
return {
|
|
465
631
|
ok: false,
|
|
466
632
|
kind: "not_found",
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
+ "
|
|
633
|
+
message: 'No domain "' + domainSlug + '" on the sil shopper to remove (already gone) —'
|
|
634
|
+
+ " call sil_profile_get with no domainSlug to see which domains exist. A"
|
|
635
|
+
+ " re-run is safe.",
|
|
470
636
|
};
|
|
471
637
|
}
|
|
638
|
+
// Remove the leaf FIRST; only de-register once the bytes are actually gone, so a
|
|
639
|
+
// failed delete never leaves the map pointing at a still-present pack.
|
|
472
640
|
try {
|
|
473
|
-
rmSync(
|
|
641
|
+
rmSync(leaf, { recursive: true, force: true });
|
|
474
642
|
}
|
|
475
643
|
catch (err) {
|
|
476
|
-
return
|
|
477
|
-
ok: false,
|
|
478
|
-
kind: "persistence_failed",
|
|
479
|
-
error: dir + ": " + errCause(err),
|
|
480
|
-
message: "The expert's behaviour artefacts could NOT be removed from the sil data"
|
|
481
|
-
+ " directory. Fix the data directory (it must be writable — check"
|
|
482
|
-
+ " permissions), then remove the expert again.",
|
|
483
|
-
recovery: "fix_data_dir",
|
|
484
|
-
};
|
|
644
|
+
return persistenceFailedRemove(leaf, err);
|
|
485
645
|
}
|
|
486
|
-
|
|
646
|
+
const nextDomains = {};
|
|
647
|
+
for (const [slug, entry] of Object.entries(manifest.domains)) {
|
|
648
|
+
if (slug !== domainSlug)
|
|
649
|
+
nextDomains[slug] = entry;
|
|
650
|
+
}
|
|
651
|
+
const updated = { ...manifest, domains: nextDomains };
|
|
652
|
+
try {
|
|
653
|
+
atomicWrite(profilePath, JSON.stringify(updated, null, 2) + "\n");
|
|
654
|
+
}
|
|
655
|
+
catch (err) {
|
|
656
|
+
return persistenceFailedRemove(profilePath, err);
|
|
657
|
+
}
|
|
658
|
+
return { ok: true, domainSlug };
|
|
659
|
+
}
|
|
660
|
+
function persistenceFailedRemove(path, err) {
|
|
661
|
+
return {
|
|
662
|
+
ok: false,
|
|
663
|
+
kind: "persistence_failed",
|
|
664
|
+
error: path + ": " + errCause(err),
|
|
665
|
+
message: "The domain's behaviour artefacts could NOT be removed from the sil data"
|
|
666
|
+
+ " directory. Fix the data directory (it must be writable — check"
|
|
667
|
+
+ " permissions), then remove the domain again.",
|
|
668
|
+
recovery: "fix_data_dir",
|
|
669
|
+
};
|
|
487
670
|
}
|
|
488
671
|
/** Extract a human-readable cause from an unknown thrown value (never PII). */
|
|
489
672
|
function errCause(err) {
|