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
35 changes: 26 additions & 9 deletions bin/gstack-docs-candidate
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Usage (run inside the repository):
* gstack-docs-candidate snapshot --out <candidate.json> --audit-id <id> --mode edit|read-only
* --base <rev> [--select <path>]... [--docs <path>[,<path>...]]... [--exception <docs-audit.json>]
* [--run <id>] [--pre <prepare.json> [--carry <earlier-post.json>]]
* [--run <id>] [--pre <prepare.json> [--carry <earlier-post.json>] [--carry-stopped <stopped-prepare.json>]]
* gstack-docs-candidate compare <candidate.json>
* gstack-docs-candidate attempt <child-output> --state <attempts.json>
* gstack-docs-candidate result <child-output> --audit-id <id> --out-dir <record dir> [--repair-of <first child-output>]
Expand Down Expand Up @@ -35,7 +35,10 @@
* snapshot> (with --pre and --run) saves that audit and the ones it carried as
* `earlier`, so receipt write resets every file this run's audits edited
* (GST-1616); a snapshot of another run, one without `edited`, or one whose
* HEAD is not an ancestor of HEAD is refused.
* HEAD is not an ancestor of HEAD is refused. --carry-stopped <the Prepare
* snapshot of an audit of this run that stopped before its post snapshot>, at
* this audit's HEAD, adds that audit with its edits read as what changed
* between the two Prepare snapshots (GST-3502).
*
* compare re-reads the same state and prints one JSON object: whether HEAD or
* the index changed, which recorded paths' content changed or disappeared, and
Expand Down Expand Up @@ -68,7 +71,7 @@ import { baseContainsHead } from '../lib/default-base';
import { baseArg } from '../lib/gate-diff/git';
import { resultLine } from '../lib/result-codes';
import { checkDocsRepair, checkDocsResult, docsRepairPrompt, docsRetryNote, type DocsResultCheck } from '../lib/ship-docs-result';
import { parseReceiptDocs, type ReceiptDocs } from '../lib/ship-receipt-docs';
import { parseReceiptDocs, sameRun, type ReceiptDocs } from '../lib/ship-receipt-docs';

