From 61e5bbef80c1c75c38cb4ff6a60cbb60e727b76f Mon Sep 17 00:00:00 2001 From: kobenlys Date: Sun, 1 Nov 2026 21:49:03 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20Claude=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=ED=8F=B4=EB=8D=94=EA=B0=80=20=EC=97=86=EC=9C=BC=EB=A9=B4=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A4=EC=96=B4=20=EA=B8=B0=EB=A1=9D=20=EB=B3=B4?= =?UTF-8?q?=EC=A1=B4=20=EC=84=A4=EC=A0=95=20=EC=A0=80=EC=9E=A5=20(#28)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - applyRetentionSetting이 settings.json 상위 폴더를 0700으로 만든 뒤 쓴다 - 폴더를 만들지 못하면 전처럼 false를 돌려주고 수동 안내로 간다 - 테스트 2개 추가: 상위 폴더 없음(재현), 상위 경로가 파일(폴백) --- src/lib/retention.js | 9 +++++---- test/retention.test.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/lib/retention.js b/src/lib/retention.js index a86838b..fce9067 100644 --- a/src/lib/retention.js +++ b/src/lib/retention.js @@ -1,5 +1,5 @@ -import { existsSync, readFileSync, writeFileSync, renameSync } from 'fs'; -import { join } from 'path'; +import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync } from 'fs'; +import { dirname, join } from 'path'; // Claude Code의 cleanupPeriodDays(트랜스크립트 보존 기간) 검사·설정. // 규칙은 하나: 99999 이상이면 통과, 아니면 동의를 받아 무조건 99999로 설정한다. @@ -41,9 +41,9 @@ export function checkClaudeRetention(claudeDirs) { } /** - * settings.json의 cleanupPeriodDays를 무조건 99999로 설정한다 (다른 필드 보존, 원자 쓰기). + * settings.json의 cleanupPeriodDays를 무조건 99999로 설정한다 (다른 필드 보존, 원자 쓰기, 상위 폴더가 없으면 0700으로 생성). * @param {string} settingsPath checkClaudeRetention()이 돌려준 settings.json 경로 - * @returns {boolean} 성공 여부 — 손상 JSON이면 수정하지 않고 false + * @returns {boolean} 성공 여부 — 손상 JSON이면 수정하지 않고 false, 쓰기(폴더 생성 포함)에 실패해도 false */ export function applyRetentionSetting(settingsPath) { let config = {}; @@ -57,6 +57,7 @@ export function applyRetentionSetting(settingsPath) { try { const next = { ...config, cleanupPeriodDays: FOREVER_DAYS }; + mkdirSync(dirname(settingsPath), { recursive: true, mode: 0o700 }); // Claude Code가 이 폴더에 대화 기록을 쌓는다 const tmpPath = settingsPath + '.tmp'; writeFileSync(tmpPath, JSON.stringify(next, null, 2) + '\n'); renameSync(tmpPath, settingsPath); // 같은 디렉터리 내 rename = 원자적 교체 diff --git a/test/retention.test.js b/test/retention.test.js index a1f259d..c49ae2e 100644 --- a/test/retention.test.js +++ b/test/retention.test.js @@ -1,6 +1,6 @@ import { test, beforeEach, after } from 'node:test'; import assert from 'node:assert/strict'; -import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, existsSync } from 'fs'; +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, existsSync, statSync } from 'fs'; import { tmpdir } from 'os'; import { join } from 'path'; import { checkClaudeRetention, applyRetentionSetting, FOREVER_DAYS, DEFAULT_CLEANUP_DAYS } from '../src/lib/retention.js'; @@ -17,8 +17,10 @@ import { checkClaudeRetention, applyRetentionSetting, FOREVER_DAYS, DEFAULT_CLEA // 6. apply_기존필드보존하며무조건99999_원자쓰기 (성공) // 7. apply_손상JSON_수정하지않고false (실패 입력) // 8. apply_settings없음_새로생성 (경계) +// 9. apply_상위폴더없음_폴더만들고생성 (경계: Claude Code 미사용 환경) +// 10. apply_상위경로가파일_예외없이false (실패 입력) // [constants] -// 9. constants_DEFAULT_CLEANUP_DAYS_기본30 (성공) +// 11. constants_DEFAULT_CLEANUP_DAYS_기본30 (성공) let root; const cleanupDirs = []; @@ -141,6 +143,34 @@ test('apply_settings없음_새로생성', () => { assert.equal(JSON.parse(readFileSync(path, 'utf8')).cleanupPeriodDays, FOREVER_DAYS); }); +test('apply_상위폴더없음_폴더만들고생성', () => { + // Given: settings.json의 상위 폴더(.claude)부터 없는 경로 (Claude Code 미사용 환경) + const dir = join(root, 'k', '.claude'); + const path = join(dir, 'settings.json'); + + // When + const ok = applyRetentionSetting(path); + + // Then: 본인만 접근하는 폴더와 파일을 만들고, 다음 검사에서는 설정이 필요 없다 + assert.equal(ok, true); + assert.equal(JSON.parse(readFileSync(path, 'utf8')).cleanupPeriodDays, FOREVER_DAYS); + assert.equal(checkClaudeRetention([dir]).needed, false); + if (process.platform !== 'win32') assert.equal(statSync(dir).mode & 0o777, 0o700); // Windows는 mode를 무시한다 +}); + +test('apply_상위경로가파일_예외없이false', () => { + // Given: 상위 경로 중간이 일반 파일이라 폴더를 만들 수 없다 + const blocker = join(root, 'blocker'); + writeFileSync(blocker, 'x'); + + // When + const ok = applyRetentionSetting(join(blocker, '.claude', 'settings.json')); + + // Then: 예외 없이 false(호출부가 수동 안내로 간다), 막고 있던 파일은 그대로 + assert.equal(ok, false); + assert.equal(readFileSync(blocker, 'utf8'), 'x'); +}); + test('constants_DEFAULT_CLEANUP_DAYS_기본30', () => { // Given / When / Then: Claude Code 기본 보존일수 상수 assert.equal(DEFAULT_CLEANUP_DAYS, 30); From 0bf1ed31ad7d32cde2ca90521dd5f709aeb3a829 Mon Sep 17 00:00:00 2001 From: kobenlys Date: Sun, 1 Nov 2026 23:27:09 +0900 Subject: [PATCH 2/2] =?UTF-8?q?test:=20retention=20=EC=83=88=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=97=90=20=EC=A4=84=EB=B3=84=20=EC=84=A4?= =?UTF-8?q?=EB=AA=85=20=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80=20(#28)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/retention.test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/retention.test.js b/test/retention.test.js index c49ae2e..7af80d5 100644 --- a/test/retention.test.js +++ b/test/retention.test.js @@ -145,30 +145,30 @@ test('apply_settings없음_새로생성', () => { test('apply_상위폴더없음_폴더만들고생성', () => { // Given: settings.json의 상위 폴더(.claude)부터 없는 경로 (Claude Code 미사용 환경) - const dir = join(root, 'k', '.claude'); + const dir = join(root, 'k', '.claude'); // makeClaudeDir는 폴더를 생성하므로 경로만 조립 const path = join(dir, 'settings.json'); - // When + // When: 로그인 확인에서 Yes 선택 시의 호출 const ok = applyRetentionSetting(path); - // Then: 본인만 접근하는 폴더와 파일을 만들고, 다음 검사에서는 설정이 필요 없다 - assert.equal(ok, true); - assert.equal(JSON.parse(readFileSync(path, 'utf8')).cleanupPeriodDays, FOREVER_DAYS); - assert.equal(checkClaudeRetention([dir]).needed, false); - if (process.platform !== 'win32') assert.equal(statSync(dir).mode & 0o777, 0o700); // Windows는 mode를 무시한다 + // Then + assert.equal(ok, true); // ~/.claude가 없으면 설정 저장이 실패하던 버그(#28) 재현 지점, 수정 전에는 ENOENT로 false + assert.equal(JSON.parse(readFileSync(path, 'utf8')).cleanupPeriodDays, FOREVER_DAYS); // 반환값이 아닌 실제 파일 내용으로 확인 + assert.equal(checkClaudeRetention([dir]).needed, false); // 재로그인 시 확인이 다시 뜨지 않음 + if (process.platform !== 'win32') assert.equal(statSync(dir).mode & 0o777, 0o700); // 권한 비트만 비교, Windows는 mode 무시 }); test('apply_상위경로가파일_예외없이false', () => { // Given: 상위 경로 중간이 일반 파일이라 폴더를 만들 수 없다 const blocker = join(root, 'blocker'); - writeFileSync(blocker, 'x'); + writeFileSync(blocker, 'x'); // chmod 대신 파일로 차단(root·OS와 무관하게 mkdir 실패) - // When + // When: 예외 없이 끝나야 함(mkdirSync가 try 밖으로 빠지는 회귀 감지) const ok = applyRetentionSetting(join(blocker, '.claude', 'settings.json')); - // Then: 예외 없이 false(호출부가 수동 안내로 간다), 막고 있던 파일은 그대로 - assert.equal(ok, false); - assert.equal(readFileSync(blocker, 'utf8'), 'x'); + // Then + assert.equal(ok, false); // false면 호출부가 수동 안내 출력 + assert.equal(readFileSync(blocker, 'utf8'), 'x'); // 경로를 막던 파일 보존 }); test('constants_DEFAULT_CLEANUP_DAYS_기본30', () => {