@caupulican/pi-adaptative 0.93.4 → 0.93.7

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.
Files changed (78) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/bundled-resources/skills/n-plus-2-architecture/SKILL.md +13 -0
  3. package/dist/core/agent-session.d.ts.map +1 -1
  4. package/dist/core/agent-session.js +5 -20
  5. package/dist/core/agent-session.js.map +1 -1
  6. package/dist/core/autonomy/envelope-enforcement.d.ts +3 -1
  7. package/dist/core/autonomy/envelope-enforcement.d.ts.map +1 -1
  8. package/dist/core/autonomy/envelope-enforcement.js +23 -12
  9. package/dist/core/autonomy/envelope-enforcement.js.map +1 -1
  10. package/dist/core/autonomy/gates.d.ts.map +1 -1
  11. package/dist/core/autonomy/gates.js +24 -26
  12. package/dist/core/autonomy/gates.js.map +1 -1
  13. package/dist/core/context-gc.d.ts.map +1 -1
  14. package/dist/core/context-gc.js +128 -3
  15. package/dist/core/context-gc.js.map +1 -1
  16. package/dist/core/session-shutdown.d.ts +9 -0
  17. package/dist/core/session-shutdown.d.ts.map +1 -0
  18. package/dist/core/session-shutdown.js +44 -0
  19. package/dist/core/session-shutdown.js.map +1 -0
  20. package/dist/core/settings-manager.js.map +1 -1
  21. package/dist/core/skill-vault.d.ts +24 -16
  22. package/dist/core/skill-vault.d.ts.map +1 -1
  23. package/dist/core/skill-vault.js +159 -82
  24. package/dist/core/skill-vault.js.map +1 -1
  25. package/dist/core/system-prompt.d.ts.map +1 -1
  26. package/dist/core/system-prompt.js +7 -18
  27. package/dist/core/system-prompt.js.map +1 -1
  28. package/dist/core/tools/bash.d.ts +3 -1
  29. package/dist/core/tools/bash.d.ts.map +1 -1
  30. package/dist/core/tools/bash.js +15 -7
  31. package/dist/core/tools/bash.js.map +1 -1
  32. package/dist/core/tools/edit.d.ts.map +1 -1
  33. package/dist/core/tools/edit.js +86 -33
  34. package/dist/core/tools/edit.js.map +1 -1
  35. package/dist/core/tools/file-mutation-intent.d.ts +16 -0
  36. package/dist/core/tools/file-mutation-intent.d.ts.map +1 -1
  37. package/dist/core/tools/file-mutation-intent.js +51 -2
  38. package/dist/core/tools/file-mutation-intent.js.map +1 -1
  39. package/dist/core/tools/persistent-process-coordinator.d.ts +8 -2
  40. package/dist/core/tools/persistent-process-coordinator.d.ts.map +1 -1
  41. package/dist/core/tools/persistent-process-coordinator.js +52 -4
  42. package/dist/core/tools/persistent-process-coordinator.js.map +1 -1
  43. package/dist/core/tools/shell-execution-session.d.ts +8 -1
  44. package/dist/core/tools/shell-execution-session.d.ts.map +1 -1
  45. package/dist/core/tools/shell-execution-session.js +54 -3
  46. package/dist/core/tools/shell-execution-session.js.map +1 -1
  47. package/dist/core/tools/shell-session.d.ts +5 -2
  48. package/dist/core/tools/shell-session.d.ts.map +1 -1
  49. package/dist/core/tools/shell-session.js +112 -17
  50. package/dist/core/tools/shell-session.js.map +1 -1
  51. package/dist/core/tools/skill.d.ts +2 -1
  52. package/dist/core/tools/skill.d.ts.map +1 -1
  53. package/dist/core/tools/skill.js +26 -12
  54. package/dist/core/tools/skill.js.map +1 -1
  55. package/dist/core/tools/windows-shell-engine.d.ts +1 -1
  56. package/dist/core/tools/windows-shell-engine.d.ts.map +1 -1
  57. package/dist/core/tools/windows-shell-engine.js +6 -2
  58. package/dist/core/tools/windows-shell-engine.js.map +1 -1
  59. package/dist/index.d.ts +1 -1
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +1 -1
  62. package/dist/index.js.map +1 -1
  63. package/dist/modes/interactive/components/settings-selector.js +2 -2
  64. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  65. package/dist/utils/shell.d.ts +3 -5
  66. package/dist/utils/shell.d.ts.map +1 -1
  67. package/dist/utils/shell.js +4 -7
  68. package/dist/utils/shell.js.map +1 -1
  69. package/docs/settings.md +7 -7
  70. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  71. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  72. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  73. package/examples/extensions/sandbox/package-lock.json +2 -2
  74. package/examples/extensions/sandbox/package.json +1 -1
  75. package/examples/extensions/with-deps/package-lock.json +2 -2
  76. package/examples/extensions/with-deps/package.json +1 -1
  77. package/npm-shrinkwrap.json +12 -12
  78. package/package.json +4 -4
@@ -2,6 +2,8 @@ import { type Skill } from "./skills.ts";
2
2
  export declare const DEFAULT_SKILL_IDLE_TIMEOUT_MS: number;
3
3
  export declare const MAX_ACTIVE_SKILL_BODY_BYTES: number;
4
4
  export declare const MIN_ACTIVE_SKILL_BODY_BYTES: number;
5
+ export declare const MAX_LOADED_SKILLS = 3;
6
+ export declare const MAX_PINNED_SKILLS = 2;
5
7
  type SkillVaultRequester = "model" | "user";
6
8
  type SkillVaultUnloadReason = "explicit" | "idle_expired" | "resource_unavailable" | "budget_exceeded";