function git(args: string[]): string {
const r = spawnSync('git', args, { encoding: 'utf8', env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' }, maxBuffer: 256 * 1024 * 1024 });
Expand Down Expand Up @@ -149,7 +152,7 @@ function one(args: string[], name: string): string {
return values[0]!;
}

const USAGE = `usage: gstack-docs-candidate snapshot --out <candidate.json> --audit-id <id> --mode edit|read-only --base <rev> [--select <path>]... [--docs <path>[,<path>...]]... [--exception <docs-audit.json>] [--run <id>] [--pre <prepare.json> [--carry <earlier-post.json>]]
const USAGE = `usage: gstack-docs-candidate snapshot --out <candidate.json> --audit-id <id> --mode edit|read-only --base <rev> [--select <path>]... [--docs <path>[,<path>...]]... [--exception <docs-audit.json>] [--run <id>] [--pre <prepare.json> [--carry <earlier-post.json>] [--carry-stopped <stopped-prepare.json>]]
gstack-docs-candidate compare <candidate.json>
gstack-docs-candidate attempt <child-output> --state <attempts.json>
gstack-docs-candidate result <child-output> --audit-id <id> --out-dir <record dir> [--repair-of <first child-output>]
Expand Down Expand Up @@ -190,7 +193,7 @@ type Edited = Record<string, { before: string | null; after: string | null }>;

/** The post snapshot's own audit: each path whose content differs from this audit's Prepare snapshot, before (its Prepare hash, else HEAD's blob) and after. */
function auditEdits(pre: Record<string, any>, file: string, post: { audit_id: string; run_id?: string; head: string; hashes: Record<string, string | null> }): { edited: Edited } {
if (pre.audit_id !== post.audit_id || pre.head !== post.head || pre.run_id !== post.run_id) throw new Error(`--pre ${file} is not this audit's Prepare snapshot (its audit id, HEAD or run differs)`);
if (pre.audit_id !== post.audit_id || pre.head !== post.head || !sameRun(pre.run_id, post.run_id)) throw new Error(`--pre ${file} is not this audit's Prepare snapshot (its audit id, HEAD or run differs)`);
const paths = [...new Set([...Object.keys(pre.hashes), ...Object.keys(post.hashes)])].sort();
const now = hashes(paths.filter(p => !(p in post.hashes)));
const blob = (p: string) => spawnSync('git', ['rev-parse', '--verify', '--quiet', `${pre.head}:${p}`], { encoding: 'utf8', timeout: 60_000 }).stdout?.trim() || null;
Expand All @@ -205,13 +208,25 @@ function auditEdits(pre: Record<string, any>, file: string, post: { audit_id: st

/** An earlier audit of this run and the audits it carried, oldest first; refuses another run's snapshot, one without `edited` and one whose HEAD is not an ancestor of HEAD. */
function carried(prior: Record<string, any>, file: string, post: { audit_id: string; run_id?: string; head: string }): Array<{ run_id: string; audit_id: string; head: string; edited: Edited }> {
if (prior.run_id !== post.run_id) throw new Error(`--carry ${file} is audit ${prior.audit_id} of another /ship run (${prior.run_id ?? 'no --run'}), not ${post.run_id}: a post snapshot carries only this run's earlier audits`);
if (!sameRun(prior.run_id, post.run_id)) throw new Error(`--carry ${file} is audit ${prior.audit_id} of another /ship run (${prior.run_id ?? 'no --run'}), not ${post.run_id}: a post snapshot carries only this run's earlier audits`);
if (!prior.edited || typeof prior.edited !== 'object' || Array.isArray(prior.edited)) throw new Error(`--carry ${file} names no edited files: take each post snapshot with --pre <its Prepare snapshot>`);
if (prior.audit_id === post.audit_id) throw new Error(`--carry ${file} is this audit (${post.audit_id}), not an earlier one`);
if (spawnSync('git', ['merge-base', '--is-ancestor', String(prior.head), post.head], { timeout: 60_000 }).status !== 0) throw new Error(`--carry ${file}: its HEAD ${String(prior.head).slice(0, 12)} is not an ancestor of HEAD`);
return [...(Array.isArray(prior.earlier) ? prior.earlier : []), { run_id: prior.run_id, audit_id: prior.audit_id, head: prior.head, edited: prior.edited }];
}

/**
* An audit of this run that stopped (blocked) before its post snapshot (GST-3502), from its Prepare snapshot: its edits are what changed
* between that snapshot and this re-audit's Prepare snapshot at the same HEAD, so receipt write resets them like a carried audit's.
*/
function stoppedAudit(prepare: Record<string, any>, file: string, post: { audit_id: string; run_id?: string; head: string }, pre: Record<string, any>): { run_id: string; audit_id: string; head: string; edited: Edited } {
if (!sameRun(prepare.run_id, post.run_id)) throw new Error(`--carry-stopped ${file} is audit ${prepare.audit_id} of another /ship run (${prepare.run_id ?? 'no --run'}), not ${post.run_id}`);
if (prepare.audit_id === post.audit_id) throw new Error(`--carry-stopped ${file} is this audit (${post.audit_id}), not an earlier one`);
if (prepare.edited !== undefined || prepare.earlier !== undefined) throw new Error(`--carry-stopped ${file} is a post snapshot: carry it with --carry`);
if (prepare.head !== pre.head) throw new Error(`--carry-stopped ${file} was taken at HEAD ${String(prepare.head).slice(0, 12)}, not this audit's HEAD ${String(pre.head).slice(0, 12)}: a stopped audit is carried only at the HEAD the re-audit read`);
return { run_id: prepare.run_id, audit_id: prepare.audit_id, head: prepare.head, edited: auditEdits({ ...prepare, audit_id: post.audit_id, run_id: post.run_id }, file, { ...post, hashes: pre.hashes }).edited };
}

/** Each field compare needs that the record lacks or types wrongly; empty for a valid record. */
function recordProblems(record: any): string[] {
if (!record || typeof record !== 'object' || Array.isArray(record)) return ['not a JSON object'];
Expand All @@ -231,7 +246,7 @@ function main(argv: string[]): void {
return;
}
if (command === 'snapshot') {
const known = new Set(['--out', '--audit-id', '--mode', '--base', '--select', '--docs', '--exception', '--run', '--pre', '--carry']);
const known = new Set(['--out', '--audit-id', '--mode', '--base', '--select', '--docs', '--exception', '--run', '--pre', '--carry', '--carry-stopped']);
for (let i = 0; i < args.length; i += 2) if (!known.has(args[i]!)) throw new Error(`unknown argument: ${args[i]}`);
const out = path.resolve(one(args, '--out'));
const top = fs.realpathSync(git(['rev-parse', '--show-toplevel']).trim());
Expand All @@ -243,7 +258,7 @@ function main(argv: string[]): void {
if (self) throw new Error(resultLine('snapshot', 'BASE_CONTAINS_HEAD', self));
const exceptionFile = flag(args, '--exception')[0];
const exception = exceptionFile === undefined ? undefined : acceptedException(exceptionFile);
const [preFile, carryFile] = ['--pre', '--carry'].map(name => { const v = optional(args, name); return v === undefined ? undefined : path.resolve(v); });
const [preFile, carryFile, stoppedFile] = ['--pre', '--carry', '--carry-stopped'].map(name => { const v = optional(args, name); return v === undefined ? undefined : path.resolve(v); });
process.chdir(top);
const now = state(base);
const listed = nul(git(['ls-files', '-z', '--cached', '--others', '--exclude-standard']));
Expand All @@ -255,8 +270,10 @@ function main(argv: string[]): void {
const run = optional(args, '--run');
const record = { schema_version: 1, audit_id: auditId, ...(run === undefined ? {} : { run_id: run }), mode, base, ...now, selected, docs, hashes: hashes(tracked), ...(exception ? { exception } : {}) };
if (carryFile !== undefined && (preFile === undefined || run === undefined)) throw new Error('--carry needs --pre and --run: only a post snapshot of a named /ship run carries an earlier audit');
if (stoppedFile !== undefined && (preFile === undefined || run === undefined)) throw new Error('--carry-stopped needs --pre and --run: only a post snapshot of a named /ship run carries a stopped audit');
const audit = preFile === undefined ? {} : auditEdits(readRecord(preFile, '--pre'), preFile, record);
const earlier = carryFile === undefined ? {} : { earlier: carried(readRecord(carryFile, '--carry'), carryFile, record) };
const chain = [...(carryFile === undefined ? [] : carried(readRecord(carryFile, '--carry'), carryFile, record)), ...(stoppedFile === undefined ? [] : [stoppedAudit(readRecord(stoppedFile, '--carry-stopped'), stoppedFile, record, readRecord(preFile!, '--pre'))])];
const earlier = carryFile === undefined && stoppedFile === undefined ? {} : { earlier: chain };
fs.writeFileSync(out, JSON.stringify({ ...record, ...audit, ...earlier }, null, 2) + '\n', { mode: 0o600 });
console.log(`CANDIDATE: ${out}`);
console.log(`base=${base} head=${now.head} branch=${now.branch || '(detached)'} mode=${mode}`);
Expand Down
1 change: 1 addition & 0 deletions changelog.d/docs-re-audit-keeps-stopped-audit-edits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/ship can write its receipt after a docs audit that stopped before saving its snapshot.** Before, when Step 14.5's first docs audit edited a docs file the review leaves out and then stopped as blocked, the re-audit's snapshot could not account for that edit, so `gstack-ship-receipt write` refused on `review_exclude` and no supported path led to a receipt (GST-3502). `gstack-docs-candidate snapshot --carry-stopped <stopped audit's Prepare snapshot>` now carries the stopped audit's edits into the re-audit's snapshot, and two spellings of the same run record path now count as one run.
4 changes: 2 additions & 2 deletions lib/ship-receipt-depth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { branchSides } from './stamp-only';
import { classifyFiles } from './tree-receipt';
import { mergeChangelogs } from './changelog-check';
import { bindReceipt, type ReceiptBindingCode } from './ship-receipt';
import { carriedAudits, docsExceptionLine, parseReceiptDocs, type CarriedAudit } from './ship-receipt-docs';
import { carriedAudits, docsExceptionLine, parseReceiptDocs, sameRun, type CarriedAudit } from './ship-receipt-docs';

export type ReceiptDepth = NonNullable<ShipReceipt['review_depth']>;
export type ReviewLoopException = NonNullable<ShipReceipt['review_loop_exception']>;
Expand Down Expand Up @@ -371,7 +371,7 @@ export function writeExcludeBinds(root: string, base: string, head: string, revi
if (typeof post === 'string') return no(post);
const pre = docsPre === undefined ? null : snapshot(docsPre);
if (typeof pre === 'string') return no(`--docs-pre ${docsPre} ${pre}`);
if (pre && (pre.head !== post.head || pre.audit_id !== post.audit_id || pre.run_id !== post.run_id)) return no(`--docs-pre ${docsPre} is not this audit's Prepare snapshot (its HEAD, audit_id or run_id differs)`);
if (pre && (pre.head !== post.head || pre.audit_id !== post.audit_id || !sameRun(pre.run_id, post.run_id))) return no(`--docs-pre ${docsPre} is not this audit's Prepare snapshot (its HEAD, audit_id or run_id differs)`);
const earlier = carriedAudits(post);
if (typeof earlier === 'string') return no(earlier);
if (earlier.length && !pre) return no(`it carries audit ${earlier.map(e => e.audit_id).join(',')}, so write needs --docs-pre <its Prepare snapshot>`);
Expand Down
7 changes: 6 additions & 1 deletion lib/ship-receipt-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* (`docs.caps`, docs/ship-policy.md#ship-receipt). Kept beside
* lib/ship-receipt.ts so that module stays under the size ratchet.
*/
import * as path from 'node:path';

export const DOCS_STATUSES = ['updated', 'current', 'blocked'] as const;
export type DocsStatus = typeof DOCS_STATUSES[number];
/** Only the owner accepts a documentation risk; the record names who, when, where and why, and the PR body repeats it word for word. */
Expand Down Expand Up @@ -47,6 +49,9 @@ export function docsExceptionLine(docs: ReceiptDocs): string | null {
return `Documentation exception: accepted by ${e.accepted_by} ${e.date} via ${e.via}: ${e.reason}`;
}

/** Whether two snapshots name the same /ship run: its record's path, compared resolved, so `ship2/../ship2-record.md` is `ship2-record.md` (GST-3502). */
export const sameRun = (a: unknown, b: unknown): boolean => a === b || (typeof a === 'string' && typeof b === 'string' && path.resolve(a) === path.resolve(b));

/** One earlier Step 14.5 audit of the same /ship run that a post snapshot carries (gstack-docs-candidate snapshot --carry, GST-1616). */
export interface CarriedAudit { run_id: string; audit_id: string; head: string; edited: Record<string, { before: string | null; after: string | null }> }

Expand All @@ -61,7 +66,7 @@ export function carriedAudits(post: { run_id?: unknown; audit_id?: unknown; earl
const edited = e?.edited;
if (!e || typeof e !== 'object' || !text(e.audit_id) || !sha(e.head) || !edited || typeof edited !== 'object' || Array.isArray(edited)
|| !Object.values(edited).every((v: any) => v && typeof v === 'object' && (v.before === null || sha(v.before)) && (v.after === null || sha(v.after)))) return 'it carries an earlier audit that is not { run_id, audit_id, head, edited }';
if (e.run_id !== post.run_id) return `it carries audit ${e.audit_id} of another /ship run (${String(e.run_id)}), not ${post.run_id}`;
if (!sameRun(e.run_id, post.run_id)) return `it carries audit ${e.audit_id} of another /ship run (${String(e.run_id)}), not ${post.run_id}`;
if (seen.has(e.audit_id)) return `it carries audit ${e.audit_id} twice`;
seen.add(e.audit_id);
}
Expand Down
4 changes: 3 additions & 1 deletion ship/sections/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ auto-commit or push unexpected child commits.
result is such a correction: end the remaining attempt's prompt with its
`RETRY_NOTE:` text verbatim.
Repeat Prepare using current inputs and a fresh id/snapshot, run the remaining
attempt, then validate it through Parent processing.
attempt, then validate it through Parent processing. When the stopped audit saved no post
snapshot, the re-audit's post snapshot adds `--carry-stopped <the stopped audit's Prepare snapshot>`
so receipt write resets the stopped audit's edits too.
3. Otherwise STOP before commit/publication and do not launch another child.
AskUserQuestion: stop for repair (recommended), or ship with the specific named
documentation risk. Only an actual user exception counts, never a default,
Expand Down
4 changes: 3 additions & 1 deletion ship/sections/documentation.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ auto-commit or push unexpected child commits.
result is such a correction: end the remaining attempt's prompt with its
`RETRY_NOTE:` text verbatim.
Repeat Prepare using current inputs and a fresh id/snapshot, run the remaining
attempt, then validate it through Parent processing.
attempt, then validate it through Parent processing. When the stopped audit saved no post
snapshot, the re-audit's post snapshot adds `--carry-stopped <the stopped audit's Prepare snapshot>`
so receipt write resets the stopped audit's edits too.
3. Otherwise STOP before commit/publication and do not launch another child.
AskUserQuestion: stop for repair (recommended), or ship with the specific named
documentation risk. Only an actual user exception counts, never a default,
Expand Down
Loading
Loading