@agentic-sdlc-kit/cli 0.9.1 → 1.1.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.
Files changed (149) hide show
  1. package/dist/application/agent/orchestrator.d.ts.map +1 -1
  2. package/dist/application/agent/orchestrator.js +4 -3
  3. package/dist/application/agent/orchestrator.js.map +1 -1
  4. package/dist/application/agent/providers/claude-code/permission-config.d.ts.map +1 -1
  5. package/dist/application/agent/providers/claude-code/permission-config.js +8 -2
  6. package/dist/application/agent/providers/claude-code/permission-config.js.map +1 -1
  7. package/dist/application/agent-tools/dispatch.d.ts +12 -0
  8. package/dist/application/agent-tools/dispatch.d.ts.map +1 -0
  9. package/dist/application/agent-tools/dispatch.js +189 -0
  10. package/dist/application/agent-tools/dispatch.js.map +1 -0
  11. package/dist/application/agent-tools/mcp-server.d.ts +10 -0
  12. package/dist/application/agent-tools/mcp-server.d.ts.map +1 -0
  13. package/dist/application/agent-tools/mcp-server.js +100 -0
  14. package/dist/application/agent-tools/mcp-server.js.map +1 -0
  15. package/dist/application/bench/harness.d.ts +5 -0
  16. package/dist/application/bench/harness.d.ts.map +1 -0
  17. package/dist/application/bench/harness.js +82 -0
  18. package/dist/application/bench/harness.js.map +1 -0
  19. package/dist/application/dispatcher.d.ts.map +1 -1
  20. package/dist/application/dispatcher.js +24 -0
  21. package/dist/application/dispatcher.js.map +1 -1
  22. package/dist/application/rule-adapters/ast-grep.d.ts +3 -0
  23. package/dist/application/rule-adapters/ast-grep.d.ts.map +1 -0
  24. package/dist/application/rule-adapters/ast-grep.js +112 -0
  25. package/dist/application/rule-adapters/ast-grep.js.map +1 -0
  26. package/dist/application/rule-adapters/index.d.ts +3 -0
  27. package/dist/application/rule-adapters/index.d.ts.map +1 -0
  28. package/dist/application/rule-adapters/index.js +15 -0
  29. package/dist/application/rule-adapters/index.js.map +1 -0
  30. package/dist/application/rule-adapters/linter.d.ts +31 -0
  31. package/dist/application/rule-adapters/linter.d.ts.map +1 -0
  32. package/dist/application/rule-adapters/linter.js +242 -0
  33. package/dist/application/rule-adapters/linter.js.map +1 -0
  34. package/dist/application/rule-adapters/process.d.ts +10 -0
  35. package/dist/application/rule-adapters/process.d.ts.map +1 -0
  36. package/dist/application/rule-adapters/process.js +38 -0
  37. package/dist/application/rule-adapters/process.js.map +1 -0
  38. package/dist/application/version.d.ts +1 -1
  39. package/dist/application/version.d.ts.map +1 -1
  40. package/dist/application/version.js +7 -4
  41. package/dist/application/version.js.map +1 -1
  42. package/dist/commands/bench.d.ts +3 -0
  43. package/dist/commands/bench.d.ts.map +1 -0
  44. package/dist/commands/bench.js +75 -0
  45. package/dist/commands/bench.js.map +1 -0
  46. package/dist/commands/context.d.ts +3 -0
  47. package/dist/commands/context.d.ts.map +1 -0
  48. package/dist/commands/context.js +56 -0
  49. package/dist/commands/context.js.map +1 -0
  50. package/dist/commands/doctor.d.ts.map +1 -1
  51. package/dist/commands/doctor.js +38 -0
  52. package/dist/commands/doctor.js.map +1 -1
  53. package/dist/commands/feature.d.ts.map +1 -1
  54. package/dist/commands/feature.js +48 -6
  55. package/dist/commands/feature.js.map +1 -1
  56. package/dist/commands/index.d.ts +8 -0
  57. package/dist/commands/index.d.ts.map +1 -1
  58. package/dist/commands/index.js +8 -0
  59. package/dist/commands/index.js.map +1 -1
  60. package/dist/commands/install.d.ts.map +1 -1
  61. package/dist/commands/install.js +7 -4
  62. package/dist/commands/install.js.map +1 -1
  63. package/dist/commands/knowledge.d.ts +3 -0
  64. package/dist/commands/knowledge.d.ts.map +1 -0
  65. package/dist/commands/knowledge.js +59 -0
  66. package/dist/commands/knowledge.js.map +1 -0
  67. package/dist/commands/learning.d.ts +3 -0
  68. package/dist/commands/learning.d.ts.map +1 -0
  69. package/dist/commands/learning.js +126 -0
  70. package/dist/commands/learning.js.map +1 -0
  71. package/dist/commands/mcp.d.ts +3 -0
  72. package/dist/commands/mcp.d.ts.map +1 -0
  73. package/dist/commands/mcp.js +26 -0
  74. package/dist/commands/mcp.js.map +1 -0
  75. package/dist/commands/next.d.ts.map +1 -1
  76. package/dist/commands/next.js +3 -1
  77. package/dist/commands/next.js.map +1 -1
  78. package/dist/commands/onboard.d.ts +5 -0
  79. package/dist/commands/onboard.d.ts.map +1 -0
  80. package/dist/commands/onboard.js +137 -0
  81. package/dist/commands/onboard.js.map +1 -0
  82. package/dist/commands/routing.d.ts +3 -0
  83. package/dist/commands/routing.d.ts.map +1 -0
  84. package/dist/commands/routing.js +107 -0
  85. package/dist/commands/routing.js.map +1 -0
  86. package/dist/commands/rules-format.d.ts +4 -0
  87. package/dist/commands/rules-format.d.ts.map +1 -0
  88. package/dist/commands/rules-format.js +16 -0
  89. package/dist/commands/rules-format.js.map +1 -0
  90. package/dist/commands/rules.d.ts +3 -0
  91. package/dist/commands/rules.d.ts.map +1 -0
  92. package/dist/commands/rules.js +270 -0
  93. package/dist/commands/rules.js.map +1 -0
  94. package/dist/commands/verify.d.ts.map +1 -1
  95. package/dist/commands/verify.js +78 -6
  96. package/dist/commands/verify.js.map +1 -1
  97. package/package.json +11 -5
  98. package/src/application/agent/orchestrator.test.ts +39 -0
  99. package/src/application/agent/orchestrator.ts +6 -0
  100. package/src/application/agent/providers/claude-code/claude-code-smoke.test.ts +1 -1
  101. package/src/application/agent/providers/claude-code/permission-config.test.ts +4 -4
  102. package/src/application/agent/providers/claude-code/permission-config.ts +8 -2
  103. package/src/application/agent/providers/claude-code/provider.test.ts +2 -2
  104. package/src/application/agent-tools/dispatch.test.ts +160 -0
  105. package/src/application/agent-tools/dispatch.ts +227 -0
  106. package/src/application/agent-tools/mcp-server.test.ts +86 -0
  107. package/src/application/agent-tools/mcp-server.ts +126 -0
  108. package/src/application/bench/harness.ts +95 -0
  109. package/src/application/dispatcher.test.ts +4 -3
  110. package/src/application/dispatcher.ts +16 -0
  111. package/src/application/rule-adapters/adapters.test.ts +224 -0
  112. package/src/application/rule-adapters/ast-grep.ts +126 -0
  113. package/src/application/rule-adapters/index.ts +16 -0
  114. package/src/application/rule-adapters/linter-paths.test.ts +95 -0
  115. package/src/application/rule-adapters/linter.ts +263 -0
  116. package/src/application/rule-adapters/process.ts +48 -0
  117. package/src/application/version.ts +9 -4
  118. package/src/commands/bench.test.ts +136 -0
  119. package/src/commands/bench.ts +74 -0
  120. package/src/commands/context.test.ts +47 -0
  121. package/src/commands/context.ts +59 -0
  122. package/src/commands/doctor-isolation.test.ts +76 -0
  123. package/src/commands/doctor.test.ts +2 -1
  124. package/src/commands/doctor.ts +36 -0
  125. package/src/commands/evolve.test.ts +153 -0
  126. package/src/commands/feature.test.ts +36 -4
  127. package/src/commands/feature.ts +71 -8
  128. package/src/commands/index.ts +8 -0
  129. package/src/commands/install.ts +7 -4
  130. package/src/commands/journal.test.ts +150 -0
  131. package/src/commands/knowledge.test.ts +39 -0
  132. package/src/commands/knowledge.ts +63 -0
  133. package/src/commands/learning.test.ts +104 -0
  134. package/src/commands/learning.ts +134 -0
  135. package/src/commands/mcp.ts +30 -0
  136. package/src/commands/memory.test.ts +146 -0
  137. package/src/commands/next.ts +3 -1
  138. package/src/commands/onboard.test.ts +83 -0
  139. package/src/commands/onboard.ts +146 -0
  140. package/src/commands/optimize.test.ts +53 -0
  141. package/src/commands/refactor.test.ts +57 -0
  142. package/src/commands/routing.test.ts +64 -0
  143. package/src/commands/routing.ts +107 -0
  144. package/src/commands/rules-format.ts +22 -0
  145. package/src/commands/rules.test.ts +153 -0
  146. package/src/commands/rules.ts +286 -0
  147. package/src/commands/verify.test.ts +115 -0
  148. package/src/commands/verify.ts +95 -7
  149. package/src/commands/version.test.ts +9 -0
