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
86 changes: 86 additions & 0 deletions templates/scripts/agent-branch-finish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ OPENSPEC_TASKS_RECONCILER="${GUARDEX_FINISH_OPENSPEC_TASKS_RECONCILER:-${FINISH_
PREFLIGHT_ENABLED_RAW="${GUARDEX_FINISH_PREFLIGHT:-true}"
PREFLIGHT_SCRIPT_RAW="${GUARDEX_FINISH_PREFLIGHT_SCRIPT:-scripts/agent-preflight.sh}"
PREFLIGHT_REQUIRED_RAW="${GUARDEX_FINISH_REQUIRE_PREFLIGHT:-false}"
PREFLIGHT_CACHE_ENABLED_RAW="${GUARDEX_FINISH_PREFLIGHT_CACHE:-true}"
AUTO_PROMOTE_DRAFT_RAW="${GUARDEX_FINISH_AUTO_PROMOTE:-true}"
FINISH_CHECKLIST_RAW="${GUARDEX_FINISH_CHECKLIST:-false}"
FINISH_GATE_DONE_RAW="${GUARDEX_FINISH_GATE_DONE:-false}"
Expand Down Expand Up @@ -188,6 +189,71 @@ resolve_preflight_script() {
fi
}

# Bind a reusable success receipt to every input that can change the result.
# The receipt is per source branch and is overwritten when HEAD, the base ref,
# or the configured preflight script changes. Mandatory billing-waiver
# preflights deliberately bypass this cache and always execute fail-closed.
preflight_cache_fingerprint() {
local worktree="$1"
local script_path="$2"
local source_sha=""
local base_sha=""
local script_sha=""

source_sha="$(git -C "$worktree" rev-parse 'HEAD^{commit}' 2>/dev/null || true)"
base_sha="$(git -C "$worktree" rev-parse "${start_ref}^{commit}" 2>/dev/null || true)"
script_sha="$(git -C "$worktree" hash-object --no-filters "$script_path" 2>/dev/null || true)"
if [[ -z "$source_sha" || -z "$base_sha" || -z "$script_sha" ]]; then
return 0
fi

printf 'guardex-preflight-cache-v1\n%s\n%s\n%s\n%s\n' \
"$source_sha" "$base_sha" "$PREFLIGHT_SCRIPT_RAW" "$script_sha" \
| git -C "$worktree" hash-object --stdin 2>/dev/null || true
}

