@aaac/runtime 0.1.55 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -11
- package/dist/adapters/adk-sdk.d.ts +3 -3
- package/dist/adapters/adk-sdk.js +6 -8
- package/dist/adapters/adk-sdk.js.map +1 -1
- package/dist/adapters/claude-agent-sdk.d.ts +3 -3
- package/dist/adapters/claude-agent-sdk.js +22 -23
- package/dist/adapters/claude-agent-sdk.js.map +1 -1
- package/dist/adapters/mock.d.ts +2 -3
- package/dist/adapters/openai-agents-sdk.d.ts +3 -3
- package/dist/adapters/openai-agents-sdk.js +6 -5
- package/dist/adapters/openai-agents-sdk.js.map +1 -1
- package/dist/chunk-2INQL5TG.js +298 -0
- package/dist/chunk-2INQL5TG.js.map +1 -0
- package/dist/chunk-3CLWG72V.js +47 -0
- package/dist/chunk-3CLWG72V.js.map +1 -0
- package/dist/chunk-3DDB34BU.js +237 -0
- package/dist/chunk-3DDB34BU.js.map +1 -0
- package/dist/chunk-ABXDSTUV.js +685 -0
- package/dist/chunk-ABXDSTUV.js.map +1 -0
- package/dist/{chunk-3H3PVPPF.js → chunk-F5R46S7I.js} +151 -204
- package/dist/chunk-F5R46S7I.js.map +1 -0
- package/dist/chunk-FKAI54OM.js +234 -0
- package/dist/chunk-FKAI54OM.js.map +1 -0
- package/dist/chunk-KTVMMG33.js +1111 -0
- package/dist/chunk-KTVMMG33.js.map +1 -0
- package/dist/chunk-MSVIEEGR.js +269 -0
- package/dist/chunk-MSVIEEGR.js.map +1 -0
- package/dist/chunk-VJXGCWSZ.js +36 -0
- package/dist/chunk-VJXGCWSZ.js.map +1 -0
- package/dist/claude-bundles/full.js +311 -309
- package/dist/claude-bundles/lean.js +6 -6
- package/dist/claude-bundles/prompt.js +17 -0
- package/dist/generator/index.d.ts +2 -2
- package/dist/generator/index.js +8 -7
- package/dist/guardrail-hooks-CB23nhZV.d.ts +202 -0
- package/dist/hook-protocol-CFnNudiV.d.ts +96 -0
- package/dist/hooks-lite-CoWWnCRH.d.ts +428 -0
- package/dist/index-BlSepCSZ.d.ts +296 -0
- package/dist/index.d.ts +785 -105
- package/dist/index.js +2189 -634
- package/dist/index.js.map +1 -1
- package/dist/lib/claude-native/hooks-lite.d.ts +3 -26
- package/dist/lib/claude-native/hooks-lite.js +18 -36
- package/dist/lib/claude-native/hooks-prompt.d.ts +5 -0
- package/dist/lib/claude-native/hooks-prompt.js +33 -0
- package/dist/lib/claude-native/hooks-prompt.js.map +1 -0
- package/dist/lib/claude-native/index.d.ts +56 -214
- package/dist/lib/claude-native/index.js +79 -280
- package/dist/lib/claude-native/index.js.map +1 -1
- package/dist/{navigation-context-D_E1NIUf.d.ts → navigation-context-B-RpUg3_.d.ts} +1 -1
- package/dist/navigation-index-prompt-CbrS5laO.d.ts +43 -0
- package/dist/{task-runner-DfIEorkU.d.ts → task-runner-GWoI7_71.d.ts} +100 -493
- package/dist/workspace-user-prompt-submit-D11Iu0zO.d.ts +269 -0
- package/package.json +15 -6
- package/dist/chunk-3H3PVPPF.js.map +0 -1
- package/dist/chunk-DBM5XN67.js +0 -354
- package/dist/chunk-DBM5XN67.js.map +0 -1
- package/dist/chunk-KXGUWBGK.js +0 -370
- package/dist/chunk-KXGUWBGK.js.map +0 -1
- package/dist/chunk-LDCQZPDG.js +0 -11
- package/dist/chunk-LDCQZPDG.js.map +0 -1
- package/dist/chunk-N4KNGDYY.js +0 -1963
- package/dist/chunk-N4KNGDYY.js.map +0 -1
- package/dist/chunk-NMX4C6LC.js +0 -332
- package/dist/chunk-NMX4C6LC.js.map +0 -1
- package/dist/chunk-QVH5KKBR.js +0 -374
- package/dist/chunk-QVH5KKBR.js.map +0 -1
- package/dist/context-DKkamYX_.d.ts +0 -31
- package/dist/guardrail-hooks-slK-z0Dz.d.ts +0 -83
- package/dist/index-LqEvaRk-.d.ts +0 -299
- package/dist/workspace-pre-tool-use-C6nidc0q.d.ts +0 -333
|
@@ -0,0 +1,1111 @@
|
|
|
1
|
+
// src/lib/analyzer-bridge.ts
|
|
2
|
+
var ANALYZER_PKG = "artifact-contracts-analyzer";
|
|
3
|
+
async function loadAnalyzerModule() {
|
|
4
|
+
const mod = await import(
|
|
5
|
+
/* webpackIgnore: true */
|
|
6
|
+
ANALYZER_PKG
|
|
7
|
+
);
|
|
8
|
+
return mod;
|
|
9
|
+
}
|
|
10
|
+
function createAnalyzerBridge(options) {
|
|
11
|
+
const { projectRoot, navigationIndex, artifactPatterns, manifests } = options;
|
|
12
|
+
let cachedGraph = null;
|
|
13
|
+
let cachedFileContents = null;
|
|
14
|
+
let graphBuildPromise = null;
|
|
15
|
+
async function ensureGraph() {
|
|
16
|
+
if (cachedGraph) return cachedGraph;
|
|
17
|
+
if (graphBuildPromise) {
|
|
18
|
+
await graphBuildPromise;
|
|
19
|
+
return cachedGraph;
|
|
20
|
+
}
|
|
21
|
+
graphBuildPromise = doBuildGraph();
|
|
22
|
+
await graphBuildPromise;
|
|
23
|
+
return cachedGraph;
|
|
24
|
+
}
|
|
25
|
+
async function doBuildGraph() {
|
|
26
|
+
let mod;
|
|
27
|
+
try {
|
|
28
|
+
mod = await loadAnalyzerModule();
|
|
29
|
+
} catch (error) {
|
|
30
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
31
|
+
throw new Error(
|
|
32
|
+
`artifact-contracts-analyzer is not available. Install it to use analyzer features: ${msg}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
const patterns = artifactPatterns ?? /* @__PURE__ */ new Map();
|
|
36
|
+
const registry = mod.createDefaultRegistry();
|
|
37
|
+
const astResult = await mod.buildAstGraph({
|
|
38
|
+
projectRoot,
|
|
39
|
+
artifactPatterns: patterns,
|
|
40
|
+
registry
|
|
41
|
+
});
|
|
42
|
+
cachedFileContents = astResult.fileContents;
|
|
43
|
+
cachedGraph = await mod.buildDependencyGraph({
|
|
44
|
+
ast: astResult.graph,
|
|
45
|
+
projectRoot,
|
|
46
|
+
artifactPatterns: patterns,
|
|
47
|
+
manifests: manifests ?? []
|
|
48
|
+
});
|
|
49
|
+
if (typeof mod.loadExternalProviders === "function" && typeof mod.integrateExternalInsights === "function") {
|
|
50
|
+
try {
|
|
51
|
+
const loadProviders = mod.loadExternalProviders;
|
|
52
|
+
const integrate = mod.integrateExternalInsights;
|
|
53
|
+
const providers = await loadProviders(projectRoot);
|
|
54
|
+
if (providers.length > 0) {
|
|
55
|
+
const integration = await integrate(
|
|
56
|
+
cachedGraph,
|
|
57
|
+
providers,
|
|
58
|
+
{ projectRoot }
|
|
59
|
+
);
|
|
60
|
+
cachedGraph = integration.graph;
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const bridge = {
|
|
67
|
+
async buildGraph() {
|
|
68
|
+
await ensureGraph();
|
|
69
|
+
},
|
|
70
|
+
async getGraph() {
|
|
71
|
+
return ensureGraph();
|
|
72
|
+
},
|
|
73
|
+
async analyzeImpact(changed, opts) {
|
|
74
|
+
let mod;
|
|
75
|
+
try {
|
|
76
|
+
mod = await loadAnalyzerModule();
|
|
77
|
+
} catch {
|
|
78
|
+
return {
|
|
79
|
+
error: "artifact-contracts-analyzer is not installed. Install it as a peer dependency to use impact analysis.",
|
|
80
|
+
changed
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const graph = await ensureGraph();
|
|
84
|
+
return mod.analyzeImpact({
|
|
85
|
+
graph,
|
|
86
|
+
changed,
|
|
87
|
+
direction: opts?.direction ?? "downstream",
|
|
88
|
+
...opts?.maxDepth !== void 0 ? { maxDepth: opts.maxDepth } : {},
|
|
89
|
+
...opts?.limit !== void 0 ? { limit: opts.limit } : {},
|
|
90
|
+
...navigationIndex ? { navigationIndex } : {}
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
async generateContextPack(changed, opts) {
|
|
94
|
+
let mod;
|
|
95
|
+
try {
|
|
96
|
+
mod = await loadAnalyzerModule();
|
|
97
|
+
} catch {
|
|
98
|
+
return {
|
|
99
|
+
entries: [],
|
|
100
|
+
metadata: {
|
|
101
|
+
totalTokens: 0,
|
|
102
|
+
budgetTokens: opts?.budgetTokens ?? 5e4,
|
|
103
|
+
filesIncluded: 0,
|
|
104
|
+
filesExcluded: 0,
|
|
105
|
+
filesReferenced: 0,
|
|
106
|
+
artifactsCovered: [],
|
|
107
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const graph = await ensureGraph();
|
|
112
|
+
return mod.generateContextPack({
|
|
113
|
+
graph,
|
|
114
|
+
changed,
|
|
115
|
+
projectRoot,
|
|
116
|
+
...opts?.budgetTokens !== void 0 ? { budgetTokens: opts.budgetTokens } : {},
|
|
117
|
+
...opts?.granularity ? { granularity: opts.granularity } : {},
|
|
118
|
+
...navigationIndex ? { navigationIndex } : {},
|
|
119
|
+
...cachedFileContents ? { fileContents: cachedFileContents } : {}
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
async buildCallGraph(entry, opts) {
|
|
123
|
+
let mod;
|
|
124
|
+
try {
|
|
125
|
+
mod = await loadAnalyzerModule();
|
|
126
|
+
} catch {
|
|
127
|
+
return {
|
|
128
|
+
error: "artifact-contracts-analyzer is not installed. Install it as a peer dependency to use call graph analysis.",
|
|
129
|
+
entry
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const graph = await ensureGraph();
|
|
133
|
+
return mod.buildCallGraph({
|
|
134
|
+
graph,
|
|
135
|
+
entry,
|
|
136
|
+
...opts?.maxDepth !== void 0 ? { maxDepth: opts.maxDepth } : {},
|
|
137
|
+
...opts?.direction ? { direction: opts.direction } : {}
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
async getContextMap(input) {
|
|
141
|
+
let mod;
|
|
142
|
+
try {
|
|
143
|
+
mod = await loadAnalyzerModule();
|
|
144
|
+
} catch {
|
|
145
|
+
return {
|
|
146
|
+
error: "artifact-contracts-analyzer is not installed. Install it as a peer dependency to use context map analysis.",
|
|
147
|
+
instruction: input.instruction
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const graph = await ensureGraph();
|
|
151
|
+
return mod.getContextMap({
|
|
152
|
+
graph,
|
|
153
|
+
projectRoot,
|
|
154
|
+
instruction: input.instruction,
|
|
155
|
+
candidates: input.candidates,
|
|
156
|
+
...input.depth !== void 0 ? { depth: input.depth } : {},
|
|
157
|
+
...input.direction ? { direction: input.direction } : {},
|
|
158
|
+
...input.budgetTokens !== void 0 ? { budgetTokens: input.budgetTokens } : {},
|
|
159
|
+
...navigationIndex ? { navigationIndex } : {},
|
|
160
|
+
...cachedFileContents ? { fileContents: cachedFileContents } : {}
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
invalidateCache() {
|
|
164
|
+
cachedGraph = null;
|
|
165
|
+
cachedFileContents = null;
|
|
166
|
+
graphBuildPromise = null;
|
|
167
|
+
},
|
|
168
|
+
buildAnalyzerTools() {
|
|
169
|
+
return [
|
|
170
|
+
{
|
|
171
|
+
name: "analyzer.analyzeImpact",
|
|
172
|
+
description: "Analyze the impact of changed files across the dependency graph",
|
|
173
|
+
inputSchema: {
|
|
174
|
+
type: "object",
|
|
175
|
+
properties: {
|
|
176
|
+
changed: {
|
|
177
|
+
type: "array",
|
|
178
|
+
items: { type: "string" },
|
|
179
|
+
description: "File paths that changed"
|
|
180
|
+
},
|
|
181
|
+
direction: {
|
|
182
|
+
type: "string",
|
|
183
|
+
enum: ["downstream", "upstream"],
|
|
184
|
+
description: "Traversal direction. downstream = what breaks if this changes"
|
|
185
|
+
},
|
|
186
|
+
maxDepth: {
|
|
187
|
+
type: "number",
|
|
188
|
+
description: "Maximum BFS depth to explore"
|
|
189
|
+
},
|
|
190
|
+
limit: {
|
|
191
|
+
type: "number",
|
|
192
|
+
description: "Cap the number of affected entries returned"
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
required: ["changed"]
|
|
196
|
+
},
|
|
197
|
+
handler: async (args) => {
|
|
198
|
+
const changed = args.changed;
|
|
199
|
+
const result = await bridge.analyzeImpact(changed, {
|
|
200
|
+
direction: args.direction,
|
|
201
|
+
maxDepth: args.maxDepth,
|
|
202
|
+
limit: args.limit
|
|
203
|
+
});
|
|
204
|
+
return JSON.stringify(result);
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "analyzer.generateContextPack",
|
|
209
|
+
description: "Generate a token-budget-constrained context pack for changed files",
|
|
210
|
+
inputSchema: {
|
|
211
|
+
type: "object",
|
|
212
|
+
properties: {
|
|
213
|
+
changed: {
|
|
214
|
+
type: "array",
|
|
215
|
+
items: { type: "string" },
|
|
216
|
+
description: "File paths that changed"
|
|
217
|
+
},
|
|
218
|
+
budgetTokens: {
|
|
219
|
+
type: "number",
|
|
220
|
+
description: "Token budget for the context pack"
|
|
221
|
+
},
|
|
222
|
+
granularity: {
|
|
223
|
+
type: "string",
|
|
224
|
+
enum: ["file", "symbol"],
|
|
225
|
+
description: "Context granularity level"
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
required: ["changed"]
|
|
229
|
+
},
|
|
230
|
+
handler: async (args) => {
|
|
231
|
+
const changed = args.changed;
|
|
232
|
+
const result = await bridge.generateContextPack(changed, {
|
|
233
|
+
budgetTokens: args.budgetTokens,
|
|
234
|
+
granularity: args.granularity
|
|
235
|
+
});
|
|
236
|
+
return JSON.stringify(result);
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
name: "analyzer.getContextMap",
|
|
241
|
+
description: "Build a structured context map from instruction and candidate paths/artifacts/symbols",
|
|
242
|
+
inputSchema: {
|
|
243
|
+
type: "object",
|
|
244
|
+
properties: {
|
|
245
|
+
instruction: {
|
|
246
|
+
type: "string",
|
|
247
|
+
description: "Natural-language task instruction"
|
|
248
|
+
},
|
|
249
|
+
candidates: {
|
|
250
|
+
type: "array",
|
|
251
|
+
items: {
|
|
252
|
+
type: "object",
|
|
253
|
+
properties: {
|
|
254
|
+
path: { type: "string" },
|
|
255
|
+
symbolId: { type: "string" },
|
|
256
|
+
artifactId: { type: "string" }
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
description: "Candidate seeds from navigation-index (paths, symbols, artifacts)"
|
|
260
|
+
},
|
|
261
|
+
depth: {
|
|
262
|
+
type: "number",
|
|
263
|
+
description: "Traversal depth (default 2)"
|
|
264
|
+
},
|
|
265
|
+
direction: {
|
|
266
|
+
type: "string",
|
|
267
|
+
enum: ["downstream", "upstream", "both"],
|
|
268
|
+
description: "Impact traversal direction"
|
|
269
|
+
},
|
|
270
|
+
budgetTokens: {
|
|
271
|
+
type: "number",
|
|
272
|
+
description: "Token budget for suggested read targets"
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
required: ["instruction", "candidates"]
|
|
276
|
+
},
|
|
277
|
+
handler: async (args) => {
|
|
278
|
+
const result = await bridge.getContextMap({
|
|
279
|
+
instruction: args.instruction,
|
|
280
|
+
candidates: args.candidates,
|
|
281
|
+
depth: args.depth,
|
|
282
|
+
direction: args.direction,
|
|
283
|
+
budgetTokens: args.budgetTokens
|
|
284
|
+
});
|
|
285
|
+
return JSON.stringify(result);
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
name: "analyzer.buildCallGraph",
|
|
290
|
+
description: "Build a call graph rooted at a symbol",
|
|
291
|
+
inputSchema: {
|
|
292
|
+
type: "object",
|
|
293
|
+
properties: {
|
|
294
|
+
entry: {
|
|
295
|
+
type: "string",
|
|
296
|
+
description: "Entry symbolId (filePath#name) to root the call graph at"
|
|
297
|
+
},
|
|
298
|
+
maxDepth: {
|
|
299
|
+
type: "number",
|
|
300
|
+
description: "Maximum traversal depth"
|
|
301
|
+
},
|
|
302
|
+
direction: {
|
|
303
|
+
type: "string",
|
|
304
|
+
enum: ["forward", "reverse"],
|
|
305
|
+
description: "Traversal direction: forward (callees) or reverse (callers)"
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
required: ["entry"]
|
|
309
|
+
},
|
|
310
|
+
handler: async (args) => {
|
|
311
|
+
const entry = args.entry;
|
|
312
|
+
const result = await bridge.buildCallGraph(entry, {
|
|
313
|
+
maxDepth: args.maxDepth,
|
|
314
|
+
direction: args.direction
|
|
315
|
+
});
|
|
316
|
+
return JSON.stringify(result);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
];
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
return bridge;
|
|
323
|
+
}
|
|
324
|
+
function isContextPackEntry(value) {
|
|
325
|
+
if (typeof value !== "object" || value === null) return false;
|
|
326
|
+
const entry = value;
|
|
327
|
+
return typeof entry.path === "string" && (entry.artifactId === void 0 || typeof entry.artifactId === "string") && typeof entry.relevanceScore === "number" && typeof entry.contentMode === "string" && typeof entry.content === "string" && typeof entry.reason === "string";
|
|
328
|
+
}
|
|
329
|
+
function isContextPackMetadata(value) {
|
|
330
|
+
if (typeof value !== "object" || value === null) return false;
|
|
331
|
+
const meta = value;
|
|
332
|
+
return typeof meta.totalTokens === "number" && typeof meta.budgetTokens === "number" && typeof meta.filesIncluded === "number" && typeof meta.filesExcluded === "number" && typeof meta.filesReferenced === "number" && Array.isArray(meta.artifactsCovered) && meta.artifactsCovered.every((a) => typeof a === "string") && typeof meta.generatedAt === "string";
|
|
333
|
+
}
|
|
334
|
+
function isContextPack(value) {
|
|
335
|
+
if (typeof value !== "object" || value === null) return false;
|
|
336
|
+
const obj = value;
|
|
337
|
+
return Array.isArray(obj.entries) && obj.entries.every(isContextPackEntry) && isContextPackMetadata(obj.metadata) && (obj.taskId === void 0 || typeof obj.taskId === "string");
|
|
338
|
+
}
|
|
339
|
+
function renderContextPackPrompt(pack) {
|
|
340
|
+
const m = pack.metadata;
|
|
341
|
+
const sections = [];
|
|
342
|
+
sections.push("## Source Code Context");
|
|
343
|
+
sections.push(
|
|
344
|
+
`### Budget
|
|
345
|
+
- Tokens used: ${m.totalTokens} / ${m.budgetTokens}
|
|
346
|
+
- Files included: ${m.filesIncluded}, excluded: ${m.filesExcluded}, referenced: ${m.filesReferenced}`
|
|
347
|
+
);
|
|
348
|
+
if (m.artifactsCovered.length > 0) {
|
|
349
|
+
sections.push(
|
|
350
|
+
`### Affected Artifacts
|
|
351
|
+
${m.artifactsCovered.map((a) => `- ${a}`).join("\n")}`
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
if (pack.entries.length > 0) {
|
|
355
|
+
const fileBlocks = [];
|
|
356
|
+
for (const entry of pack.entries) {
|
|
357
|
+
const ext = entry.path.split(".").pop() ?? "";
|
|
358
|
+
const header = `#### ${entry.path} (relevance: ${entry.relevanceScore}, mode: ${entry.contentMode})`;
|
|
359
|
+
const reason = `> ${entry.reason}`;
|
|
360
|
+
if (entry.content) {
|
|
361
|
+
fileBlocks.push(
|
|
362
|
+
`${header}
|
|
363
|
+
${reason}
|
|
364
|
+
|
|
365
|
+
\`\`\`${ext}
|
|
366
|
+
${entry.content}
|
|
367
|
+
\`\`\``
|
|
368
|
+
);
|
|
369
|
+
} else {
|
|
370
|
+
fileBlocks.push(`${header}
|
|
371
|
+
${reason}`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
sections.push(`### Files
|
|
375
|
+
${fileBlocks.join("\n\n")}`);
|
|
376
|
+
}
|
|
377
|
+
return sections.join("\n\n");
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/lib/observability-bridge.ts
|
|
381
|
+
import { generateId } from "@aaac/observability";
|
|
382
|
+
var OBSERVABILITY_ENV_VARS = {
|
|
383
|
+
sessionId: "AAAC_SESSION_ID",
|
|
384
|
+
traceId: "AAAC_TRACE_ID",
|
|
385
|
+
runId: "AAAC_RUN_ID"
|
|
386
|
+
};
|
|
387
|
+
function buildObservabilityEnv(ids, baseEnv = process.env) {
|
|
388
|
+
const env = { ...baseEnv };
|
|
389
|
+
if (ids.sessionId) env[OBSERVABILITY_ENV_VARS.sessionId] = ids.sessionId;
|
|
390
|
+
if (ids.traceId) env[OBSERVABILITY_ENV_VARS.traceId] = ids.traceId;
|
|
391
|
+
if (ids.runId) env[OBSERVABILITY_ENV_VARS.runId] = ids.runId;
|
|
392
|
+
return env;
|
|
393
|
+
}
|
|
394
|
+
function serializeObserverEmit(emit) {
|
|
395
|
+
let queue = [];
|
|
396
|
+
let draining = false;
|
|
397
|
+
function drain() {
|
|
398
|
+
if (draining) return;
|
|
399
|
+
draining = true;
|
|
400
|
+
try {
|
|
401
|
+
while (queue.length > 0) {
|
|
402
|
+
const next = queue.shift();
|
|
403
|
+
emit(next);
|
|
404
|
+
}
|
|
405
|
+
} finally {
|
|
406
|
+
draining = false;
|
|
407
|
+
if (queue.length > 0) {
|
|
408
|
+
drain();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return (event) => {
|
|
413
|
+
queue.push(event);
|
|
414
|
+
drain();
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function createExecutionEnvelope(params = {}) {
|
|
418
|
+
const envelope = {
|
|
419
|
+
run_id: generateId(),
|
|
420
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
421
|
+
};
|
|
422
|
+
if (params.parentRunId !== void 0) envelope.parent_run_id = params.parentRunId;
|
|
423
|
+
if (params.componentId !== void 0) envelope.component_id = params.componentId;
|
|
424
|
+
if (params.fromAgent !== void 0) envelope.from_agent = params.fromAgent;
|
|
425
|
+
if (params.toAgent !== void 0) envelope.to_agent = params.toAgent;
|
|
426
|
+
if (params.contractVersion !== void 0) envelope.contract_version = params.contractVersion;
|
|
427
|
+
if (params.adapter !== void 0) envelope.adapter = params.adapter;
|
|
428
|
+
if (params.model !== void 0) envelope.model = params.model;
|
|
429
|
+
if (params.taskInstanceId !== void 0) envelope.task_instance_id = params.taskInstanceId;
|
|
430
|
+
return envelope;
|
|
431
|
+
}
|
|
432
|
+
function attrs(base) {
|
|
433
|
+
const out = {};
|
|
434
|
+
for (const [k, v] of Object.entries(base)) {
|
|
435
|
+
if (v !== void 0 && v !== "") out[k] = v;
|
|
436
|
+
}
|
|
437
|
+
return out;
|
|
438
|
+
}
|
|
439
|
+
function resolveAgentSessionId(envelope, parentSpanId) {
|
|
440
|
+
return envelope.parent_run_id ?? parentSpanId ?? envelope.run_id;
|
|
441
|
+
}
|
|
442
|
+
function tokenAttrs(meta) {
|
|
443
|
+
const out = {};
|
|
444
|
+
const tu = meta.token_usage;
|
|
445
|
+
if (tu && typeof tu === "object") {
|
|
446
|
+
const t = tu;
|
|
447
|
+
if (typeof t.input_tokens === "number") out["gen_ai.usage.input_tokens"] = t.input_tokens;
|
|
448
|
+
if (typeof t.output_tokens === "number") out["gen_ai.usage.output_tokens"] = t.output_tokens;
|
|
449
|
+
if (typeof t.total_tokens === "number") out["gen_ai.usage.total_tokens"] = t.total_tokens;
|
|
450
|
+
if (typeof t.cache_read_input_tokens === "number") out["gen_ai.usage.cache_read_input_tokens"] = t.cache_read_input_tokens;
|
|
451
|
+
if (typeof t.cache_creation_input_tokens === "number") out["gen_ai.usage.cache_creation_input_tokens"] = t.cache_creation_input_tokens;
|
|
452
|
+
}
|
|
453
|
+
if (typeof meta.cost_usd === "number") out["cost_usd"] = meta.cost_usd;
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
function emitSessionOpen(observer, envelope, workflowId) {
|
|
457
|
+
if (!observer) return void 0;
|
|
458
|
+
const spanId = envelope.run_id;
|
|
459
|
+
observer({
|
|
460
|
+
source: "aaac-runtime",
|
|
461
|
+
eventType: "agent.session",
|
|
462
|
+
lifecycle: "open",
|
|
463
|
+
spanId,
|
|
464
|
+
parentSpanId: envelope.parent_run_id,
|
|
465
|
+
attributes: attrs({
|
|
466
|
+
axis: "agent",
|
|
467
|
+
"agent.session_id": envelope.run_id,
|
|
468
|
+
"agent.run_id": envelope.run_id,
|
|
469
|
+
"workflow.id": workflowId,
|
|
470
|
+
"component.id": envelope.component_id,
|
|
471
|
+
"gen_ai.request.model": envelope.model,
|
|
472
|
+
"gen_ai.system": envelope.adapter,
|
|
473
|
+
contract_version: envelope.contract_version
|
|
474
|
+
})
|
|
475
|
+
});
|
|
476
|
+
return spanId;
|
|
477
|
+
}
|
|
478
|
+
function emitSessionClose(observer, spanId, envelope, durationMs, status, errorMessage) {
|
|
479
|
+
if (!observer) return;
|
|
480
|
+
const metaAttrs = envelope.execution_metadata ? tokenAttrs(envelope.execution_metadata) : {};
|
|
481
|
+
observer({
|
|
482
|
+
source: "aaac-runtime",
|
|
483
|
+
eventType: "agent.session",
|
|
484
|
+
lifecycle: "close",
|
|
485
|
+
spanId,
|
|
486
|
+
attributes: {
|
|
487
|
+
...attrs({
|
|
488
|
+
axis: "agent",
|
|
489
|
+
"agent.session_id": envelope.run_id,
|
|
490
|
+
"agent.session.duration_ms": durationMs,
|
|
491
|
+
"agent.status": status,
|
|
492
|
+
"agent.error": errorMessage
|
|
493
|
+
}),
|
|
494
|
+
...metaAttrs
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
function emitSubagentOpen(observer, envelope, parentSpanId, taskId, agentRole, sessionId) {
|
|
499
|
+
if (!observer) return void 0;
|
|
500
|
+
const spanId = envelope.run_id;
|
|
501
|
+
observer({
|
|
502
|
+
source: "aaac-runtime",
|
|
503
|
+
eventType: "agent.subagent",
|
|
504
|
+
lifecycle: "open",
|
|
505
|
+
spanId,
|
|
506
|
+
parentSpanId,
|
|
507
|
+
attributes: attrs({
|
|
508
|
+
axis: "agent",
|
|
509
|
+
"agent.session_id": sessionId ?? resolveAgentSessionId(envelope, parentSpanId),
|
|
510
|
+
"agent.role": agentRole,
|
|
511
|
+
"agent.kind": "task",
|
|
512
|
+
"task.id": taskId,
|
|
513
|
+
"agent.run_id": envelope.run_id,
|
|
514
|
+
"agent.parent_run_id": envelope.parent_run_id,
|
|
515
|
+
"gen_ai.request.model": envelope.model,
|
|
516
|
+
"gen_ai.system": envelope.adapter,
|
|
517
|
+
"component.id": envelope.component_id,
|
|
518
|
+
"agent.from_agent": envelope.from_agent,
|
|
519
|
+
"agent.to_agent": envelope.to_agent
|
|
520
|
+
})
|
|
521
|
+
});
|
|
522
|
+
return spanId;
|
|
523
|
+
}
|
|
524
|
+
function emitSubagentClose(observer, spanId, envelope, durationMs, status, agentRole, errorMessage, sessionId) {
|
|
525
|
+
if (!observer) return;
|
|
526
|
+
const metaAttrs = envelope.execution_metadata ? tokenAttrs(envelope.execution_metadata) : {};
|
|
527
|
+
observer({
|
|
528
|
+
source: "aaac-runtime",
|
|
529
|
+
eventType: "agent.subagent",
|
|
530
|
+
lifecycle: "close",
|
|
531
|
+
spanId,
|
|
532
|
+
attributes: {
|
|
533
|
+
...attrs({
|
|
534
|
+
axis: "agent",
|
|
535
|
+
"agent.session_id": sessionId ?? resolveAgentSessionId(envelope),
|
|
536
|
+
"agent.role": agentRole,
|
|
537
|
+
"agent.status": status,
|
|
538
|
+
"agent.duration_ms": durationMs,
|
|
539
|
+
"agent.error": errorMessage
|
|
540
|
+
}),
|
|
541
|
+
...metaAttrs
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
function emitIterationOpen(observer, parentSpanId, evaluateTaskId, loopTo, iterationNumber, maxIterations) {
|
|
546
|
+
if (!observer) return void 0;
|
|
547
|
+
const spanId = generateId();
|
|
548
|
+
observer({
|
|
549
|
+
source: "aaac-runtime",
|
|
550
|
+
eventType: "evaluate.iteration",
|
|
551
|
+
lifecycle: "open",
|
|
552
|
+
spanId,
|
|
553
|
+
parentSpanId,
|
|
554
|
+
attributes: attrs({
|
|
555
|
+
axis: "agent",
|
|
556
|
+
"evaluate.task": evaluateTaskId,
|
|
557
|
+
"evaluate.loop_to": loopTo,
|
|
558
|
+
"evaluate.iteration": iterationNumber,
|
|
559
|
+
"evaluate.max_iterations": maxIterations
|
|
560
|
+
})
|
|
561
|
+
});
|
|
562
|
+
return spanId;
|
|
563
|
+
}
|
|
564
|
+
function emitIterationClose(observer, spanId, evaluateTaskId, iterationNumber, verdict, durationMs, feedbackInjected) {
|
|
565
|
+
if (!observer) return;
|
|
566
|
+
observer({
|
|
567
|
+
source: "aaac-runtime",
|
|
568
|
+
eventType: "evaluate.iteration",
|
|
569
|
+
lifecycle: "close",
|
|
570
|
+
spanId,
|
|
571
|
+
attributes: attrs({
|
|
572
|
+
axis: "agent",
|
|
573
|
+
"evaluate.task": evaluateTaskId,
|
|
574
|
+
"evaluate.iteration": iterationNumber,
|
|
575
|
+
"evaluate.verdict": verdict,
|
|
576
|
+
"evaluate.duration_ms": durationMs,
|
|
577
|
+
"evaluate.feedback_injected": feedbackInjected
|
|
578
|
+
})
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
function emitIterationMetric(observer, parentSpanId, evaluateTaskId, totalIterations, terminalOutcome) {
|
|
582
|
+
if (!observer) return;
|
|
583
|
+
const spanId = generateId();
|
|
584
|
+
observer({
|
|
585
|
+
source: "aaac-runtime",
|
|
586
|
+
eventType: "evaluate.metric",
|
|
587
|
+
lifecycle: "instant",
|
|
588
|
+
spanId,
|
|
589
|
+
parentSpanId,
|
|
590
|
+
attributes: attrs({
|
|
591
|
+
axis: "agent",
|
|
592
|
+
"evaluate.task": evaluateTaskId,
|
|
593
|
+
"evaluate.total_iterations": totalIterations,
|
|
594
|
+
"evaluate.terminal_outcome": terminalOutcome
|
|
595
|
+
})
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
function emitLlmTurnOpen(observer, parentSpanId, turnNumber, model, llmParams, sessionId) {
|
|
599
|
+
if (!observer) return void 0;
|
|
600
|
+
const spanId = generateId();
|
|
601
|
+
observer({
|
|
602
|
+
source: "aaac-runtime",
|
|
603
|
+
eventType: "agent.llm_turn",
|
|
604
|
+
lifecycle: "open",
|
|
605
|
+
spanId,
|
|
606
|
+
parentSpanId,
|
|
607
|
+
attributes: attrs({
|
|
608
|
+
axis: "agent",
|
|
609
|
+
"agent.session_id": sessionId,
|
|
610
|
+
"llm.turn_number": turnNumber,
|
|
611
|
+
"gen_ai.request.model": model,
|
|
612
|
+
"llm.temperature": llmParams?.temperature,
|
|
613
|
+
"llm.reasoning_effort": llmParams?.reasoningEffort
|
|
614
|
+
})
|
|
615
|
+
});
|
|
616
|
+
return spanId;
|
|
617
|
+
}
|
|
618
|
+
function emitLlmTurnClose(observer, spanId, turnNumber, durationMs, tokenAttrs2, sessionId) {
|
|
619
|
+
if (!observer) return;
|
|
620
|
+
observer({
|
|
621
|
+
source: "aaac-runtime",
|
|
622
|
+
eventType: "agent.llm_turn",
|
|
623
|
+
lifecycle: "close",
|
|
624
|
+
spanId,
|
|
625
|
+
attributes: attrs({
|
|
626
|
+
axis: "agent",
|
|
627
|
+
"agent.session_id": sessionId,
|
|
628
|
+
"llm.turn_number": turnNumber,
|
|
629
|
+
"llm.duration_ms": durationMs,
|
|
630
|
+
"gen_ai.usage.input_tokens": tokenAttrs2?.inputTokens,
|
|
631
|
+
"gen_ai.usage.output_tokens": tokenAttrs2?.outputTokens,
|
|
632
|
+
"llm.cache_hit_tokens": tokenAttrs2?.cacheHitTokens,
|
|
633
|
+
"llm.tool_calls_count": tokenAttrs2?.toolCallsCount
|
|
634
|
+
})
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
function resolveToolExecOpenOptions(sessionIdOrOptions) {
|
|
638
|
+
if (typeof sessionIdOrOptions === "string") {
|
|
639
|
+
return { sessionId: sessionIdOrOptions };
|
|
640
|
+
}
|
|
641
|
+
return sessionIdOrOptions ?? {};
|
|
642
|
+
}
|
|
643
|
+
function resolveToolExecCloseOptions(sessionIdOrOptions) {
|
|
644
|
+
if (typeof sessionIdOrOptions === "string") {
|
|
645
|
+
return { sessionId: sessionIdOrOptions };
|
|
646
|
+
}
|
|
647
|
+
return sessionIdOrOptions ?? {};
|
|
648
|
+
}
|
|
649
|
+
function emitToolExecOpen(observer, parentSpanId, toolName, inputSummary, turnNumber, sessionIdOrOptions) {
|
|
650
|
+
if (!observer) return void 0;
|
|
651
|
+
const opts = resolveToolExecOpenOptions(sessionIdOrOptions);
|
|
652
|
+
const spanId = generateId();
|
|
653
|
+
observer({
|
|
654
|
+
source: "aaac-runtime",
|
|
655
|
+
eventType: "agent.tool_exec",
|
|
656
|
+
lifecycle: "open",
|
|
657
|
+
spanId,
|
|
658
|
+
parentSpanId,
|
|
659
|
+
links: opts.links,
|
|
660
|
+
attributes: {
|
|
661
|
+
...attrs({
|
|
662
|
+
axis: "agent",
|
|
663
|
+
"agent.session_id": opts.sessionId,
|
|
664
|
+
"tool.name": toolName,
|
|
665
|
+
"tool.input_summary": inputSummary,
|
|
666
|
+
"llm.turn_number": turnNumber
|
|
667
|
+
}),
|
|
668
|
+
...opts.extraAttributes ?? {}
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
return spanId;
|
|
672
|
+
}
|
|
673
|
+
function emitToolExecClose(observer, spanId, toolName, durationMs, sessionIdOrOptions) {
|
|
674
|
+
if (!observer) return;
|
|
675
|
+
const opts = resolveToolExecCloseOptions(sessionIdOrOptions);
|
|
676
|
+
observer({
|
|
677
|
+
source: "aaac-runtime",
|
|
678
|
+
eventType: "agent.tool_exec",
|
|
679
|
+
lifecycle: "close",
|
|
680
|
+
spanId,
|
|
681
|
+
attributes: {
|
|
682
|
+
...attrs({
|
|
683
|
+
axis: "agent",
|
|
684
|
+
"agent.session_id": opts.sessionId,
|
|
685
|
+
"tool.name": toolName,
|
|
686
|
+
"tool.duration_ms": durationMs,
|
|
687
|
+
"tool.output_summary": opts.outputSummary
|
|
688
|
+
}),
|
|
689
|
+
...opts.extraAttributes ?? {}
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
function emitToolchainOpen(observer, parentSpanId, toolchainId, operation, artifactIds, componentId) {
|
|
694
|
+
if (!observer) return void 0;
|
|
695
|
+
const spanId = generateId();
|
|
696
|
+
observer({
|
|
697
|
+
source: "aaac-runtime",
|
|
698
|
+
eventType: "toolchain.execution",
|
|
699
|
+
lifecycle: "open",
|
|
700
|
+
spanId,
|
|
701
|
+
parentSpanId,
|
|
702
|
+
attributes: attrs({
|
|
703
|
+
axis: "toolchain",
|
|
704
|
+
"toolchain.id": toolchainId,
|
|
705
|
+
"toolchain.operation": operation,
|
|
706
|
+
"toolchain.artifact_ids": artifactIds.join(","),
|
|
707
|
+
"component.id": componentId
|
|
708
|
+
})
|
|
709
|
+
});
|
|
710
|
+
return spanId;
|
|
711
|
+
}
|
|
712
|
+
function emitToolchainClose(observer, spanId, toolchainId, operation, durationMs, status, errorMessage) {
|
|
713
|
+
if (!observer) return;
|
|
714
|
+
observer({
|
|
715
|
+
source: "aaac-runtime",
|
|
716
|
+
eventType: "toolchain.execution",
|
|
717
|
+
lifecycle: "close",
|
|
718
|
+
spanId,
|
|
719
|
+
attributes: attrs({
|
|
720
|
+
axis: "toolchain",
|
|
721
|
+
"toolchain.id": toolchainId,
|
|
722
|
+
"toolchain.operation": operation,
|
|
723
|
+
"toolchain.status": status,
|
|
724
|
+
"toolchain.duration_ms": durationMs,
|
|
725
|
+
...errorMessage ? { "toolchain.error": errorMessage } : {}
|
|
726
|
+
})
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// src/lib/context-pack-adoption.ts
|
|
731
|
+
import { generateId as generateId2 } from "@aaac/observability";
|
|
732
|
+
var PATH_PROBE_TOOLS = /* @__PURE__ */ new Set(["Read", "Grep", "Glob"]);
|
|
733
|
+
var CONTEXT_PACK_TOOLS = /* @__PURE__ */ new Set([
|
|
734
|
+
"analyzer.generateContextPack",
|
|
735
|
+
"generateContextPack"
|
|
736
|
+
]);
|
|
737
|
+
var CONTEXT_MAP_TOOLS = /* @__PURE__ */ new Set([
|
|
738
|
+
"analyzer.getContextMap",
|
|
739
|
+
"getContextMap"
|
|
740
|
+
]);
|
|
741
|
+
var IMPACT_TOOLS = /* @__PURE__ */ new Set([
|
|
742
|
+
"analyzer.analyzeImpact",
|
|
743
|
+
"analyzeImpact"
|
|
744
|
+
]);
|
|
745
|
+
var MAX_ENTRIES_PATHS = 20;
|
|
746
|
+
var MAX_TOP_PATHS = 20;
|
|
747
|
+
function normalizeToolPath(path) {
|
|
748
|
+
let p = path.trim().replace(/\\/g, "/");
|
|
749
|
+
if (p.startsWith("./")) p = p.slice(2);
|
|
750
|
+
while (p.startsWith("/")) p = p.slice(1);
|
|
751
|
+
return p.toLowerCase();
|
|
752
|
+
}
|
|
753
|
+
function extractProbePaths(toolName, input) {
|
|
754
|
+
if (input == null || typeof input !== "object") return [];
|
|
755
|
+
const obj = input;
|
|
756
|
+
const paths = [];
|
|
757
|
+
if (toolName === "Read") {
|
|
758
|
+
const fp = obj.file_path ?? obj.path;
|
|
759
|
+
if (typeof fp === "string" && fp.length > 0) paths.push(fp);
|
|
760
|
+
return paths;
|
|
761
|
+
}
|
|
762
|
+
if (toolName === "Grep") {
|
|
763
|
+
const fp = obj.path ?? obj.file_path;
|
|
764
|
+
if (typeof fp === "string" && fp.length > 0) paths.push(fp);
|
|
765
|
+
return paths;
|
|
766
|
+
}
|
|
767
|
+
if (toolName === "Glob") {
|
|
768
|
+
const fp = obj.path ?? obj.cwd;
|
|
769
|
+
if (typeof fp === "string" && fp.length > 0) paths.push(fp);
|
|
770
|
+
return paths;
|
|
771
|
+
}
|
|
772
|
+
return paths;
|
|
773
|
+
}
|
|
774
|
+
function isPathProbeTool(toolName) {
|
|
775
|
+
return PATH_PROBE_TOOLS.has(toolName);
|
|
776
|
+
}
|
|
777
|
+
function isContextPackTool(toolName) {
|
|
778
|
+
return CONTEXT_PACK_TOOLS.has(toolName);
|
|
779
|
+
}
|
|
780
|
+
function isContextMapTool(toolName) {
|
|
781
|
+
return CONTEXT_MAP_TOOLS.has(toolName);
|
|
782
|
+
}
|
|
783
|
+
function isImpactTool(toolName) {
|
|
784
|
+
return IMPACT_TOOLS.has(toolName);
|
|
785
|
+
}
|
|
786
|
+
function isAnalyzerObservabilityTool(toolName) {
|
|
787
|
+
return isContextPackTool(toolName) || isContextMapTool(toolName) || isImpactTool(toolName);
|
|
788
|
+
}
|
|
789
|
+
function buildContextPackOutputSummary(pack) {
|
|
790
|
+
const entriesPaths = pack.entries.map((e) => e.path).slice(0, MAX_ENTRIES_PATHS);
|
|
791
|
+
return JSON.stringify({
|
|
792
|
+
files_included: pack.metadata.filesIncluded,
|
|
793
|
+
files_referenced: pack.metadata.filesReferenced,
|
|
794
|
+
artifacts_covered: pack.metadata.artifactsCovered,
|
|
795
|
+
total_tokens: pack.metadata.totalTokens,
|
|
796
|
+
budget_tokens: pack.metadata.budgetTokens,
|
|
797
|
+
entries_paths: entriesPaths
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
function isRecord(value) {
|
|
801
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
802
|
+
}
|
|
803
|
+
function isContextMapTarget(value) {
|
|
804
|
+
if (!isRecord(value)) return false;
|
|
805
|
+
return (value.path === void 0 || typeof value.path === "string") && (value.symbolId === void 0 || typeof value.symbolId === "string");
|
|
806
|
+
}
|
|
807
|
+
function isAffectedEntry(value) {
|
|
808
|
+
if (!isRecord(value)) return false;
|
|
809
|
+
return typeof value.path === "string" && typeof value.depth === "number";
|
|
810
|
+
}
|
|
811
|
+
function isContextMapOutput(value) {
|
|
812
|
+
if (!isRecord(value)) return false;
|
|
813
|
+
return Array.isArray(value.roots) && Array.isArray(value.nodes) && Array.isArray(value.edges) && Array.isArray(value.suggestedReadTargets) && value.suggestedReadTargets.every(isContextMapTarget);
|
|
814
|
+
}
|
|
815
|
+
function isImpactReportOutput(value) {
|
|
816
|
+
if (!isRecord(value)) return false;
|
|
817
|
+
return Array.isArray(value.directlyAffected) && value.directlyAffected.every(isAffectedEntry) && Array.isArray(value.transitivelyAffected) && value.transitivelyAffected.every(isAffectedEntry);
|
|
818
|
+
}
|
|
819
|
+
function buildContextMapOutputSummary(map) {
|
|
820
|
+
const paths = map.suggestedReadTargets.map((t) => t.path).filter((p) => typeof p === "string").slice(0, MAX_TOP_PATHS);
|
|
821
|
+
return JSON.stringify({
|
|
822
|
+
roots: map.roots.length,
|
|
823
|
+
nodes: map.nodes.length,
|
|
824
|
+
edges: map.edges.length,
|
|
825
|
+
read_targets: map.suggestedReadTargets.length,
|
|
826
|
+
top_paths: paths
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
function buildAnalyzeImpactOutputSummary(report) {
|
|
830
|
+
const affected = [...report.directlyAffected, ...report.transitivelyAffected];
|
|
831
|
+
return JSON.stringify({
|
|
832
|
+
affected_count: affected.length,
|
|
833
|
+
max_depth_reached: Math.max(...affected.map((e) => e.depth), 0),
|
|
834
|
+
top_paths: affected.map((e) => e.path).slice(0, MAX_TOP_PATHS)
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
function buildAnalyzerCloseAttributes(toolName, resultText) {
|
|
838
|
+
let parsed;
|
|
839
|
+
try {
|
|
840
|
+
parsed = JSON.parse(resultText);
|
|
841
|
+
} catch {
|
|
842
|
+
return { extraAttributes: {}, paths: [] };
|
|
843
|
+
}
|
|
844
|
+
if (isContextPackTool(toolName)) {
|
|
845
|
+
if (!isContextPack(parsed)) return { extraAttributes: {}, paths: [] };
|
|
846
|
+
const contextPackId = generateId2();
|
|
847
|
+
const outputSummary = buildContextPackOutputSummary(parsed);
|
|
848
|
+
const paths = parsed.entries.map((e) => e.path);
|
|
849
|
+
return {
|
|
850
|
+
extraAttributes: {
|
|
851
|
+
"tool.output_summary": outputSummary,
|
|
852
|
+
"tool.context_pack_id": contextPackId
|
|
853
|
+
},
|
|
854
|
+
paths,
|
|
855
|
+
contextPackId
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
if (isContextMapTool(toolName)) {
|
|
859
|
+
if (!isContextMapOutput(parsed)) return { extraAttributes: {}, paths: [] };
|
|
860
|
+
const contextPackId = generateId2();
|
|
861
|
+
const outputSummary = buildContextMapOutputSummary(parsed);
|
|
862
|
+
const paths = parsed.suggestedReadTargets.map((t) => t.path ?? t.symbolId).filter((p) => typeof p === "string");
|
|
863
|
+
return {
|
|
864
|
+
extraAttributes: {
|
|
865
|
+
"tool.output_summary": outputSummary,
|
|
866
|
+
"tool.context_pack_id": contextPackId,
|
|
867
|
+
"tool.context_map": "true"
|
|
868
|
+
},
|
|
869
|
+
paths,
|
|
870
|
+
contextPackId
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
if (isImpactTool(toolName)) {
|
|
874
|
+
if (!isImpactReportOutput(parsed)) return { extraAttributes: {}, paths: [] };
|
|
875
|
+
const outputSummary = buildAnalyzeImpactOutputSummary(parsed);
|
|
876
|
+
const contextPackId = generateId2();
|
|
877
|
+
const paths = [...parsed.directlyAffected, ...parsed.transitivelyAffected].map(
|
|
878
|
+
(e) => e.path
|
|
879
|
+
);
|
|
880
|
+
return {
|
|
881
|
+
extraAttributes: {
|
|
882
|
+
"tool.output_summary": outputSummary,
|
|
883
|
+
"tool.context_pack_id": contextPackId
|
|
884
|
+
},
|
|
885
|
+
paths,
|
|
886
|
+
contextPackId
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
return { extraAttributes: {}, paths: [] };
|
|
890
|
+
}
|
|
891
|
+
var ContextPackAdoptionTracker = class {
|
|
892
|
+
packsBySession = /* @__PURE__ */ new Map();
|
|
893
|
+
registerPack(sessionId, spanId, contextPackId, paths) {
|
|
894
|
+
if (!sessionId) return;
|
|
895
|
+
const normalized = new Set(
|
|
896
|
+
paths.map(normalizeToolPath).filter((p) => p.length > 0)
|
|
897
|
+
);
|
|
898
|
+
const records = this.packsBySession.get(sessionId) ?? [];
|
|
899
|
+
records.push({
|
|
900
|
+
contextPackId,
|
|
901
|
+
spanId,
|
|
902
|
+
paths: normalized,
|
|
903
|
+
readPaths: /* @__PURE__ */ new Set()
|
|
904
|
+
});
|
|
905
|
+
this.packsBySession.set(sessionId, records);
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Evaluate adoption for a path-probe tool at emit time.
|
|
909
|
+
* Returns undefined when no pack was registered before this call.
|
|
910
|
+
*/
|
|
911
|
+
evaluate(sessionId, toolName, input) {
|
|
912
|
+
if (!sessionId || !isPathProbeTool(toolName)) return void 0;
|
|
913
|
+
const packs = this.packsBySession.get(sessionId);
|
|
914
|
+
if (!packs || packs.length === 0) return void 0;
|
|
915
|
+
const probePaths = extractProbePaths(toolName, input).map(normalizeToolPath);
|
|
916
|
+
if (probePaths.length === 0) return void 0;
|
|
917
|
+
const latestPack = packs[packs.length - 1];
|
|
918
|
+
const probePath = probePaths[0];
|
|
919
|
+
const inPack = latestPack.paths.has(probePath);
|
|
920
|
+
let kind;
|
|
921
|
+
if (inPack) {
|
|
922
|
+
kind = latestPack.readPaths.has(probePath) ? "pack_redundant" : "pack_hit";
|
|
923
|
+
latestPack.readPaths.add(probePath);
|
|
924
|
+
} else {
|
|
925
|
+
kind = "pack_miss";
|
|
926
|
+
}
|
|
927
|
+
return {
|
|
928
|
+
kind,
|
|
929
|
+
contextPackId: latestPack.contextPackId,
|
|
930
|
+
links: [
|
|
931
|
+
{
|
|
932
|
+
linkType: "uses_context_from",
|
|
933
|
+
targetSpanId: latestPack.spanId
|
|
934
|
+
}
|
|
935
|
+
]
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// src/lib/tool-exec-span-registry.ts
|
|
941
|
+
var ToolExecSpanRegistry = class {
|
|
942
|
+
pending = [];
|
|
943
|
+
register(span) {
|
|
944
|
+
this.pending.push({ ...span, closed: false });
|
|
945
|
+
}
|
|
946
|
+
/** Close the oldest still-open span for {@link toolName} (FIFO). */
|
|
947
|
+
closeFirst(toolName) {
|
|
948
|
+
const idx = this.pending.findIndex((s) => !s.closed && s.toolName === toolName);
|
|
949
|
+
if (idx === -1) return void 0;
|
|
950
|
+
this.pending[idx].closed = true;
|
|
951
|
+
return this.pending[idx];
|
|
952
|
+
}
|
|
953
|
+
isClosed(spanId) {
|
|
954
|
+
const span = this.pending.find((s) => s.spanId === spanId);
|
|
955
|
+
return span?.closed ?? false;
|
|
956
|
+
}
|
|
957
|
+
/** Return spans that are still open (for turn-boundary close). */
|
|
958
|
+
getStillOpen() {
|
|
959
|
+
return this.pending.filter((s) => !s.closed);
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
// src/lib/library-tool-sdk.ts
|
|
964
|
+
function buildSdkFunctionTools(registry) {
|
|
965
|
+
return registry.getAll().map((def) => ({
|
|
966
|
+
name: def.toolId,
|
|
967
|
+
description: def.description,
|
|
968
|
+
inputSchema: def.inputSchema ?? { type: "object", properties: {} },
|
|
969
|
+
handler: async (args) => {
|
|
970
|
+
const result = await registry.execute(def.toolId, args);
|
|
971
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
972
|
+
}
|
|
973
|
+
}));
|
|
974
|
+
}
|
|
975
|
+
async function buildClaudeMcpServer(registry, observability) {
|
|
976
|
+
const tools = buildSdkFunctionTools(registry);
|
|
977
|
+
if (tools.length === 0) return void 0;
|
|
978
|
+
const sdk = await import("@anthropic-ai/claude-agent-sdk");
|
|
979
|
+
const toolFn = sdk.tool;
|
|
980
|
+
const createServer = sdk.createSdkMcpServer;
|
|
981
|
+
if (!toolFn || !createServer) return void 0;
|
|
982
|
+
const { z } = await import("zod");
|
|
983
|
+
const { jsonSchemaToZod } = await import("./schema-converter-J47JVW2D.js");
|
|
984
|
+
const sdkTools = tools.map((t) => {
|
|
985
|
+
const zodType = jsonSchemaToZod(t.inputSchema);
|
|
986
|
+
const shape = zodType instanceof z.ZodObject ? zodType.shape : {};
|
|
987
|
+
return toolFn(
|
|
988
|
+
t.name,
|
|
989
|
+
t.description,
|
|
990
|
+
shape,
|
|
991
|
+
async (args) => {
|
|
992
|
+
const startMs = Date.now();
|
|
993
|
+
const text = await t.handler(
|
|
994
|
+
args ?? {}
|
|
995
|
+
);
|
|
996
|
+
if (observability?.observer && observability.spanRegistry) {
|
|
997
|
+
const pending = observability.spanRegistry.closeFirst(t.name);
|
|
998
|
+
if (pending) {
|
|
999
|
+
const closeAttrs = isAnalyzerObservabilityTool(t.name) ? buildAnalyzerCloseAttributes(t.name, text) : { extraAttributes: {}, paths: [], contextPackId: void 0 };
|
|
1000
|
+
emitToolExecClose(
|
|
1001
|
+
observability.observer,
|
|
1002
|
+
pending.spanId,
|
|
1003
|
+
t.name,
|
|
1004
|
+
Date.now() - startMs,
|
|
1005
|
+
{
|
|
1006
|
+
sessionId: observability.sessionId,
|
|
1007
|
+
extraAttributes: closeAttrs.extraAttributes,
|
|
1008
|
+
outputSummary: closeAttrs.extraAttributes["tool.output_summary"]
|
|
1009
|
+
}
|
|
1010
|
+
);
|
|
1011
|
+
if (observability.adoptionTracker && closeAttrs.contextPackId && closeAttrs.paths.length > 0) {
|
|
1012
|
+
observability.adoptionTracker.registerPack(
|
|
1013
|
+
observability.sessionId,
|
|
1014
|
+
pending.spanId,
|
|
1015
|
+
closeAttrs.contextPackId,
|
|
1016
|
+
closeAttrs.paths
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
return { content: [{ type: "text", text }] };
|
|
1022
|
+
}
|
|
1023
|
+
);
|
|
1024
|
+
});
|
|
1025
|
+
return createServer({
|
|
1026
|
+
name: "aaac-library-tools",
|
|
1027
|
+
version: "1.0.0",
|
|
1028
|
+
tools: sdkTools
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
async function buildOpenAIFunctionTools(registry) {
|
|
1032
|
+
const tools = buildSdkFunctionTools(registry);
|
|
1033
|
+
if (tools.length === 0) return [];
|
|
1034
|
+
try {
|
|
1035
|
+
const sdk = await import("@openai/agents");
|
|
1036
|
+
const toolFn = sdk.tool;
|
|
1037
|
+
if (toolFn) {
|
|
1038
|
+
const { jsonSchemaToZod } = await import("./schema-converter-J47JVW2D.js");
|
|
1039
|
+
return tools.map(
|
|
1040
|
+
(t) => toolFn({
|
|
1041
|
+
name: t.name,
|
|
1042
|
+
description: t.description,
|
|
1043
|
+
parameters: jsonSchemaToZod(t.inputSchema),
|
|
1044
|
+
execute: async (_ctx, args) => {
|
|
1045
|
+
return t.handler(
|
|
1046
|
+
args ?? {}
|
|
1047
|
+
);
|
|
1048
|
+
}
|
|
1049
|
+
})
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
} catch {
|
|
1053
|
+
}
|
|
1054
|
+
return tools.map((t) => ({
|
|
1055
|
+
type: "function",
|
|
1056
|
+
function: {
|
|
1057
|
+
name: t.name,
|
|
1058
|
+
description: t.description,
|
|
1059
|
+
parameters: t.inputSchema
|
|
1060
|
+
},
|
|
1061
|
+
execute: async (args) => {
|
|
1062
|
+
return t.handler(
|
|
1063
|
+
args ?? {}
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
}));
|
|
1067
|
+
}
|
|
1068
|
+
async function buildAdkFunctionTools(registry) {
|
|
1069
|
+
const tools = buildSdkFunctionTools(registry);
|
|
1070
|
+
if (tools.length === 0) return [];
|
|
1071
|
+
return tools.map((t) => ({
|
|
1072
|
+
name: t.name,
|
|
1073
|
+
description: t.description,
|
|
1074
|
+
inputSchema: t.inputSchema,
|
|
1075
|
+
execute: async (args) => {
|
|
1076
|
+
return t.handler(
|
|
1077
|
+
args ?? {}
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
}));
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
export {
|
|
1084
|
+
createAnalyzerBridge,
|
|
1085
|
+
isContextPack,
|
|
1086
|
+
renderContextPackPrompt,
|
|
1087
|
+
OBSERVABILITY_ENV_VARS,
|
|
1088
|
+
buildObservabilityEnv,
|
|
1089
|
+
serializeObserverEmit,
|
|
1090
|
+
createExecutionEnvelope,
|
|
1091
|
+
emitSessionOpen,
|
|
1092
|
+
emitSessionClose,
|
|
1093
|
+
emitSubagentOpen,
|
|
1094
|
+
emitSubagentClose,
|
|
1095
|
+
emitIterationOpen,
|
|
1096
|
+
emitIterationClose,
|
|
1097
|
+
emitIterationMetric,
|
|
1098
|
+
emitLlmTurnOpen,
|
|
1099
|
+
emitLlmTurnClose,
|
|
1100
|
+
emitToolExecOpen,
|
|
1101
|
+
emitToolExecClose,
|
|
1102
|
+
emitToolchainOpen,
|
|
1103
|
+
emitToolchainClose,
|
|
1104
|
+
ContextPackAdoptionTracker,
|
|
1105
|
+
ToolExecSpanRegistry,
|
|
1106
|
+
buildSdkFunctionTools,
|
|
1107
|
+
buildClaudeMcpServer,
|
|
1108
|
+
buildOpenAIFunctionTools,
|
|
1109
|
+
buildAdkFunctionTools
|
|
1110
|
+
};
|
|
1111
|
+
//# sourceMappingURL=chunk-KTVMMG33.js.map
|