diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 84dd5609b..6ebbdd2a3 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "f6ccbad6d88a98062fec14aee7319256f2bfd5e8d3e547c122cf4a6bd3bc2e5e", + "registryDigest": "ed2327a440175dcdbc4f56cf364b78b1ed76ab6c5f205451bda497626d737f20", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/ck/commands/migrate.mjs b/skills/ck/commands/migrate.mjs index eaa3fbd91..4b9c3e79c 100644 --- a/skills/ck/commands/migrate.mjs +++ b/skills/ck/commands/migrate.mjs @@ -23,17 +23,20 @@ if (isDryRun) { // ── v1 markdown parsers ─────────────────────────────────────────────────────── +const BULLET_RE = /^(?:[-*]|\d+\.)\s+/; + function extractSection(md, heading) { - const re = new RegExp(`## ${heading}\\n([\\s\\S]*?)(?=\\n## |$)`); + const re = new RegExp(`## ${heading}[ \\t]*\\r?\\n([\\s\\S]*?)(?=\\r?\\n## |$)`); const m = md.match(re); return m ? m[1].trim() : null; } function parseBullets(text) { if (!text) return []; - return text.split('\n') - .filter(l => /^[-*\d]\s/.test(l.trim())) - .map(l => l.replace(/^[-*\d]+\.?\s+/, '').trim()) + return text.split(/\r?\n/) + .map(l => l.trim()) + .filter(l => BULLET_RE.test(l)) + .map(l => l.replace(BULLET_RE, '').trim()) .filter(Boolean); } @@ -56,6 +59,16 @@ function parseDecisionsTable(text) { * - Multi-session blocks: "Session N (date):\n- bullet\n" * Returns array of session-like objects {date?, leftOff} */ +function normalizeLeftOffBlock(body) { + const lines = body + .split(/\r?\n/) + .map(l => l.trim()) + .filter(Boolean) + .map(l => l.replace(BULLET_RE, '').trim()) + .filter(Boolean); + return lines.join('\n'); +} + function parseLeftOff(text) { if (!text) return [{ leftOff: null }]; @@ -66,17 +79,16 @@ function parseLeftOff(text) { .filter(b => b.trim()) .map(block => { const dateMatch = block.match(/\((\d{4}-\d{2}-\d{2})\)/); - const bullets = parseBullets(block); + const body = block.replace(/^Session \d+[^\r\n]*(?:\r?\n|$)/, ''); return { date: dateMatch?.[1] || null, - leftOff: bullets.length ? bullets.join('\n') : block.replace(/^Session \d+.*\n/, '').trim(), + leftOff: normalizeLeftOffBlock(body), }; }); } // Simple format - const bullets = parseBullets(text); - return [{ leftOff: bullets.length ? bullets.join('\n') : text.trim() }]; + return [{ leftOff: normalizeLeftOffBlock(text) }]; } // ── Main migration ───────────────────────────────────────────────────────────── @@ -95,21 +107,36 @@ for (const [projectPath, info] of Object.entries(projects)) { // Already v2 if (existsSync(contextJsonPath)) { + let existing = null; try { - const existing = JSON.parse(readFileSync(contextJsonPath, 'utf8')); - if (existing.version === 2) { - console.log(` ✓ ${contextDir} — already v2, skipping`); - skipped++; - continue; - } - } catch { /* fall through to migrate */ } + existing = JSON.parse(readFileSync(contextJsonPath, 'utf8')); + } catch { + existing = null; + } + if ( + existing + && typeof existing === 'object' + && !Array.isArray(existing) + && existing.version === 2 + && Array.isArray(existing.sessions) + && existing.sessions.every(s => s && typeof s === 'object') + ) { + console.log(` ✓ ${contextDir} — already v2, skipping`); + skipped++; + continue; + } + console.log(` ✗ ${contextDir}: ${contextJsonPath} is not a readable ck context; skipping without changes`); + errors++; + continue; } console.log(`\n → Migrating: ${contextDir}`); try { // Read v1 files - const contextMd = existsSync(contextMdPath) ? readFileSync(contextMdPath, 'utf8') : ''; + const contextMd = existsSync(contextMdPath) + ? readFileSync(contextMdPath, 'utf8').replace(/\r\n/g, '\n') + : ''; let meta = {}; if (existsSync(metaPath)) { try { diff --git a/tests/skills/ck-migrate.test.js b/tests/skills/ck-migrate.test.js new file mode 100644 index 000000000..50af5737a --- /dev/null +++ b/tests/skills/ck-migrate.test.js @@ -0,0 +1,234 @@ +#!/usr/bin/env node + +'use strict'; + +const assert = require('assert'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { spawnSync } = require('child_process'); + +const repoRoot = path.resolve(__dirname, '..', '..'); +const commandsDir = path.join(repoRoot, 'skills', 'ck', 'commands'); +let passed = 0; +let failed = 0; + +const NUMBERED_V1_CONTEXT = [ + '# Project: app', + '', + '## What This Is', + 'CLI tool for notes', + '', + '## Tech Stack', + 'Node.js, SQLite', + '', + '## Current Goal', + 'Ship v2', + '', + '## Where I Left Off', + '1. Finished schema draft', + '2. Wired CLI flags', + '', + '## Next Steps', + '1. Add migration command', + '2. Write integration tests', + '10. Tag release', + '', + '## Blockers', + '1. Waiting on fixture review', + '', + '## Do Not Do', + '1. Do not add external dependencies', + '- Keep zero network calls', + '', + '## Decisions Made', + '| Decision | Why | Date |', + '|----------|-----|------|', + '| Use SQLite | Local file | 2026-01-10 |', + '', +].join('\n'); + +const CRLF_V1_CONTEXT = [ + '# Project: win-app', + '', + '## What This Is', + 'Windows desktop helper', + '', + '## Tech Stack', + '- TypeScript', + '- Electron', + '', + '## Current Goal', + 'Package installer', + '', + '## Where I Left Off', + 'Session 1 (2026-01-05):', + 'Initial scaffold ready', + 'Session 2 (2026-01-10):', + '- Signed binary locally', + '', + '## Next Steps', + '1. Verify installer hash', + '2. Test clean uninstall', + '', + '## Blockers', + '- None', + '', + '## Do Not Do', + '- Do not write to global registry', + '', + '## Decisions Made', + '| Decision | Why | Date |', + '|----------|-----|------|', + '| Portable zip | Simpler updates | 2026-01-10 |', + '', +].join('\r\n'); + +function test(description, fn) { + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-ck-migrate-')); + try { + fn(home); + console.log(` ✓ ${description}`); + passed++; + } catch (error) { + console.log(` ✗ ${description}: ${error.message}`); + failed++; + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +} + +function runMigrate(home, args = []) { + return spawnSync(process.execPath, [path.join(commandsDir, 'migrate.mjs'), ...args], { + encoding: 'utf8', + cwd: home, + env: { ...process.env, HOME: home, USERPROFILE: home }, + }); +} + +function seedV1Project(home, name, contextMd, meta = null) { + const project = path.join(home, name); + const contextDir = path.join(home, '.claude', 'ck', 'contexts', name); + fs.mkdirSync(project, { recursive: true }); + fs.mkdirSync(contextDir, { recursive: true }); + fs.writeFileSync(path.join(contextDir, 'CONTEXT.md'), contextMd, 'utf8'); + if (meta !== null) { + fs.writeFileSync(path.join(contextDir, 'meta.json'), JSON.stringify(meta), 'utf8'); + } + fs.writeFileSync(path.join(home, '.claude', 'ck', 'projects.json'), JSON.stringify({ + [project]: { name, contextDir: name, lastUpdated: '2026-01-10' }, + })); + return { project, contextDir }; +} + +console.log('\nck migrate tests:'); + +test('preserves numbered list items in Next Steps, Do Not Do, and Where I Left Off', home => { + const { contextDir } = seedV1Project(home, 'app', NUMBERED_V1_CONTEXT, { + lastUpdated: '2026-01-10', + lastSessionId: 'deadbeef1234', + lastSessionSummary: 'Drafted schema', + }); + + const result = runMigrate(home); + assert.strictEqual(result.status, 0, result.stdout + result.stderr); + + const context = JSON.parse(fs.readFileSync(path.join(contextDir, 'context.json'), 'utf8')); + assert.strictEqual(context.description, 'CLI tool for notes'); + assert.deepStrictEqual(context.stack, ['Node.js', 'SQLite']); + assert.strictEqual(context.goal, 'Ship v2'); + assert.deepStrictEqual(context.constraints, [ + 'Do not add external dependencies', + 'Keep zero network calls', + ]); + assert.strictEqual(context.sessions.length, 1); + assert.strictEqual(context.sessions[0].leftOff, 'Finished schema draft\nWired CLI flags'); + assert.deepStrictEqual(context.sessions[0].nextSteps, [ + 'Add migration command', + 'Write integration tests', + 'Tag release', + ]); + assert.deepStrictEqual(context.sessions[0].blockers, ['Waiting on fixture review']); + assert.deepStrictEqual(context.sessions[0].decisions, [ + { what: 'Use SQLite', why: 'Local file', date: '2026-01-10' }, + ]); +}); + +test('keeps mixed prose and unpunctuated leading numbers intact in Where I Left Off', home => { + const mixedMd = [ + '# Project: mixed', + '', + '## Where I Left Off', + 'Investigating login', + '1. Add retries', + '2026 release planning', + 'Keep cookie compatibility', + '', + '## Next Steps', + '2026 release planning', + '1. Ship retries', + '', + ].join('\n'); + const { contextDir } = seedV1Project(home, 'mixed', mixedMd); + + const result = runMigrate(home); + assert.strictEqual(result.status, 0, result.stdout + result.stderr); + + const context = JSON.parse(fs.readFileSync(path.join(contextDir, 'context.json'), 'utf8')); + assert.strictEqual( + context.sessions[0].leftOff, + 'Investigating login\nAdd retries\n2026 release planning\nKeep cookie compatibility' + ); + assert.deepStrictEqual(context.sessions[0].nextSteps, ['Ship retries']); +}); + +test('migrates CRLF CONTEXT.md files without dropping sections', home => { + const { contextDir } = seedV1Project(home, 'win-app', CRLF_V1_CONTEXT); + + const result = runMigrate(home); + assert.strictEqual(result.status, 0, result.stdout + result.stderr); + + const context = JSON.parse(fs.readFileSync(path.join(contextDir, 'context.json'), 'utf8')); + assert.strictEqual(context.description, 'Windows desktop helper'); + assert.deepStrictEqual(context.stack, ['TypeScript', 'Electron']); + assert.strictEqual(context.goal, 'Package installer'); + assert.deepStrictEqual(context.constraints, ['Do not write to global registry']); + assert.strictEqual(context.sessions.length, 2); + assert.strictEqual(context.sessions[0].date, '2026-01-05'); + assert.strictEqual(context.sessions[0].leftOff, 'Initial scaffold ready'); + assert.strictEqual(context.sessions[1].date, '2026-01-10'); + assert.strictEqual(context.sessions[1].leftOff, 'Signed binary locally'); + assert.deepStrictEqual(context.sessions[1].nextSteps, [ + 'Verify installer hash', + 'Test clean uninstall', + ]); + assert.deepStrictEqual(context.sessions[1].blockers, []); + assert.deepStrictEqual(context.sessions[1].decisions, [ + { what: 'Portable zip', why: 'Simpler updates', date: '2026-01-10' }, + ]); +}); + +test('refuses to overwrite an unreadable or non-v2 context.json', home => { + const originalMd = '# Project: app\n\n## What This Is\nKeep me safe\n'; + const { contextDir } = seedV1Project(home, 'app', originalMd); + const contextJsonPath = path.join(contextDir, 'context.json'); + const contextMdPath = path.join(contextDir, 'CONTEXT.md'); + + for (const damaged of [ + '{"version": 2, "sessions": [{"summary": "partial"}', + '[{"version": 2}]', + '{"version": 3, "sessions": []}', + ]) { + fs.writeFileSync(contextJsonPath, damaged, 'utf8'); + fs.writeFileSync(contextMdPath, originalMd, 'utf8'); + + const result = runMigrate(home); + assert.strictEqual(result.status, 1, result.stdout + result.stderr); + assert.strictEqual(fs.readFileSync(contextJsonPath, 'utf8'), damaged); + assert.strictEqual(fs.readFileSync(contextMdPath, 'utf8'), originalMd); + } +}); + +console.log(`\nPassed: ${passed}`); +console.log(`Failed: ${failed}`); +process.exit(failed > 0 ? 1 : 0);