@@ -0,0 +1,150 @@
1
+ // packages/cli/src/commands/journal.test.ts
2
+ //
3
+ // CLI-layer tests for `kit journal` argument handling and output
4
+ // formatting (the chain-integration paths — read/verify/tamper on a
5
+ // driven feature run — are covered in next.test.ts). This file covers
6
+ // --help, the missing-identifier validation, empty journals, --limit
7
+ // truncation, and the --verify --json machine surface.
8
+ import { promises as fs } from 'node:fs';
9
+ import os from 'node:os';
10
+ import path from 'node:path';
11
+ import { describe, expect, it } from 'vitest';
12
+ import { appendJournalEvent } from '@agentic-sdlc-kit/core';
13
+ import { journalCommand } from './journal.js';
14
+ import type { CommandContext } from './types.js';
15
+
16
+ const makeCtx = (
17
+ argv: string[],
18
+ cwd: string,
19
+ ): CommandContext & { out: string[]; err: string[] } => {
20
+ const out: string[] = [];
21
+ const err: string[] = [];
22
+ return {
23
+ argv,
24
+ cwd,
25
+ stdout: (s) => void out.push(s),
26
+ stderr: (s) => void err.push(s),
27
+ out,
28
+ err,
29
+ };
30
+ };
31
+
32
+ async function tmpRoot(): Promise<string> {
33
+ return fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-journal-'));
34
+ }
35
+
36
+ async function seedEvents(root: string, featureId: string, count: number): Promise<void> {
37
+ for (let i = 0; i < count; i++) {
38
+ const res = await appendJournalEvent(root, {
39
+ featureId,
40
+ kind: 'workflow.started',
41
+ actor: { kind: 'kit' },
42
+ payload: { attempt: i },
43
+ });
44
+ expect(res.ok).toBe(true);
45
+ }
46
+ }
47
+
48
+ describe('kit journal (argument handling)', () => {
49
+ it('shows usage with --help and exits 0', async () => {
50
+ const ctx = makeCtx(['--help'], await tmpRoot());
51
+ const result = await journalCommand.run(ctx);
52
+ expect(result.exitCode).toBe(0);
53
+ expect(ctx.out.join('')).toContain('Usage: kit journal <feature-id>');
54
+ });
55
+
56
+ it('fails (exit 1) when the feature identifier is missing', async () => {
57
+ const ctx = makeCtx([], await tmpRoot());
58
+ const result = await journalCommand.run(ctx);
59
+ expect(result.exitCode).toBe(1);
60
+ expect(ctx.err.join('')).toContain('missing required feature identifier');
61
+ });
62
+ });
63
+
64
+ describe('kit journal (reading)', () => {
65
+ it('lists seeded events in human form', async () => {
66
+ const root = await tmpRoot();
67
+ await seedEvents(root, 'FEATURE-001', 2);
68
+ const ctx = makeCtx(['FEATURE-001'], root);
69
+ const result = await journalCommand.run(ctx);
70
+ expect(result.exitCode).toBe(0);
71
+ const text = ctx.out.join('');
72
+ expect(text).toContain('[0]');
73
+ expect(text).toContain('[1]');
74
+ expect(text).toContain('workflow.started');
75
+ });
76
+
77
+ it('emits machine-readable events with --json', async () => {
78
+ const root = await tmpRoot();
79
+ await seedEvents(root, 'FEATURE-001', 2);
80
+ const ctx = makeCtx(['FEATURE-001', '--json'], root);
81
+ const result = await journalCommand.run(ctx);
82
+ expect(result.exitCode).toBe(0);
83
+ const events = JSON.parse(ctx.out.join('')) as Array<{ seq: number; kind: string }>;
84
+ expect(events).toHaveLength(2);
85
+ expect(events[0]?.seq).toBe(0);
86
+ expect(events[1]?.kind).toBe('workflow.started');
87
+ });
88
+
89
+ it('hides earlier events with --limit N', async () => {
90
+ const root = await tmpRoot();
91
+ await seedEvents(root, 'FEATURE-001', 5);
92
+ const ctx = makeCtx(['FEATURE-001', '--limit', '2'], root);
93
+ const result = await journalCommand.run(ctx);
94
+ expect(result.exitCode).toBe(0);
95
+ const text = ctx.out.join('');
96
+ expect(text).toContain('3 earlier event(s) hidden');
97
+ expect(text).toContain('[4]');
98
+ expect(text).not.toContain('[0]');
99
+ });
100
+
101
+ it('reports an empty journal honestly', async () => {
102
+ const root = await tmpRoot();
103
+ const file = path.join(root, '.ai-kit', 'journal', 'feature-runs', 'FEATURE-001.jsonl');
104
+ await fs.mkdir(path.dirname(file), { recursive: true });
105
+ await fs.writeFile(file, '', 'utf8');
106
+ const ctx = makeCtx(['FEATURE-001'], root);
107
+ const result = await journalCommand.run(ctx);
108
+ expect(result.exitCode).toBe(0);
109
+ expect(ctx.out.join('')).toContain('is empty');
110
+ });
111
+
112
+ it('fails (exit 1) when the journal does not exist', async () => {
113
+ const ctx = makeCtx(['FEATURE-404'], await tmpRoot());
114
+ const result = await journalCommand.run(ctx);
115
+ expect(result.exitCode).toBe(1);
116
+ expect(ctx.err.join('')).toContain('No journal found');
117
+ });
118
+ });
119
+
120
+ describe('kit journal (verification)', () => {
121
+ it('verifies a seeded chain (exit 0, human form)', async () => {
122
+ const root = await tmpRoot();
123
+ await seedEvents(root, 'FEATURE-001', 3);
124
+ const ctx = makeCtx(['FEATURE-001', '--verify'], root);
125
+ const result = await journalCommand.run(ctx);
126
+ expect(result.exitCode).toBe(0);
127
+ const text = ctx.out.join('');
128
+ expect(text).toContain('Journal verified for FEATURE-001: 3 event(s)');
129
+ });
130
+
131
+ it('emits the verification result as JSON with --verify --json', async () => {
132
+ const root = await tmpRoot();
133
+ await seedEvents(root, 'FEATURE-001', 2);
134
+ const ctx = makeCtx(['FEATURE-001', '--verify', '--json'], root);
135
+ const result = await journalCommand.run(ctx);
136
+ expect(result.exitCode).toBe(0);
137
+ const payload = JSON.parse(ctx.out.join('')) as {
138
+ featureId: string;
139
+ valid: boolean;
140
+ eventCount: number;
141
+ lastHash?: string;
142
+ };
143
+ expect(payload.featureId).toBe('FEATURE-001');
144
+ expect(payload.valid).toBe(true);
145
+ expect(payload.eventCount).toBe(2);
146
+ if (payload.eventCount > 0) {
147
+ expect(payload.lastHash).toMatch(/^[0-9a-f]{64}$/);
148
+ }
149
+ });
150
+ });
@@ -0,0 +1,39 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
5
+ import { knowledgeCommand } from './knowledge.js';
6
+ import type { CommandContext } from './types.js';
7
+
8
+ function ctx(argv: string[], cwd: string): CommandContext & { out: string[]; err: string[] } {
9
+ const out: string[] = [];
10
+ const err: string[] = [];
11
+ return { argv, cwd, stdout: (s) => void out.push(s), stderr: (s) => void err.push(s), out, err };
12
+ }
13
+
14
+ let root: string;
15
+ beforeEach(async () => {
16
+ root = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-knowledge-'));
17
+ });
18
+ afterEach(async () => {
19
+ await fs.rm(root, { recursive: true, force: true });
20
+ });
21
+
22
+ describe('kit knowledge export', () => {
23
+ it('refuses to run without --dry-run: there is no transport', async () => {
24
+ const c = ctx(['export', '--namespace', 'tooling'], root);
25
+ expect((await knowledgeCommand.run(c)).exitCode).toBe(2);
26
+ expect(c.err.join('')).toContain('only --dry-run exists');
27
+ });
28
+
29
+ it('requires a namespace', async () => {
30
+ expect((await knowledgeCommand.run(ctx(['export', '--dry-run'], root))).exitCode).toBe(2);
31
+ });
32
+
33
+ it('a dry run with no constitution shares nothing and writes nothing', async () => {
34
+ const c = ctx(['export', '--dry-run', '--namespace', 'tooling'], root);
35
+ expect((await knowledgeCommand.run(c)).exitCode).toBe(0);
36
+ expect(c.out.join('')).toContain('nothing written, nothing sent');
37
+ await expect(fs.access(path.join(root, '.ai-kit', 'knowledge'))).rejects.toThrow();
38
+ });
39
+ });
@@ -0,0 +1,63 @@
1
+ // packages/cli/src/commands/knowledge.ts
2
+ //
3
+ // `kit knowledge export --dry-run --namespace <name> [--json]` (ADR-0027). Shows which rules this
4
+ // repository would share, after the secret scan. Only --dry-run exists: there is no transport, and
5
+ // nothing is pushed or written.
6
+
7
+ import path from 'node:path';
8
+ import { knowledgeExportDryRun } from '@agentic-sdlc-kit/core';
9
+ import type { Command, CommandContext, CommandResult } from './types.js';
10
+
11
+ const HELP = `Usage: kit knowledge export --dry-run --namespace <name> [--json]
12
+
13
+ Shows the rules this repository would share, after a secret scan. Only rules marked
14
+ shareable: true are considered. Nothing is written and nothing is sent: the transport is not built.
15
+ `;
16
+
17
+ function flag(argv: readonly string[], name: string): string | undefined {
18
+ const i = argv.indexOf(name);
19
+ return i >= 0 ? argv[i + 1] : undefined;
20
+ }
21
+
22
+ export const knowledgeCommand: Command = {
23
+ name: 'knowledge',
24
+ summary: 'Show the rules this repository would share, dry run only (ADR-0027).',
25
+ async run(ctx: CommandContext): Promise<CommandResult> {
26
+ if (ctx.argv.includes('--help') || ctx.argv.includes('-h') || ctx.argv.length === 0) {
27
+ ctx.stdout(HELP);
28
+ return { exitCode: 0 };
29
+ }
30
+ const sub = ctx.argv.find((a) => !a.startsWith('-'));
31
+ if (sub !== 'export') {
32
+ ctx.stderr(`kit knowledge: unknown subcommand "${sub ?? ''}"\n${HELP}`);
33
+ return { exitCode: 2, message: 'unknown subcommand' };
34
+ }
35
+ if (!ctx.argv.includes('--dry-run')) {
36
+ ctx.stderr('kit knowledge export: only --dry-run exists; the transport is not built (ADR-0027)\n');
37
+ return { exitCode: 2, message: 'dry run required' };
38
+ }
39
+ const namespace = flag(ctx.argv, '--namespace');
40
+ if (namespace === undefined) {
41
+ ctx.stderr('kit knowledge export: --namespace <name> is required\n');
42
+ return { exitCode: 2, message: 'namespace required' };
43
+ }
44
+ const res = await knowledgeExportDryRun(path.resolve(ctx.cwd), namespace);
45
+ if (!res.ok) {
46
+ ctx.stderr(`kit knowledge export: ${res.error.message}\n`);
47
+ return { exitCode: 1, message: res.error.message };
48
+ }
49
+ if (ctx.argv.includes('--json')) {
50
+ ctx.stdout(`${JSON.stringify(res.value, null, 2)}\n`);
51
+ return { exitCode: 0 };
52
+ }
53
+ const m = res.value;
54
+ const lines = [
55
+ `Dry run for namespace ${m.namespace}: nothing written, nothing sent.`,
56
+ `Scanned ${m.scanned} rule(s): ${m.shared.length} shareable, ${m.keptLocal} kept local, ${m.refused.length} refused by the secret scan.`,
57
+ ...m.shared.map((s) => ` share ${s.id} ${s.title}`),
58
+ ...m.refused.map((r) => ` refuse ${r.id} ${r.reason}`),
59
+ ];
60
+ ctx.stdout(`${lines.join('\n')}\n`);
61
+ return { exitCode: 0 };
62
+ },
63
+ };
@@ -0,0 +1,104 @@
1
+ // kit learning: record from a file, propose candidates, status, and a non-destructive migration.
2
+ import { promises as fs } from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
6
+ import { learningCommand } from './learning.js';
7
+ import type { CommandContext } from './types.js';
8
+
9
+ function ctx(argv: string[], cwd: string): CommandContext & { out: string[]; err: string[] } {
10
+ const out: string[] = [];
11
+ const err: string[] = [];
12
+ return { argv, cwd, stdout: (s) => void out.push(s), stderr: (s) => void err.push(s), out, err };
13
+ }
14
+
15
+ const event = (runId: string) => ({
16
+ schemaVersion: 1,
17
+ id: `ev-${runId}`,
18
+ occurredAt: '2026-11-28T10:00:00.000Z',
19
+ runId,
20
+ taskType: 'feature',
21
+ outcome: 'failed',
22
+ stacks: ['node-typescript'],
23
+ affectedFiles: ['packages/core/src/a.ts'],
24
+ ruleViolations: [{ ruleId: 'ARCH-CORE-001', path: 'packages/core/src/a.ts' }],
25
+ verification: { passed: 1, failed: 1, skipped: 0 },
26
+ durationMs: 10,
27
+ });
28
+
29
+ let root: string;
30
+ beforeEach(async () => {
31
+ root = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-learning-'));
32
+ });
33
+ afterEach(async () => {
34
+ await fs.rm(root, { recursive: true, force: true });
35
+ });
36
+
37
+ async function recordRun(runId: string): Promise<void> {
38
+ const file = path.join(root, `${runId}.json`);
39
+ await fs.writeFile(file, JSON.stringify(event(runId)));
40
+ expect((await learningCommand.run(ctx(['record', '--file', file], root))).exitCode).toBe(0);
41
+ }
42
+
43
+ describe('kit learning', () => {
44
+ it('record refuses an event with free text and writes nothing', async () => {
45
+ const file = path.join(root, 'bad.json');
46
+ await fs.writeFile(file, JSON.stringify({ ...event('r'), prompt: 'the user said' }));
47
+ const c = ctx(['record', '--file', file], root);
48
+ expect((await learningCommand.run(c)).exitCode).toBe(1);
49
+ expect(c.err.join('')).toContain('validation');
50
+ });
51
+
52
+ it('propose adds candidates only after the recurrence threshold, and status counts them', async () => {
53
+ await recordRun('run-1');
54
+ await recordRun('run-2');
55
+ const early = ctx(['propose'], root);
56
+ await learningCommand.run(early);
57
+ expect(early.out.join('')).toContain('candidates added: 0');
58
+ await recordRun('run-3');
59
+ const p = ctx(['propose'], root);
60
+ await learningCommand.run(p);
61
+ expect(p.out.join('')).toContain('candidates added: 1');
62
+ const s = ctx(['status'], root);
63
+ await learningCommand.run(s);
64
+ expect(s.out.join('')).toContain('events: 3');
65
+ expect(s.out.join('')).toContain('candidates: 1');
66
+ });
67
+
68
+ it('a second propose adds nothing and does not extend a candidate’s expiry', async () => {
69
+ for (const r of ['run-1', 'run-2', 'run-3']) await recordRun(r);
70
+ await learningCommand.run(ctx(['propose'], root));
71
+ const file = path.join(root, '.ai-kit', 'state', 'engineering-memory.json');
72
+ const first = await fs.readFile(file, 'utf8');
73
+ const again = ctx(['propose'], root);
74
+ await learningCommand.run(again);
75
+ expect(again.out.join('')).toContain('candidates added: 0');
76
+ expect(await fs.readFile(file, 'utf8')).toBe(first);
77
+ });
78
+
79
+ it('migrate-memory copies legacy entries, keeps the original file, and is idempotent', async () => {
80
+ const legacyDir = path.join(root, '.ai-kit', 'state');
81
+ await fs.mkdir(legacyDir, { recursive: true });
82
+ const legacy = path.join(legacyDir, 'memory.json');
83
+ const original = JSON.stringify({
84
+ schemaVersion: 1,
85
+ entries: [{
86
+ id: 'mem-1', type: 'semantic', domain: 'billing', summary: 's', content: 'c', confidence: 0.7,
87
+ provenance: ['run-9'], appliedCount: 2, successCount: 1, createdAt: '2026-10-01T00:00:00.000Z', updatedAt: '2026-10-01T00:00:00.000Z',
88
+ }],
89
+ });
90
+ await fs.writeFile(legacy, original);
91
+ const first = ctx(['migrate-memory'], root);
92
+ expect((await learningCommand.run(first)).exitCode).toBe(0);
93
+ expect(first.out.join('')).toContain('migrated 1 of 1');
94
+ expect(first.out.join('')).toContain('lossy:');
95
+ expect(await fs.readFile(legacy, 'utf8')).toBe(original);
96
+ const second = ctx(['migrate-memory'], root);
97
+ await learningCommand.run(second);
98
+ expect(second.out.join('')).toContain('migrated 0 of 1');
99
+ });
100
+
101
+ it('an unknown subcommand is a usage error', async () => {
102
+ expect((await learningCommand.run(ctx(['frobnicate'], root))).exitCode).toBe(2);
103
+ });
104
+ });
@@ -0,0 +1,134 @@
1
+ // packages/cli/src/commands/learning.ts
2
+ //
3
+ // `kit learning record|propose|status|migrate-memory` (platform roadmap Phase 6, ADR-0025).
4
+ // record append one privacy-safe LearningEvent from a JSON file
5
+ // propose distill recurring rule violations into memory candidates (never rules)
6
+ // status counts of events and candidates, including expired ones
7
+ // migrate-memory copy legacy MemoryEntry records into the canonical file; keeps the original
8
+
9
+ import { promises as fs } from 'node:fs';
10
+ import path from 'node:path';
11
+ import {
12
+ appendLearningEvent,
13
+ distillCandidates,
14
+ expiredCandidates,
15
+ migrateMemoryEntries,
16
+ readEngineeringMemory,
17
+ readLearningEvents,
18
+ writeEngineeringMemory,
19
+ } from '@agentic-sdlc-kit/core';
20
+ import type { EngineeringMemoryEntry, MemoryEntry } from '@agentic-sdlc-kit/contracts';
21
+ import type { Command, CommandContext, CommandResult } from './types.js';
22
+
23
+ const HELP = `Usage: kit learning <record|propose|status|migrate-memory>
24
+
25
+ record --file <event.json> Append one learning event (no free text is accepted).
26
+ propose Propose memory candidates from recurring rule violations.
27
+ status Show event and candidate counts, including expired candidates.
28
+ migrate-memory Copy legacy memory into the canonical file; the original is kept.
29
+ `;
30
+
31
+ function flag(argv: readonly string[], name: string): string | undefined {
32
+ const i = argv.indexOf(name);
33
+ return i >= 0 ? argv[i + 1] : undefined;
34
+ }
35
+
36
+ async function record(ctx: CommandContext, root: string): Promise<CommandResult> {
37
+ const file = flag(ctx.argv, '--file');
38
+ if (file === undefined) return fail(ctx, 'record needs --file <event.json>', 2);
39
+ let input: unknown;
40
+ try {
41
+ input = JSON.parse(await fs.readFile(path.resolve(ctx.cwd, file), 'utf8'));
42
+ } catch (cause) {
43
+ return fail(ctx, `cannot read ${file} as JSON: ${cause instanceof Error ? cause.message : String(cause)}`);
44
+ }
45
+ const res = await appendLearningEvent(root, input);
46
+ if (!res.ok) return fail(ctx, res.error.message);
47
+ ctx.stdout(`recorded ${res.value.id}\n`);
48
+ return { exitCode: 0 };
49
+ }
50
+
51
+ async function propose(ctx: CommandContext, root: string): Promise<CommandResult> {
52
+ const events = await readLearningEvents(root);
53
+ if (!events.ok) return fail(ctx, events.error.message);
54
+ const existing = await readEngineeringMemory(root);
55
+ if (!existing.ok) return fail(ctx, existing.error.message);
56
+ const known = new Set(existing.value.map((e) => e.id));
57
+ const fresh = distillCandidates(events.value, new Date()).filter((c) => !known.has(c.id));
58
+ const merged: EngineeringMemoryEntry[] = [...existing.value, ...fresh];
59
+ const written = await writeEngineeringMemory(root, merged);
60
+ if (!written.ok) return fail(ctx, written.error.message);
61
+ const expired = expiredCandidates(merged, new Date());
62
+ ctx.stdout(`candidates added: ${fresh.length}; expired and unreviewed: ${expired.length} (kept, never deleted)\n`);
63
+ return { exitCode: 0 };
64
+ }
65
+
66
+ async function status(ctx: CommandContext, root: string): Promise<CommandResult> {
67
+ const events = await readLearningEvents(root);
68
+ if (!events.ok) return fail(ctx, events.error.message);
69
+ const memory = await readEngineeringMemory(root);
70
+ if (!memory.ok) return fail(ctx, memory.error.message);
71
+ const candidates = memory.value.filter((e) => e.tags.includes('candidate'));
72
+ const reviewed = candidates.filter((e) => e.tags.includes('reviewed')).length;
73
+ const expired = expiredCandidates(memory.value, new Date()).length;
74
+ ctx.stdout(
75
+ `events: ${events.value.length}\ncandidates: ${candidates.length} (reviewed ${reviewed}, expired unreviewed ${expired})\n`,
76
+ );
77
+ return { exitCode: 0 };
78
+ }
79
+
80
+ async function migrateMemory(ctx: CommandContext, root: string): Promise<CommandResult> {
81
+ // The legacy store has always been written under <root>/.ai-kit, independent of KIT_STATE_DIR.
82
+ const legacyFile = path.join(root, '.ai-kit', 'state', 'memory.json');
83
+ let legacy: MemoryEntry[];
84
+ try {
85
+ const data = JSON.parse(await fs.readFile(legacyFile, 'utf8')) as { entries?: MemoryEntry[] };
86
+ legacy = Array.isArray(data.entries) ? data.entries : [];
87
+ } catch (cause) {
88
+ if ((cause as { code?: string }).code === 'ENOENT') {
89
+ ctx.stdout('no legacy memory to migrate\n');
90
+ return { exitCode: 0 };
91
+ }
92
+ return fail(ctx, `cannot read the legacy memory file: ${cause instanceof Error ? cause.message : String(cause)}`);
93
+ }
94
+ const existing = await readEngineeringMemory(root);
95
+ if (!existing.ok) return fail(ctx, existing.error.message);
96
+ const known = new Set(existing.value.map((e) => e.id));
97
+ const { entries, lossy } = migrateMemoryEntries(legacy);
98
+ const added = entries.filter((e) => !known.has(e.id));
99
+ const written = await writeEngineeringMemory(root, [...existing.value, ...added]);
100
+ if (!written.ok) return fail(ctx, written.error.message);
101
+ ctx.stdout(`migrated ${added.length} of ${legacy.length} legacy entr${legacy.length === 1 ? 'y' : 'ies'}; the original is kept\n`);
102
+ for (const line of lossy) ctx.stdout(` lossy: ${line}\n`);
103
+ return { exitCode: 0 };
104
+ }
105
+
106
+ function fail(ctx: CommandContext, message: string, exitCode = 1): CommandResult {
107
+ ctx.stderr(`kit learning: ${message}\n`);
108
+ return { exitCode, message };
109
+ }
110
+
111
+ export const learningCommand: Command = {
112
+ name: 'learning',
113
+ summary: 'Record outcomes, propose memory candidates, and migrate legacy memory (ADR-0025).',
114
+ async run(ctx: CommandContext): Promise<CommandResult> {
115
+ const sub = ctx.argv.find((a) => !a.startsWith('-'));
116
+ if (ctx.argv.includes('--help') || ctx.argv.includes('-h') || sub === undefined) {
117
+ ctx.stdout(HELP);
118
+ return { exitCode: sub === undefined && !ctx.argv.includes('--help') ? 2 : 0 };
119
+ }
120
+ const root = path.resolve(ctx.cwd);
121
+ switch (sub) {
122
+ case 'record':
123
+ return record(ctx, root);
124
+ case 'propose':
125
+ return propose(ctx, root);
126
+ case 'status':
127
+ return status(ctx, root);
128
+ case 'migrate-memory':
129
+ return migrateMemory(ctx, root);
130
+ default:
131
+ return fail(ctx, `unknown subcommand "${sub}"\n${HELP}`, 2);
132
+ }
133
+ },
134
+ };
@@ -0,0 +1,30 @@
1
+ // packages/cli/src/commands/mcp.ts
2
+ //
3
+ // `kit mcp` serves the agent tool surface (kit_next, kit_phase, kit_status) over
4
+ // the Model Context Protocol on stdio, so a host can call the kit's typed tools
5
+ // instead of shelling out. Stdout carries the protocol only.
6
+
7
+ import path from 'node:path';
8
+ import { serveAgentTools } from '../application/agent-tools/mcp-server.js';
9
+ import type { Command, CommandContext, CommandResult } from './types.js';
10
+
11
+ export const mcpCommand: Command = {
12
+ name: 'mcp',
13
+ summary: 'Serve the agent tool surface over MCP on stdio (kit_next, kit_phase, kit_status).',
14
+ async run(ctx: CommandContext): Promise<CommandResult> {
15
+ if (ctx.argv.includes('--help') || ctx.argv.includes('-h')) {
16
+ ctx.stdout(
17
+ [
18
+ 'Usage: kit mcp',
19
+ '',
20
+ 'Serve kit_next, kit_phase and kit_status over the Model Context Protocol on',
21
+ 'stdio. The working directory is the project. Stdout carries the protocol only.',
22
+ '',
23
+ ].join('\n'),
24
+ );
25
+ return { exitCode: 0 };
26
+ }
27
+ await serveAgentTools({ projectRoot: path.resolve(ctx.cwd) });
28
+ return { exitCode: 0 };
29
+ },
30
+ };
@@ -0,0 +1,146 @@
1
+ // packages/cli/src/commands/memory.test.ts
2
+ //
3
+ // CLI-layer tests for `kit memory` (M29): honest no-memory reporting,
4
+ // query search, --list, --domain filtering, and the --json surface.
5
+ // The persisted memory is seeded through the real MemoryStore
6
+ // (saveToDisk), so the tests exercise the exact on-disk contract the
7
+ // command reads (.ai-kit/state/memory.json).
8
+ import { promises as fs } from 'node:fs';
9
+ import os from 'node:os';
10
+ import path from 'node:path';
11
+ import { MemoryStore } from '@agentic-sdlc-kit/core';
12
+ import type { MemoryEntry } from '@agentic-sdlc-kit/contracts';
13
+ import { describe, expect, it } from 'vitest';
14
+ import { memoryCommand } from './memory.js';
15
+ import type { CommandContext } from './types.js';
16
+
17
+ const makeCtx = (
18
+ argv: string[],
19
+ cwd: string,
20
+ ): CommandContext & { out: string[]; err: string[] } => {
21
+ const out: string[] = [];
22
+ const err: string[] = [];
23
+ return {
24
+ argv,
25
+ cwd,
26
+ stdout: (s) => void out.push(s),
27
+ stderr: (s) => void err.push(s),
28
+ out,
29
+ err,
30
+ };
31
+ };
32
+
33
+ function entry(overrides: Partial<MemoryEntry> = {}): MemoryEntry {
34
+ return {
35
+ id: 'MEM-001',
36
+ type: 'semantic',
37
+ domain: 'architecture',
38
+ summary: 'Keep contracts package dependency-free',
39
+ content: 'The contracts package must import nothing from sibling packages.',
40
+ confidence: 0.8,
41
+ provenance: ['FEATURE-001'],
42
+ appliedCount: 2,
43
+ successCount: 2,
44
+ createdAt: '2026-01-01T00:00:00.000Z',
45
+ updatedAt: '2026-01-01T00:00:00.000Z',
46
+ ...overrides,
47
+ };
48
+ }
49
+
50
+ async function seedMemory(root: string, entries: readonly MemoryEntry[]): Promise<void> {
51
+ const store = new MemoryStore();
52
+ for (const e of entries) store.put(e);
53
+ await store.saveToDisk(root);
54
+ }
55
+
56
+ async function tmpRoot(): Promise<string> {
57
+ return fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-memory-'));
58
+ }
59
+
60
+ describe('kit memory', () => {
61
+ it('shows usage with --help and exits 0', async () => {
62
+ const ctx = makeCtx(['--help'], await tmpRoot());
63
+ const result = await memoryCommand.run(ctx);
64
+ expect(result.exitCode).toBe(0);
65
+ expect(ctx.out.join('')).toContain('Usage: kit memory [query]');
66
+ });
67
+
68
+ it('reports honestly when no memory has been accumulated yet', async () => {
69
+ const ctx = makeCtx(['architecture'], await tmpRoot());
70
+ const result = await memoryCommand.run(ctx);
71
+ expect(result.exitCode).toBe(0);
72
+ const text = ctx.out.join('');
73
+ expect(text).toContain('no memory entries found');
74
+ expect(text).toContain('kit evolve');
75
+ });
76
+
77
+ it('reports the empty state as JSON with --json', async () => {
78
+ const ctx = makeCtx(['--json'], await tmpRoot());
79
+ const result = await memoryCommand.run(ctx);
80
+ expect(result.exitCode).toBe(0);
81
+ const payload = JSON.parse(ctx.out.join('')) as { entries: unknown[]; message: string };
82
+ expect(payload.entries).toEqual([]);
83
+ expect(payload.message).toContain('no memory entries found');
84
+ });
85
+
86
+ it('finds memory entries by query text', async () => {
87
+ const root = await tmpRoot();
88
+ await seedMemory(root, [entry()]);
89
+ const ctx = makeCtx(['contracts'], root);
90
+ const result = await memoryCommand.run(ctx);
91
+ expect(result.exitCode).toBe(0);
92
+ const text = ctx.out.join('');
93
+ expect(text).toContain('Found 1 memory entries');
94
+ expect(text).toContain('MEM-001');
95
+ expect(text).toContain('[SEMANTIC]');
96
+ });
97
+
98
+ it('says so when a query matches nothing', async () => {
99
+ const root = await tmpRoot();
100
+ await seedMemory(root, [entry()]);
101
+ const ctx = makeCtx(['kubernetes'], root);
102
+ const result = await memoryCommand.run(ctx);
103
+ expect(result.exitCode).toBe(0);
104
+ expect(ctx.out.join('')).toContain('No matching engineering memories found');
105
+ });
106
+
107
+ it('lists all entries with --list', async () => {
108
+ const root = await tmpRoot();
109
+ await seedMemory(root, [
110
+ entry(),
111
+ entry({ id: 'MEM-002', type: 'procedural', domain: 'tdd', summary: 'Red before Green' }),
112
+ ]);
113
+ const ctx = makeCtx(['--list'], root);
114
+ const result = await memoryCommand.run(ctx);
115
+ expect(result.exitCode).toBe(0);
116
+ const text = ctx.out.join('');
117
+ expect(text).toContain('MEM-001');
118
+ expect(text).toContain('MEM-002');
119
+ });
120
+
121
+ it('filters --list output by --domain', async () => {
122
+ const root = await tmpRoot();
123
+ await seedMemory(root, [
124
+ entry(),
125
+ entry({ id: 'MEM-002', domain: 'tdd', summary: 'Red before Green' }),
126
+ ]);
127
+ const ctx = makeCtx(['--list', '--domain', 'tdd'], root);
128
+ const result = await memoryCommand.run(ctx);
129
+ expect(result.exitCode).toBe(0);
130
+ const text = ctx.out.join('');
131
+ expect(text).not.toContain('MEM-001');
132
+ expect(text).toContain('MEM-002');
133
+ });
134
+
135
+ it('emits machine-readable entries with --json', async () => {
136
+ const root = await tmpRoot();
137
+ await seedMemory(root, [entry()]);
138
+ const ctx = makeCtx(['--json', '--list'], root);
139
+ const result = await memoryCommand.run(ctx);
140
+ expect(result.exitCode).toBe(0);
141
+ const entries = JSON.parse(ctx.out.join('')) as Array<{ id: string; domain: string }>;
142
+ expect(entries).toHaveLength(1);
143
+ expect(entries[0]?.id).toBe('MEM-001');
144
+ expect(entries[0]?.domain).toBe('architecture');
145
+ });
146
+ });