@yuaone/core 0.9.7 → 0.9.9

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 (193) hide show
  1. package/dist/__tests__/context-manager.test.js +5 -9
  2. package/dist/__tests__/context-manager.test.js.map +1 -1
  3. package/dist/agent-coordinator.d.ts +172 -0
  4. package/dist/agent-coordinator.d.ts.map +1 -0
  5. package/dist/agent-coordinator.js +390 -0
  6. package/dist/agent-coordinator.js.map +1 -0
  7. package/dist/agent-loop.d.ts +83 -29
  8. package/dist/agent-loop.d.ts.map +1 -1
  9. package/dist/agent-loop.js +697 -335
  10. package/dist/agent-loop.js.map +1 -1
  11. package/dist/agent-reputation.d.ts +72 -0
  12. package/dist/agent-reputation.d.ts.map +1 -0
  13. package/dist/agent-reputation.js +222 -0
  14. package/dist/agent-reputation.js.map +1 -0
  15. package/dist/arch-summarizer.d.ts +48 -0
  16. package/dist/arch-summarizer.d.ts.map +1 -0
  17. package/dist/arch-summarizer.js +239 -0
  18. package/dist/arch-summarizer.js.map +1 -0
  19. package/dist/autonomous/explicit-planner.d.ts +45 -0
  20. package/dist/autonomous/explicit-planner.d.ts.map +1 -0
  21. package/dist/autonomous/explicit-planner.js +99 -0
  22. package/dist/autonomous/explicit-planner.js.map +1 -0
  23. package/dist/autonomous/incident-debugger.d.ts +78 -0
  24. package/dist/autonomous/incident-debugger.d.ts.map +1 -0
  25. package/dist/autonomous/incident-debugger.js +324 -0
  26. package/dist/autonomous/incident-debugger.js.map +1 -0
  27. package/dist/autonomous/index.d.ts +15 -0
  28. package/dist/autonomous/index.d.ts.map +1 -0
  29. package/dist/autonomous/index.js +10 -0
  30. package/dist/autonomous/index.js.map +1 -0
  31. package/dist/autonomous/patch-tournament.d.ts +82 -0
  32. package/dist/autonomous/patch-tournament.d.ts.map +1 -0
  33. package/dist/autonomous/patch-tournament.js +150 -0
  34. package/dist/autonomous/patch-tournament.js.map +1 -0
  35. package/dist/autonomous/research-agent.d.ts +66 -0
  36. package/dist/autonomous/research-agent.d.ts.map +1 -0
  37. package/dist/autonomous/research-agent.js +210 -0
  38. package/dist/autonomous/research-agent.js.map +1 -0
  39. package/dist/autonomous/task-memory.d.ts +63 -0
  40. package/dist/autonomous/task-memory.d.ts.map +1 -0
  41. package/dist/autonomous/task-memory.js +143 -0
  42. package/dist/autonomous/task-memory.js.map +1 -0
  43. package/dist/budget-governor-v2.d.ts +93 -0
  44. package/dist/budget-governor-v2.d.ts.map +1 -0
  45. package/dist/budget-governor-v2.js +345 -0
  46. package/dist/budget-governor-v2.js.map +1 -0
  47. package/dist/capability-graph.d.ts +102 -0
  48. package/dist/capability-graph.d.ts.map +1 -0
  49. package/dist/capability-graph.js +397 -0
  50. package/dist/capability-graph.js.map +1 -0
  51. package/dist/capability-self-model.d.ts +144 -0
  52. package/dist/capability-self-model.d.ts.map +1 -0
  53. package/dist/capability-self-model.js +312 -0
  54. package/dist/capability-self-model.js.map +1 -0
  55. package/dist/checkpoint-manager.d.ts +94 -0
  56. package/dist/checkpoint-manager.d.ts.map +1 -0
  57. package/dist/checkpoint-manager.js +225 -0
  58. package/dist/checkpoint-manager.js.map +1 -0
  59. package/dist/continuation-engine.js +1 -1
  60. package/dist/continuation-engine.js.map +1 -1
  61. package/dist/dag-orchestrator.d.ts +0 -3
  62. package/dist/dag-orchestrator.d.ts.map +1 -1
  63. package/dist/dag-orchestrator.js +0 -1
  64. package/dist/dag-orchestrator.js.map +1 -1
  65. package/dist/evidence-chain.d.ts +99 -0
  66. package/dist/evidence-chain.d.ts.map +1 -0
  67. package/dist/evidence-chain.js +200 -0
  68. package/dist/evidence-chain.js.map +1 -0
  69. package/dist/execution-engine.d.ts.map +1 -1
  70. package/dist/execution-engine.js +0 -1
  71. package/dist/execution-engine.js.map +1 -1
  72. package/dist/failure-signature-memory.d.ts +61 -0
  73. package/dist/failure-signature-memory.d.ts.map +1 -0
  74. package/dist/failure-signature-memory.js +278 -0
  75. package/dist/failure-signature-memory.js.map +1 -0
  76. package/dist/index.d.ts +52 -5
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +48 -7
  79. package/dist/index.js.map +1 -1
  80. package/dist/language-detector.d.ts.map +1 -1
  81. package/dist/language-detector.js +122 -43
  82. package/dist/language-detector.js.map +1 -1
  83. package/dist/llm-client.d.ts +0 -7
  84. package/dist/llm-client.d.ts.map +1 -1
  85. package/dist/llm-client.js +15 -122
  86. package/dist/llm-client.js.map +1 -1
  87. package/dist/mcp-client.js +1 -1
  88. package/dist/mcp-client.js.map +1 -1
  89. package/dist/memory.d.ts.map +1 -1
  90. package/dist/memory.js +0 -15
  91. package/dist/memory.js.map +1 -1
  92. package/dist/meta-learning-collector.d.ts +64 -0
  93. package/dist/meta-learning-collector.d.ts.map +1 -0
  94. package/dist/meta-learning-collector.js +169 -0
  95. package/dist/meta-learning-collector.js.map +1 -0
  96. package/dist/meta-learning-engine.d.ts +61 -0
  97. package/dist/meta-learning-engine.d.ts.map +1 -0
  98. package/dist/meta-learning-engine.js +250 -0
  99. package/dist/meta-learning-engine.js.map +1 -0
  100. package/dist/overhead-governor.d.ts +105 -0
  101. package/dist/overhead-governor.d.ts.map +1 -0
  102. package/dist/overhead-governor.js +239 -0
  103. package/dist/overhead-governor.js.map +1 -0
  104. package/dist/playbook-library.d.ts +75 -0
  105. package/dist/playbook-library.d.ts.map +1 -0
  106. package/dist/playbook-library.js +241 -0
  107. package/dist/playbook-library.js.map +1 -0
  108. package/dist/project-executive.d.ts +97 -0
  109. package/dist/project-executive.d.ts.map +1 -0
  110. package/dist/project-executive.js +223 -0
  111. package/dist/project-executive.js.map +1 -0
  112. package/dist/research-loop.d.ts +79 -0
  113. package/dist/research-loop.d.ts.map +1 -0
  114. package/dist/research-loop.js +363 -0
  115. package/dist/research-loop.js.map +1 -0
  116. package/dist/resolve-memory-path.d.ts +32 -0
  117. package/dist/resolve-memory-path.d.ts.map +1 -0
  118. package/dist/resolve-memory-path.js +97 -0
  119. package/dist/resolve-memory-path.js.map +1 -0
  120. package/dist/safe-bounds.d.ts +101 -0
  121. package/dist/safe-bounds.d.ts.map +1 -0
  122. package/dist/safe-bounds.js +140 -0
  123. package/dist/safe-bounds.js.map +1 -0
  124. package/dist/sandbox-tiers.d.ts +5 -0
  125. package/dist/sandbox-tiers.d.ts.map +1 -1
  126. package/dist/sandbox-tiers.js +14 -6
  127. package/dist/sandbox-tiers.js.map +1 -1
  128. package/dist/security.d.ts.map +1 -1
  129. package/dist/security.js +3 -0
  130. package/dist/security.js.map +1 -1
  131. package/dist/self-improvement-loop.d.ts +64 -0
  132. package/dist/self-improvement-loop.d.ts.map +1 -0
  133. package/dist/self-improvement-loop.js +156 -0
  134. package/dist/self-improvement-loop.js.map +1 -0
  135. package/dist/session-persistence.d.ts +5 -0
  136. package/dist/session-persistence.d.ts.map +1 -1
  137. package/dist/session-persistence.js +19 -3
  138. package/dist/session-persistence.js.map +1 -1
  139. package/dist/skill-loader.d.ts +16 -9
  140. package/dist/skill-loader.d.ts.map +1 -1
  141. package/dist/skill-loader.js +52 -116
  142. package/dist/skill-loader.js.map +1 -1
  143. package/dist/skill-registry.d.ts +60 -0
  144. package/dist/skill-registry.d.ts.map +1 -0
  145. package/dist/skill-registry.js +162 -0
  146. package/dist/skill-registry.js.map +1 -0
  147. package/dist/stall-detector.d.ts +56 -0
  148. package/dist/stall-detector.d.ts.map +1 -0
  149. package/dist/stall-detector.js +142 -0
  150. package/dist/stall-detector.js.map +1 -0
  151. package/dist/strategy-learner.d.ts +57 -0
  152. package/dist/strategy-learner.d.ts.map +1 -0
  153. package/dist/strategy-learner.js +160 -0
  154. package/dist/strategy-learner.js.map +1 -0
  155. package/dist/strategy-market.d.ts +73 -0
  156. package/dist/strategy-market.d.ts.map +1 -0
  157. package/dist/strategy-market.js +200 -0
  158. package/dist/strategy-market.js.map +1 -0
  159. package/dist/sub-agent.d.ts +0 -3
  160. package/dist/sub-agent.d.ts.map +1 -1
  161. package/dist/sub-agent.js +0 -10
  162. package/dist/sub-agent.js.map +1 -1
  163. package/dist/system-prompt.d.ts +0 -2
  164. package/dist/system-prompt.d.ts.map +1 -1
  165. package/dist/system-prompt.js +97 -490
  166. package/dist/system-prompt.js.map +1 -1
  167. package/dist/task-classifier.d.ts.map +1 -1
  168. package/dist/task-classifier.js +2 -54
  169. package/dist/task-classifier.js.map +1 -1
  170. package/dist/tool-synthesizer.d.ts +149 -0
  171. package/dist/tool-synthesizer.d.ts.map +1 -0
  172. package/dist/tool-synthesizer.js +455 -0
  173. package/dist/tool-synthesizer.js.map +1 -0
  174. package/dist/trace-pattern-extractor.d.ts +76 -0
  175. package/dist/trace-pattern-extractor.d.ts.map +1 -0
  176. package/dist/trace-pattern-extractor.js +321 -0
  177. package/dist/trace-pattern-extractor.js.map +1 -0
  178. package/dist/trace-recorder.d.ts +38 -0
  179. package/dist/trace-recorder.d.ts.map +1 -0
  180. package/dist/trace-recorder.js +94 -0
  181. package/dist/trace-recorder.js.map +1 -0
  182. package/dist/trust-economics.d.ts +50 -0
  183. package/dist/trust-economics.d.ts.map +1 -0
  184. package/dist/trust-economics.js +148 -0
  185. package/dist/trust-economics.js.map +1 -0
  186. package/dist/types.d.ts +272 -3
  187. package/dist/types.d.ts.map +1 -1
  188. package/dist/types.js.map +1 -1
  189. package/dist/yuan-md-loader.d.ts +22 -0
  190. package/dist/yuan-md-loader.d.ts.map +1 -0
  191. package/dist/yuan-md-loader.js +75 -0
  192. package/dist/yuan-md-loader.js.map +1 -0
  193. package/package.json +1 -1
