vouchington-tooling 0.58.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/dist/agent-issue-labels/ensure-labels.d.mts +1 -1
- package/dist/agent-issue-labels/ensure-labels.mjs +1 -1
- package/dist/browser-session-runner/tail-queue.d.mts +1 -1
- package/dist/browser-session-runner/tail-queue.mjs +1 -1
- package/dist/cli/commands/gitleaks-directory-scan.d.mts +4 -0
- package/dist/cli/commands/gitleaks-directory-scan.mjs +9 -0
- package/dist/cli/commands/transient-retry.d.mts +13 -0
- package/dist/cli/commands/transient-retry.mjs +68 -0
- package/dist/cli/index.mjs +8 -53
- package/dist/cli/parse-forwarded-commands.d.mts +6 -0
- package/dist/cli/parse-forwarded-commands.mjs +4 -0
- package/dist/cli/parse.d.mts +6 -0
- package/dist/cli/script-paths.d.mts +5 -0
- package/dist/cli/script-paths.mjs +48 -0
- package/dist/cli/usage.d.mts +1 -1
- package/dist/cli/usage.mjs +7 -0
- package/dist/dead-code/baseline.d.mts +19 -0
- package/dist/dead-code/baseline.mjs +124 -0
- package/dist/dead-code/child-command.d.mts +6 -0
- package/dist/dead-code/child-command.mjs +107 -0
- package/dist/dead-code/cli.d.mts +5 -0
- package/dist/dead-code/cli.mjs +71 -0
- package/dist/dead-code/entry-roots.d.mts +7 -0
- package/dist/dead-code/entry-roots.mjs +43 -0
- package/dist/dead-code/fixture-deadline.test-helpers.d.mts +1 -0
- package/dist/dead-code/fixture-deadline.test-helpers.mjs +9 -0
- package/dist/dead-code/gate.d.mts +29 -0
- package/dist/dead-code/gate.mjs +33 -0
- package/dist/dead-code/gate.test-helpers.d.mts +14 -0
- package/dist/dead-code/gate.test-helpers.mjs +35 -0
- package/dist/dead-code/index.d.mts +9 -0
- package/dist/dead-code/index.mjs +7 -0
- package/dist/dead-code/scan.d.mts +11 -0
- package/dist/dead-code/scan.mjs +37 -0
- package/dist/dead-code/tracked-snapshot.d.mts +2 -0
- package/dist/dead-code/tracked-snapshot.mjs +72 -0
- package/dist/markdown/details-scope.d.mts +1 -1
- package/dist/markdown/details-scope.mjs +1 -1
- package/dist/transient-retry/attempts.d.mts +1 -1
- package/dist/transient-retry/attempts.mjs +1 -1
- package/dist/transient-retry/decision-evaluator.d.mts +3 -3
- package/dist/transient-retry/gh-api.d.mts +30 -0
- package/dist/transient-retry/gh-api.mjs +68 -0
- package/dist/transient-retry/github-actions-log.d.mts +7 -0
- package/dist/transient-retry/github-actions-log.mjs +33 -0
- package/dist/transient-retry/index.d.mts +15 -0
- package/dist/transient-retry/index.mjs +8 -0
- package/dist/transient-retry/log-stream.d.mts +12 -0
- package/dist/transient-retry/log-stream.mjs +39 -0
- package/dist/transient-retry/log-window.d.mts +18 -0
- package/dist/transient-retry/log-window.mjs +94 -0
- package/dist/transient-retry/rerun.d.mts +23 -0
- package/dist/transient-retry/rerun.mjs +68 -0
- package/dist/transient-retry/run-context-annotations.d.mts +7 -0
- package/dist/transient-retry/run-context-annotations.mjs +44 -0
- package/dist/transient-retry/run-context-jobs.d.mts +20 -0
- package/dist/transient-retry/run-context-jobs.mjs +54 -0
- package/dist/transient-retry/run-context-logs.d.mts +12 -0
- package/dist/transient-retry/run-context-logs.mjs +86 -0
- package/dist/transient-retry/run-context-rule-attempts.d.mts +14 -0
- package/dist/transient-retry/run-context-rule-attempts.mjs +65 -0
- package/dist/transient-retry/run-context-types.d.mts +49 -0
- package/dist/transient-retry/run-context-types.mjs +3 -0
- package/dist/transient-retry/run-context.d.mts +9 -0
- package/dist/transient-retry/run-context.mjs +25 -0
- package/dist/transient-retry/run-context.test-helpers.d.mts +16 -0
- package/dist/transient-retry/run-context.test-helpers.mjs +56 -0
- package/dist/transient-retry/types.d.mts +8 -7
- package/dist/vitest-diagnostics/vitest-worker-exit-diagnostics-reporter.mjs +1 -1
- package/dist/workflow-secrets-drift/gh.d.mts +1 -1
- package/dist/workflow-secrets-drift/gh.mjs +1 -1
- package/package.json +6 -1
- package/scripts/__pycache__/run_bounded_process.cpython-312.pyc +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { setTimeout as sleepFor } from 'node:timers/promises';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { ghSpawnEnv } from "../gh-cli/env.mjs";
|
|
5
|
+
import { createCommandRunner } from "../gh-cli/exec.mjs";
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
/** Substrings of a failed `gh` call that mark a transport hiccup worth retrying. */
|
|
8
|
+
export const DEFAULT_RETRYABLE_MARKERS = [
|
|
9
|
+
'ECONNRESET',
|
|
10
|
+
'ETIMEDOUT',
|
|
11
|
+
'EAI_AGAIN',
|
|
12
|
+
'connection reset',
|
|
13
|
+
'TLS handshake timeout',
|
|
14
|
+
'unexpected EOF',
|
|
15
|
+
'HTTP 502',
|
|
16
|
+
'HTTP 503',
|
|
17
|
+
'HTTP 504',
|
|
18
|
+
];
|
|
19
|
+
/** The largest stdout one buffered `gh api` call may return; job logs are streamed, not buffered. */
|
|
20
|
+
export const API_MAX_BUFFER_BYTES = 16 * 1024 * 1024;
|
|
21
|
+
/**
|
|
22
|
+
* Builds the default `gh` runner. `tokenEnv` names the environment variable that holds the
|
|
23
|
+
* token; when set and present it is passed to `gh` as `GH_TOKEN`. Otherwise `gh` uses its own
|
|
24
|
+
* credentials.
|
|
25
|
+
*/
|
|
26
|
+
export function createGhRunner(tokenEnv) {
|
|
27
|
+
return createCommandRunner('gh', (command, args, options) => execFileAsync(command, args, {
|
|
28
|
+
...options,
|
|
29
|
+
encoding: 'utf8',
|
|
30
|
+
maxBuffer: API_MAX_BUFFER_BYTES,
|
|
31
|
+
}), ghEnvWithToken(tokenEnv));
|
|
32
|
+
}
|
|
33
|
+
/** The `gh` child environment: forced color removed, and `tokenEnv`'s value exposed as `GH_TOKEN`. */
|
|
34
|
+
export function ghEnvWithToken(tokenEnv) {
|
|
35
|
+
return (env) => {
|
|
36
|
+
const base = ghSpawnEnv(env);
|
|
37
|
+
const token = tokenEnv === undefined ? undefined : env[tokenEnv];
|
|
38
|
+
return token ? { ...base, GH_TOKEN: token } : base;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function errorText(error) {
|
|
42
|
+
const stderr = error?.stderr;
|
|
43
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
44
|
+
return typeof stderr === 'string' ? `${message}\n${stderr}` : message;
|
|
45
|
+
}
|
|
46
|
+
export function createGhApi({ run = createGhRunner(), attempts = 3, retryableMarkers = DEFAULT_RETRYABLE_MARKERS, sleep = (ms) => sleepFor(ms), warn = (message) => console.error(message), } = {}) {
|
|
47
|
+
if (!Number.isInteger(attempts) || attempts < 1) {
|
|
48
|
+
throw new Error(`Invalid gh api retry attempts: ${attempts}`);
|
|
49
|
+
}
|
|
50
|
+
const isRetryable = (error) => {
|
|
51
|
+
const text = errorText(error);
|
|
52
|
+
return retryableMarkers.some((marker) => text.includes(marker));
|
|
53
|
+
};
|
|
54
|
+
const call = async (args) => {
|
|
55
|
+
for (let attempt = 1;; attempt += 1) {
|
|
56
|
+
try {
|
|
57
|
+
return await run(['api', ...args]);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (attempt >= attempts || !isRetryable(error))
|
|
61
|
+
throw error;
|
|
62
|
+
warn(`::warning::gh api attempt ${attempt}/${attempts} failed; retrying`);
|
|
63
|
+
await sleep(attempt * 1000);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
return { call, isRetryable, sleep, run };
|
|
68
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface GithubActionsStepGroupSlice {
|
|
2
|
+
header: string;
|
|
3
|
+
log: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function getGithubActionsStepGroupSlices(log: string): GithubActionsStepGroupSlice[];
|
|
6
|
+
export declare function sliceGithubActionsStepGroup(log: string, runGroup: string): string;
|
|
7
|
+
export declare function terminalFailedGithubActionsStepLog(log: string): string;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function findGithubActionsStepGroupStarts(log) {
|
|
2
|
+
return [...log.matchAll(/##\[group\]Run [^\n]*/g)].map((match) => match.index);
|
|
3
|
+
}
|
|
4
|
+
export function getGithubActionsStepGroupSlices(log) {
|
|
5
|
+
const starts = findGithubActionsStepGroupStarts(log);
|
|
6
|
+
return starts.map((start, index) => {
|
|
7
|
+
const end = starts[index + 1] ?? log.length;
|
|
8
|
+
const headerEnd = log.indexOf('\n', start);
|
|
9
|
+
return {
|
|
10
|
+
header: headerEnd === -1 ? log.slice(start, end) : log.slice(start, headerEnd),
|
|
11
|
+
log: log.slice(start, end),
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export function sliceGithubActionsStepGroup(log, runGroup) {
|
|
16
|
+
const start = log.indexOf(runGroup);
|
|
17
|
+
if (start === -1)
|
|
18
|
+
return '';
|
|
19
|
+
const searchStart = start + runGroup.length;
|
|
20
|
+
const nextGroupIndex = log.slice(searchStart).search(/##\[group\]Run [^\n]*/);
|
|
21
|
+
const end = nextGroupIndex === -1 ? log.length : searchStart + nextGroupIndex;
|
|
22
|
+
return log.slice(start, end);
|
|
23
|
+
}
|
|
24
|
+
const stepFailurePattern = /(?:Process completed with exit code [1-9]\d*\.|The action(?: '[^']+')? has timed out)/;
|
|
25
|
+
export function terminalFailedGithubActionsStepLog(log) {
|
|
26
|
+
const slices = getGithubActionsStepGroupSlices(log);
|
|
27
|
+
for (let index = slices.length - 1; index >= 0; index -= 1) {
|
|
28
|
+
const stepLog = slices[index].log;
|
|
29
|
+
if (stepFailurePattern.test(stepLog))
|
|
30
|
+
return stepLog;
|
|
31
|
+
}
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
@@ -2,3 +2,18 @@ export { deriveRetryAttempt } from './attempts.mts';
|
|
|
2
2
|
export { decide } from './decision-evaluator.mts';
|
|
3
3
|
export type { DecisionResult, EvaluateRulesOptions, NoMatchReason } from './decision-evaluator.mts';
|
|
4
4
|
export type { RetryContext, RetryDecision, RetryRule, RetryTarget } from './types.mts';
|
|
5
|
+
export { API_MAX_BUFFER_BYTES, createGhApi, createGhRunner, ghEnvWithToken, DEFAULT_RETRYABLE_MARKERS, } from './gh-api.mts';
|
|
6
|
+
export type { GhApi, GhApiOptions } from './gh-api.mts';
|
|
7
|
+
export { getGithubActionsStepGroupSlices, sliceGithubActionsStepGroup, terminalFailedGithubActionsStepLog, } from './github-actions-log.mts';
|
|
8
|
+
export type { GithubActionsStepGroupSlice } from './github-actions-log.mts';
|
|
9
|
+
export { createGhLogStreamer, streamGhApiLog } from './log-stream.mts';
|
|
10
|
+
export { DEFAULT_LOG_LIMITS, LOG_OMISSION_MARKER } from './log-window.mts';
|
|
11
|
+
export type { LogLimits } from './log-window.mts';
|
|
12
|
+
export { NO_RERUN, RERUN_REQUESTED, rerunKnownTransient } from './rerun.mts';
|
|
13
|
+
export type { RerunKnownTransientOptions } from './rerun.mts';
|
|
14
|
+
export { buildWorkflowRunContext } from './run-context.mts';
|
|
15
|
+
export type { BuildWorkflowRunContextOptions } from './run-context.mts';
|
|
16
|
+
export { fetchWorkflowRunSummary } from './run-context-jobs.mts';
|
|
17
|
+
export type { WorkflowRunSummary } from './run-context-jobs.mts';
|
|
18
|
+
export { isFailedWorkflowConclusion } from './run-context-types.mts';
|
|
19
|
+
export type { JobEntry, RunFetchEnv, WorkflowConclusion, WorkflowJobStep, WorkflowRetryRule, WorkflowRunContext, } from './run-context-types.mts';
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
export { deriveRetryAttempt } from "./attempts.mjs";
|
|
2
2
|
export { decide } from "./decision-evaluator.mjs";
|
|
3
|
+
export { API_MAX_BUFFER_BYTES, createGhApi, createGhRunner, ghEnvWithToken, DEFAULT_RETRYABLE_MARKERS, } from "./gh-api.mjs";
|
|
4
|
+
export { getGithubActionsStepGroupSlices, sliceGithubActionsStepGroup, terminalFailedGithubActionsStepLog, } from "./github-actions-log.mjs";
|
|
5
|
+
export { createGhLogStreamer, streamGhApiLog } from "./log-stream.mjs";
|
|
6
|
+
export { DEFAULT_LOG_LIMITS, LOG_OMISSION_MARKER } from "./log-window.mjs";
|
|
7
|
+
export { NO_RERUN, RERUN_REQUESTED, rerunKnownTransient } from "./rerun.mjs";
|
|
8
|
+
export { buildWorkflowRunContext } from "./run-context.mjs";
|
|
9
|
+
export { fetchWorkflowRunSummary } from "./run-context-jobs.mjs";
|
|
10
|
+
export { isFailedWorkflowConclusion } from "./run-context-types.mjs";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ChildProcessByStdio } from 'node:child_process';
|
|
2
|
+
import type { Readable } from 'node:stream';
|
|
3
|
+
type GhChild = ChildProcessByStdio<null, Readable, Readable>;
|
|
4
|
+
export type SpawnGh = (args: string[]) => GhChild;
|
|
5
|
+
/** A log streamer whose `gh` child sees `tokenEnv`'s value as `GH_TOKEN`, like `createGhRunner`. */
|
|
6
|
+
export declare const createGhLogStreamer: (tokenEnv?: string) => (path: string) => AsyncGenerator<Buffer<ArrayBufferLike>, any, any>;
|
|
7
|
+
/**
|
|
8
|
+
* Streams one `gh api` response body in chunks so the caller never buffers a whole job log. If the
|
|
9
|
+
* consumer stops early the child is killed. A non-zero exit throws with at most 4 KiB of stderr.
|
|
10
|
+
*/
|
|
11
|
+
export declare function streamGhApiLog(path: string, spawnChild?: SpawnGh): AsyncGenerator<Buffer>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { ghEnvWithToken } from "./gh-api.mjs";
|
|
3
|
+
const STDERR_MAX_BYTES = 4 * 1024;
|
|
4
|
+
const spawnGhWith = (buildEnv) => (args) => spawn('gh', args, { env: buildEnv(process.env), stdio: ['ignore', 'pipe', 'pipe'] });
|
|
5
|
+
/** A log streamer whose `gh` child sees `tokenEnv`'s value as `GH_TOKEN`, like `createGhRunner`. */
|
|
6
|
+
export const createGhLogStreamer = (tokenEnv) => (path) => streamGhApiLog(path, spawnGhWith(ghEnvWithToken(tokenEnv)));
|
|
7
|
+
/**
|
|
8
|
+
* Streams one `gh api` response body in chunks so the caller never buffers a whole job log. If the
|
|
9
|
+
* consumer stops early the child is killed. A non-zero exit throws with at most 4 KiB of stderr.
|
|
10
|
+
*/
|
|
11
|
+
export async function* streamGhApiLog(path, spawnChild = spawnGhWith(ghEnvWithToken())) {
|
|
12
|
+
const child = spawnChild(['api', '--allow-escape-sequences', path]);
|
|
13
|
+
const stderr = [];
|
|
14
|
+
let stderrBytes = 0;
|
|
15
|
+
child.stderr.on('data', (chunk) => {
|
|
16
|
+
const part = Buffer.from(chunk).subarray(0, STDERR_MAX_BYTES - stderrBytes);
|
|
17
|
+
stderr.push(part);
|
|
18
|
+
stderrBytes += part.byteLength;
|
|
19
|
+
});
|
|
20
|
+
const closed = new Promise((resolve, reject) => {
|
|
21
|
+
child.once('error', reject);
|
|
22
|
+
child.once('close', resolve);
|
|
23
|
+
});
|
|
24
|
+
closed.catch(() => undefined);
|
|
25
|
+
let finished = false;
|
|
26
|
+
try {
|
|
27
|
+
for await (const chunk of child.stdout)
|
|
28
|
+
yield Buffer.from(chunk);
|
|
29
|
+
finished = true;
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
if (!finished)
|
|
33
|
+
child.kill();
|
|
34
|
+
}
|
|
35
|
+
const code = await closed;
|
|
36
|
+
if (code !== 0) {
|
|
37
|
+
throw new Error(`gh api ${path} exited with status ${String(code)}: ${Buffer.concat(stderr).toString('utf8')}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const LOG_OMISSION_MARKER = "\n[...job log middle omitted...]\n";
|
|
2
|
+
export interface LogLimits {
|
|
3
|
+
/** Bytes kept from the start of each job log. */
|
|
4
|
+
headBytes: number;
|
|
5
|
+
/** Bytes kept from the end of each job log. */
|
|
6
|
+
tailBytes: number;
|
|
7
|
+
/** Most bytes read from one job log stream before the rest is abandoned. */
|
|
8
|
+
maxReadBytes: number;
|
|
9
|
+
/** Most bytes retained across every job log of one run context. */
|
|
10
|
+
totalBytes: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const DEFAULT_LOG_LIMITS: LogLimits;
|
|
13
|
+
/**
|
|
14
|
+
* Reads a log stream keeping only a head and a tail window, so memory stays bounded by the limits
|
|
15
|
+
* rather than by the log size. Bytes between the windows are replaced by an omission marker.
|
|
16
|
+
*/
|
|
17
|
+
export declare function collectDiagnosticWindow(stream: AsyncIterable<Buffer>, { headBytes, tailBytes, maxReadBytes }: LogLimits): Promise<string>;
|
|
18
|
+
export declare function fitDiagnosticWindow(log: string, maxBytes: number): string;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export const LOG_OMISSION_MARKER = '\n[...job log middle omitted...]\n';
|
|
2
|
+
export const DEFAULT_LOG_LIMITS = {
|
|
3
|
+
headBytes: 256 * 1024,
|
|
4
|
+
tailBytes: 512 * 1024,
|
|
5
|
+
maxReadBytes: 64 * 1024 * 1024,
|
|
6
|
+
totalBytes: 16 * 1024 * 1024,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Reads a log stream keeping only a head and a tail window, so memory stays bounded by the limits
|
|
10
|
+
* rather than by the log size. Bytes between the windows are replaced by an omission marker.
|
|
11
|
+
*/
|
|
12
|
+
export async function collectDiagnosticWindow(stream, { headBytes, tailBytes, maxReadBytes }) {
|
|
13
|
+
const head = [];
|
|
14
|
+
const tail = [];
|
|
15
|
+
let headLength = 0;
|
|
16
|
+
let tailLength = 0;
|
|
17
|
+
let total = 0;
|
|
18
|
+
let capped = false;
|
|
19
|
+
for await (let chunk of stream) {
|
|
20
|
+
if (total + chunk.byteLength > maxReadBytes) {
|
|
21
|
+
chunk = chunk.subarray(0, maxReadBytes - total);
|
|
22
|
+
capped = true;
|
|
23
|
+
}
|
|
24
|
+
total += chunk.byteLength;
|
|
25
|
+
const headPart = chunk.subarray(0, headBytes - headLength);
|
|
26
|
+
if (headPart.byteLength > 0)
|
|
27
|
+
head.push(headPart);
|
|
28
|
+
headLength += headPart.byteLength;
|
|
29
|
+
tail.push(chunk);
|
|
30
|
+
tailLength += chunk.byteLength;
|
|
31
|
+
while (tailLength > tailBytes) {
|
|
32
|
+
const excess = tailLength - tailBytes;
|
|
33
|
+
const first = tail[0];
|
|
34
|
+
if (first.byteLength <= excess)
|
|
35
|
+
tail.shift();
|
|
36
|
+
else
|
|
37
|
+
tail[0] = first.subarray(excess);
|
|
38
|
+
tailLength -= Math.min(first.byteLength, excess);
|
|
39
|
+
}
|
|
40
|
+
if (capped)
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
const headBuffer = Buffer.concat(head);
|
|
44
|
+
const tailBuffer = Buffer.concat(tail);
|
|
45
|
+
if (capped || total > headBytes + tailBytes) {
|
|
46
|
+
return headBuffer.toString('utf8') + LOG_OMISSION_MARKER + tailBuffer.toString('utf8');
|
|
47
|
+
}
|
|
48
|
+
const unseen = total - headBuffer.byteLength;
|
|
49
|
+
return (headBuffer.toString('utf8') +
|
|
50
|
+
tailBuffer.subarray(tailBuffer.byteLength - unseen).toString('utf8'));
|
|
51
|
+
}
|
|
52
|
+
export function fitDiagnosticWindow(log, maxBytes) {
|
|
53
|
+
if (Buffer.byteLength(log) <= maxBytes)
|
|
54
|
+
return log;
|
|
55
|
+
const markerBytes = Buffer.byteLength(LOG_OMISSION_MARKER);
|
|
56
|
+
if (maxBytes <= markerBytes)
|
|
57
|
+
return takeUtf8Suffix(log, maxBytes);
|
|
58
|
+
const contentBytes = maxBytes - markerBytes;
|
|
59
|
+
const headBytes = Math.floor(contentBytes / 3);
|
|
60
|
+
return (takeUtf8Prefix(log, headBytes) +
|
|
61
|
+
LOG_OMISSION_MARKER +
|
|
62
|
+
takeUtf8Suffix(log, contentBytes - headBytes));
|
|
63
|
+
}
|
|
64
|
+
function takeUtf8Prefix(value, maxBytes) {
|
|
65
|
+
let bytes = 0;
|
|
66
|
+
let end = 0;
|
|
67
|
+
for (const character of value) {
|
|
68
|
+
const characterBytes = Buffer.byteLength(character);
|
|
69
|
+
if (bytes + characterBytes > maxBytes)
|
|
70
|
+
break;
|
|
71
|
+
bytes += characterBytes;
|
|
72
|
+
end += character.length;
|
|
73
|
+
}
|
|
74
|
+
return value.slice(0, end);
|
|
75
|
+
}
|
|
76
|
+
function takeUtf8Suffix(value, maxBytes) {
|
|
77
|
+
let bytes = 0;
|
|
78
|
+
let start = value.length;
|
|
79
|
+
while (start > 0) {
|
|
80
|
+
let characterStart = start - 1;
|
|
81
|
+
const trailing = value.charCodeAt(characterStart);
|
|
82
|
+
if (trailing >= 0xdc00 && trailing <= 0xdfff && characterStart > 0) {
|
|
83
|
+
const leading = value.charCodeAt(characterStart - 1);
|
|
84
|
+
if (leading >= 0xd800 && leading <= 0xdbff)
|
|
85
|
+
characterStart -= 1;
|
|
86
|
+
}
|
|
87
|
+
const characterBytes = Buffer.byteLength(value.slice(characterStart, start));
|
|
88
|
+
if (bytes + characterBytes > maxBytes)
|
|
89
|
+
break;
|
|
90
|
+
bytes += characterBytes;
|
|
91
|
+
start = characterStart;
|
|
92
|
+
}
|
|
93
|
+
return value.slice(start);
|
|
94
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type GhApi } from './gh-api.mts';
|
|
2
|
+
import { type LogLimits } from './log-window.mts';
|
|
3
|
+
import { type WorkflowRetryRule } from './run-context-types.mts';
|
|
4
|
+
export interface RerunKnownTransientOptions {
|
|
5
|
+
/** `owner/name`; inferred from the current checkout when omitted. */
|
|
6
|
+
repository?: string;
|
|
7
|
+
runId: string;
|
|
8
|
+
/** The caller's rule catalog. The engine has no built-in rules. */
|
|
9
|
+
rules: readonly WorkflowRetryRule[];
|
|
10
|
+
dryRun?: boolean;
|
|
11
|
+
limits?: Partial<LogLimits>;
|
|
12
|
+
api?: GhApi;
|
|
13
|
+
streamLog?: (path: string) => AsyncIterable<Buffer>;
|
|
14
|
+
log?: (message: string) => void;
|
|
15
|
+
}
|
|
16
|
+
/** Process exit codes: a rerun was requested (or would be), or none was warranted. */
|
|
17
|
+
export declare const RERUN_REQUESTED = 0;
|
|
18
|
+
export declare const NO_RERUN = 2;
|
|
19
|
+
/**
|
|
20
|
+
* Evaluates the injected rules for a failed workflow run and reruns it only when one rule asks
|
|
21
|
+
* for a rerun. Only job names and the decision are logged; log text is never printed.
|
|
22
|
+
*/
|
|
23
|
+
export declare function rerunKnownTransient({ repository: requestedRepository, runId, rules, dryRun, limits, api, streamLog, log, }: RerunKnownTransientOptions): Promise<number>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { decide } from "./decision-evaluator.mjs";
|
|
2
|
+
import { createGhApi } from "./gh-api.mjs";
|
|
3
|
+
import { streamGhApiLog } from "./log-stream.mjs";
|
|
4
|
+
import { DEFAULT_LOG_LIMITS } from "./log-window.mjs";
|
|
5
|
+
import { buildWorkflowRunContext } from "./run-context.mjs";
|
|
6
|
+
import { fetchWorkflowRunSummary } from "./run-context-jobs.mjs";
|
|
7
|
+
import { isFailedWorkflowConclusion, } from "./run-context-types.mjs";
|
|
8
|
+
/** Process exit codes: a rerun was requested (or would be), or none was warranted. */
|
|
9
|
+
export const RERUN_REQUESTED = 0;
|
|
10
|
+
export const NO_RERUN = 2;
|
|
11
|
+
async function resolveRepository(repository, api) {
|
|
12
|
+
if (repository)
|
|
13
|
+
return repository;
|
|
14
|
+
try {
|
|
15
|
+
const inferred = (await api.run(['repo', 'view', '--json', 'nameWithOwner', '--jq', '.nameWithOwner'])).trim();
|
|
16
|
+
if (inferred)
|
|
17
|
+
return inferred;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// Fall through to the actionable error below.
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Missing repository. Pass --repo owner/name, set GITHUB_REPOSITORY, or run from a gh-authenticated checkout.');
|
|
23
|
+
}
|
|
24
|
+
function describeDecision(result) {
|
|
25
|
+
return result.matchedRule ? `${result.decision} (rule ${result.matchedRule})` : result.decision;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Evaluates the injected rules for a failed workflow run and reruns it only when one rule asks
|
|
29
|
+
* for a rerun. Only job names and the decision are logged; log text is never printed.
|
|
30
|
+
*/
|
|
31
|
+
export async function rerunKnownTransient({ repository: requestedRepository, runId, rules, dryRun = false, limits, api = createGhApi(), streamLog = streamGhApiLog, log = console.log, }) {
|
|
32
|
+
const repository = await resolveRepository(requestedRepository, api);
|
|
33
|
+
const env = {
|
|
34
|
+
api,
|
|
35
|
+
limits: { ...DEFAULT_LOG_LIMITS, ...limits },
|
|
36
|
+
repository,
|
|
37
|
+
runId,
|
|
38
|
+
streamLog,
|
|
39
|
+
};
|
|
40
|
+
const summary = await fetchWorkflowRunSummary(env);
|
|
41
|
+
log(`Workflow: ${summary.workflowName}`);
|
|
42
|
+
log(`Conclusion: ${summary.conclusion ?? '(none)'}`);
|
|
43
|
+
if (!isFailedWorkflowConclusion(summary.conclusion)) {
|
|
44
|
+
log('Run conclusion is not failure, timed_out, or cancelled; no rerun requested.');
|
|
45
|
+
return NO_RERUN;
|
|
46
|
+
}
|
|
47
|
+
const context = await buildWorkflowRunContext(env, {
|
|
48
|
+
conclusion: summary.conclusion,
|
|
49
|
+
rules,
|
|
50
|
+
runAttempt: summary.runAttempt,
|
|
51
|
+
workflowName: summary.workflowName,
|
|
52
|
+
});
|
|
53
|
+
const result = await decide(context, rules, { unmatchedDecision: 'dispatch' });
|
|
54
|
+
log(`Run attempt: ${context.runAttempt} (retry attempt: ${context.retryAttempt})`);
|
|
55
|
+
log(`Failed jobs: ${context.failedJobNames.join(', ') || '(none)'}`);
|
|
56
|
+
log(`Decision: ${describeDecision(result)}`);
|
|
57
|
+
if (result.decision !== 'rerun') {
|
|
58
|
+
log(`No rerun requested: decision is '${result.decision}', not 'rerun'.`);
|
|
59
|
+
return NO_RERUN;
|
|
60
|
+
}
|
|
61
|
+
if (dryRun) {
|
|
62
|
+
log(`Dry run: would run gh run rerun --repo ${repository} ${runId}`);
|
|
63
|
+
return RERUN_REQUESTED;
|
|
64
|
+
}
|
|
65
|
+
await api.run(['run', 'rerun', '--repo', repository, runId]);
|
|
66
|
+
log(`Requested rerun for run ${runId}.`);
|
|
67
|
+
return RERUN_REQUESTED;
|
|
68
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { JobEntry, RunFetchEnv } from './run-context-types.mts';
|
|
2
|
+
export declare function annotationMessagesFromPages(stdout: string): string[];
|
|
3
|
+
/** Lazily fetches and caches check-run annotation messages for the failed jobs. */
|
|
4
|
+
export declare function createAnnotationFetchers(env: RunFetchEnv, failedJobs: readonly JobEntry[]): {
|
|
5
|
+
failedJobAnnotations: (jobName: string) => Promise<string[]>;
|
|
6
|
+
failedJobAnnotationFetchFailures: () => Promise<Set<string>>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function annotationMessagesFromPages(stdout) {
|
|
2
|
+
const pages = JSON.parse(stdout.trim());
|
|
3
|
+
return Array.isArray(pages)
|
|
4
|
+
? pages.flatMap((page) => (Array.isArray(page) ? page.flatMap(annotationMessage) : []))
|
|
5
|
+
: [];
|
|
6
|
+
}
|
|
7
|
+
function annotationMessage(annotation) {
|
|
8
|
+
const message = annotation?.message;
|
|
9
|
+
return typeof message === 'string' ? [message] : [];
|
|
10
|
+
}
|
|
11
|
+
/** Lazily fetches and caches check-run annotation messages for the failed jobs. */
|
|
12
|
+
export function createAnnotationFetchers(env, failedJobs) {
|
|
13
|
+
const cache = new Map();
|
|
14
|
+
const failures = new Set();
|
|
15
|
+
const failedJobAnnotations = async (jobName) => {
|
|
16
|
+
const cached = cache.get(jobName);
|
|
17
|
+
if (cached !== undefined)
|
|
18
|
+
return cached;
|
|
19
|
+
const job = failedJobs.find((entry) => entry.name === jobName);
|
|
20
|
+
let messages = [];
|
|
21
|
+
if (job !== undefined) {
|
|
22
|
+
try {
|
|
23
|
+
messages = annotationMessagesFromPages(await env.api.call([
|
|
24
|
+
'-X',
|
|
25
|
+
'GET',
|
|
26
|
+
`repos/${env.repository}/check-runs/${job.id}/annotations`,
|
|
27
|
+
'-F',
|
|
28
|
+
'per_page=100',
|
|
29
|
+
'--paginate',
|
|
30
|
+
'--slurp',
|
|
31
|
+
]));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
failures.add(jobName);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
cache.set(jobName, messages);
|
|
38
|
+
return messages;
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
failedJobAnnotations,
|
|
42
|
+
failedJobAnnotationFetchFailures: async () => new Set(failures),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type JobEntry, type RunFetchEnv, type WorkflowConclusion, type WorkflowRunContext } from './run-context-types.mts';
|
|
2
|
+
export declare function parseWorkflowJobEntries(stdout: string): JobEntry[];
|
|
3
|
+
/** Every job of the run, or of one prior attempt when `attempt` is given. */
|
|
4
|
+
export declare function fetchJobs(env: RunFetchEnv, attempt?: number): Promise<JobEntry[]>;
|
|
5
|
+
/** Job counts of attempts 1..runAttempt-1; `null` where the attempt could not be read. */
|
|
6
|
+
export declare function fetchPriorAttemptJobCounts(env: RunFetchEnv, runAttempt: number): Promise<Array<number | null>>;
|
|
7
|
+
export interface WorkflowRunSummary {
|
|
8
|
+
conclusion: string | null | undefined;
|
|
9
|
+
runAttempt: number;
|
|
10
|
+
workflowName: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function fetchWorkflowRunSummary(env: RunFetchEnv): Promise<WorkflowRunSummary>;
|
|
13
|
+
export interface JobsContextInput {
|
|
14
|
+
conclusion: WorkflowConclusion;
|
|
15
|
+
retryAttempt: number;
|
|
16
|
+
runAttempt: number;
|
|
17
|
+
workflowName: string;
|
|
18
|
+
}
|
|
19
|
+
/** Builds a context over one attempt's jobs. Logs and annotations stay unfetched until read. */
|
|
20
|
+
export declare function buildJobsContext(env: RunFetchEnv, jobs: readonly JobEntry[], input: JobsContextInput): WorkflowRunContext;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createAnnotationFetchers } from "./run-context-annotations.mjs";
|
|
2
|
+
import { createLogFetchers } from "./run-context-logs.mjs";
|
|
3
|
+
import { isFailedWorkflowConclusion, } from "./run-context-types.mjs";
|
|
4
|
+
export function parseWorkflowJobEntries(stdout) {
|
|
5
|
+
const pages = JSON.parse(stdout.trim());
|
|
6
|
+
return pages.flatMap((page) => page.jobs ?? []);
|
|
7
|
+
}
|
|
8
|
+
/** Every job of the run, or of one prior attempt when `attempt` is given. */
|
|
9
|
+
export async function fetchJobs(env, attempt) {
|
|
10
|
+
const base = `repos/${env.repository}/actions/runs/${env.runId}`;
|
|
11
|
+
const path = attempt === undefined ? `${base}/jobs` : `${base}/attempts/${attempt}/jobs`;
|
|
12
|
+
return parseWorkflowJobEntries(await env.api.call([path, '--paginate', '--slurp']));
|
|
13
|
+
}
|
|
14
|
+
/** Job counts of attempts 1..runAttempt-1; `null` where the attempt could not be read. */
|
|
15
|
+
export async function fetchPriorAttemptJobCounts(env, runAttempt) {
|
|
16
|
+
return Promise.all(Array.from({ length: Math.max(0, runAttempt - 1) }, async (_, index) => {
|
|
17
|
+
try {
|
|
18
|
+
return (await fetchJobs(env, index + 1)).length;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
export async function fetchWorkflowRunSummary(env) {
|
|
26
|
+
const stdout = await env.api.call([`repos/${env.repository}/actions/runs/${env.runId}`]);
|
|
27
|
+
const run = JSON.parse(stdout.trim());
|
|
28
|
+
const invalid = (what) => new Error(`Invalid workflow run ${what} in GitHub API response for run ${env.runId}`);
|
|
29
|
+
if (typeof run !== 'object' || run === null)
|
|
30
|
+
throw invalid('response');
|
|
31
|
+
const { conclusion, name, run_attempt: runAttempt } = run;
|
|
32
|
+
if (typeof runAttempt !== 'number' || !Number.isInteger(runAttempt) || runAttempt < 1) {
|
|
33
|
+
throw invalid('attempt');
|
|
34
|
+
}
|
|
35
|
+
if (typeof name !== 'string' || name.length === 0)
|
|
36
|
+
throw invalid('name');
|
|
37
|
+
if (conclusion != null && typeof conclusion !== 'string')
|
|
38
|
+
throw invalid('conclusion');
|
|
39
|
+
return { conclusion, runAttempt, workflowName: name };
|
|
40
|
+
}
|
|
41
|
+
/** Builds a context over one attempt's jobs. Logs and annotations stay unfetched until read. */
|
|
42
|
+
export function buildJobsContext(env, jobs, input) {
|
|
43
|
+
const failedJobs = jobs.filter((job) => isFailedWorkflowConclusion(job.conclusion));
|
|
44
|
+
return {
|
|
45
|
+
...input,
|
|
46
|
+
jobNames: jobs.map((job) => job.name),
|
|
47
|
+
targetNames: new Set(jobs.map((job) => job.name)),
|
|
48
|
+
jobConclusions: new Map(jobs.map((job) => [job.name, job.conclusion])),
|
|
49
|
+
jobSteps: new Map(jobs.map((job) => [job.name, job.steps ?? []])),
|
|
50
|
+
failedJobNames: failedJobs.map((job) => job.name),
|
|
51
|
+
...createLogFetchers(env, jobs, failedJobs),
|
|
52
|
+
...createAnnotationFetchers(env, failedJobs),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JobEntry, RunFetchEnv } from './run-context-types.mts';
|
|
2
|
+
/**
|
|
3
|
+
* Lazily fetches job logs through a head+tail window, one job at a time, and keeps no more than
|
|
4
|
+
* `limits.totalBytes` of text across the run. A job that cannot be read, or that arrives after the
|
|
5
|
+
* budget is spent, is recorded as a fetch failure with an empty log so rules never see a partial
|
|
6
|
+
* log presented as complete.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createLogFetchers(env: RunFetchEnv, allJobs: readonly JobEntry[], failedJobs: readonly JobEntry[]): {
|
|
9
|
+
failedJobLogs: () => Promise<Map<string, string>>;
|
|
10
|
+
jobLogs: (jobNames: readonly string[]) => Promise<Map<string, string>>;
|
|
11
|
+
failedJobLogFetchFailures: () => Promise<Set<string>>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { collectDiagnosticWindow, fitDiagnosticWindow } from "./log-window.mjs";
|
|
2
|
+
const STREAM_ATTEMPTS = 3;
|
|
3
|
+
/**
|
|
4
|
+
* Lazily fetches job logs through a head+tail window, one job at a time, and keeps no more than
|
|
5
|
+
* `limits.totalBytes` of text across the run. A job that cannot be read, or that arrives after the
|
|
6
|
+
* budget is spent, is recorded as a fetch failure with an empty log so rules never see a partial
|
|
7
|
+
* log presented as complete.
|
|
8
|
+
*/
|
|
9
|
+
export function createLogFetchers(env, allJobs, failedJobs) {
|
|
10
|
+
// Fetch state is keyed by job id: distinct jobs may share a display name.
|
|
11
|
+
const cached = new Map();
|
|
12
|
+
const inFlight = new Map();
|
|
13
|
+
const jobsByName = new Map();
|
|
14
|
+
for (const job of allJobs)
|
|
15
|
+
if (!jobsByName.has(job.name))
|
|
16
|
+
jobsByName.set(job.name, job);
|
|
17
|
+
const failures = new Set();
|
|
18
|
+
let retainedBytes = 0;
|
|
19
|
+
let queueTail = Promise.resolve();
|
|
20
|
+
const readWindow = async (path, maxBytes) => {
|
|
21
|
+
for (let attempt = 1;; attempt += 1) {
|
|
22
|
+
try {
|
|
23
|
+
// A fresh collector per attempt drops partial text from a failed stream.
|
|
24
|
+
const log = await collectDiagnosticWindow(env.streamLog(path), env.limits);
|
|
25
|
+
return fitDiagnosticWindow(log, maxBytes);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (attempt >= STREAM_ATTEMPTS || !env.api.isRetryable(error))
|
|
29
|
+
throw error;
|
|
30
|
+
await env.api.sleep(attempt * 1000);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const fetchWithinBudget = async (job) => {
|
|
35
|
+
let log = '';
|
|
36
|
+
const remaining = env.limits.totalBytes - retainedBytes;
|
|
37
|
+
try {
|
|
38
|
+
if (remaining <= 0)
|
|
39
|
+
throw new Error('log budget exhausted');
|
|
40
|
+
log = await readWindow(`repos/${env.repository}/actions/jobs/${job.id}/logs`, remaining);
|
|
41
|
+
retainedBytes += Buffer.byteLength(log);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
failures.add(job.name);
|
|
45
|
+
}
|
|
46
|
+
cached.set(job.id, log);
|
|
47
|
+
return log;
|
|
48
|
+
};
|
|
49
|
+
const fetchLog = (job) => {
|
|
50
|
+
const hit = cached.get(job.id);
|
|
51
|
+
if (hit !== undefined)
|
|
52
|
+
return Promise.resolve(hit);
|
|
53
|
+
const pending = inFlight.get(job.id);
|
|
54
|
+
if (pending)
|
|
55
|
+
return pending;
|
|
56
|
+
// Sequential on purpose: many failed jobs must not hold many windows at once.
|
|
57
|
+
const settled = queueTail
|
|
58
|
+
.then(() => fetchWithinBudget(job))
|
|
59
|
+
.finally(() => inFlight.delete(job.id));
|
|
60
|
+
inFlight.set(job.id, settled);
|
|
61
|
+
// `fetchWithinBudget` records failures instead of throwing, so the queue never rejects.
|
|
62
|
+
queueTail = settled.then(() => undefined);
|
|
63
|
+
return settled;
|
|
64
|
+
};
|
|
65
|
+
let failedLogs;
|
|
66
|
+
return {
|
|
67
|
+
failedJobLogs: () => (failedLogs ??= (async () => {
|
|
68
|
+
const logs = new Map();
|
|
69
|
+
for (const job of failedJobs) {
|
|
70
|
+
const log = await fetchLog(job);
|
|
71
|
+
// Failed jobs that share a name are joined so no job's evidence is dropped.
|
|
72
|
+
logs.set(job.name, logs.has(job.name) ? `${logs.get(job.name)}\n${log}` : log);
|
|
73
|
+
}
|
|
74
|
+
return logs;
|
|
75
|
+
})()),
|
|
76
|
+
jobLogs: async (jobNames) => {
|
|
77
|
+
const logs = new Map();
|
|
78
|
+
for (const name of jobNames) {
|
|
79
|
+
const job = jobsByName.get(name);
|
|
80
|
+
logs.set(name, job === undefined ? '' : await fetchLog(job));
|
|
81
|
+
}
|
|
82
|
+
return logs;
|
|
83
|
+
},
|
|
84
|
+
failedJobLogFetchFailures: async () => new Set(failures),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RunFetchEnv, WorkflowRetryRule } from './run-context-types.mts';
|
|
2
|
+
export interface DeriveRuleAttemptsOptions {
|
|
3
|
+
priorCounts: readonly number[];
|
|
4
|
+
rules: readonly WorkflowRetryRule[];
|
|
5
|
+
runAttempt: number;
|
|
6
|
+
workflowName: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Replays the rules over each prior attempt in order and counts, per rerun rule, how many earlier
|
|
10
|
+
* attempts that rule won. The result is each rule's next-occurrence count. A rule whose replay
|
|
11
|
+
* evidence cannot be fetched, and every rule after it, drops out of the map so its shared attempt
|
|
12
|
+
* applies; if the very first rule is unreadable the whole replay throws.
|
|
13
|
+
*/
|
|
14
|
+
export declare function deriveRuleAttempts(env: RunFetchEnv, { priorCounts, rules, runAttempt, workflowName }: DeriveRuleAttemptsOptions): Promise<Map<string, number>>;
|