fix(ck): find a project from either Windows path form

projects.json is keyed by PWD or process.cwd(), and lookups compared
that string exactly. On Windows, Git Bash passes C:/... and PowerShell
gives C:\..., so a folder registered from one shell was unknown in the
other and init registered it a second time. Match registrations by
exact key first, then by resolved path, in save, init, resume, list and
the SessionStart hook, and rebind the skill triggers manifest to the
new registry digest.

Fixes #3924
This commit is contained in:
adityajai119 committed 2026-11-11 23:39:39 +05:30
1 parent d2ef268059
commit 7aebf1f2e2
7 files changed
+85 -13

No files matched your search

@@ -10,7 +10,7 @@
"id": "hand-seeded",
"source": "manual-curation-pending-regeneration"
},
"registryDigest": "6f213dc845937b4093bc018d041b25b538d88d78451b9d799ad614989b360ee4",
"registryDigest": "e47ea43be9bce75b6659b087008304205104e06b07e524f9b245ee428fac9bf3",
"schemaVersion": 1,
"triggers": {
"skill:agent-runtime-gateway-smoke-test": [
+2 -2
View File
@@ -10,7 +10,7 @@
import { readFileSync, existsSync } from 'fs';
import { resolve, basename } from 'path';
import { readProjects } from './shared.mjs';
import { readProjects, findProject } from './shared.mjs';
const cwd = process.env.PWD || process.cwd();
const projects = readProjects();
@@ -23,7 +23,7 @@ const output = {
goal: null,
constraints: [],
repo: null,
alreadyRegistered: !!projects[cwd],
alreadyRegistered: !!findProject(projects, cwd),
};
function readFile(filename) {
+2 -1
View File
@@ -9,6 +9,7 @@
*/
import { existsSync } from 'fs';
import { resolve } from 'path';
import { resolveContext, renderBriefingBox } from './shared.mjs';
const arg = process.argv[2];
@@ -24,7 +25,7 @@ if (!resolved) {
const { context, projectPath } = resolved;
// Attempt to cd to the project path
if (projectPath && projectPath !== cwd) {
if (projectPath && resolve(projectPath) !== resolve(cwd)) {
if (existsSync(projectPath)) {
console.log(`→ cd ${projectPath}`);
} else {
+6 -5
View File
@@ -20,7 +20,7 @@ import { readFileSync, mkdirSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { createHash } from 'crypto';
import {
readProjects, writeProjects, loadContext, saveContext,
readProjects, writeProjects, findProject, loadContext, saveContext,
today, shortId, gitSummary, nativeMemoryDir,
CURRENT_SESSION,
} from './shared.mjs';
@@ -53,6 +53,7 @@ if (isInit) {
}
const projects = readProjects();
const registered = findProject(projects, projectPath);
// Derive contextDir (lowercase, spaces→dashes, deduplicate). Names with no
// ASCII letters/digits (e.g. "Проект") fall back to a stable path hash, so
@@ -61,7 +62,7 @@ if (isInit) {
|| `project-${createHash('sha256').update(projectPath).digest('hex').slice(0, 8)}`;
let suffix = 2;
const existingDirs = Object.values(projects).map(p => p.contextDir);
while (existingDirs.includes(contextDir) && projects[projectPath]?.contextDir !== contextDir) {
while (existingDirs.includes(contextDir) && registered?.contextDir !== contextDir) {
contextDir = `${contextDir.replace(/-\d+$/, '')}-${suffix++}`;
}
@@ -82,7 +83,7 @@ if (isInit) {
saveContext(contextDir, context);
// Update projects.json
projects[projectPath] = {
projects[registered?.path ?? projectPath] = {
name,
contextDir,
lastUpdated: today(),
@@ -98,7 +99,7 @@ if (isInit) {
// SAVE MODE: record a session
// ─────────────────────────────────────────────────────────────────────────────
const projects = readProjects();
const projectEntry = projects[cwd];
const projectEntry = findProject(projects, cwd);
if (!projectEntry) {
console.log("This project isn't registered yet. Run /ck:init first.");
@@ -158,7 +159,7 @@ if (goal && goal !== context.goal) {
saveContext(contextDir, context);
// Update projects.json timestamp
projects[cwd].lastUpdated = today();
projects[projectEntry.path].lastUpdated = today();
writeProjects(projects);
// ── Write to native memory ────────────────────────────────────────────────────
+10 -3
View File
@@ -45,6 +45,13 @@ export function writeProjects(projects) {
writeJson(PROJECTS_FILE, projects);
}
export function findProject(projects, projectPath) {
const key = Object.hasOwn(projects, projectPath)
? projectPath
: Object.keys(projects).find(p => resolve(p) === resolve(projectPath));
return key === undefined ? null : { path: key, ...projects[key] };
}
// ─── Context I/O ──────────────────────────────────────────────────────────────
export function contextPath(contextDir) {
@@ -78,11 +85,11 @@ export function resolveContext(arg, cwd) {
if (!arg) {
// Match by cwd
const entry = projects[cwd];
const entry = findProject(projects, cwd);
if (!entry) return null;
const context = loadContext(entry.contextDir);
if (!context) return null;
return { name: entry.name, contextDir: entry.contextDir, projectPath: cwd, context };
return { name: entry.name, contextDir: entry.contextDir, projectPath: entry.path, context };
}
// Collect all contexts sorted alphabetically by contextDir
@@ -350,7 +357,7 @@ export function renderListTable(entries, cwd, _todayStr) {
// entries: [{name, contextDir, path, context, lastUpdated}]
// Sorted alphabetically by contextDir before calling
const rows = entries.map((e, i) => {
const isHere = e.path === cwd;
const isHere = resolve(e.path) === resolve(cwd);
const latest = e.context?.sessions?.[e.context.sessions.length - 1] || {};
const when = daysAgoLabel(latest.date);
const icon = stalenessIcon(latest.date);
+2 -1
View File
@@ -97,7 +97,8 @@ function main() {
: '';
const projects = readJson(PROJECTS_FILE) || {};
const entry = projects[cwd];
const key = Object.hasOwn(projects, cwd) ? cwd : Object.keys(projects).find(p => resolve(p) === resolve(cwd));
const entry = projects[key];
// Read previous session BEFORE overwriting current-session.json
const prevSession = readJson(CURRENT_SESSION);
+62
View File
@@ -56,6 +56,12 @@ function runDocumentedSave(marker, placeholder, payload, ctx) {
return spawnSync('bash', ['-c', script], { cwd: ctx.project, env: ctx.env, encoding: 'utf8' });
}
function runCk(ctx, script, args, env, payload) {
return spawnSync(process.execPath, [path.join(SKILL_DIR, script), ...args], {
cwd: ctx.project, env, input: payload === undefined ? '' : JSON.stringify(payload), encoding: 'utf8',
});
}
async function runTests() {
console.log('\n=== Testing ck path handling ===\n');
@@ -135,6 +141,62 @@ async function runTests() {
}
})) passed++; else failed++;
if (process.platform !== 'win32') {
console.log(' - skipped Git Bash and PowerShell path form tests off Windows');
} else {
if (await test('a project registered from Git Bash (C:/...) is the same project from PowerShell (C:\\...)', async () => {
const ctx = tempEnv();
try {
const gitBashPath = ctx.project.replaceAll('\\', '/');
const { PWD: _pwd, ...powerShellEnv } = ctx.env;
const init = runCk(ctx, 'commands/save.mjs', ['--init'], { ...ctx.env, PWD: gitBashPath }, { name: 'bobs-app', path: gitBashPath });
assert.strictEqual(init.status, 0, init.stdout + init.stderr);
assert.strictEqual(JSON.parse(runCk(ctx, 'commands/init.mjs', [], powerShellEnv).stdout).alreadyRegistered, true);
const save = runCk(ctx, 'commands/save.mjs', [], powerShellEnv, { summary: 'saved from PowerShell' });
assert.strictEqual(save.status, 0, save.stdout + save.stderr);
assert.ok(runCk(ctx, 'commands/list.mjs', [], powerShellEnv).stdout.includes('bobs-app <-'), 'list does not mark the current project');
const resume = runCk(ctx, 'commands/resume.mjs', [], powerShellEnv);
assert.strictEqual(resume.status, 0, resume.stdout + resume.stderr);
assert.ok(resume.stdout.includes('RESUMING: bobs-app'), 'resume did not show the project briefing');
assert.ok(!resume.stdout.includes('→ cd'), 'resume asks to cd into the current directory');
const hook = runCk(ctx, 'hooks/session-start.mjs', [], powerShellEnv, {});
assert.ok(JSON.parse(hook.stdout).additionalContext.includes('ck: bobs-app |'), 'SessionStart did not find the project');
const reinit = runCk(ctx, 'commands/save.mjs', ['--init'], powerShellEnv, { name: 'bobs-app', path: ctx.project });
assert.strictEqual(reinit.status, 0, reinit.stdout + reinit.stderr);
const projects = JSON.parse(fs.readFileSync(path.join(ctx.home, '.claude', 'ck', 'projects.json'), 'utf8'));
assert.deepStrictEqual(Object.keys(projects), [gitBashPath]);
} finally {
fs.rmSync(ctx.root, { recursive: true, force: true });
}
})) passed++; else failed++;
if (await test('when both spellings of a folder are registered, the exact one is used', async () => {
const ctx = tempEnv();
try {
const ckHome = path.join(ctx.home, '.claude', 'ck');
const { PWD: _pwd, ...powerShellEnv } = ctx.env;
const projects = {};
for (const [name, projectPath] of [['git-bash', ctx.project.replaceAll('\\', '/')], ['powershell', ctx.project]]) {
projects[projectPath] = { name, contextDir: name, lastUpdated: '2026-01-01' };
fs.mkdirSync(path.join(ckHome, 'contexts', name), { recursive: true });
fs.writeFileSync(path.join(ckHome, 'contexts', name, 'context.json'), JSON.stringify({ version: 2, name, path: projectPath, sessions: [] }));
}
fs.writeFileSync(path.join(ckHome, 'projects.json'), JSON.stringify(projects));
const save = runCk(ctx, 'commands/save.mjs', [], powerShellEnv, { summary: 'saved from PowerShell' });
assert.strictEqual(save.status, 0, save.stdout + save.stderr);
const sessions = name => JSON.parse(fs.readFileSync(path.join(ckHome, 'contexts', name, 'context.json'), 'utf8')).sessions.length;
assert.deepStrictEqual([sessions('git-bash'), sessions('powershell')], [0, 1]);
const hook = runCk(ctx, 'hooks/session-start.mjs', [], powerShellEnv, {});
assert.ok(JSON.parse(hook.stdout).additionalContext.includes('ck: powershell |'), 'SessionStart picked the other registration');
} finally {
fs.rmSync(ctx.root, { recursive: true, force: true });
}
})) passed++; else failed++;
}
console.log(`\nPassed: ${passed}`);
console.log(`Failed: ${failed}`);
process.exitCode = failed > 0 ? 1 : 0;