@inetafrica/open-claudia 2.6.37 → 2.6.39

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.
@@ -9,14 +9,21 @@
9
9
  const { spawnSubagent } = require("./subagent");
10
10
  const packs = require("./packs");
11
11
  const entities = require("./entities");
12
+ const lessons = require("./lessons");
12
13
  const packGuard = require("./pack-guard");
13
14
  const { redactSensitive } = require("./redact");
14
15
 
15
16
  const MIN_TURN_CHARS = 400;
16
- const MAX_TEXT_CHARS = 7000;
17
- const REVIEW_MODEL = process.env.PACK_REVIEW_MODEL || "haiku";
17
+ const MAX_TEXT_CHARS = 16000;
18
+ // Create/merge decisions need real judgement (the cheap model over-created
19
+ // per-version/per-facet packs), so default to the latest Sonnet. Override with
20
+ // PACK_REVIEW_MODEL if cost/latency of running it on every substantial turn matters.
21
+ const REVIEW_MODEL = process.env.PACK_REVIEW_MODEL || "claude-sonnet-4-6";
18
22
  const MAX_ACTIONS = 2;
19
23
  const MAX_ENTITY_ACTIONS = 3;
24
+ // Lessons are precious always-on budget — at most one promotion per turn,
25
+ // and only when the turn shows a genuine miss.
26
+ const MAX_LESSON_ACTIONS = 1;
20
27
 
