@luanpdd/kit-mcp 1.35.0 → 1.36.0
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/bin/cli.js +2 -2
- package/bin/mcp.js +6 -6
- package/bin/ui.js +74 -74
- package/gates/ai-prompt-stability.md +120 -120
- package/gates/budget-description.md +68 -68
- package/gates/confidence.md +29 -29
- package/gates/dependency-check.md +33 -33
- package/gates/dept-cycle-prevention.md +179 -179
- package/gates/golden-signals-coverage.md +133 -133
- package/gates/legacy-refactor-safety.md +178 -178
- package/gates/multi-tenant-rls-coverage.md +102 -102
- package/gates/no-personal-uuid.md +72 -72
- package/gates/obs-agents-mcp-supabase.md +86 -86
- package/gates/obs-skills-frontmatter.md +76 -76
- package/gates/observability-coverage.md +151 -151
- package/gates/omm-no-regression.md +83 -83
- package/gates/postmortem-template-required.md +127 -127
- package/gates/prr-checklist-coverage.md +128 -128
- package/gates/regression.md +32 -32
- package/gates/release-pipeline-policy.md +132 -132
- package/gates/secrets-scan.md +33 -33
- package/gates/service-role-not-in-user-facing.md +113 -113
- package/gates/skill-must-include.md +71 -71
- package/gates/sync-idempotent.md +62 -62
- package/gates/verify-phase-goal.md +34 -34
- package/kit/agents/designer-ui.md +216 -216
- package/kit/agents/workflow-generator.md +537 -167
- package/kit/commands/adicionar-backlog.md +1 -1
- package/kit/commands/adicionar-fase.md +1 -1
- package/kit/commands/adicionar-tarefa.md +1 -1
- package/kit/commands/auditar-observabilidade.md +103 -103
- package/kit/commands/auditar-toil.md +129 -129
- package/kit/commands/caracterizar-prompt.md +195 -195
- package/kit/commands/criar-workflow.md +158 -158
- package/kit/commands/definir-perfil.md +1 -1
- package/kit/commands/definir-slo.md +108 -108
- package/kit/commands/fio.md +1 -1
- package/kit/commands/golden-signals.md +142 -142
- package/kit/commands/instrumentar-fase.md +200 -200
- package/kit/commands/investigar-producao.md +162 -162
- package/kit/commands/observabilidade.md +118 -118
- package/kit/commands/postmortem.md +179 -179
- package/kit/commands/prr.md +205 -205
- package/kit/commands/publicar-rapido.md +207 -207
- package/kit/commands/risk-budget.md +220 -220
- package/kit/commands/sre.md +230 -230
- package/kit/file-manifest.json +424 -424
- package/kit/framework/references/output-style.md +22 -22
- package/kit/hooks/post-apply-migration.js +199 -199
- package/kit/hooks/sidecar-tool-publisher.js +210 -210
- package/kit/skills/_shared-dados-distribuidos/glossary.md +224 -224
- package/kit/skills/_shared-legacy/glossary.md +389 -389
- package/kit/skills/_shared-multi-tenant/glossary.md +186 -186
- package/kit/skills/_shared-observability/glossary.md +396 -396
- package/kit/skills/_shared-sre/glossary.md +712 -712
- package/kit/skills/_shared-supabase/glossary.md +234 -234
- package/kit/skills/blameless-postmortems/SKILL.md +340 -340
- package/kit/skills/burn-rate-alerting/SKILL.md +258 -258
- package/kit/skills/cascading-failures/SKILL.md +311 -311
- package/kit/skills/core-analysis-loop/SKILL.md +352 -352
- package/kit/skills/distributed-tracing/SKILL.md +362 -362
- package/kit/skills/dynamic-workflow-authoring/SKILL.md +327 -223
- package/kit/skills/eliminating-toil/SKILL.md +243 -243
- package/kit/skills/event-based-slos/SKILL.md +296 -296
- package/kit/skills/four-golden-signals/SKILL.md +314 -314
- package/kit/skills/hermetic-builds/SKILL.md +323 -323
- package/kit/skills/legacy-monster-methods/SKILL.md +444 -444
- package/kit/skills/llm-as-dependency/SKILL.md +436 -436
- package/kit/skills/load-shedding-graceful-degradation/SKILL.md +396 -396
- package/kit/skills/observability-driven-development/SKILL.md +315 -315
- package/kit/skills/observability-maturity-model/SKILL.md +222 -222
- package/kit/skills/opentelemetry-standard/SKILL.md +351 -351
- package/kit/skills/production-readiness-review/SKILL.md +305 -305
- package/kit/skills/release-engineering/SKILL.md +367 -367
- package/kit/skills/retry-strategies/SKILL.md +372 -372
- package/kit/skills/sre-risk-management/SKILL.md +221 -221
- package/kit/skills/structured-events/SKILL.md +265 -265
- package/kit/skills/supabase-cron-queues/SKILL.md +275 -275
- package/kit/skills/supabase-database-functions/SKILL.md +332 -332
- package/kit/skills/supabase-declarative-schema/SKILL.md +183 -183
- package/kit/skills/supabase-pgvector-rag/SKILL.md +253 -253
- package/kit/skills/supabase-postgres-style/SKILL.md +138 -138
- package/kit/skills/supabase-storage/SKILL.md +234 -234
- package/kit/skills/telemetry-pipelines/SKILL.md +259 -259
- package/kit/skills/telemetry-sampling/SKILL.md +256 -256
- package/kit/skills/ui-anti-padroes-ia/SKILL.md +261 -261
- package/kit/skills/ui-contexto-produto/SKILL.md +248 -248
- package/kit/skills/ui-cor-estrategia/SKILL.md +213 -213
- package/kit/skills/ui-critica-auditoria/SKILL.md +260 -260
- package/kit/skills/ui-motion-funcional/SKILL.md +264 -264
- package/kit/skills/ui-ritmo-espacial/SKILL.md +259 -259
- package/kit/skills/ui-tipografia/SKILL.md +211 -211
- package/package.json +1 -1
- package/src/cli/index.js +1114 -1114
- package/src/cli/render.js +194 -194
- package/src/cli/upgrade-check.js +135 -135
- package/src/core/error-redaction.js +76 -76
- package/src/core/failures.js +153 -153
- package/src/core/gate-runner.js +205 -205
- package/src/core/gates.js +82 -82
- package/src/core/logger.js +170 -170
- package/src/core/manifest-verify.js +174 -174
- package/src/core/metrics.js +268 -268
- package/src/core/notify.js +60 -60
- package/src/core/path-safety.js +141 -141
- package/src/core/replays.js +120 -120
- package/src/core/ui.js +185 -185
- package/src/mcp-server/install.js +149 -149
- package/src/mcp-server/roots.js +124 -124
- package/src/ui/auto-spawn.js +113 -113
- package/src/ui/browser.js +78 -78
- package/src/ui/client.js +130 -130
- package/src/ui/events.js +65 -65
- package/src/ui/lockfile.js +191 -191
- package/src/ui/port.js +67 -67
- package/src/ui/server.js +547 -547
- package/src/ui/wrapper.js +129 -129
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
// SEC-14-06 — central redaction helpers shared by mcp-server, reflect, and replays.
|
|
2
|
-
//
|
|
3
|
-
// Pure module: no I/O, no globals other than the constant regex set.
|
|
4
|
-
//
|
|
5
|
-
// Why a single choke point: the threat model is "leakage of API keys, Bearer
|
|
6
|
-
// tokens, and absolute filesystem paths through MCP error envelopes / persisted
|
|
7
|
-
// replays". Scattering redaction across each call site invites drift. One file,
|
|
8
|
-
// one regex set, three import sites — and a single grep proves coverage.
|
|
9
|
-
//
|
|
10
|
-
// Order rationale (PATTERNS array):
|
|
11
|
-
// 1. sk-ant-* before sk-* — Anthropic prefix is more specific. (In practice
|
|
12
|
-
// the openai pattern's [A-Za-z0-9] character class would NOT swallow
|
|
13
|
-
// "sk-ant-" because of the dash, but ordering keeps intent legible.)
|
|
14
|
-
// 2. x-api-key header before Bearer — both are distinct shapes; order is
|
|
15
|
-
// arbitrary but stable.
|
|
16
|
-
// 3. Path patterns last — broadest character class, matched after specific
|
|
17
|
-
// secrets so a secret that contains slash-like characters has been
|
|
18
|
-
// stripped already.
|
|
19
|
-
//
|
|
20
|
-
// Non-false-positive contract (verified by test/unit/error-redaction.test.js):
|
|
21
|
-
// - "Compare A:B" stays unchanged (no `\` or `/` after `:`)
|
|
22
|
-
// - "Modal: hello" stays unchanged (no `\` or `/` after `:`)
|
|
23
|
-
// - "Visit https://example.com/path" stays (lowercase scheme, no Drive: pattern)
|
|
24
|
-
// - "Bearer x" stays unchanged (1 char, below 20 minimum)
|
|
25
|
-
// - "sk-foo" stays unchanged (3 chars after sk-, below 20 minimum)
|
|
26
|
-
// - "see /etc/passwd" stays unchanged (etc not in {home,Users,root} allowlist)
|
|
27
|
-
//
|
|
28
|
-
// Idempotency: redactSecrets(redactSecrets(x)) === redactSecrets(x). The
|
|
29
|
-
// substitution strings ('[REDACTED:*]', '[PATH]', etc.) contain no characters
|
|
30
|
-
// that match any of the patterns themselves.
|
|
31
|
-
|
|
32
|
-
const PATTERNS = [
|
|
33
|
-
{ re: /sk-ant-[A-Za-z0-9_\-]{20,}/g, sub: '[REDACTED:anthropic_key]' },
|
|
34
|
-
{ re: /sk-[A-Za-z0-9]{20,}/g, sub: '[REDACTED:openai_key]' },
|
|
35
|
-
{ re: /x-api-key\s*:\s*[^\s,;'"]+/gi, sub: 'x-api-key: [REDACTED]' },
|
|
36
|
-
{ re: /Bearer\s+[A-Za-z0-9._\-]{20,}/gi, sub: 'Bearer [REDACTED]' },
|
|
37
|
-
{ re: /[A-Z]:[\\\/][^\s'"`<>]+/g, sub: '[PATH]' },
|
|
38
|
-
{ re: /\/(home|Users|root)\/[^\s'"`<>]+/g, sub: '[PATH]' },
|
|
39
|
-
];
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Strip secrets and absolute filesystem paths from a string. Defensive: coerces
|
|
43
|
-
* non-string inputs via String(value); null/undefined return ''.
|
|
44
|
-
*
|
|
45
|
-
* @param {unknown} text
|
|
46
|
-
* @returns {string}
|
|
47
|
-
*/
|
|
48
|
-
export function redactSecrets(text) {
|
|
49
|
-
if (text == null) return '';
|
|
50
|
-
let s = String(text);
|
|
51
|
-
for (const { re, sub } of PATTERNS) {
|
|
52
|
-
s = s.replace(re, sub);
|
|
53
|
-
}
|
|
54
|
-
return s;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Build the public MCP error envelope for an arbitrary thrown value. The
|
|
59
|
-
* server-side stderr keeps the full trace for operator debugging; the
|
|
60
|
-
* JSON-RPC client receives only `{error, code}` — no trace field is emitted.
|
|
61
|
-
*
|
|
62
|
-
* Preserves err.code when present (Phase 83.03 added `EMANIFESTMISMATCH`;
|
|
63
|
-
* downstream callers can keep dispatching on that code).
|
|
64
|
-
*
|
|
65
|
-
* @param {unknown} err
|
|
66
|
-
* @returns {{ error: string, code: string }}
|
|
67
|
-
*/
|
|
68
|
-
export function sanitizeMcpError(err) {
|
|
69
|
-
const msg = err && typeof err === 'object' && 'message' in err
|
|
70
|
-
? err.message
|
|
71
|
-
: err;
|
|
72
|
-
return {
|
|
73
|
-
error: redactSecrets(msg ?? 'unknown error'),
|
|
74
|
-
code: (err && typeof err === 'object' && err.code) ? err.code : 'MCP_INTERNAL_ERROR',
|
|
75
|
-
};
|
|
76
|
-
}
|
|
1
|
+
// SEC-14-06 — central redaction helpers shared by mcp-server, reflect, and replays.
|
|
2
|
+
//
|
|
3
|
+
// Pure module: no I/O, no globals other than the constant regex set.
|
|
4
|
+
//
|
|
5
|
+
// Why a single choke point: the threat model is "leakage of API keys, Bearer
|
|
6
|
+
// tokens, and absolute filesystem paths through MCP error envelopes / persisted
|
|
7
|
+
// replays". Scattering redaction across each call site invites drift. One file,
|
|
8
|
+
// one regex set, three import sites — and a single grep proves coverage.
|
|
9
|
+
//
|
|
10
|
+
// Order rationale (PATTERNS array):
|
|
11
|
+
// 1. sk-ant-* before sk-* — Anthropic prefix is more specific. (In practice
|
|
12
|
+
// the openai pattern's [A-Za-z0-9] character class would NOT swallow
|
|
13
|
+
// "sk-ant-" because of the dash, but ordering keeps intent legible.)
|
|
14
|
+
// 2. x-api-key header before Bearer — both are distinct shapes; order is
|
|
15
|
+
// arbitrary but stable.
|
|
16
|
+
// 3. Path patterns last — broadest character class, matched after specific
|
|
17
|
+
// secrets so a secret that contains slash-like characters has been
|
|
18
|
+
// stripped already.
|
|
19
|
+
//
|
|
20
|
+
// Non-false-positive contract (verified by test/unit/error-redaction.test.js):
|
|
21
|
+
// - "Compare A:B" stays unchanged (no `\` or `/` after `:`)
|
|
22
|
+
// - "Modal: hello" stays unchanged (no `\` or `/` after `:`)
|
|
23
|
+
// - "Visit https://example.com/path" stays (lowercase scheme, no Drive: pattern)
|
|
24
|
+
// - "Bearer x" stays unchanged (1 char, below 20 minimum)
|
|
25
|
+
// - "sk-foo" stays unchanged (3 chars after sk-, below 20 minimum)
|
|
26
|
+
// - "see /etc/passwd" stays unchanged (etc not in {home,Users,root} allowlist)
|
|
27
|
+
//
|
|
28
|
+
// Idempotency: redactSecrets(redactSecrets(x)) === redactSecrets(x). The
|
|
29
|
+
// substitution strings ('[REDACTED:*]', '[PATH]', etc.) contain no characters
|
|
30
|
+
// that match any of the patterns themselves.
|
|
31
|
+
|
|
32
|
+
const PATTERNS = [
|
|
33
|
+
{ re: /sk-ant-[A-Za-z0-9_\-]{20,}/g, sub: '[REDACTED:anthropic_key]' },
|
|
34
|
+
{ re: /sk-[A-Za-z0-9]{20,}/g, sub: '[REDACTED:openai_key]' },
|
|
35
|
+
{ re: /x-api-key\s*:\s*[^\s,;'"]+/gi, sub: 'x-api-key: [REDACTED]' },
|
|
36
|
+
{ re: /Bearer\s+[A-Za-z0-9._\-]{20,}/gi, sub: 'Bearer [REDACTED]' },
|
|
37
|
+
{ re: /[A-Z]:[\\\/][^\s'"`<>]+/g, sub: '[PATH]' },
|
|
38
|
+
{ re: /\/(home|Users|root)\/[^\s'"`<>]+/g, sub: '[PATH]' },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Strip secrets and absolute filesystem paths from a string. Defensive: coerces
|
|
43
|
+
* non-string inputs via String(value); null/undefined return ''.
|
|
44
|
+
*
|
|
45
|
+
* @param {unknown} text
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
48
|
+
export function redactSecrets(text) {
|
|
49
|
+
if (text == null) return '';
|
|
50
|
+
let s = String(text);
|
|
51
|
+
for (const { re, sub } of PATTERNS) {
|
|
52
|
+
s = s.replace(re, sub);
|
|
53
|
+
}
|
|
54
|
+
return s;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Build the public MCP error envelope for an arbitrary thrown value. The
|
|
59
|
+
* server-side stderr keeps the full trace for operator debugging; the
|
|
60
|
+
* JSON-RPC client receives only `{error, code}` — no trace field is emitted.
|
|
61
|
+
*
|
|
62
|
+
* Preserves err.code when present (Phase 83.03 added `EMANIFESTMISMATCH`;
|
|
63
|
+
* downstream callers can keep dispatching on that code).
|
|
64
|
+
*
|
|
65
|
+
* @param {unknown} err
|
|
66
|
+
* @returns {{ error: string, code: string }}
|
|
67
|
+
*/
|
|
68
|
+
export function sanitizeMcpError(err) {
|
|
69
|
+
const msg = err && typeof err === 'object' && 'message' in err
|
|
70
|
+
? err.message
|
|
71
|
+
: err;
|
|
72
|
+
return {
|
|
73
|
+
error: redactSecrets(msg ?? 'unknown error'),
|
|
74
|
+
code: (err && typeof err === 'object' && err.code) ? err.code : 'MCP_INTERNAL_ERROR',
|
|
75
|
+
};
|
|
76
|
+
}
|
package/src/core/failures.js
CHANGED
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
// Failure dataset — closes the learning loop.
|
|
2
|
-
//
|
|
3
|
-
// Aggregates resolved debug sessions (`.planning/debug/resolved/*.md`),
|
|
4
|
-
// failed verifications (`.planning/phases/*/[0-9]*-VERIFICATION.md` with status: gaps_found),
|
|
5
|
-
// and forensics reports into a structured dataset that can be queried to find
|
|
6
|
-
// recurring failure patterns per agent.
|
|
7
|
-
//
|
|
8
|
-
// Output is written to `.planning/learnings/{agent}.md` so future agent edits
|
|
9
|
-
// have evidence-based input.
|
|
10
|
-
|
|
11
|
-
import path from 'node:path';
|
|
12
|
-
import fs from 'node:fs/promises';
|
|
13
|
-
|
|
14
|
-
export async function collectFailures(opts = {}) {
|
|
15
|
-
const projectRoot = path.resolve(opts.projectRoot ?? process.cwd());
|
|
16
|
-
const planning = path.join(projectRoot, '.planning');
|
|
17
|
-
|
|
18
|
-
const [debugFailures, verifyFailures, forensicsReports] = await Promise.all([
|
|
19
|
-
readDebugSessions(path.join(planning, 'debug', 'resolved')),
|
|
20
|
-
readFailedVerifications(path.join(planning, 'phases')),
|
|
21
|
-
readForensics(path.join(planning, 'forensics')),
|
|
22
|
-
]);
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
projectRoot,
|
|
26
|
-
counts: {
|
|
27
|
-
debug: debugFailures.length,
|
|
28
|
-
verify: verifyFailures.length,
|
|
29
|
-
forensics: forensicsReports.length,
|
|
30
|
-
},
|
|
31
|
-
items: [...debugFailures, ...verifyFailures, ...forensicsReports],
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function summarizeByAgent(failures) {
|
|
36
|
-
const byAgent = {};
|
|
37
|
-
for (const item of failures.items) {
|
|
38
|
-
const agent = item.agentHint ?? 'unknown';
|
|
39
|
-
byAgent[agent] ??= { agent, count: 0, samples: [] };
|
|
40
|
-
byAgent[agent].count++;
|
|
41
|
-
if (byAgent[agent].samples.length < 5) byAgent[agent].samples.push(item);
|
|
42
|
-
}
|
|
43
|
-
return Object.values(byAgent).sort((a, b) => b.count - a.count);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function writeLearnings(failures, opts = {}) {
|
|
47
|
-
const projectRoot = path.resolve(opts.projectRoot ?? process.cwd());
|
|
48
|
-
const outDir = path.join(projectRoot, '.planning', 'learnings');
|
|
49
|
-
await fs.mkdir(outDir, { recursive: true });
|
|
50
|
-
|
|
51
|
-
const summaries = await summarizeByAgent(failures);
|
|
52
|
-
const written = [];
|
|
53
|
-
for (const s of summaries) {
|
|
54
|
-
const out = path.join(outDir, `${s.agent}.md`);
|
|
55
|
-
const md = renderLearningDoc(s);
|
|
56
|
-
await fs.writeFile(out, md, 'utf8');
|
|
57
|
-
written.push(out);
|
|
58
|
-
}
|
|
59
|
-
return { written, summaries };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// --- readers ---
|
|
63
|
-
|
|
64
|
-
async function readDebugSessions(dir) {
|
|
65
|
-
return readDir(dir, raw => ({
|
|
66
|
-
source: 'debug',
|
|
67
|
-
agentHint: detectAgentHint(raw),
|
|
68
|
-
summary: firstHeading(raw),
|
|
69
|
-
raw: raw.slice(0, 2000),
|
|
70
|
-
}));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function readFailedVerifications(phasesDir) {
|
|
74
|
-
let out = [];
|
|
75
|
-
let phases;
|
|
76
|
-
try { phases = await fs.readdir(phasesDir, { withFileTypes: true }); }
|
|
77
|
-
catch { return out; }
|
|
78
|
-
for (const p of phases) {
|
|
79
|
-
if (!p.isDirectory()) continue;
|
|
80
|
-
const phaseDir = path.join(phasesDir, p.name);
|
|
81
|
-
let files;
|
|
82
|
-
try { files = await fs.readdir(phaseDir); } catch { continue; }
|
|
83
|
-
for (const f of files) {
|
|
84
|
-
if (!/-VERIFICATION\.md$/.test(f)) continue;
|
|
85
|
-
const raw = await fs.readFile(path.join(phaseDir, f), 'utf8');
|
|
86
|
-
if (!/^status:\s*gaps_found/m.test(raw)) continue;
|
|
87
|
-
out.push({
|
|
88
|
-
source: 'verify',
|
|
89
|
-
agentHint: 'verifier',
|
|
90
|
-
phase: p.name,
|
|
91
|
-
summary: firstHeading(raw),
|
|
92
|
-
raw: raw.slice(0, 2000),
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return out;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function readForensics(dir) {
|
|
100
|
-
return readDir(dir, raw => ({
|
|
101
|
-
source: 'forensics',
|
|
102
|
-
agentHint: detectAgentHint(raw),
|
|
103
|
-
summary: firstHeading(raw),
|
|
104
|
-
raw: raw.slice(0, 2000),
|
|
105
|
-
}));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function readDir(dir, mapper) {
|
|
109
|
-
let entries;
|
|
110
|
-
try { entries = await fs.readdir(dir, { withFileTypes: true }); }
|
|
111
|
-
catch { return []; }
|
|
112
|
-
const out = [];
|
|
113
|
-
for (const e of entries) {
|
|
114
|
-
if (!e.isFile() || !e.name.endsWith('.md')) continue;
|
|
115
|
-
const raw = await fs.readFile(path.join(dir, e.name), 'utf8');
|
|
116
|
-
out.push({ file: e.name, ...mapper(raw) });
|
|
117
|
-
}
|
|
118
|
-
return out;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function detectAgentHint(raw) {
|
|
122
|
-
for (const a of ['executor', 'verifier', 'planner', 'debugger', 'phase-researcher',
|
|
123
|
-
'plan-checker', 'integration-checker', 'nyquist-auditor', 'ui-checker']) {
|
|
124
|
-
if (raw.toLowerCase().includes(a)) return a;
|
|
125
|
-
}
|
|
126
|
-
return 'unknown';
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function firstHeading(raw) {
|
|
130
|
-
const m = raw.match(/^#+\s*(.+)$/m);
|
|
131
|
-
return m ? m[1].trim() : '';
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function renderLearningDoc(s) {
|
|
135
|
-
return `# Learnings — ${s.agent}
|
|
136
|
-
|
|
137
|
-
**Failure samples:** ${s.count}
|
|
138
|
-
**Generated:** ${new Date().toISOString()}
|
|
139
|
-
|
|
140
|
-
## Recurring patterns
|
|
141
|
-
|
|
142
|
-
> Review the samples below and edit \`kit/agents/${s.agent}.md\` to address recurring causes.
|
|
143
|
-
|
|
144
|
-
## Samples
|
|
145
|
-
|
|
146
|
-
${s.samples.map((x, i) => `### Sample ${i + 1} (${x.source})
|
|
147
|
-
${x.summary ? `*${x.summary}*\n` : ''}
|
|
148
|
-
\`\`\`
|
|
149
|
-
${x.raw.slice(0, 800)}
|
|
150
|
-
\`\`\`
|
|
151
|
-
`).join('\n---\n\n')}
|
|
152
|
-
`;
|
|
153
|
-
}
|
|
1
|
+
// Failure dataset — closes the learning loop.
|
|
2
|
+
//
|
|
3
|
+
// Aggregates resolved debug sessions (`.planning/debug/resolved/*.md`),
|
|
4
|
+
// failed verifications (`.planning/phases/*/[0-9]*-VERIFICATION.md` with status: gaps_found),
|
|
5
|
+
// and forensics reports into a structured dataset that can be queried to find
|
|
6
|
+
// recurring failure patterns per agent.
|
|
7
|
+
//
|
|
8
|
+
// Output is written to `.planning/learnings/{agent}.md` so future agent edits
|
|
9
|
+
// have evidence-based input.
|
|
10
|
+
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
13
|
+
|
|
14
|
+
export async function collectFailures(opts = {}) {
|
|
15
|
+
const projectRoot = path.resolve(opts.projectRoot ?? process.cwd());
|
|
16
|
+
const planning = path.join(projectRoot, '.planning');
|
|
17
|
+
|
|
18
|
+
const [debugFailures, verifyFailures, forensicsReports] = await Promise.all([
|
|
19
|
+
readDebugSessions(path.join(planning, 'debug', 'resolved')),
|
|
20
|
+
readFailedVerifications(path.join(planning, 'phases')),
|
|
21
|
+
readForensics(path.join(planning, 'forensics')),
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
projectRoot,
|
|
26
|
+
counts: {
|
|
27
|
+
debug: debugFailures.length,
|
|
28
|
+
verify: verifyFailures.length,
|
|
29
|
+
forensics: forensicsReports.length,
|
|
30
|
+
},
|
|
31
|
+
items: [...debugFailures, ...verifyFailures, ...forensicsReports],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function summarizeByAgent(failures) {
|
|
36
|
+
const byAgent = {};
|
|
37
|
+
for (const item of failures.items) {
|
|
38
|
+
const agent = item.agentHint ?? 'unknown';
|
|
39
|
+
byAgent[agent] ??= { agent, count: 0, samples: [] };
|
|
40
|
+
byAgent[agent].count++;
|
|
41
|
+
if (byAgent[agent].samples.length < 5) byAgent[agent].samples.push(item);
|
|
42
|
+
}
|
|
43
|
+
return Object.values(byAgent).sort((a, b) => b.count - a.count);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function writeLearnings(failures, opts = {}) {
|
|
47
|
+
const projectRoot = path.resolve(opts.projectRoot ?? process.cwd());
|
|
48
|
+
const outDir = path.join(projectRoot, '.planning', 'learnings');
|
|
49
|
+
await fs.mkdir(outDir, { recursive: true });
|
|
50
|
+
|
|
51
|
+
const summaries = await summarizeByAgent(failures);
|
|
52
|
+
const written = [];
|
|
53
|
+
for (const s of summaries) {
|
|
54
|
+
const out = path.join(outDir, `${s.agent}.md`);
|
|
55
|
+
const md = renderLearningDoc(s);
|
|
56
|
+
await fs.writeFile(out, md, 'utf8');
|
|
57
|
+
written.push(out);
|
|
58
|
+
}
|
|
59
|
+
return { written, summaries };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// --- readers ---
|
|
63
|
+
|
|
64
|
+
async function readDebugSessions(dir) {
|
|
65
|
+
return readDir(dir, raw => ({
|
|
66
|
+
source: 'debug',
|
|
67
|
+
agentHint: detectAgentHint(raw),
|
|
68
|
+
summary: firstHeading(raw),
|
|
69
|
+
raw: raw.slice(0, 2000),
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function readFailedVerifications(phasesDir) {
|
|
74
|
+
let out = [];
|
|
75
|
+
let phases;
|
|
76
|
+
try { phases = await fs.readdir(phasesDir, { withFileTypes: true }); }
|
|
77
|
+
catch { return out; }
|
|
78
|
+
for (const p of phases) {
|
|
79
|
+
if (!p.isDirectory()) continue;
|
|
80
|
+
const phaseDir = path.join(phasesDir, p.name);
|
|
81
|
+
let files;
|
|
82
|
+
try { files = await fs.readdir(phaseDir); } catch { continue; }
|
|
83
|
+
for (const f of files) {
|
|
84
|
+
if (!/-VERIFICATION\.md$/.test(f)) continue;
|
|
85
|
+
const raw = await fs.readFile(path.join(phaseDir, f), 'utf8');
|
|
86
|
+
if (!/^status:\s*gaps_found/m.test(raw)) continue;
|
|
87
|
+
out.push({
|
|
88
|
+
source: 'verify',
|
|
89
|
+
agentHint: 'verifier',
|
|
90
|
+
phase: p.name,
|
|
91
|
+
summary: firstHeading(raw),
|
|
92
|
+
raw: raw.slice(0, 2000),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function readForensics(dir) {
|
|
100
|
+
return readDir(dir, raw => ({
|
|
101
|
+
source: 'forensics',
|
|
102
|
+
agentHint: detectAgentHint(raw),
|
|
103
|
+
summary: firstHeading(raw),
|
|
104
|
+
raw: raw.slice(0, 2000),
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function readDir(dir, mapper) {
|
|
109
|
+
let entries;
|
|
110
|
+
try { entries = await fs.readdir(dir, { withFileTypes: true }); }
|
|
111
|
+
catch { return []; }
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const e of entries) {
|
|
114
|
+
if (!e.isFile() || !e.name.endsWith('.md')) continue;
|
|
115
|
+
const raw = await fs.readFile(path.join(dir, e.name), 'utf8');
|
|
116
|
+
out.push({ file: e.name, ...mapper(raw) });
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function detectAgentHint(raw) {
|
|
122
|
+
for (const a of ['executor', 'verifier', 'planner', 'debugger', 'phase-researcher',
|
|
123
|
+
'plan-checker', 'integration-checker', 'nyquist-auditor', 'ui-checker']) {
|
|
124
|
+
if (raw.toLowerCase().includes(a)) return a;
|
|
125
|
+
}
|
|
126
|
+
return 'unknown';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function firstHeading(raw) {
|
|
130
|
+
const m = raw.match(/^#+\s*(.+)$/m);
|
|
131
|
+
return m ? m[1].trim() : '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function renderLearningDoc(s) {
|
|
135
|
+
return `# Learnings — ${s.agent}
|
|
136
|
+
|
|
137
|
+
**Failure samples:** ${s.count}
|
|
138
|
+
**Generated:** ${new Date().toISOString()}
|
|
139
|
+
|
|
140
|
+
## Recurring patterns
|
|
141
|
+
|
|
142
|
+
> Review the samples below and edit \`kit/agents/${s.agent}.md\` to address recurring causes.
|
|
143
|
+
|
|
144
|
+
## Samples
|
|
145
|
+
|
|
146
|
+
${s.samples.map((x, i) => `### Sample ${i + 1} (${x.source})
|
|
147
|
+
${x.summary ? `*${x.summary}*\n` : ''}
|
|
148
|
+
\`\`\`
|
|
149
|
+
${x.raw.slice(0, 800)}
|
|
150
|
+
\`\`\`
|
|
151
|
+
`).join('\n---\n\n')}
|
|
152
|
+
`;
|
|
153
|
+
}
|