From d2b601d9ae15bb50acb6946d593ea448f752cb82 Mon Sep 17 00:00:00 2001 From: garrytan Date: Thu, 8 Oct 2026 16:51:43 +0000 Subject: [PATCH] fix(windows): owned-read requires the read-only attribute on Windows; setup.ps1 passes arguments NUL-separated through a private file; Windows-only tests expect the owned-read approval --- autoplan/bin/owned-read.ts | 4 ++-- setup.ps1 | 13 ++++++++++++- test/autoplan-owned-read.test.ts | 2 +- test/autoplan-publication-guard.test.ts | 6 +++--- test/setup-ps1.test.ts | 5 +++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/autoplan/bin/owned-read.ts b/autoplan/bin/owned-read.ts index 2339d01c7..7764951df 100644 --- a/autoplan/bin/owned-read.ts +++ b/autoplan/bin/owned-read.ts @@ -35,7 +35,7 @@ const artifactFiles = (phase: string) => ['methodology.md', 'methodology.json', 'source-implementation.md', `${phase}-implementation.md`, 'close-packet.md']; /** True only for this installation's autoplan/sections/*.md, or an immutable artifact in - * /.gstack/tmp/autoplan/autoplan--*\/ where is the session's project directory or contains it. */ + * /.gstack/tmp/autoplan/autoplan--*\/ (read-only: mode 0444, or the read-only attribute on Windows) where is the session's project directory or contains it. */ export function ownedAutoplanRead(file: unknown, cwd: string, root: string): boolean { if (typeof file !== 'string') return false; try { @@ -55,7 +55,7 @@ export function ownedAutoplanRead(file: unknown, cwd: string, root: string): boo const project = fs.realpathSync(nativePathSpelling(process.env.CLAUDE_PROJECT_DIR || cwd)); return stat.isFile() && sameNativePath(store, path.join(owner, '.gstack', 'tmp', 'autoplan')) && (sameNativePath(project, owner) || nativePathSpelling(project).startsWith(nativePathSpelling(owner) + path.sep)) && - (process.platform === 'win32' || (stat.mode & 0o777) === 0o444); + (process.platform === 'win32' ? (stat.mode & 0o222) === 0 : (stat.mode & 0o777) === 0o444); } catch { return false; } } diff --git a/setup.ps1 b/setup.ps1 index ab3b82386..2dcdffe93 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -44,11 +44,22 @@ if (-not $bash -or -not (Test-Path -LiteralPath $bash -PathType Leaf)) { exit 1 } +# Arguments travel NUL-separated in a private file, never on the native command +# line: PowerShell's native-argument quoting splits or merges arguments that +# contain double quotes, so bash reads them back as an exact array instead. +$argFile = [System.IO.Path]::GetTempFileName() +$writer = New-Object System.IO.StreamWriter($argFile, $false, (New-Object System.Text.UTF8Encoding($false))) +try { + foreach ($arg in $args) { $writer.Write([string]$arg); $writer.Write([char]0) } +} finally { + $writer.Close() +} Push-Location -LiteralPath $PSScriptRoot try { - & $bash -c 'exec ./setup "$@"' setup @args + & $bash -c 'mapfile -d "" -t argv < "$1"; rm -f "$1"; exec ./setup "${argv[@]}"' setup $argFile $code = $LASTEXITCODE } finally { Pop-Location + Remove-Item -LiteralPath $argFile -Force -ErrorAction SilentlyContinue } exit $code diff --git a/test/autoplan-owned-read.test.ts b/test/autoplan-owned-read.test.ts index b8d92da5d..8943bc662 100644 --- a/test/autoplan-owned-read.test.ts +++ b/test/autoplan-owned-read.test.ts @@ -124,7 +124,7 @@ describe('/autoplan owned reads after the skill turn', () => { const first = run(), second = run(); expect(first.status).toBe(0); expect(second.status).toBe(0); const restore = /^RESTORE_PATH=(.+)$/m.exec(first.stdout)?.[1] ?? ''; - expect(path.dirname(restore)).toBe(path.join(repo, '.gstack', 'tmp', 'autoplan')); + expect(path.resolve(path.dirname(restore))).toBe(path.join(repo, '.gstack', 'tmp', 'autoplan')); expect(path.basename(restore)).toMatch(/-autoplan-restore-\d{8}-\d{6}\.md$/); expect(fs.readFileSync(path.join(repo, '.git', 'info', 'exclude'), 'utf8').split('\n').filter(l => l === '/.gstack/tmp/')).toHaveLength(1); expect(spawnSync('git', ['status', '--porcelain', '--untracked-files=all'], { cwd: repo, encoding: 'utf8', timeout: 10_000 }).stdout).toBe(''); diff --git a/test/autoplan-publication-guard.test.ts b/test/autoplan-publication-guard.test.ts index d03bafe29..fb1199a7a 100644 --- a/test/autoplan-publication-guard.test.ts +++ b/test/autoplan-publication-guard.test.ts @@ -1274,8 +1274,8 @@ describe('Autoplan native journal roots and Windows spellings', () => { test.if(windows)('CLAUDE_PROJECT_DIR spelled C:/ or c:\\ names the native journal cwd', async () => { const { f } = published(); - expect(await hook(f, f.cwd.replaceAll('\\', '/'))).toEqual({}); - expect(await hook(f, f.cwd[0]!.toLowerCase() + f.cwd.slice(1))).toEqual({}); + expect(await hook(f, f.cwd.replaceAll('\\', '/'))).toEqual(OWNED_READ_APPROVAL); + expect(await hook(f, f.cwd[0]!.toLowerCase() + f.cwd.slice(1))).toEqual(OWNED_READ_APPROVAL); expect((await hook(f, f.cwd + '\\.') as any).hookSpecificOutput?.permissionDecision).toBe('deny'); }); @@ -1465,7 +1465,7 @@ describe('Autoplan ownership in a linked git worktree session', () => { const entry = path.join(root, '.git', 'worktrees', 'session'); fs.mkdirSync(entry, { recursive: true }); fs.writeFileSync(path.join(entry, 'gitdir'), lower(path.join(f.cwd, '.git')) + '\n'); fs.writeFileSync(path.join(f.cwd, '.git'), `gitdir: ${lower(entry)}\n`); - expect(await run(f, lower(root))).toEqual({}); + expect(await run(f, lower(root))).toEqual(OWNED_READ_APPROVAL); }); test('worktree.useRelativePaths links resolve against each file\'s own directory', async () => { diff --git a/test/setup-ps1.test.ts b/test/setup-ps1.test.ts index abd1beff6..6bc598b2b 100644 --- a/test/setup-ps1.test.ts +++ b/test/setup-ps1.test.ts @@ -39,8 +39,9 @@ function runPs1(dir: string, args: string[], env: Record { - test('passes arguments as an array to a constant bash script', () => { - expect(SOURCE).toContain(`& $bash -c 'exec ./setup "$@"' setup @args`); + test('passes arguments NUL-separated through a private file to a constant bash script', () => { + expect(SOURCE).toContain(`& $bash -c 'mapfile -d "" -t argv < "$1"; rm -f "$1"; exec ./setup "\${argv[@]}"' setup $argFile`); + expect(SOURCE).toContain('foreach ($arg in $args) { $writer.Write([string]$arg); $writer.Write([char]0) }'); expect(SOURCE).not.toMatch(/-join|Invoke-Expression|\biex\b|git-bash\.exe/); expect([...SOURCE].every((c) => c.charCodeAt(0) < 128)).toBe(true); });