preflight_cache_receipt_path() {
local worktree="$1"
local common_git_dir=""
local branch_name=""
local branch_key=""

common_git_dir="$(git -C "$worktree" rev-parse --git-common-dir 2>/dev/null || true)"
if [[ -z "$common_git_dir" ]]; then
return 0
fi
if [[ "$common_git_dir" != /* ]]; then
common_git_dir="${worktree}/${common_git_dir}"
fi
branch_name="${SOURCE_BRANCH:-$(git -C "$worktree" symbolic-ref --quiet --short HEAD 2>/dev/null || true)}"
if [[ -z "$branch_name" ]]; then
return 0
fi
branch_key="$(printf '%s' "$branch_name" | git -C "$worktree" hash-object --stdin 2>/dev/null || true)"
if [[ -z "$branch_key" ]]; then
return 0
fi

printf '%s/guardex/preflight-cache/%s.receipt' "$common_git_dir" "$branch_key"
}

write_preflight_cache_receipt() {
local receipt_path="$1"
local fingerprint="$2"
local receipt_dir=""
local temporary_receipt=""

[[ -n "$receipt_path" && -n "$fingerprint" ]] || return 0
receipt_dir="$(dirname "$receipt_path")"
mkdir -p "$receipt_dir" 2>/dev/null || return 0
temporary_receipt="${receipt_path}.$$"
if printf '%s\n' "$fingerprint" > "$temporary_receipt" 2>/dev/null; then
mv -f "$temporary_receipt" "$receipt_path" 2>/dev/null || rm -f "$temporary_receipt"
else
rm -f "$temporary_receipt"
fi
}

# Run the pre-flight verification gate in the agent worktree before
# any push happens. Returns 0 on success or when no gate is
# configured; returns non-zero (and prints a hint) on failure, which
Expand Down Expand Up @@ -239,6 +305,24 @@ run_preflight() {
return 1
fi
fi
local cache_fingerprint=""
local cache_receipt_path=""
local cached_fingerprint=""
if [[ "${PREFLIGHT_CACHE_ENABLED:-1}" -eq 1 && "$PREFLIGHT_REQUIRED" -ne 1 ]]; then
cache_fingerprint="$(preflight_cache_fingerprint "$worktree" "$script_path")"
cache_receipt_path="$(preflight_cache_receipt_path "$worktree")"
if [[ -n "$cache_fingerprint" && -n "$cache_receipt_path" && -f "$cache_receipt_path" ]]; then
cached_fingerprint="$(head -n 1 "$cache_receipt_path" 2>/dev/null || true)"
if [[ "$cached_fingerprint" == "$cache_fingerprint" ]]; then
if [[ -n "$trusted_tree" ]]; then
rm -rf -- "$trusted_tree"
fi
finish_progress complete preflight "cached pass"
echo "[agent-branch-finish] Pre-flight unchanged; reusing cached successful result." >&2
return 0
fi
fi
fi
local preflight_status=0
if [[ -n "$trusted_tree" ]]; then
( cd "$preflight_cwd" && "$script_path" --guardex-target-worktree "$worktree" ) || preflight_status=$?
Expand All @@ -249,6 +333,7 @@ run_preflight() {
rm -rf -- "$trusted_tree"
fi
if [[ "$preflight_status" -eq 0 ]]; then
write_preflight_cache_receipt "$cache_receipt_path" "$cache_fingerprint"
finish_progress complete preflight "passed"
echo "[agent-branch-finish] Pre-flight passed." >&2
return 0
Expand Down Expand Up @@ -475,6 +560,7 @@ done
# silently ignored the flags, leaving --no-preflight inert. Flags that set the
# normalized var directly in-loop (--cleanup, --wait-for-merge, ...) are unaffected.
PREFLIGHT_ENABLED="$(normalize_bool "$PREFLIGHT_ENABLED_RAW" "1")"
PREFLIGHT_CACHE_ENABLED="$(normalize_bool "$PREFLIGHT_CACHE_ENABLED_RAW" "1")"
AUTO_PROMOTE_DRAFT="$(normalize_bool "$AUTO_PROMOTE_DRAFT_RAW" "1")"

if [[ "$CLEANUP_AFTER_MERGE" -eq 1 && "$DELETE_REMOTE_BRANCH_EXPLICIT" -eq 0 ]]; then
Expand Down
110 changes: 110 additions & 0 deletions test/finish-preflight-flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ function extractPreflightFunctions() {
return script.slice(functionStart, functionEnd);
}

function commitAll(repoDir, message) {
cp.execFileSync('git', ['add', '.'], { cwd: repoDir });
cp.execFileSync('git', ['commit', '-m', message], {
cwd: repoDir,
env: {
...process.env,
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
GUARDEX_ALLOW_CODEX_ON_NON_AGENT: '1',
},
});
}

test('--no-preflight is honored: PREFLIGHT_ENABLED normalized after the parse loop', () => {
assertNormalizedAfterFlag(
'PREFLIGHT_ENABLED_RAW="false"',
Expand All @@ -60,6 +72,104 @@ test('--no-preflight is honored: PREFLIGHT_ENABLED normalized after the parse lo
);
});

test('successful preflight is reused only while source and base commits are unchanged', () => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'guardex-preflight-cache-'));
const counterPath = path.join(os.tmpdir(), `guardex-preflight-counter-${process.pid}-${Date.now()}`);
const preflightPath = path.join(repoDir, 'scripts', 'agent-preflight.sh');

try {
cp.execFileSync('git', ['init', '-b', 'main'], { cwd: repoDir });
cp.execFileSync('git', ['config', 'user.name', 'Guardex Test'], { cwd: repoDir });
cp.execFileSync('git', ['config', 'user.email', 'guardex@example.test'], { cwd: repoDir });
fs.mkdirSync(path.dirname(preflightPath), { recursive: true });
fs.writeFileSync(
preflightPath,
'#!/usr/bin/env bash\nif [[ "${1:-}" == "--guardex-capabilities" ]]; then printf "%s\\n" guardex-target-worktree-v1; exit 0; fi\ncount=0\n[[ -f "$PREFLIGHT_COUNTER" ]] && count="$(cat "$PREFLIGHT_COUNTER")"\nprintf "%s\\n" "$((count + 1))" > "$PREFLIGHT_COUNTER"\n',
{ mode: 0o755 },
);
fs.writeFileSync(path.join(repoDir, 'base.txt'), 'base\n');
commitAll(repoDir, 'base');
cp.execFileSync('git', ['switch', '-c', 'agent/cache-test'], {
cwd: repoDir,
env: { ...process.env, GUARDEX_ALLOW_PRIMARY_BRANCH_SWITCH: '1' },
});
fs.writeFileSync(path.join(repoDir, 'change.txt'), 'one\n');
commitAll(repoDir, 'agent change');

const command = [
extractPreflightFunctions(),
'finish_progress() { :; }',
'PREFLIGHT_ENABLED=1',
'PREFLIGHT_REQUIRED=0',
'PREFLIGHT_CACHE_ENABLED=1',
'PREFLIGHT_SCRIPT_RAW=scripts/agent-preflight.sh',
'SOURCE_BRANCH=agent/cache-test',
'start_ref=main',
'run_preflight "$1"',
'run_preflight "$1"',
].join('\n');
const first = cp.spawnSync('bash', ['-c', command, 'preflight-cache-test', repoDir], {
encoding: 'utf8',
env: { ...process.env, PREFLIGHT_COUNTER: counterPath },
});

assert.equal(first.status, 0, first.stderr);
assert.equal(fs.readFileSync(counterPath, 'utf8'), '1\n');
assert.match(first.stderr, /cached successful result/i);

fs.writeFileSync(path.join(repoDir, 'change.txt'), 'two\n');
commitAll(repoDir, 'agent change two');
const afterSourceChange = cp.spawnSync(
'bash',
['-c', command.replace('run_preflight "$1"\nrun_preflight "$1"', 'run_preflight "$1"'), 'preflight-cache-test', repoDir],
{
encoding: 'utf8',
env: { ...process.env, PREFLIGHT_COUNTER: counterPath },
},
);

assert.equal(afterSourceChange.status, 0, afterSourceChange.stderr);
assert.equal(fs.readFileSync(counterPath, 'utf8'), '2\n');

const mainParent = cp.execFileSync('git', ['rev-parse', 'main'], { cwd: repoDir, encoding: 'utf8' }).trim();
const mainTree = cp.execFileSync('git', ['rev-parse', 'main^{tree}'], { cwd: repoDir, encoding: 'utf8' }).trim();
const advancedMain = cp.execFileSync(
'git',
['commit-tree', mainTree, '-p', mainParent, '-m', 'advance base'],
{ cwd: repoDir, encoding: 'utf8' },
).trim();
cp.execFileSync('git', ['update-ref', 'refs/heads/main', advancedMain], { cwd: repoDir });
const afterBaseChange = cp.spawnSync(
'bash',
['-c', command.replace('run_preflight "$1"\nrun_preflight "$1"', 'run_preflight "$1"'), 'preflight-cache-test', repoDir],
{
encoding: 'utf8',
env: { ...process.env, PREFLIGHT_COUNTER: counterPath },
},
);

assert.equal(afterBaseChange.status, 0, afterBaseChange.stderr);
assert.equal(fs.readFileSync(counterPath, 'utf8'), '3\n');

const mandatoryCommand = command.replace('PREFLIGHT_REQUIRED=0', 'PREFLIGHT_REQUIRED=1');
const mandatory = cp.spawnSync(
'bash',
['-c', mandatoryCommand, 'mandatory-preflight-cache-test', repoDir],
{
encoding: 'utf8',
env: { ...process.env, PREFLIGHT_COUNTER: counterPath },
},
);

assert.equal(mandatory.status, 0, mandatory.stderr);
assert.equal(fs.readFileSync(counterPath, 'utf8'), '5\n');
assert.doesNotMatch(mandatory.stderr, /cached successful result/i);
} finally {
fs.rmSync(repoDir, { recursive: true, force: true });
fs.rmSync(counterPath, { force: true });
}
});

test('a billing waiver makes local preflight mandatory and fail-closed', () => {
assert.ok(
script.includes('GUARDEX_FINISH_REQUIRE_PREFLIGHT'),
Expand Down