21
28
  function enabled() {
22
29
  return (process.env.PACK_REVIEW || "on").toLowerCase() !== "off";
@@ -52,14 +59,22 @@ function formatAnnouncement(lines) {
52
59
  }
53
60
 
54
61
  function buildReviewPrompt(userText, assistantText) {
55
- const index = packs.listPacks().map((p) =>
56
- `- ${p.dir}: ${p.name} ${p.description}${p.tags.length ? ` [${p.tags.join(", ")}]` : ""}`
57
- ).join("\n") || "(none yet)";
62
+ const index = packs.listPacks().map((p) => {
63
+ const ab = p.kind === "ability" ? " ◆ability" : "";
64
+ const prov = p.kind === "ability" && p.learned_on
65
+ ? ` (learned on ${p.learned_on}${p.applied_on && p.applied_on.length ? `; applied on ${p.applied_on.join(", ")}` : ""})`
66
+ : "";
67
+ return `- ${p.dir}: ${p.name} — ${p.description}${p.tags.length ? ` [${p.tags.join(", ")}]` : ""}${ab}${prov}`;
68
+ }).join("\n") || "(none yet)";
58
69
 
59
70
  const entityIndex = entities.listEntities().map((e) =>
60
71
  `- ${e.slug}: ${e.name} (${e.type})${e.aliases.length ? ` aka ${e.aliases.join(", ")}` : ""} — ${e.description}`
61
72
  ).join("\n") || "(none yet)";
62
73
 
74
+ const lessonIndex = lessons.listLessons().map((l) =>
75
+ `- ${l.text}${l.src ? ` (src: ${l.src})` : ""}`
76
+ ).join("\n") || "(none yet)";
77
+
63
78
  return `You are the memory reviewer for a personal AI assistant. After each conversation turn you decide whether the assistant's long-term "context packs" and "entity notes" should change.
64
79
 
65
80
  A context pack is a living document about ONE topic (a project, a system, a recurring task, a domain). It has four sections:
@@ -72,12 +87,19 @@ An entity note is a short memory file about ONE specific named entity — a pers
72
87
  - Notes: current truth about the entity (who/what it is, role, preferences, relationships). Replaces wholesale.
73
88
  - Log: one-line dated observations, appended.
74
89
 
90
+ An ABILITY is a special kind of pack (kind:"ability") for a REUSABLE HOW-TO that is NOT tied to one project — a procedure, pattern, or technique you would follow just as well on a different project later (e.g. "ship a mobile app: bump versionCode, build the APK, push the in-app updater"; "safely run a destructive DB write"; "wire up a new ArgoCD app"). A normal pack (kind:"context", the default) is about ONE project/system and stays scoped to it. Decide by NATURE, not by repetition: if THIS turn demonstrated a self-contained method you would re-run on a DIFFERENT project, capture it as an ability the FIRST time you see it — do not wait for it to recur. Give an ability an ACTIVITY-oriented name, description, and tags (what you DO — the verbs, tools, and artifacts involved) so it can be found later from a different project by the work being done, not by a project name. Set "learned_on" to the project pack dir this turn worked on and list that same dir in "applied_on". If an ability below (marked ◆ability) already covers the method, do NOT duplicate it — UPDATE it and add the current project dir to "applied_on" so it visibly transfers.
91
+
92
+ A LESSON is a single always-loaded rule (NOT topic-gated like packs/entities — it loads on every turn). Lessons exist for ONE purpose: to stop a recurring mistake. Propose a lesson ONLY when THIS turn shows the assistant MISSED something it should already have known — i.e. the user corrected the assistant ("no", "that's wrong", "actually it's X"), or signalled repetition ("again", "I keep telling you", "as I said", "I've told you before", "like I mentioned"). That friction is the proof that topic-matched memory failed, so the corrected fact must move to the always-on tier. No correction/repetition signal in the turn → NO lesson (return an empty lessons array). A lesson is the CORRECT fact written as one crisp imperative line, cross-cutting and durable (a mechanism, rule, or constraint that will matter on future, possibly off-topic turns) — never a one-off task detail or a fact with no miss behind it. Point "src" at the pack that should hold the full context if one fits. If an existing lesson below already covers it, reuse its EXACT wording so it reinforces rather than duplicating.
93
+
75
94
  Existing packs:
76
95
  ${index}
77
96
 
78
97
  Known entities:
79
98
  ${entityIndex}
80
99
 
100
+ Current lessons (always loaded):
101
+ ${lessonIndex}
102
+
81
103
  The turn to review:
82
104
 
83
105
  <user_message>
@@ -92,22 +114,27 @@ Decide. Rules:
92
114
  - Bias toward action: if the turn did real work on an identifiable topic (a named system, project, server, app, person, or domain), record it — at minimum a journal line. Most working turns deserve one. Reserve empty actions for small talk, pure status checks, and turns that contain nothing new.
93
115
  - UPDATE an existing pack when the turn touched that topic: append a journal line, and rewrite State if where-things-stand changed. Update Stance when the user expressed a lasting preference or rule; Procedure when a verified working method emerged.
94
116
  - PROMOTE durable conclusions out of the Journal. A confirmed root-cause diagnosis, an established fact, or a settled design decision must go into Stance/Procedure/State (the parts always injected in full), NOT only a Journal line — Journal is truncated to the last few lines on injection, so a conclusion left only there will silently fall out of recall over time.
95
- - CREATE a pack when the turn worked on a durable topic no existing pack covers. Topics recur more than you expect — a named system or project is durable by default. Do not create packs for one-off trivia, and prefer update when an existing pack fits.
117
+ - CREATE a pack ONLY when the turn worked on a durable topic that NO existing pack covers. Before creating, scan the existing packs above for one already about this project/system/domain if one exists, UPDATE it instead. A new pack must be a genuinely DISTINCT topic, never a narrower facet, version, release, sub-feature, phase, or component of a topic an existing pack already owns. In particular: a release or version of a project (e.g. a pack named after "<project> v2.6.39") is a Journal line + State update on that project's EXISTING pack — NEVER its own pack. Likewise sub-features of one system belong in that system's pack, not in siblings. When unsure whether something is a new topic or a facet of an existing one, treat it as a facet and UPDATE. Do not create packs for one-off trivia.
118
+ - ABILITY vs CONTEXT: most creates are context packs (a project/system tracker). Create an ability (kind:"ability") only for a genuinely reusable how-to as defined above, and always set learned_on + applied_on to the originating project dir. When a turn re-used an EXISTING ability on a NEW project, do NOT create anything — UPDATE that ability and put the new project dir in "applied_on" so the cross-project reuse is recorded.
96
119
  - Never store secrets, tokens, passwords, or credentials.
97
120
  - State should be concise (under 150 words). Journal entries one sentence. Never start a journal or log sentence with a date — dates are prepended automatically.
98
121
  - At most ${MAX_ACTIONS} pack actions.
99
122
  - Entities: add an item when the turn revealed something durable about a specific named person, place, project, org, or system — their role, status, preferences, or relationship to other work. Use the existing entity name when one matches (check aliases). Skip entities mentioned only in passing with nothing learned. Notes under 100 words; "notes" null means leave Notes unchanged. At most ${MAX_ENTITY_ACTIONS} entity items.
100
123
  - Packs and entities are independent — a turn can update both, either, or neither. Do not duplicate the same fact in a pack AND an entity unless it genuinely belongs to both.
124
+ - Lessons are the rare case: at most ${MAX_LESSON_ACTIONS} per turn, and ONLY on a real correction/repetition signal as defined above. The default is an empty lessons array. When you do promote one, ALSO record the same fact in the appropriate pack (update/create) so the durable home stays authoritative — the lesson is just the always-on shortcut. Never put secrets in a lesson.
101
125
 
102
126
  Reply with ONLY a JSON object, no prose, no code fences:
103
127
  {"actions": [
104
- {"action": "update", "pack": "<existing dir>", "journal": "<one sentence>", "state": "<full new State or null>", "stance": null, "procedure": null}
105
- | {"action": "create", "dir": "<kebab-slug>", "name": "<title>", "description": "<one line: when this pack is relevant>", "tags": ["..."], "stance": "<or empty>", "procedure": "<or empty>", "state": "<where things stand>", "journal": "<one sentence>"}
128
+ {"action": "update", "pack": "<existing dir>", "journal": "<one sentence>", "state": "<full new State or null>", "stance": null, "procedure": null, "applied_on": ["<project dir, ONLY when this turn re-applied an existing ability to a new project>"]}
129
+ | {"action": "create", "dir": "<kebab-slug>", "name": "<title>", "description": "<one line: when this pack is relevant>", "tags": ["..."], "kind": "context|ability", "learned_on": "<originating project dir — abilities only>", "applied_on": ["<project dirs — abilities only>"], "stance": "<or empty>", "procedure": "<or empty>", "state": "<where things stand>", "journal": "<one sentence>"}
106
130
  ],
107
131
  "entities": [
108
132
  {"name": "<canonical name>", "type": "person|place|project|org|system|thing", "aliases": ["..."], "description": "<one line: who/what this is>", "notes": "<full new Notes or null>", "log": "<one sentence observation>"}
133
+ ],
134
+ "lessons": [
135
+ {"text": "<the correct fact as one crisp imperative line>", "src": "<pack dir that holds the full context, or empty>", "trigger": "<the exact correction/repetition phrase from the turn>"}
109
136
  ]}
110
- or {"actions": [], "entities": []}`;
137
+ or {"actions": [], "entities": [], "lessons": []}`;
111
138
  }
112
139
 
113
140
  function parseDecision(text) {
@@ -119,7 +146,8 @@ function parseDecision(text) {
119
146
  const obj = JSON.parse(raw.slice(start, end + 1));
120
147
  const actions = Array.isArray(obj.actions) ? obj.actions.slice(0, MAX_ACTIONS) : (obj.action ? [obj] : []);
121
148
  const entityActions = Array.isArray(obj.entities) ? obj.entities.slice(0, MAX_ENTITY_ACTIONS) : [];
122
- return { actions, entities: entityActions };
149
+ const lessonActions = Array.isArray(obj.lessons) ? obj.lessons.slice(0, MAX_LESSON_ACTIONS) : [];
150
+ return { actions, entities: entityActions, lessons: lessonActions };
123
151
  } catch (e) {
124
152
  return null;
125
153
  }
@@ -139,6 +167,35 @@ function guardCheck(a) {
139
167
  return hit;
140
168
  }
141
169
 
170
+ // Deterministic dedupe backstop for the create path. Even with the prompt rule,
171
+ // a cheap model tends to spawn a fresh pack for each version/release/facet of a
172
+ // project an existing pack already owns. This catches two robust signals — a
173
+ // slug that is a hyphen-boundary prefix/extension of an existing pack dir, and a
174
+ // version-named proposal whose name/description strongly matches an existing
175
+ // pack — and the caller folds the proposal into that pack instead. Conservative
176
+ // by design (only context packs; only a clear prefix relationship or a versioned
177
+ // proposal with a strong text match) so genuinely distinct topics still get their
178
+ // own pack. Mirrors the structural "no lesson without a trigger" enforcement.
179
+ function findDuplicatePack({ dir, name, description, tags }) {
180
+ const slug = packs.slugify(dir || name || "");
181
+ if (!slug) return null;
182
+ const contextDirs = packs.listPacks().filter((p) => p.kind !== "ability").map((p) => p.dir);
183
+ for (const d of contextDirs) {
184
+ if (d === slug) continue;
185
+ if (slug.startsWith(d + "-") || d.startsWith(slug + "-")) return d;
186
+ }
187
+ const tagStr = Array.isArray(tags) ? tags.join(" ") : "";
188
+ const versioned = /\bv?\d+\.\d+(?:\.\d+)?\b|\brelease\b|\bv\d+\b/i.test(`${slug} ${name || ""} ${description || ""} ${tagStr}`);
189
+ if (versioned) {
190
+ const ctx = new Set(contextDirs);
191
+ const text = `${name || ""} ${description || ""} ${tagStr}`.trim();
192
+ for (const m of packs.matchPacks(text, { limit: 3, threshold: 4 })) {
193
+ if (m.dir !== slug && ctx.has(m.dir)) return m.dir;
194
+ }
195
+ }
196
+ return null;
197
+ }
198
+
142
199
  function applyAction(a) {
143
200
  if (!a || typeof a !== "object") return null;
144
201
  const guard = guardCheck(a);
@@ -154,14 +211,38 @@ function applyAction(a) {
154
211
  stance: typeof a.stance === "string" ? a.stance : "",
155
212
  procedure: typeof a.procedure === "string" ? a.procedure : "",
156
213
  }, "reviewer");
157
- return { kind: "update", dir: a.pack, name: existing.name, note: a.journal || "state updated", protectedStance: !!r.protectedStance };
214
+ // Cross-project reuse: only abilities carry applied_on, so ignore the field
215
+ // on context packs (keeps project trackers churn-free).
216
+ let appliedTo = null;
217
+ if (existing.kind === "ability" && Array.isArray(a.applied_on)) {
218
+ for (const proj of a.applied_on.map((x) => String(x || "").trim()).filter(Boolean)) {
219
+ if (packs.recordApplied(a.pack, proj)) appliedTo = proj;
220
+ }
221
+ }
222
+ return { kind: "update", dir: a.pack, name: existing.name, note: a.journal || (appliedTo ? `reused on ${appliedTo}` : "state updated"), protectedStance: !!r.protectedStance, appliedTo };
158
223
  }
159
224
  if (a.action === "create" && (a.dir || a.name)) {
160
225
  const dir = packs.slugify(a.dir || a.name);
226
+ const kind = a.kind === "ability" ? "ability" : "context";
227
+ const learned_on = String(a.learned_on || "").trim();
228
+ const applied_on = Array.isArray(a.applied_on)
229
+ ? a.applied_on.map((x) => String(x || "").trim()).filter(Boolean)
230
+ : [];
231
+ const projects = applied_on.length ? applied_on : (learned_on ? [learned_on] : []);
161
232
  if (packs.readPack(dir)) {
162
233
  packs.updatePack(dir, { journal: a.journal || "", state: a.state || "" }, "reviewer");
234
+ if (kind === "ability") for (const proj of projects) packs.recordApplied(dir, proj);
163
235
  return { kind: "update", dir, name: a.name || dir, note: a.journal || "state updated" };
164
236
  }
237
+ // Deterministic dedupe: a versioned/facet proposal folds INTO the project
238
+ // pack it belongs to rather than spawning a sibling (abilities are exempt —
239
+ // they are activity-named and cross-project by design).
240
+ const dupeDir = kind === "context" ? findDuplicatePack({ dir, name: a.name, description: a.description, tags: a.tags }) : null;
241
+ if (dupeDir && packs.readPack(dupeDir)) {
242
+ packs.updatePack(dupeDir, { journal: a.journal || "", state: a.state || "" }, "reviewer");
243
+ const ex = packs.readPack(dupeDir);
244
+ return { kind: "update", dir: dupeDir, name: (ex && ex.name) || dupeDir, note: a.journal || "folded a new-pack proposal into the existing pack" };
245
+ }
165
246
  const pack = packs.createPack({
166
247
  dir,
167
248
  name: a.name,
@@ -171,8 +252,11 @@ function applyAction(a) {
171
252
  procedure: a.procedure || "",
172
253
  state: a.state || "",
173
254
  journal: a.journal || "",
255
+ kind,
256
+ learned_on: kind === "ability" ? learned_on : "",
257
+ applied_on: kind === "ability" ? projects : [],
174
258
  }, "reviewer");
175
- return { kind: "create", dir: pack.dir, name: pack.name, note: a.description || "" };
259
+ return { kind: "create", dir: pack.dir, name: pack.name, note: a.description || "", ability: kind === "ability", learned_on: pack.learned_on };
176
260
  }
177
261
  return null;
178
262
  }
@@ -190,6 +274,25 @@ function applyEntityAction(e) {
190
274
  return { kind: created ? "create" : "update", slug: entity.slug, name: entity.name, type: entity.type, note: e.log || e.description || "" };
191
275
  }
192
276
 
277
+ function applyLessonAction(l) {
278
+ if (!l || typeof l !== "object") return null;
279
+ const text = String(l.text || "").trim();
280
+ if (!text) return null;
281
+ // Structural enforcement of "promote only after a miss": the model must
282
+ // cite the in-turn correction/repetition phrase. No trigger → no lesson,
283
+ // regardless of what the model decided.
284
+ if (!String(l.trigger || "").trim()) return null;
285
+ const guard = packGuard.scanForInjection(text, { strict: true });
286
+ if (guard.flagged) {
287
+ console.warn(`[pack-review] guard blocked lesson (${guard.kind}): ${guard.evidence || text.slice(0, 60)}`);
288
+ return { kind: "skipped", reason: guard.kind };
289
+ }
290
+ const r = lessons.addLesson({ text, src: String(l.src || "").trim(), origin: "reviewer" });
291
+ if (r.added) return { kind: "create", id: r.id, text, overCap: !!r.overCap };
292
+ if (r.reinforced) return { kind: "reinforce", id: r.id, text };
293
+ return { kind: "skipped", reason: r.reason || "not added" };
294
+ }
295
+
193
296
  // Fire-and-forget. `announce` is an async (text) => void bound to the
194
297
  // originating channel; failures are logged, never thrown into the turn.
195
298
  function reviewTurn({ userText, assistantText, channelId, announce }) {
@@ -206,17 +309,19 @@ function reviewTurn({ userText, assistantText, channelId, announce }) {
206
309
  systemPrompt: "You are a background memory reviewer. Reply with ONLY the requested JSON object. No prose, no markdown, no tool use.",
207
310
  }).then(({ text }) => {
208
311
  const decision = parseDecision(text);
209
- if (!decision || (decision.actions.length === 0 && decision.entities.length === 0)) return;
312
+ if (!decision || (decision.actions.length === 0 && decision.entities.length === 0 && (decision.lessons || []).length === 0)) return;
210
313
  const lines = [];
211
314
  for (const a of decision.actions) {
212
315
  try {
213
316
  const r = applyAction(a);
214
317
  if (r && r.kind === "skipped") {
215
318
  lines.push(`🛡️ Skipped a memory write that looked like an injected instruction (${r.reason}).`);
319
+ } else if (r && r.kind === "create" && r.ability) {
320
+ lines.push(`🧩 New ability: ${r.name}${r.learned_on ? ` (learned on ${r.learned_on})` : ""}\n${clipWords(r.note, 180)}\n↳ open-claudia pack show ${r.dir}`);
321
+ } else if (r && r.kind === "create") {
322
+ lines.push(`📦 New pack: ${r.name}\n${clipWords(r.note, 180)}\n↳ open-claudia pack show ${r.dir}`);
216
323
  } else if (r) {
217
- lines.push(r.kind === "create"
218
- ? `📦 New pack: ${r.name}\n${clipWords(r.note, 180)}\n↳ open-claudia pack show ${r.dir}`
219
- : `✏️ ${r.name} — ${clipWords(r.note, 180)}\n↳ open-claudia pack show ${r.dir}`);
324
+ lines.push(`✏️ ${r.name} ${clipWords(r.note, 180)}${r.appliedTo ? ` 🧩 (now also applies to ${r.appliedTo})` : ""}\n↳ open-claudia pack show ${r.dir}`);
220
325
  }
221
326
  } catch (e) {
222
327
  console.warn(`[pack-review] apply failed: ${e.message}`);
@@ -235,6 +340,23 @@ function reviewTurn({ userText, assistantText, channelId, announce }) {
235
340
  console.warn(`[pack-review] entity apply failed: ${e.message}`);
236
341
  }
237
342
  }
343
+ for (const la of (decision.lessons || [])) {
344
+ try {
345
+ const r = applyLessonAction(la);
346
+ if (!r) continue;
347
+ if (r.kind === "skipped") {
348
+ if (r.reason && /exfil|override|base64/.test(r.reason)) {
349
+ lines.push(`🛡️ Skipped a lesson that looked like an injected instruction (${r.reason}).`);
350
+ }
351
+ } else if (r.kind === "create") {
352
+ lines.push(`📌 New lesson — I got this wrong before, so I'll always keep it loaded now:\n"${clipWords(r.text, 200)}"${r.overCap ? "\n(That's over the lessons cap — the nightly dream will tidy.)" : ""}`);
353
+ } else if (r.kind === "reinforce") {
354
+ lines.push(`📌 Reinforced a lesson I keep needing — clearly worth keeping front of mind:\n"${clipWords(r.text, 200)}"`);
355
+ }
356
+ } catch (e) {
357
+ console.warn(`[pack-review] lesson apply failed: ${e.message}`);
358
+ }
359
+ }
238
360
  if (lines.length > 0 && typeof announce === "function") {
239
361
  announce(formatAnnouncement(lines)).catch(() => {});
240
362
  }