7
9
  interface LoadedSkill {
@@ -9,6 +11,7 @@ interface LoadedSkill {
9
11
  bodyBytes: number;
10
12
  systemPromptSection: string;
11
13
  requester: SkillVaultRequester;
14
+ pinned: boolean;
12
15
  loadedAtMs: number;
13
16
  fileDevice: number;
14
17
  fileInode: number;
@@ -16,25 +19,26 @@ interface LoadedSkill {
16
19
  fileModifiedAtMs: number;
17
20
  fileChangedAtMs: number;
18
21
  }
19
- type SkillVaultState = {
20
- state: "unloaded";
21
- reason?: SkillVaultUnloadReason;
22
- } | ({
22
+ type SkillSlotState = ({
23
23
  state: "loaded_pending";
24
24
  } & LoadedSkill) | ({
25
25
  state: "active";
26
26
  lastUsedAtMs: number;
27
27
  useCount: number;
28
28
  } & LoadedSkill);
29
- export interface SkillVaultStatus {
30
- state: SkillVaultState["state"];
31
- idleTimeoutMs: number;
32
- name?: string;
33
- loadedAtMs?: number;
29
+ export interface SkillSlotStatus {
30
+ state: SkillSlotState["state"];
31
+ name: string;
32
+ pinned: boolean;
33
+ loadedAtMs: number;
34
34
  lastUsedAtMs?: number;
35
35
  idleForMs?: number;
36
36
  expiresInMs?: number;
37
37
  useCount?: number;
38
+ }
39
+ export interface SkillVaultStatus {
40
+ idleTimeoutMs: number;
41
+ slots: SkillSlotStatus[];
38
42
  reason?: SkillVaultUnloadReason;
39
43
  }
40
44
  export interface SkillSearchResult {
@@ -47,10 +51,12 @@ export type SkillLoadResult = {
47
51
  ok: true;
48
52
  state: "loaded_pending";
49
53
  name: string;
50
- replaced?: string;
54
+ baseDir: string;
55
+ pinned: boolean;
56
+ evicted?: string[];
51
57
  } | {
52
58
  ok: false;
53
- reason: "not_found" | "body_too_large" | "invalid_body" | "read_failed";
59
+ reason: "not_found" | "body_too_large" | "invalid_body" | "read_failed" | "pin_limit";
54
60
  message: string;
55
61
  };
56
62
  export interface SkillVaultControllerOptions {
@@ -69,14 +75,15 @@ export declare class SkillVaultController {
69
75
  private readonly idleTimeoutMs;
70
76
  private readonly getMaxBodyBytes;
71
77
  private readonly onSkillUsed;
72
- private current;
78
+ private slots;
79
+ private unloadReason;
73
80
  private contextRevision;
74
81
  constructor(options: SkillVaultControllerOptions);
75
82
  search(rawQuery: string): SkillSearchResult;
76
- load(name: string, requester: SkillVaultRequester): SkillLoadResult;
77
- unload(): {
83
+ load(name: string, requester: SkillVaultRequester, pin?: boolean): SkillLoadResult;
84
+ unload(name?: string): {
78
85
  ok: true;
79
- unloaded?: string;
86
+ unloaded: string[];
80
87
  };
81
88
  status(): SkillVaultStatus;
82
89
  commitSystemPromptSection(): string | undefined;
@@ -88,8 +95,9 @@ export declare class SkillVaultController {
88
95
  getContextRevision(): number;
89
96
  /** Record host-observed work derived from an active skill, independent of agent cooperation. */
90
97
  noteActivity(): void;
98
+ private slotStatus;
91
99
  private reconcile;
92
- private reconcileResource;
100
+ private reconcileSlot;
93
101
  private replaceState;
94
102
  private resolveMaxBodyBytes;
95
103
  }
@@ -1 +1 @@
1
- {"version":3,"file":"skill-vault.d.ts","sourceRoot":"","sources":["../../src/core/skill-vault.ts"],"names":[],"mappings":"AAKA,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AAC,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,IAAI,CAAC;QAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;KAAE,CAUtD;IAED,MAAM,IAAI,gBAAgB,CASzB;IAED,yBAAyB,IAAI,MAAM,GAAG,SAAS,CAmB9C;IAED,gGAAgG;IAChG,0BAA0B,IAAI,MAAM,GAAG,SAAS,CAI/C;IAED,0EAA0E;IAC1E,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAEvE;IAED,wEAAwE;IACxE,kBAAkB,IAAI,MAAM,CAG3B;IAED,gGAAgG;IAChG,YAAY,IAAI,IAAI,CAOnB;IAED,OAAO,CAAC,UAAU;IAiBlB,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;IAgCrB,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,mBAAmB;CAK3B"}
@@ -8,6 +8,8 @@ import { readBoundedTextFileSync, sameFileVersion } from "./util/bounded-file.js
8
8
  export const DEFAULT_SKILL_IDLE_TIMEOUT_MS = 10 * 60 * 1000;
9
9
  export const MAX_ACTIVE_SKILL_BODY_BYTES = 64 * 1024;
10
10
  export const MIN_ACTIVE_SKILL_BODY_BYTES = 4 * 1024;
11
+ export const MAX_LOADED_SKILLS = 3;
12
+ export const MAX_PINNED_SKILLS = 2;
11
13
  const MAX_SEARCH_RESULTS = 5;
12
14
  const MAX_SEARCH_DESCRIPTION_CHARS = 240;
13
15
  function compactDescription(description) {
@@ -36,6 +38,29 @@ function searchScore(skill, query, tokens) {
36
38
  function activeSkillContext(skill, body) {
37
39
  return [`ACTIVE SKILL ${skill.name}`, `BASE ${skill.baseDir}`, "NON-NEGOTIABLE WHILE ACTIVE:", body].join("\n");
38
40
  }
41
+ function slotLastUsedAtMs(slot) {
42
+ return slot.state === "loaded_pending" ? slot.loadedAtMs : slot.lastUsedAtMs;
43
+ }
44
+ function aggregateBodyBytes(slots) {
45
+ let total = 0;
46
+ for (const slot of slots.values())
47
+ total += slot.bodyBytes;
48
+ return total;
49
+ }
50
+ /** Per-skill use is unobservable host-side (every loaded body rides every request), so eviction is honest FIFO by loadedAtMs: oldest unpinned first, oldest pinned only once no unpinned slot remains. */
51
+ function evictionVictimName(slots, excludeName) {
52
+ let victim;
53
+ for (const [name, slot] of slots) {
54
+ if (name === excludeName)
55
+ continue;
56
+ if (victim === undefined ||
57
+ (victim.pinned && !slot.pinned) ||
58
+ (victim.pinned === slot.pinned && slot.loadedAtMs < victim.loadedAtMs)) {
59
+ victim = { name, pinned: slot.pinned, loadedAtMs: slot.loadedAtMs };
60
+ }
61
+ }
62
+ return victim?.name;
63
+ }
39
64
  /** Reserve at most roughly one context token's worth of bytes per advertised context token. */
40
65
  export function resolveActiveSkillBodyByteLimit(contextWindow) {
41
66
  if (contextWindow === undefined || !Number.isFinite(contextWindow) || contextWindow <= 0) {
@@ -46,7 +71,7 @@ export function resolveActiveSkillBodyByteLimit(contextWindow) {
46
71
  /** One host-owned, event-driven lifecycle for lazy skill discovery and transient context projection. */
47
72
  export class SkillVaultController {
48
73
  constructor(options) {
49
- this.current = { state: "unloaded" };
74
+ this.slots = new Map();
50
75
  this.contextRevision = 0;
51
76
  if (!Number.isFinite(options.idleTimeoutMs ?? DEFAULT_SKILL_IDLE_TIMEOUT_MS)) {
52
77
  throw new TypeError("Skill idle timeout must be finite.");
@@ -71,11 +96,27 @@ export class SkillVaultController {
71
96
  .map(({ skill }) => ({ name: skill.name, description: compactDescription(skill.description) }));
72
97
  return { candidates };
73
98
  }
74
- load(name, requester) {
99
+ load(name, requester, pin = false) {
100
+ const now = this.now();
101
+ this.reconcile(now);
75
102
  const skill = this.getSkills().find((candidate) => candidate.name === name && (requester === "user" || !candidate.disableModelInvocation));
76
103
  if (!skill) {
77
104
  return { ok: false, reason: "not_found", message: `No eligible skill named ${JSON.stringify(name)}.` };
78
105
  }
106
+ if (pin) {
107
+ let pinnedCount = 0;
108
+ for (const [slotName, slot] of this.slots) {
109
+ if (slotName !== skill.name && slot.pinned)
110
+ pinnedCount++;
111
+ }
112
+ if (pinnedCount >= MAX_PINNED_SKILLS) {
113
+ return {
114
+ ok: false,
115
+ reason: "pin_limit",
116
+ message: `At most ${MAX_PINNED_SKILLS} skills can be pinned; unload a pinned skill or reload it without pin first.`,
117
+ };
118
+ }
119
+ }
79
120
  const maxBodyBytes = this.resolveMaxBodyBytes();
80
121
  let before;
81
122
  try {
@@ -122,80 +163,89 @@ export class SkillVaultController {
122
163
  if (!sameFileVersion(before, file)) {
123
164
  return { ok: false, reason: "read_failed", message: "Skill changed while it was being loaded." };
124
165
  }
125
- const replaced = this.current.state === "unloaded" ? undefined : this.current.skill.name;
126
- this.replaceState({
166
+ const next = new Map(this.slots);
167
+ next.set(skill.name, {
127
168
  state: "loaded_pending",
128
169
  skill,
129
170
  bodyBytes,
130
171
  systemPromptSection: activeSkillContext(skill, body),
131
172
  requester,
132
- loadedAtMs: this.now(),
173
+ pinned: pin,
174
+ loadedAtMs: now,
133
175
  fileDevice: file.dev,
134
176
  fileInode: file.ino,
135
177
  fileSize: file.size,
136
178
  fileModifiedAtMs: file.mtimeMs,
137
179
  fileChangedAtMs: file.ctimeMs,
138
180
  });
139
- return { ok: true, state: "loaded_pending", name: skill.name, ...(replaced ? { replaced } : {}) };
181
+ const evicted = [];
182
+ while (next.size > MAX_LOADED_SKILLS || aggregateBodyBytes(next) > maxBodyBytes) {
183
+ const victim = evictionVictimName(next, skill.name);
184
+ if (!victim)
185
+ break;
186
+ next.delete(victim);
187
+ evicted.push(victim);
188
+ }
189
+ this.replaceState(next);
190
+ return {
191
+ ok: true,
192
+ state: "loaded_pending",
193
+ name: skill.name,
194
+ baseDir: skill.baseDir,
195
+ pinned: pin,
196
+ ...(evicted.length > 0 ? { evicted } : {}),
197
+ };
140
198
  }
141
- unload() {
142
- const unloaded = this.current.state === "unloaded" ? undefined : this.current.skill.name;
143
- this.replaceState({ state: "unloaded", reason: "explicit" });
144
- return { ok: true, ...(unloaded ? { unloaded } : {}) };
199
+ unload(name) {
200
+ this.reconcile(this.now());
201
+ const target = name?.trim();
202
+ const unloaded = !target ? [...this.slots.keys()] : this.slots.has(target) ? [target] : [];
203
+ if (unloaded.length > 0) {
204
+ const next = new Map(this.slots);
205
+ for (const slotName of unloaded)
206
+ next.delete(slotName);
207
+ this.replaceState(next, "explicit");
208
+ }
209
+ return { ok: true, unloaded };
145
210
  }
146
211
  status() {
147
212
  const now = this.now();
148
213
  this.reconcile(now);
149
- if (this.current.state === "unloaded") {
150
- return {
151
- state: "unloaded",
152
- idleTimeoutMs: this.idleTimeoutMs,
153
- ...(this.current.reason ? { reason: this.current.reason } : {}),
154
- };
155
- }
156
- if (this.current.state === "loaded_pending") {
157
- return {
158
- state: "loaded_pending",
159
- idleTimeoutMs: this.idleTimeoutMs,
160
- name: this.current.skill.name,
161
- loadedAtMs: this.current.loadedAtMs,
162
- };
163
- }
164
- const idleForMs = Math.max(0, now - this.current.lastUsedAtMs);
214
+ const slots = [...this.slots.values()].map((slot) => this.slotStatus(slot, now));
165
215
  return {
166
- state: "active",
167
216
  idleTimeoutMs: this.idleTimeoutMs,
168
- name: this.current.skill.name,
169
- loadedAtMs: this.current.loadedAtMs,
170
- lastUsedAtMs: this.current.lastUsedAtMs,
171
- idleForMs,
172
- expiresInMs: Math.max(0, this.idleTimeoutMs - idleForMs),
173
- useCount: this.current.useCount,
217
+ slots,
218
+ ...(slots.length === 0 && this.unloadReason ? { reason: this.unloadReason } : {}),
174
219
  };
175
220
  }
176
221
  commitSystemPromptSection() {
177
222
  const now = this.now();
178
223
  this.reconcile(now);
179
- if (this.current.state === "unloaded")
224
+ if (this.slots.size === 0)
180
225
  return undefined;
181
- const loaded = this.current;
182
- const firstUse = loaded.state === "loaded_pending";
183
- const useCount = firstUse ? 1 : loaded.useCount + 1;
184
- this.current = { ...loaded, state: "active", lastUsedAtMs: now, useCount };
185
- if (firstUse) {
186
- try {
187
- this.onSkillUsed?.(loaded.skill, now);
188
- }
189
- catch {
190
- // Usage telemetry must never block skill application.
226
+ const sections = [];
227
+ for (const [name, slot] of this.slots) {
228
+ sections.push(slot.systemPromptSection);
229
+ const firstUse = slot.state === "loaded_pending";
230
+ const useCount = firstUse ? 1 : slot.useCount + 1;
231
+ this.slots.set(name, { ...slot, state: "active", lastUsedAtMs: now, useCount });
232
+ if (firstUse) {
233
+ try {
234
+ this.onSkillUsed?.(slot.skill, now);
235
+ }
236
+ catch {
237
+ // Usage telemetry must never block skill application.
238
+ }
191
239
  }
192
240
  }
193
- return loaded.systemPromptSection;
241
+ return sections.join("\n\n");
194
242
  }
195
243
  /** Model the next request's transient system cost without treating a diagnostic read as use. */
196
244
  previewSystemPromptSection() {
197
245
  this.reconcile(this.now());
198
- return this.current.state === "unloaded" ? undefined : this.current.systemPromptSection;
246
+ if (this.slots.size === 0)
247
+ return undefined;
248
+ return [...this.slots.values()].map((slot) => slot.systemPromptSection).join("\n\n");
199
249
  }
200
250
  /** Compose the exact provider system prompt for read-only diagnostics. */
201
251
  previewRequestSystemPrompt(base) {
@@ -209,55 +259,82 @@ export class SkillVaultController {
209
259
  /** Record host-observed work derived from an active skill, independent of agent cooperation. */
210
260
  noteActivity() {
211
261
  const now = this.now();
212
- if (this.current.state === "active") {
213
- this.current = { ...this.current, lastUsedAtMs: now };
262
+ for (const [name, slot] of this.slots) {
263
+ if (slot.state === "active") {
264
+ this.slots.set(name, { ...slot, lastUsedAtMs: now });
265
+ }
214
266
  }
215
267
  }
268
+ slotStatus(slot, now) {
269
+ if (slot.state === "loaded_pending") {
270
+ return { state: "loaded_pending", name: slot.skill.name, pinned: slot.pinned, loadedAtMs: slot.loadedAtMs };
271
+ }
272
+ const idleForMs = Math.max(0, now - slot.lastUsedAtMs);
273
+ return {
274
+ state: "active",
275
+ name: slot.skill.name,
276
+ pinned: slot.pinned,
277
+ loadedAtMs: slot.loadedAtMs,
278
+ lastUsedAtMs: slot.lastUsedAtMs,
279
+ idleForMs,
280
+ expiresInMs: Math.max(0, this.idleTimeoutMs - idleForMs),
281
+ useCount: slot.useCount,
282
+ };
283
+ }
216
284
  reconcile(now) {
217
- if (!this.reconcileResource())
285
+ if (this.slots.size === 0)
218
286
  return;
219
- const current = this.current;
220
- if (current.state === "unloaded")
221
- return;
222
- const lastUsedAtMs = current.state === "loaded_pending" ? current.loadedAtMs : current.lastUsedAtMs;
223
- if (now - lastUsedAtMs >= this.idleTimeoutMs) {
224
- this.replaceState({ state: "unloaded", reason: "idle_expired" });
287
+ const maxBodyBytes = this.resolveMaxBodyBytes();
288
+ const skills = this.getSkills();
289
+ const next = new Map(this.slots);
290
+ let reason;
291
+ for (const [name, slot] of this.slots) {
292
+ const slotReason = this.reconcileSlot(slot, skills, maxBodyBytes, now);
293
+ if (slotReason !== undefined) {
294
+ next.delete(name);
295
+ reason = slotReason;
296
+ }
225
297
  }
226
- }
227
- reconcileResource() {
228
- if (this.current.state === "unloaded")
229
- return false;
230
- const loaded = this.current;
231
- if (loaded.bodyBytes > this.resolveMaxBodyBytes()) {
232
- this.replaceState({ state: "unloaded", reason: "budget_exceeded" });
233
- return false;
298
+ while (aggregateBodyBytes(next) > maxBodyBytes) {
299
+ const victim = evictionVictimName(next);
300
+ if (!victim)
301
+ break;
302
+ next.delete(victim);
303
+ reason = "budget_exceeded";
234
304
  }
235
- const currentSkill = this.getSkills().find((skill) => skill.name === loaded.skill.name &&
236
- skill.filePath === loaded.skill.filePath &&
237
- (loaded.requester === "user" || !skill.disableModelInvocation));
238
- if (!currentSkill) {
239
- this.replaceState({ state: "unloaded", reason: "resource_unavailable" });
240
- return false;
305
+ if (next.size !== this.slots.size) {
306
+ this.replaceState(next, reason);
241
307
  }
308
+ }
309
+ reconcileSlot(slot, skills, maxBodyBytes, now) {
310
+ if (slot.bodyBytes > maxBodyBytes)
311
+ return "budget_exceeded";
312
+ const currentSkill = skills.find((skill) => skill.name === slot.skill.name &&
313
+ skill.filePath === slot.skill.filePath &&
314
+ (slot.requester === "user" || !skill.disableModelInvocation));
315
+ if (!currentSkill)
316
+ return "resource_unavailable";
242
317
  try {
243
- const file = statSync(loaded.skill.filePath);
244
- if (file.dev !== loaded.fileDevice ||
245
- file.ino !== loaded.fileInode ||
246
- file.size !== loaded.fileSize ||
247
- file.mtimeMs !== loaded.fileModifiedAtMs ||
248
- file.ctimeMs !== loaded.fileChangedAtMs) {
249
- this.replaceState({ state: "unloaded", reason: "resource_unavailable" });
250
- return false;
318
+ const file = statSync(slot.skill.filePath);
319
+ if (file.dev !== slot.fileDevice ||
320
+ file.ino !== slot.fileInode ||
321
+ file.size !== slot.fileSize ||
322
+ file.mtimeMs !== slot.fileModifiedAtMs ||
323
+ file.ctimeMs !== slot.fileChangedAtMs) {
324
+ return "resource_unavailable";
251
325
  }
252
326
  }
253
327
  catch {
254
- this.replaceState({ state: "unloaded", reason: "resource_unavailable" });
255
- return false;
328
+ return "resource_unavailable";
256
329
  }
257
- return true;
330
+ if (now - slotLastUsedAtMs(slot) >= this.idleTimeoutMs)
331
+ return "idle_expired";
332
+ return undefined;
258
333
  }
259
- replaceState(state) {
260
- this.current = state;
334
+ replaceState(slots, unloadReason) {
335
+ this.slots = slots;
336
+ if (unloadReason !== undefined)
337
+ this.unloadReason = unloadReason;
261
338
  this.contextRevision++;
262
339
  }
263
340
  resolveMaxBodyBytes() {
@@ -1 +1 @@
1
- {"version":3,"file":"skill-vault.js","sourceRoot":"","sources":["../../src/core/skill-vault.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,0BAA0B,EAAE,MAAM,oDAAoD,CAAC;AAChG,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,2BAA2B,EAAqC,MAAM,aAAa,CAAC;AAC7F,OAAO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I,CAAC,OAAO,KAAK,MAAM,CAAC,eAAe,EACtC,CAAC;gBACF,IAAI,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC,CAAC;gBACzE,OAAO,KAAK,CAAC;YACd,CAAC;QACF,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC,CAAC;YACzE,OAAO,KAAK,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;IAEO,YAAY,CAAC,KAAsB;QAC1C,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,eAAe,EAAE,CAAC;IACxB,CAAC;IAEO,mBAAmB;QAC1B,MAAM,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,CAAC;QAC1C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC;YAAE,OAAO,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,2BAA2B,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACnF,CAAC;CACD","sourcesContent":["import { statSync } from \"node:fs\";\nimport { performance } from \"node:perf_hooks\";\nimport { composeRequestSystemPrompt } from \"@caupulican/pi-agent-core/provider-request-planner\";\nimport { parseFrontmatter } from \"../utils/frontmatter.ts\";\nimport { stripResourceProfileBlocks } from \"./resource-profile-blocks.ts\";\nimport { MAX_SKILL_FRONTMATTER_BYTES, type Skill, type SkillFrontmatter } from \"./skills.ts\";\nimport { readBoundedTextFileSync, sameFileVersion } from \"./util/bounded-file.ts\";\n\nexport const DEFAULT_SKILL_IDLE_TIMEOUT_MS = 10 * 60 * 1000;\nexport const MAX_ACTIVE_SKILL_BODY_BYTES = 64 * 1024;\nexport const MIN_ACTIVE_SKILL_BODY_BYTES = 4 * 1024;\nconst MAX_SEARCH_RESULTS = 5;\nconst MAX_SEARCH_DESCRIPTION_CHARS = 240;\n\ntype SkillVaultRequester = \"model\" | \"user\";\ntype SkillVaultUnloadReason = \"explicit\" | \"idle_expired\" | \"resource_unavailable\" | \"budget_exceeded\";\n\ninterface LoadedSkill {\n\tskill: Skill;\n\tbodyBytes: number;\n\tsystemPromptSection: string;\n\trequester: SkillVaultRequester;\n\tloadedAtMs: number;\n\tfileDevice: number;\n\tfileInode: number;\n\tfileSize: number;\n\tfileModifiedAtMs: number;\n\tfileChangedAtMs: number;\n}\n\ntype SkillVaultState =\n\t| { state: \"unloaded\"; reason?: SkillVaultUnloadReason }\n\t| ({ state: \"loaded_pending\" } & LoadedSkill)\n\t| ({ state: \"active\"; lastUsedAtMs: number; useCount: number } & LoadedSkill);\n\nexport interface SkillVaultStatus {\n\tstate: SkillVaultState[\"state\"];\n\tidleTimeoutMs: number;\n\tname?: string;\n\tloadedAtMs?: number;\n\tlastUsedAtMs?: number;\n\tidleForMs?: number;\n\texpiresInMs?: number;\n\tuseCount?: number;\n\treason?: SkillVaultUnloadReason;\n}\n\nexport interface SkillSearchResult {\n\tcandidates: Array<{ name: string; description: string }>;\n}\n\nexport type SkillLoadResult =\n\t| { ok: true; state: \"loaded_pending\"; name: string; replaced?: string }\n\t| { ok: false; reason: \"not_found\" | \"body_too_large\" | \"invalid_body\" | \"read_failed\"; message: string };\n\nexport interface SkillVaultControllerOptions {\n\tgetSkills(): readonly Skill[];\n\tnow?: () => number;\n\tidleTimeoutMs?: number;\n\tgetMaxBodyBytes?: () => number;\n\tonSkillUsed?: (skill: Skill, usedAtMs: number) => void;\n}\n\nfunction compactDescription(description: string): string {\n\tconst normalized = description.replace(/\\s+/g, \" \").trim();\n\tif (normalized.length <= MAX_SEARCH_DESCRIPTION_CHARS) return normalized;\n\treturn `${normalized.slice(0, MAX_SEARCH_DESCRIPTION_CHARS - 1)}…`;\n}\n\nfunction queryTokens(value: string): string[] {\n\treturn [...new Set(value.toLowerCase().match(/[\\p{L}\\p{N}]+/gu) ?? [])];\n}\n\nfunction searchScore(skill: Skill, query: string, tokens: readonly string[]): number {\n\tconst name = skill.name.toLowerCase();\n\tconst description = skill.description.toLowerCase();\n\tlet score = name === query ? 100 : name.includes(query) ? 40 : description.includes(query) ? 20 : 0;\n\tfor (const token of tokens) {\n\t\tif (name === token) score += 16;\n\t\telse if (name.includes(token)) score += 8;\n\t\tif (description.includes(token)) score += 3;\n\t}\n\treturn score;\n}\n\nfunction activeSkillContext(skill: Skill, body: string): string {\n\treturn [`ACTIVE SKILL ${skill.name}`, `BASE ${skill.baseDir}`, \"NON-NEGOTIABLE WHILE ACTIVE:\", body].join(\"\\n\");\n}\n\n/** Reserve at most roughly one context token's worth of bytes per advertised context token. */\nexport function resolveActiveSkillBodyByteLimit(contextWindow: number | undefined): number {\n\tif (contextWindow === undefined || !Number.isFinite(contextWindow) || contextWindow <= 0) {\n\t\treturn MAX_ACTIVE_SKILL_BODY_BYTES;\n\t}\n\treturn Math.min(MAX_ACTIVE_SKILL_BODY_BYTES, Math.max(MIN_ACTIVE_SKILL_BODY_BYTES, Math.floor(contextWindow)));\n}\n\n/** One host-owned, event-driven lifecycle for lazy skill discovery and transient context projection. */\nexport class SkillVaultController {\n\tprivate readonly getSkills: () => readonly Skill[];\n\tprivate readonly now: () => number;\n\tprivate readonly idleTimeoutMs: number;\n\tprivate readonly getMaxBodyBytes: () => number;\n\tprivate readonly onSkillUsed: ((skill: Skill, usedAtMs: number) => void) | undefined;\n\tprivate current: SkillVaultState = { state: \"unloaded\" };\n\tprivate contextRevision = 0;\n\n\tconstructor(options: SkillVaultControllerOptions) {\n\t\tif (!Number.isFinite(options.idleTimeoutMs ?? DEFAULT_SKILL_IDLE_TIMEOUT_MS)) {\n\t\t\tthrow new TypeError(\"Skill idle timeout must be finite.\");\n\t\t}\n\t\tthis.getSkills = options.getSkills;\n\t\tthis.now = options.now ?? (() => performance.now());\n\t\tthis.idleTimeoutMs = Math.max(1, options.idleTimeoutMs ?? DEFAULT_SKILL_IDLE_TIMEOUT_MS);\n\t\tthis.getMaxBodyBytes = options.getMaxBodyBytes ?? (() => MAX_ACTIVE_SKILL_BODY_BYTES);\n\t\tthis.onSkillUsed = options.onSkillUsed;\n\t}\n\n\tsearch(rawQuery: string): SkillSearchResult {\n\t\tconst query = rawQuery.trim().toLowerCase();\n\t\tconst tokens = queryTokens(query);\n\t\tif (!query || tokens.length === 0) return { candidates: [] };\n\t\tconst candidates = this.getSkills()\n\t\t\t.filter((skill) => !skill.disableModelInvocation)\n\t\t\t.map((skill) => ({ skill, score: searchScore(skill, query, tokens) }))\n\t\t\t.filter((entry) => entry.score > 0)\n\t\t\t.sort((left, right) => right.score - left.score || left.skill.name.localeCompare(right.skill.name))\n\t\t\t.slice(0, MAX_SEARCH_RESULTS)\n\t\t\t.map(({ skill }) => ({ name: skill.name, description: compactDescription(skill.description) }));\n\t\treturn { candidates };\n\t}\n\n\tload(name: string, requester: SkillVaultRequester): SkillLoadResult {\n\t\tconst skill = this.getSkills().find(\n\t\t\t(candidate) => candidate.name === name && (requester === \"user\" || !candidate.disableModelInvocation),\n\t\t);\n\t\tif (!skill) {\n\t\t\treturn { ok: false, reason: \"not_found\", message: `No eligible skill named ${JSON.stringify(name)}.` };\n\t\t}\n\t\tconst maxBodyBytes = this.resolveMaxBodyBytes();\n\t\tlet before: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tbefore = statSync(skill.filePath);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t\tlet raw: string;\n\t\ttry {\n\t\t\traw = readBoundedTextFileSync(\n\t\t\t\tskill.filePath,\n\t\t\t\tmaxBodyBytes + MAX_SKILL_FRONTMATTER_BYTES,\n\t\t\t\t`Skill ${JSON.stringify(skill.name)}`,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\treturn message.includes(\"exceeds its byte limit\")\n\t\t\t\t? {\n\t\t\t\t\t\tok: false,\n\t\t\t\t\t\treason: \"body_too_large\",\n\t\t\t\t\t\tmessage: `Skill body exceeds ${maxBodyBytes} bytes; not truncated.`,\n\t\t\t\t\t}\n\t\t\t\t: { ok: false, reason: \"read_failed\", message };\n\t\t}\n\t\tconst parsed = parseFrontmatter<SkillFrontmatter>(raw);\n\t\tconst currentName = parsed.frontmatter.name ?? skill.name;\n\t\tconst body = stripResourceProfileBlocks(parsed.body).trim();\n\t\tif (currentName !== skill.name || !parsed.frontmatter.description || !body) {\n\t\t\treturn { ok: false, reason: \"invalid_body\", message: \"Skill metadata changed or its body is empty.\" };\n\t\t}\n\t\tconst bodyBytes = Buffer.byteLength(body, \"utf8\");\n\t\tif (bodyBytes > maxBodyBytes) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\treason: \"body_too_large\",\n\t\t\t\tmessage: `Skill body exceeds ${maxBodyBytes} bytes; not truncated.`,\n\t\t\t};\n\t\t}\n\t\tlet file: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tfile = statSync(skill.filePath);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t\tif (!sameFileVersion(before, file)) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: \"Skill changed while it was being loaded.\" };\n\t\t}\n\t\tconst replaced = this.current.state === \"unloaded\" ? undefined : this.current.skill.name;\n\t\tthis.replaceState({\n\t\t\tstate: \"loaded_pending\",\n\t\t\tskill,\n\t\t\tbodyBytes,\n\t\t\tsystemPromptSection: activeSkillContext(skill, body),\n\t\t\trequester,\n\t\t\tloadedAtMs: this.now(),\n\t\t\tfileDevice: file.dev,\n\t\t\tfileInode: file.ino,\n\t\t\tfileSize: file.size,\n\t\t\tfileModifiedAtMs: file.mtimeMs,\n\t\t\tfileChangedAtMs: file.ctimeMs,\n\t\t});\n\t\treturn { ok: true, state: \"loaded_pending\", name: skill.name, ...(replaced ? { replaced } : {}) };\n\t}\n\n\tunload(): { ok: true; unloaded?: string } {\n\t\tconst unloaded = this.current.state === \"unloaded\" ? undefined : this.current.skill.name;\n\t\tthis.replaceState({ state: \"unloaded\", reason: \"explicit\" });\n\t\treturn { ok: true, ...(unloaded ? { unloaded } : {}) };\n\t}\n\n\tstatus(): SkillVaultStatus {\n\t\tconst now = this.now();\n\t\tthis.reconcile(now);\n\t\tif (this.current.state === \"unloaded\") {\n\t\t\treturn {\n\t\t\t\tstate: \"unloaded\",\n\t\t\t\tidleTimeoutMs: this.idleTimeoutMs,\n\t\t\t\t...(this.current.reason ? { reason: this.current.reason } : {}),\n\t\t\t};\n\t\t}\n\t\tif (this.current.state === \"loaded_pending\") {\n\t\t\treturn {\n\t\t\t\tstate: \"loaded_pending\",\n\t\t\t\tidleTimeoutMs: this.idleTimeoutMs,\n\t\t\t\tname: this.current.skill.name,\n\t\t\t\tloadedAtMs: this.current.loadedAtMs,\n\t\t\t};\n\t\t}\n\t\tconst idleForMs = Math.max(0, now - this.current.lastUsedAtMs);\n\t\treturn {\n\t\t\tstate: \"active\",\n\t\t\tidleTimeoutMs: this.idleTimeoutMs,\n\t\t\tname: this.current.skill.name,\n\t\t\tloadedAtMs: this.current.loadedAtMs,\n\t\t\tlastUsedAtMs: this.current.lastUsedAtMs,\n\t\t\tidleForMs,\n\t\t\texpiresInMs: Math.max(0, this.idleTimeoutMs - idleForMs),\n\t\t\tuseCount: this.current.useCount,\n\t\t};\n\t}\n\n\tcommitSystemPromptSection(): string | undefined {\n\t\tconst now = this.now();\n\t\tthis.reconcile(now);\n\t\tif (this.current.state === \"unloaded\") return undefined;\n\t\tconst loaded = this.current;\n\t\tconst firstUse = loaded.state === \"loaded_pending\";\n\t\tconst useCount = firstUse ? 1 : loaded.useCount + 1;\n\t\tthis.current = { ...loaded, state: \"active\", lastUsedAtMs: now, useCount };\n\t\tif (firstUse) {\n\t\t\ttry {\n\t\t\t\tthis.onSkillUsed?.(loaded.skill, now);\n\t\t\t} catch {\n\t\t\t\t// Usage telemetry must never block skill application.\n\t\t\t}\n\t\t}\n\t\treturn loaded.systemPromptSection;\n\t}\n\n\t/** Model the next request's transient system cost without treating a diagnostic read as use. */\n\tpreviewSystemPromptSection(): string | undefined {\n\t\tthis.reconcile(this.now());\n\t\treturn this.current.state === \"unloaded\" ? undefined : this.current.systemPromptSection;\n\t}\n\n\t/** Compose the exact provider system prompt for read-only diagnostics. */\n\tpreviewRequestSystemPrompt(base: string | undefined): string | undefined {\n\t\treturn composeRequestSystemPrompt(base, this.previewSystemPromptSection());\n\t}\n\n\t/** Monotonic identity for provider-visible skill projection changes. */\n\tgetContextRevision(): number {\n\t\tthis.reconcile(this.now());\n\t\treturn this.contextRevision;\n\t}\n\n\t/** Record host-observed work derived from an active skill, independent of agent cooperation. */\n\tnoteActivity(): void {\n\t\tconst now = this.now();\n\t\tif (this.current.state === \"active\") {\n\t\t\tthis.current = { ...this.current, lastUsedAtMs: now };\n\t\t}\n\t}\n\n\tprivate reconcile(now: number): void {\n\t\tif (!this.reconcileResource()) return;\n\t\tconst current = this.current;\n\t\tif (current.state === \"unloaded\") return;\n\t\tconst lastUsedAtMs = current.state === \"loaded_pending\" ? current.loadedAtMs : current.lastUsedAtMs;\n\t\tif (now - lastUsedAtMs >= this.idleTimeoutMs) {\n\t\t\tthis.replaceState({ state: \"unloaded\", reason: \"idle_expired\" });\n\t\t}\n\t}\n\n\tprivate reconcileResource(): boolean {\n\t\tif (this.current.state === \"unloaded\") return false;\n\t\tconst loaded = this.current;\n\t\tif (loaded.bodyBytes > this.resolveMaxBodyBytes()) {\n\t\t\tthis.replaceState({ state: \"unloaded\", reason: \"budget_exceeded\" });\n\t\t\treturn false;\n\t\t}\n\t\tconst currentSkill = this.getSkills().find(\n\t\t\t(skill) =>\n\t\t\t\tskill.name === loaded.skill.name &&\n\t\t\t\tskill.filePath === loaded.skill.filePath &&\n\t\t\t\t(loaded.requester === \"user\" || !skill.disableModelInvocation),\n\t\t);\n\t\tif (!currentSkill) {\n\t\t\tthis.replaceState({ state: \"unloaded\", reason: \"resource_unavailable\" });\n\t\t\treturn false;\n\t\t}\n\t\ttry {\n\t\t\tconst file = statSync(loaded.skill.filePath);\n\t\t\tif (\n\t\t\t\tfile.dev !== loaded.fileDevice ||\n\t\t\t\tfile.ino !== loaded.fileInode ||\n\t\t\t\tfile.size !== loaded.fileSize ||\n\t\t\t\tfile.mtimeMs !== loaded.fileModifiedAtMs ||\n\t\t\t\tfile.ctimeMs !== loaded.fileChangedAtMs\n\t\t\t) {\n\t\t\t\tthis.replaceState({ state: \"unloaded\", reason: \"resource_unavailable\" });\n\t\t\t\treturn false;\n\t\t\t}\n\t\t} catch {\n\t\t\tthis.replaceState({ state: \"unloaded\", reason: \"resource_unavailable\" });\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tprivate replaceState(state: SkillVaultState): void {\n\t\tthis.current = state;\n\t\tthis.contextRevision++;\n\t}\n\n\tprivate resolveMaxBodyBytes(): number {\n\t\tconst configured = this.getMaxBodyBytes();\n\t\tif (!Number.isFinite(configured)) return 1;\n\t\treturn Math.min(MAX_ACTIVE_SKILL_BODY_BYTES, Math.max(1, Math.floor(configured)));\n\t}\n}\n"]}
1
+ {"version":3,"file":"skill-vault.js","sourceRoot":"","sources":["../../src/core/skill-vault.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,0BAA0B,EAAE,MAAM,oDAAoD,CAAC;AAChG,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,2BAA2B,EAAqC,MAAM,aAAa,CAAC;AAC7F,OAAO,EAAE,uBAAuB,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,OAAO,IAAI,CAAC,GAAG,CAAC,2BAA2B,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACnF,CAAC;CACD","sourcesContent":["import { statSync } from \"node:fs\";\nimport { performance } from \"node:perf_hooks\";\nimport { composeRequestSystemPrompt } from \"@caupulican/pi-agent-core/provider-request-planner\";\nimport { parseFrontmatter } from \"../utils/frontmatter.ts\";\nimport { stripResourceProfileBlocks } from \"./resource-profile-blocks.ts\";\nimport { MAX_SKILL_FRONTMATTER_BYTES, type Skill, type SkillFrontmatter } from \"./skills.ts\";\nimport { readBoundedTextFileSync, sameFileVersion } from \"./util/bounded-file.ts\";\n\nexport const DEFAULT_SKILL_IDLE_TIMEOUT_MS = 10 * 60 * 1000;\nexport const MAX_ACTIVE_SKILL_BODY_BYTES = 64 * 1024;\nexport const MIN_ACTIVE_SKILL_BODY_BYTES = 4 * 1024;\nexport const MAX_LOADED_SKILLS = 3;\nexport const MAX_PINNED_SKILLS = 2;\nconst MAX_SEARCH_RESULTS = 5;\nconst MAX_SEARCH_DESCRIPTION_CHARS = 240;\n\ntype SkillVaultRequester = \"model\" | \"user\";\ntype SkillVaultUnloadReason = \"explicit\" | \"idle_expired\" | \"resource_unavailable\" | \"budget_exceeded\";\n\ninterface LoadedSkill {\n\tskill: Skill;\n\tbodyBytes: number;\n\tsystemPromptSection: string;\n\trequester: SkillVaultRequester;\n\tpinned: boolean;\n\tloadedAtMs: number;\n\tfileDevice: number;\n\tfileInode: number;\n\tfileSize: number;\n\tfileModifiedAtMs: number;\n\tfileChangedAtMs: number;\n}\n\ntype SkillSlotState =\n\t| ({ state: \"loaded_pending\" } & LoadedSkill)\n\t| ({ state: \"active\"; lastUsedAtMs: number; useCount: number } & LoadedSkill);\n\nexport interface SkillSlotStatus {\n\tstate: SkillSlotState[\"state\"];\n\tname: string;\n\tpinned: boolean;\n\tloadedAtMs: number;\n\tlastUsedAtMs?: number;\n\tidleForMs?: number;\n\texpiresInMs?: number;\n\tuseCount?: number;\n}\n\nexport interface SkillVaultStatus {\n\tidleTimeoutMs: number;\n\tslots: SkillSlotStatus[];\n\treason?: SkillVaultUnloadReason;\n}\n\nexport interface SkillSearchResult {\n\tcandidates: Array<{ name: string; description: string }>;\n}\n\nexport type SkillLoadResult =\n\t| { ok: true; state: \"loaded_pending\"; name: string; baseDir: string; pinned: boolean; evicted?: string[] }\n\t| {\n\t\t\tok: false;\n\t\t\treason: \"not_found\" | \"body_too_large\" | \"invalid_body\" | \"read_failed\" | \"pin_limit\";\n\t\t\tmessage: string;\n\t };\n\nexport interface SkillVaultControllerOptions {\n\tgetSkills(): readonly Skill[];\n\tnow?: () => number;\n\tidleTimeoutMs?: number;\n\tgetMaxBodyBytes?: () => number;\n\tonSkillUsed?: (skill: Skill, usedAtMs: number) => void;\n}\n\nfunction compactDescription(description: string): string {\n\tconst normalized = description.replace(/\\s+/g, \" \").trim();\n\tif (normalized.length <= MAX_SEARCH_DESCRIPTION_CHARS) return normalized;\n\treturn `${normalized.slice(0, MAX_SEARCH_DESCRIPTION_CHARS - 1)}…`;\n}\n\nfunction queryTokens(value: string): string[] {\n\treturn [...new Set(value.toLowerCase().match(/[\\p{L}\\p{N}]+/gu) ?? [])];\n}\n\nfunction searchScore(skill: Skill, query: string, tokens: readonly string[]): number {\n\tconst name = skill.name.toLowerCase();\n\tconst description = skill.description.toLowerCase();\n\tlet score = name === query ? 100 : name.includes(query) ? 40 : description.includes(query) ? 20 : 0;\n\tfor (const token of tokens) {\n\t\tif (name === token) score += 16;\n\t\telse if (name.includes(token)) score += 8;\n\t\tif (description.includes(token)) score += 3;\n\t}\n\treturn score;\n}\n\nfunction activeSkillContext(skill: Skill, body: string): string {\n\treturn [`ACTIVE SKILL ${skill.name}`, `BASE ${skill.baseDir}`, \"NON-NEGOTIABLE WHILE ACTIVE:\", body].join(\"\\n\");\n}\n\nfunction slotLastUsedAtMs(slot: SkillSlotState): number {\n\treturn slot.state === \"loaded_pending\" ? slot.loadedAtMs : slot.lastUsedAtMs;\n}\n\nfunction aggregateBodyBytes(slots: ReadonlyMap<string, SkillSlotState>): number {\n\tlet total = 0;\n\tfor (const slot of slots.values()) total += slot.bodyBytes;\n\treturn total;\n}\n\n/** Per-skill use is unobservable host-side (every loaded body rides every request), so eviction is honest FIFO by loadedAtMs: oldest unpinned first, oldest pinned only once no unpinned slot remains. */\nfunction evictionVictimName(slots: ReadonlyMap<string, SkillSlotState>, excludeName?: string): string | undefined {\n\tlet victim: { name: string; pinned: boolean; loadedAtMs: number } | undefined;\n\tfor (const [name, slot] of slots) {\n\t\tif (name === excludeName) continue;\n\t\tif (\n\t\t\tvictim === undefined ||\n\t\t\t(victim.pinned && !slot.pinned) ||\n\t\t\t(victim.pinned === slot.pinned && slot.loadedAtMs < victim.loadedAtMs)\n\t\t) {\n\t\t\tvictim = { name, pinned: slot.pinned, loadedAtMs: slot.loadedAtMs };\n\t\t}\n\t}\n\treturn victim?.name;\n}\n\n/** Reserve at most roughly one context token's worth of bytes per advertised context token. */\nexport function resolveActiveSkillBodyByteLimit(contextWindow: number | undefined): number {\n\tif (contextWindow === undefined || !Number.isFinite(contextWindow) || contextWindow <= 0) {\n\t\treturn MAX_ACTIVE_SKILL_BODY_BYTES;\n\t}\n\treturn Math.min(MAX_ACTIVE_SKILL_BODY_BYTES, Math.max(MIN_ACTIVE_SKILL_BODY_BYTES, Math.floor(contextWindow)));\n}\n\n/** One host-owned, event-driven lifecycle for lazy skill discovery and transient context projection. */\nexport class SkillVaultController {\n\tprivate readonly getSkills: () => readonly Skill[];\n\tprivate readonly now: () => number;\n\tprivate readonly idleTimeoutMs: number;\n\tprivate readonly getMaxBodyBytes: () => number;\n\tprivate readonly onSkillUsed: ((skill: Skill, usedAtMs: number) => void) | undefined;\n\tprivate slots = new Map<string, SkillSlotState>();\n\tprivate unloadReason: SkillVaultUnloadReason | undefined;\n\tprivate contextRevision = 0;\n\n\tconstructor(options: SkillVaultControllerOptions) {\n\t\tif (!Number.isFinite(options.idleTimeoutMs ?? DEFAULT_SKILL_IDLE_TIMEOUT_MS)) {\n\t\t\tthrow new TypeError(\"Skill idle timeout must be finite.\");\n\t\t}\n\t\tthis.getSkills = options.getSkills;\n\t\tthis.now = options.now ?? (() => performance.now());\n\t\tthis.idleTimeoutMs = Math.max(1, options.idleTimeoutMs ?? DEFAULT_SKILL_IDLE_TIMEOUT_MS);\n\t\tthis.getMaxBodyBytes = options.getMaxBodyBytes ?? (() => MAX_ACTIVE_SKILL_BODY_BYTES);\n\t\tthis.onSkillUsed = options.onSkillUsed;\n\t}\n\n\tsearch(rawQuery: string): SkillSearchResult {\n\t\tconst query = rawQuery.trim().toLowerCase();\n\t\tconst tokens = queryTokens(query);\n\t\tif (!query || tokens.length === 0) return { candidates: [] };\n\t\tconst candidates = this.getSkills()\n\t\t\t.filter((skill) => !skill.disableModelInvocation)\n\t\t\t.map((skill) => ({ skill, score: searchScore(skill, query, tokens) }))\n\t\t\t.filter((entry) => entry.score > 0)\n\t\t\t.sort((left, right) => right.score - left.score || left.skill.name.localeCompare(right.skill.name))\n\t\t\t.slice(0, MAX_SEARCH_RESULTS)\n\t\t\t.map(({ skill }) => ({ name: skill.name, description: compactDescription(skill.description) }));\n\t\treturn { candidates };\n\t}\n\n\tload(name: string, requester: SkillVaultRequester, pin = false): SkillLoadResult {\n\t\tconst now = this.now();\n\t\tthis.reconcile(now);\n\t\tconst skill = this.getSkills().find(\n\t\t\t(candidate) => candidate.name === name && (requester === \"user\" || !candidate.disableModelInvocation),\n\t\t);\n\t\tif (!skill) {\n\t\t\treturn { ok: false, reason: \"not_found\", message: `No eligible skill named ${JSON.stringify(name)}.` };\n\t\t}\n\t\tif (pin) {\n\t\t\tlet pinnedCount = 0;\n\t\t\tfor (const [slotName, slot] of this.slots) {\n\t\t\t\tif (slotName !== skill.name && slot.pinned) pinnedCount++;\n\t\t\t}\n\t\t\tif (pinnedCount >= MAX_PINNED_SKILLS) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\treason: \"pin_limit\",\n\t\t\t\t\tmessage: `At most ${MAX_PINNED_SKILLS} skills can be pinned; unload a pinned skill or reload it without pin first.`,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\tconst maxBodyBytes = this.resolveMaxBodyBytes();\n\t\tlet before: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tbefore = statSync(skill.filePath);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t\tlet raw: string;\n\t\ttry {\n\t\t\traw = readBoundedTextFileSync(\n\t\t\t\tskill.filePath,\n\t\t\t\tmaxBodyBytes + MAX_SKILL_FRONTMATTER_BYTES,\n\t\t\t\t`Skill ${JSON.stringify(skill.name)}`,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\treturn message.includes(\"exceeds its byte limit\")\n\t\t\t\t? {\n\t\t\t\t\t\tok: false,\n\t\t\t\t\t\treason: \"body_too_large\",\n\t\t\t\t\t\tmessage: `Skill body exceeds ${maxBodyBytes} bytes; not truncated.`,\n\t\t\t\t\t}\n\t\t\t\t: { ok: false, reason: \"read_failed\", message };\n\t\t}\n\t\tconst parsed = parseFrontmatter<SkillFrontmatter>(raw);\n\t\tconst currentName = parsed.frontmatter.name ?? skill.name;\n\t\tconst body = stripResourceProfileBlocks(parsed.body).trim();\n\t\tif (currentName !== skill.name || !parsed.frontmatter.description || !body) {\n\t\t\treturn { ok: false, reason: \"invalid_body\", message: \"Skill metadata changed or its body is empty.\" };\n\t\t}\n\t\tconst bodyBytes = Buffer.byteLength(body, \"utf8\");\n\t\tif (bodyBytes > maxBodyBytes) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\treason: \"body_too_large\",\n\t\t\t\tmessage: `Skill body exceeds ${maxBodyBytes} bytes; not truncated.`,\n\t\t\t};\n\t\t}\n\t\tlet file: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tfile = statSync(skill.filePath);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t\tif (!sameFileVersion(before, file)) {\n\t\t\treturn { ok: false, reason: \"read_failed\", message: \"Skill changed while it was being loaded.\" };\n\t\t}\n\t\tconst next = new Map(this.slots);\n\t\tnext.set(skill.name, {\n\t\t\tstate: \"loaded_pending\",\n\t\t\tskill,\n\t\t\tbodyBytes,\n\t\t\tsystemPromptSection: activeSkillContext(skill, body),\n\t\t\trequester,\n\t\t\tpinned: pin,\n\t\t\tloadedAtMs: now,\n\t\t\tfileDevice: file.dev,\n\t\t\tfileInode: file.ino,\n\t\t\tfileSize: file.size,\n\t\t\tfileModifiedAtMs: file.mtimeMs,\n\t\t\tfileChangedAtMs: file.ctimeMs,\n\t\t});\n\t\tconst evicted: string[] = [];\n\t\twhile (next.size > MAX_LOADED_SKILLS || aggregateBodyBytes(next) > maxBodyBytes) {\n\t\t\tconst victim = evictionVictimName(next, skill.name);\n\t\t\tif (!victim) break;\n\t\t\tnext.delete(victim);\n\t\t\tevicted.push(victim);\n\t\t}\n\t\tthis.replaceState(next);\n\t\treturn {\n\t\t\tok: true,\n\t\t\tstate: \"loaded_pending\",\n\t\t\tname: skill.name,\n\t\t\tbaseDir: skill.baseDir,\n\t\t\tpinned: pin,\n\t\t\t...(evicted.length > 0 ? { evicted } : {}),\n\t\t};\n\t}\n\n\tunload(name?: string): { ok: true; unloaded: string[] } {\n\t\tthis.reconcile(this.now());\n\t\tconst target = name?.trim();\n\t\tconst unloaded = !target ? [...this.slots.keys()] : this.slots.has(target) ? [target] : [];\n\t\tif (unloaded.length > 0) {\n\t\t\tconst next = new Map(this.slots);\n\t\t\tfor (const slotName of unloaded) next.delete(slotName);\n\t\t\tthis.replaceState(next, \"explicit\");\n\t\t}\n\t\treturn { ok: true, unloaded };\n\t}\n\n\tstatus(): SkillVaultStatus {\n\t\tconst now = this.now();\n\t\tthis.reconcile(now);\n\t\tconst slots = [...this.slots.values()].map((slot) => this.slotStatus(slot, now));\n\t\treturn {\n\t\t\tidleTimeoutMs: this.idleTimeoutMs,\n\t\t\tslots,\n\t\t\t...(slots.length === 0 && this.unloadReason ? { reason: this.unloadReason } : {}),\n\t\t};\n\t}\n\n\tcommitSystemPromptSection(): string | undefined {\n\t\tconst now = this.now();\n\t\tthis.reconcile(now);\n\t\tif (this.slots.size === 0) return undefined;\n\t\tconst sections: string[] = [];\n\t\tfor (const [name, slot] of this.slots) {\n\t\t\tsections.push(slot.systemPromptSection);\n\t\t\tconst firstUse = slot.state === \"loaded_pending\";\n\t\t\tconst useCount = firstUse ? 1 : slot.useCount + 1;\n\t\t\tthis.slots.set(name, { ...slot, state: \"active\", lastUsedAtMs: now, useCount });\n\t\t\tif (firstUse) {\n\t\t\t\ttry {\n\t\t\t\t\tthis.onSkillUsed?.(slot.skill, now);\n\t\t\t\t} catch {\n\t\t\t\t\t// Usage telemetry must never block skill application.\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn sections.join(\"\\n\\n\");\n\t}\n\n\t/** Model the next request's transient system cost without treating a diagnostic read as use. */\n\tpreviewSystemPromptSection(): string | undefined {\n\t\tthis.reconcile(this.now());\n\t\tif (this.slots.size === 0) return undefined;\n\t\treturn [...this.slots.values()].map((slot) => slot.systemPromptSection).join(\"\\n\\n\");\n\t}\n\n\t/** Compose the exact provider system prompt for read-only diagnostics. */\n\tpreviewRequestSystemPrompt(base: string | undefined): string | undefined {\n\t\treturn composeRequestSystemPrompt(base, this.previewSystemPromptSection());\n\t}\n\n\t/** Monotonic identity for provider-visible skill projection changes. */\n\tgetContextRevision(): number {\n\t\tthis.reconcile(this.now());\n\t\treturn this.contextRevision;\n\t}\n\n\t/** Record host-observed work derived from an active skill, independent of agent cooperation. */\n\tnoteActivity(): void {\n\t\tconst now = this.now();\n\t\tfor (const [name, slot] of this.slots) {\n\t\t\tif (slot.state === \"active\") {\n\t\t\t\tthis.slots.set(name, { ...slot, lastUsedAtMs: now });\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate slotStatus(slot: SkillSlotState, now: number): SkillSlotStatus {\n\t\tif (slot.state === \"loaded_pending\") {\n\t\t\treturn { state: \"loaded_pending\", name: slot.skill.name, pinned: slot.pinned, loadedAtMs: slot.loadedAtMs };\n\t\t}\n\t\tconst idleForMs = Math.max(0, now - slot.lastUsedAtMs);\n\t\treturn {\n\t\t\tstate: \"active\",\n\t\t\tname: slot.skill.name,\n\t\t\tpinned: slot.pinned,\n\t\t\tloadedAtMs: slot.loadedAtMs,\n\t\t\tlastUsedAtMs: slot.lastUsedAtMs,\n\t\t\tidleForMs,\n\t\t\texpiresInMs: Math.max(0, this.idleTimeoutMs - idleForMs),\n\t\t\tuseCount: slot.useCount,\n\t\t};\n\t}\n\n\tprivate reconcile(now: number): void {\n\t\tif (this.slots.size === 0) return;\n\t\tconst maxBodyBytes = this.resolveMaxBodyBytes();\n\t\tconst skills = this.getSkills();\n\t\tconst next = new Map(this.slots);\n\t\tlet reason: SkillVaultUnloadReason | undefined;\n\t\tfor (const [name, slot] of this.slots) {\n\t\t\tconst slotReason = this.reconcileSlot(slot, skills, maxBodyBytes, now);\n\t\t\tif (slotReason !== undefined) {\n\t\t\t\tnext.delete(name);\n\t\t\t\treason = slotReason;\n\t\t\t}\n\t\t}\n\t\twhile (aggregateBodyBytes(next) > maxBodyBytes) {\n\t\t\tconst victim = evictionVictimName(next);\n\t\t\tif (!victim) break;\n\t\t\tnext.delete(victim);\n\t\t\treason = \"budget_exceeded\";\n\t\t}\n\t\tif (next.size !== this.slots.size) {\n\t\t\tthis.replaceState(next, reason);\n\t\t}\n\t}\n\n\tprivate reconcileSlot(\n\t\tslot: SkillSlotState,\n\t\tskills: readonly Skill[],\n\t\tmaxBodyBytes: number,\n\t\tnow: number,\n\t): SkillVaultUnloadReason | undefined {\n\t\tif (slot.bodyBytes > maxBodyBytes) return \"budget_exceeded\";\n\t\tconst currentSkill = skills.find(\n\t\t\t(skill) =>\n\t\t\t\tskill.name === slot.skill.name &&\n\t\t\t\tskill.filePath === slot.skill.filePath &&\n\t\t\t\t(slot.requester === \"user\" || !skill.disableModelInvocation),\n\t\t);\n\t\tif (!currentSkill) return \"resource_unavailable\";\n\t\ttry {\n\t\t\tconst file = statSync(slot.skill.filePath);\n\t\t\tif (\n\t\t\t\tfile.dev !== slot.fileDevice ||\n\t\t\t\tfile.ino !== slot.fileInode ||\n\t\t\t\tfile.size !== slot.fileSize ||\n\t\t\t\tfile.mtimeMs !== slot.fileModifiedAtMs ||\n\t\t\t\tfile.ctimeMs !== slot.fileChangedAtMs\n\t\t\t) {\n\t\t\t\treturn \"resource_unavailable\";\n\t\t\t}\n\t\t} catch {\n\t\t\treturn \"resource_unavailable\";\n\t\t}\n\t\tif (now - slotLastUsedAtMs(slot) >= this.idleTimeoutMs) return \"idle_expired\";\n\t\treturn undefined;\n\t}\n\n\tprivate replaceState(slots: Map<string, SkillSlotState>, unloadReason?: SkillVaultUnloadReason): void {\n\t\tthis.slots = slots;\n\t\tif (unloadReason !== undefined) this.unloadReason = unloadReason;\n\t\tthis.contextRevision++;\n\t}\n\n\tprivate resolveMaxBodyBytes(): number {\n\t\tconst configured = this.getMaxBodyBytes();\n\t\tif (!Number.isFinite(configured)) return 1;\n\t\treturn Math.min(MAX_ACTIVE_SKILL_BODY_BYTES, Math.max(1, Math.floor(configured)));\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"system-prompt.d.ts","sourceRoot":"","sources":["../../src/core/system-prompt.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAA4C,KAAK,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAE9G,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,WAAW,wB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CAAC,CAAC;IAClH,+CAA+C;IAC/C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yFAAyF;IACzF,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,0DAA0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,qFAAqF;IACrF,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,uCAAuC;IACvC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,yBAAyB;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,wFAAwF;IACxF,YAAY,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACzD,iGAAiG;IACjG,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;IACjB,8CAA8C;IAC9C,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;CACzB;AAkKD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,MAAM,CAqJ3E"}
@@ -13,7 +13,8 @@ ULTRA-TERSE OUTPUT
13
13
 
14
14
  - Drop articles/filler/pleasantries/hedging; fragments valid. Strip conjunctions only when clear; each fact once.
15
15
  - Never drop not/never/no/only/except; never invent abbreviations or use causal arrows. Preserve numbers, units, code symbols, function/API names, commands, errors.
16
- - No self-reference/tool narration/tables/emoji/log dumps. Keep user language. Full grammar: security, irreversible actions, ambiguous order. Replies terse; artifacts normal prose.`;
16
+ - No self-reference/tool narration/tables/emoji/log dumps. Keep user language. Full grammar: security, irreversible actions, ambiguous order. Replies terse; artifacts normal prose.
17
+ - Answer shape: status/ops turns terse; analysis/review/evaluation asks get complete structured answers; never restate harness protocol text or failure-record JSON to the user as an answer.`;
17
18
  const PI_ADAPTATIVE_CORE_SECTION = `
18
19
 
19
20
  OPERATING CONTRACT
@@ -23,31 +24,19 @@ OPERATING CONTRACT
23
24
  - User outcome governs, method does not. Outcome risk: show evidence, test when practical, offer safest effective path, execute.
24
25
  - Work over 15 seconds: managed background run, event terminal, bounded handoff, owner notice; never poll.
25
26
  - Delegate independent work within host bounds; root integrates/verifies. Facts: memory, specialization: skills, behavior: source; discard noise.
27
+ - Implementation/verification work loads skill evidence-gated-tdd; architecture/performance design loads skill n-plus-2-architecture; their gates bind only while that work is active.
28
+ - Explicit user instruction in current message overrides standing style (length/format/tone); security, untrusted-content, and authorization rules are never overridable.
26
29
  - Ask before broader scope, credential or authentication change/disclosure, destructive action, publication. Bound, source-label output; show paths.
27
- - ${MODEL_BLIND_CREDENTIAL_AUTHORITY}
28
-
29
- N+2 ARCHITECTURE
30
-
31
- Language-agnostic principles:
32
- 1. Bounded flat arenas/pools/rings/chunks, batch recycle.
33
- 2. Safe zero/default data/state, no hidden allocation, one activation owner.
34
- 3. Validate trust boundary once; internal miss gets safe stub/default, external failure explicit.
35
- 4. Stable IDs/indexes/buffers/batches; avoid pointer graphs/dispatch/fallback.
36
- 5. Never concatenate growing prefixes, prepend, rescan consumed input, serialize unchanged history, rebuild incremental state; materialize once.
37
-
38
- EVIDENCE GATE
39
-
40
- - Map flow/lifetimes/boundaries/hot paths. Detect, verify, score, gate: baseline; deterministic positive/negative controls; lowest-owner fix; focused/proportionate checks.
41
- - Scanner/fuzzer/log/static/model finding stays candidate pending reproduction. Never weaken tests, claim success from skipped/incomplete probes.`;
30
+ - ${MODEL_BLIND_CREDENTIAL_AUTHORITY}`;
42
31
  const PI_ADAPTATIVE_LEAN_CORE_SECTION = `
43
32
 
44
33
  OPERATING CONTRACT
45
34
 
46
35
  - Complete current goal within scope, granted authority; keep progress, evidence concise.
47
- - Inspect relevant files, project instructions before mutation; make smallest coherent change, run focused checks.
36
+ - Inspect relevant files and instructions before editing; make minimal coherent change, run focused checks.
48
37
  - Use active tools, exact schemas. On failure, read error, expected shape, correct call; never repeat unchanged failure.
49
38
  - Batch independent reads, order mutations; report real output, unresolved failures.
50
- - Ask before destructive actions, credential disclosure or provider authentication changes, publication, push/tag/release, material scope expansion.
39
+ - Ask before destructive actions, credential disclosure or provider authentication changes, publication, push/tag/release, scope expansion.
51
40
  - ${MODEL_BLIND_CREDENTIAL_AUTHORITY}`;
52
41
  const PI_ADAPTATIVE_MINIMAL_CORE_SECTION = `
53
42