Open
fix: preflight hub compatibility before clipboard installation #1
gcw_SCCFBRjWAdd on Oct 19
fix: preflight hub compatibility before clipboard installation #1
Open
Total: 2 files changed+39-5
| @@ -62,6 +62,21 @@ log(`target checkout: ${checkout}`) | |||
| 62 | 62 | ||
| 63 | // --- 2. copy the clipboard panel sources ---------------------------------- | 63 | // --- 2. copy the clipboard panel sources ---------------------------------- |
| 64 | 64 | ||
| 65 | +// Validate all inputs before modifying the target checkout. Otherwise an | ||
| 66 | +// outdated hub.ts leaves behind copied clipboard assets on installation error. | ||
| 67 | +const hubPath = join(checkout, HUB_RELATIVE) | ||
| 68 | +if (!existsSync(hubPath)) fail(`input hub not found: ${hubPath}`) | ||
| 69 | +const originalHub = readFileSync(hubPath, 'utf8') | ||
| 70 | +const alreadyPatched = originalHub.includes('isClipboardTrigger') | ||
| 71 | +const anchorImport = "import { queueReadFaceOf } from './queue-store.ts'" | ||
| 72 | +const anchorSink = " if (text === '' && attachmentIds.length === 0) return Promise.resolve({ kind: 'success' })" | ||
| 73 | +if (!alreadyPatched && (!originalHub.includes(anchorImport) || !originalHub.includes(anchorSink))) { | ||
| 74 | + fail('hub.ts injection anchors not found; no files copied. Patch manually per assets/hub-sink.patch.md') | ||
| 75 | +} | ||
| 76 | +for (const asset of ASSETS) { | ||
| 77 | + if (!existsSync(join(assetsDir, asset))) fail(`skill asset missing: ${asset}`) | ||
| 78 | +} | ||
| 79 | + | ||
| 65 | const clipboardDir = join(checkout, CLIPBOARD_DIR_RELATIVE) | 80 | const clipboardDir = join(checkout, CLIPBOARD_DIR_RELATIVE) |
| 66 | mkdirSync(clipboardDir, { recursive: true }) | 81 | mkdirSync(clipboardDir, { recursive: true }) |
| 67 | for (const asset of ASSETS) { | 82 | for (const asset of ASSETS) { |
| @@ -73,14 +88,11 @@ for (const asset of ASSETS) { | |||
| 73 | 88 | ||
| 74 | // --- 3. patch the input hub ------------------------------------------------ | 89 | // --- 3. patch the input hub ------------------------------------------------ |
| 75 | 90 | ||
| 76 | -const hubPath = join(checkout, HUB_RELATIVE) | 91 | +let hub = originalHub |
| 77 | -if (!existsSync(hubPath)) fail(`input hub not found: ${hubPath}`) | ||
| 78 | -let hub = readFileSync(hubPath, 'utf8') | ||
| 79 | 92 | ||
| 80 | if (hub.includes('isClipboardTrigger')) { | 93 | if (hub.includes('isClipboardTrigger')) { |
| 81 | log('hub.ts already patched — skipping injection') | 94 | log('hub.ts already patched — skipping injection') |
| 82 | } else { | 95 | } else { |
| 83 | - const anchorImport = "import { queueReadFaceOf } from './queue-store.ts'" | ||
| 84 | const importInjection = [ | 96 | const importInjection = [ |
| 85 | anchorImport, | 97 | anchorImport, |
| 86 | "import { isClipboardTrigger } from '../clipboard/history.ts'", | 98 | "import { isClipboardTrigger } from '../clipboard/history.ts'", |
| @@ -89,7 +101,6 @@ if (hub.includes('isClipboardTrigger')) { | |||
| 89 | if (!hub.includes(anchorImport)) fail('hub.ts anchor for imports not found; patch manually per assets/hub-sink.patch.md') | 101 | if (!hub.includes(anchorImport)) fail('hub.ts anchor for imports not found; patch manually per assets/hub-sink.patch.md') |
| 90 | hub = hub.replace(anchorImport, importInjection) | 102 | hub = hub.replace(anchorImport, importInjection) |
| 91 | 103 | ||
| 92 | - const anchorSink = " if (text === '' && attachmentIds.length === 0) return Promise.resolve({ kind: 'success' })" | ||
| 93 | const sinkInjection = [ | 104 | const sinkInjection = [ |
| 94 | anchorSink, | 105 | anchorSink, |
| 95 | ' if (attachmentIds.length === 0 && isClipboardTrigger(text)) {', | 106 | ' if (attachmentIds.length === 0 && isClipboardTrigger(text)) {', |
| @@ -0,0 +1,23 @@ | |||
| 1 | +import { test } from 'node:test' | ||
| 2 | +import assert from 'node:assert/strict' | ||
| 3 | +import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs' | ||
| 4 | +import { tmpdir } from 'node:os' | ||
| 5 | +import { join, resolve } from 'node:path' | ||
| 6 | +import { spawnSync } from 'node:child_process' | ||
| 7 | + | ||
| 8 | +test('unrecognized hub does not leave copied clipboard assets', () => { | ||
| 9 | + const root = mkdtempSync(join(tmpdir(), 'cutting-board-test-')) | ||
| 10 | + try { | ||
| 11 | + const ui = join(root, 'packages/client/ui-conversation') | ||
| 12 | + const input = join(ui, 'src/client/input') | ||
| 13 | + mkdirSync(input, { recursive: true }) | ||
| 14 | + writeFileSync(join(ui, 'package.json'), '{}') | ||
| 15 | + writeFileSync(join(input, 'hub.ts'), '// incompatible hub fixture\n') | ||
| 16 | + const result = spawnSync(process.execPath, [resolve('scripts/install.mjs'), root], { encoding: 'utf8' }) | ||
| 17 | + assert.notEqual(result.status, 0) | ||
| 18 | + assert.match(result.stderr, /injection anchors not found/) | ||
| 19 | + assert.equal(existsSync(join(ui, 'src/client/clipboard')), false) | ||
| 20 | + } finally { | ||
| 21 | + rmSync(root, { recursive: true, force: true }) | ||
| 22 | + } | ||
| 23 | +}) | ||