@@ -243,4 +365,4 @@ function reviewTurn({ userText, assistantText, channelId, announce }) {
243
365
  });
244
366
  }
245
367
 
246
- module.exports = { reviewTurn, parseDecision, applyAction, applyEntityAction, buildReviewPrompt, clipWords, ENTITY_EMOJI };
368
+ module.exports = { reviewTurn, parseDecision, applyAction, applyEntityAction, applyLessonAction, buildReviewPrompt, clipWords, ENTITY_EMOJI };
package/core/packs.js CHANGED
@@ -66,6 +66,10 @@ function serialize(pack) {
66
66
  `tags: ${(pack.tags || []).join(", ")}`,
67
67
  ];
68
68
  if (pack.parent) fmLines.push(`parent: ${pack.parent}`);
69
+ if (pack.skill) fmLines.push("skill: true");
70
+ if (pack.kind && pack.kind !== "context") fmLines.push(`kind: ${pack.kind}`);
71
+ if (pack.learned_on) fmLines.push(`learned_on: ${pack.learned_on}`);
72
+ if (Array.isArray(pack.applied_on) && pack.applied_on.length) fmLines.push(`applied_on: ${pack.applied_on.join(", ")}`);
69
73
  fmLines.push(
70
74
  `created: ${pack.created || new Date().toISOString()}`,
71
75
  `updated: ${pack.updated || new Date().toISOString()}`,
@@ -98,6 +102,10 @@ function readPack(dir) {
98
102
  description: fm.description || "",
99
103
  tags: (fm.tags || "").split(",").map((t) => t.trim()).filter(Boolean),
100
104
  parent: fm.parent || null,
105
+ skill: fm.skill === "true" || fm.skill === true,
106
+ kind: fm.kind || "context",
107
+ learned_on: fm.learned_on || "",
108
+ applied_on: (fm.applied_on || "").split(",").map((t) => t.trim()).filter(Boolean),
101
109
  created: fm.created || "",
102
110
  updated: fm.updated || (stat ? stat.mtime.toISOString() : ""),
103
111
  last_used: fm.last_used || "",
@@ -128,6 +136,83 @@ function findPack(nameOrDir) {
128
136
  return listPacks().find((p) => p.dir.toLowerCase() === needle || p.name.toLowerCase() === needle) || null;
129
137
  }
130
138
 
139
+ // Packs explicitly flagged as reusable how-tos. These get an always-on
140
+ // Tier-1 index in the system prompt (name + description), so the agent knows
141
+ // the skill exists and can load its Procedure on demand. Most packs are
142
+ // project trackers, not skills — so this is opt-in, not "any pack with a
143
+ // Procedure".
144
+ function listSkillPacks() {
145
+ return listPacks().filter((p) => p.skill && !p.archived);
146
+ }
147
+
148
+ function setSkill(nameOrDir, on) {
149
+ const pack = findPack(nameOrDir);
150
+ if (!pack) return null;
151
+ pack.skill = !!on;
152
+ if (on && pack.kind !== "ability") pack.kind = "ability";
153
+ pack.updated = new Date().toISOString();
154
+ writePack(pack);
155
+ return pack;
156
+ }
157
+
158
+ // Reusable abilities (how-tos / patterns / themes) — kind:"ability". These are
159
+ // the nodes that transfer across projects via governed-by edges, and the pool
160
+ // the always-on skill index is promoted from. Project trackers are kind:"context".
161
+ function listAbilities() {
162
+ return listPacks().filter((p) => p.kind === "ability" && !p.archived);
163
+ }
164
+
165
+ function setKind(nameOrDir, kind) {
166
+ const pack = findPack(nameOrDir);
167
+ if (!pack) return null;
168
+ pack.kind = String(kind || "context").trim() || "context";
169
+ pack.updated = new Date().toISOString();
170
+ writePack(pack);
171
+ return pack;
172
+ }
173
+
174
+ // Record that an ability was applied on a project (provenance of reuse): appends
175
+ // to applied_on (deduped) and sets learned_on to the first project if unset.
176
+ function recordApplied(nameOrDir, project) {
177
+ const pack = findPack(nameOrDir);
178
+ if (!pack || !project) return null;
179
+ const proj = String(project).trim();
180
+ if (!proj) return pack;
181
+ if (!pack.learned_on) pack.learned_on = proj;
182
+ const set = new Set(pack.applied_on || []);
183
+ set.add(proj);
184
+ pack.applied_on = [...set];
185
+ pack.updated = new Date().toISOString();
186
+ writePack(pack);
187
+ return pack;
188
+ }
189
+
190
+ // Close the learning loop from actual USE. Given the node ids the agent opened
191
+ // together in one turn (pack:<dir> / entity:<slug>), treat every ability opened
192
+ // alongside a project (context) pack as "this ability was applied while working
193
+ // on that project" and record it. applied_on growing is what forms the
194
+ // project→ability governed-by edge on the next structural sync, so reuse
195
+ // transfers automatically from real use. Returns the pairs NEWLY transferred
196
+ // (for a one-line "it transfers there now too" announcement).
197
+ function recordCoUse(openedIds) {
198
+ const opened = [...new Set(openedIds || [])]
199
+ .filter((id) => typeof id === "string" && id.startsWith("pack:"))
200
+ .map((id) => readPack(id.slice(5)))
201
+ .filter(Boolean);
202
+ const abilities = opened.filter((p) => p.kind === "ability");
203
+ const contexts = opened.filter((p) => p.kind !== "ability");
204
+ const transferred = [];
205
+ for (const ab of abilities) {
206
+ for (const ctx of contexts) {
207
+ if (ab.dir === ctx.dir) continue;
208
+ const isNew = !(ab.applied_on || []).includes(ctx.dir);
209
+ recordApplied(ab.dir, ctx.dir);
210
+ if (isNew) transferred.push({ ability: ab.dir, abilityName: ab.name, project: ctx.dir, projectName: ctx.name });
211
+ }
212
+ }
213
+ return transferred;
214
+ }
215
+
131
216
  function writePack(pack) {
132
217
  ensureDir();
133
218
  const dir = path.join(PACKS_DIR, pack.dir);
@@ -137,7 +222,7 @@ function writePack(pack) {
137
222
  return pack.dir;
138
223
  }
139
224
 
140
- function createPack({ dir, name, description, tags, stance, procedure, state, journal, parent }, origin = "user") {
225
+ function createPack({ dir, name, description, tags, stance, procedure, state, journal, parent, skill, kind, learned_on, applied_on }, origin = "user") {
141
226
  const d = slugify(dir || name);
142
227
  if (!d) throw new Error("pack needs a name");
143
228
  if (readPack(d)) throw new Error(`pack ${d} already exists`);
@@ -147,6 +232,10 @@ function createPack({ dir, name, description, tags, stance, procedure, state, jo
147
232
  description: description || "",
148
233
  tags: Array.isArray(tags) ? tags : [],
149
234
  parent: parent || null,
235
+ skill: !!skill,
236
+ kind: kind || "context",
237
+ learned_on: learned_on || "",
238
+ applied_on: Array.isArray(applied_on) ? applied_on : [],
150
239
  created: new Date().toISOString(),
151
240
  sections: {
152
241
  Stance: stance || "",
@@ -227,12 +316,15 @@ function recordForegroundWrite(dir, { tool, oldString, content } = {}) {
227
316
 
228
317
  // Apply a reviewer mutation. Only supplied fields change; journal entries
229
318
  // append (capped); state replaces (it represents "current truth").
230
- function updatePack(dir, { description, tags, stance, procedure, state, journal } = {}, origin = "user") {
319
+ function updatePack(dir, { description, tags, stance, procedure, state, journal, skill, kind, learned_on } = {}, origin = "user") {
231
320
  const pack = readPack(dir);
232
321
  if (!pack) throw new Error(`no pack: ${dir}`);
233
322
  pack.updated = new Date().toISOString();
234
323
  if (description) pack.description = description;
235
324
  if (Array.isArray(tags) && tags.length) pack.tags = tags;
325
+ if (typeof skill === "boolean") pack.skill = skill;
326
+ if (typeof kind === "string" && kind.trim()) pack.kind = kind.trim();
327
+ if (typeof learned_on === "string" && learned_on.trim()) pack.learned_on = learned_on.trim();
236
328
  const applied = [];
237
329
  let protectedStance = false;
238
330
  if (typeof stance === "string" && stance.trim()) {
@@ -470,6 +562,7 @@ function matchPacks(text, { limit = 3, threshold = null } = {}) {
470
562
  module.exports = {
471
563
  PACKS_DIR, SECTIONS, slugify,
472
564
  listPacks, findPack, readPack, writePack, createPack, updatePack, removePack,
565
+ listSkillPacks, setSkill, listAbilities, setKind, recordApplied, recordCoUse,
473
566
  touchUsed, packNameFromPath, matchPacks, reindex, markIndexDirty,
474
567
  archivePack, restorePack, listArchived,
475
568
  readProvenance, provenanceOf, setProvenance, recordForegroundWrite,
@@ -264,6 +264,7 @@ function parseLinks(text) {
264
264
  }
265
265
 
266
266
  function isSharedConcern(pack) {
267
+ if (pack && pack.kind === "ability") return true;
267
268
  const tags = (pack.tags || []).map((t) => t.toLowerCase());
268
269
  return tags.includes("shared") || tags.includes("concern") || tags.includes("cross-cutting");
269
270
  }
@@ -303,6 +304,22 @@ function syncFromCorpus(packsLib, entitiesLib) {
303
304
  const type = targetPack && isSharedConcern(targetPack) ? "governed-by" : "related";
304
305
  if (addEdge(id, target, type)) count++;
305
306
  }
307
+ // Abilities transfer across projects: derive governed-by edges from the
308
+ // ability's OWN provenance (learned_on + applied_on) so the link forms after
309
+ // the FIRST occurrence, without having to mutate the project packs. Direction
310
+ // matches the [[link]] convention — project (child) → ability (concern).
311
+ if (p.kind === "ability") {
312
+ const projects = new Set(
313
+ [p.learned_on, ...(p.applied_on || [])]
314
+ .map((x) => String(x || "").trim().toLowerCase())
315
+ .filter(Boolean)
316
+ );
317
+ for (const proj of projects) {
318
+ const src = resolve(proj);
319
+ if (!src || src === id) continue;
320
+ if (addEdge(src, id, "governed-by")) count++;
321
+ }
322
+ }
306
323
  }
307
324
  for (const e of entities) {
308
325
  const id = `entity:${e.slug}`;
@@ -4,8 +4,10 @@
4
4
  // Each engine implements: async run(ctx) -> { packBlock, entityBlock,
5
5
  // packMatches, entityMatches }. The active engine is chosen per channel via
6
6
  // the `recallEngine` setting (set by the /engine slash command), falling back
7
- // to the RECALL_ENGINE env var, then to "classic". Unknown names fall back to
8
- // classic so a bad value can never break recall.
7
+ // to the RECALL_ENGINE env var, then to DEFAULT_ENGINE. Unknown names fall
8
+ // back to the default so a bad value can never break recall. The discoverer is
9
+ // the default: it's fail-open (falls back to the classic keyword baseline on
10
+ // any error), so it never recalls worse than classic, only better.
9
11
 
10
12
  const classic = require("./classic");
11
13
  const discoverer = require("./discoverer");
@@ -15,6 +17,11 @@ const ENGINES = {
15
17
  discoverer,
16
18
  };
17
19
 
20
+ // The default recall engine for channels that haven't explicitly chosen one
21
+ // (recallEngine === null). Single source of truth — flip this to change the
22
+ // product default. `classic` remains selectable as an explicit opt-out.
23
+ const DEFAULT_ENGINE = "discoverer";
24
+
18
25
  function listEngines() {
19
26
  return Object.keys(ENGINES);
20
27
  }
@@ -22,12 +29,12 @@ function listEngines() {
22
29
  function activeEngineName(settings) {
23
30
  const fromSettings = settings && settings.recallEngine;
24
31
  const fromEnv = process.env.RECALL_ENGINE;
25
- const name = String(fromSettings || fromEnv || "classic").toLowerCase();
26
- return ENGINES[name] ? name : "classic";
32
+ const name = String(fromSettings || fromEnv || DEFAULT_ENGINE).toLowerCase();
33
+ return ENGINES[name] ? name : DEFAULT_ENGINE;
27
34
  }
28
35
 
29
36
  function getEngine(name) {
30
37
  return ENGINES[name] || classic;
31
38
  }
32
39
 
33
- module.exports = { ENGINES, listEngines, activeEngineName, getEngine };
40
+ module.exports = { ENGINES, listEngines, activeEngineName, getEngine, DEFAULT_ENGINE };
package/core/redact.js CHANGED
@@ -1,15 +1,37 @@
1
1
  // Secret-redaction + terminal-control stripping. Used everywhere we ship
2
2
  // CLI output (stderr, stdout, transcripts) back to a chat surface.
3
3
 
4
+ // Known literal secrets (operational keyring values) registered at startup
5
+ // and whenever one is set. Matched by literal substring, not regex, so a
6
+ // value containing regex metacharacters can't break or widen the match.
7
+ const dynamicSecrets = new Set();
8
+
9
+ // Register one or more secret literals to scrub from all future output.
10
+ // Short values are ignored to avoid redacting common substrings.
11
+ function registerSecrets(values) {
12
+ for (const v of [].concat(values || [])) {
13
+ const s = String(v == null ? "" : v);
14
+ if (s.length >= 6) dynamicSecrets.add(s);
15
+ }
16
+ }
17
+
18
+ function redactDynamic(text) {
19
+ let out = String(text);
20
+ for (const secret of dynamicSecrets) {
21
+ if (out.includes(secret)) out = out.split(secret).join("[REDACTED_SECRET]");
22
+ }
23
+ return out;
24
+ }
25
+
4
26
  function redactSensitive(value) {
5
- return String(value || "")
27
+ return redactDynamic(String(value || "")
6
28
  .replace(/sk-ant-[A-Za-z0-9._-]+/g, "[REDACTED_TOKEN]")
7
29
  .replace(/sk-proj-[A-Za-z0-9._-]+/g, "[REDACTED_OPENAI_KEY]")
8
30
  .replace(/sk-[A-Za-z0-9._-]{20,}/g, "[REDACTED_OPENAI_KEY]")
9
31
  .replace(/(Bearer\s+)[A-Za-z0-9._=-]+/gi, "$1[REDACTED_TOKEN]")
10
32
  .replace(/(CLAUDE_CODE_OAUTH_TOKEN\s*=\s*)\S+/gi, "$1[REDACTED_TOKEN]")
11
33
  .replace(/(OPENAI_API_KEY\s*=\s*)\S+/gi, "$1[REDACTED_OPENAI_KEY]")
12
- .replace(/([?&](?:token|access_token|refresh_token|api_key)=)[^\s&]+/gi, "$1[REDACTED]");
34
+ .replace(/([?&](?:token|access_token|refresh_token|api_key)=)[^\s&]+/gi, "$1[REDACTED]"));
13
35
  }
14
36
 
15
37
  function stripTerminalControls(value) {
@@ -23,4 +45,4 @@ function extractUrls(text) {
23
45
  return [...stripTerminalControls(text).matchAll(/https?:\/\/[^\s)]+/g)].map((m) => m[0]);
24
46
  }
25
47
 
26
- module.exports = { redactSensitive, stripTerminalControls, extractUrls };
48
+ module.exports = { redactSensitive, registerSecrets, stripTerminalControls, extractUrls };