Files
gstack/test/autoplan-long-session-observer.test.ts
T
garrytanandcapy-ai[bot] 30f17d569a fix(autoplan): the publication hook approves /autoplan's own Reads after the skill turn
Claude Code applies a skill's allowed-tools only to the turn that invoked it.
On 2.1.292 the default fork-subagent mode runs reviewers in the background, and
the completion notice starts a new turn, so later Reads outside the working
directory (the next phase's section file, the close packet) raised permission
cards. Skill hooks stay registered for the session, and a PreToolUse "allow"
skips the card (reproduced in a PTY on 2.1.292).

autoplan/bin/owned-read.ts approves a parent Read only when the guard itself
returned no output, and only for this installation's autoplan/sections/*.md
(canonical path or the gbrain :user render) or an immutable phase artifact in
<repo>/.gstack/tmp/autoplan/autoplan-<phase>-*/ whose repo is the session's
project directory or contains it. Denials, unverified allows, child calls and
every other path are unchanged. renderSectionBase and userRenderRoot move
there with it.

Tests that asserted a verified driver Read as {} now expect the approval;
autoplan-owned-read covers the shipped shim, the close packet without a
journal, verified artifacts through the guard, look-alikes (foreign install,
writable copy, symlink alias, other repository, other names) and a guard
denial staying a denial.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-10-08 08:06:30 +00:00

65 lines
4.0 KiB
TypeScript

/**
* ENG-6: the long-session case (CEO-15) observes the guard through its decision
* log, not the PTY counting reader, which keeps a whole-file cap. Free proof on
* a padded journal through the lower-only bound seams: the counting reader
* refuses the journal while the guard reads it and logs a verified allow.
*/
import { afterEach, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { randomUUID } from 'node:crypto';
import { guardFixture, section, type GuardFixture } from './helpers/autoplan-guard-fixture';
import { padBeforeCompactBoundary } from './helpers/journal-padding';
import { readPlanCountTranscript } from '../lib/claude-public-transcript';
import { OWNED_READ_APPROVAL } from '../autoplan/bin/owned-read';
const fixtures: GuardFixture[] = [];
const prior = process.env.GSTACK_TRANSCRIPT_TEST_MAX_BYTES;
afterEach(() => {
for (const f of fixtures.splice(0)) f.cleanup();
if (prior === undefined) delete process.env.GSTACK_TRANSCRIPT_TEST_MAX_BYTES; else process.env.GSTACK_TRANSCRIPT_TEST_MAX_BYTES = prior;
});
/** A typed first turn and its reply, then a real-shaped compact boundary and summary, then the fixture's /autoplan turn. */
function compactedPrelude(f: GuardFixture): void {
f.journal(undefined, at => at === -1 ? [
{ type: 'user', message: { role: 'user', content: 'Earlier work in this long session.' }, extra: { origin: { kind: 'human' }, promptSource: 'typed', promptId: randomUUID() } },
{ type: 'assistant', message: { role: 'assistant', id: 'msg_earlier', content: [{ type: 'text', text: 'Done with the earlier work.' }] } },
] : []);
const rows = fs.readFileSync(f.transcript, 'utf8').trim().split('\n').map(line => JSON.parse(line));
const reply = rows[1], boundary = { ...reply, uuid: randomUUID(), parentUuid: null, logicalParentUuid: reply.uuid, type: 'system',
subtype: 'compact_boundary', content: 'Conversation compacted', compactMetadata: { trigger: 'manual', preTokens: 1000 } };
delete boundary.message;
const summary = { ...rows[0], uuid: randomUUID(), parentUuid: boundary.uuid, isCompactSummary: true, promptSource: undefined,
origin: undefined, message: { role: 'user', content: 'This session is being continued from a previous conversation.' } };
rows[2] = { ...rows[2], parentUuid: summary.uuid };
fs.writeFileSync(f.transcript, [rows[0], reply, boundary, summary, ...rows.slice(2)].map(r => JSON.stringify(r)).join('\n') + '\n');
}
test('a padded long-session journal: the counting reader refuses it, the guard log records a verified allow', async () => {
const f = guardFixture(); fixtures.push(f);
f.publish();
compactedPrelude(f);
const padded = padBeforeCompactBoundary(f.transcript, 1_536 * 1024, 64 * 1024);
expect(padded.records).toBeGreaterThan(20);
process.env.GSTACK_TRANSCRIPT_TEST_MAX_BYTES = String(256 * 1024);
const counting = readPlanCountTranscript(path.join(f.cwd, 'config'), f.cwd);
expect(counting).toMatchObject({ status: 'error', reason: 'too_large' });
expect(await f.hook(f.input('next', 'Read', { file_path: section('design-phase.md') }))).toEqual(OWNED_READ_APPROVAL);
expect(f.log()).toEqual([expect.objectContaining({ decision: 'allow', disposition: 'allow', code: null, path: 'payload' })]);
});
test('control: a padding record outside the parent chain (a sidechain) breaks ownership, so the guard denies', async () => {
const f = guardFixture(); fixtures.push(f);
f.publish();
compactedPrelude(f);
padBeforeCompactBoundary(f.transcript, 256 * 1024, 64 * 1024);
const rows = fs.readFileSync(f.transcript, 'utf8').trim().split('\n').map(line => JSON.parse(line));
const pad = rows.find(r => r.message?.id === 'msg_padding0');
pad.isSidechain = true;
fs.writeFileSync(f.transcript, rows.map(r => JSON.stringify(r)).join('\n') + '\n');
const output = await f.hook(f.input('next', 'Read', { file_path: section('design-phase.md') }));
expect(output.hookSpecificOutput?.permissionDecision).toBe('deny');
expect(f.log().at(-1)).toMatchObject({ decision: 'deny' });
});