feat(workshop-console): 프로젝트 폴더의 Claude Code 메모리를 홈클로 메모리로 심볼릭 링크 — 폴더별 메모리라 콘솔의 claude가 기존 지식을 못 보던 문제. 내용 있는 고유 메모리는 건드리지 않음(데이터 보존)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-02 14:26:17 +09:00
1 parent 16c851c018
commit 5877d03419
3 files changed
+122

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
// 작업실 콘솔(2026-10-02) — Claude Code의 메모리는 "실행한 폴더"별로 따로 저장된다(~/.claude/projects/<폴더 경로를 - 로 바꾼 이름>/memory).
// 프로젝트 폴더에서 claude 를 시작하면 기존 메모리(홈클로 폴더의 것)가 따라오지 않으므로, 콘솔을 열 때 그 프로젝트 폴더의
// memory 를 홈클로 메모리로 심볼릭 링크해서 모든 프로젝트 콘솔이 같은 메모리를 쓰게 한다.
//
// 안전 원칙: 프로젝트 쪽에 이미 내용이 있는 실제 memory 폴더는 절대 건드리지 않는다(데이터 손실 방지). 빈 폴더나 없는 경우에만 링크.
import fs from 'fs';
import os from 'os';
import path from 'path';
/** Claude Code가 폴더 경로로 프로젝트 키를 만드는 규칙: 영숫자가 아닌 모든 문자를 '-' 로(실측: /srv/homeclaw/.smallclaw/…/proj_x → -srv-homeclaw--smallclaw-…-proj-x). */
export function claudeProjectKey(absPath: string): string {
return path.resolve(absPath).replace(/[^a-zA-Z0-9]/g, '-');
}
export type MemoryLinkResult =
| { action: 'linked' | 'exists' }
| { action: 'skipped'; reason: string };
export function ensureSharedClaudeMemory(cwd: string, opts: { masterRoot: string; home?: string }): MemoryLinkResult {
const home = opts.home || os.homedir();
const projects = path.join(home, '.claude', 'projects');
const masterKey = claudeProjectKey(opts.masterRoot);
const key = claudeProjectKey(cwd);
if (key === masterKey) return { action: 'skipped', reason: '기준 폴더 자신' };
const master = path.join(projects, masterKey, 'memory');
try { if (!fs.statSync(master).isDirectory()) return { action: 'skipped', reason: '기준 메모리가 폴더가 아님' }; }
catch { return { action: 'skipped', reason: '기준 메모리 없음' }; }
const dir = path.join(projects, key);
const link = path.join(dir, 'memory');
try {
fs.mkdirSync(dir, { recursive: true });
let st: fs.Stats | null = null;
try { st = fs.lstatSync(link); } catch { st = null; }
if (st) {
if (st.isSymbolicLink()) {
return fs.realpathSync(link) === fs.realpathSync(master) ? { action: 'exists' } : { action: 'skipped', reason: '다른 곳을 가리키는 링크가 이미 있음' };
}
if (st.isDirectory()) {
if (fs.readdirSync(link).length > 0) return { action: 'skipped', reason: '이 프로젝트 고유 메모리가 이미 있음(건드리지 않음)' };
fs.rmdirSync(link); // 빈 폴더만 제거
} else return { action: 'skipped', reason: '같은 이름의 파일이 있음' };
}
fs.symlinkSync(master, link, 'dir');
return { action: 'linked' };
} catch (e: any) {
return { action: 'skipped', reason: String(e?.message || e) };
}
}
+7
View File
@@ -61,6 +61,7 @@ import { registerSkillsRoutes } from './routes/routes-skills';
import { registerCodeRoutes } from './routes/routes-code';
import { registerClaudeFsRoutes } from './routes/routes-claude-fs';
import { ptyAccessDecision, resolveWorkshopCwd, allowedAutorun } from './pty-guard';
import { ensureSharedClaudeMemory } from './claude-memory';
import { registerTaskRoutes } from './routes/routes-tasks';
import { registerAgentRoutes } from './routes/routes-agents';
import { registerChannelsRoutes } from './routes/routes-channels';
@@ -4762,6 +4763,12 @@ wss.on('connection', (ws: WebSocket, req: http.IncomingMessage) => {
const pty = (ptyModule as any).default || ptyModule;
// 작업실 콘솔: workshopProjectId 가 있으면 서버가 그 프로젝트 폴더를 해석해 시작 위치로 쓴다(클라이언트 경로 불신).
const wsCwd = resolveWorkshopCwd(user?.workspace, msg.workshopProjectId);
// Claude Code 메모리는 폴더별이라, 이 프로젝트 폴더가 기존(홈클로) 메모리를 공유하도록 링크한다(이미 고유 메모리가 있으면 건드리지 않음).
if (wsCwd) {
const mem = ensureSharedClaudeMemory(wsCwd, { masterRoot: path.resolve(__dirname, '..', '..') });
if (mem.action === 'linked') console.log(`[pty] claude 메모리 공유 링크 생성: ${msg.workshopProjectId}`);
else if (mem.action === 'skipped' && !/기준 폴더 자신/.test(mem.reason)) console.warn(`[pty] claude 메모리 링크 건너뜀(${msg.workshopProjectId}): ${mem.reason}`);
}
const proc = pty.spawn(process.env.SHELL || '/bin/bash', [], {
name: 'xterm-256color',
cols: msg.cols || 120,
+65
View File
@@ -0,0 +1,65 @@
/**
* ensureSharedClaudeMemory — 프로젝트 폴더의 Claude Code 메모리를 기준(홈클로) 메모리로 링크
* 핵심: 이미 내용이 있는 프로젝트 고유 메모리는 절대 지우거나 덮지 않는다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { claudeProjectKey, ensureSharedClaudeMemory } from '../src/gateway/claude-memory';
const mk = () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cm-'));
const masterRoot = '/srv/homeclaw';
const master = path.join(home, '.claude', 'projects', claudeProjectKey(masterRoot), 'memory');
fs.mkdirSync(master, { recursive: true });
fs.writeFileSync(path.join(master, 'MEMORY.md'), '# index');
return { home, masterRoot, master };
};
const CWD = '/srv/homeclaw/.smallclaw/users/papa/workspace/workshop/proj_m5j2c2bp';
describe('claudeProjectKey', () => {
test('실측 규칙: 영숫자 외 모든 문자가 - (점·밑줄 포함)', () => {
assert.equal(claudeProjectKey('/srv/homeclaw'), '-srv-homeclaw');
assert.equal(claudeProjectKey(CWD), '-srv-homeclaw--smallclaw-users-papa-workspace-workshop-proj-m5j2c2bp');
});
});
describe('ensureSharedClaudeMemory', () => {
test('메모리가 없으면 기준 메모리로 링크한다(MEMORY.md가 보임)', () => {
const { home, masterRoot, master } = mk();
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'linked' });
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
assert.equal(fs.realpathSync(link), fs.realpathSync(master));
assert.equal(fs.readFileSync(path.join(link, 'MEMORY.md'), 'utf8'), '# index');
});
test('두 번째 호출은 아무것도 바꾸지 않는다', () => {
const { home, masterRoot } = mk();
ensureSharedClaudeMemory(CWD, { home, masterRoot });
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'exists' });
});
test('빈 memory 폴더는 링크로 바꾼다', () => {
const { home, masterRoot } = mk();
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
fs.mkdirSync(link, { recursive: true });
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'linked' });
assert.ok(fs.lstatSync(link).isSymbolicLink());
});
test('내용이 있는 프로젝트 고유 메모리는 건드리지 않는다 — 데이터 보존', () => {
const { home, masterRoot } = mk();
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
fs.mkdirSync(link, { recursive: true });
fs.writeFileSync(path.join(link, 'mine.md'), '내 메모');
const r = ensureSharedClaudeMemory(CWD, { home, masterRoot });
assert.equal(r.action, 'skipped');
assert.ok(!fs.lstatSync(link).isSymbolicLink());
assert.equal(fs.readFileSync(path.join(link, 'mine.md'), 'utf8'), '내 메모');
});
test('기준 메모리가 없거나 기준 폴더 자신이면 아무것도 하지 않는다', () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cm-'));
assert.equal(ensureSharedClaudeMemory(CWD, { home, masterRoot: '/srv/homeclaw' }).action, 'skipped');
const { home: h2, masterRoot } = mk();
assert.equal(ensureSharedClaudeMemory(masterRoot, { home: h2, masterRoot }).action, 'skipped');
});
});