cb0dbcd371
* feat(action): make prepush hook non-blocking after one failure push_branch now treats the repository's prepush hook as best-effort: it runs at most once per run, surfaces the failure output if the script exits non-zero, and every subsequent push_branch call this run skips the hook so the agent isn't blocked by failures unrelated to its change. The agent can iterate by running the hook command itself via the shell tool when shell access is available; push_branch will not re-run the hook automatically after a failure. Why: a one-line OSS-allowlist change took 9 minutes (#776) because the agent retried push_branch six times against a prepush hook that was failing for env-leak and missing-build-artifact reasons unrelated to the change. CI catches the same checks on the GitHub side; the local prepush gate was duplicating work and blocking unrelated fixes. - ToolState: new prepushFailureCount counter (per-run, never resets) - executeLifecycleHook: returns structured failure (kind/output/exitCode) so prepush can compose its own agent-facing message instead of inheriting the generic retry/no-retry advice meant for setup - push_branch: composes a shell-mode-aware error message; surfaces prepushSkipped on the success payload + appends a note to the message - instructions.ts + wiki/prompt.md + docs/comparisons.mdx: updated to reflect best-effort semantics * fix(action): clarify prepush latch semantics + soften static guidance review fixes from PR #777: - toolState comment, instructions, success message, tool description: replace "runs at most once per run" / "first call only" wording with the actual semantic — successful prepush keeps running on later push_branch calls; only a hook FAILURE latches the bypass. - tool description: drop hardcoded "via the shell tool" guidance so the static description doesn't mislead in shell:disabled runs (the dynamic agent prompt in instructions.ts already does shell-conditional messaging). - LifecycleHookFailure.output JSDoc: match the implementation (stderr-preferred fallback to stdout, empty for timeout/spawn). * fix(action): shorten prepush-skip log to terse operator telemetry the previous log line tried to address the agent ("re-run the hook command yourself via shell"), but log.info writes to the action runtime's stdout — the agent never sees it. agent-facing skip guidance already lives in the error message from buildPrepushFailureMessage, the success message when bypassed, and the system prompt in instructions.ts. log line is now just operator telemetry. * refactor(action): drop slop from prepush soft-fail self-audit pass after the previous review-fix round. removed duplication between code-level comment and the five other places that already explain the same behavior, tightened verbose JSDoc, and collapsed redundant clauses in agent-facing strings. - LifecycleHookFailure → discriminated union. drops the optional exitCode/spawnError fields (and the empty-output sentinel for timeout/spawn) plus the corresponding ?? fallbacks in the helper. - PushBranchTool: 7-line code comment above the latch removed (toolState field comment + tool description + error message + success message + system prompt all already cover it). tool description third sentence dropped (restated the second). success message tightened to a parenthetical. - buildPrepushFailureMessage: 4-line JSDoc → 1 line. shared "if you think the failure could indicate a real bug in your code" prefix factored out across the shell-conditional branches. - ToolState.prepushFailureCount comment: 8 lines → 3. the "what" is in git.ts; comment now only documents the invariant (never decremented within a run). - instructions.ts prepush guidance: collapsed nested bullets + ternary into one paragraph; dropped the "so re-running via shell is the only way…" tail that restated "push_branch will NOT re-run it". * fix(action): hint prepush bypass on dirty tree after hook failure When push_branch blocks on a dirty working tree and the prepush latch is already set, tell the agent the hook will be skipped once the tree is clean. * fix(test): narrow CI matrix for lifecycle and toolState changes Remove lifecycle.ts from ALWAYS_RUN_ALL and add lifecycle.ts + toolState.ts to push/git agnostic test coverage so PRs touching prepush latch logic run targeted tests instead of the full matrix.
102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
import { LIFECYCLE_HOOK_TIMEOUT_MS } from "../lifecycle.ts";
|
|
import { log } from "./cli.ts";
|
|
import {
|
|
SPAWN_ACTIVITY_TIMEOUT_CODE,
|
|
SPAWN_TIMEOUT_CODE,
|
|
SpawnTimeoutError,
|
|
spawn,
|
|
} from "./subprocess.ts";
|
|
|
|
export interface ExecuteLifecycleHookParams {
|
|
event: string;
|
|
script: string | null;
|
|
}
|
|
|
|
/** structured failure info — `output` on the `exit` variant is trimmed
|
|
* stderr, falling back to stdout when stderr is empty. */
|
|
export type LifecycleHookFailure =
|
|
| { kind: "exit"; exitCode: number; output: string }
|
|
| { kind: "timeout" }
|
|
| { kind: "spawn"; spawnError: string };
|
|
|
|
export interface LifecycleHookResult {
|
|
/**
|
|
* human-readable warning when the hook failed. includes retry guidance:
|
|
* transient spawn/exit errors are worth retrying, timeouts and
|
|
* persistent failures are not. absent when the hook succeeded or was
|
|
* skipped. setup/post-checkout callers surface this verbatim; prepush
|
|
* builds its own message from `failure` instead.
|
|
*/
|
|
warning?: string;
|
|
/**
|
|
* structured failure info — undefined when the hook succeeded or was
|
|
* skipped. lets callers compose their own messaging without parsing the
|
|
* `warning` string.
|
|
*/
|
|
failure?: LifecycleHookFailure;
|
|
}
|
|
|
|
/**
|
|
* execute a lifecycle hook script if one is configured.
|
|
*
|
|
* soft-fails: instead of throwing on hook errors, returns a warning string
|
|
* (and structured failure info) so callers can choose whether to surface
|
|
* it (mcp tools) or upgrade it to a fatal error (setup). timeouts are
|
|
* flagged as non-retryable in the warning text.
|
|
*/
|
|
export async function executeLifecycleHook(
|
|
params: ExecuteLifecycleHookParams
|
|
): Promise<LifecycleHookResult> {
|
|
if (!params.script) return {};
|
|
|
|
log.info(`» executing ${params.event} lifecycle hook...`);
|
|
|
|
try {
|
|
const result = await spawn({
|
|
cmd: "bash",
|
|
args: ["-c", params.script],
|
|
env: process.env,
|
|
timeout: LIFECYCLE_HOOK_TIMEOUT_MS,
|
|
activityTimeout: 0,
|
|
onStdout: (chunk) => process.stdout.write(chunk),
|
|
onStderr: (chunk) => process.stderr.write(chunk),
|
|
});
|
|
|
|
if (result.exitCode !== 0) {
|
|
const output = (result.stderr || result.stdout).trim();
|
|
return {
|
|
failure: { kind: "exit", output, exitCode: result.exitCode },
|
|
warning:
|
|
`lifecycle hook '${params.event}' failed with exit code ${result.exitCode}. ` +
|
|
`output: ${output || "(empty)"}. ` +
|
|
`retry the operation if the failure looks flaky (network blips, transient rate limits). ` +
|
|
`do NOT retry if the script is broken (missing commands, syntax errors) or the error is persistent.`,
|
|
};
|
|
}
|
|
|
|
log.info(`» ${params.event} lifecycle hook completed successfully`);
|
|
return {};
|
|
} catch (err) {
|
|
const isTimeout =
|
|
err instanceof SpawnTimeoutError &&
|
|
(err.code === SPAWN_TIMEOUT_CODE || err.code === SPAWN_ACTIVITY_TIMEOUT_CODE);
|
|
if (isTimeout) {
|
|
const minutes = Math.round(LIFECYCLE_HOOK_TIMEOUT_MS / 60000);
|
|
return {
|
|
failure: { kind: "timeout" },
|
|
warning:
|
|
`lifecycle hook '${params.event}' timed out after ${minutes}min. ` +
|
|
`do NOT retry — the script is likely hung or doing too much work. ` +
|
|
`ask the repo owner to simplify the hook (e.g. move long-running work out of the hook, add caching, or split it).`,
|
|
};
|
|
}
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
return {
|
|
failure: { kind: "spawn", spawnError: msg },
|
|
warning:
|
|
`lifecycle hook '${params.event}' failed to spawn: ${msg}. ` +
|
|
`this is likely a transient failure — retry the operation.`,
|
|
};
|
|
}
|
|
}
|