fix(ck): refuse to re-initialize over an unreadable context

When a registered project's context.json exists but cannot be parsed,
loadContext() returns null and init wrote a fresh context over the file,
destroying history that could still be recovered by hand. Stop with a
message and leave the files alone instead.

Tests: forgetting a project renamed by init leaves no context directory
behind, and init over an unreadable context.json exits 1 without
touching it. Rebind the skill triggers manifest to the new registry
digest.

Refs #3922
This commit is contained in:
adityajai119 committed 2026-11-11 23:50:17 +05:30
1 parent 71737c8c38
commit 3ecdcac3b4
3 files changed
+24 -3

No files matched your search

@@ -10,7 +10,7 @@
"id": "hand-seeded",
"source": "manual-curation-pending-regeneration"
},
"registryDigest": "7f8d410b92a3c39618e44fe9f7f9ecbe926f5626405684fa6c11c52ad389ad07",
"registryDigest": "7f1ea8d6abde21485bf7134fcb54d08ebe30e7d475d1fe89d10e03492300742c",
"schemaVersion": 1,
"triggers": {
"skill:agent-runtime-gateway-smoke-test": [
+6 -2
View File
@@ -16,11 +16,11 @@
* exit 0: success exit 1: error
*/
import { readFileSync, mkdirSync, writeFileSync } from 'fs';
import { readFileSync, mkdirSync, writeFileSync, existsSync } from 'fs';
import { resolve } from 'path';
import { createHash } from 'crypto';
import {
readProjects, writeProjects, loadContext, saveContext,
readProjects, writeProjects, loadContext, saveContext, contextPath,
today, shortId, gitSummary, nativeMemoryDir,
CURRENT_SESSION,
} from './shared.mjs';
@@ -55,6 +55,10 @@ if (isInit) {
const projects = readProjects();
const registered = projects[projectPath];
const previous = registered ? loadContext(registered.contextDir) : null;
if (registered && !previous && existsSync(contextPath(registered.contextDir))) {
console.log(`ck init: ${contextPath(registered.contextDir)} cannot be read. Nothing was changed; fix or move that file, then run init again.`);
process.exit(1);
}
// Derive contextDir (lowercase, spaces→dashes, deduplicate). Names with no
// ASCII letters/digits (e.g. "Проект") fall back to a stable path hash, so
+17
View File
@@ -77,6 +77,23 @@ test('initializing a registered project under a new name keeps its context direc
const context = JSON.parse(fs.readFileSync(path.join(contextsDir, 'app', 'context.json'), 'utf8'));
assert.strictEqual(context.displayName, 'App Server');
assert.deepStrictEqual(context.sessions.map(s => s.summary), ['built the parser']);
const forget = run(home, project, 'forget.mjs', ['App Server']);
assert.strictEqual(forget.status, 0, forget.stdout + forget.stderr);
assert.deepStrictEqual(fs.readdirSync(contextsDir), []);
});
test('initializing a registered project whose context cannot be read changes nothing', home => {
const project = path.join(home, 'app');
fs.mkdirSync(project);
runOk(home, project, 'save.mjs', ['--init'], { name: 'app', path: project });
const contextFile = path.join(home, '.claude', 'ck', 'contexts', 'app', 'context.json');
const damaged = '{"version": 2, "sessions": [{"summary": "built the parser"}';
fs.writeFileSync(contextFile, damaged);
const result = run(home, project, 'save.mjs', ['--init'], JSON.stringify({ name: 'app', path: project }));
assert.strictEqual(result.status, 1, result.stdout + result.stderr);
assert.strictEqual(fs.readFileSync(contextFile, 'utf8'), damaged);
});
console.log(`\nPassed: ${passed}`);