joycraft 0.7.17 → 1.0.1
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 +148 -140
- package/dist/cli.js +1593 -373
- package/dist/cli.js.map +1 -1
- package/dist/joycraft-release.json +1 -1
- package/package.json +9 -3
- package/payload/.agents/skills/joycraft-context/SKILL.md +36 -0
- package/payload/.agents/skills/joycraft-decompose/SKILL.md +68 -0
- package/payload/.agents/skills/joycraft-entry/SKILL.md +109 -0
- package/payload/.agents/skills/joycraft-implement/SKILL.md +118 -0
- package/payload/.agents/skills/joycraft-retro/SKILL.md +54 -0
- package/payload/.agents/skills/joycraft-retro/agents/openai.yaml +5 -0
- package/payload/.agents/skills/joycraft-session-end/SKILL.md +82 -0
- package/payload/.agents/skills/joycraft-testing/SKILL.md +60 -0
- package/payload/.agents/skills/joycraft-ui-slice/SKILL.md +59 -0
- package/payload/.agents/skills/joycraft-ui-slice/scripts/serve.mjs +97 -0
- package/payload/.agents/skills/joycraft-verify/SKILL.md +103 -0
- package/payload/.claude/agents/joycraft-implementer.md +36 -0
- package/payload/.claude/agents/joycraft-reviewer.md +35 -0
- package/payload/.joycraft/assets/artifacts/kit.css +102 -0
- package/payload/.joycraft/assets/artifacts/kit.js +301 -0
- package/payload/.joycraft/assets/artifacts/transports.js +236 -0
- package/payload/.joycraft/contracts/types.d.ts +185 -0
- package/payload/.joycraft/references/architecture.md +90 -0
- package/payload/.joycraft/references/archive.md +211 -0
- package/payload/.joycraft/references/artifacts.md +176 -0
- package/payload/.joycraft/references/completion.md +109 -0
- package/payload/.joycraft/references/context.md +85 -0
- package/payload/.joycraft/references/delivery.md +32 -0
- package/payload/.joycraft/references/dispatch.md +231 -0
- package/payload/.joycraft/references/recurring-failures.md +33 -0
- package/payload/.joycraft/references/retro.md +94 -0
- package/payload/.joycraft/references/setup.md +295 -0
- package/payload/.joycraft/references/testing.md +120 -0
- package/payload/.joycraft/references/update.md +44 -0
- package/payload/.joycraft/references/workflow.md +113 -0
- package/payload/.joycraft/references/worktrees.md +174 -0
- package/payload/.joycraft/runtime/archive.mjs +463 -0
- package/payload/.joycraft/runtime/artifacts/cli.mjs +61 -0
- package/payload/.joycraft/runtime/artifacts/decisions.mjs +214 -0
- package/payload/.joycraft/runtime/artifacts/model.mjs +161 -0
- package/payload/.joycraft/runtime/artifacts/preferences.mjs +127 -0
- package/payload/.joycraft/runtime/artifacts/render.mjs +21 -0
- package/payload/.joycraft/runtime/artifacts/server.mjs +40 -0
- package/payload/.joycraft/runtime/completion.mjs +300 -0
- package/payload/.joycraft/runtime/context/cli.mjs +75 -0
- package/payload/.joycraft/runtime/context/index.mjs +29 -0
- package/payload/.joycraft/runtime/context/search.mjs +149 -0
- package/payload/.joycraft/runtime/coordinator.mjs +169 -0
- package/payload/.joycraft/runtime/delivery.mjs +142 -0
- package/payload/.joycraft/runtime/dispatch-preflight.mjs +535 -0
- package/payload/.joycraft/runtime/dispatch-start.mjs +106 -0
- package/payload/.joycraft/runtime/evidence.mjs +137 -0
- package/payload/.joycraft/runtime/identity.mjs +486 -0
- package/payload/.joycraft/runtime/kickoff.mjs +174 -0
- package/payload/.joycraft/runtime/redact.mjs +9 -0
- package/payload/.joycraft/runtime/retro.mjs +151 -0
- package/payload/.joycraft/runtime/review-tools.json +34 -0
- package/payload/.joycraft/runtime/review-tools.mjs +331 -0
- package/payload/.joycraft/runtime/scope.mjs +129 -0
- package/payload/.joycraft/runtime/status.mjs +96 -0
- package/payload/.joycraft/runtime/update-check.mjs +142 -0
- package/payload/.joycraft/runtime/worktrees.mjs +201 -0
- package/payload/.joycraft/templates/README.md +7 -0
- package/payload/.joycraft/templates/artifact.json +33 -0
- package/payload/.joycraft/templates/brief.md +35 -0
- package/payload/.joycraft/templates/decisions.md +13 -0
- package/payload/.joycraft/templates/project-context.md +36 -0
- package/payload/.joycraft/templates/queue.json +27 -0
- package/payload/.joycraft/templates/spec.md +39 -0
- package/payload/.joycraft/templates/ui-slice.html +71 -0
- package/payload/PIN +77 -0
- package/payload/scripts/export.mjs +301 -0
- package/payload/scripts/hook-paths.mjs +35 -0
- package/payload/scripts/legacy-hooks.mjs +128 -0
- package/payload/scripts/legacy-inventory.json +117 -0
- package/dist/chunk-35RQLGKM.js +0 -2167
- package/dist/chunk-35RQLGKM.js.map +0 -1
- package/dist/chunk-7BPB2IKY.js +0 -255
- package/dist/chunk-7BPB2IKY.js.map +0 -1
- package/dist/chunk-B73DPYYC.js +0 -377
- package/dist/chunk-B73DPYYC.js.map +0 -1
- package/dist/chunk-K4AI6FKB.js +0 -417
- package/dist/chunk-K4AI6FKB.js.map +0 -1
- package/dist/chunk-KKDKC4FM.js +0 -115
- package/dist/chunk-KKDKC4FM.js.map +0 -1
- package/dist/chunk-OMFGDJW4.js +0 -97
- package/dist/chunk-OMFGDJW4.js.map +0 -1
- package/dist/chunk-YLWBDXF4.js +0 -3999
- package/dist/chunk-YLWBDXF4.js.map +0 -1
- package/dist/chunk-Z5JFEGWG.js +0 -563
- package/dist/chunk-Z5JFEGWG.js.map +0 -1
- package/dist/init-O2QC5X57.js +0 -60
- package/dist/init-O2QC5X57.js.map +0 -1
- package/dist/init-autofix-2DMBO4QN.js +0 -122
- package/dist/init-autofix-2DMBO4QN.js.map +0 -1
- package/dist/install-manifest-EQHT4474.js +0 -37
- package/dist/install-manifest-EQHT4474.js.map +0 -1
- package/dist/release-artifact-4TL5L2ZY.js +0 -14
- package/dist/release-artifact-4TL5L2ZY.js.map +0 -1
- package/dist/release-resolver-P2H46QTU.js +0 -293
- package/dist/release-resolver-P2H46QTU.js.map +0 -1
- package/dist/telemetry-store-IBO5GRTA.js +0 -14
- package/dist/telemetry-store-IBO5GRTA.js.map +0 -1
- package/dist/update-LYHS4FBH.js +0 -21
- package/dist/update-LYHS4FBH.js.map +0 -1
- package/dist/update-check-YT765NE7.js +0 -36
- package/dist/update-check-YT765NE7.js.map +0 -1
- package/dist/upgrade-GMP6JQ52.js +0 -28
- package/dist/upgrade-GMP6JQ52.js.map +0 -1
package/payload/PIN
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"source": "joycraft-prototype",
|
|
4
|
+
"tag": "field-8",
|
|
5
|
+
"commit": "3016d9c32eac089aab99e46c4a1dbeced984a0a0",
|
|
6
|
+
"files": {
|
|
7
|
+
".agents/skills/joycraft-context/SKILL.md": "e0a5f7d82fe8b6b6d3aeab38f7cf570ca939dba07034243af1ef86d24448c084",
|
|
8
|
+
".agents/skills/joycraft-decompose/SKILL.md": "a24c9d9cf2dc7af4b99ad67575b64b9d0c99818044f7fafc3a5243dc4c557b5f",
|
|
9
|
+
".agents/skills/joycraft-entry/SKILL.md": "92371a30535d76bcc188c159f4d112c23441efc4dc94eca2a7939bbc6d10b714",
|
|
10
|
+
".agents/skills/joycraft-implement/SKILL.md": "70ed58c6cfb08b9b97189496d7ab14cad2891136d2fbcee34f2aec635d530faf",
|
|
11
|
+
".agents/skills/joycraft-retro/SKILL.md": "156f36cb047604c734bf273bd02822d964b6af86619ed6c2a30fa6d79eb9a986",
|
|
12
|
+
".agents/skills/joycraft-retro/agents/openai.yaml": "f0eb4732261635f41e08f0bbe9a931d62fd8ac645149d3406e6fc7e16a81750b",
|
|
13
|
+
".agents/skills/joycraft-session-end/SKILL.md": "3cf405eb201dbe680c42c6d6718a524ccf5b12525070fae25daf7a59e00fb66a",
|
|
14
|
+
".agents/skills/joycraft-testing/SKILL.md": "89e983b285099c38821dc807311b27d450e6dec9fb691cd687ca74332b091157",
|
|
15
|
+
".agents/skills/joycraft-ui-slice/SKILL.md": "fcb5f109c9bd23d4afe5b47d19c90fd34485754ce9061c64bce1ae2ce0376484",
|
|
16
|
+
".agents/skills/joycraft-ui-slice/scripts/serve.mjs": "c16858800b6fbe6f7a3a21b21e785ad70c9a89a3fe970bc08701fefeac5042fa",
|
|
17
|
+
".agents/skills/joycraft-verify/SKILL.md": "cce30cf1ee39b6a6aa0211a661660ef9be9ec55bb83650456ddac0bac0037274",
|
|
18
|
+
".claude/agents/joycraft-implementer.md": "e424866be649c4ac401317f592b8d3709f3eeb474a7b03ec83e401dba620b755",
|
|
19
|
+
".claude/agents/joycraft-reviewer.md": "a82179fba810c57bf4049f3fc3216d806f764fdb3fe3c39226c8664fc03b7230",
|
|
20
|
+
".joycraft/assets/artifacts/kit.css": "9b04b739b3b137d4a3052a939cbc46b5acb9abde085989bd599481ae40e5c6a9",
|
|
21
|
+
".joycraft/assets/artifacts/kit.js": "e54db2157cfee278f5e6777b6b6fe2d2f5297cd2401b173d1a4b72e3fad00562",
|
|
22
|
+
".joycraft/assets/artifacts/transports.js": "e53c67ac030a1d7c9ce4666b998604a471598cb4df08bc83f37e3d166b8017eb",
|
|
23
|
+
".joycraft/contracts/types.d.ts": "5c4e654ea929778c586bc6cce44e60dc031578b2414dd1b8161359cf9f424b41",
|
|
24
|
+
".joycraft/references/architecture.md": "52eaaa4b88053481c9f8cad2418ea0f1db880184c3a50c95901f30f24be75852",
|
|
25
|
+
".joycraft/references/archive.md": "d421e17ed3724fc1dacc9f3397a50d7976329d5dfb5e274791931df5544d5be4",
|
|
26
|
+
".joycraft/references/artifacts.md": "1f21223601342f4ce54c4d76320f89f4eea1c8d0089396495993ba62155f422e",
|
|
27
|
+
".joycraft/references/completion.md": "7df48664861f41e2e503a828e97d6b6f615745591a3f47f17d196fa4d24a122d",
|
|
28
|
+
".joycraft/references/context.md": "f2c8313eaf996b633040e52855ae1eef57b03cad2b024bb5eacb70b38d39ca95",
|
|
29
|
+
".joycraft/references/delivery.md": "1afc417ee3922ebf5e84c0ae87796d6cb6305679c0257ed38a35f396949de5e8",
|
|
30
|
+
".joycraft/references/dispatch.md": "b80d6899c52b7c003012fdfc774be271773213b7fedc37f4c247b431cdf8207d",
|
|
31
|
+
".joycraft/references/recurring-failures.md": "7080f6c55492bc19c2897f4f0b128a830a71cdb516678c79ff6e97ffa1dc6b86",
|
|
32
|
+
".joycraft/references/retro.md": "f359eae19747777fb4687c1bc7eb280f0319ae629ee62913346e60fc56551562",
|
|
33
|
+
".joycraft/references/setup.md": "5bbd5f5a5fcfa00988bf1b97a04011667eeeba7dea09cb15a9bcb273c5c2277f",
|
|
34
|
+
".joycraft/references/testing.md": "064edc82feb4c29485e9b5d9fbc5140d1f908d77006963482445ae1e4793fc2f",
|
|
35
|
+
".joycraft/references/update.md": "8e26ddaa21d3b133902bebcb6662a16f52f3ae3f04c51c69fdb325a8e9c0aeac",
|
|
36
|
+
".joycraft/references/workflow.md": "1e982310f98cb61749ff921f96c7aa2da52fcd3df4eb444608216fbf6e126a53",
|
|
37
|
+
".joycraft/references/worktrees.md": "fb18af0cd76d4e3c5d5b892a7ae10946f21f189e642243235c188b571635f735",
|
|
38
|
+
".joycraft/runtime/archive.mjs": "44a2953e60af1aa231f4b8affba3d51058b58bc025a72ae1affdbe7c3182f9d1",
|
|
39
|
+
".joycraft/runtime/artifacts/cli.mjs": "d30152e6829e07d9c7ff6ceedbfa6813931674d8529778f62b0d64e3a4049ab0",
|
|
40
|
+
".joycraft/runtime/artifacts/decisions.mjs": "9cba81346daf59cd020b8d976af98b1f6c2aec8d0a1575e5e0f806334ae622d0",
|
|
41
|
+
".joycraft/runtime/artifacts/model.mjs": "fbe44d37049759387ecc02ba2580c94e26e6c5cb8cac3dcbcc79aeab3b75756b",
|
|
42
|
+
".joycraft/runtime/artifacts/preferences.mjs": "9165e1c753aa8533affe976cacecb135b0ee3cc9f455f5f80b1568103bb49fd6",
|
|
43
|
+
".joycraft/runtime/artifacts/render.mjs": "488673cab94fa6bfde223c2c41650f8e183f7ac0be3840d371ee057d95ea0fa5",
|
|
44
|
+
".joycraft/runtime/artifacts/server.mjs": "a96048fad649e8053c70ce95c5a757929ab57ae9de27198121224326551c0a05",
|
|
45
|
+
".joycraft/runtime/completion.mjs": "11635b11eb7df90398a69b6d41f96d9c7b9d2dc2255ae4a4a129562d46572ec1",
|
|
46
|
+
".joycraft/runtime/context/cli.mjs": "40adc206c8c3b7f4f5bedffb097d9ae1b0718f0586a055796e4987e9c3088eee",
|
|
47
|
+
".joycraft/runtime/context/index.mjs": "5a69d33d9a1a66ef23301e952ecb1ec427ff4b0baf408ede67a2600d3f2a173c",
|
|
48
|
+
".joycraft/runtime/context/search.mjs": "e7f48d8ada52574ddca22d0eaee5ce2a405c4074910879e47c01ca287bba52b4",
|
|
49
|
+
".joycraft/runtime/coordinator.mjs": "6c120f4bcca76f5e976a5afd0be676cba82c1f25c10ac2def2ae3163bb4dc7c5",
|
|
50
|
+
".joycraft/runtime/delivery.mjs": "4fe897714eec33b24003f0d69abe1522a5199193ab7333fa6421776ee048df1c",
|
|
51
|
+
".joycraft/runtime/dispatch-preflight.mjs": "1b8283c06c8a2a41bfca06b1d5f0f83a093825911fbab1e04657dee06fa91c5d",
|
|
52
|
+
".joycraft/runtime/dispatch-start.mjs": "10cd5d257f98378c94c560a21b79c0492f1026eeb7725a08fca2daca37459186",
|
|
53
|
+
".joycraft/runtime/evidence.mjs": "80a386dd6d81e5fd32c64e53d5fb089072bc28f36df6c4f6c924077dd064604b",
|
|
54
|
+
".joycraft/runtime/identity.mjs": "f5a10dd58f2dd259502b04eaa2af42d2a74a09614e097a6efc35104c80bba024",
|
|
55
|
+
".joycraft/runtime/kickoff.mjs": "9d96998bff94662863182b9a922f26732c649b7a0b0130b6da7d63ec73eb3590",
|
|
56
|
+
".joycraft/runtime/redact.mjs": "cc8c0fe5e8d8fe78116ef126f6eee757a247739084e19c342bcf09a2b06524bc",
|
|
57
|
+
".joycraft/runtime/retro.mjs": "6f3664b0d50c12d4f2d8eef4b4d0d62897939d03e45860d4dcf35c0f5f92677c",
|
|
58
|
+
".joycraft/runtime/review-tools.json": "e4bc5ba2e0f6ab71a8af90a6f225f4655b9c1e7ee13d9303bd6e219002547a96",
|
|
59
|
+
".joycraft/runtime/review-tools.mjs": "17c13f7ab746478bce5711501a5190991b46616c849780196614950cfc6f0208",
|
|
60
|
+
".joycraft/runtime/scope.mjs": "e2cdd929596246bf24e18850698db440119badc7068a3c5beab03ded45abbd65",
|
|
61
|
+
".joycraft/runtime/status.mjs": "1899e2e458eadb9acd60ceffda73701b69edaa33141651e2e68d25a7ac6451eb",
|
|
62
|
+
".joycraft/runtime/update-check.mjs": "324b2eda4ac1feb6bb5b15ef488bea5f2f8a6312bc7b1c24719e0b7fd6a84634",
|
|
63
|
+
".joycraft/runtime/worktrees.mjs": "ba11b2e3fe96f47275c1dfddd087b7d40b8989e3d73f6187cb04887b5a66cd7f",
|
|
64
|
+
".joycraft/templates/README.md": "56a166d0729e8b6c3bcb098afb8145006259357087fd8b1f1f343d9a2b4d36e0",
|
|
65
|
+
".joycraft/templates/artifact.json": "fda5f7c32516d6ab094ee0e8506d84a1a0d6d4d0f49048b01aa392c619996f05",
|
|
66
|
+
".joycraft/templates/brief.md": "03479342500570aba9fb206beef6f6049fd180bec814dd8001987174aecfef25",
|
|
67
|
+
".joycraft/templates/decisions.md": "49e0c9b3efe51d1c2e87345fcefe23ef0579e524d50febef8ef43a35141c8002",
|
|
68
|
+
".joycraft/templates/project-context.md": "d7c81fee08609a7db45c5df0cb2bd5cb3409760169d8b03cc5f19b1ff82d03c2",
|
|
69
|
+
".joycraft/templates/queue.json": "88f922b6cc47b80befc27df86a143d6bae0b30869c46d712958b64291e5ebcc2",
|
|
70
|
+
".joycraft/templates/spec.md": "5f4b1490c2ba2e47ced1ef5b369fe3e41386eb61386c42c7a561b1e8af02ea5e",
|
|
71
|
+
".joycraft/templates/ui-slice.html": "0c3cf45709ff37815f8f0550b4b9431cb935589b282d40e8688b310a91de2274",
|
|
72
|
+
"scripts/export.mjs": "016d675b19ba3122468ce34817b8a9c444be919aa4ec583842cd9bc16d299243",
|
|
73
|
+
"scripts/hook-paths.mjs": "fed8a147a9bf6501bfda1e4c7f015b41947277920dc2bf49c23ad826cfb0c724",
|
|
74
|
+
"scripts/legacy-hooks.mjs": "d44f841e28d10b0a6aacf999bdd3e8bcec0feb742fa18c7459d6265d1d4776ba",
|
|
75
|
+
"scripts/legacy-inventory.json": "92455cceac13cc8bd27a0cbd5c16df71a44a82bf7a18280f5ba05b5dde2908ba"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { cp, lstat, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { execFileSync } from 'node:child_process';
|
|
4
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
+
import { applyLegacyHooks, planLegacyHooks } from './legacy-hooks.mjs';
|
|
7
|
+
import { relativePath } from '../.joycraft/runtime/identity.mjs';
|
|
8
|
+
|
|
9
|
+
export const installationManifestPath = '.joycraft/installation.json';
|
|
10
|
+
const mutableBundlePaths = new Set(['.joycraft/hooks-config.json', installationManifestPath]);
|
|
11
|
+
// Project-owned state shares the .joycraft/ directory with the bundle; it is never exported or replaced.
|
|
12
|
+
const projectOwned = path => /^\.joycraft\/(local\/|preferences\.json$|state\.json$)/.test(path);
|
|
13
|
+
export const previousBundleDir = '.joycraft-prototype';
|
|
14
|
+
const managedHookFiles = ['.claude/settings.json', '.codex/hooks.json', '.github/hooks/joycraft.json'];
|
|
15
|
+
const legacySkillNames = ['add-context', 'add-fact', 'bugfix', 'collaborative-setup', 'decide', 'decompose', 'design', 'gather-context', 'harden', 'implement-feature', 'implement-level5', 'implement', 'interview', 'lockdown', 'new-feature', 'optimize', 'research', 'session-end', 'setup', 'spec-done', 'tune', 'verify'];
|
|
16
|
+
const legacyDiscoveryPaths = ['.agents/skills/joycraft', '.claude/skills/joycraft', '.codex/skills/joycraft', '.agents/commands/joycraft', '.claude/commands/joycraft', '.codex/commands/joycraft', ...['.agents/skills', '.claude/skills', '.github/skills', '.pi/skills', '.omp/skills'].flatMap(root => legacySkillNames.map(name => `${root}/joycraft-${name}`)), '.pi/scripts/joycraft'];
|
|
17
|
+
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
18
|
+
|
|
19
|
+
async function statOrNull(path) {
|
|
20
|
+
try { return await lstat(path); }
|
|
21
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function filesUnder(root, relative) {
|
|
25
|
+
const path = join(root, relative);
|
|
26
|
+
const stat = await lstat(path);
|
|
27
|
+
if (stat.isSymbolicLink()) throw new Error(`Symlink in bundle: ${relative}`);
|
|
28
|
+
if (stat.isFile()) return [relative];
|
|
29
|
+
if (!stat.isDirectory()) throw new Error(`Unsupported bundle entry: ${relative}`);
|
|
30
|
+
const names = (await readdir(path)).sort();
|
|
31
|
+
const files = [];
|
|
32
|
+
for (const name of names) files.push(...await filesUnder(root, join(relative, name)));
|
|
33
|
+
return files;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function checkDestination(target, relative) {
|
|
37
|
+
const rootStat = await statOrNull(target);
|
|
38
|
+
if (rootStat && (!rootStat.isDirectory() || rootStat.isSymbolicLink())) {
|
|
39
|
+
throw new Error('Target must be a directory, not a symlink');
|
|
40
|
+
}
|
|
41
|
+
const parts = relative.split('/');
|
|
42
|
+
for (let count = 1; count < parts.length; count += 1) {
|
|
43
|
+
const path = join(target, ...parts.slice(0, count));
|
|
44
|
+
const stat = await statOrNull(path);
|
|
45
|
+
if (stat && (!stat.isDirectory() || stat.isSymbolicLink())) {
|
|
46
|
+
throw new Error(`Destination parent is not a plain directory: ${path}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const path = join(target, relative);
|
|
50
|
+
const stat = await statOrNull(path);
|
|
51
|
+
if (stat && (!stat.isFile() || stat.isSymbolicLink())) {
|
|
52
|
+
throw new Error(`Destination is not a plain file: ${path}`);
|
|
53
|
+
}
|
|
54
|
+
return stat;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Exact generated files from a pinned upstream revision. Other releases and
|
|
58
|
+
// local modifications remain conflicts rather than assumed ownership.
|
|
59
|
+
const legacyInventory = JSON.parse(await readFile(new URL('./legacy-inventory.json', import.meta.url), 'utf8'));
|
|
60
|
+
const isKnownLegacySkill = (path, bytes) => legacyInventory.files[path] === sha256(bytes);
|
|
61
|
+
|
|
62
|
+
async function readInstallationManifest(target) {
|
|
63
|
+
await checkDestination(target, installationManifestPath);
|
|
64
|
+
let text;
|
|
65
|
+
try { text = await readFile(join(target, installationManifestPath), 'utf8'); }
|
|
66
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
67
|
+
let manifest;
|
|
68
|
+
try { manifest = JSON.parse(text); } catch { throw new Error('Invalid Joycraft installation manifest; review it before using --upgrade'); }
|
|
69
|
+
if (manifest?.schemaVersion !== 1 || !Array.isArray(manifest.files)
|
|
70
|
+
|| manifest.files.some(entry => !entry || typeof entry.path !== 'string' || !/^[a-f0-9]{64}$/.test(entry.sha256))) {
|
|
71
|
+
throw new Error('Unsupported Joycraft installation manifest; review it before using --upgrade');
|
|
72
|
+
}
|
|
73
|
+
const entries = new Map();
|
|
74
|
+
for (const entry of manifest.files) {
|
|
75
|
+
if (relativePath(entry.path) !== entry.path || entries.has(entry.path)) throw new Error('Unsafe or duplicate installation manifest path');
|
|
76
|
+
entries.set(entry.path, entry.sha256);
|
|
77
|
+
}
|
|
78
|
+
return entries;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function prepareBackup(target, backupRoot) {
|
|
82
|
+
const ignored = join(backupRoot, '.gitignore');
|
|
83
|
+
await checkDestination(target, relative(target, ignored));
|
|
84
|
+
await mkdir(backupRoot, { recursive: true, mode: 0o700 });
|
|
85
|
+
await writeFile(ignored, '*\n', { flag: 'wx', mode: 0o600 });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function backupFile(target, path, bytes, backupRoot) {
|
|
89
|
+
const backup = join(backupRoot, path);
|
|
90
|
+
await checkDestination(target, relative(target, backup));
|
|
91
|
+
await mkdir(dirname(backup), { recursive: true, mode: 0o700 });
|
|
92
|
+
await writeFile(backup, bytes, { flag: 'wx', mode: 0o600 });
|
|
93
|
+
return relative(target, backup);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function legacyPlan(target, installed, canonicalDestinations) {
|
|
97
|
+
const archive = []; const conflicts = [];
|
|
98
|
+
for (const path of legacyDiscoveryPaths) {
|
|
99
|
+
const parts = path.split('/'); let unsafe = false;
|
|
100
|
+
for (let count = 1; count < parts.length; count += 1) { const parent = await statOrNull(join(target, ...parts.slice(0, count))); if (parent?.isSymbolicLink() || (parent && !parent.isDirectory())) { unsafe = true; break; } }
|
|
101
|
+
if (unsafe) { conflicts.push({ path, reason: 'legacy discovery parent is not a plain directory' }); continue; }
|
|
102
|
+
const stat = await statOrNull(join(target, path));
|
|
103
|
+
if (!stat) continue;
|
|
104
|
+
if (stat.isSymbolicLink()) { conflicts.push({ path, reason: 'symlinked legacy discovery path' }); continue; }
|
|
105
|
+
if (!stat.isDirectory()) { conflicts.push({ path, reason: 'legacy discovery path is not a directory' }); continue; }
|
|
106
|
+
const entries = await readdir(join(target, path));
|
|
107
|
+
const skillPath = `${path}/SKILL.md`;
|
|
108
|
+
const skillStat = entries.length === 1 && entries[0] === 'SKILL.md' ? await lstat(join(target, skillPath)) : null;
|
|
109
|
+
const skillBytes = skillStat?.isFile() && !skillStat.isSymbolicLink() ? await readFile(join(target, skillPath)) : null;
|
|
110
|
+
// A byte-verified installed skill owned by this export is current bundle
|
|
111
|
+
// content, not an unknown legacy directory merely because names overlap.
|
|
112
|
+
if (skillBytes && canonicalDestinations.has(skillPath) && installed?.get(skillPath) === sha256(skillBytes)) continue;
|
|
113
|
+
const known = skillBytes && isKnownLegacySkill(skillPath, skillBytes);
|
|
114
|
+
if (!known) conflicts.push({ path, reason: 'contains bytes not in the pinned legacy inventory' });
|
|
115
|
+
else archive.push(path);
|
|
116
|
+
}
|
|
117
|
+
return { archive, conflicts };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Move an install from the pre-1.0 `.joycraft-prototype/` directory to `.joycraft/`, once.
|
|
121
|
+
* Backs up the old directory first, never touches `.joycraft/local/` or other project-owned
|
|
122
|
+
* state, and refuses to replace a different file. A second run finds nothing to move. */
|
|
123
|
+
export async function migrateBundleDir(targetRoot, { check = false, stamp = new Date().toISOString().replace(/[:.]/g, '-') } = {}) {
|
|
124
|
+
const target = resolve(targetRoot);
|
|
125
|
+
const old = join(target, previousBundleDir);
|
|
126
|
+
const oldStat = await statOrNull(old);
|
|
127
|
+
if (!oldStat) return { moved: [], hookFiles: [], backup: null };
|
|
128
|
+
if (!oldStat.isDirectory() || oldStat.isSymbolicLink()) throw new Error(`${previousBundleDir} is not a plain directory`);
|
|
129
|
+
const moved = (await filesUnder(target, previousBundleDir)).map(path => ({ from: path, to: `.joycraft/${path.slice(previousBundleDir.length + 1)}` }));
|
|
130
|
+
for (const { from, to } of moved) {
|
|
131
|
+
if (projectOwned(to)) throw new Error(`${from} would replace project state at ${to}; move it by hand`);
|
|
132
|
+
await checkDestination(target, to);
|
|
133
|
+
const existing = await statOrNull(join(target, to));
|
|
134
|
+
if (existing && !(await readFile(join(target, to))).equals(await readFile(join(target, from)))) throw new Error(`Both ${from} and a different ${to} exist; reconcile them before upgrading`);
|
|
135
|
+
}
|
|
136
|
+
const hookFiles = [];
|
|
137
|
+
for (const file of managedHookFiles) {
|
|
138
|
+
const text = (await statOrNull(join(target, file))) ? await readFile(join(target, file), 'utf8') : null;
|
|
139
|
+
if (text?.includes(`${previousBundleDir}/`)) hookFiles.push(file);
|
|
140
|
+
}
|
|
141
|
+
if (check) return { moved, hookFiles, backup: null };
|
|
142
|
+
const backup = join('.joycraft-backups', `bundle-dir-${stamp}`);
|
|
143
|
+
await prepareBackup(target, join(target, backup));
|
|
144
|
+
await cp(old, join(target, backup, previousBundleDir), { recursive: true, errorOnExist: true, force: false });
|
|
145
|
+
for (const file of hookFiles) {
|
|
146
|
+
await mkdir(dirname(join(target, backup, file)), { recursive: true });
|
|
147
|
+
await cp(join(target, file), join(target, backup, file));
|
|
148
|
+
// Only Joycraft's own launcher names the bundle directory; user hooks keep their bytes.
|
|
149
|
+
await writeFile(join(target, file), (await readFile(join(target, file), 'utf8')).replaceAll(`${previousBundleDir}/`, '.joycraft/'));
|
|
150
|
+
}
|
|
151
|
+
for (const { from, to } of moved) {
|
|
152
|
+
await mkdir(dirname(join(target, to)), { recursive: true });
|
|
153
|
+
await rename(join(target, from), join(target, to));
|
|
154
|
+
}
|
|
155
|
+
const manifest = join(target, installationManifestPath);
|
|
156
|
+
if (await statOrNull(manifest)) {
|
|
157
|
+
const text = await readFile(manifest, 'utf8');
|
|
158
|
+
await writeFile(manifest, text.replaceAll(`"${previousBundleDir}/`, '".joycraft/'));
|
|
159
|
+
}
|
|
160
|
+
await rm(old, { recursive: true });
|
|
161
|
+
return { moved, hookFiles, backup };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Copy the explicit public bundle only. Never replace a different user file. */
|
|
165
|
+
export async function exportBundle({ sourceRoot, targetRoot, check = false, hooks = false, harness = 'codex', upgrade = false }) {
|
|
166
|
+
if (!['codex', 'claude', 'pi', 'omp', 'github-copilot', 'opencode'].includes(harness)) throw new Error('Unknown harness');
|
|
167
|
+
const source = resolve(sourceRoot);
|
|
168
|
+
const target = resolve(targetRoot);
|
|
169
|
+
if (source === target) throw new Error('Source and target must differ');
|
|
170
|
+
const bundleMove = upgrade ? await migrateBundleDir(target, { check }) : null;
|
|
171
|
+
const files = (await filesUnder(source, '.joycraft')).filter(path => !mutableBundlePaths.has(path) && !projectOwned(path));
|
|
172
|
+
const skillsPath = join(source, '.agents/skills');
|
|
173
|
+
const skillsStat = await statOrNull(skillsPath);
|
|
174
|
+
if (skillsStat) {
|
|
175
|
+
if (!skillsStat.isDirectory() || skillsStat.isSymbolicLink()) throw new Error('Invalid skills directory');
|
|
176
|
+
for (const name of (await readdir(skillsPath)).sort()) {
|
|
177
|
+
if (/^joycraft-[a-z0-9-]+$/.test(name)) {
|
|
178
|
+
files.push(...await filesUnder(source, join('.agents/skills', name)));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Claude Code gets the generic implementer and reviewer agent types.
|
|
183
|
+
if (harness === 'claude' && (await statOrNull(join(source, '.claude/agents')))?.isDirectory()) {
|
|
184
|
+
for (const name of (await readdir(join(source, '.claude/agents'))).sort()) {
|
|
185
|
+
if (/^joycraft-[a-z0-9-]+\.md$/.test(name)) files.push(...await filesUnder(source, join('.claude/agents', name)));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
files.sort();
|
|
189
|
+
const destinationPath = path => harness === 'claude' ? path.replace(/^\.agents\/skills\//, '.claude/skills/') : path;
|
|
190
|
+
const bundleBytes = async path => {
|
|
191
|
+
const bytes = await readFile(join(source, path));
|
|
192
|
+
return harness === 'claude' && path.endsWith('.md') ? Buffer.from(bytes.toString('utf8').replaceAll('.agents/skills/', '.claude/skills/')) : bytes;
|
|
193
|
+
};
|
|
194
|
+
// Native hooks are optional; their installer loads only when hooks are requested.
|
|
195
|
+
const hookInstaller = hooks ? await import('./install-hooks.mjs') : null;
|
|
196
|
+
if (hooks && !files.includes(hookInstaller.hookRuntimePath)) throw new Error('The source bundle has no hook runtime');
|
|
197
|
+
const installed = await readInstallationManifest(target);
|
|
198
|
+
const legacy = upgrade ? await legacyPlan(target, installed, new Set(files.map(destinationPath))) : { archive: [], conflicts: [] };
|
|
199
|
+
const legacyHooks = upgrade ? await planLegacyHooks({ targetRoot: target }) : null;
|
|
200
|
+
if (legacyHooks?.conflicts.length) throw new Error(`Legacy native hooks need reconciliation before upgrade: ${legacyHooks.conflicts.map(item => item.path).join(', ')}`);
|
|
201
|
+
const pending = [];
|
|
202
|
+
const replacements = [];
|
|
203
|
+
for (const relative of files) {
|
|
204
|
+
const path = destinationPath(relative);
|
|
205
|
+
const stat = await checkDestination(target, path);
|
|
206
|
+
const wanted = await bundleBytes(relative);
|
|
207
|
+
if (!stat) pending.push({ relative, path, wanted });
|
|
208
|
+
else {
|
|
209
|
+
const actual = await readFile(join(target, path));
|
|
210
|
+
if (!wanted.equals(actual)) {
|
|
211
|
+
const known = installed?.get(path) === sha256(actual) || (!installed && isKnownLegacySkill(path, actual));
|
|
212
|
+
if (!upgrade || !known) throw new Error(`Refusing to replace existing file: ${relative}${upgrade ? ' (it is not a recognized prior Joycraft file)' : ''}`);
|
|
213
|
+
replacements.push({ relative, path, wanted, actual });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const adapters = hooks ? hookInstaller.hookAdapters(harness) : [];
|
|
218
|
+
for (const adapter of adapters) {
|
|
219
|
+
const stat = await checkDestination(target, adapter.path); const wanted = await readFile(join(source, adapter.source));
|
|
220
|
+
if (!stat) pending.push({ relative: adapter.source, path: adapter.path, wanted });
|
|
221
|
+
else { const actual = await readFile(join(target, adapter.path)); if (!wanted.equals(actual)) {
|
|
222
|
+
const known = installed?.get(adapter.path) === sha256(actual);
|
|
223
|
+
if (!upgrade || !known) throw new Error(`Refusing to replace existing adapter: ${adapter.path}`);
|
|
224
|
+
replacements.push({ relative: adapter.source, path: adapter.path, wanted, actual });
|
|
225
|
+
} }
|
|
226
|
+
}
|
|
227
|
+
let sourceRevision = 'unavailable';
|
|
228
|
+
try { sourceRevision = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: source, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch {}
|
|
229
|
+
const manifestFiles = await Promise.all(files.map(async relative => ({ path: destinationPath(relative), sha256: sha256(await bundleBytes(relative)) })));
|
|
230
|
+
const adapterFiles = [];
|
|
231
|
+
for (const adapter of adapters) adapterFiles.push({ path: adapter.path, sha256: sha256(await readFile(join(source, adapter.source))) });
|
|
232
|
+
const manifest = { schemaVersion: 1, sourceRevision, harness, files: [...manifestFiles, ...adapterFiles], legacyConflicts: legacy.conflicts };
|
|
233
|
+
manifest.files.sort((a, b) => a.path.localeCompare(b.path));
|
|
234
|
+
const manifestBytes = Buffer.from(`${JSON.stringify(manifest, null, 2)}\n`);
|
|
235
|
+
const manifestStat = await statOrNull(join(target, installationManifestPath));
|
|
236
|
+
const hooksPlan = hooks && !['pi', 'omp', 'opencode'].includes(harness) ? await hookInstaller.planCodexHooks({ targetRoot: target, harness }) : null;
|
|
237
|
+
const backupRoot = join(target, '.joycraft-backups', `upgrade-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomUUID()}`);
|
|
238
|
+
legacy.archive = legacy.archive.filter(path => !manifest.files.some(file => file.path.startsWith(`${path}/`)));
|
|
239
|
+
if (!check) {
|
|
240
|
+
if (replacements.length || legacy.archive.length || legacyHooks?.backups.length) await prepareBackup(target, backupRoot);
|
|
241
|
+
for (const { path, wanted } of pending) {
|
|
242
|
+
const destination = join(target, path);
|
|
243
|
+
await checkDestination(target, path);
|
|
244
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
245
|
+
await writeFile(destination, wanted, { flag: 'wx' });
|
|
246
|
+
}
|
|
247
|
+
for (const { path, wanted, actual } of replacements) {
|
|
248
|
+
const destination = join(target, path);
|
|
249
|
+
if (!(await readFile(destination)).equals(actual)) throw new Error(`Destination changed during upgrade: ${path}; rerun the preview`);
|
|
250
|
+
await backupFile(target, path, actual, backupRoot);
|
|
251
|
+
await writeFile(destination, wanted);
|
|
252
|
+
}
|
|
253
|
+
for (const path of legacy.archive) {
|
|
254
|
+
const sourcePath = join(target, path); const destination = join(backupRoot, 'legacy-discovery', path);
|
|
255
|
+
await mkdir(dirname(destination), { recursive: true }); await rename(sourcePath, destination);
|
|
256
|
+
}
|
|
257
|
+
const previousManifest = manifestStat ? await readFile(join(target, installationManifestPath)) : null;
|
|
258
|
+
if (!previousManifest || !previousManifest.equals(manifestBytes)) {
|
|
259
|
+
await checkDestination(target, installationManifestPath);
|
|
260
|
+
await mkdir(dirname(join(target, installationManifestPath)), { recursive: true });
|
|
261
|
+
const temp = join(target, '.joycraft', `installation.${randomUUID()}.tmp`);
|
|
262
|
+
await writeFile(temp, manifestBytes, { flag: 'wx' });
|
|
263
|
+
await rename(temp, join(target, installationManifestPath));
|
|
264
|
+
}
|
|
265
|
+
if (hooksPlan) await hookInstaller.applyCodexHooks(hooksPlan);
|
|
266
|
+
if (legacyHooks) {
|
|
267
|
+
const currentLegacyHooks = await planLegacyHooks({ targetRoot: target });
|
|
268
|
+
if (currentLegacyHooks.conflicts.length) throw new Error('Legacy hooks changed during upgrade; reconcile them before continuing');
|
|
269
|
+
await applyLegacyHooks(currentLegacyHooks, { backupRoot });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return { check, harness, files: manifest.files.map(entry => entry.path), added: pending.map(item => item.path), replaced: replacements.map(item => item.path), archivedLegacyDiscovery: legacy.archive, legacyConflicts: legacy.conflicts, unchanged: manifest.files.length - pending.length - replacements.length, manifest: installationManifestPath,
|
|
273
|
+
...(legacyHooks ? { legacyHooks } : {}),
|
|
274
|
+
...(bundleMove?.moved.length ? { bundleMove } : {}),
|
|
275
|
+
...(replacements.length ? { backups: check ? replacements.map(item => item.path) : replacements.map(item => relative(target, join(backupRoot, item.path))) } : {}),
|
|
276
|
+
...(hooksPlan ? { hooks: hookInstaller.hookPlanSummary(hooksPlan) } : {}) };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
280
|
+
try {
|
|
281
|
+
const args = process.argv.slice(2);
|
|
282
|
+
let harness = 'codex';
|
|
283
|
+
const harnessIndex = args.indexOf('--harness');
|
|
284
|
+
if (harnessIndex >= 0) { harness = args[harnessIndex + 1]; args.splice(harnessIndex, 2); }
|
|
285
|
+
const targetIndex = args.indexOf('--target');
|
|
286
|
+
const target = targetIndex >= 0 ? args[targetIndex + 1] : undefined;
|
|
287
|
+
const remaining = args.filter((_, index) => index !== targetIndex && index !== targetIndex + 1);
|
|
288
|
+
if (!target || target.startsWith('--') || remaining.some(arg => !['--check', '--without-hooks', '--upgrade'].includes(arg)) || new Set(remaining).size !== remaining.length) {
|
|
289
|
+
throw new Error('Usage: node scripts/export.mjs --target <project> [--check] [--upgrade] [--without-hooks] [--harness codex|claude|pi|omp|github-copilot|opencode]');
|
|
290
|
+
}
|
|
291
|
+
const sourceRoot = fileURLToPath(new URL('..', import.meta.url));
|
|
292
|
+
const hooks = !remaining.includes('--without-hooks') && harness !== 'opencode';
|
|
293
|
+
const result = await exportBundle({ sourceRoot, targetRoot: target, check: remaining.includes('--check'), upgrade: remaining.includes('--upgrade'), hooks, harness });
|
|
294
|
+
let revision = 'unavailable';
|
|
295
|
+
try { revision = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: sourceRoot, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch {}
|
|
296
|
+
console.log(JSON.stringify({ revision, ...result }, null, 2));
|
|
297
|
+
} catch (error) {
|
|
298
|
+
console.error(error.message);
|
|
299
|
+
process.exitCode = 1;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Hook commands run from whatever directory the agent is in: a worktree, a
|
|
2
|
+
// subdirectory or the project root. A relative script path then exits 127.
|
|
3
|
+
// Claude Code exports the project root as CLAUDE_PROJECT_DIR; quote it so a
|
|
4
|
+
// path with spaces stays one word.
|
|
5
|
+
export const claudeProjectDir = '"$CLAUDE_PROJECT_DIR"';
|
|
6
|
+
|
|
7
|
+
// The stable 0.7 safeguard registered its scripts by bare relative path.
|
|
8
|
+
const relativeJoycraftHook = /^(?:(bash|sh|node)\s+)?(?:\.\/)?(\.claude\/hooks\/joycraft\/[\w.-]+)$/;
|
|
9
|
+
|
|
10
|
+
/** The command that runs a project script from the Claude project root. */
|
|
11
|
+
export function claudeHookCommand(path, interpreter = null) {
|
|
12
|
+
return `${interpreter ? `${interpreter} ` : ''}${claudeProjectDir}/${path}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Rewrite relative Joycraft hook commands in Claude settings. Every other
|
|
16
|
+
* entry, including user hooks, keeps its exact value. */
|
|
17
|
+
export function resolveRelativeJoycraftHooks(settings) {
|
|
18
|
+
const rewritten = [];
|
|
19
|
+
const hooks = settings?.hooks;
|
|
20
|
+
if (!hooks || typeof hooks !== 'object' || Array.isArray(hooks)) return { settings, rewritten };
|
|
21
|
+
const next = {};
|
|
22
|
+
for (const [event, groups] of Object.entries(hooks)) {
|
|
23
|
+
next[event] = !Array.isArray(groups) ? groups : groups.map(group => {
|
|
24
|
+
if (!Array.isArray(group?.hooks)) return group;
|
|
25
|
+
return { ...group, hooks: group.hooks.map(hook => {
|
|
26
|
+
const match = hook?.type === 'command' && typeof hook.command === 'string' ? relativeJoycraftHook.exec(hook.command.trim()) : null;
|
|
27
|
+
if (!match) return hook;
|
|
28
|
+
const command = claudeHookCommand(match[2], match[1] ?? null);
|
|
29
|
+
rewritten.push({ event, from: hook.command, to: command });
|
|
30
|
+
return { ...hook, command };
|
|
31
|
+
}) };
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return { settings: rewritten.length ? { ...settings, hooks: next } : settings, rewritten };
|
|
35
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
4
|
+
import { resolveRelativeJoycraftHooks } from './hook-paths.mjs';
|
|
5
|
+
|
|
6
|
+
const claudeHandler = '.claude/hooks/joycraft-version-check.mjs';
|
|
7
|
+
const claudeSettings = '.claude/settings.json';
|
|
8
|
+
const claudeCommand = 'node .claude/hooks/joycraft-version-check.mjs';
|
|
9
|
+
const piPipeline = '.pi/extensions/joycraft-pipeline.ts';
|
|
10
|
+
const knownBytes = new Map([
|
|
11
|
+
[claudeHandler, '6bb3f624676067f66bd2b31dba5551f4af65164267abc449cb2fe7cc8771fb31'],
|
|
12
|
+
[piPipeline, 'ef5468ee36fd6c5c7358153b498c5b6b383e9f5be4138751e0e695c8468b02a8'],
|
|
13
|
+
]);
|
|
14
|
+
const privatePlans = new WeakMap();
|
|
15
|
+
|
|
16
|
+
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
17
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
18
|
+
|
|
19
|
+
async function statOrNull(path) {
|
|
20
|
+
try { return await lstat(path); }
|
|
21
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function plainFile(root, path) {
|
|
25
|
+
await plainParents(root, path);
|
|
26
|
+
const full = join(root, path);
|
|
27
|
+
const stat = await statOrNull(full);
|
|
28
|
+
if (!stat) return null;
|
|
29
|
+
if (!stat.isFile() || stat.isSymbolicLink()) throw new Error(`Legacy hook path is not a plain file: ${path}`);
|
|
30
|
+
return readFile(full);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function plainParents(root, path) {
|
|
34
|
+
const parts = path.split('/');
|
|
35
|
+
for (let index = 1; index < parts.length; index += 1) {
|
|
36
|
+
const current = join(root, ...parts.slice(0, index));
|
|
37
|
+
const stat = await statOrNull(current);
|
|
38
|
+
if (stat && (!stat.isDirectory() || stat.isSymbolicLink())) throw new Error(`Legacy hook parent is not a plain directory: ${parts.slice(0, index).join('/')}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function removeClaudeCommand(settings) {
|
|
43
|
+
if (!object(settings) || (settings.hooks !== undefined && !object(settings.hooks))) throw new Error('Legacy Claude settings are not a supported JSON object');
|
|
44
|
+
const hooks = settings.hooks ?? {};
|
|
45
|
+
const changed = [];
|
|
46
|
+
for (const [event, groups] of Object.entries(hooks)) {
|
|
47
|
+
if (!Array.isArray(groups)) throw new Error(`Legacy Claude hook groups are unsupported for ${event}`);
|
|
48
|
+
const retained = [];
|
|
49
|
+
for (const group of groups) {
|
|
50
|
+
if (!object(group) || !Array.isArray(group.hooks)) throw new Error(`Legacy Claude hook group is unsupported for ${event}`);
|
|
51
|
+
const nextHooks = group.hooks.filter(hook => !(object(hook) && hook.type === 'command' && hook.command === claudeCommand));
|
|
52
|
+
if (nextHooks.length !== group.hooks.length) changed.push({ event, removed: group.hooks.length - nextHooks.length });
|
|
53
|
+
if (nextHooks.length) retained.push({ ...group, hooks: nextHooks });
|
|
54
|
+
}
|
|
55
|
+
if (retained.length) hooks[event] = retained;
|
|
56
|
+
else delete hooks[event];
|
|
57
|
+
}
|
|
58
|
+
return { settings: { ...settings, ...(Object.keys(hooks).length ? { hooks } : {}) }, changed };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Inspect only exact legacy native-hook surfaces. Unknown bytes are conflicts,
|
|
63
|
+
* never evidence that a hook is ours to replace or archive.
|
|
64
|
+
*/
|
|
65
|
+
export async function planLegacyHooks({ targetRoot }) {
|
|
66
|
+
const root = resolve(targetRoot);
|
|
67
|
+
const archive = []; const conflicts = []; let settingsBefore = null; let settingsAfter = null; let removedHandlers = []; let resolvedCommands = [];
|
|
68
|
+
const [claudeBytes, piBytes, settingsBytes] = await Promise.all([
|
|
69
|
+
plainFile(root, claudeHandler), plainFile(root, piPipeline), plainFile(root, claudeSettings),
|
|
70
|
+
]);
|
|
71
|
+
const exactClaude = claudeBytes && sha256(claudeBytes) === knownBytes.get(claudeHandler);
|
|
72
|
+
const exactPi = piBytes && sha256(piBytes) === knownBytes.get(piPipeline);
|
|
73
|
+
if (claudeBytes && !exactClaude) conflicts.push({ path: claudeHandler, reason: 'custom legacy hook bytes preserved' });
|
|
74
|
+
else if (exactClaude) archive.push(claudeHandler);
|
|
75
|
+
if (piBytes && !exactPi) conflicts.push({ path: piPipeline, reason: 'custom legacy Pi extension preserved' });
|
|
76
|
+
else if (exactPi) archive.push(piPipeline);
|
|
77
|
+
if (settingsBytes) {
|
|
78
|
+
let parsed;
|
|
79
|
+
try { parsed = JSON.parse(settingsBytes.toString('utf8')); } catch { throw new Error('Legacy Claude settings are not valid JSON'); }
|
|
80
|
+
const migration = removeClaudeCommand(parsed);
|
|
81
|
+
const resolved = resolveRelativeJoycraftHooks(migration.settings);
|
|
82
|
+
if (migration.changed.length || resolved.rewritten.length) {
|
|
83
|
+
settingsBefore = settingsBytes;
|
|
84
|
+
settingsAfter = Buffer.from(`${JSON.stringify(resolved.settings, null, 2)}\n`);
|
|
85
|
+
removedHandlers = migration.changed;
|
|
86
|
+
resolvedCommands = resolved.rewritten;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const plan = { targetRoot: root, archive, conflicts, removedHandlers, resolvedCommands, settings: settingsAfter ? { path: claudeSettings, changed: true } : { path: claudeSettings, changed: false }, backups: [...archive, ...(settingsAfter ? [claudeSettings] : [])] };
|
|
90
|
+
// Settings can include private project configuration. Keep exact comparison
|
|
91
|
+
// bytes only in memory so a dry-run result is safe to print as JSON.
|
|
92
|
+
privatePlans.set(plan, { settingsBefore, settingsAfter });
|
|
93
|
+
return plan;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Apply a reviewed plan. All modified old bytes are copied to a local backup. */
|
|
97
|
+
export async function applyLegacyHooks(plan, { backupRoot } = {}) {
|
|
98
|
+
const root = resolve(plan.targetRoot);
|
|
99
|
+
const privatePlan = privatePlans.get(plan);
|
|
100
|
+
if (!privatePlan) throw new Error('Legacy hook plan is not available for application; rerun the preview in this process');
|
|
101
|
+
const destinationRoot = backupRoot ? resolve(backupRoot) : join(root, '.joycraft-backups', `legacy-hooks-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomUUID()}`);
|
|
102
|
+
if (!relative(root, destinationRoot) || relative(root, destinationRoot).startsWith('..')) throw new Error('Legacy hook backup must remain inside the target project');
|
|
103
|
+
if (plan.archive.length || privatePlan.settingsAfter) {
|
|
104
|
+
await plainParents(root, `${relative(root, destinationRoot)}/.gitignore`);
|
|
105
|
+
await mkdir(destinationRoot, { recursive: true, mode: 0o700 });
|
|
106
|
+
const ignored = join(destinationRoot, '.gitignore');
|
|
107
|
+
const existingIgnore = await statOrNull(ignored);
|
|
108
|
+
if (existingIgnore && (existingIgnore.isSymbolicLink() || !existingIgnore.isFile())) throw new Error('Unsafe legacy backup ignore file');
|
|
109
|
+
if (!existingIgnore) await writeFile(ignored, '*\n', { flag: 'wx', mode: 0o600 });
|
|
110
|
+
}
|
|
111
|
+
if (privatePlan.settingsAfter) {
|
|
112
|
+
const current = await plainFile(root, claudeSettings);
|
|
113
|
+
if (!current || !current.equals(privatePlan.settingsBefore)) throw new Error('Legacy Claude settings changed after preview; rerun migration');
|
|
114
|
+
await plainParents(root, claudeSettings);
|
|
115
|
+
const backup = join(destinationRoot, claudeSettings);
|
|
116
|
+
await mkdir(dirname(backup), { recursive: true }); await writeFile(backup, current, { flag: 'wx', mode: 0o600 });
|
|
117
|
+
const temporary = `${join(root, claudeSettings)}.${randomUUID()}.tmp`;
|
|
118
|
+
await writeFile(temporary, privatePlan.settingsAfter, { flag: 'wx', mode: 0o600 }); await rename(temporary, join(root, claudeSettings));
|
|
119
|
+
}
|
|
120
|
+
for (const path of plan.archive) {
|
|
121
|
+
const bytes = await plainFile(root, path);
|
|
122
|
+
if (!bytes || sha256(bytes) !== knownBytes.get(path)) throw new Error(`Legacy hook changed after preview: ${path}`);
|
|
123
|
+
await plainParents(root, path);
|
|
124
|
+
const backup = join(destinationRoot, path);
|
|
125
|
+
await mkdir(dirname(backup), { recursive: true }); await rename(join(root, path), backup);
|
|
126
|
+
}
|
|
127
|
+
return { ...plan, backups: plan.archive.length || privatePlan.settingsAfter ? relative(root, destinationRoot) : null };
|
|
128
|
+
}
|