@@ -0,0 +1,397 @@
1
+ /**
2
+ * @module capability-graph
3
+ * @description Capability Graph — represents tools, skills, playbooks, agents, and evidence
4
+ * as interconnected graph nodes. Enables skill composition, smarter tool selection, and
5
+ * reasoning about what the agent can/cannot do.
6
+ *
7
+ * Storage: ~/.yuan/capability-graph.json
8
+ * Atomic writes (.tmp + renameSync).
9
+ */
10
+ import { EventEmitter } from "node:events";
11
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
12
+ import { homedir } from "node:os";
13
+ import { join } from "node:path";
14
+ // ─── Default Nodes ───
15
+ function buildDefaultNodes() {
16
+ const now = new Date().toISOString();
17
+ const toolNodes = [
18
+ {
19
+ id: "tool:read_file",
20
+ type: "tool",
21
+ name: "read_file",
22
+ description: "Read the contents of a file from the filesystem",
23
+ successRate: 0.95,
24
+ usageCount: 0,
25
+ tags: ["io", "read", "file"],
26
+ metadata: {},
27
+ addedAt: now,
28
+ },
29
+ {
30
+ id: "tool:write_file",
31
+ type: "tool",
32
+ name: "write_file",
33
+ description: "Write or overwrite a file on the filesystem",
34
+ successRate: 0.9,
35
+ usageCount: 0,
36
+ tags: ["io", "write", "file"],
37
+ metadata: {},
38
+ addedAt: now,
39
+ },
40
+ {
41
+ id: "tool:shell_exec",
42
+ type: "tool",
43
+ name: "shell_exec",
44
+ description: "Execute a shell command in a sandboxed environment",
45
+ successRate: 0.85,
46
+ usageCount: 0,
47
+ tags: ["shell", "exec", "command"],
48
+ metadata: {},
49
+ addedAt: now,
50
+ },
51
+ {
52
+ id: "tool:grep",
53
+ type: "tool",
54
+ name: "grep",
55
+ description: "Search file contents using regular expressions",
56
+ successRate: 0.95,
57
+ usageCount: 0,
58
+ tags: ["search", "grep", "regex", "file"],
59
+ metadata: {},
60
+ addedAt: now,
61
+ },
62
+ {
63
+ id: "tool:glob",
64
+ type: "tool",
65
+ name: "glob",
66
+ description: "Find files matching a glob pattern",
67
+ successRate: 0.95,
68
+ usageCount: 0,
69
+ tags: ["search", "glob", "file"],
70
+ metadata: {},
71
+ addedAt: now,
72
+ },
73
+ {
74
+ id: "tool:git_ops",
75
+ type: "tool",
76
+ name: "git_ops",
77
+ description: "Perform git operations (status, diff, commit, log)",
78
+ successRate: 0.88,
79
+ usageCount: 0,
80
+ tags: ["git", "vcs", "diff"],
81
+ metadata: {},
82
+ addedAt: now,
83
+ },
84
+ ];
85
+ const skillNodes = [
86
+ {
87
+ id: "skill:ts_bugfix",
88
+ type: "skill",
89
+ name: "ts_bugfix",
90
+ description: "Fix TypeScript compilation errors and type mismatches",
91
+ successRate: 0.75,
92
+ usageCount: 0,
93
+ tags: ["typescript", "bugfix", "ts-bugfix"],
94
+ metadata: {},
95
+ addedAt: now,
96
+ },
97
+ {
98
+ id: "skill:refactor",
99
+ type: "skill",
100
+ name: "refactor",
101
+ description: "Restructure code without changing observable behaviour",
102
+ successRate: 0.8,
103
+ usageCount: 0,
104
+ tags: ["refactor", "clean", "restructure"],
105
+ metadata: {},
106
+ addedAt: now,
107
+ },
108
+ {
109
+ id: "skill:feature_add",
110
+ type: "skill",
111
+ name: "feature_add",
112
+ description: "Implement a new feature end-to-end",
113
+ successRate: 0.7,
114
+ usageCount: 0,
115
+ tags: ["feature-add", "implement", "add"],
116
+ metadata: {},
117
+ addedAt: now,
118
+ },
119
+ ];
120
+ return [...toolNodes, ...skillNodes];
121
+ }
122
+ function buildDefaultEdges() {
123
+ const now = new Date().toISOString();
124
+ return [
125
+ // ts_bugfix uses: read_file, grep, shell_exec (tsc)
126
+ { fromId: "skill:ts_bugfix", toId: "tool:read_file", type: "uses", weight: 0.9, addedAt: now },
127
+ { fromId: "skill:ts_bugfix", toId: "tool:grep", type: "uses", weight: 0.8, addedAt: now },
128
+ { fromId: "skill:ts_bugfix", toId: "tool:shell_exec", type: "uses", weight: 0.7, addedAt: now },
129
+ // refactor uses: read_file, grep, write_file
130
+ { fromId: "skill:refactor", toId: "tool:read_file", type: "uses", weight: 0.9, addedAt: now },
131
+ { fromId: "skill:refactor", toId: "tool:grep", type: "uses", weight: 0.8, addedAt: now },
132
+ { fromId: "skill:refactor", toId: "tool:write_file", type: "uses", weight: 0.85, addedAt: now },
133
+ // feature_add uses: read_file, write_file, grep, glob, shell_exec
134
+ { fromId: "skill:feature_add", toId: "tool:read_file", type: "uses", weight: 0.9, addedAt: now },
135
+ { fromId: "skill:feature_add", toId: "tool:write_file", type: "uses", weight: 0.9, addedAt: now },
136
+ { fromId: "skill:feature_add", toId: "tool:grep", type: "uses", weight: 0.7, addedAt: now },
137
+ { fromId: "skill:feature_add", toId: "tool:glob", type: "uses", weight: 0.6, addedAt: now },
138
+ { fromId: "skill:feature_add", toId: "tool:shell_exec", type: "uses", weight: 0.75, addedAt: now },
139
+ ];
140
+ }
141
+ // ─── CapabilityGraph ───
142
+ export class CapabilityGraph extends EventEmitter {
143
+ storageFile;
144
+ storageDir;
145
+ nodes = new Map();
146
+ // Adjacency list keyed by fromId
147
+ edges = new Map();
148
+ constructor(storageDir) {
149
+ super();
150
+ this.storageDir =
151
+ storageDir ?? join(homedir(), ".yuan");
152
+ this.storageFile = join(this.storageDir, "capability-graph.json");
153
+ this._load();
154
+ }
155
+ // ─── Public API ───
156
+ /**
157
+ * Add or update a node. Emits agent:capability_graph_updated.
158
+ */
159
+ upsertNode(node) {
160
+ const existing = this.nodes.get(node.id);
161
+ const action = existing ? "updated" : "added";
162
+ const resolved = {
163
+ ...node,
164
+ addedAt: node.addedAt ?? new Date().toISOString(),
165
+ };
166
+ this.nodes.set(node.id, resolved);
167
+ this._save();
168
+ this.emit("event", {
169
+ kind: "agent:capability_graph_updated",
170
+ nodeId: node.id,
171
+ action,
172
+ nodeType: node.type,
173
+ timestamp: Date.now(),
174
+ });
175
+ }
176
+ /**
177
+ * Add a directed edge between two nodes.
178
+ * If the edge (fromId, toId, type) already exists, it is replaced.
179
+ */
180
+ addEdge(fromId, toId, type, weight = 0.5) {
181
+ const edge = {
182
+ fromId,
183
+ toId,
184
+ type,
185
+ weight: Math.max(0, Math.min(1, weight)),
186
+ addedAt: new Date().toISOString(),
187
+ };
188
+ const list = this.edges.get(fromId) ?? [];
189
+ // Replace if same (fromId, toId, type) triple already exists
190
+ const idx = list.findIndex((e) => e.toId === toId && e.type === type);
191
+ if (idx >= 0) {
192
+ list[idx] = edge;
193
+ }
194
+ else {
195
+ list.push(edge);
196
+ }
197
+ this.edges.set(fromId, list);
198
+ this._save();
199
+ }
200
+ /**
201
+ * Find nodes matching the given filter criteria.
202
+ */
203
+ findNodes(filter) {
204
+ const results = [];
205
+ for (const node of this.nodes.values()) {
206
+ if (filter.type !== undefined && node.type !== filter.type)
207
+ continue;
208
+ if (filter.minSuccessRate !== undefined &&
209
+ node.successRate < filter.minSuccessRate)
210
+ continue;
211
+ if (filter.tags && filter.tags.length > 0) {
212
+ const hasAllTags = filter.tags.every((t) => node.tags.includes(t));
213
+ if (!hasAllTags)
214
+ continue;
215
+ }
216
+ results.push(node);
217
+ }
218
+ return results;
219
+ }
220
+ /**
221
+ * Get all edges originating from a node, optionally filtered by edge type.
222
+ */
223
+ getEdges(nodeId, edgeType) {
224
+ const list = this.edges.get(nodeId) ?? [];
225
+ if (edgeType === undefined)
226
+ return [...list];
227
+ return list.filter((e) => e.type === edgeType);
228
+ }
229
+ /**
230
+ * BFS path search between two nodes. Max depth 5. Returns shortest path by hop count.
231
+ */
232
+ findPath(fromId, toId) {
233
+ if (!this.nodes.has(fromId) || !this.nodes.has(toId))
234
+ return null;
235
+ if (fromId === toId) {
236
+ const node = this.nodes.get(fromId);
237
+ return { nodes: [node], edges: [], totalWeight: 1 };
238
+ }
239
+ const MAX_DEPTH = 5;
240
+ const queue = [
241
+ { nodeId: fromId, pathNodeIds: [fromId], pathEdges: [] },
242
+ ];
243
+ const visited = new Set([fromId]);
244
+ while (queue.length > 0) {
245
+ const current = queue.shift();
246
+ if (current.pathNodeIds.length > MAX_DEPTH)
247
+ continue;
248
+ const outEdges = this.edges.get(current.nodeId) ?? [];
249
+ for (const edge of outEdges) {
250
+ if (visited.has(edge.toId))
251
+ continue;
252
+ const newPathNodeIds = [...current.pathNodeIds, edge.toId];
253
+ const newPathEdges = [...current.pathEdges, edge];
254
+ if (edge.toId === toId) {
255
+ // Found! Build result
256
+ const nodes = newPathNodeIds.map((id) => this.nodes.get(id)).filter(Boolean);
257
+ const totalWeight = newPathEdges.length > 0
258
+ ? newPathEdges.reduce((sum, e) => sum + e.weight, 0) /
259
+ newPathEdges.length
260
+ : 0;
261
+ return { nodes, edges: newPathEdges, totalWeight };
262
+ }
263
+ visited.add(edge.toId);
264
+ queue.push({
265
+ nodeId: edge.toId,
266
+ pathNodeIds: newPathNodeIds,
267
+ pathEdges: newPathEdges,
268
+ });
269
+ }
270
+ }
271
+ return null;
272
+ }
273
+ /**
274
+ * Get composed capabilities: all tool/skill nodes reachable from nodes
275
+ * tagged with taskType via "uses" or "composed_of" edges (max depth 3).
276
+ */
277
+ getComposition(taskType) {
278
+ // Find seed nodes tagged with taskType
279
+ const seeds = this.findNodes({ tags: [taskType] });
280
+ const result = new Map();
281
+ const seen = new Set();
282
+ const traverse = (nodeId, depth) => {
283
+ if (depth > 3 || seen.has(nodeId))
284
+ return;
285
+ seen.add(nodeId);
286
+ const node = this.nodes.get(nodeId);
287
+ if (node)
288
+ result.set(nodeId, node);
289
+ const outEdges = this.edges.get(nodeId) ?? [];
290
+ for (const edge of outEdges) {
291
+ if (edge.type === "uses" || edge.type === "composed_of") {
292
+ traverse(edge.toId, depth + 1);
293
+ }
294
+ }
295
+ };
296
+ for (const seed of seeds) {
297
+ traverse(seed.id, 0);
298
+ }
299
+ return Array.from(result.values());
300
+ }
301
+ /**
302
+ * Update success rate for a node using exponential moving average.
303
+ * newRate = (oldRate * (n-1) + (success ? 1 : 0)) / n
304
+ */
305
+ recordOutcome(nodeId, success) {
306
+ const node = this.nodes.get(nodeId);
307
+ if (!node)
308
+ return;
309
+ const n = node.usageCount + 1;
310
+ node.successRate = (node.successRate * (n - 1) + (success ? 1 : 0)) / n;
311
+ node.usageCount = n;
312
+ this._save();
313
+ }
314
+ /**
315
+ * Export the full graph as a plain JSON object.
316
+ */
317
+ export() {
318
+ const nodes = Array.from(this.nodes.values());
319
+ const edges = [];
320
+ for (const list of this.edges.values()) {
321
+ edges.push(...list);
322
+ }
323
+ return { nodes, edges };
324
+ }
325
+ /**
326
+ * Import skills from SkillRegistry as graph nodes.
327
+ * Each skill is added as a "skill" type node; existing nodes are updated.
328
+ */
329
+ importFromSkillRegistry(skills) {
330
+ for (const skill of skills) {
331
+ this.upsertNode({
332
+ id: `skill:${skill.id}`,
333
+ type: "skill",
334
+ name: skill.name,
335
+ description: skill.pattern,
336
+ successRate: skill.successRate,
337
+ usageCount: skill.usageCount,
338
+ tags: [skill.taskType],
339
+ metadata: { sourceId: skill.id },
340
+ });
341
+ }
342
+ }
343
+ // ─── Internal ───
344
+ _load() {
345
+ try {
346
+ if (!existsSync(this.storageDir)) {
347
+ mkdirSync(this.storageDir, { recursive: true });
348
+ }
349
+ if (!existsSync(this.storageFile)) {
350
+ // First init: pre-populate defaults
351
+ this._populateDefaults();
352
+ this._save();
353
+ return;
354
+ }
355
+ const raw = readFileSync(this.storageFile, "utf-8");
356
+ const data = JSON.parse(raw);
357
+ for (const node of data.nodes ?? []) {
358
+ this.nodes.set(node.id, node);
359
+ }
360
+ for (const edge of data.edges ?? []) {
361
+ const list = this.edges.get(edge.fromId) ?? [];
362
+ list.push(edge);
363
+ this.edges.set(edge.fromId, list);
364
+ }
365
+ }
366
+ catch {
367
+ // Non-fatal: start with defaults on corrupt/missing file
368
+ this._populateDefaults();
369
+ this._save();
370
+ }
371
+ }
372
+ _populateDefaults() {
373
+ for (const node of buildDefaultNodes()) {
374
+ this.nodes.set(node.id, node);
375
+ }
376
+ for (const edge of buildDefaultEdges()) {
377
+ const list = this.edges.get(edge.fromId) ?? [];
378
+ list.push(edge);
379
+ this.edges.set(edge.fromId, list);
380
+ }
381
+ }
382
+ _save() {
383
+ const tmpFile = `${this.storageFile}.tmp`;
384
+ try {
385
+ if (!existsSync(this.storageDir)) {
386
+ mkdirSync(this.storageDir, { recursive: true });
387
+ }
388
+ const data = this.export();
389
+ writeFileSync(tmpFile, JSON.stringify(data, null, 2), "utf-8");
390
+ renameSync(tmpFile, this.storageFile);
391
+ }
392
+ catch {
393
+ // Non-fatal: storage failures should not crash the agent loop
394
+ }
395
+ }
396
+ }
397
+ //# sourceMappingURL=capability-graph.js.map
@@ -0,0 +1 @@
1
+ {"version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,KAAK,MAAM,IAAI,IAAI,iBAAiB,EAAE,EAAE,CAAC;YACvC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QAChC,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,iBAAiB,EAAE,EAAE,CAAC;YACvC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YAC/C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAChB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IAEO,KAAK;QACX,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,WAAW,MAAM,CAAC;QAC1C,IAAI,CAAC;YACH,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;gBACjC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAClD,CAAC;YACD,MAAM,IAAI,GAAc,IAAI,CAAC,MAAM,EAAE,CAAC;YACtC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;YAC/D,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QACxC,CAAC;QAAC,MAAM,CAAC;YACP,8DAA8D;QAChE,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @module capability-self-model
3
+ * @description Capability-Aware Self Model — tracks tool availability, environment
4
+ * strengths/weaknesses, and generates self-assessments for planning.
5
+ *
6
+ * Reduces overconfidence and improves plan quality by surfacing:
7
+ * - Which tools are available and which require approval
8
+ * - Which environment+taskType combinations have low success rates
9
+ * - Human-readable planning constraints derived from recorded outcomes
10
+ *
11
+ * Storage: ~/.yuan/self-model/capability-state.json
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const model = new CapabilitySelfModel();
16
+ *
17
+ * // Record outcomes over time
18
+ * model.recordOutcome("typescript", "bugfix", true);
19
+ * model.recordOutcome("python", "refactor", false);
20
+ *
21
+ * // Check tool availability before planning
22
+ * const check = model.canUse("shell_exec");
23
+ * // { allowed: true, requiresApproval: true }
24
+ *
25
+ * // Generate self-assessment at planning time
26
+ * const assessment = model.assess();
27
+ * // assessment.planningConstraints → ["Weak at Python refactor (40% success)", ...]
28
+ * // assessment.overallConfidence → 0.67
29
+ * ```
30
+ */
31
+ import { EventEmitter } from "node:events";
32
+ /** Tracks whether a specific tool is available and what approval it requires. */
33
+ export interface ToolAvailability {
34
+ toolName: string;
35
+ isAvailable: boolean;
36
+ requiresApproval: boolean;
37
+ isReadOnly: boolean;
38
+ lastChecked: string;
39
+ }
40
+ /**
41
+ * Tracks the agent's success rate in a specific environment+taskType combination.
42
+ *
43
+ * Rating thresholds (Laplace-smoothed success rate):
44
+ * - >= 0.75 → "strong"
45
+ * - >= 0.50 → "adequate"
46
+ * - >= 0.25 → "weak"
47
+ * - sampleCount < 3 → "unknown"
48
+ */
49
+ export interface EnvironmentStrength {
50
+ /** e.g. "typescript", "nextjs", "python", "rust" */
51
+ environment: string;
52
+ /** e.g. "bugfix", "refactor", "feature", "test" */
53
+ taskType: string;
54
+ /** Laplace-smoothed success rate (0..1) */
55
+ successRate: number;
56
+ sampleCount: number;
57
+ rating: "strong" | "adequate" | "weak" | "unknown";
58
+ }
59
+ /**
60
+ * Full self-assessment snapshot generated at planning time.
61
+ */
62
+ export interface SelfAssessment {
63
+ availableTools: ToolAvailability[];
64
+ strengths: EnvironmentStrength[];
65
+ weaknesses: EnvironmentStrength[];
66
+ /** Human-readable list of what the agent should be cautious about. */
67
+ planningConstraints: string[];
68
+ /** Weighted average of environment success rates (0..1). */
69
+ overallConfidence: number;
70
+ generatedAt: string;
71
+ }
72
+ /**
73
+ * Capability-Aware Self Model.
74
+ *
75
+ * Maintains an in-memory + persisted record of:
76
+ * 1. Tool availability (registered via `registerTool`)
77
+ * 2. Environment+taskType success rates (updated via `recordOutcome`)
78
+ *
79
+ * Emits `agent:self_model_updated` whenever state changes.
80
+ */
81
+ export declare class CapabilitySelfModel extends EventEmitter {
82
+ private readonly storageFile;
83
+ private tools;
84
+ private environments;
85
+ constructor(storageDir?: string);
86
+ /**
87
+ * Register (or update) a tool's availability metadata.
88
+ *
89
+ * @param toolName - Unique tool identifier.
90
+ * @param opts - Approval/read-only/availability flags.
91
+ */
92
+ registerTool(toolName: string, opts: {
93
+ requiresApproval: boolean;
94
+ isReadOnly: boolean;
95
+ isAvailable?: boolean;
96
+ }): void;
97
+ /**
98
+ * Record a success or failure for a specific environment+taskType combination.
99
+ * Updates the Laplace-smoothed success rate and re-derives the rating.
100
+ *
101
+ * @param environment - e.g. "typescript", "python"
102
+ * @param taskType - e.g. "bugfix", "refactor"
103
+ * @param success - Whether the task succeeded.
104
+ */
105
+ recordOutcome(environment: string, taskType: string, success: boolean): void;
106
+ /**
107
+ * Generate a self-assessment snapshot.
108
+ * Intended to be called at planning time to inform plan quality.
109
+ */
110
+ assess(): SelfAssessment;
111
+ /**
112
+ * Check whether a specific tool can be used.
113
+ *
114
+ * @param toolName - Tool to check.
115
+ * @returns `{ allowed, requiresApproval, reason? }`
116
+ */
117
+ canUse(toolName: string): {
118
+ allowed: boolean;
119
+ requiresApproval: boolean;
120
+ reason?: string;
121
+ };
122
+ /**
123
+ * Return weak and unknown (with sufficient samples) environment+taskType areas.
124
+ * Use this to add caution to planning prompts.
125
+ */
126
+ getWeakAreas(): EnvironmentStrength[];
127
+ /** Upsert a tool into the in-memory map (does NOT save or emit). */
128
+ private _upsertTool;
129
+ /**
130
+ * Build human-readable planning constraints from tool and environment state.
131
+ */
132
+ private _buildConstraints;
133
+ /**
134
+ * Compute overall confidence as a weighted average of environment success rates.
135
+ * Weights are proportional to sample counts.
136
+ * Returns 0.5 if no environment data is available.
137
+ */
138
+ private _computeConfidence;
139
+ /** Load persisted state from disk. Non-fatal on any error. */
140
+ private _load;
141
+ /** Atomically persist current state to disk. Non-fatal on any error. */
142
+ private _save;
143
+ }
144
+ //# sourceMappingURL=capability-self-model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capability-self-model.d.ts","sourceRoot":"","sources":["../src/capability-self-model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAa3C,iFAAiF;AACjF,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;IACrB,gBAAgB,EAAE,OAAO,CAAC;IAC1B,UAAU,EAAE,OAAO,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,mBAAmB;IAClC,oDAAoD;IACpD,WAAW,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,QAAQ,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,QAAQ,GAAG,UAAU,GAAG,MAAM,GAAG,SAAS,CAAC;CACpD;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,cAAc,EAAE,gBAAgB,EAAE,CAAC;IACnC,SAAS,EAAE,mBAAmB,EAAE,CAAC;IACjC,UAAU,EAAE,mBAAmB,EAAE,CAAC;IAClC,sEAAsE;IACtE,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,4DAA4D;IAC5D,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,MAAM,CAAC;CACrB;AAyDD;;;;;;;;GAQG;AACH,qBAAa,mBAAoB,SAAQ,YAA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