Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions scripts/pm/dispatch-gates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15967,10 +15967,27 @@ function runGit(args, cwd) {
return { status: r.status ?? 1, stdout: r.stdout ?? '', stderr: (r.stderr ?? '').trim() };
}

function gitLines(args, cwd) {
/**
* A path list from git, read NUL-separated: `git <subcommand> -z <rest…>`.
*
* The `-z` is spliced in here rather than written at each call site, so no
* caller can list paths the other way. Without it git C-quotes any path that
* holds a non-ASCII byte, a double quote or a control character
* (`"skills/x/\346\226\207.md"` for a file named in Chinese), and the quoted
* spelling begins with a quote mark, so it matches no glob: a governed
* `skills/**` file read as no tier at all, every derived gate watching it went
* unnamed, and an untracked one was counted as an unreadable file of 0 lines.
* ⛔ `core.quotePath=false` alone is no repair: it unquotes the non-ASCII bytes
* only, and a newline in a path still splits one name into two on a line
* read. Split on NUL and never trim: a leading or trailing space is part of
* the name. Same reader shape as `trackedFiles` and the landing gates'
* `enumerateRows`.
*/
function gitPathList([subcommand, ...rest], cwd) {
const args = [subcommand, '-z', ...rest];
const r = runGit(args, cwd);
if (r.status !== 0) throw new Error(`git ${args.join(' ')} failed — ${r.stderr || `exit ${r.status}`}`);
return r.stdout.split('\n').map((s) => s.trim()).filter(Boolean);
return r.stdout.split('\0').filter(Boolean);
}

/**
Expand Down Expand Up @@ -16046,9 +16063,11 @@ export function changedPathsFromGit({ cwd = ROOT, base = DEFAULT_BASE_REF } = {}
// --no-renames so a moved file contributes BOTH names. Rename detection
// prints only the new one, and a gate watching the old directory is exactly
// as implicated by the move as one watching the new.
const committed = gitLines(['diff', '--name-only', '--no-renames', mergeBase, 'HEAD'], cwd);
const worktree = gitLines(['diff', '--name-only', '--no-renames', 'HEAD'], cwd);
const untracked = gitLines(['ls-files', '--others', '--exclude-standard'], cwd);
// Every list is read NUL-separated (`gitPathList`), so a name git would
// C-quote reaches the tier and the gate match spelled as it is on disk.
const committed = gitPathList(['diff', '--name-only', '--no-renames', mergeBase, 'HEAD'], cwd);
const worktree = gitPathList(['diff', '--name-only', '--no-renames', 'HEAD'], cwd);
const untracked = gitPathList(['ls-files', '--others', '--exclude-standard'], cwd);

const paths = [...new Set([...committed, ...worktree, ...untracked])].sort();
const size = changedLinesFromGit({ cwd, mergeBase, untracked });
Expand Down Expand Up @@ -16084,7 +16103,7 @@ export function changedLinesFromGit({ cwd = ROOT, mergeBase, untracked = null })
throw new Error(`git diff --numstat ${mergeBase.slice(0, 9)} failed — ${tracked.stderr || `exit ${tracked.status}`}; the changed-line count cannot be answered, and an unanswered count is never zero`);
}
const counted = parseNumstat(tracked.stdout);
const others = untracked ?? gitLines(['ls-files', '--others', '--exclude-standard'], cwd);
const others = untracked ?? gitPathList(['ls-files', '--others', '--exclude-standard'], cwd);
let untrackedLines = 0;
let untrackedBinary = 0;
let unreadable = 0;
Expand Down
66 changes: 66 additions & 0 deletions scripts/pm/dispatch-gates.self-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11332,6 +11332,72 @@ export function selfTest({ tier = 'full' } = {}) {
}
t('an unresolvable base ref is refused on its own terms', !!missingBaseErr && /does not resolve/.test(missingBaseErr.message));

// ── A name git would C-quote still reaches the tier and the gates (#33847)
//
// Without `-z` git prints a path holding a non-ASCII byte or a control
// character C-quoted and octal-escaped, and that spelling starts with a
// quote mark, so no glob covers it: a governed `skills/**` file derived as
// NO tier and named no gate family. Each of the three reads carries one such
// name here: a committed file named in Chinese, an uncommitted edit to
// another, and an untracked file whose name holds a newline (the case
// `core.quotePath=false` alone would still split in two). The fixture sets
// `core.quotePath true` itself so the CONTROL quotes on any machine,
// whatever the global config says.
//
// The CONTROL is the load-bearing half: it reads the same branch the old
// way, in the same repo, and must come back quoted, tierless and gateless —
// otherwise the rows below would pass against a fixture that never
// reproduced the defect.
const quoted = nodePath.join(gitTmp, 'quoted-names');
mkdirSync(quoted, { recursive: true });
g(['init', '--initial-branch=main', '.'], quoted);
g(['config', 'core.quotePath', 'true'], quoted);
const nonAsciiEdited = 'skills/objectstack-data/\u57fa\u7ebf.md';
const nonAsciiCommitted = 'skills/objectstack-data/\u6587\u6863.md';
const newlineUntracked = 'skills/objectstack-data/two\nlines.md';
commit(quoted, nonAsciiEdited, 'root');
g(['update-ref', 'refs/remotes/origin/main', g(['rev-parse', 'main'], quoted)], quoted);
g(['checkout', '-b', 'feature'], quoted);
commit(quoted, nonAsciiCommitted, 'a skill file named in Chinese');
write(quoted, nonAsciiEdited, 'edited, not committed\n');
write(quoted, newlineUntracked, 'never added\n');

const plainRead = [
...g(['diff', '--name-only', '--no-renames', 'origin/main', 'HEAD'], quoted).split('\n'),
...g(['diff', '--name-only', '--no-renames', 'HEAD'], quoted).split('\n'),
...g(['ls-files', '--others', '--exclude-standard'], quoted).split('\n'),
].filter(Boolean);
const familiesOf = (paths) =>
[...discoverFamilies().byCheck.entries()].filter(([, e]) => classifyEntry(e, paths).verdict === 'matched').map(([check]) => check);
t(
'the CONTROL reproduces the defect: the plain read quotes all three names, so they derive no tier and name no gate family',
plainRead.length === 3 && plainRead.every((p) => p.startsWith('"')) && deriveTier(plainRead).tier === null && familiesOf(plainRead).length === 0,
plainRead,
);

const raw = changedPathsFromGit({ cwd: quoted });
t(
'the derived change set spells each C-quoted name as it is on disk: committed, uncommitted and untracked (newline included)',
[nonAsciiCommitted, nonAsciiEdited, newlineUntracked].every((p) => raw.paths.includes(p)) && raw.paths.length === 3 &&
raw.counts.committed === 1 && raw.counts.worktree === 1 && raw.counts.untracked === 1,
raw.paths,
);
t(
'and those raw names derive the contract-review tier through the skills/** glob',
deriveTier(raw.paths).tier === CONTRACT_REVIEW_TIER && deriveTier(raw.paths).hits.length === 3,
deriveTier(raw.paths),
);
t(
'and they name the skill gate families the derived --commands list is built from',
familiesOf(raw.paths).some((check) => /^check:skill-/.test(check)),
familiesOf(raw.paths),
);
t(
'and the untracked newline-named file is counted from disk, not as an unreadable file of 0 lines',
raw.size.untrackedFiles === 1 && raw.size.unreadableFiles === 0 && raw.size.additions === 3,
raw.size,
);

// ── A leftover in-tree test fixture must not reach the change set (#12632)
//
// The derivation reads untracked files on purpose, so `.gitignore` is the
Expand Down
Loading