@kweaver-ai/kweaver-sdk 0.8.1 → 0.8.3
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 +40 -52
- package/README.zh.md +41 -46
- package/dist/agent-providers/index.d.ts +7 -0
- package/dist/agent-providers/index.js +5 -0
- package/dist/agent-providers/prompt-template.d.ts +62 -0
- package/dist/agent-providers/prompt-template.js +105 -0
- package/dist/agent-providers/prompts/rubric-judge-v1.prompt.md +51 -0
- package/dist/agent-providers/prompts/within-trace-synthesizer-v1.prompt.md +60 -0
- package/dist/agent-providers/providers/claude-code-subprocess.d.ts +74 -0
- package/dist/agent-providers/providers/claude-code-subprocess.js +259 -0
- package/dist/agent-providers/providers/stub.d.ts +47 -0
- package/dist/agent-providers/providers/stub.js +77 -0
- package/dist/agent-providers/registry.d.ts +45 -0
- package/dist/agent-providers/registry.js +77 -0
- package/dist/agent-providers/types.d.ts +91 -0
- package/dist/agent-providers/types.js +25 -0
- package/dist/api/agent-chat.js +8 -6
- package/dist/api/context-loader.d.ts +1 -0
- package/dist/api/resources.d.ts +94 -0
- package/dist/api/resources.js +166 -0
- package/dist/api/semantic-search.d.ts +5 -0
- package/dist/api/semantic-search.js +5 -0
- package/dist/api/skills.d.ts +75 -2
- package/dist/api/skills.js +108 -12
- package/dist/api/trace.d.ts +5 -0
- package/dist/api/trace.js +4 -0
- package/dist/cli.js +109 -15
- package/dist/client.d.ts +3 -3
- package/dist/client.js +5 -5
- package/dist/commands/agent/mode.d.ts +6 -0
- package/dist/commands/agent/mode.js +75 -0
- package/dist/commands/agent-members.js +27 -11
- package/dist/commands/agent.js +469 -286
- package/dist/commands/auth.js +184 -71
- package/dist/commands/bkn-metric.js +37 -16
- package/dist/commands/bkn-ops.js +164 -86
- package/dist/commands/bkn-query.js +99 -31
- package/dist/commands/bkn-schema.d.ts +3 -3
- package/dist/commands/bkn-schema.js +127 -86
- package/dist/commands/bkn.js +153 -114
- package/dist/commands/call.js +23 -13
- package/dist/commands/config.js +22 -12
- package/dist/commands/context-loader.js +625 -49
- package/dist/commands/dataflow.js +14 -6
- package/dist/commands/ds.js +52 -30
- package/dist/commands/explore.js +18 -15
- package/dist/commands/model.js +53 -42
- package/dist/commands/resource.d.ts +1 -0
- package/dist/commands/{dataview.js → resource.js} +62 -84
- package/dist/commands/skill.d.ts +21 -1
- package/dist/commands/skill.js +567 -43
- package/dist/commands/token.js +11 -0
- package/dist/commands/tool.js +46 -29
- package/dist/commands/toolbox.js +31 -15
- package/dist/commands/trace.d.ts +26 -1
- package/dist/commands/trace.js +515 -15
- package/dist/commands/vega.js +466 -250
- package/dist/help/format.d.ts +65 -0
- package/dist/help/format.js +141 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3 -3
- package/dist/resources/bkn.d.ts +5 -0
- package/dist/resources/bkn.js +5 -0
- package/dist/resources/{dataviews.d.ts → resources.d.ts} +10 -11
- package/dist/resources/{dataviews.js → resources.js} +12 -13
- package/dist/resources/skills.d.ts +17 -1
- package/dist/resources/skills.js +32 -1
- package/dist/trace-ai/diagnose/agent-binding.d.ts +67 -0
- package/dist/trace-ai/diagnose/agent-binding.js +257 -0
- package/dist/trace-ai/diagnose/builtin-rules/tool-retry-intent-mismatch.yaml +68 -0
- package/dist/trace-ai/diagnose/index.d.ts +32 -0
- package/dist/trace-ai/diagnose/index.js +246 -0
- package/dist/trace-ai/diagnose/output-schema-converter.d.ts +24 -0
- package/dist/trace-ai/diagnose/output-schema-converter.js +81 -0
- package/dist/trace-ai/diagnose/query-extractor.d.ts +14 -0
- package/dist/trace-ai/diagnose/query-extractor.js +45 -0
- package/dist/trace-ai/diagnose/report-assembler.d.ts +31 -0
- package/dist/{trace-core → trace-ai}/diagnose/report-assembler.js +19 -9
- package/dist/trace-ai/diagnose/report-markdown.d.ts +18 -0
- package/dist/trace-ai/diagnose/report-markdown.js +192 -0
- package/dist/{trace-core → trace-ai}/diagnose/rule-loader.js +42 -8
- package/dist/{trace-core → trace-ai}/diagnose/schemas.d.ts +77 -2
- package/dist/trace-ai/diagnose/schemas.js +154 -0
- package/dist/trace-ai/diagnose/signal-probe.d.ts +17 -0
- package/dist/trace-ai/diagnose/signal-probe.js +39 -0
- package/dist/trace-ai/diagnose/synthesizer-agent.d.ts +40 -0
- package/dist/trace-ai/diagnose/synthesizer-agent.js +158 -0
- package/dist/{trace-core → trace-ai}/diagnose/trace-shaper.js +1 -0
- package/dist/{trace-core → trace-ai}/diagnose/types.d.ts +55 -6
- package/dist/trace-ai/eval-set/assertion-evaluator.d.ts +29 -0
- package/dist/trace-ai/eval-set/assertion-evaluator.js +100 -0
- package/dist/trace-ai/eval-set/builder.d.ts +36 -0
- package/dist/trace-ai/eval-set/builder.js +126 -0
- package/dist/trace-ai/eval-set/index.d.ts +15 -0
- package/dist/trace-ai/eval-set/index.js +10 -0
- package/dist/trace-ai/eval-set/output-writer.d.ts +27 -0
- package/dist/trace-ai/eval-set/output-writer.js +126 -0
- package/dist/trace-ai/eval-set/query-picker.d.ts +37 -0
- package/dist/trace-ai/eval-set/query-picker.js +147 -0
- package/dist/trace-ai/eval-set/redactor.d.ts +42 -0
- package/dist/trace-ai/eval-set/redactor.js +133 -0
- package/dist/trace-ai/eval-set/rubric-templates/answer-match-reference.prompt.md +19 -0
- package/dist/trace-ai/eval-set/schemas.d.ts +136 -0
- package/dist/trace-ai/eval-set/schemas.js +130 -0
- package/dist/trace-ai/eval-set/semantic-match-provider.d.ts +33 -0
- package/dist/trace-ai/eval-set/semantic-match-provider.js +51 -0
- package/dist/trace-ai/eval-set/test-runner.d.ts +34 -0
- package/dist/trace-ai/eval-set/test-runner.js +153 -0
- package/dist/trace-ai/eval-set/types.d.ts +46 -0
- package/dist/trace-ai/eval-set/types.js +8 -0
- package/dist/trace-ai/exp/bundle-writer.d.ts +10 -0
- package/dist/trace-ai/exp/bundle-writer.js +54 -0
- package/dist/trace-ai/exp/claude-binary.d.ts +5 -0
- package/dist/trace-ai/exp/claude-binary.js +30 -0
- package/dist/trace-ai/exp/coordinator.d.ts +45 -0
- package/dist/trace-ai/exp/coordinator.js +203 -0
- package/dist/trace-ai/exp/eval-runner.d.ts +14 -0
- package/dist/trace-ai/exp/eval-runner.js +47 -0
- package/dist/trace-ai/exp/exp-store/abort-signal.d.ts +3 -0
- package/dist/trace-ai/exp/exp-store/abort-signal.js +27 -0
- package/dist/trace-ai/exp/exp-store/candidate-lineage-yaml.d.ts +4 -0
- package/dist/trace-ai/exp/exp-store/candidate-lineage-yaml.js +37 -0
- package/dist/trace-ai/exp/exp-store/events-jsonl.d.ts +17 -0
- package/dist/trace-ai/exp/exp-store/events-jsonl.js +60 -0
- package/dist/trace-ai/exp/exp-store/exp-registry.d.ts +6 -0
- package/dist/trace-ai/exp/exp-store/exp-registry.js +41 -0
- package/dist/trace-ai/exp/exp-store/index.d.ts +46 -0
- package/dist/trace-ai/exp/exp-store/index.js +59 -0
- package/dist/trace-ai/exp/exp-store/lock.d.ts +3 -0
- package/dist/trace-ai/exp/exp-store/lock.js +73 -0
- package/dist/trace-ai/exp/exp-store/mission-md.d.ts +3 -0
- package/dist/trace-ai/exp/exp-store/mission-md.js +37 -0
- package/dist/trace-ai/exp/exp-store/readme-template.d.ts +5 -0
- package/dist/trace-ai/exp/exp-store/readme-template.js +25 -0
- package/dist/trace-ai/exp/exp-store/round-yaml.d.ts +3 -0
- package/dist/trace-ai/exp/exp-store/round-yaml.js +33 -0
- package/dist/trace-ai/exp/index.d.ts +8 -0
- package/dist/trace-ai/exp/index.js +238 -0
- package/dist/trace-ai/exp/info.d.ts +35 -0
- package/dist/trace-ai/exp/info.js +120 -0
- package/dist/trace-ai/exp/patch/agent-config.d.ts +1 -0
- package/dist/trace-ai/exp/patch/agent-config.js +26 -0
- package/dist/trace-ai/exp/patch/index.d.ts +2 -0
- package/dist/trace-ai/exp/patch/index.js +13 -0
- package/dist/trace-ai/exp/patch/skill.d.ts +1 -0
- package/dist/trace-ai/exp/patch/skill.js +24 -0
- package/dist/trace-ai/exp/providers/synthesizer-client.d.ts +14 -0
- package/dist/trace-ai/exp/providers/synthesizer-client.js +39 -0
- package/dist/trace-ai/exp/providers/triage-client.d.ts +19 -0
- package/dist/trace-ai/exp/providers/triage-client.js +51 -0
- package/dist/trace-ai/exp/schemas.d.ts +147 -0
- package/dist/trace-ai/exp/schemas.js +50 -0
- package/dist/trace-ai/exp/scoring.d.ts +2 -0
- package/dist/trace-ai/exp/scoring.js +46 -0
- package/dist/trace-ai/scan/aggregator.d.ts +20 -0
- package/dist/trace-ai/scan/aggregator.js +26 -0
- package/dist/trace-ai/scan/artifacts/paths.d.ts +12 -0
- package/dist/trace-ai/scan/artifacts/paths.js +18 -0
- package/dist/trace-ai/scan/artifacts/writer.d.ts +67 -0
- package/dist/trace-ai/scan/artifacts/writer.js +96 -0
- package/dist/trace-ai/scan/batched-rubric.d.ts +55 -0
- package/dist/trace-ai/scan/batched-rubric.js +159 -0
- package/dist/trace-ai/scan/cross-trace-synthesizer.d.ts +24 -0
- package/dist/trace-ai/scan/cross-trace-synthesizer.js +93 -0
- package/dist/trace-ai/scan/index.d.ts +31 -0
- package/dist/trace-ai/scan/index.js +390 -0
- package/dist/trace-ai/scan/prompts/builtin/cross-trace-synthesizer-v1.prompt.md +44 -0
- package/dist/trace-ai/scan/prompts/builtin/rubric-judge-batch-v1.prompt.md +44 -0
- package/dist/trace-ai/scan/runner.d.ts +25 -0
- package/dist/trace-ai/scan/runner.js +42 -0
- package/dist/trace-ai/scan/sampler.d.ts +18 -0
- package/dist/trace-ai/scan/sampler.js +81 -0
- package/dist/trace-ai/scan/scan-summary-markdown.d.ts +2 -0
- package/dist/trace-ai/scan/scan-summary-markdown.js +71 -0
- package/dist/trace-ai/scan/scan-summary-schema.d.ts +73 -0
- package/dist/trace-ai/scan/scan-summary-schema.js +61 -0
- package/dist/trace-ai/scan/single-agent-validator.d.ts +23 -0
- package/dist/trace-ai/scan/single-agent-validator.js +42 -0
- package/dist/trace-ai/scan/traces-list-parser.d.ts +15 -0
- package/dist/trace-ai/scan/traces-list-parser.js +46 -0
- package/package.json +2 -2
- package/dist/api/dataviews.d.ts +0 -117
- package/dist/api/dataviews.js +0 -265
- package/dist/commands/dataview.d.ts +0 -8
- package/dist/trace-core/diagnose/index.d.ts +0 -9
- package/dist/trace-core/diagnose/index.js +0 -104
- package/dist/trace-core/diagnose/report-assembler.d.ts +0 -12
- package/dist/trace-core/diagnose/schemas.js +0 -94
- package/dist/trace-core/diagnose/signal-probe.d.ts +0 -5
- package/dist/trace-core/diagnose/signal-probe.js +0 -21
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.yaml +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.yaml +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/register.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/register.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.yaml +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.yaml +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.yaml +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/predicate-registry.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/predicate-registry.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/rule-loader.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/synthesizer-template.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/synthesizer-template.js +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/trace-shaper.d.ts +0 -0
- /package/dist/{trace-core → trace-ai}/diagnose/types.js +0 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import yaml from "js-yaml";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { AgentProviderError } from "../../agent-providers/types.js";
|
|
4
|
+
import { render as renderPrompt, languageInstructionFor, } from "../../agent-providers/prompt-template.js";
|
|
5
|
+
function chunkArray(arr, size) {
|
|
6
|
+
const out = [];
|
|
7
|
+
for (let i = 0; i < arr.length; i += size)
|
|
8
|
+
out.push(arr.slice(i, i + size));
|
|
9
|
+
return out;
|
|
10
|
+
}
|
|
11
|
+
function buildTracesYaml(chunk) {
|
|
12
|
+
return yaml.dump(chunk.map((t) => ({ trace_id: t.traceId, spans: t.spans, inputs: t.inputs })), { lineWidth: 120 });
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Takes the single-verdict raw JSON Schema object (from rule YAML's `output_schema`
|
|
16
|
+
* block) and returns a wrapper schema that:
|
|
17
|
+
* 1. Injects `trace_id` into the item's `required` array and `properties` map.
|
|
18
|
+
* 2. Wraps the augmented item inside `{ trace_results: array<augmented-item> }`.
|
|
19
|
+
*
|
|
20
|
+
* This ensures the prompt's `output_schema` block instructs the LLM to emit
|
|
21
|
+
* `trace_id` on every verdict, matching the zod schema used for validation.
|
|
22
|
+
*/
|
|
23
|
+
function wrapSchemaForBatch(single) {
|
|
24
|
+
const required = Array.isArray(single.required)
|
|
25
|
+
? [...single.required, "trace_id"]
|
|
26
|
+
: ["trace_id"];
|
|
27
|
+
const properties = {
|
|
28
|
+
...(single.properties ?? {}),
|
|
29
|
+
trace_id: { type: "string", description: "Echo back the trace_id from input" },
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
type: "object",
|
|
33
|
+
required: ["trace_results"],
|
|
34
|
+
properties: {
|
|
35
|
+
trace_results: {
|
|
36
|
+
type: "array",
|
|
37
|
+
items: { ...single, type: "object", required, properties },
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Stage-2 batched rubric evaluator. Splits flagged traces into chunks of K
|
|
44
|
+
* (default 10), one LLM call per chunk, then validates each per-trace verdict
|
|
45
|
+
* against the rule's output schema PLUS two ground-truth checks:
|
|
46
|
+
* - trace_id must echo back one of this chunk's input trace_ids
|
|
47
|
+
* - first_violating_step_id must be a real span_id in THAT trace's spans
|
|
48
|
+
* Failures isolate to the affected trace; chunk-wide LLM failures skip the
|
|
49
|
+
* whole chunk with agent-error:<kind>.
|
|
50
|
+
*/
|
|
51
|
+
export async function runBatchedRubric(opts) {
|
|
52
|
+
const { rule, traces, agentId, provider, promptRegistry, chunkSize, artifacts } = opts;
|
|
53
|
+
const verdicts = [];
|
|
54
|
+
const skipped = [];
|
|
55
|
+
if (artifacts) {
|
|
56
|
+
await artifacts.writeStageTwoWorkQueue(rule.ruleId, traces.map((t) => t.traceId));
|
|
57
|
+
}
|
|
58
|
+
const tpl = promptRegistry.get(rule.promptTemplateRef);
|
|
59
|
+
const chunks = chunkArray(traces, chunkSize);
|
|
60
|
+
for (let chunkIdx = 0; chunkIdx < chunks.length; chunkIdx++) {
|
|
61
|
+
const chunk = chunks[chunkIdx];
|
|
62
|
+
const traceIdSet = new Set(chunk.map((t) => t.traceId));
|
|
63
|
+
const spansByTraceId = new Map(chunk.map((t) => [t.traceId, new Set(t.spans)]));
|
|
64
|
+
const prompt = renderPrompt(tpl, {
|
|
65
|
+
rule_id: rule.ruleId,
|
|
66
|
+
batch_size: chunk.length,
|
|
67
|
+
agent_id: agentId,
|
|
68
|
+
judge_question: rule.judgeQuestion,
|
|
69
|
+
traces_yaml: buildTracesYaml(chunk),
|
|
70
|
+
output_schema: wrapSchemaForBatch(rule.outputSchemaRaw),
|
|
71
|
+
language_instruction: languageInstructionFor(opts.lang ?? "en"),
|
|
72
|
+
});
|
|
73
|
+
if (artifacts)
|
|
74
|
+
await artifacts.writeStageTwoPrompt(rule.ruleId, chunkIdx, prompt);
|
|
75
|
+
// rule.outputSchema is the SINGLE-verdict shape (zod converted from rule YAML's
|
|
76
|
+
// output_schema block). The Stage-2 batched prompt asks the LLM to return
|
|
77
|
+
// { trace_results: [<verdict>, ...] }, so we wrap before validation.
|
|
78
|
+
// We also extend each array item with trace_id (the LLM echoes back the
|
|
79
|
+
// trace_id from the input; the raw prompt schema enforces this via
|
|
80
|
+
// wrapSchemaForBatch above, so the zod schema must match).
|
|
81
|
+
const batchedOutputSchema = z.object({
|
|
82
|
+
trace_results: z.array(rule.outputSchema.and(z.object({ trace_id: z.string() }))),
|
|
83
|
+
});
|
|
84
|
+
let response;
|
|
85
|
+
try {
|
|
86
|
+
const resp = await provider.invoke({
|
|
87
|
+
prompt,
|
|
88
|
+
outputSchema: batchedOutputSchema,
|
|
89
|
+
tier: "fast",
|
|
90
|
+
timeoutMs: opts.timeoutMs,
|
|
91
|
+
correlationId: `stage-2/${rule.ruleId}/chunk-${chunkIdx}`,
|
|
92
|
+
});
|
|
93
|
+
response = resp.output;
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
const kind = e instanceof AgentProviderError ? e.kind : "transport";
|
|
97
|
+
for (const t of chunk)
|
|
98
|
+
skipped.push({ traceId: t.traceId, reason: `agent-error:${kind}` });
|
|
99
|
+
if (artifacts)
|
|
100
|
+
await artifacts.writeStageTwoResponse(rule.ruleId, chunkIdx, { error: String(e) });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (artifacts)
|
|
104
|
+
await artifacts.writeStageTwoResponse(rule.ruleId, chunkIdx, response);
|
|
105
|
+
const parseErrors = [];
|
|
106
|
+
const items = response.trace_results ?? [];
|
|
107
|
+
const seenInChunk = new Set(); // NEW: defend against LLM emitting same trace_id twice
|
|
108
|
+
for (const item of items) {
|
|
109
|
+
const itm = item;
|
|
110
|
+
const traceId = typeof itm.trace_id === "string" ? itm.trace_id : undefined;
|
|
111
|
+
if (!traceId || !traceIdSet.has(traceId)) {
|
|
112
|
+
// Unrecognized trace_id: silently discard — the "missing in trace_results"
|
|
113
|
+
// loop below will create a schema_violation entry for the actual input trace.
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (seenInChunk.has(traceId)) {
|
|
117
|
+
// LLM violated "no duplicates" — drop the duplicate verdict, record parse-error.
|
|
118
|
+
// The first occurrence has already been accepted; we keep that one as the verdict.
|
|
119
|
+
parseErrors.push({ traceId, reason: "schema_violation: duplicate trace_id in trace_results" });
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
seenInChunk.add(traceId);
|
|
123
|
+
const first = typeof itm.first_violating_step_id === "string" ? itm.first_violating_step_id : undefined;
|
|
124
|
+
if (!first || !spansByTraceId.get(traceId).has(first)) {
|
|
125
|
+
parseErrors.push({
|
|
126
|
+
traceId,
|
|
127
|
+
reason: `schema_violation: first_violating_step_id '${first}' not in trace's spans`,
|
|
128
|
+
});
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
verdicts.push({
|
|
132
|
+
traceId,
|
|
133
|
+
category: String(itm.category ?? "other"),
|
|
134
|
+
reasoning: String(itm.reasoning ?? ""),
|
|
135
|
+
severity: itm.severity ?? "low",
|
|
136
|
+
firstViolatingStepId: first,
|
|
137
|
+
evidenceSpanIds: Array.isArray(itm.evidence_span_ids)
|
|
138
|
+
? itm.evidence_span_ids.map(String)
|
|
139
|
+
: [first],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
// Any trace_id in this chunk's input that didn't appear in trace_results → schema_violation.
|
|
143
|
+
const verdictTraceIds = new Set(items.map((i) => i.trace_id));
|
|
144
|
+
for (const t of chunk) {
|
|
145
|
+
if (!verdictTraceIds.has(t.traceId)) {
|
|
146
|
+
parseErrors.push({
|
|
147
|
+
traceId: t.traceId,
|
|
148
|
+
reason: "schema_violation: missing in trace_results",
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const pe of parseErrors)
|
|
153
|
+
skipped.push({ traceId: pe.traceId, reason: `agent-error:${pe.reason}` });
|
|
154
|
+
if (parseErrors.length > 0 && artifacts) {
|
|
155
|
+
await artifacts.writeStageTwoParseErrors(rule.ruleId, chunkIdx, parseErrors);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { verdicts, skipped };
|
|
159
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AgentProvider } from "../../agent-providers/types.js";
|
|
2
|
+
import { PromptTemplateRegistry, type AgentOutputLang } from "../../agent-providers/prompt-template.js";
|
|
3
|
+
import { ScanSummaryShape } from "./scan-summary-schema.js";
|
|
4
|
+
import type { AggregatesBlock } from "./aggregator.js";
|
|
5
|
+
import type { SamplerOutput } from "./sampler.js";
|
|
6
|
+
import { ArtifactWriter } from "./artifacts/writer.js";
|
|
7
|
+
export interface CrossTraceSynthesizerResult {
|
|
8
|
+
summary: import("zod").infer<typeof ScanSummaryShape> | null;
|
|
9
|
+
/** Non-null when summary is null (schema_violation / transport / etc.). */
|
|
10
|
+
fallbackReason?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface RunCrossTraceSynthesizerOpts {
|
|
13
|
+
agentId: string;
|
|
14
|
+
aggregates: AggregatesBlock;
|
|
15
|
+
samples: SamplerOutput;
|
|
16
|
+
nTotal: number;
|
|
17
|
+
provider: AgentProvider;
|
|
18
|
+
promptRegistry: PromptTemplateRegistry;
|
|
19
|
+
promptRef?: string;
|
|
20
|
+
lang?: AgentOutputLang;
|
|
21
|
+
artifacts?: ArtifactWriter;
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function runCrossTraceSynthesizer(opts: RunCrossTraceSynthesizerOpts): Promise<CrossTraceSynthesizerResult>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import yaml from "js-yaml";
|
|
2
|
+
import { AgentProviderError } from "../../agent-providers/types.js";
|
|
3
|
+
import { render as renderPrompt, languageInstructionFor } from "../../agent-providers/prompt-template.js";
|
|
4
|
+
import { ScanSummaryShape } from "./scan-summary-schema.js";
|
|
5
|
+
const SUMMARY_OUTPUT_SCHEMA_DESCRIPTION = {
|
|
6
|
+
type: "object",
|
|
7
|
+
required: ["headline", "primary_root_cause", "fix_priority", "cross_rule_links"],
|
|
8
|
+
properties: {
|
|
9
|
+
headline: { type: "string", maxLength: 160 },
|
|
10
|
+
primary_root_cause: {
|
|
11
|
+
anyOf: [
|
|
12
|
+
{ type: "null" },
|
|
13
|
+
{
|
|
14
|
+
type: "object",
|
|
15
|
+
required: ["rule_ids", "description", "target_for_fix"],
|
|
16
|
+
properties: {
|
|
17
|
+
rule_ids: { type: "array", items: { type: "string" }, minItems: 1 },
|
|
18
|
+
description: { type: "string" },
|
|
19
|
+
target_for_fix: { type: "string" },
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
fix_priority: {
|
|
25
|
+
type: "array",
|
|
26
|
+
items: {
|
|
27
|
+
type: "object",
|
|
28
|
+
required: ["rule_id", "affected_trace_count", "reason"],
|
|
29
|
+
properties: {
|
|
30
|
+
rule_id: { type: "string" },
|
|
31
|
+
affected_trace_count: { type: "integer", minimum: 0 },
|
|
32
|
+
reason: { type: "string" },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
cross_rule_links: {
|
|
37
|
+
type: "array",
|
|
38
|
+
items: {
|
|
39
|
+
type: "object",
|
|
40
|
+
required: ["rule_ids", "relation"],
|
|
41
|
+
properties: {
|
|
42
|
+
rule_ids: { type: "array", items: { type: "string" }, minItems: 2 },
|
|
43
|
+
relation: { type: "string" },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
function formatRatio(k, n) {
|
|
50
|
+
if (n === 0)
|
|
51
|
+
return "0%";
|
|
52
|
+
return `${Math.round((k / n) * 100)}%`;
|
|
53
|
+
}
|
|
54
|
+
export async function runCrossTraceSynthesizer(opts) {
|
|
55
|
+
const { agentId, aggregates, samples, nTotal, provider, promptRegistry, artifacts } = opts;
|
|
56
|
+
const ref = opts.promptRef ?? "builtin:cross-trace-synthesizer-v1";
|
|
57
|
+
const sampleCount = samples.samples.length;
|
|
58
|
+
if (artifacts)
|
|
59
|
+
await artifacts.writeStageFourInputs(aggregates, samples);
|
|
60
|
+
const tpl = promptRegistry.get(ref);
|
|
61
|
+
const prompt = renderPrompt(tpl, {
|
|
62
|
+
n_total: nTotal,
|
|
63
|
+
sample_count: sampleCount,
|
|
64
|
+
sample_ratio: formatRatio(sampleCount, nTotal),
|
|
65
|
+
agent_id: agentId,
|
|
66
|
+
aggregates: yaml.dump(aggregates, { lineWidth: 120 }),
|
|
67
|
+
samples_yaml: yaml.dump(samples, { lineWidth: 120 }),
|
|
68
|
+
output_schema: SUMMARY_OUTPUT_SCHEMA_DESCRIPTION,
|
|
69
|
+
language_instruction: languageInstructionFor(opts.lang ?? "en"),
|
|
70
|
+
});
|
|
71
|
+
if (artifacts)
|
|
72
|
+
await artifacts.writeStageFourPrompt(prompt);
|
|
73
|
+
try {
|
|
74
|
+
const resp = await provider.invoke({
|
|
75
|
+
prompt,
|
|
76
|
+
outputSchema: ScanSummaryShape,
|
|
77
|
+
tier: "std",
|
|
78
|
+
timeoutMs: opts.timeoutMs,
|
|
79
|
+
correlationId: `stage-4/${agentId}`,
|
|
80
|
+
});
|
|
81
|
+
if (artifacts)
|
|
82
|
+
await artifacts.writeStageFourResponse(resp.output);
|
|
83
|
+
return { summary: resp.output };
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
const kind = e instanceof AgentProviderError ? e.kind : "transport";
|
|
87
|
+
if (artifacts) {
|
|
88
|
+
await artifacts.writeStageFourResponse({ error: String(e) });
|
|
89
|
+
await artifacts.writeStageFourParseErrors([{ reason: `agent-error:${kind}`, detail: String(e) }]);
|
|
90
|
+
}
|
|
91
|
+
return { summary: null, fallbackReason: `agent-error:${kind}` };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "../diagnose/builtin-rules/register.js";
|
|
2
|
+
export interface RunBatchOpts {
|
|
3
|
+
traces: string[];
|
|
4
|
+
out: string;
|
|
5
|
+
rulesDir: string | null;
|
|
6
|
+
noBuiltin: boolean;
|
|
7
|
+
noArtifacts: boolean;
|
|
8
|
+
lang?: "en" | "zh";
|
|
9
|
+
/**
|
|
10
|
+
* Output format. The YAML report is always written (it's the resume
|
|
11
|
+
* ground truth via ReportSchema.safeParse). Markdown is written when
|
|
12
|
+
* format ∈ {'markdown', 'both'}. Default: 'both'.
|
|
13
|
+
*/
|
|
14
|
+
format?: "yaml" | "markdown" | "both";
|
|
15
|
+
timeoutMs: number;
|
|
16
|
+
maxParallel: number;
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
token: string;
|
|
19
|
+
businessDomain: string;
|
|
20
|
+
}
|
|
21
|
+
export interface RunBatchResult {
|
|
22
|
+
scanSummaryPath: string;
|
|
23
|
+
perTraceReportPaths: string[];
|
|
24
|
+
tracesDiagnosed: number;
|
|
25
|
+
tracesReused: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Orchestrator: walks N conv_ids through the batch pipeline.
|
|
29
|
+
* Single-agent enforced; --no-llm rejected by CLI (not here).
|
|
30
|
+
*/
|
|
31
|
+
export declare function runBatch(opts: RunBatchOpts): Promise<RunBatchResult>;
|