mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 00:21:14 +01:00
A green CI run still counts when the repository's artifact storage is full (GST-3231) (#4536)
When a private repository's GitHub Actions artifact storage is full, a green run uploads no `tree.json`, so /ship and the merge checks could not reuse it and refused it as `artifact_missing`. A run profile can now set `tree_log_step`, and gstack reads `tree.json` from the one `GSTACK_TREE_JSON` line that step printed in the job log, only when the run has no artifact and only when GitHub's own log timestamp puts the line inside that step. To use it, add the one-line workflow step described under `tree_log_step` in the docs, and raise `min_gstack_version` to the release that ships this. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
ab6a38b767
commit
dab7549451
15 files changed
+466
-30
No files matched your search
+4
-3
@@ -83,7 +83,7 @@ function newestVerified(src: RunSource, name: string, profile: RunProfile, heads
|
||||
for (const run of listed) {
|
||||
const v = verifyRun(src.deps, { repo: src.repo, runRef: String(run.id), profileName: name, profile, vetoHeads: heads, now: src.now, unchanged: src.unchanged, docsOnly });
|
||||
if (v.ok) return v;
|
||||
refusals.push(`run ${run.id}: ${v.reason}${v.reason === 'rerun_malformed' ? ` (${v.detail})` : ''}${v.fix ? ` (the pull request's base moved after the run; fix: ${v.fix})` : ''}`);
|
||||
refusals.push(`run ${run.id}: ${v.reason}${v.reason === 'rerun_malformed' || v.reason === 'tree_log_unbound' || (v.reason === 'artifact_missing' && profile.tree_log_step) ? ` (${v.detail})` : ''}${v.fix ? ` (the pull request's base moved after the run; fix: ${v.fix})` : ''}`);
|
||||
}
|
||||
return no('no_verified_run', `${name}: ${refusals.join('; ')}`);
|
||||
}
|
||||
@@ -237,7 +237,7 @@ export function importedTestLines(cwd: string, missing: string[], candidates: st
|
||||
* not-owed reason, or the last CARRY_NONE reason. A lane the cleared remote gate stood in for (--remote-gate-stands-in, GST-1688)
|
||||
* reads as its receipt, its count and why the CI carry refused.
|
||||
*/
|
||||
export function recordCiResult(stderr: string): { runs?: string; tests?: string; files?: boolean; notOwed?: string; here?: string; exception?: string; reused?: string; from?: string; none?: string; diagnosis?: string[]; remoteGate?: string; standIn?: string } {
|
||||
export function recordCiResult(stderr: string): { runs?: string; treeSource?: string; tests?: string; files?: boolean; notOwed?: string; here?: string; exception?: string; reused?: string; from?: string; none?: string; diagnosis?: string[]; remoteGate?: string; standIn?: string } {
|
||||
const lines = stderr.split('\n');
|
||||
const last = lines.findLast(x => x.startsWith('gstack-evidence: recorded label='));
|
||||
const gateRow = last && / tests_ran=(\d+) log=- source=remote_gate receipt=(.+)$/.exec(last);
|
||||
@@ -249,12 +249,13 @@ export function recordCiResult(stderr: string): { runs?: string; tests?: string;
|
||||
if (at < 0) return none ? { none, ...(diagnosis.length ? { diagnosis } : {}) } : {};
|
||||
const exception = lines.slice(at + 1).find(x => x.startsWith('TEST_LANE_EXCEPTION: '))?.slice('TEST_LANE_EXCEPTION: '.length);
|
||||
const got = / tests_ran=(\d+|n\/a)( unit=files)? log=- source=ci runs=(\S+)/.exec(lines[at]!);
|
||||
const treeSource = / tree_source=(job-log)(?= |$)/.exec(lines[at]!)?.[1];
|
||||
const here = / skipped_here=(\d+) tests_ran_here=(\S+)( not_owed_here=\d+)?$/.exec(lines[at]!);
|
||||
const notOwed = lines.slice(at + 1).find(x => x.startsWith('gstack-evidence: not owed: '))?.slice('gstack-evidence: not owed: '.length);
|
||||
const reused = lines.slice(at + 1).map(x => /^CI_RECORD_REUSED: .* tree=([0-9a-f]+) /.exec(x)?.[1]).find(Boolean);
|
||||
const fromLine = lines.slice(at + 1).find(x => x.startsWith('CI_CARRY_FROM: '));
|
||||
const from = fromLine ? [...new Set([...fromLine.matchAll(/ at commit ([0-9a-f]{12})/g)].map(m => m[1]!))].join(',') : '';
|
||||
return { runs: got?.[3], tests: got?.[1], files: !!got?.[2], ...(notOwed ? { notOwed } : {}), ...(here ? { here: `skipped_here=${here[1]} file(s) tests_ran_here=${here[2]}${here[3] ?? ''}` } : {}), ...(exception ? { exception } : {}), ...(reused ? { reused } : {}), ...(from ? { from } : {}) };
|
||||
return { runs: got?.[3], tests: got?.[1], files: !!got?.[2], ...(treeSource ? { treeSource } : {}), ...(notOwed ? { notOwed } : {}), ...(here ? { here: `skipped_here=${here[1]} file(s) tests_ran_here=${here[2]}${here[3] ?? ''}` } : {}), ...(exception ? { exception } : {}), ...(reused ? { reused } : {}), ...(from ? { from } : {}) };
|
||||
}
|
||||
|
||||
/** The per_file runner command for `files`: `{files}` replaced by the paths, each anchored with ./ (Bun reads a bare relative path as a substring filter) and single-quoted for bash. */
|
||||
|
||||
@@ -104,8 +104,8 @@ export interface ShipReceipt {
|
||||
gate?: Record<string, unknown>; ci?: Record<string, unknown>; spend_usd?: number | string; predecessor?: string;
|
||||
/** The run's calls from the call log (lib/ship-clock.ts callsSince: gstack-command calls plus keep-alives), so a 200-call run shows at the receipt; absent on older receipts. */
|
||||
calls?: number;
|
||||
/** GSTA-258: the lanes CI carried at this head's tree, one entry per run (gstack-evidence carried); `rerun` and `attempts` when files passed only on a re-run (GST-3054). */
|
||||
ci_carried?: Array<{ lane: string; run: number; attempt: number; tree: string; rerun?: number; attempts?: number[]; exception?: BoundLaneException }>;
|
||||
/** GSTA-258: the lanes CI carried at this head's tree, one entry per run (gstack-evidence carried); `rerun` and `attempts` when files passed only on a re-run (GST-3054); `tree_source: job-log` when the run's tree.json came from its job log (HARN-118). */
|
||||
ci_carried?: Array<{ lane: string; run: number; attempt: number; tree: string; rerun?: number; attempts?: number[]; tree_source?: 'job-log'; exception?: BoundLaneException }>;
|
||||
session_kind?: string; artifacts_consumed?: string; run?: string;
|
||||
// C-stamp (C3/C5/C7): the tree receipt's verdicts and the policy provenance, all optional.
|
||||
thread?: string; tree?: string; gate_reuse?: string; policy?: string; queue_mode?: string; preregistration?: string; history?: string;
|
||||
@@ -267,7 +267,7 @@ export const ARTIFACT_SCHEMAS = {
|
||||
schema_version: { const: 1 }, pr: { type: 'integer' }, head: str, version: str, base: str, gated_tree: str,
|
||||
gate: { type: 'object' }, ci: { type: 'object' }, spend_usd: { type: ['number', 'string'] }, calls: { type: 'integer', minimum: 0 }, predecessor: str,
|
||||
ci_carried: { type: 'array', items: { type: 'object', required: ['lane', 'run', 'attempt', 'tree'], properties: { lane: str, run: { type: 'integer', minimum: 1 }, attempt: { type: 'integer', minimum: 1 }, tree: str,
|
||||
rerun: { type: 'integer', minimum: 1 }, attempts: { type: 'array', items: { type: 'integer', minimum: 1 } },
|
||||
rerun: { type: 'integer', minimum: 1 }, attempts: { type: 'array', items: { type: 'integer', minimum: 1 } }, tree_source: { enum: ['job-log'] },
|
||||
exception: { type: 'object', required: ['head', 'files', 'by', 'reason', 'at', 'approval', 'preflight'], properties: { head: str, files: strList, by: str, reason: str, at: str, approval: str,
|
||||
preflight: { type: 'object', required: ['label', 'tests_ran'], properties: { label: str, tests_ran: { type: 'integer', minimum: 1 } } } } } } } },
|
||||
session_kind: str, artifacts_consumed: { ...str, description: '<n>/<m>' }, run: str,
|
||||
|
||||
@@ -425,6 +425,11 @@ export const RELEASE_RESULT_CODES = {
|
||||
summary: 'write --review-loop-exception got an exception that is malformed, accepts a stop the review loop does not show (a fix_check with review_loop.fix_checks 0 or missing, or a second_pass with fewer than two passes or no deferred finding), names a head that is not the receipt head or its ancestor, or is missing from the PR body',
|
||||
fix: 'pass { accepted_by, date, via, reason } plus fix_check: { head, findings } or second_pass: { head, deferred: [<finding id>] } with the owner’s words, the --review-loop summary that shows that stop, and the printed "Review-loop exception: …" line in the body',
|
||||
},
|
||||
CI_CARRY_TREE_LOG_UNBOUND: {
|
||||
anchor: 'ci-carry-tree-log-unbound',
|
||||
summary: 'a CI run with no tree.json artifact (artifact storage full) has one GSTACK_TREE_JSON line in its job log, but the runner did not stamp it inside the step the run profile names in tree_log_step, at least a second after every earlier step ended and a second before every later one started, or that step is missing, repeated or did not succeed; a line printed anywhere else (a test, for one) is never evidence',
|
||||
fix: 'print the line only from the step tree_log_step names, after the test step, with sleep 1 before it and sleep 2 after it (docs/pregate.md#tree-log-step); then dispatch a fresh run at the same head and rerun gstack-ci-carry wait',
|
||||
},
|
||||
CI_CARRY_RERUN_MALFORMED: {
|
||||
anchor: 'ci-carry-rerun-malformed',
|
||||
summary: 'a CI run\'s tree.json carries rerun_files or attempts in a shape gstack cannot read (rerun_files not a list of paths, attempts not a list of attempt numbers), so the run is not evidence and no lane carries from it',
|
||||
|
||||
+88
-10
@@ -11,7 +11,9 @@
|
||||
* the run belongs to this repository and ran for a same-repository head;
|
||||
* its workflow file and event are the profile's; it is the newest attempt,
|
||||
* completed with success; every job the profile names appears exactly once
|
||||
* in that attempt and succeeded; its artifact names this run, attempt,
|
||||
* in that attempt and succeeded; its artifact (or, for a profile with
|
||||
* tree_log_step and a run with no artifact at all, the one tree.json line
|
||||
* the runner logged inside that step; treeFromLog) names this run, attempt,
|
||||
* event and head (the API always answers with the newest attempt, so an
|
||||
* older green attempt under a newer red one is refused), is not a reuse
|
||||
* record, records the tree git computes for the head, and holds well-formed
|
||||
@@ -47,14 +49,19 @@ import { runBashSync } from './script-invocation';
|
||||
* `skippable` (GST-1190): named jobs the workflow may skip by path; a skip counts only in a pull_request run whose `gate` job
|
||||
* succeeded, with the workflow, the declared `scripts` and any path the gate's steps name matching origin/<base> byte for byte.
|
||||
*/
|
||||
export interface RunProfile { workflow: string; events: string[]; jobs: string[]; artifact: string; max_age_hours?: number; os?: string; skippable?: { jobs: string[]; gate: string; scripts: string[] } }
|
||||
/**
|
||||
* `tree_log_step` (HARN-118): the opt-in for a run with no unexpired artifact named `artifact` (artifact storage full): its
|
||||
* tree.json is read from the one `GSTACK_TREE_JSON <artifact> <base64>` line the runner logged inside this step of a profile job.
|
||||
*/
|
||||
export interface RunProfile { workflow: string; events: string[]; jobs: string[]; artifact: string; max_age_hours?: number; os?: string; skippable?: { jobs: string[]; gate: string; scripts: string[] }; tree_log_step?: string }
|
||||
export interface RemoteGateRuns { profiles: string[]; inventory: string; skipped?: string }
|
||||
export interface ApiRun {
|
||||
id: number; run_attempt: number; status: string; conclusion: string | null; event: string; head_sha: string;
|
||||
path?: string; created_at?: string; run_started_at?: string; updated_at?: string;
|
||||
repository?: { full_name: string } | null; head_repository?: { full_name: string } | null; head_branch?: string | null;
|
||||
}
|
||||
export interface ApiJob { name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null }
|
||||
export interface ApiStep { number: number; name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null }
|
||||
export interface ApiJob { id?: number; name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null; steps?: ApiStep[] }
|
||||
export interface TreeRecord { run_id?: unknown; run_attempt?: unknown; event?: unknown; sha?: unknown; head_sha?: unknown; tree?: unknown; reused?: unknown; reuse?: unknown; sizing_experiment?: unknown; os?: unknown; files?: unknown; tests?: unknown; skipped_files?: unknown; rerun_files?: unknown; attempts?: unknown }
|
||||
|
||||
/** What a red run's reader needs beyond RunDeps (lib/pregate/flaky-exception.ts): every file of one artifact, and a pull request's head. */
|
||||
@@ -64,6 +71,9 @@ export interface RunArtifactDeps {
|
||||
pull(n: number): { head_sha: string; state: string; head_repo: string | null };
|
||||
}
|
||||
|
||||
/** What `RunDeps.artifact` throws when the run holds no unexpired artifact by that name at all: the only case a job log may stand in. */
|
||||
export class NoArtifactError extends Error {}
|
||||
|
||||
/** Everything verifyRun reads, injectable so every refusal has a fixture. */
|
||||
export interface RunDeps {
|
||||
run(id: number): ApiRun;
|
||||
@@ -71,6 +81,8 @@ export interface RunDeps {
|
||||
jobs(id: number, attempt: number): ApiJob[];
|
||||
/** The tree.json of the run's unexpired artifact `name`: the only one, or with one per rerun attempt of its job, the one that records run_attempt `attempt`. */
|
||||
artifact(id: number, name: string, attempt: number): TreeRecord;
|
||||
/** One job's whole log as the runner wrote it, each line led by the runner's own timestamp (HARN-118's fallback only). */
|
||||
jobLog?(jobId: number): string;
|
||||
commitTree(sha: string): string | null;
|
||||
/** `sha` and its ancestors, newest first, at most `max` (git rev-list): where a commit with `sha`'s exact tree is looked for (GST-3019). */
|
||||
ancestors?(sha: string, max: number): string[];
|
||||
@@ -78,17 +90,18 @@ export interface RunDeps {
|
||||
|
||||
export type RunRefusal = 'run_ref_invalid' | 'other_repository' | 'fork_run' | 'workflow_mismatch' | 'event_mismatch' | 'run_in_progress'
|
||||
| 'run_not_success' | 'job_missing' | 'job_ambiguous' | 'job_skipped' | 'job_not_success' | 'artifact_missing' | 'artifact_mismatch'
|
||||
| 'artifact_reused' | 'sizing_experiment' | 'tree_mismatch' | 'newer_run_vetoes' | 'run_changed' | 'run_stale' | 'os_mismatch' | 'rerun_malformed' | 'api_error';
|
||||
| 'artifact_reused' | 'sizing_experiment' | 'tree_mismatch' | 'newer_run_vetoes' | 'run_changed' | 'run_stale' | 'os_mismatch' | 'rerun_malformed' | 'tree_log_unbound' | 'api_error';
|
||||
/** `skipped`: the files whose every test skipped on the runner (tree.json `skipped_files`, GST-1190); undefined when an older tree.json does not say. */
|
||||
/** `docs_only`: a pull_request run whose gate skipped every skippable job on a change lib/docs-only.ts calls human docs only; it ran and recorded no tests (R81). */
|
||||
/** `tree_source`: `job-log` when tree.json came from the job log because the run held no artifact (HARN-118); absent for an artifact. */
|
||||
/** `rerun`: the files whose newest result came from a re-run attempt (tree.json `rerun_files`, gbrain#9690), with the run's `attempts`; set only when a file passed on a re-run (GST-3054). */
|
||||
export interface RunEvidence { run_id: number; run_attempt: number; head_sha: string; tree: string; profile: string; event: string; finished_at: string; files: string[]; tests: number | null; skipped?: string[]; skipped_jobs?: string[]; docs_only?: true; rerun?: string[]; attempts?: number[] }
|
||||
export interface RunEvidence { run_id: number; run_attempt: number; head_sha: string; tree: string; profile: string; event: string; finished_at: string; files: string[]; tests: number | null; skipped?: string[]; skipped_jobs?: string[]; docs_only?: true; rerun?: string[]; attempts?: number[]; tree_source?: 'job-log' }
|
||||
/** `fix`: the one command that clears a refusal the caller can repair, such as a pull_request run that tested a merge with an older base. */
|
||||
export type RunVerdict = { ok: true; evidence: RunEvidence } | { ok: false; reason: RunRefusal; detail: string; fix?: string };
|
||||
|
||||
const SHA = /^[0-9a-f]{40}$/;
|
||||
const RUN_EVENTS = ['push', 'workflow_dispatch', 'pull_request'];
|
||||
const PROFILE_KEYS = ['workflow', 'events', 'jobs', 'artifact', 'max_age_hours', 'os', 'skippable'];
|
||||
const PROFILE_KEYS = ['workflow', 'events', 'jobs', 'artifact', 'max_age_hours', 'os', 'skippable', 'tree_log_step'];
|
||||
const WORKFLOW_FILE = /^[A-Za-z0-9_.-]+\.ya?ml$/;
|
||||
const NAME = /^[A-Za-z0-9_.\/-]{1,128}$/;
|
||||
export const DEFAULT_MAX_AGE_HOURS = 24;
|
||||
@@ -97,6 +110,9 @@ const MAX_ARTIFACT_BYTES = 8 * 1024 * 1024;
|
||||
const MAX_ARTIFACT_ATTEMPTS = 10;
|
||||
/** Shard logs are the whole test stream, so a red run's reader allows more than a tree.json. */
|
||||
const MAX_LOG_ARTIFACT_BYTES = 64 * 1024 * 1024;
|
||||
/** The one log line that carries a tree.json when a run holds no artifact (HARN-118): the runner's timestamp, the marker, the artifact name, base64. */
|
||||
const TREE_LOG_LINE = /^\S+Z GSTACK_TREE_JSON (\S+) ([A-Za-z0-9+/]+={0,2})\r?$/;
|
||||
const LOG_STAMP = /^(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)(?:\.(\d{1,9}))?Z$/;
|
||||
|
||||
/** A numeric run id, or a run URL on this repository (`…/actions/runs/<id>[/attempts/<n>][/job/<j>]`). */
|
||||
export function parseRunRef(ref: string, repo: string): number | null {
|
||||
@@ -127,6 +143,7 @@ export function runProfileErrors(profiles: unknown, gateRuns: unknown): Array<{
|
||||
if (typeof p.artifact !== 'string' || !NAME.test(p.artifact)) errors.push({ path: `${at}.artifact`, message: 'must be the artifact name holding tree.json' });
|
||||
if ('max_age_hours' in p && (!Number.isInteger(p.max_age_hours) || (p.max_age_hours as number) < 1 || (p.max_age_hours as number) > 168)) errors.push({ path: `${at}.max_age_hours`, message: 'must be an integer from 1 to 168' });
|
||||
if ('os' in p && (typeof p.os !== 'string' || !/^(Linux|macOS|Windows)$/.test(p.os))) errors.push({ path: `${at}.os`, message: 'must be Linux, macOS or Windows' });
|
||||
if ('tree_log_step' in p && (typeof p.tree_log_step !== 'string' || !p.tree_log_step.trim() || p.tree_log_step.length > 200 || /[\0\r\n]/.test(p.tree_log_step))) errors.push({ path: `${at}.tree_log_step`, message: 'must be the name of the step in a profile job that prints GSTACK_TREE_JSON <artifact> <base64 of tree.json>, one line of at most 200 characters' });
|
||||
if ('skippable' in p) {
|
||||
const s = p.skippable as Record<string, unknown> | null;
|
||||
const names = (v: unknown) => Array.isArray(v) && v.length > 0 && v.length <= 32 && v.every(x => typeof x === 'string' && !!x.trim() && x.length <= 128);
|
||||
@@ -216,8 +233,15 @@ export function verifyRun(deps: RunDeps, o: { repo: string; runRef: string; prof
|
||||
}
|
||||
const ranIn = ownAttempt(deps, id, run.run_attempt, jobs, p.jobs.filter(n => !skippedJobs.includes(n)));
|
||||
let rec: TreeRecord;
|
||||
let fromLog = false;
|
||||
try { rec = deps.artifact(id, p.artifact, ranIn); }
|
||||
catch (e: any) { return fail('artifact_missing', `${at} has no readable ${p.artifact} tree.json: ${String(e?.message ?? e).slice(0, 200)}`); }
|
||||
catch (e: any) {
|
||||
if (!(e instanceof NoArtifactError) || !p.tree_log_step || !deps.jobLog) return fail('artifact_missing', `${at} has no readable ${p.artifact} tree.json: ${String(e?.message ?? e).slice(0, 200)}`);
|
||||
const logged = treeFromLog(deps, id, ranIn, p, p.jobs.filter(n => !skippedJobs.includes(n)), at);
|
||||
if (!logged.ok) return logged;
|
||||
rec = logged.rec;
|
||||
fromLog = true;
|
||||
}
|
||||
if (!rec || typeof rec !== 'object') return fail('artifact_mismatch', `${at}'s ${p.artifact} tree.json is not a JSON object`);
|
||||
if (rec.reused === true || rec.reuse === true) return fail('artifact_reused', `${at} reused another run; a reuse is never evidence`);
|
||||
if (rec.sizing_experiment !== undefined) return fail('sizing_experiment', `${at} was a runner-sizing experiment (a dispatch that overrode the runner, shards or sub-shards); it is never a Free Tests result`);
|
||||
@@ -253,12 +277,60 @@ export function verifyRun(deps: RunDeps, o: { repo: string; runRef: string; prof
|
||||
const skipped = rec.skipped_files === undefined ? {} : { skipped: (rec.skipped_files as string[]).map(normalPath) };
|
||||
const rerun = ((rec.rerun_files ?? []) as string[]).map(normalPath);
|
||||
const reran = rerun.length ? { rerun, attempts: [...new Set((rec.attempts ?? []) as number[])].sort((a, b) => a - b) } : {};
|
||||
return { ok: true, evidence: { run_id: id, run_attempt: run.run_attempt, head_sha: run.head_sha, tree: headTree, profile: o.profileName, event: run.event, finished_at: finishedAt!, files, tests, ...skipped, ...(skippedJobs.length ? { skipped_jobs: skippedJobs } : {}), ...reran } };
|
||||
return { ok: true, evidence: { run_id: id, run_attempt: run.run_attempt, head_sha: run.head_sha, tree: headTree, profile: o.profileName, event: run.event, finished_at: finishedAt!, files, tests, ...skipped, ...(skippedJobs.length ? { skipped_jobs: skippedJobs } : {}), ...reran, ...(fromLog ? { tree_source: 'job-log' as const } : {}) } };
|
||||
} catch (e: any) {
|
||||
return fail('api_error', `reading run ${id} failed: ${String(e?.message ?? e).slice(0, 300)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tree.json run `id` logged at `attempt` for a profile that opted in with `tree_log_step`, read only when the run holds no
|
||||
* artifact (HARN-118). Exactly one TREE_LOG_LINE naming the profile's artifact across the profile's `names` jobs counts, and it
|
||||
* binds only by the runner's own timestamp, which a step's output cannot set: inside the named step (one step of that name,
|
||||
* a success), at least a second after every earlier step that ran ended and a second before every later one started,
|
||||
* since the API times steps only to the second. A test
|
||||
* that prints a well-formed line therefore adds a second line, or stands alone outside the step, and refuses either way.
|
||||
* The decoded tree.json is capped at MAX_ARTIFACT_BYTES; verifyRun then checks it exactly as it checks an artifact.
|
||||
*/
|
||||
function treeFromLog(deps: RunDeps, id: number, attempt: number, p: RunProfile, names: string[], at: string): { ok: true; rec: TreeRecord } | { ok: false; reason: RunRefusal; detail: string } {
|
||||
const missing = (detail: string) => ({ ok: false as const, reason: 'artifact_missing' as const, detail: `${at} has no unexpired ${p.artifact} artifact and ${detail}` });
|
||||
const hits: Array<{ job: ApiJob; stamp: string; data: string }> = [];
|
||||
for (const job of deps.jobs(id, attempt).filter(j => names.includes(j.name))) {
|
||||
if (!Number.isSafeInteger(job.id)) return missing(`its ${job.name} job has no numeric id, so its log cannot be read`);
|
||||
let text: string;
|
||||
try { text = deps.jobLog!(job.id!); } catch (e: any) { return missing(`its ${job.name} job's log cannot be read: ${String(e?.message ?? e).slice(0, 200)}`); }
|
||||
for (const raw of text.replace(/^\uFEFF/, '').split('\n')) {
|
||||
const m = TREE_LOG_LINE.exec(raw);
|
||||
if (m && m[1] === p.artifact) hits.push({ job, stamp: raw.slice(0, raw.indexOf(' ')), data: m[2]! });
|
||||
}
|
||||
}
|
||||
if (hits.length !== 1) return missing(`${hits.length} GSTACK_TREE_JSON lines name it in the logs of ${names.join(', ')} at attempt ${attempt}; exactly one counts`);
|
||||
const { job, stamp, data } = hits[0]!;
|
||||
const step = p.tree_log_step!;
|
||||
const unbound = (why: string) => ({ ok: false as const, reason: 'tree_log_unbound' as const, detail: `${at}'s ${p.artifact} line in the ${job.name} job ${why} (CI_CARRY_TREE_LOG_UNBOUND)` });
|
||||
if (job.conclusion !== 'success') return unbound(`is from a job that concluded ${job.conclusion}`);
|
||||
const named = (job.steps ?? []).filter(s => s.name === step);
|
||||
if (named.length !== 1) return unbound(named.length ? `cannot be bound: the job has ${named.length} steps named "${step}"` : `cannot be bound: the job has no step named "${step}"`);
|
||||
const s = named[0]!;
|
||||
if (s.conclusion !== 'success') return unbound(`cannot be bound: step "${step}" concluded ${s.conclusion}`);
|
||||
const ran = (job.steps ?? []).filter(x => x.number < s.number && x.conclusion !== 'skipped');
|
||||
const later = (job.steps ?? []).filter(x => x.number > s.number && x.conclusion !== 'skipped');
|
||||
const untimed = [s, ...ran, ...later].find(x => !Number.isFinite(Date.parse(x.started_at ?? '')) || !Number.isFinite(Date.parse(x.completed_at ?? '')));
|
||||
if (untimed) return unbound(`cannot be bound: step "${untimed.name}" has no start and finish time`);
|
||||
const t = LOG_STAMP.exec(stamp);
|
||||
const when = t ? Date.parse(`${t[1]}.${(t[2] ?? '').padEnd(3, '0').slice(0, 3)}Z`) : NaN;
|
||||
const from = Math.max(Date.parse(s.started_at!), ...ran.map(x => Date.parse(x.completed_at!) + 1000));
|
||||
const until = Math.min(Date.parse(s.completed_at!) + 1000, ...later.map(x => Date.parse(x.started_at!) - 1000));
|
||||
if (!(when >= from && when < until)) return unbound(`was stamped ${stamp.slice(0, 40)}, not inside step "${step}" (${s.started_at} to ${s.completed_at}) at least a second after every earlier step ended and before every later step started`);
|
||||
if (data.length > Math.ceil(MAX_ARTIFACT_BYTES / 3) * 4) return missing(`its line holds a tree.json over ${MAX_ARTIFACT_BYTES} bytes`);
|
||||
const bytes = Buffer.from(data, 'base64');
|
||||
if (bytes.length > MAX_ARTIFACT_BYTES) return missing(`its line holds a tree.json over ${MAX_ARTIFACT_BYTES} bytes`);
|
||||
const bad = { ok: false as const, reason: 'artifact_mismatch' as const };
|
||||
if (bytes.toString('base64') !== data) return { ...bad, detail: `${at}'s ${p.artifact} line is not canonical base64` };
|
||||
try { return { ok: true, rec: JSON.parse(bytes.toString('utf8')) as TreeRecord }; }
|
||||
catch { return { ...bad, detail: `${at}'s ${p.artifact} line does not decode to JSON` }; }
|
||||
}
|
||||
|
||||
/**
|
||||
* The newest attempt that ran any of `names` itself (GST-2284). A job-level rerun relists every job it did not rerun under the
|
||||
* new attempt with its old start and finish times, so a job listed with the same times and conclusion one attempt earlier
|
||||
@@ -332,7 +404,7 @@ export function verifyFromRun(src: RunSource, ref: string, names: string[], tree
|
||||
* whose files passed only on a re-run adds `rerun=<files> attempts=<list>` (GST-3054): allowed, but never shown as a clean pass.
|
||||
*/
|
||||
export function runReceiptFields(evidence: RunEvidence[], now: Date): string {
|
||||
return `runs=${evidence.map(e => `${e.run_id}/${e.run_attempt}`).join(',')} profiles=${evidence.map(e => e.profile).join(',')} heads=${[...new Set(evidence.map(e => e.head_sha.slice(0, 12)))].join(',')}${rerunMarker([...rerunByRun(evidence).values()])} verified=${now.toISOString()}`;
|
||||
return `runs=${evidence.map(e => `${e.run_id}/${e.run_attempt}`).join(',')} profiles=${evidence.map(e => e.profile).join(',')} heads=${[...new Set(evidence.map(e => e.head_sha.slice(0, 12)))].join(',')}${rerunMarker([...rerunByRun(evidence).values()])}${evidence.some(e => e.tree_source === 'job-log') ? ' tree_source=job-log' : ''} verified=${now.toISOString()}`;
|
||||
}
|
||||
|
||||
/** The re-run record per carried run (`<id>/<attempt>`): the distinct re-run files and attempts across every profile that read that run, so two profiles on one run count each file once. */
|
||||
@@ -394,7 +466,8 @@ export function ghRunDeps(repo: string, repoRoot: string): RunDeps {
|
||||
// A rerun of the uploading job leaves one artifact per attempt (GST-2284): read each by id and keep the one from `attempt`.
|
||||
artifact: (id, name, attempt) => {
|
||||
const listed = all<{ id: number; name: string; size_in_bytes: number; expired: boolean }>(`repos/${repo}/actions/runs/${id}/artifacts?name=${encodeURIComponent(name)}&per_page=100`, 'artifacts').filter(a => a.name === name && !a.expired);
|
||||
if (listed.length === 0 || listed.length > MAX_ARTIFACT_ATTEMPTS) throw new Error(`run ${id} has ${listed.length} unexpired artifacts named ${name}`);
|
||||
if (listed.length === 0) throw new NoArtifactError(`run ${id} has 0 unexpired artifacts named ${name}`);
|
||||
if (listed.length > MAX_ARTIFACT_ATTEMPTS) throw new Error(`run ${id} has ${listed.length} unexpired artifacts named ${name}`);
|
||||
for (const a of listed) if (!(a.size_in_bytes <= MAX_ARTIFACT_BYTES)) throw new Error(`artifact ${name} is ${a.size_in_bytes} bytes, over ${MAX_ARTIFACT_BYTES}`);
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-run-evidence-'));
|
||||
try {
|
||||
@@ -417,6 +490,11 @@ export function ghRunDeps(repo: string, repoRoot: string): RunDeps {
|
||||
return hits[0]!;
|
||||
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
||||
},
|
||||
// A job log holds the runner's escape sequences, which gh refuses to print without --allow-escape-sequences.
|
||||
jobLog: jobId => {
|
||||
if (!Number.isSafeInteger(jobId)) throw new Error(`job id ${jobId} is not a number`);
|
||||
return gh(['api', '--allow-escape-sequences', `repos/${repo}/actions/jobs/${jobId}/logs`], 120_000);
|
||||
},
|
||||
commitTree: sha => {
|
||||
const read = () => spawnSync('git', ['rev-parse', '--verify', '--quiet', `${sha}^{tree}`], { cwd: repoRoot, encoding: 'utf8', timeout: 30_000 });
|
||||
let r = read();
|
||||
|
||||
@@ -185,7 +185,7 @@ export function laneConcurrency(mode: string | undefined, cpus = os.availablePar
|
||||
return cpus <= 4 ? 1 : Infinity;
|
||||
}
|
||||
|
||||
export interface LaneResult { label: string; exit: number | null; testsRan: string; log: string; carried?: string; from?: string; held?: true; reused?: string; carryNone?: string; diagnosis?: string[]; notOwed?: string; here?: string; exception?: string; remoteGate?: string; standIn?: string }
|
||||
export interface LaneResult { label: string; exit: number | null; testsRan: string; log: string; carried?: string; treeSource?: string; from?: string; held?: true; reused?: string; carryNone?: string; diagnosis?: string[]; notOwed?: string; here?: string; exception?: string; remoteGate?: string; standIn?: string }
|
||||
|
||||
/** The base branch's `ship_lanes_from` entry for each lane whose command is a key or a declared alias, byte for byte (the ledger binds its sha256). */
|
||||
export function mappedLanes(cwd: string, base: string, list: { label: string; command: string }[]): Map<string, ShipLaneMapping> {
|
||||
@@ -206,7 +206,7 @@ export function carryLane(cwd: string, base: string, l: { label: string; command
|
||||
const rec = bin(cwd, 'gstack-evidence', ['record-ci', '--label', l.label, '--base', base, '--remote-gate-stands-in', '--', l.command], timeoutS * 1000);
|
||||
const got = recordCiResult(rec.stderr);
|
||||
if (rec.status === 0 && got.remoteGate) return { label: l.label, exit: 0, testsRan: got.tests!, log: '-', remoteGate: got.remoteGate, standIn: got.standIn ?? 'not carried' };
|
||||
if (rec.status === 0 && got.runs) return { label: l.label, exit: 0, testsRan: got.files ? `${got.tests} (files)` : got.tests!, log: '-', carried: got.runs, ...(got.notOwed ? { notOwed: got.notOwed } : {}), ...(got.here ? { here: got.here } : {}), ...(got.exception ? { exception: got.exception } : {}), ...(got.reused ? { reused: got.reused } : {}), ...(got.from ? { from: got.from } : {}) };
|
||||
if (rec.status === 0 && got.runs) return { label: l.label, exit: 0, testsRan: got.files ? `${got.tests} (files)` : got.tests!, log: '-', carried: got.runs, ...(got.treeSource ? { treeSource: got.treeSource } : {}), ...(got.notOwed ? { notOwed: got.notOwed } : {}), ...(got.here ? { here: got.here } : {}), ...(got.exception ? { exception: got.exception } : {}), ...(got.reused ? { reused: got.reused } : {}), ...(got.from ? { from: got.from } : {}) };
|
||||
return { label: l.label, exit: null, testsRan: 'unknown', log: '-', carryNone: got.none ?? `record_failed: ${firstLine(rec.stderr)}`, ...(got.diagnosis ? { diagnosis: got.diagnosis } : {}) };
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ export async function runLanes(cwd: string, list: { label: string; command: stri
|
||||
/** Step 5's lane verdict: a lane passes on its runner count, never exit 0 alone; a carried lane names its CI runs. */
|
||||
export function laneLine(l: LaneResult): { line: string; ok: boolean } {
|
||||
if (l.notOwed) return { line: `LANE: ${l.label} not owed: ${l.notOwed} run=${l.carried}`, ok: true };
|
||||
if (l.carried) return { line: `LANE: ${l.label} carried run=${l.carried} tests_ran=${l.testsRan}${l.here ? ` ${l.here}` : ''}${l.from ? ` from=${l.from} (same tree as HEAD)` : ''}${l.reused ? ` (reused the CI record at tree ${l.reused})` : ''}`, ok: true };
|
||||
if (l.carried) return { line: `LANE: ${l.label} carried run=${l.carried} tests_ran=${l.testsRan}${l.treeSource ? ` tree_source=${l.treeSource}` : ''}${l.here ? ` ${l.here}` : ''}${l.from ? ` from=${l.from} (same tree as HEAD)` : ''}${l.reused ? ` (reused the CI record at tree ${l.reused})` : ''}`, ok: true };
|
||||
if (l.remoteGate) return { line: `LANE: ${l.label} satisfied by remote_gate receipt=${l.remoteGate}`, ok: true };
|
||||
if (l.held) return { line: `LANE: ${l.label} held: not run`, ok: false };
|
||||
const zero = l.exit === 0 && l.testsRan === '0';
|
||||
|
||||
@@ -267,7 +267,7 @@ function simulate(cwd: string, base: string, list: { label: string; command: str
|
||||
const v = findCarry(check, { profiles: m.profiles, head, tree, wtree, ...mappingCoverage(m, cwd) });
|
||||
if (!v.ok) { say(`CI_CARRY: none label=${l.label} (${v.reason}: ${v.detail})`); continue; }
|
||||
carried++;
|
||||
say(`LANE: ${l.label} would carry run=${v.runs.map(e => `${e.run_id}/${e.run_attempt}`).join(',')} tests_ran=${v.tests}${v.unit ? ` unit=${v.unit}` : ''}${v.remainder ? ` and would run ${v.remainder.length} skipped file(s) here with per_file` : ''}`);
|
||||
say(`LANE: ${l.label} would carry run=${v.runs.map(e => `${e.run_id}/${e.run_attempt}`).join(',')} tests_ran=${v.tests}${v.unit ? ` unit=${v.unit}` : ''}${v.runs.some(e => e.tree_source === 'job-log') ? ' tree_source=job-log' : ''}${v.remainder ? ` and would run ${v.remainder.length} skipped file(s) here with per_file` : ''}`);
|
||||
}
|
||||
say(`${carried} of ${mapped} mapped lane(s) would carry once origin/${base} holds this map`);
|
||||
return done(mapped > 0 && carried === mapped ? 0 : 1);
|
||||
|
||||
+1
-1
@@ -220,7 +220,7 @@ export function receiptSummaryLine(r: ShipReceipt): string {
|
||||
const seen = runs.get(key) ?? { files: 0, attempts: [] };
|
||||
runs.set(key, { files: Math.max(seen.files, typeof c.rerun === 'number' ? c.rerun : 0), attempts: [...seen.attempts, ...(Array.isArray(c.attempts) ? c.attempts : [])] });
|
||||
}
|
||||
parts.push(`ci_carried=${r.ci_carried.map(c => `${c.lane}:${c.run}/${c.attempt}`).join(',')}${rerunMarker([...runs.values()])}`);
|
||||
parts.push(`ci_carried=${r.ci_carried.map(c => `${c.lane}:${c.run}/${c.attempt}`).join(',')}${rerunMarker([...runs.values()])}${r.ci_carried.some(c => c.tree_source === 'job-log') ? ' tree_source=job-log' : ''}`);
|
||||
}
|
||||
if (r.base_red && typeof r.base_red.base === 'string') parts.push(`base_red=${r.base_red.base.slice(0, 12)}`);
|
||||
return `SHIP_RECEIPT: ${oneLine(parts.join(' '))}`;
|
||||
|
||||
Reference in new issue
Block a user