Skip to content

Commit 08040c7

Browse files
committed
feat(#922): pin the resolved commit SHA in ground-check output
Grounding has a shelf life: a check runs against a tree that keeps moving, and the old output could not say which tree. `ref` echoed the raw CLI string — a moving name like a branch, or null in working-tree mode — so a consumer reading the JSON later had no way to tell whether the confirmation was stale. `GroundCheckResult` now carries `resolvedSha`: the full SHA of `ref` resolved via `git rev-parse --verify`, or of HEAD in working-tree mode (where `ref: null` continues to signal that uncommitted changes may sit on top). An unresolvable ref pins null rather than silently reporting HEAD as if it were the requested ref, mirroring `buildRepoIndex`'s empty-index degrade. This is the Phase-A-safe slice of @zeweihan's shelf-life review point on the issue: no gate is wired (AC-3 recommends against), but the surviving on-demand checker now emits drift-detectable provenance for free, so any future consumer gets "when and against what" without a schema change. Mutation-verified: deleting `resolvedSha` from the result object fails exactly the four new assertions (working-tree pin, historical-ref pin, HEAD-vs-base distinction, unresolvable-ref null); restored. Refs #922
1 parent 81f2107 commit 08040c7

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎scripts/spec/ground-check-cli.integration.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ describe("ground-check CLI (integration)", { timeout: 90_000 }, () => {
6060
expect(status).toBe(0);
6161
const result = JSON.parse(stdout);
6262
expect(result.schemaVersion).toBe(1);
63+
// Working-tree mode still pins the SHA it ran against (HEAD), so a later
64+
// consumer can detect that the tree moved since the check.
65+
expect(result.resolvedSha).toMatch(/^[0-9a-f]{40}$/);
6366
const raws = result.citations.map((c: { raw: string }) => c.raw);
6467
expect(raws).toContain("scripts/qa/precheck.ts");
6568
expect(raws).toContain("bin/cli.ts");
@@ -144,6 +147,7 @@ describe("ground-check CLI (integration)", { timeout: 90_000 }, () => {
144147
});
145148
const baseResult = JSON.parse(atBase);
146149
expect(baseResult.ref).toBe(base);
150+
expect(baseResult.resolvedSha).toBe(base);
147151
const byRaw = (r: {
148152
citations: Array<{ raw: string; exists: boolean }>;
149153
}) => Object.fromEntries(r.citations.map((c) => [c.raw, c.exists]));
@@ -160,10 +164,15 @@ describe("ground-check CLI (integration)", { timeout: 90_000 }, () => {
160164
timeout: 60_000,
161165
input,
162166
});
163-
expect(byRaw(JSON.parse(atHead))).toEqual({
167+
const headResult = JSON.parse(atHead);
168+
expect(byRaw(headResult)).toEqual({
164169
"src/old.ts": true,
165170
"src/new.ts": true,
166171
});
172+
// Working-tree mode pins HEAD — here the second commit, not the base.
173+
const head = git("rev-parse", "HEAD").trim();
174+
expect(headResult.resolvedSha).toBe(head);
175+
expect(head).not.toBe(base);
167176
} finally {
168177
fs.rmSync(repo, { recursive: true, force: true });
169178
}
@@ -175,6 +184,10 @@ describe("ground-check CLI (integration)", { timeout: 90_000 }, () => {
175184
"See `bin/cli.ts`.",
176185
);
177186
expect(status).toBe(0);
178-
expect(JSON.parse(stdout).citations[0].exists).toBe(false);
187+
const result = JSON.parse(stdout);
188+
expect(result.citations[0].exists).toBe(false);
189+
// An unresolvable ref cannot be pinned; null says so rather than
190+
// silently reporting HEAD as if it were the requested ref.
191+
expect(result.resolvedSha).toBeNull();
179192
});
180193
});

‎scripts/spec/ground-check.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ export interface GroundCheckSummary {
7777
export interface GroundCheckResult {
7878
schemaVersion: 1;
7979
ref: string | null;
80+
/**
81+
* Full SHA of the commit the check actually ran against: `ref` resolved,
82+
* or HEAD in working-tree mode (where `ref: null` signals the tree may
83+
* carry uncommitted changes on top). Null when the ref did not resolve.
84+
* Grounding has a shelf life — a consumer reading this result after the
85+
* tree has moved can compare SHAs to see that the confirmation is stale
86+
* instead of trusting it blind.
87+
*/
88+
resolvedSha: string | null;
8089
generatedAt: string;
8190
summary: GroundCheckSummary;
8291
citations: Citation[];
@@ -644,6 +653,22 @@ export function resolveSymbol(
644653
}
645654
}
646655

656+
/**
657+
* Pin the commit the check runs against to a full SHA. Working-tree mode
658+
* resolves HEAD. An unresolvable ref yields null, mirroring
659+
* `buildRepoIndex`'s empty-index degrade rather than crashing.
660+
*/
661+
export function resolveSha(cwd: string, ref: string | null): string | null {
662+
try {
663+
return git(
664+
["rev-parse", "--verify", `${ref ?? "HEAD"}^{commit}`],
665+
cwd,
666+
).trim();
667+
} catch {
668+
return null;
669+
}
670+
}
671+
647672
// ---------------------------------------------------------------------------
648673
// Orchestration
649674
// ---------------------------------------------------------------------------
@@ -684,6 +709,7 @@ export function runGroundCheck(opts: GroundCheckOptions): GroundCheckResult {
684709
return {
685710
schemaVersion: 1,
686711
ref,
712+
resolvedSha: resolveSha(cwd, ref),
687713
generatedAt: opts.now ?? new Date().toISOString(),
688714
summary: summarize(citations),
689715
citations,

0 commit comments

Comments
 (0)