From 2f58fd2e86cd01432e9142f70cc8eb08b7c795f0 Mon Sep 17 00:00:00 2001 From: pelikhan <4175913+pelikhan@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:35:48 +0000 Subject: [PATCH 1/3] Trace pull-request safe-output execution paths in unified sessions Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- actions/setup/js/create_pull_request.cjs | 68 ++++++- actions/setup/js/create_pull_request.test.cjs | 10 + .../setup/js/push_to_pull_request_branch.cjs | 67 ++++++- .../js/push_to_pull_request_branch.test.cjs | 19 +- .../setup/js/safe_output_handler_manager.cjs | 5 +- actions/setup/js/safe_output_outcomes.cjs | 2 + .../setup/js/safe_output_outcomes.test.cjs | 59 ++++++ actions/setup/js/safe_output_trace.cjs | 47 +++++ actions/setup/js/safe_output_trace.test.cjs | 172 ++++++++++++++++++ actions/setup/js/types/unified_session.d.ts | 7 + .../schemas/unified-session.schema.json | 4 +- .../unified-agent-session-specification.md | 12 ++ 12 files changed, 463 insertions(+), 9 deletions(-) create mode 100644 actions/setup/js/safe_output_trace.cjs create mode 100644 actions/setup/js/safe_output_trace.test.cjs diff --git a/actions/setup/js/create_pull_request.cjs b/actions/setup/js/create_pull_request.cjs index 635f8b39594..5b4ab8966e1 100644 --- a/actions/setup/js/create_pull_request.cjs +++ b/actions/setup/js/create_pull_request.cjs @@ -53,6 +53,7 @@ const { parseDiffGitHeader: parseDiffGitHeaderPaths, extractDiffGitHeaderEntries const { resolveTransportPaths } = require("./resolve_transport_paths.cjs"); const { recoverObjectPack, isObjectPack, recoveryToken } = require("./object_pack_transport.cjs"); const { withTransportIntegrity } = require("./transport_integrity.cjs"); +const { withExecutionTrace } = require("./safe_output_trace.cjs"); const { resolveDefaultMentions, resolveMentionsForItem } = require("./resolve_mentions_from_payload.cjs"); const { MANAGED_FALLBACK_ISSUE_LABEL, @@ -973,10 +974,31 @@ async function main(config = {}) { * @param {Object} resolvedTemporaryIds - Map of temporary IDs to {repo, number} * @returns {Promise} Result with success/error status and PR details */ - return withTransportIntegrity(async function handleCreatePullRequest(message, resolvedTemporaryIds, transport) { + // Trace map: A invocation; B count limit; C invalid temporary ID; D invalid repo; + // E head repo; F base override; G object pack; H missing patch; I allow empty; + // J patch limits; K patch generation failed; L oversized patch; M protection deny; + // N protection fallback; O protection review; P empty patch; Q staged; R bundle; + // S bundle failure; T signed merge retry; U unrecovered bundle push; V bundle issue; + // W patch; X patch failure; Y add/add recovered; Z base replay; a apply failed; + // b/c/d post-apply deny/fallback/review; e patch push failure; f push issue; + // g empty commit; h empty push failure; i protected issue; j reused issue; + // k new protected issue; l protected issue failure; m PR publication; + // n PR publication failure; o permission issue; p permission issue failure; + // q PR fallback disabled; r PR fallback issue; s PR fallback failure; + // t bundle push attempt; u PR delivery capture; v/w no-changes error/ignore; + // x successful return; y unsuccessful/skipped return; z thrown error. + return withExecutionTrace( + { success: "x", failure: "y", error: "z" }, + withTransportIntegrity + )(async function handleCreatePullRequest(message, resolvedTemporaryIds, transport, trace) { + trace("A"); + if (ifNoChanges === "error") trace("v"); + if (ifNoChanges === "ignore") trace("w"); + if (isStaged) trace("Q"); const exec = transport.guardExec(globalThis.exec); // Check if we've hit the max limit if (processedCount >= maxCount) { + trace("B"); core.warning(`Skipping create_pull_request: max count of ${maxCount} reached`); return { success: false, @@ -990,6 +1012,7 @@ async function main(config = {}) { const tempIdResult = getOrGenerateTemporaryId(pullRequestItem, "pull request"); if (tempIdResult.error) { + trace("C"); core.warning(`Skipping create_pull_request: ${tempIdResult.error}`); return { success: false, error: tempIdResult.error }; } @@ -999,6 +1022,7 @@ async function main(config = {}) { const repoResult = resolveAndValidateRepo(pullRequestItem, defaultTargetRepo, allowedRepos, "pull request"); if (!repoResult.success) { + trace("D"); core.warning(`Skipping pull request: ${repoResult.error}`); return { success: false, error: repoResult.error }; } @@ -1026,6 +1050,7 @@ async function main(config = {}) { let pushRepoParts = repoParts; let pushGithubClient = githubClient; if (configuredHeadRepo) { + trace("E"); const headRepoResult = resolveAndValidateRepo({ repo: configuredHeadRepo }, itemRepo, allowedRepos, "pull request head repository"); if (!headRepoResult.success) { return { success: false, error: headRepoResult.error }; @@ -1059,6 +1084,7 @@ async function main(config = {}) { // Overriding to a different branch requires allowed_base_branches to be configured, unless the // base branch belongs to a pull request created earlier in this same run (a stacked pull request). if (typeof pullRequestItem.base === "string" && pullRequestItem.base.trim() !== "") { + trace("F"); const requestedBaseBranchRaw = pullRequestItem.base.trim(); const requestedBaseBranchForLog = JSON.stringify(requestedBaseBranchRaw); core.info(`Base branch override requested: ${requestedBaseBranchForLog}`); @@ -1225,6 +1251,7 @@ async function main(config = {}) { } if (bundleFilePath && isObjectPack(transport.read(bundleFilePath))) { + trace("G"); const recovered = await recoverObjectPack(transport, transportPaths, { repo: itemRepo, baseBranch, @@ -1252,8 +1279,10 @@ async function main(config = {}) { } const hasPatchFile = !!(patchFilePath && fs.existsSync(patchFilePath)); if (!hasPatchFile) { + trace("H"); // If allow-empty is enabled, we can proceed without a patch file if (allowEmpty) { + trace("I"); core.info("No patch file found, but allow-empty is enabled - will create empty PR"); } else { const message = "No patch file found - cannot create pull request without changes"; @@ -1304,6 +1333,7 @@ async function main(config = {}) { try { enforcePullRequestLimits(patchContent, maxFiles); } catch (error) { + trace("J"); const errorMessage = getErrorMessage(error); core.warning(`Pull request limit exceeded: ${errorMessage}`); @@ -1355,6 +1385,7 @@ async function main(config = {}) { // Check for actual error conditions (but allow empty patches as valid noop) if (patchContent.includes("Failed to generate patch")) { + trace("K"); // If allow-empty is enabled, ignore patch errors and proceed if (allowEmpty) { core.info("Patch file contains error, but allow-empty is enabled - will create empty PR"); @@ -1401,6 +1432,7 @@ async function main(config = {}) { core.info(`Patch size: ${patchSizeKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (patchSizeKb > maxSizeKb) { + trace("L"); const message = `Patch size (${patchSizeKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)`; // If in staged mode, still show preview with error @@ -1435,6 +1467,7 @@ async function main(config = {}) { protected_files_policy: config.protected_files_policy ?? "request_review", }); if (protection.action === "deny") { + trace("M"); const filesStr = protection.files.join(", "); const message = protection.source === "allowlist" @@ -1444,16 +1477,19 @@ async function main(config = {}) { return { success: false, error: message }; } if (protection.action === "fallback") { + trace("N"); manifestProtectionFallback = protection.files; core.warning(`Protected file protection triggered (fallback-to-issue): ${protection.files.join(", ")}. Will create review issue instead of pull request.`); } if (protection.action === "request_review") { + trace("O"); manifestProtectionRequestReview = protection.files; core.warning(`Protected file protection triggered (request_review): ${protection.files.join(", ")}. Will create pull request with caution and request-changes review.`); } } if (isEmpty && !isStaged && !allowEmpty) { + trace("P"); const message = "Patch file is empty - no changes to apply (noop operation)"; switch (ifNoChanges) { @@ -1870,6 +1906,7 @@ async function main(config = {}) { // token on multi-commit branches where workflow files may have been modified). let newCommitCount = 0; if (hasBundleFile) { + trace("R"); // Bundle transport: fetch commits directly from the bundle file. // This preserves merge commit topology and per-commit metadata (messages, authorship) // unlike git format-patch which flattens history and drops merge resolution content. @@ -1877,6 +1914,7 @@ async function main(config = {}) { try { await applyBundleToBranch(bundleFilePath, branchName, originalAgentBranch, exec, config.allow_full_history_fetch === true); } catch (bundleError) { + trace("S"); transport.assertUnchanged(); core.error(`Failed to apply bundle: ${getErrorMessage(bundleError)}`); return { success: false, error: `Failed to apply bundle: ${getErrorMessage(bundleError)}` }; @@ -1889,6 +1927,7 @@ async function main(config = {}) { { const forkCwd = process.cwd(); const runBundlePush = async () => { + trace("t"); transport.assertUnchanged(); branchName = await handleRemoteBranchCollision(branchName, preserveBranchName, { assertTransportUnchanged: transport.assertUnchanged, @@ -1939,6 +1978,7 @@ async function main(config = {}) { const isSignedMergeReplayRefusal = signedCommits && /pushSignedCommits: refusing unsigned push/.test(pushErrorMessage) && /merge commit/i.test(pushErrorMessage); if (isSignedMergeReplayRefusal) { + trace("T"); core.warning("Signed push rejected merge commit topology from bundle; rewriting branch and retrying signed push"); try { await rewriteBundleBranchAsSingleCommit(baseBranch, exec, bundleFilePath, { @@ -1980,6 +2020,7 @@ async function main(config = {}) { } if (!pushRecovered) { + trace("U"); core.error(`Git push failed: ${getErrorMessage(pushError)}`); if (manifestProtectionFallback) { @@ -1993,6 +2034,7 @@ async function main(config = {}) { const error = `Failed to push changes: ${getErrorMessage(pushError)}`; return { success: false, error, error_type: "push_failed" }; } else { + trace("V"); core.warning("Git push operation failed - creating fallback issue instead of pull request"); const runUrl = buildWorkflowRunUrl(context, context.repo); @@ -2087,6 +2129,7 @@ async function main(config = {}) { // Apply the patch using git CLI (skip if empty) if (!isEmpty && patchFilePath) { + trace("W"); /** @type {any} */ let postApplyBaseRef = null; const capturePostApplyBaseRef = async () => { @@ -2131,11 +2174,13 @@ async function main(config = {}) { core.info("Patch applied successfully"); patchApplied = true; } catch (patchError) { + trace("X"); transport.assertUnchanged(); core.error(`Failed to apply patch with --3way: ${getErrorMessage(patchError)}`); const recoveredFromAddAddConflict = await tryRecoverGitAmAddAddConflict(exec); if (recoveredFromAddAddConflict.recovered) { + trace("Y"); patchApplied = true; } else { if (recoveredFromAddAddConflict.errorMessage) { @@ -2172,6 +2217,7 @@ async function main(config = {}) { // the patch was generated, making --3way unable to resolve the conflicts automatically. core.info("Attempting fallback: create PR branch at original base commit..."); try { + trace("Z"); // Use the base commit recorded at patch generation time. // The From header in format-patch output contains the agent's new commit SHA // which does not exist in this checkout, so we cannot derive the base from it. @@ -2238,6 +2284,7 @@ async function main(config = {}) { } if (!patchApplied) { + trace("a"); return { success: false, error: "Failed to apply patch" }; } } @@ -2260,16 +2307,19 @@ async function main(config = {}) { protected_files_policy: config.protected_files_policy ?? "request_review", }); if (postApplyProtection.action === "deny") { + trace("b"); const filesStr = postApplyProtection.files.join(", "); const msg = `SECURITY: Post-apply file-protection check failed. ` + `The patch applied files that were not detected by the pre-apply parser: ${filesStr}. ` + `This may indicate a patch-parser bypass attempt. Aborting.`; core.error(msg); return { success: false, error: msg }; } if (postApplyProtection.action === "fallback") { + trace("c"); manifestProtectionFallback = postApplyProtection.files; core.warning(`Post-apply: Protected file protection triggered (fallback-to-issue): ${postApplyProtection.files.join(", ")}`); } if (postApplyProtection.action === "request_review") { + trace("d"); manifestProtectionRequestReview = postApplyProtection.files; core.warning(`Post-apply: Protected file protection triggered (request_review): ${postApplyProtection.files.join(", ")}`); } @@ -2327,6 +2377,7 @@ async function main(config = {}) { core.info("Could not count new commits - extra empty commit will be skipped"); } } catch (pushError) { + trace("e"); transport.assertUnchanged(); // Push failed - create fallback issue instead of PR (if fallback is enabled) core.error(`Git push failed: ${getErrorMessage(pushError)}`); @@ -2353,6 +2404,7 @@ async function main(config = {}) { const runUrl = buildWorkflowRunUrl(context, context.repo); const runId = context.runId; + trace("f"); // Read patch content for preview let patchPreview = buildPatchPreviewBlocks(""); if (patchFilePath && fs.existsSync(patchFilePath)) { @@ -2447,6 +2499,7 @@ async function main(config = {}) { // For empty patches with allow-empty, we still need to push the branch if (allowEmpty) { + trace("g"); core.info("allow-empty is enabled - will create branch and push with empty commit"); // Push the branch with an empty commit to allow PR creation try { @@ -2499,6 +2552,7 @@ async function main(config = {}) { } } catch (pushError) { transport.assertUnchanged(); + trace("h"); const error = `Failed to push empty branch: ${getErrorMessage(pushError)}`; core.error(error); return { @@ -2536,6 +2590,7 @@ async function main(config = {}) { // - Push-failed case: push was rejected (e.g. missing `workflows` permission); provides // patch artifact download instructions instead of the compare URL. if (manifestProtectionFallback) { + trace("i"); const allFound = manifestProtectionFallback; const githubServer = process.env.GITHUB_SERVER_URL || "https://github.com"; // Use head branch (branchName) for links when push succeeded; fall back to baseBranch @@ -2617,6 +2672,7 @@ async function main(config = {}) { let issue; let issueRepoParts; if (existingIssue) { + trace("j"); issueRepoParts = existingIssueMatch.issueRepoParts; const existingIssueBody = appendReportExecutionProvenance(protectedFallbackFrame(withProposalMetadata(fallbackBody)), process.env.GH_AW_WORKFLOW_NAME || "Workflow", ""); const existingIssueLabels = Array.isArray(existingIssue.labels) ? existingIssue.labels.map(label => (typeof label === "string" ? label : label?.name)).filter(label => typeof label === "string") : []; @@ -2641,6 +2697,7 @@ async function main(config = {}) { issue = { ...existingIssue, ...response.data }; core.info(`Updated protected-file-protection review issue #${issue.number}: ${issue.html_url}`); } else { + trace("k"); const result = await createFallbackIssue(githubClient, repoParts, title, protectedFallbackFrame(withProposalMetadata(fallbackBody)), issueLabels, configAssignees, transport.assertUnchanged); issue = result.data; issueRepoParts = result.issueRepoParts; @@ -2687,6 +2744,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { + trace("l"); const error = `Protected file protection: failed to create review issue. Error: ${getErrorMessage(issueError)}`; core.error(error); return { success: false, error }; @@ -2695,6 +2753,7 @@ async function main(config = {}) { // Try to create the pull request, with fallback to issue creation try { + trace("m"); if (body.length > MAX_GITHUB_BODY_LENGTH) { throw new Error(`Pull request body exceeds GitHub's maximum length of ${MAX_GITHUB_BODY_LENGTH} characters`); } @@ -2941,6 +3000,7 @@ async function main(config = {}) { } // Return success with PR details + trace("u"); return await capturePullRequestDelivery( { success: true, @@ -2974,6 +3034,7 @@ async function main(config = {}) { githubClient ); } catch (prError) { + trace("n"); transport.assertUnchanged(); const errorMessage = getErrorMessage(prError); core.warning(`Failed to create pull request: ${errorMessage}`); @@ -3012,6 +3073,7 @@ async function main(config = {}) { }); try { + trace("o"); transport.assertUnchanged(); const { data: issue, issueRepoParts } = await createFallbackIssue(githubClient, repoParts, title, fallbackFrame(fallbackBody), mergeFallbackIssueLabels(effectiveFallbackLabels), configAssignees, transport.assertUnchanged); @@ -3030,6 +3092,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { + trace("p"); const error = `Failed to create pull request (permission denied) and failed to create fallback issue. PR error: ${errorMessage}. Issue error: ${getErrorMessage(issueError)}`; core.error(error); return { @@ -3042,6 +3105,7 @@ async function main(config = {}) { if (!fallbackAsIssue) { // Fallback is disabled - return error without creating issue + trace("q"); core.error("fallback-as-issue is disabled - not creating fallback issue"); return { success: false, @@ -3050,6 +3114,7 @@ async function main(config = {}) { }; } + trace("r"); core.info("Falling back to creating an issue instead"); // Create issue as fallback with enhanced body content @@ -3122,6 +3187,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { + trace("s"); const error = `Failed to create both pull request and fallback issue. PR error: ${errorMessage}. Issue error: ${getErrorMessage(issueError)}`; core.error(error); return { diff --git a/actions/setup/js/create_pull_request.test.cjs b/actions/setup/js/create_pull_request.test.cjs index b965021b8b4..33ce10155ce 100644 --- a/actions/setup/js/create_pull_request.test.cjs +++ b/actions/setup/js/create_pull_request.test.cjs @@ -160,6 +160,7 @@ describe("create_pull_request - draft policy enforcement", () => { expect(result.success).toBe(true); expect(result.id).toBe(555111222); + expect(result.execution_trace).toBe("AHIgmux"); expect(result.metadata).toEqual({ node_id: "PR_kwDOtest456" }); }); @@ -610,6 +611,7 @@ index 0000000..abc1234 expect(result.success).toBe(false); expect(result.error).toContain("allow-full-history-fetch: true"); + expect(result.execution_trace).toBe("ARSy"); expect(result.error).toContain("safe-outputs.create-pull-request"); const directFetch = global.exec.exec.mock.calls.find(([, args]) => Array.isArray(args) && args[0] === "fetch" && args.includes("origin") && args.includes(prereq)); expect(directFetch).toBeUndefined(); @@ -648,6 +650,7 @@ index 0000000..abc1234 expect(result.success).toBe(true); expect(pushSignedSpy).toHaveBeenCalledWith(expect.objectContaining({ signedCommits: false })); + expect(result.execution_trace).toBe("ARtmux"); }); it("should create the PR when the bundle push fails once with a transient workflows-scope timeout and succeeds on retry", async () => { @@ -685,6 +688,7 @@ index 0000000..abc1234 expect(result.success).toBe(true); expect(result.fallback_used).not.toBe(true); + expect(result.execution_trace).toBe("ARttmux"); expect(pushSignedSpy).toHaveBeenCalledTimes(2); expect(global.github.rest.pulls.create).toHaveBeenCalled(); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); @@ -796,6 +800,7 @@ index 0000000..abc1234 expect(result.fallback_used).not.toBe(true); expect(pushSignedSpy).toHaveBeenCalledTimes(2); expect(global.exec.exec).toHaveBeenCalledWith("git", ["reset", "--soft", "origin/main"]); + expect(result.execution_trace).toBe("ARtTmux"); expect(global.exec.exec).toHaveBeenCalledWith("git", ["commit", "-m", "bundle merge headline"]); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); }); @@ -2207,6 +2212,7 @@ ${diffs} expect(result.success).toBe(false); expect(result.error).toContain("outside the allowed-files list"); + expect(result.execution_trace).toBe("AMy"); expect(result.error).toContain("src/index.js"); }); @@ -2388,6 +2394,7 @@ ${diffs} const result = await handler({ title: "Test PR", body: "Test body", branch: "feature/protected-identical" }, {}); expect(result.issue_number).toBe(76); + expect(result.execution_trace).toBe("ANWijx"); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); expect(global.github.paginate).toHaveBeenCalledWith(global.github.rest.issues.listForRepo, expect.objectContaining({ owner: "test-owner", repo: "test-repo", state: "all", labels: "pull-request-fallback" })); const updateCall = global.github.rest.issues.update.mock.calls[0][0]; @@ -3560,6 +3567,7 @@ describe("create_pull_request - patch apply fallback to original base commit", ( expect(result.success).toBe(true); // Should warn that the PR will show merge conflicts + expect(result.execution_trace).toBe("AWXZmux"); expect(global.core.warning).toHaveBeenCalledWith(expect.stringContaining("merge conflicts")); }); @@ -5102,6 +5110,7 @@ describe("create_pull_request - E003 file-limit fallback-to-issue", () => { expect(global.github.rest.issues.create).toHaveBeenCalledTimes(1); expect(result.success).toBe(false); expect(result.error).toContain("SECURITY:"); + expect(result.execution_trace).toBe("AJy"); }); it("should use the actual received file count (not maxFiles*2) as the suggested limit", async () => { @@ -5148,6 +5157,7 @@ describe("create_pull_request - E003 file-limit fallback-to-issue", () => { // Staged mode: no API side effects, just a preview expect(result.success).toBe(true); expect(result.staged).toBe(true); + expect(result.execution_trace).toBe("AQJx"); expect(result.fallback_used).toBeUndefined(); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); expect(global.github.rest.pulls.create).not.toHaveBeenCalled(); diff --git a/actions/setup/js/push_to_pull_request_branch.cjs b/actions/setup/js/push_to_pull_request_branch.cjs index 8cbc43f045c..ac7ad86bd3e 100644 --- a/actions/setup/js/push_to_pull_request_branch.cjs +++ b/actions/setup/js/push_to_pull_request_branch.cjs @@ -11,6 +11,7 @@ const { getStalePullRequestReason, StalePullRequestError } = require("./pr_lifec /** @type {typeof import("fs")} */ const fs = require("fs"); const { withTransportIntegrity } = require("./transport_integrity.cjs"); +const { withExecutionTrace } = require("./safe_output_trace.cjs"); const path = require("path"); const { generateStagedPreview } = require("./staged_preview.cjs"); const { isStagedMode } = require("./safe_output_helpers.cjs"); @@ -569,12 +570,31 @@ async function main(config = {}) { * @param {import('./types/handler-factory').ResolvedTemporaryIds} resolvedTemporaryIds - Map of temporary IDs to resolved IDs * @returns {Promise} */ - return withTransportIntegrity(async function handlePushToPullRequestBranch(message, resolvedTemporaryIds, transport) { + // Trace map: A invocation; B count limit; C invalid repo; D object pack; + // E missing patch; F invalid patch; G oversized patch; H/I protection deny/fallback; + // J empty patch; K staged; L configured target; M triggering target; N wildcard target; + // O closed PR; P missing head; Q unauthorized head; R fork; S branch protection denied; + // T title/label validation; U protected issue; V missing remote; W fetch failure; + // X checkout failure; Y bundle; Z bundle failure; a octopus merge; + // b/c bundle protection deny/fallback; d patch; e patch failure; f add/add recovery; + // g/h post-apply deny/fallback; i blob size limit; j review PR; k review failure; + // l signed squash; m workflow scope; n push; o stale push; p push failure; + // q fallback PR; r fallback failure; s unchanged checkout; t missing SHA; + // u successful push; v/w no-changes error/ignore; x successful return; + // y unsuccessful/skipped return; z thrown error. + return withExecutionTrace( + { success: "x", failure: "y", error: "z" }, + withTransportIntegrity + )(async function handlePushToPullRequestBranch(message, resolvedTemporaryIds, transport, trace) { + trace("A"); + if (ifNoChanges === "error") trace("v"); + if (ifNoChanges === "ignore") trace("w"); const exec = transport.guardExec(globalThis.exec); let compilationNotice = getWorkflowCompilationNotice(message, config); if (compilationNotice) core.warning(compilationNotice); // Check max count if (maxCount > 0 && processedCount >= maxCount) { + trace("B"); core.info(`Skipping message - max count (${maxCount}) reached`); return { success: false, error: `Max count (${maxCount}) reached`, skipped: true }; } @@ -583,6 +603,7 @@ async function main(config = {}) { const repoResult = resolveAndValidateRepo(message, defaultTargetRepo, allowedRepos, "push to PR branch"); if (!repoResult.success) { + trace("C"); return { success: false, error: repoResult.error }; } const itemRepo = repoResult.repo; @@ -596,6 +617,7 @@ async function main(config = {}) { transport.captureOptional(transportPaths.bundlePath); const objectPackRecovery = {}; if (transportPaths.bundlePath && isObjectPack(transport.read(transportPaths.bundlePath))) { + trace("D"); const triggeringPR = context.payload?.pull_request; const aw = parseAwContext(context.payload?.inputs?.aw_context); const explicitPull = message.pull_request_number ?? message.pullRequestNumber ?? message.target ?? message.pr_number ?? message.pull_number ?? message.item_number ?? message.pr; @@ -659,6 +681,7 @@ async function main(config = {}) { // the first-pass validation input; bundle transport adds an authoritative // pre-apply git diff check later after the bundle ref has been fetched. if (!hasPatchFile) { + trace("E"); const msg = "No patch file found - cannot push without changes"; switch (ifNoChanges) { @@ -682,6 +705,7 @@ async function main(config = {}) { // Check for actual error conditions if (patchContent.includes("Failed to generate patch")) { + trace("F"); const msg = "Patch file contains error message - cannot push without changes"; core.error("Patch file generation failed"); core.error(`Patch file location: ${patchFilePath}`); @@ -731,6 +755,7 @@ async function main(config = {}) { core.info(`${sizeLabel}: ${sizeForCheckKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (sizeForCheckKb > maxSizeKb) { + trace("G"); const msg = `Patch size (${sizeForCheckKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)${mergedBaseBaselineHint}`; return { error: { success: false, error: msg }, fallbackFiles }; } @@ -743,6 +768,7 @@ async function main(config = {}) { if (!isEmptyPatch) { const protection = checkFileProtection(patchContent, config); if (protection.action === "deny") { + trace("H"); const filesStr = protection.files.join(", "); const msg = protection.source === "allowlist" @@ -752,6 +778,7 @@ async function main(config = {}) { return { error: { success: false, skipped: true, reasonCode: POLICY_FILE_PROTECTION_DENIED_REASON_CODE, error: msg }, fallbackFiles }; } if (protection.action === "fallback") { + trace("I"); fallbackFiles.push(...protection.files); core.warning(`Protected file protection triggered (fallback-to-issue): ${protection.files.join(", ")}. Will create review issue instead of pushing.`); } @@ -768,6 +795,7 @@ async function main(config = {}) { } if (isEmpty) { + trace("J"); const msg = "Patch file is empty - no changes to apply (noop operation)"; switch (ifNoChanges) { @@ -787,6 +815,7 @@ async function main(config = {}) { // If in staged mode, emit 🎭 Staged Mode Preview via generateStagedPreview if (isStaged) { + trace("K"); await generateStagedPreview({ title: "Push to PR Branch", description: "The following changes would be pushed if staged mode was disabled:", @@ -820,6 +849,7 @@ async function main(config = {}) { // Validate target configuration if (target !== "*" && target !== "triggering") { + trace("L"); const pullNumber = parsePositiveInteger(target); if (pullNumber === null) { return { success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number' }; @@ -829,6 +859,7 @@ async function main(config = {}) { // Compute the target branch name based on target configuration let pullNumber; if (target === "triggering") { + trace("M"); pullNumber = typeof context !== "undefined" ? context.payload?.pull_request?.number || context.payload?.issue?.number : undefined; if (!pullNumber) { const awContext = typeof context !== "undefined" ? parseAwContext(context.payload?.inputs?.aw_context) : null; @@ -849,6 +880,7 @@ async function main(config = {}) { }; } } else if (target === "*") { + trace("N"); const explicitPull = message.pull_request_number ?? message.pullRequestNumber ?? message.target ?? message.pr_number ?? message.pull_number ?? message.item_number ?? message.pr; if (explicitPull !== undefined && explicitPull !== null && explicitPull !== "") { pullNumber = parsePositiveInteger(explicitPull); @@ -920,6 +952,7 @@ async function main(config = {}) { }); pullRequest = response.data; if (pullRequest.state === "closed") { + trace("O"); const reason = `Pull request #${pullNumber} is closed; refusing to push to its branch`; core.info(reason); return { skipped: true, reason }; @@ -943,6 +976,7 @@ async function main(config = {}) { // SECURITY: When head.repo is null (likely a deleted fork) we cannot verify which // repository the PR head came from. Always reject to prevent writes to an unverifiable PR. if (pullRequest.head?.repo == null) { + trace("P"); const nullHeadErr = "Cannot push to PR: head repository is null (likely a deleted fork)"; core.error(nullHeadErr); return { success: false, error: nullHeadErr }; @@ -953,6 +987,7 @@ async function main(config = {}) { // names an automation fork, and prevents writes to a fork PR when head-repo is not set. const expectedHeadRepo = configuredHeadRepo || itemRepo; if (actualHeadRepo && actualHeadRepo.toLowerCase() !== expectedHeadRepo.toLowerCase()) { + trace("Q"); const { isFork: actualIsFork } = detectForkPR(pullRequest); if (actualIsFork && !configuredHeadRepo) { core.error(`Cannot push to fork PR branch: head is '${actualHeadRepo}', not '${itemRepo}'`); @@ -972,6 +1007,7 @@ async function main(config = {}) { // forks are eligible for updates. const { isFork, reason: forkReason } = detectForkPR(pullRequest); if (isFork) { + trace("R"); if (!configuredHeadRepo) { core.error(`Cannot push to fork PR branch: ${forkReason}`); core.error("Fork PRs remain blocked unless safe-outputs.push-to-pull-request-branch.head-repo is configured."); @@ -1032,11 +1068,13 @@ async function main(config = {}) { }, }); if (blockReason) { + trace("S"); core.error(blockReason); return { success: false, error: blockReason }; } } + trace("T"); // Validate title prefix if specified if (titlePrefix && !prTitle.startsWith(titlePrefix)) { return { success: false, error: `Pull request title "${prTitle}" does not start with required prefix "${titlePrefix}"` }; @@ -1058,6 +1096,7 @@ async function main(config = {}) { } const createProtectedFilesFallbackIssue = async files => { + trace("U"); transport.assertUnchanged(); const runUrl = buildWorkflowRunUrl(context, context.repo); const runId = context.runId; @@ -1155,6 +1194,7 @@ async function main(config = {}) { ); if (lsRemoteResult.exitCode === 2) { + trace("V"); const missingBranchError = MISSING_BRANCH_ERROR_TEMPLATE(branchName); if (ignoreMissingBranchFailure) { core.warning(`${missingBranchError} Skipping as configured by ignore-missing-branch-failure.`); @@ -1194,6 +1234,7 @@ async function main(config = {}) { baseGitOpts.cwd ); } catch (fetchError) { + trace("W"); const fetchErrorMessage = getErrorMessage(fetchError); if (ignoreMissingBranchFailure && looksLikeMissingRemoteBranchError(fetchErrorMessage)) { const missingBranchError = MISSING_BRANCH_ERROR_TEMPLATE(branchName); @@ -1220,6 +1261,7 @@ async function main(config = {}) { await exec.exec("git", ["checkout", "-B", branchName, branchRemoteRef], baseGitOpts); core.info(`Checked out existing branch from ${pushRepo}: ${branchName}`); } catch (checkoutError) { + trace("X"); return { success: false, error: `Failed to checkout branch ${branchName}: ${getErrorMessage(checkoutError)}` }; } @@ -1281,6 +1323,7 @@ async function main(config = {}) { } if (hasBundleFile) { + trace("Y"); // Bundle transport: import commits directly from the local bundle file. // This preserves merge commit topology and per-commit metadata. core.info(`Applying changes from bundle: ${bundleFilePath}`); @@ -1341,6 +1384,7 @@ async function main(config = {}) { if (deferBundleValidation) { const octopus = await exec.getExecOutput("git", ["rev-list", "--min-parents=3", bundleRef, "--not", ...agentHistoryExclusions], bundleGitOpts); if (octopus.stdout.trim()) { + trace("a"); return { success: false, error: "Cannot validate agent-created octopus merge commits in the bundle. Use two-parent merges instead." }; } // Preserve changes that were later reverted. Ancestor reachability @@ -1352,6 +1396,7 @@ async function main(config = {}) { core.info(`Pre-apply bundle verification: ${bundleFiles.length} file(s) detected from bundle transport`); const bundleProtection = checkFileProtectionPostApply(bundleFiles, config); if (bundleProtection.action === "deny") { + trace("b"); const filesStr = bundleProtection.files.join(", "); const msg = bundleProtection.source === "post-apply" @@ -1361,6 +1406,7 @@ async function main(config = {}) { return { success: false, skipped: true, reasonCode: POLICY_FILE_PROTECTION_DENIED_REASON_CODE, error: msg }; } if (bundleProtection.action === "fallback") { + trace("c"); core.warning(`Protected file protection triggered (fallback-to-issue): ${bundleProtection.files.join(", ")}. Will create review issue instead of pushing.`); return await createProtectedFilesFallbackIssue(bundleProtection.files); } @@ -1387,6 +1433,7 @@ async function main(config = {}) { // Non-fatal cleanup } } catch (bundleError) { + trace("Z"); transport.assertUnchanged(); core.error(`Failed to apply bundle: ${getErrorMessage(bundleError)}`); // Clean up temp ref if it exists @@ -1399,6 +1446,7 @@ async function main(config = {}) { } } else { // Patch transport (non-default): git am --3way + trace("d"); core.info("Applying patch..."); try { if (commitTitleSuffix) { @@ -1430,6 +1478,7 @@ async function main(config = {}) { core.info("Patch applied successfully"); } catch (error) { transport.assertUnchanged(); + trace("e"); core.warning(`Initial patch apply failed, attempting add/add recovery: ${getErrorMessage(error)}`); let recoveredFromAddAddConflict = false; @@ -1470,6 +1519,7 @@ async function main(config = {}) { } if (recoveredFromAddAddConflict) { + trace("f"); // Continue with normal push flow } else { core.error(`Failed to apply patch: ${getErrorMessage(error)}`); @@ -1521,6 +1571,7 @@ async function main(config = {}) { core.info(`Post-apply verification: ${protectionFiles.length} file(s) actually modified`); const postApplyProtection = checkFileProtectionPostApply(protectionFiles, config); if (postApplyProtection.action === "deny") { + trace("g"); const filesStr = postApplyProtection.files.join(", "); const msg = `SECURITY: Post-apply file-protection check failed. ` + `The patch applied files that were not detected by the pre-apply parser: ${filesStr}. ` + `This may indicate a patch-parser bypass attempt. Aborting push.`; core.error(msg); @@ -1529,6 +1580,7 @@ async function main(config = {}) { return { success: false, error: msg }; } if (postApplyProtection.action === "fallback") { + trace("h"); core.warning(`Post-apply: Protected file protection triggered (fallback-to-issue): ${postApplyProtection.files.join(", ")}`); await exec.exec("git", ["reset", "--hard", rangeBaseRef], baseGitOpts); return await createProtectedFilesFallbackIssue(postApplyProtection.files); @@ -1539,6 +1591,7 @@ async function main(config = {}) { const changedBlobSizeKb = Math.ceil(changedBlobSizeBytes / 1024); core.info(`Changed content size: ${changedBlobSizeKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (changedBlobSizeKb > maxSizeKb) { + trace("i"); const msg = `Changed content size (${changedBlobSizeKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)${mergedBaseBaselineHint}`; await exec.exec("git", ["reset", "--hard", rangeBaseRef], baseGitOpts); return { success: false, error: msg }; @@ -1551,6 +1604,7 @@ async function main(config = {}) { // before they are merged into the target PR. const detectionConclusionEnv = getEffectiveDetectionConclusion(process.env.GH_AW_DETECTION_CONCLUSION, process.env.GH_AW_DETECTION_REASON); if (detectionConclusionEnv === "warning") { + trace("j"); core.info("⚠️ Threat detection warning: creating review PR instead of direct push"); // Create a review branch name based on the original branch, using @@ -1667,6 +1721,7 @@ async function main(config = {}) { pr_url: reviewPR.html_url, }; } catch (reviewError) { + trace("k"); transport.assertUnchanged(); core.error(`Failed to create review PR: ${getErrorMessage(reviewError)}`); return { success: false, error: `Failed to create review PR: ${getErrorMessage(reviewError)}` }; @@ -1682,6 +1737,7 @@ async function main(config = {}) { const { stdout: mergeCountOut } = await exec.getExecOutput("git", ["rev-list", "--merges", "--count", `${squashBase}..HEAD`], baseGitOpts); const mergeCount = parseInt(mergeCountOut.trim(), 10); if (Number.isFinite(mergeCount) && mergeCount > 0) { + trace("l"); core.warning( `push_to_pull_request_branch: detected ${mergeCount} merge commit(s) in range ${squashBase}..HEAD. ` + `Merge commits cannot be pushed as signed commits. Squashing the range into a single regular commit to preserve content. ` + @@ -1707,6 +1763,7 @@ async function main(config = {}) { const agentWorkflowFiles = agentChangedFiles.filter(isWorkflowYamlFile); if (!allowWorkflows && agentWorkflowFiles.length > 0) { + trace("m"); return buildWorkflowsScopeError("Branch", core); } @@ -1717,6 +1774,7 @@ async function main(config = {}) { // scope may be required." — a transient server-side condition, not a real scope problem. // Retry with exponential backoff before treating the push as failed. const pushedSha = await withTransientPushRetry(async () => { + trace("n"); const staleReason = await getStalePullRequestReason(githubClient, { repo: repoParts, payload: { pull_request: { number: pullNumber, state: "open" } } }); if (staleReason) throw new StalePullRequestError(staleReason); transport.assertUnchanged(); @@ -1743,10 +1801,13 @@ async function main(config = {}) { pushedCommitSha = pushedSha; core.info(`pushSignedCommits returned pushed SHA: ${pushedSha}`); } + trace("u"); core.info(`Changes committed and pushed to branch: ${branchName}`); } catch (pushError) { + trace("p"); transport.assertUnchanged(); if (pushError instanceof StalePullRequestError) { + trace("o"); core.info(pushError.message); return { skipped: true, reason: pushError.message }; } @@ -1786,6 +1847,7 @@ async function main(config = {}) { // Fallback path for diverged branches: create a new pull request so changes // can still be reviewed and merged into the original PR branch. if ((isNonFastForward || isWorkflowsScopeTimeout) && fallbackAsPullRequest && allowGitPushFallback) { + trace("q"); const staleReason = await getStalePullRequestReason(githubClient, { repo: repoParts, payload: { pull_request: { number: pullNumber, state: "open" } } }); if (staleReason) { if (isNonFastForward) { @@ -1879,6 +1941,7 @@ async function main(config = {}) { url: fallbackPR.html_url, }; } catch (fallbackError) { + trace("r"); transport.assertUnchanged(); const fallbackErrorMessage = getErrorMessage(fallbackError); core.error(`Failed to create fallback pull request: ${fallbackErrorMessage}`); @@ -1902,6 +1965,7 @@ async function main(config = {}) { } } } else { + trace("s"); core.info("Skipping patch application (empty patch)"); const msg = "No changes to apply - noop operation completed successfully"; @@ -1925,6 +1989,7 @@ async function main(config = {}) { if (!commitSha) { const commitShaRes = await exec.getExecOutput("git", ["rev-parse", "HEAD"], baseGitOpts); if (commitShaRes.exitCode !== 0) { + trace("t"); return { success: false, error: "Failed to get commit SHA" }; } commitSha = commitShaRes.stdout.trim(); diff --git a/actions/setup/js/push_to_pull_request_branch.test.cjs b/actions/setup/js/push_to_pull_request_branch.test.cjs index 812d7313b3b..6abfbd3f66e 100644 --- a/actions/setup/js/push_to_pull_request_branch.test.cjs +++ b/actions/setup/js/push_to_pull_request_branch.test.cjs @@ -531,7 +531,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({ target }); const result = await handler({ branch }, {}); - expect(result).toEqual({ success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number' }); + expect(result).toEqual({ success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number', execution_trace: "ALy" }); expect(mockGithub.rest.pulls.get).not.toHaveBeenCalled(); }); @@ -545,7 +545,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({ target: "*" }); const result = await handler({ branch, [alias]: value }, {}); - expect(result).toEqual({ success: false, error: "Pull request number is required but not found" }); + expect(result).toEqual({ success: false, error: "Pull request number is required but not found", execution_trace: "ANy" }); expect(mockGithub.rest.pulls.get).not.toHaveBeenCalled(); } ); @@ -1178,6 +1178,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.branch_name).toBe("feature-branch"); expect(result.commit_url).toContain("test-owner/test-repo/commit/"); expect(result.before_state).toEqual({ head_sha: "remote-head-before" }); + expect(result.execution_trace).toBe("AMTdnux"); expect(result.after_state).toEqual({ head_sha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }); }); @@ -1195,6 +1196,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.skipped).not.toBe(true); expect(result.error).toContain("Authentication failed"); + expect(result.execution_trace).toBe("AMTdnpy"); } finally { pushSignedSpy.mockRestore(); } @@ -1337,6 +1339,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.error).toContain("no longer exists on origin"); + expect(result.execution_trace).toBe("AMTVy"); expect(mockExec.exec).not.toHaveBeenCalled(); }); @@ -1511,6 +1514,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(true); expect(mockCore.info).toHaveBeenCalledWith("Patch applied successfully after resolving add/add conflict(s)"); + expect(result.execution_trace).toBe("AMTdefnux"); expect(mockExec.exec).toHaveBeenCalledWith("git", ["checkout", "--theirs", "--", "docs/findings.md"], expect.any(Object)); expect(mockExec.exec).toHaveBeenCalledWith("git", ["add", "--", "docs/findings.md"], expect.any(Object)); expect(mockExec.exec).toHaveBeenCalledWith("git", ["am", "--continue"], expect.any(Object)); @@ -1526,6 +1530,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.fallback_used).toBe(true); expect(result.fallback_type).toBe("pull_request"); expect(result.pull_request_number).toBe(999); + expect(result.execution_trace).toBe("AMTdnpqx"); expect(mockGithub.rest.pulls.create).toHaveBeenCalled(); }); @@ -1583,7 +1588,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({}); const result = await handler({ branch }, {}); - expect(result).toEqual({ skipped: true, reason: expect.stringContaining("Pull request #123 closed") }); + expect(result).toEqual({ skipped: true, reason: expect.stringContaining("Pull request #123 closed"), execution_trace: "AMTdnpqy" }); expect(pushSignedSpy).toHaveBeenCalledTimes(1); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockExec.exec.mock.calls.some(([, args]) => args[0] === "checkout" && args[1] === "-b")).toBe(false); @@ -1616,7 +1621,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({}); const result = await handler({ branch }, {}); - expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(pushError) }); + expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(pushError), execution_trace: pushError.includes("`workflows`") ? "AMTdnpqy" : "AMTdnpy" }); expect(pushSignedSpy).toHaveBeenCalledTimes(1); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining(pushError)); @@ -1809,7 +1814,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const result = await resultPromise; expect(pushSignedSpy).toHaveBeenCalledTimes(6); - expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT) }); + expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT), execution_trace: "AMTdnnnnnnpqy" }); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT)); } finally { @@ -2668,6 +2673,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.skipped).toBe(true); expect(mockCore.info).toHaveBeenCalledWith(expect.stringContaining("noop")); + expect(result.execution_trace).toBe("AJy"); }); it("should error when patch is empty and if-no-changes is error", async () => { @@ -2679,6 +2685,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.error).toContain("No changes"); + expect(result.execution_trace).toBe("AvJy"); }); it("should silently skip when patch is empty and if-no-changes is ignore", async () => { @@ -2689,6 +2696,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const result = await handler({ branch: "should-silently-skip-when-patch-is-empty-and-if-no-changes-i" }, {}); expect(result.success).toBe(false); + expect(result.execution_trace).toBe("AwJy"); expect(result.skipped).toBe(true); }); @@ -3886,6 +3894,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(true); expect(result.staged).toBe(true); + expect(result.execution_trace).toBe("AKx"); // Should not have made any git commands expect(mockExec.exec).not.toHaveBeenCalled(); }); diff --git a/actions/setup/js/safe_output_handler_manager.cjs b/actions/setup/js/safe_output_handler_manager.cjs index a48676cc068..6ca9da2d355 100644 --- a/actions/setup/js/safe_output_handler_manager.cjs +++ b/actions/setup/js/safe_output_handler_manager.cjs @@ -16,7 +16,7 @@ const { getStalePullRequestReason } = require("./pr_lifecycle.cjs"); const { ERR_CONFIG, ERR_PARSE, ERR_SYSTEM, ERR_VALIDATION, SAFE_OUTPUT_E007, SAFE_OUTPUT_E099 } = require("./error_codes.cjs"); const { classifySafeOutputResult, computeSafeOutputsStatus, isFailedProcessingResult } = require("./safe_outputs_status.cjs"); const { buildSafeOutputDependencyPlan, checkSafeOutputDependencies } = require("./safe_output_dependencies.cjs"); -const { finalizeBufferedReviewOutcomes } = require("./safe_output_outcomes.cjs"); +const { finalizeBufferedReviewOutcomes, projectSafeOutputData } = require("./safe_output_outcomes.cjs"); const { hasUnresolvedTemporaryIds, replaceTemporaryIdReferences, replaceArtifactUrlReferences, normalizeTemporaryId, extractTemporaryIdReferences, getCreatedTemporaryId } = require("./temporary_id.cjs"); const { generateMissingInfoSections } = require("./missing_info_formatter.cjs"); const { setCollectedMissings } = require("./missing_messages_helper.cjs"); @@ -1428,6 +1428,7 @@ async function processMessages(messageHandlers, messages, onItemCreated = null, messageIndex: i, success: false, error: errorMsg, + ...projectSafeOutputData({ execution_trace: result.execution_trace }), ...(messageType === "merge_pull_request" ? { result: projectMergeFailure(result) } : result.operation_outcome === "indeterminate" ? { result: { operation_outcome: "indeterminate" } } : {}), }); // Track code-push failures for later comment annotations and conclusion reporting. @@ -1574,6 +1575,7 @@ async function processMessages(messageHandlers, messages, onItemCreated = null, messageIndex: i, success: false, error: errorMsg, + ...projectSafeOutputData({ execution_trace: error?.execution_trace }), }); // Track code-push failures for later comment annotations and conclusion reporting. if (CODE_PUSH_TYPES.has(messageType)) { @@ -1725,6 +1727,7 @@ async function processMessages(messageHandlers, messages, onItemCreated = null, success: false, deferred: false, error: errorMsg, + ...projectSafeOutputData({ execution_trace: error?.execution_trace }), result: { success: false, deferred: false, error: errorMsg }, }; } diff --git a/actions/setup/js/safe_output_outcomes.cjs b/actions/setup/js/safe_output_outcomes.cjs index 949bf31ab5d..552547f1545 100644 --- a/actions/setup/js/safe_output_outcomes.cjs +++ b/actions/setup/js/safe_output_outcomes.cjs @@ -71,6 +71,7 @@ function projectSafeOutputData(source) { if (source.identifier === null) data.identifier = null; if (OUTCOME_STATUSES.has(source.status)) data.status = source.status; if (typeof source.staged === "boolean") data.staged = source.staged; + if (typeof source.execution_trace === "string" && /^[A-Za-z]{1,1024}$/.test(source.execution_trace)) data.execution_trace = source.execution_trace; if (source.operation_outcome === "indeterminate") data.operation_outcome = "indeterminate"; const url = canonicalSafeOutputUrl(source.url); if (url) data.url = url; @@ -102,6 +103,7 @@ function buildSafeOutputOutcomes(results, staged = false) { status: isStaged && status === "success" ? "staged" : status, staged: isStaged, operation_outcome: pickOutcomeField(item, "operation_outcome"), + execution_trace: pickOutcomeField(item, "execution_trace"), url, }), }; diff --git a/actions/setup/js/safe_output_outcomes.test.cjs b/actions/setup/js/safe_output_outcomes.test.cjs index 804e8a8dcfe..a46677c4e70 100644 --- a/actions/setup/js/safe_output_outcomes.test.cjs +++ b/actions/setup/js/safe_output_outcomes.test.cjs @@ -60,6 +60,65 @@ describe("safe-output application outcomes", () => { expect(outcome).toMatchObject({ status: "failed", operation_outcome: "indeterminate" }); }); + it.each(["success", "failed", "skipped", "staged"])("retains the execution path for %s outcomes", status => { + const result = { success: status !== "failed", skipped: status === "skipped", staged: status === "staged", execution_trace: "ABca" }; + const [outcome] = buildSafeOutputOutcomes([{ type: "create_pull_request", success: result.success, result }]); + expect(outcome).toMatchObject({ status, execution_trace: "ABca" }); + expect(projectSafeOutputData(outcome).execution_trace).toBe("ABca"); + }); + + it.each(["", "A".repeat(1025), "AB\n", "a/b", "route=private", 123, ["AB"]])("omits invalid execution traces: %j", execution_trace => { + expect(projectSafeOutputData({ execution_trace })).not.toHaveProperty("execution_trace"); + expect(buildSafeOutputOutcomes([{ success: true, result: { execution_trace } }])[0]).not.toHaveProperty("execution_trace"); + }); + + it("persists isolated handler traces through processing and unified-session collection, including failures", async () => { + const root = fs.mkdtempSync("/tmp/safe-output-traces-"); + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn(), setOutput: vi.fn(), setFailed: vi.fn() }); + vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); + const traces = ["AB", "AC", "AD", "AE", "AF"]; + try { + for (const type of ["create_pull_request", "push_to_pull_request_branch"]) { + const handler = async message => { + const execution_trace = traces[message.scenario]; + if (message.scenario === 2) throw Object.assign(new Error("PRIVATE_ERROR"), { execution_trace }); + return { + success: message.scenario !== 1, + execution_trace, + ...(message.scenario === 1 ? { error: "PRIVATE_ERROR" } : {}), + ...(message.scenario === 3 ? { skipped: true, reason: "PRIVATE_REASON" } : {}), + ...(message.scenario === 4 ? { staged: true } : {}), + }; + }; + // Agent-request fields must never substitute for handler execution evidence. + const messages = traces.map((_, scenario) => ({ type, scenario, execution_trace: "FORGED" })); + const processing = await processMessages(new Map([[type, handler]]), messages); + writeSafeOutputOutcomes(processing.results, false, path.join(root, type, "safe-output-outcomes.jsonl")); + const events = writeUnifiedSession({ rootDir: path.join(root, type), graders: [] }); + const outcomes = events.filter(event => event.type === "safe_output.outcome"); + expect(outcomes.map(event => event.data.execution_trace)).toEqual(traces); + expect(outcomes.map(event => event.data.status)).toEqual(["success", "failed", "failed", "skipped", "staged"]); + const persisted = fs.readFileSync(path.join(root, type, "usage/aw_session.jsonl"), "utf8"); + expect(persisted).not.toContain("FORGED"); + expect(persisted).not.toContain("PRIVATE"); + expect(validateSession(persisted)).toBe(events.length); + } + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + + it("replaces a deferred trace with the terminal retry failure trace", async () => { + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn(), setOutput: vi.fn(), setFailed: vi.fn() }); + vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); + const handler = vi + .fn() + .mockResolvedValueOnce({ deferred: true, execution_trace: "AB" }) + .mockRejectedValueOnce(Object.assign(new Error("PRIVATE_ERROR"), { execution_trace: "AC" })); + const processing = await processMessages(new Map([["create_pull_request", handler]]), [{ type: "create_pull_request" }]); + expect(buildSafeOutputOutcomes(processing.results)[0]).toMatchObject({ status: "failed", execution_trace: "AC" }); + }); + it("records queued ledger appends as delegated until persistence", () => { const record = { type: "ledger_append", messageIndex: 0, success: true, result: { queued: true } }; const [outcome] = buildSafeOutputOutcomes([record]); diff --git a/actions/setup/js/safe_output_trace.cjs b/actions/setup/js/safe_output_trace.cjs new file mode 100644 index 00000000000..8bdc53832b1 --- /dev/null +++ b/actions/setup/js/safe_output_trace.cjs @@ -0,0 +1,47 @@ +// @ts-check + +const MAX_EXECUTION_TRACE_LENGTH = 1024; + +/** + * Bind the trace inside each invocation so nested helpers share only that + * invocation's trace. Markers must be stable, unique source literals per handler. + * The outer wrapper also observes failures converted by transport integrity. + * + * @param {{success: string, failure: string, error: string}} terminal + * @param {(handler: (...args: any[]) => any) => (...args: any[]) => any} [wrap] + * @returns {(handler: (...args: any[]) => any) => (...args: any[]) => Promise} + */ +function withExecutionTrace(terminal, wrap = handler => handler) { + return handler => + async function (...args) { + let path = ""; + const trace = marker => { + if (typeof marker === "string" && /^[A-Za-z]$/.test(marker) && path.length < MAX_EXECUTION_TRACE_LENGTH - 1) path += marker; + }; + const attach = value => { + if ((value !== null && typeof value === "object") || typeof value === "function") { + try { + Object.defineProperty(value, "execution_trace", { value: path, configurable: true, enumerable: true, writable: true }); + } catch { + // Frozen errors/results must not turn observability into a new failure. + } + } + }; + const finish = marker => { + // Reserve one byte for the terminal outcome even when a route is bounded. + if (typeof marker === "string" && /^[A-Za-z]$/.test(marker)) path += marker; + }; + try { + const result = await wrap((...handlerArgs) => handler.call(this, ...handlerArgs, trace)).apply(this, args); + finish(result?.success === false || result?.skipped ? terminal.failure : terminal.success); + attach(result); + return result; + } catch (error) { + finish(terminal.error); + attach(error); + throw error; + } + }; +} + +module.exports = { withExecutionTrace, MAX_EXECUTION_TRACE_LENGTH }; diff --git a/actions/setup/js/safe_output_trace.test.cjs b/actions/setup/js/safe_output_trace.test.cjs new file mode 100644 index 00000000000..96faa9fe985 --- /dev/null +++ b/actions/setup/js/safe_output_trace.test.cjs @@ -0,0 +1,172 @@ +import { describe, it, expect, vi } from "vitest"; +import fs from "node:fs"; +import path from "node:path"; + +const { withExecutionTrace, MAX_EXECUTION_TRACE_LENGTH } = require("./safe_output_trace.cjs"); +const { withTransportIntegrity, TransportIntegrityError } = require("./transport_integrity.cjs"); +const terminal = { success: "x", failure: "y", error: "z" }; + +describe("safe-output execution traces", () => { + it("preserves result identity, arguments, receiver and nested branch order", async () => { + const result = { success: true }; + const receiver = { value: 42 }; + const handler = withExecutionTrace(terminal)(async function (message, ids, trace) { + expect(this).toBe(receiver); + expect(message).toEqual({ value: 42 }); + expect(ids).toEqual({ temp: 1 }); + trace("A"); + const nested = () => trace("b"); + nested(); + trace("C"); + return result; + }); + expect(await handler.call(receiver, { value: 42 }, { temp: 1 })).toBe(result); + expect(result.execution_trace).toBe("AbCx"); + expect(JSON.parse(JSON.stringify(result)).execution_trace).toBe("AbCx"); + }); + + it.each([{ success: false }, { skipped: true }, { success: true, skipped: true }])("marks unsuccessful/skipped returns: %j", async result => { + const handler = withExecutionTrace(terminal)(async trace => { + trace("A"); + return result; + }); + expect(await handler()).toBe(result); + expect(result.execution_trace).toBe("Ay"); + }); + + it("preserves thrown Error identity, type, message and cause", async () => { + const cause = new Error("cause"); + const error = new TypeError("original", { cause }); + const handler = withExecutionTrace(terminal)(async trace => { + trace("A"); + await Promise.resolve(); + trace("B"); + throw error; + }); + await expect(handler()).rejects.toBe(error); + expect(error.execution_trace).toBe("ABz"); + expect(error).toBeInstanceOf(TypeError); + expect(error.message).toBe("original"); + expect(error.cause).toBe(cause); + }); + + it("traces synchronous failures and does not replace primitive throws", async () => { + const error = new Error("sync"); + const handler = withExecutionTrace(terminal)(trace => { + trace("A"); + throw error; + }); + await expect(handler()).rejects.toBe(error); + expect(error.execution_trace).toBe("Az"); + await expect( + withExecutionTrace(terminal)(() => { + throw "original"; + })() + ).rejects.toBe("original"); + }); + + it("keeps overlapping and sequential invocations private", async () => { + let release; + const blocked = new Promise(resolve => { + release = resolve; + }); + const handler = withExecutionTrace(terminal)(async (first, trace) => { + if (first) { + trace("A"); + await blocked; + trace("B"); + } else { + trace("C"); + } + return { success: true }; + }); + const first = handler(true); + expect((await handler(false)).execution_trace).toBe("Cx"); + release(); + expect((await first).execution_trace).toBe("ABx"); + expect((await handler(false)).execution_trace).toBe("Cx"); + }); + + it("bounds paths and reserves the final outcome without recording invalid markers or inputs", async () => { + const secret = "untrusted-input-do-not-record"; + const handler = withExecutionTrace(terminal)(async (input, trace) => { + trace(input); + trace("AB"); + trace("1"); + trace("\n"); + trace(undefined); + for (let i = 0; i < MAX_EXECUTION_TRACE_LENGTH * 2; i++) trace("A"); + return { success: true }; + }); + const result = await handler(secret); + expect(result.execution_trace).toBe("A".repeat(MAX_EXECUTION_TRACE_LENGTH - 1) + "x"); + expect(result.execution_trace).toHaveLength(1024); + expect(result.execution_trace).not.toContain(secret); + }); + + it("does not turn immutable results or errors into tracing failures", async () => { + const result = Object.freeze({ success: true }); + expect(await withExecutionTrace(terminal)(() => result)()).toBe(result); + const error = Object.freeze(new Error("immutable")); + await expect( + withExecutionTrace(terminal)(() => { + throw error; + })() + ).rejects.toBe(error); + expect(await withExecutionTrace(terminal)(() => undefined)()).toBeUndefined(); + }); + + it("wraps transport conversion and cleanup without introducing API effects", async () => { + const previousCore = global.core; + global.core = { error: vi.fn() }; + try { + const handler = withExecutionTrace( + terminal, + withTransportIntegrity + )(async (message, ids, transport, trace) => { + expect(message).toEqual({ untouched: true }); + expect(ids).toEqual({}); + expect(typeof transport.assertUnchanged).toBe("function"); + trace("A"); + throw new TransportIntegrityError("artifact"); + }); + const result = await handler({ untouched: true }, {}); + expect(result).toMatchObject({ success: false, error_type: "ERR_TRANSPORT_INTEGRITY", execution_trace: "Ay" }); + expect(global.core.error).toHaveBeenCalledOnce(); + } finally { + global.core = previousCore; + } + }); + + it("retains route state when an outer wrapper fails after the handler returns", async () => { + const error = new Error("cleanup failed"); + const wrap = + handler => + async (...args) => { + await handler(...args); + throw error; + }; + const handler = withExecutionTrace( + terminal, + wrap + )(trace => { + trace("A"); + return { success: true }; + }); + await expect(handler()).rejects.toBe(error); + expect(error.execution_trace).toBe("Az"); + }); + + it.each(["create_pull_request.cjs", "push_to_pull_request_branch.cjs"])("%s has unique, explicit, documented single-letter branch markers", filename => { + const source = fs.readFileSync(path.join(__dirname, filename), "utf8"); + const calls = [...source.matchAll(/\btrace\(([^)]*)\)/g)].map(match => match[1]); + expect(calls.length).toBeGreaterThan(40); + for (const call of calls) expect(call).toMatch(/^"[A-Za-z]"$/); + const markers = calls.map(call => JSON.parse(call)); + const outcomes = [...source.matchAll(/(?:success|failure|error): "([xyz])"/g)].map(match => match[1]); + expect(outcomes).toEqual(["x", "y", "z"]); + expect(new Set([...markers, ...outcomes]).size).toBe(markers.length + outcomes.length); + expect(source).toContain("// Trace map:"); + expect(source).toContain('require("./safe_output_trace.cjs")'); + }); +}); diff --git a/actions/setup/js/types/unified_session.d.ts b/actions/setup/js/types/unified_session.d.ts index 1152d758f67..4269cb5ce15 100644 --- a/actions/setup/js/types/unified_session.d.ts +++ b/actions/setup/js/types/unified_session.d.ts @@ -456,6 +456,13 @@ export interface SafeOutputData { staged?: boolean; /** The operation may have applied even though processing failed to confirm it. */ operation_outcome?: "indeterminate"; + /** + * Ordered branch letters from the safe-output handler source, not a correlation ID. + * @pattern ^[A-Za-z]+$ + * @minLength 1 + * @maxLength 1024 + */ + execution_trace?: string; } export interface SafeOutputErrorData extends SafeOutputData { diff --git a/docs/public/schemas/unified-session.schema.json b/docs/public/schemas/unified-session.schema.json index 2ae6e9b41dc..1abe2d0d9c2 100644 --- a/docs/public/schemas/unified-session.schema.json +++ b/docs/public/schemas/unified-session.schema.json @@ -1260,7 +1260,8 @@ "output_id": { "type": "string" }, "messageIndex": { "type": "number" }, "staged": { "type": "boolean" }, - "operation_outcome": { "type": "string", "const": "indeterminate", "description": "The operation may have applied even though processing failed to confirm it." } + "operation_outcome": { "type": "string", "const": "indeterminate", "description": "The operation may have applied even though processing failed to confirm it." }, + "execution_trace": { "type": "string", "description": "Ordered branch letters from the safe-output handler source, not a correlation ID.", "pattern": "^[A-Za-z]+$", "minLength": 1, "maxLength": 1024 } }, "additionalProperties": false }, @@ -1279,6 +1280,7 @@ "messageIndex": { "type": "number" }, "staged": { "type": "boolean" }, "operation_outcome": { "type": "string", "const": "indeterminate", "description": "The operation may have applied even though processing failed to confirm it." }, + "execution_trace": { "type": "string", "description": "Ordered branch letters from the safe-output handler source, not a correlation ID.", "pattern": "^[A-Za-z]+$", "minLength": 1, "maxLength": 1024 }, "errors": { "type": "array", "items": { "$ref": "#/definitions/SafeOutputData" } } }, "additionalProperties": false diff --git a/docs/src/content/docs/specs/unified-agent-session-specification.md b/docs/src/content/docs/specs/unified-agent-session-specification.md index 5bd84612270..fe6155e8f66 100644 --- a/docs/src/content/docs/specs/unified-agent-session-specification.md +++ b/docs/src/content/docs/specs/unified-agent-session-specification.md @@ -867,6 +867,18 @@ source for opaque fields. | `session.collection_warning`, `session.collection` | Explicit collection diagnostics and coverage. | | `session.format` | Leading collector-owned file-format metadata, distinct from source-native events with the same type. | +Pull-request creation and branch-push outcomes also retain `data.execution_trace`, +an ordered sequence of source-defined branch letters (`A–Z`, `a–z`, at most +1,024 characters, retaining the prefix and terminal outcome at the limit). +Each instrumented route has a unique letter within its handler; +the source comments define the mapping. Equal paths produce equal identifiers, +not unique invocation IDs. Interpret a trace with its operation `type` and the +handler source revision from that run to compare hot paths and diagnose recovery +or fallback routes. Traces survive skipped, staged, and failed execution, including +thrown errors; absent traces mean instrumentation was unavailable or the handler +was not entered. The reusable tracing helper can instrument other safe outputs +without recording request bodies, branch names, credentials, or error text. + Job-result records cover only the conclusion job's direct dependencies, including custom jobs when present in `needs`. They do not imply a final workflow result or a result for the still-running conclusion job. Missing dependency metadata, From 430c17272cf34e6e6e23ad5cb6da77149d379fbf Mon Sep 17 00:00:00 2001 From: pelikhan <4175913+pelikhan@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:39:09 +0000 Subject: [PATCH 2/3] Declare execution-trace wrapper receiver type Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- actions/setup/js/safe_output_trace.cjs | 1 + 1 file changed, 1 insertion(+) diff --git a/actions/setup/js/safe_output_trace.cjs b/actions/setup/js/safe_output_trace.cjs index 8bdc53832b1..487995b2943 100644 --- a/actions/setup/js/safe_output_trace.cjs +++ b/actions/setup/js/safe_output_trace.cjs @@ -13,6 +13,7 @@ const MAX_EXECUTION_TRACE_LENGTH = 1024; */ function withExecutionTrace(terminal, wrap = handler => handler) { return handler => + /** @this {any} */ async function (...args) { let path = ""; const trace = marker => { From ea0dd79daa6dac3addb218299a801fdcef7221f6 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Tue, 17 Nov 2026 02:34:33 -0800 Subject: [PATCH 3/3] Make pull-request execution traces self-describing and accurate Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- actions/setup/js/create_pull_request.cjs | 140 +++++++------- actions/setup/js/create_pull_request.test.cjs | 81 ++++++-- .../setup/js/push_to_pull_request_branch.cjs | 130 +++++++------ .../js/push_to_pull_request_branch.test.cjs | 82 ++++++-- .../setup/js/safe_output_handler_manager.cjs | 1 + actions/setup/js/safe_output_outcomes.cjs | 2 +- .../setup/js/safe_output_outcomes.test.cjs | 33 ++-- actions/setup/js/safe_output_trace.cjs | 14 +- actions/setup/js/safe_output_trace.test.cjs | 181 +++++++++++++----- actions/setup/js/types/unified_session.d.ts | 6 +- .../schemas/unified-session.schema.json | 4 +- .../unified-agent-session-specification.md | 29 ++- 12 files changed, 466 insertions(+), 237 deletions(-) diff --git a/actions/setup/js/create_pull_request.cjs b/actions/setup/js/create_pull_request.cjs index 5b4ab8966e1..6379dec5884 100644 --- a/actions/setup/js/create_pull_request.cjs +++ b/actions/setup/js/create_pull_request.cjs @@ -974,31 +974,30 @@ async function main(config = {}) { * @param {Object} resolvedTemporaryIds - Map of temporary IDs to {repo, number} * @returns {Promise} Result with success/error status and PR details */ - // Trace map: A invocation; B count limit; C invalid temporary ID; D invalid repo; - // E head repo; F base override; G object pack; H missing patch; I allow empty; - // J patch limits; K patch generation failed; L oversized patch; M protection deny; - // N protection fallback; O protection review; P empty patch; Q staged; R bundle; - // S bundle failure; T signed merge retry; U unrecovered bundle push; V bundle issue; - // W patch; X patch failure; Y add/add recovered; Z base replay; a apply failed; - // b/c/d post-apply deny/fallback/review; e patch push failure; f push issue; - // g empty commit; h empty push failure; i protected issue; j reused issue; - // k new protected issue; l protected issue failure; m PR publication; - // n PR publication failure; o permission issue; p permission issue failure; - // q PR fallback disabled; r PR fallback issue; s PR fallback failure; - // t bundle push attempt; u PR delivery capture; v/w no-changes error/ignore; - // x successful return; y unsuccessful/skipped return; z thrown error. + // Trace map: init invocation; limt count limit; tmpi invalid temporary ID; + // repo invalid repo; head head repo; base base override; pack object pack; + // miss missing patch; alwe allow empty; flim patch file limit; pgen patch generation failed; + // size oversized patch; deny/fall/revw protection deny/fallback/review; noop empty patch; + // stag staged; bund bundle; bfal bundle failure; sign signed merge retry; + // bper unrecovered bundle push; biss bundle issue; ptch patch; pfal patch failure; + // aadd add/add recovered; rply base replay; afal apply failed; + // aden/afbk/arvw post-apply deny/fallback/review; perr patch push failure; + // palt push fallback issue; ecmt empty commit; eper empty push failure; + // prot protected issue; reus reused issue; issu new protected issue; ifal protected issue failure; + // publ PR publication; pber PR publication failure; perm permission issue; + // peif permission issue failure; nofb PR fallback disabled; fbis PR fallback issue; + // fber PR fallback failure; push bundle push attempt; capt PR delivery capture; + // nerr/ignr no-changes error/ignore; done successful return; + // fail unsuccessful/skipped return; thrw thrown error. return withExecutionTrace( - { success: "x", failure: "y", error: "z" }, + { success: "done", failure: "fail", error: "thrw" }, withTransportIntegrity - )(async function handleCreatePullRequest(message, resolvedTemporaryIds, transport, trace) { - trace("A"); - if (ifNoChanges === "error") trace("v"); - if (ifNoChanges === "ignore") trace("w"); - if (isStaged) trace("Q"); + )(async function handleCreatePullRequest(trace, message, resolvedTemporaryIds, transport) { + trace("init"); const exec = transport.guardExec(globalThis.exec); // Check if we've hit the max limit if (processedCount >= maxCount) { - trace("B"); + trace("limt"); core.warning(`Skipping create_pull_request: max count of ${maxCount} reached`); return { success: false, @@ -1012,7 +1011,7 @@ async function main(config = {}) { const tempIdResult = getOrGenerateTemporaryId(pullRequestItem, "pull request"); if (tempIdResult.error) { - trace("C"); + trace("tmpi"); core.warning(`Skipping create_pull_request: ${tempIdResult.error}`); return { success: false, error: tempIdResult.error }; } @@ -1022,7 +1021,7 @@ async function main(config = {}) { const repoResult = resolveAndValidateRepo(pullRequestItem, defaultTargetRepo, allowedRepos, "pull request"); if (!repoResult.success) { - trace("D"); + trace("repo"); core.warning(`Skipping pull request: ${repoResult.error}`); return { success: false, error: repoResult.error }; } @@ -1050,7 +1049,7 @@ async function main(config = {}) { let pushRepoParts = repoParts; let pushGithubClient = githubClient; if (configuredHeadRepo) { - trace("E"); + trace("head"); const headRepoResult = resolveAndValidateRepo({ repo: configuredHeadRepo }, itemRepo, allowedRepos, "pull request head repository"); if (!headRepoResult.success) { return { success: false, error: headRepoResult.error }; @@ -1084,7 +1083,7 @@ async function main(config = {}) { // Overriding to a different branch requires allowed_base_branches to be configured, unless the // base branch belongs to a pull request created earlier in this same run (a stacked pull request). if (typeof pullRequestItem.base === "string" && pullRequestItem.base.trim() !== "") { - trace("F"); + trace("base"); const requestedBaseBranchRaw = pullRequestItem.base.trim(); const requestedBaseBranchForLog = JSON.stringify(requestedBaseBranchRaw); core.info(`Base branch override requested: ${requestedBaseBranchForLog}`); @@ -1251,7 +1250,7 @@ async function main(config = {}) { } if (bundleFilePath && isObjectPack(transport.read(bundleFilePath))) { - trace("G"); + trace("pack"); const recovered = await recoverObjectPack(transport, transportPaths, { repo: itemRepo, baseBranch, @@ -1279,16 +1278,17 @@ async function main(config = {}) { } const hasPatchFile = !!(patchFilePath && fs.existsSync(patchFilePath)); if (!hasPatchFile) { - trace("H"); + trace("miss"); // If allow-empty is enabled, we can proceed without a patch file if (allowEmpty) { - trace("I"); + trace("alwe"); core.info("No patch file found, but allow-empty is enabled - will create empty PR"); } else { const message = "No patch file found - cannot create pull request without changes"; // If in staged mode, still show preview if (isStaged) { + trace("stag"); let summaryContent = "## 🎭 Staged Mode: Create Pull Request Preview\n\n"; summaryContent += "The following pull request would be created if staged mode was disabled:\n\n"; summaryContent += `**Status:** ⚠️ No patch file found\n\n`; @@ -1302,9 +1302,11 @@ async function main(config = {}) { switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: message }; case "ignore": + trace("ignr"); // Silent success - no console output return { success: false, skipped: true }; @@ -1333,12 +1335,13 @@ async function main(config = {}) { try { enforcePullRequestLimits(patchContent, maxFiles); } catch (error) { - trace("J"); + trace("flim"); const errorMessage = getErrorMessage(error); core.warning(`Pull request limit exceeded: ${errorMessage}`); // In staged mode, show a preview instead of performing API side effects if (isStaged) { + trace("stag"); let summaryContent = "## 🎭 Staged Mode: Create Pull Request Preview\n\n"; summaryContent += "The following pull request would be created if staged mode was disabled:\n\n"; summaryContent += `**Status:** ⚠️ Patch file limit exceeded\n\n`; @@ -1385,7 +1388,7 @@ async function main(config = {}) { // Check for actual error conditions (but allow empty patches as valid noop) if (patchContent.includes("Failed to generate patch")) { - trace("K"); + trace("pgen"); // If allow-empty is enabled, ignore patch errors and proceed if (allowEmpty) { core.info("Patch file contains error, but allow-empty is enabled - will create empty PR"); @@ -1396,6 +1399,7 @@ async function main(config = {}) { // If in staged mode, still show preview if (isStaged) { + trace("stag"); let summaryContent = "## 🎭 Staged Mode: Create Pull Request Preview\n\n"; summaryContent += "The following pull request would be created if staged mode was disabled:\n\n"; summaryContent += `**Status:** ⚠️ Patch file contains error\n\n`; @@ -1409,9 +1413,11 @@ async function main(config = {}) { switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: message }; case "ignore": + trace("ignr"); // Silent success - no console output return { success: false, skipped: true }; @@ -1432,11 +1438,12 @@ async function main(config = {}) { core.info(`Patch size: ${patchSizeKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (patchSizeKb > maxSizeKb) { - trace("L"); + trace("size"); const message = `Patch size (${patchSizeKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)`; // If in staged mode, still show preview with error if (isStaged) { + trace("stag"); let summaryContent = "## 🎭 Staged Mode: Create Pull Request Preview\n\n"; summaryContent += "The following pull request would be created if staged mode was disabled:\n\n"; summaryContent += `**Status:** ❌ Patch size exceeded\n\n`; @@ -1467,7 +1474,7 @@ async function main(config = {}) { protected_files_policy: config.protected_files_policy ?? "request_review", }); if (protection.action === "deny") { - trace("M"); + trace("deny"); const filesStr = protection.files.join(", "); const message = protection.source === "allowlist" @@ -1477,26 +1484,28 @@ async function main(config = {}) { return { success: false, error: message }; } if (protection.action === "fallback") { - trace("N"); + trace("fall"); manifestProtectionFallback = protection.files; core.warning(`Protected file protection triggered (fallback-to-issue): ${protection.files.join(", ")}. Will create review issue instead of pull request.`); } if (protection.action === "request_review") { - trace("O"); + trace("revw"); manifestProtectionRequestReview = protection.files; core.warning(`Protected file protection triggered (request_review): ${protection.files.join(", ")}. Will create pull request with caution and request-changes review.`); } } if (isEmpty && !isStaged && !allowEmpty) { - trace("P"); + trace("noop"); const message = "Patch file is empty - no changes to apply (noop operation)"; switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: "No changes to push - failing as configured by if-no-changes: error" }; case "ignore": + trace("ignr"); // Silent success - no console output return { success: false, skipped: true }; @@ -1517,6 +1526,7 @@ async function main(config = {}) { // If in staged mode, emit step summary instead of creating PR if (isStaged) { + trace("stag"); let summaryContent = "## 🎭 Staged Mode: Create Pull Request Preview\n\n"; summaryContent += "The following pull request would be created if staged mode was disabled:\n\n"; @@ -1906,7 +1916,7 @@ async function main(config = {}) { // token on multi-commit branches where workflow files may have been modified). let newCommitCount = 0; if (hasBundleFile) { - trace("R"); + trace("bund"); // Bundle transport: fetch commits directly from the bundle file. // This preserves merge commit topology and per-commit metadata (messages, authorship) // unlike git format-patch which flattens history and drops merge resolution content. @@ -1914,7 +1924,7 @@ async function main(config = {}) { try { await applyBundleToBranch(bundleFilePath, branchName, originalAgentBranch, exec, config.allow_full_history_fetch === true); } catch (bundleError) { - trace("S"); + trace("bfal"); transport.assertUnchanged(); core.error(`Failed to apply bundle: ${getErrorMessage(bundleError)}`); return { success: false, error: `Failed to apply bundle: ${getErrorMessage(bundleError)}` }; @@ -1927,7 +1937,7 @@ async function main(config = {}) { { const forkCwd = process.cwd(); const runBundlePush = async () => { - trace("t"); + trace("push"); transport.assertUnchanged(); branchName = await handleRemoteBranchCollision(branchName, preserveBranchName, { assertTransportUnchanged: transport.assertUnchanged, @@ -1978,7 +1988,7 @@ async function main(config = {}) { const isSignedMergeReplayRefusal = signedCommits && /pushSignedCommits: refusing unsigned push/.test(pushErrorMessage) && /merge commit/i.test(pushErrorMessage); if (isSignedMergeReplayRefusal) { - trace("T"); + trace("sign"); core.warning("Signed push rejected merge commit topology from bundle; rewriting branch and retrying signed push"); try { await rewriteBundleBranchAsSingleCommit(baseBranch, exec, bundleFilePath, { @@ -2020,7 +2030,7 @@ async function main(config = {}) { } if (!pushRecovered) { - trace("U"); + trace("bper"); core.error(`Git push failed: ${getErrorMessage(pushError)}`); if (manifestProtectionFallback) { @@ -2034,7 +2044,7 @@ async function main(config = {}) { const error = `Failed to push changes: ${getErrorMessage(pushError)}`; return { success: false, error, error_type: "push_failed" }; } else { - trace("V"); + trace("biss"); core.warning("Git push operation failed - creating fallback issue instead of pull request"); const runUrl = buildWorkflowRunUrl(context, context.repo); @@ -2129,7 +2139,7 @@ async function main(config = {}) { // Apply the patch using git CLI (skip if empty) if (!isEmpty && patchFilePath) { - trace("W"); + trace("ptch"); /** @type {any} */ let postApplyBaseRef = null; const capturePostApplyBaseRef = async () => { @@ -2174,13 +2184,13 @@ async function main(config = {}) { core.info("Patch applied successfully"); patchApplied = true; } catch (patchError) { - trace("X"); + trace("pfal"); transport.assertUnchanged(); core.error(`Failed to apply patch with --3way: ${getErrorMessage(patchError)}`); const recoveredFromAddAddConflict = await tryRecoverGitAmAddAddConflict(exec); if (recoveredFromAddAddConflict.recovered) { - trace("Y"); + trace("aadd"); patchApplied = true; } else { if (recoveredFromAddAddConflict.errorMessage) { @@ -2217,7 +2227,7 @@ async function main(config = {}) { // the patch was generated, making --3way unable to resolve the conflicts automatically. core.info("Attempting fallback: create PR branch at original base commit..."); try { - trace("Z"); + trace("rply"); // Use the base commit recorded at patch generation time. // The From header in format-patch output contains the agent's new commit SHA // which does not exist in this checkout, so we cannot derive the base from it. @@ -2284,7 +2294,7 @@ async function main(config = {}) { } if (!patchApplied) { - trace("a"); + trace("afal"); return { success: false, error: "Failed to apply patch" }; } } @@ -2307,19 +2317,19 @@ async function main(config = {}) { protected_files_policy: config.protected_files_policy ?? "request_review", }); if (postApplyProtection.action === "deny") { - trace("b"); + trace("aden"); const filesStr = postApplyProtection.files.join(", "); const msg = `SECURITY: Post-apply file-protection check failed. ` + `The patch applied files that were not detected by the pre-apply parser: ${filesStr}. ` + `This may indicate a patch-parser bypass attempt. Aborting.`; core.error(msg); return { success: false, error: msg }; } if (postApplyProtection.action === "fallback") { - trace("c"); + trace("afbk"); manifestProtectionFallback = postApplyProtection.files; core.warning(`Post-apply: Protected file protection triggered (fallback-to-issue): ${postApplyProtection.files.join(", ")}`); } if (postApplyProtection.action === "request_review") { - trace("d"); + trace("arvw"); manifestProtectionRequestReview = postApplyProtection.files; core.warning(`Post-apply: Protected file protection triggered (request_review): ${postApplyProtection.files.join(", ")}`); } @@ -2377,7 +2387,7 @@ async function main(config = {}) { core.info("Could not count new commits - extra empty commit will be skipped"); } } catch (pushError) { - trace("e"); + trace("perr"); transport.assertUnchanged(); // Push failed - create fallback issue instead of PR (if fallback is enabled) core.error(`Git push failed: ${getErrorMessage(pushError)}`); @@ -2404,7 +2414,7 @@ async function main(config = {}) { const runUrl = buildWorkflowRunUrl(context, context.repo); const runId = context.runId; - trace("f"); + trace("palt"); // Read patch content for preview let patchPreview = buildPatchPreviewBlocks(""); if (patchFilePath && fs.existsSync(patchFilePath)) { @@ -2499,7 +2509,7 @@ async function main(config = {}) { // For empty patches with allow-empty, we still need to push the branch if (allowEmpty) { - trace("g"); + trace("ecmt"); core.info("allow-empty is enabled - will create branch and push with empty commit"); // Push the branch with an empty commit to allow PR creation try { @@ -2551,8 +2561,8 @@ async function main(config = {}) { core.info("Could not count new commits - extra empty commit will be skipped"); } } catch (pushError) { + trace("eper"); transport.assertUnchanged(); - trace("h"); const error = `Failed to push empty branch: ${getErrorMessage(pushError)}`; core.error(error); return { @@ -2567,9 +2577,11 @@ async function main(config = {}) { switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: "No changes to apply - failing as configured by if-no-changes: error" }; case "ignore": + trace("ignr"); // Silent success - no console output return { success: false, skipped: true }; @@ -2590,7 +2602,7 @@ async function main(config = {}) { // - Push-failed case: push was rejected (e.g. missing `workflows` permission); provides // patch artifact download instructions instead of the compare URL. if (manifestProtectionFallback) { - trace("i"); + trace("prot"); const allFound = manifestProtectionFallback; const githubServer = process.env.GITHUB_SERVER_URL || "https://github.com"; // Use head branch (branchName) for links when push succeeded; fall back to baseBranch @@ -2672,7 +2684,7 @@ async function main(config = {}) { let issue; let issueRepoParts; if (existingIssue) { - trace("j"); + trace("reus"); issueRepoParts = existingIssueMatch.issueRepoParts; const existingIssueBody = appendReportExecutionProvenance(protectedFallbackFrame(withProposalMetadata(fallbackBody)), process.env.GH_AW_WORKFLOW_NAME || "Workflow", ""); const existingIssueLabels = Array.isArray(existingIssue.labels) ? existingIssue.labels.map(label => (typeof label === "string" ? label : label?.name)).filter(label => typeof label === "string") : []; @@ -2697,7 +2709,7 @@ async function main(config = {}) { issue = { ...existingIssue, ...response.data }; core.info(`Updated protected-file-protection review issue #${issue.number}: ${issue.html_url}`); } else { - trace("k"); + trace("issu"); const result = await createFallbackIssue(githubClient, repoParts, title, protectedFallbackFrame(withProposalMetadata(fallbackBody)), issueLabels, configAssignees, transport.assertUnchanged); issue = result.data; issueRepoParts = result.issueRepoParts; @@ -2744,7 +2756,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { - trace("l"); + trace("ifal"); const error = `Protected file protection: failed to create review issue. Error: ${getErrorMessage(issueError)}`; core.error(error); return { success: false, error }; @@ -2753,7 +2765,7 @@ async function main(config = {}) { // Try to create the pull request, with fallback to issue creation try { - trace("m"); + trace("publ"); if (body.length > MAX_GITHUB_BODY_LENGTH) { throw new Error(`Pull request body exceeds GitHub's maximum length of ${MAX_GITHUB_BODY_LENGTH} characters`); } @@ -3000,7 +3012,7 @@ async function main(config = {}) { } // Return success with PR details - trace("u"); + trace("capt"); return await capturePullRequestDelivery( { success: true, @@ -3034,7 +3046,7 @@ async function main(config = {}) { githubClient ); } catch (prError) { - trace("n"); + trace("pber"); transport.assertUnchanged(); const errorMessage = getErrorMessage(prError); core.warning(`Failed to create pull request: ${errorMessage}`); @@ -3073,7 +3085,7 @@ async function main(config = {}) { }); try { - trace("o"); + trace("perm"); transport.assertUnchanged(); const { data: issue, issueRepoParts } = await createFallbackIssue(githubClient, repoParts, title, fallbackFrame(fallbackBody), mergeFallbackIssueLabels(effectiveFallbackLabels), configAssignees, transport.assertUnchanged); @@ -3092,7 +3104,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { - trace("p"); + trace("peif"); const error = `Failed to create pull request (permission denied) and failed to create fallback issue. PR error: ${errorMessage}. Issue error: ${getErrorMessage(issueError)}`; core.error(error); return { @@ -3105,7 +3117,7 @@ async function main(config = {}) { if (!fallbackAsIssue) { // Fallback is disabled - return error without creating issue - trace("q"); + trace("nofb"); core.error("fallback-as-issue is disabled - not creating fallback issue"); return { success: false, @@ -3114,7 +3126,7 @@ async function main(config = {}) { }; } - trace("r"); + trace("fbis"); core.info("Falling back to creating an issue instead"); // Create issue as fallback with enhanced body content @@ -3187,7 +3199,7 @@ async function main(config = {}) { head_repo: pushRepo, }; } catch (issueError) { - trace("s"); + trace("fber"); const error = `Failed to create both pull request and fallback issue. PR error: ${errorMessage}. Issue error: ${getErrorMessage(issueError)}`; core.error(error); return { diff --git a/actions/setup/js/create_pull_request.test.cjs b/actions/setup/js/create_pull_request.test.cjs index 33ce10155ce..8b81ecb262f 100644 --- a/actions/setup/js/create_pull_request.test.cjs +++ b/actions/setup/js/create_pull_request.test.cjs @@ -149,6 +149,62 @@ describe("create_pull_request - draft policy enforcement", () => { return global.core.warning.mock.calls.filter(args => String(args[0]).includes("Agent requested draft")); } + it.each([ + ["warn", "init_miss_fail"], + ["error", "init_miss_nerr_fail"], + ["ignore", "init_miss_ignr_fail"], + ])("traces the exercised %s policy when the patch is missing", async (ifNoChanges, expectedTrace) => { + const { main } = require("./create_pull_request.cjs"); + const handler = await main({ if_no_changes: ifNoChanges }); + + const result = await handler({ title: "Test PR" }, {}); + + expect(result.success).toBe(false); + expect(result.execution_trace).toBe(expectedTrace); + }); + + it.each(["error", "ignore"])("does not trace staged or %s no-change routes before repository validation", async ifNoChanges => { + const { main } = require("./create_pull_request.cjs"); + const handler = await main({ staged: true, if_no_changes: ifNoChanges }); + + const result = await handler({ title: "Test PR", repo: "other-owner/other-repo" }, {}); + + expect(result.success).toBe(false); + expect(result.execution_trace).toBe("init_repo_fail"); + expect(global.core.summary.addRaw).not.toHaveBeenCalled(); + }); + + it("traces the staged preview rather than the no-change policy for a missing patch", async () => { + const { main } = require("./create_pull_request.cjs"); + const handler = await main({ staged: true, if_no_changes: "error" }); + + const result = await handler({ title: "Test PR" }, {}); + + expect(result.staged).toBe(true); + expect(result.execution_trace).toBe("init_miss_stag_done"); + expect(global.core.summary.addRaw).toHaveBeenCalled(); + }); + + it("retains the empty-push failure route when transport integrity fails", async () => { + const patchPath = canonicalPatchPath("empty-push-integrity"); + fs.writeFileSync(patchPath, ""); + global.exec.exec.mockImplementation(async (cmd, args) => { + if (cmd === "git" && args[0] === "commit") { + fs.writeFileSync(patchPath, "tampered during empty commit"); + throw new Error("empty commit failed"); + } + return 0; + }); + const { main } = require("./create_pull_request.cjs"); + const handler = await main({ allow_empty: true }); + + const result = await handler({ title: "Test PR", branch: "empty-push-integrity" }, {}); + + expect(result.success).toBe(false); + expect(result.error).toContain("SECURITY:"); + expect(result.execution_trace).toBe("init_ecmt_eper_fail"); + }); + it("should propagate the created pull request's durable id and node_id", async () => { global.github.rest.pulls.create = vi.fn().mockResolvedValue({ data: { id: 555111222, node_id: "PR_kwDOtest456", number: 1, html_url: "https://github.com/test", head: { sha: "abc123" } }, @@ -160,7 +216,7 @@ describe("create_pull_request - draft policy enforcement", () => { expect(result.success).toBe(true); expect(result.id).toBe(555111222); - expect(result.execution_trace).toBe("AHIgmux"); + expect(result.execution_trace).toBe("init_miss_alwe_ecmt_publ_capt_done"); expect(result.metadata).toEqual({ node_id: "PR_kwDOtest456" }); }); @@ -611,7 +667,7 @@ index 0000000..abc1234 expect(result.success).toBe(false); expect(result.error).toContain("allow-full-history-fetch: true"); - expect(result.execution_trace).toBe("ARSy"); + expect(result.execution_trace).toBe("init_bund_bfal_fail"); expect(result.error).toContain("safe-outputs.create-pull-request"); const directFetch = global.exec.exec.mock.calls.find(([, args]) => Array.isArray(args) && args[0] === "fetch" && args.includes("origin") && args.includes(prereq)); expect(directFetch).toBeUndefined(); @@ -650,7 +706,7 @@ index 0000000..abc1234 expect(result.success).toBe(true); expect(pushSignedSpy).toHaveBeenCalledWith(expect.objectContaining({ signedCommits: false })); - expect(result.execution_trace).toBe("ARtmux"); + expect(result.execution_trace).toBe("init_bund_push_publ_capt_done"); }); it("should create the PR when the bundle push fails once with a transient workflows-scope timeout and succeeds on retry", async () => { @@ -688,7 +744,7 @@ index 0000000..abc1234 expect(result.success).toBe(true); expect(result.fallback_used).not.toBe(true); - expect(result.execution_trace).toBe("ARttmux"); + expect(result.execution_trace).toBe("init_bund_push_push_publ_capt_done"); expect(pushSignedSpy).toHaveBeenCalledTimes(2); expect(global.github.rest.pulls.create).toHaveBeenCalled(); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); @@ -800,7 +856,7 @@ index 0000000..abc1234 expect(result.fallback_used).not.toBe(true); expect(pushSignedSpy).toHaveBeenCalledTimes(2); expect(global.exec.exec).toHaveBeenCalledWith("git", ["reset", "--soft", "origin/main"]); - expect(result.execution_trace).toBe("ARtTmux"); + expect(result.execution_trace).toBe("init_bund_push_sign_publ_capt_done"); expect(global.exec.exec).toHaveBeenCalledWith("git", ["commit", "-m", "bundle merge headline"]); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); }); @@ -2212,7 +2268,7 @@ ${diffs} expect(result.success).toBe(false); expect(result.error).toContain("outside the allowed-files list"); - expect(result.execution_trace).toBe("AMy"); + expect(result.execution_trace).toBe("init_deny_fail"); expect(result.error).toContain("src/index.js"); }); @@ -2394,7 +2450,7 @@ ${diffs} const result = await handler({ title: "Test PR", body: "Test body", branch: "feature/protected-identical" }, {}); expect(result.issue_number).toBe(76); - expect(result.execution_trace).toBe("ANWijx"); + expect(result.execution_trace).toBe("init_fall_ptch_prot_reus_done"); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); expect(global.github.paginate).toHaveBeenCalledWith(global.github.rest.issues.listForRepo, expect.objectContaining({ owner: "test-owner", repo: "test-repo", state: "all", labels: "pull-request-fallback" })); const updateCall = global.github.rest.issues.update.mock.calls[0][0]; @@ -3471,17 +3527,18 @@ describe("create_pull_request - patch apply fallback to original base commit", ( return false; } - it("should create the PR branch from the patch-embedded base commit when available", async () => { + it.each(["warn", "error", "ignore"])("should create the PR branch from the patch-embedded base commit with %s no-change policy", async ifNoChanges => { global.exec = { exec: vi.fn().mockResolvedValue(0), getExecOutput: vi.fn().mockResolvedValue({ exitCode: 0, stdout: "", stderr: "" }), }; const { main } = require("./create_pull_request.cjs"); - const handler = await main({}); + const handler = await main({ if_no_changes: ifNoChanges }); const result = await handler({ title: "Test PR", body: "Test body", branch: "test-branch", base_commit: ` ${MOCK_BASE_COMMIT_SHA} ` }, {}); expect(result.success).toBe(true); + expect(result.execution_trace).toBe("init_ptch_publ_capt_done"); expect(global.exec.exec).toHaveBeenCalledWith("git", ["cat-file", "-e", MOCK_BASE_COMMIT_SHA]); const checkoutWithBaseCommit = global.exec.exec.mock.calls.find(([cmd, args]) => cmd === "git" && Array.isArray(args) && args[0] === "checkout" && args[1] === "-b" && args[3] === MOCK_BASE_COMMIT_SHA); expect(checkoutWithBaseCommit).toBeTruthy(); @@ -3567,7 +3624,7 @@ describe("create_pull_request - patch apply fallback to original base commit", ( expect(result.success).toBe(true); // Should warn that the PR will show merge conflicts - expect(result.execution_trace).toBe("AWXZmux"); + expect(result.execution_trace).toBe("init_ptch_pfal_rply_publ_capt_done"); expect(global.core.warning).toHaveBeenCalledWith(expect.stringContaining("merge conflicts")); }); @@ -5110,7 +5167,7 @@ describe("create_pull_request - E003 file-limit fallback-to-issue", () => { expect(global.github.rest.issues.create).toHaveBeenCalledTimes(1); expect(result.success).toBe(false); expect(result.error).toContain("SECURITY:"); - expect(result.execution_trace).toBe("AJy"); + expect(result.execution_trace).toBe("init_flim_fail"); }); it("should use the actual received file count (not maxFiles*2) as the suggested limit", async () => { @@ -5157,7 +5214,7 @@ describe("create_pull_request - E003 file-limit fallback-to-issue", () => { // Staged mode: no API side effects, just a preview expect(result.success).toBe(true); expect(result.staged).toBe(true); - expect(result.execution_trace).toBe("AQJx"); + expect(result.execution_trace).toBe("init_flim_stag_done"); expect(result.fallback_used).toBeUndefined(); expect(global.github.rest.issues.create).not.toHaveBeenCalled(); expect(global.github.rest.pulls.create).not.toHaveBeenCalled(); diff --git a/actions/setup/js/push_to_pull_request_branch.cjs b/actions/setup/js/push_to_pull_request_branch.cjs index ac7ad86bd3e..1f1a17e502f 100644 --- a/actions/setup/js/push_to_pull_request_branch.cjs +++ b/actions/setup/js/push_to_pull_request_branch.cjs @@ -570,31 +570,29 @@ async function main(config = {}) { * @param {import('./types/handler-factory').ResolvedTemporaryIds} resolvedTemporaryIds - Map of temporary IDs to resolved IDs * @returns {Promise} */ - // Trace map: A invocation; B count limit; C invalid repo; D object pack; - // E missing patch; F invalid patch; G oversized patch; H/I protection deny/fallback; - // J empty patch; K staged; L configured target; M triggering target; N wildcard target; - // O closed PR; P missing head; Q unauthorized head; R fork; S branch protection denied; - // T title/label validation; U protected issue; V missing remote; W fetch failure; - // X checkout failure; Y bundle; Z bundle failure; a octopus merge; - // b/c bundle protection deny/fallback; d patch; e patch failure; f add/add recovery; - // g/h post-apply deny/fallback; i blob size limit; j review PR; k review failure; - // l signed squash; m workflow scope; n push; o stale push; p push failure; - // q fallback PR; r fallback failure; s unchanged checkout; t missing SHA; - // u successful push; v/w no-changes error/ignore; x successful return; - // y unsuccessful/skipped return; z thrown error. + // Trace map: init invocation; limt count limit; repo invalid repo; pack object pack; + // miss missing patch; pgen invalid patch; size oversized patch; deny/fall protection deny/fallback; + // noop empty patch; stag staged; conf configured target; trig triggering target; wild wildcard target; + // clos closed PR; head missing head; auth unauthorized head; fork fork; bden branch protection denied; + // vldt title/label validation; prot protected issue; remt missing remote; ferr fetch failure; + // cerr checkout failure; bund bundle; bfal bundle failure; octo octopus merge; + // bpro/bfbk bundle protection deny/fallback; ptch patch; pfal patch failure; aadd add/add recovery; + // aden/afbk post-apply deny/fallback; blob blob size limit; revw review PR; rfal review failure; + // sqsh signed squash; scop workflow scope; push push; stal stale push; perr push failure; + // fbpr fallback PR; fber fallback failure; same empty-patch skip; nsha missing SHA; + // pshd successful push; nerr/ignr no-changes error/ignore; done successful return; + // fail unsuccessful/skipped return; thrw thrown error. return withExecutionTrace( - { success: "x", failure: "y", error: "z" }, + { success: "done", failure: "fail", error: "thrw" }, withTransportIntegrity - )(async function handlePushToPullRequestBranch(message, resolvedTemporaryIds, transport, trace) { - trace("A"); - if (ifNoChanges === "error") trace("v"); - if (ifNoChanges === "ignore") trace("w"); + )(async function handlePushToPullRequestBranch(trace, message, resolvedTemporaryIds, transport) { + trace("init"); const exec = transport.guardExec(globalThis.exec); let compilationNotice = getWorkflowCompilationNotice(message, config); if (compilationNotice) core.warning(compilationNotice); // Check max count if (maxCount > 0 && processedCount >= maxCount) { - trace("B"); + trace("limt"); core.info(`Skipping message - max count (${maxCount}) reached`); return { success: false, error: `Max count (${maxCount}) reached`, skipped: true }; } @@ -603,7 +601,7 @@ async function main(config = {}) { const repoResult = resolveAndValidateRepo(message, defaultTargetRepo, allowedRepos, "push to PR branch"); if (!repoResult.success) { - trace("C"); + trace("repo"); return { success: false, error: repoResult.error }; } const itemRepo = repoResult.repo; @@ -617,7 +615,7 @@ async function main(config = {}) { transport.captureOptional(transportPaths.bundlePath); const objectPackRecovery = {}; if (transportPaths.bundlePath && isObjectPack(transport.read(transportPaths.bundlePath))) { - trace("D"); + trace("pack"); const triggeringPR = context.payload?.pull_request; const aw = parseAwContext(context.payload?.inputs?.aw_context); const explicitPull = message.pull_request_number ?? message.pullRequestNumber ?? message.target ?? message.pr_number ?? message.pull_number ?? message.item_number ?? message.pr; @@ -681,13 +679,15 @@ async function main(config = {}) { // the first-pass validation input; bundle transport adds an authoritative // pre-apply git diff check later after the bundle ref has been fetched. if (!hasPatchFile) { - trace("E"); + trace("miss"); const msg = "No patch file found - cannot push without changes"; switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: msg }; case "ignore": + trace("ignr"); return { success: false, error: msg, skipped: true }; case "warn": default: @@ -705,7 +705,7 @@ async function main(config = {}) { // Check for actual error conditions if (patchContent.includes("Failed to generate patch")) { - trace("F"); + trace("pgen"); const msg = "Patch file contains error message - cannot push without changes"; core.error("Patch file generation failed"); core.error(`Patch file location: ${patchFilePath}`); @@ -755,7 +755,7 @@ async function main(config = {}) { core.info(`${sizeLabel}: ${sizeForCheckKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (sizeForCheckKb > maxSizeKb) { - trace("G"); + trace("size"); const msg = `Patch size (${sizeForCheckKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)${mergedBaseBaselineHint}`; return { error: { success: false, error: msg }, fallbackFiles }; } @@ -768,7 +768,7 @@ async function main(config = {}) { if (!isEmptyPatch) { const protection = checkFileProtection(patchContent, config); if (protection.action === "deny") { - trace("H"); + trace("deny"); const filesStr = protection.files.join(", "); const msg = protection.source === "allowlist" @@ -778,7 +778,7 @@ async function main(config = {}) { return { error: { success: false, skipped: true, reasonCode: POLICY_FILE_PROTECTION_DENIED_REASON_CODE, error: msg }, fallbackFiles }; } if (protection.action === "fallback") { - trace("I"); + trace("fall"); fallbackFiles.push(...protection.files); core.warning(`Protected file protection triggered (fallback-to-issue): ${protection.files.join(", ")}. Will create review issue instead of pushing.`); } @@ -795,13 +795,15 @@ async function main(config = {}) { } if (isEmpty) { - trace("J"); + trace("noop"); const msg = "Patch file is empty - no changes to apply (noop operation)"; switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: "No changes to push - failing as configured by if-no-changes: error" }; case "ignore": + trace("ignr"); return { success: false, error: msg, skipped: true }; case "warn": default: @@ -815,7 +817,7 @@ async function main(config = {}) { // If in staged mode, emit 🎭 Staged Mode Preview via generateStagedPreview if (isStaged) { - trace("K"); + trace("stag"); await generateStagedPreview({ title: "Push to PR Branch", description: "The following changes would be pushed if staged mode was disabled:", @@ -849,7 +851,7 @@ async function main(config = {}) { // Validate target configuration if (target !== "*" && target !== "triggering") { - trace("L"); + trace("conf"); const pullNumber = parsePositiveInteger(target); if (pullNumber === null) { return { success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number' }; @@ -859,7 +861,7 @@ async function main(config = {}) { // Compute the target branch name based on target configuration let pullNumber; if (target === "triggering") { - trace("M"); + trace("trig"); pullNumber = typeof context !== "undefined" ? context.payload?.pull_request?.number || context.payload?.issue?.number : undefined; if (!pullNumber) { const awContext = typeof context !== "undefined" ? parseAwContext(context.payload?.inputs?.aw_context) : null; @@ -880,7 +882,7 @@ async function main(config = {}) { }; } } else if (target === "*") { - trace("N"); + trace("wild"); const explicitPull = message.pull_request_number ?? message.pullRequestNumber ?? message.target ?? message.pr_number ?? message.pull_number ?? message.item_number ?? message.pr; if (explicitPull !== undefined && explicitPull !== null && explicitPull !== "") { pullNumber = parsePositiveInteger(explicitPull); @@ -952,7 +954,7 @@ async function main(config = {}) { }); pullRequest = response.data; if (pullRequest.state === "closed") { - trace("O"); + trace("clos"); const reason = `Pull request #${pullNumber} is closed; refusing to push to its branch`; core.info(reason); return { skipped: true, reason }; @@ -976,7 +978,7 @@ async function main(config = {}) { // SECURITY: When head.repo is null (likely a deleted fork) we cannot verify which // repository the PR head came from. Always reject to prevent writes to an unverifiable PR. if (pullRequest.head?.repo == null) { - trace("P"); + trace("head"); const nullHeadErr = "Cannot push to PR: head repository is null (likely a deleted fork)"; core.error(nullHeadErr); return { success: false, error: nullHeadErr }; @@ -987,7 +989,7 @@ async function main(config = {}) { // names an automation fork, and prevents writes to a fork PR when head-repo is not set. const expectedHeadRepo = configuredHeadRepo || itemRepo; if (actualHeadRepo && actualHeadRepo.toLowerCase() !== expectedHeadRepo.toLowerCase()) { - trace("Q"); + trace("auth"); const { isFork: actualIsFork } = detectForkPR(pullRequest); if (actualIsFork && !configuredHeadRepo) { core.error(`Cannot push to fork PR branch: head is '${actualHeadRepo}', not '${itemRepo}'`); @@ -1007,7 +1009,7 @@ async function main(config = {}) { // forks are eligible for updates. const { isFork, reason: forkReason } = detectForkPR(pullRequest); if (isFork) { - trace("R"); + trace("fork"); if (!configuredHeadRepo) { core.error(`Cannot push to fork PR branch: ${forkReason}`); core.error("Fork PRs remain blocked unless safe-outputs.push-to-pull-request-branch.head-repo is configured."); @@ -1068,13 +1070,13 @@ async function main(config = {}) { }, }); if (blockReason) { - trace("S"); + trace("bden"); core.error(blockReason); return { success: false, error: blockReason }; } } - trace("T"); + trace("vldt"); // Validate title prefix if specified if (titlePrefix && !prTitle.startsWith(titlePrefix)) { return { success: false, error: `Pull request title "${prTitle}" does not start with required prefix "${titlePrefix}"` }; @@ -1096,7 +1098,7 @@ async function main(config = {}) { } const createProtectedFilesFallbackIssue = async files => { - trace("U"); + trace("prot"); transport.assertUnchanged(); const runUrl = buildWorkflowRunUrl(context, context.repo); const runId = context.runId; @@ -1194,7 +1196,7 @@ async function main(config = {}) { ); if (lsRemoteResult.exitCode === 2) { - trace("V"); + trace("remt"); const missingBranchError = MISSING_BRANCH_ERROR_TEMPLATE(branchName); if (ignoreMissingBranchFailure) { core.warning(`${missingBranchError} Skipping as configured by ignore-missing-branch-failure.`); @@ -1234,7 +1236,7 @@ async function main(config = {}) { baseGitOpts.cwd ); } catch (fetchError) { - trace("W"); + trace("ferr"); const fetchErrorMessage = getErrorMessage(fetchError); if (ignoreMissingBranchFailure && looksLikeMissingRemoteBranchError(fetchErrorMessage)) { const missingBranchError = MISSING_BRANCH_ERROR_TEMPLATE(branchName); @@ -1261,7 +1263,7 @@ async function main(config = {}) { await exec.exec("git", ["checkout", "-B", branchName, branchRemoteRef], baseGitOpts); core.info(`Checked out existing branch from ${pushRepo}: ${branchName}`); } catch (checkoutError) { - trace("X"); + trace("cerr"); return { success: false, error: `Failed to checkout branch ${branchName}: ${getErrorMessage(checkoutError)}` }; } @@ -1323,7 +1325,7 @@ async function main(config = {}) { } if (hasBundleFile) { - trace("Y"); + trace("bund"); // Bundle transport: import commits directly from the local bundle file. // This preserves merge commit topology and per-commit metadata. core.info(`Applying changes from bundle: ${bundleFilePath}`); @@ -1384,7 +1386,7 @@ async function main(config = {}) { if (deferBundleValidation) { const octopus = await exec.getExecOutput("git", ["rev-list", "--min-parents=3", bundleRef, "--not", ...agentHistoryExclusions], bundleGitOpts); if (octopus.stdout.trim()) { - trace("a"); + trace("octo"); return { success: false, error: "Cannot validate agent-created octopus merge commits in the bundle. Use two-parent merges instead." }; } // Preserve changes that were later reverted. Ancestor reachability @@ -1396,7 +1398,7 @@ async function main(config = {}) { core.info(`Pre-apply bundle verification: ${bundleFiles.length} file(s) detected from bundle transport`); const bundleProtection = checkFileProtectionPostApply(bundleFiles, config); if (bundleProtection.action === "deny") { - trace("b"); + trace("bpro"); const filesStr = bundleProtection.files.join(", "); const msg = bundleProtection.source === "post-apply" @@ -1406,7 +1408,7 @@ async function main(config = {}) { return { success: false, skipped: true, reasonCode: POLICY_FILE_PROTECTION_DENIED_REASON_CODE, error: msg }; } if (bundleProtection.action === "fallback") { - trace("c"); + trace("bfbk"); core.warning(`Protected file protection triggered (fallback-to-issue): ${bundleProtection.files.join(", ")}. Will create review issue instead of pushing.`); return await createProtectedFilesFallbackIssue(bundleProtection.files); } @@ -1433,7 +1435,7 @@ async function main(config = {}) { // Non-fatal cleanup } } catch (bundleError) { - trace("Z"); + trace("bfal"); transport.assertUnchanged(); core.error(`Failed to apply bundle: ${getErrorMessage(bundleError)}`); // Clean up temp ref if it exists @@ -1446,7 +1448,7 @@ async function main(config = {}) { } } else { // Patch transport (non-default): git am --3way - trace("d"); + trace("ptch"); core.info("Applying patch..."); try { if (commitTitleSuffix) { @@ -1477,8 +1479,8 @@ async function main(config = {}) { await exec.exec("git", ["am", "--3way", patchFilePath], baseGitOpts); core.info("Patch applied successfully"); } catch (error) { + trace("pfal"); transport.assertUnchanged(); - trace("e"); core.warning(`Initial patch apply failed, attempting add/add recovery: ${getErrorMessage(error)}`); let recoveredFromAddAddConflict = false; @@ -1519,7 +1521,7 @@ async function main(config = {}) { } if (recoveredFromAddAddConflict) { - trace("f"); + trace("aadd"); // Continue with normal push flow } else { core.error(`Failed to apply patch: ${getErrorMessage(error)}`); @@ -1571,7 +1573,7 @@ async function main(config = {}) { core.info(`Post-apply verification: ${protectionFiles.length} file(s) actually modified`); const postApplyProtection = checkFileProtectionPostApply(protectionFiles, config); if (postApplyProtection.action === "deny") { - trace("g"); + trace("aden"); const filesStr = postApplyProtection.files.join(", "); const msg = `SECURITY: Post-apply file-protection check failed. ` + `The patch applied files that were not detected by the pre-apply parser: ${filesStr}. ` + `This may indicate a patch-parser bypass attempt. Aborting push.`; core.error(msg); @@ -1580,7 +1582,7 @@ async function main(config = {}) { return { success: false, error: msg }; } if (postApplyProtection.action === "fallback") { - trace("h"); + trace("afbk"); core.warning(`Post-apply: Protected file protection triggered (fallback-to-issue): ${postApplyProtection.files.join(", ")}`); await exec.exec("git", ["reset", "--hard", rangeBaseRef], baseGitOpts); return await createProtectedFilesFallbackIssue(postApplyProtection.files); @@ -1591,7 +1593,7 @@ async function main(config = {}) { const changedBlobSizeKb = Math.ceil(changedBlobSizeBytes / 1024); core.info(`Changed content size: ${changedBlobSizeKb} KB (maximum allowed: ${maxSizeKb} KB)`); if (changedBlobSizeKb > maxSizeKb) { - trace("i"); + trace("blob"); const msg = `Changed content size (${changedBlobSizeKb} KB) exceeds maximum allowed size (${maxSizeKb} KB)${mergedBaseBaselineHint}`; await exec.exec("git", ["reset", "--hard", rangeBaseRef], baseGitOpts); return { success: false, error: msg }; @@ -1604,7 +1606,7 @@ async function main(config = {}) { // before they are merged into the target PR. const detectionConclusionEnv = getEffectiveDetectionConclusion(process.env.GH_AW_DETECTION_CONCLUSION, process.env.GH_AW_DETECTION_REASON); if (detectionConclusionEnv === "warning") { - trace("j"); + trace("revw"); core.info("⚠️ Threat detection warning: creating review PR instead of direct push"); // Create a review branch name based on the original branch, using @@ -1721,7 +1723,7 @@ async function main(config = {}) { pr_url: reviewPR.html_url, }; } catch (reviewError) { - trace("k"); + trace("rfal"); transport.assertUnchanged(); core.error(`Failed to create review PR: ${getErrorMessage(reviewError)}`); return { success: false, error: `Failed to create review PR: ${getErrorMessage(reviewError)}` }; @@ -1737,7 +1739,7 @@ async function main(config = {}) { const { stdout: mergeCountOut } = await exec.getExecOutput("git", ["rev-list", "--merges", "--count", `${squashBase}..HEAD`], baseGitOpts); const mergeCount = parseInt(mergeCountOut.trim(), 10); if (Number.isFinite(mergeCount) && mergeCount > 0) { - trace("l"); + trace("sqsh"); core.warning( `push_to_pull_request_branch: detected ${mergeCount} merge commit(s) in range ${squashBase}..HEAD. ` + `Merge commits cannot be pushed as signed commits. Squashing the range into a single regular commit to preserve content. ` + @@ -1763,7 +1765,7 @@ async function main(config = {}) { const agentWorkflowFiles = agentChangedFiles.filter(isWorkflowYamlFile); if (!allowWorkflows && agentWorkflowFiles.length > 0) { - trace("m"); + trace("scop"); return buildWorkflowsScopeError("Branch", core); } @@ -1774,7 +1776,7 @@ async function main(config = {}) { // scope may be required." — a transient server-side condition, not a real scope problem. // Retry with exponential backoff before treating the push as failed. const pushedSha = await withTransientPushRetry(async () => { - trace("n"); + trace("push"); const staleReason = await getStalePullRequestReason(githubClient, { repo: repoParts, payload: { pull_request: { number: pullNumber, state: "open" } } }); if (staleReason) throw new StalePullRequestError(staleReason); transport.assertUnchanged(); @@ -1801,13 +1803,13 @@ async function main(config = {}) { pushedCommitSha = pushedSha; core.info(`pushSignedCommits returned pushed SHA: ${pushedSha}`); } - trace("u"); + trace("pshd"); core.info(`Changes committed and pushed to branch: ${branchName}`); } catch (pushError) { - trace("p"); + trace("perr"); transport.assertUnchanged(); if (pushError instanceof StalePullRequestError) { - trace("o"); + trace("stal"); core.info(pushError.message); return { skipped: true, reason: pushError.message }; } @@ -1847,7 +1849,7 @@ async function main(config = {}) { // Fallback path for diverged branches: create a new pull request so changes // can still be reviewed and merged into the original PR branch. if ((isNonFastForward || isWorkflowsScopeTimeout) && fallbackAsPullRequest && allowGitPushFallback) { - trace("q"); + trace("fbpr"); const staleReason = await getStalePullRequestReason(githubClient, { repo: repoParts, payload: { pull_request: { number: pullNumber, state: "open" } } }); if (staleReason) { if (isNonFastForward) { @@ -1941,7 +1943,7 @@ async function main(config = {}) { url: fallbackPR.html_url, }; } catch (fallbackError) { - trace("r"); + trace("fber"); transport.assertUnchanged(); const fallbackErrorMessage = getErrorMessage(fallbackError); core.error(`Failed to create fallback pull request: ${fallbackErrorMessage}`); @@ -1965,15 +1967,17 @@ async function main(config = {}) { } } } else { - trace("s"); + trace("same"); core.info("Skipping patch application (empty patch)"); const msg = "No changes to apply - noop operation completed successfully"; switch (ifNoChanges) { case "error": + trace("nerr"); return { success: false, error: "No changes to apply - failing as configured by if-no-changes: error" }; case "ignore": + trace("ignr"); // Silent success break; case "warn": @@ -1989,7 +1993,7 @@ async function main(config = {}) { if (!commitSha) { const commitShaRes = await exec.getExecOutput("git", ["rev-parse", "HEAD"], baseGitOpts); if (commitShaRes.exitCode !== 0) { - trace("t"); + trace("nsha"); return { success: false, error: "Failed to get commit SHA" }; } commitSha = commitShaRes.stdout.trim(); diff --git a/actions/setup/js/push_to_pull_request_branch.test.cjs b/actions/setup/js/push_to_pull_request_branch.test.cjs index 6abfbd3f66e..bb858e49ff4 100644 --- a/actions/setup/js/push_to_pull_request_branch.test.cjs +++ b/actions/setup/js/push_to_pull_request_branch.test.cjs @@ -531,7 +531,8 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({ target }); const result = await handler({ branch }, {}); - expect(result).toEqual({ success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number', execution_trace: "ALy" }); + expect(result).toEqual({ success: false, error: 'Invalid target configuration: must be "triggering", "*", or a valid pull request number' }); + expect(result.execution_trace).toBe("init_conf_fail"); expect(mockGithub.rest.pulls.get).not.toHaveBeenCalled(); }); @@ -545,7 +546,8 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({ target: "*" }); const result = await handler({ branch, [alias]: value }, {}); - expect(result).toEqual({ success: false, error: "Pull request number is required but not found", execution_trace: "ANy" }); + expect(result).toEqual({ success: false, error: "Pull request number is required but not found" }); + expect(result.execution_trace).toBe("init_wild_fail"); expect(mockGithub.rest.pulls.get).not.toHaveBeenCalled(); } ); @@ -1162,7 +1164,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 // ────────────────────────────────────────────────────── describe("repository state scenarios", () => { - it("should handle successful normal push", async () => { + it.each(["warn", "error", "ignore"])("should handle successful normal push with %s no-change policy", async ifNoChanges => { const patchPath = createPatchFile("should-handle-successful-normal-push"); mockExec.getExecOutput .mockResolvedValueOnce({ exitCode: 0, stdout: "preflight-sha\trefs/heads/feature-branch\n", stderr: "" }) @@ -1171,14 +1173,14 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 .mockResolvedValue({ exitCode: 0, stdout: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", stderr: "" }); const module = await loadModule(); - const handler = await module.main({}); + const handler = await module.main({ if_no_changes: ifNoChanges }); const result = await handler({ branch: "should-handle-successful-normal-push" }, {}); expect(result.success).toBe(true); expect(result.branch_name).toBe("feature-branch"); expect(result.commit_url).toContain("test-owner/test-repo/commit/"); expect(result.before_state).toEqual({ head_sha: "remote-head-before" }); - expect(result.execution_trace).toBe("AMTdnux"); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_push_pshd_done"); expect(result.after_state).toEqual({ head_sha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }); }); @@ -1196,7 +1198,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.skipped).not.toBe(true); expect(result.error).toContain("Authentication failed"); - expect(result.execution_trace).toBe("AMTdnpy"); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_push_perr_fail"); } finally { pushSignedSpy.mockRestore(); } @@ -1339,7 +1341,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.error).toContain("no longer exists on origin"); - expect(result.execution_trace).toBe("AMTVy"); + expect(result.execution_trace).toBe("init_trig_vldt_remt_fail"); expect(mockExec.exec).not.toHaveBeenCalled(); }); @@ -1468,6 +1470,26 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(mockCore.info).toHaveBeenCalledWith("Investigating patch failure..."); }); + it("retains the patch-failure route when transport integrity fails", async () => { + const patchPath = createPatchFile("patch-apply-integrity"); + mockExec.getExecOutput.mockResolvedValue({ exitCode: 0, stdout: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", stderr: "" }); + mockExec.exec.mockImplementation(async (cmd, args) => { + if (cmd === "git" && args[0] === "am" && args.includes("--3way")) { + fs.writeFileSync(patchPath, "tampered during patch application"); + throw new Error("patch application failed"); + } + return 0; + }); + const module = await loadModule(); + const handler = await module.main({}); + + const result = await handler({ branch: "patch-apply-integrity" }, {}); + + expect(result.success).toBe(false); + expect(result.error).toContain("SECURITY:"); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_pfal_fail"); + }); + it("should resolve add/add conflicts by preferring patch version and continuing git am", async () => { const patchPath = createPatchFile("should-resolve-add-add-conflicts-by-preferring-patch-version"); const pushSignedCommitsModule = require("./push_signed_commits.cjs"); @@ -1514,7 +1536,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(true); expect(mockCore.info).toHaveBeenCalledWith("Patch applied successfully after resolving add/add conflict(s)"); - expect(result.execution_trace).toBe("AMTdefnux"); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_pfal_aadd_push_pshd_done"); expect(mockExec.exec).toHaveBeenCalledWith("git", ["checkout", "--theirs", "--", "docs/findings.md"], expect.any(Object)); expect(mockExec.exec).toHaveBeenCalledWith("git", ["add", "--", "docs/findings.md"], expect.any(Object)); expect(mockExec.exec).toHaveBeenCalledWith("git", ["am", "--continue"], expect.any(Object)); @@ -1530,7 +1552,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.fallback_used).toBe(true); expect(result.fallback_type).toBe("pull_request"); expect(result.pull_request_number).toBe(999); - expect(result.execution_trace).toBe("AMTdnpqx"); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_push_perr_fbpr_done"); expect(mockGithub.rest.pulls.create).toHaveBeenCalled(); }); @@ -1588,7 +1610,8 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({}); const result = await handler({ branch }, {}); - expect(result).toEqual({ skipped: true, reason: expect.stringContaining("Pull request #123 closed"), execution_trace: "AMTdnpqy" }); + expect(result).toEqual({ skipped: true, reason: expect.stringContaining("Pull request #123 closed") }); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_push_perr_fbpr_fail"); expect(pushSignedSpy).toHaveBeenCalledTimes(1); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockExec.exec.mock.calls.some(([, args]) => args[0] === "checkout" && args[1] === "-b")).toBe(false); @@ -1621,7 +1644,12 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const handler = await module.main({}); const result = await handler({ branch }, {}); - expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(pushError), execution_trace: pushError.includes("`workflows`") ? "AMTdnpqy" : "AMTdnpy" }); + expect(result).toEqual({ + success: false, + error_type: "push_failed", + error: expect.stringContaining(pushError), + }); + expect(result.execution_trace).toBe(pushError.includes("`workflows`") ? "init_trig_vldt_ptch_push_perr_fbpr_fail" : "init_trig_vldt_ptch_push_perr_fail"); expect(pushSignedSpy).toHaveBeenCalledTimes(1); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining(pushError)); @@ -1814,7 +1842,8 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const result = await resultPromise; expect(pushSignedSpy).toHaveBeenCalledTimes(6); - expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT), execution_trace: "AMTdnnnnnnpqy" }); + expect(result).toEqual({ success: false, error_type: "push_failed", error: expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT) }); + expect(result.execution_trace).toBe("init_trig_vldt_ptch_push_push_push_push_push_push_perr_fbpr_fail"); expect(mockGithub.rest.pulls.create).not.toHaveBeenCalled(); expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining(TRANSIENT_WORKFLOWS_SCOPE_TIMEOUT)); } finally { @@ -2673,7 +2702,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.skipped).toBe(true); expect(mockCore.info).toHaveBeenCalledWith(expect.stringContaining("noop")); - expect(result.execution_trace).toBe("AJy"); + expect(result.execution_trace).toBe("init_noop_fail"); }); it("should error when patch is empty and if-no-changes is error", async () => { @@ -2685,7 +2714,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(false); expect(result.error).toContain("No changes"); - expect(result.execution_trace).toBe("AvJy"); + expect(result.execution_trace).toBe("init_noop_nerr_fail"); }); it("should silently skip when patch is empty and if-no-changes is ignore", async () => { @@ -2696,17 +2725,32 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 const result = await handler({ branch: "should-silently-skip-when-patch-is-empty-and-if-no-changes-i" }, {}); expect(result.success).toBe(false); - expect(result.execution_trace).toBe("AwJy"); + expect(result.execution_trace).toBe("init_noop_ignr_fail"); expect(result.skipped).toBe(true); }); - it("should handle missing patch file", async () => { + it.each([ + ["warn", "init_miss_fail"], + ["error", "init_miss_nerr_fail"], + ["ignore", "init_miss_ignr_fail"], + ])("should handle missing patch file with %s no-change policy", async (ifNoChanges, expectedTrace) => { const module = await loadModule(); - const handler = await module.main({}); - const result = await handler({ branch: "should-handle-missing-patch-file" }, {}); + const handler = await module.main({ if_no_changes: ifNoChanges }); + const result = await handler({}, {}); expect(result.success).toBe(false); expect(result.error).toContain("No patch file"); + expect(result.execution_trace).toBe(expectedTrace); + }); + + it.each(["error", "ignore"])("does not trace the %s no-change policy before repository validation", async ifNoChanges => { + const module = await loadModule(); + const handler = await module.main({ if_no_changes: ifNoChanges }); + + const result = await handler({ repo: "other-owner/other-repo" }, {}); + + expect(result.success).toBe(false); + expect(result.execution_trace).toBe("init_repo_fail"); }); it("should handle patch file with error message", async () => { @@ -3894,7 +3938,7 @@ index 0000000..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4 expect(result.success).toBe(true); expect(result.staged).toBe(true); - expect(result.execution_trace).toBe("AKx"); + expect(result.execution_trace).toBe("init_stag_done"); // Should not have made any git commands expect(mockExec.exec).not.toHaveBeenCalled(); }); diff --git a/actions/setup/js/safe_output_handler_manager.cjs b/actions/setup/js/safe_output_handler_manager.cjs index 6ca9da2d355..fff9b0ed3e3 100644 --- a/actions/setup/js/safe_output_handler_manager.cjs +++ b/actions/setup/js/safe_output_handler_manager.cjs @@ -1654,6 +1654,7 @@ async function processMessages(messageHandlers, messages, onItemCreated = null, success: false, deferred: false, error: errorMsg, + ...projectSafeOutputData({ execution_trace: result.execution_trace }), result: deferred.type === "merge_pull_request" ? projectMergeFailure(result) : { ...result, success: false, deferred: false, error: errorMsg }, }; } diff --git a/actions/setup/js/safe_output_outcomes.cjs b/actions/setup/js/safe_output_outcomes.cjs index 552547f1545..a7f42cf3229 100644 --- a/actions/setup/js/safe_output_outcomes.cjs +++ b/actions/setup/js/safe_output_outcomes.cjs @@ -71,7 +71,7 @@ function projectSafeOutputData(source) { if (source.identifier === null) data.identifier = null; if (OUTCOME_STATUSES.has(source.status)) data.status = source.status; if (typeof source.staged === "boolean") data.staged = source.staged; - if (typeof source.execution_trace === "string" && /^[A-Za-z]{1,1024}$/.test(source.execution_trace)) data.execution_trace = source.execution_trace; + if (typeof source.execution_trace === "string" && source.execution_trace.length <= 1024 && /^[A-Za-z]{4}(?:_[A-Za-z]{4})*$/.test(source.execution_trace)) data.execution_trace = source.execution_trace; if (source.operation_outcome === "indeterminate") data.operation_outcome = "indeterminate"; const url = canonicalSafeOutputUrl(source.url); if (url) data.url = url; diff --git a/actions/setup/js/safe_output_outcomes.test.cjs b/actions/setup/js/safe_output_outcomes.test.cjs index a46677c4e70..177262f350f 100644 --- a/actions/setup/js/safe_output_outcomes.test.cjs +++ b/actions/setup/js/safe_output_outcomes.test.cjs @@ -61,22 +61,31 @@ describe("safe-output application outcomes", () => { }); it.each(["success", "failed", "skipped", "staged"])("retains the execution path for %s outcomes", status => { - const result = { success: status !== "failed", skipped: status === "skipped", staged: status === "staged", execution_trace: "ABca" }; + const result = { success: status !== "failed", skipped: status === "skipped", staged: status === "staged", execution_trace: "init_ptch_done" }; const [outcome] = buildSafeOutputOutcomes([{ type: "create_pull_request", success: result.success, result }]); - expect(outcome).toMatchObject({ status, execution_trace: "ABca" }); - expect(projectSafeOutputData(outcome).execution_trace).toBe("ABca"); + expect(outcome).toMatchObject({ status, execution_trace: "init_ptch_done" }); + expect(projectSafeOutputData(outcome).execution_trace).toBe("init_ptch_done"); }); - it.each(["", "A".repeat(1025), "AB\n", "a/b", "route=private", 123, ["AB"]])("omits invalid execution traces: %j", execution_trace => { - expect(projectSafeOutputData({ execution_trace })).not.toHaveProperty("execution_trace"); - expect(buildSafeOutputOutcomes([{ success: true, result: { execution_trace } }])[0]).not.toHaveProperty("execution_trace"); + it.each(["", "push_".repeat(205) + "done", "ABc", "init_done\n", "_init", "init_", "init__done", "init_don", "init_donee", "init_d0ne", "init/done", "route=private", 123, ["init"]])( + "omits invalid execution traces: %j", + execution_trace => { + expect(projectSafeOutputData({ execution_trace })).not.toHaveProperty("execution_trace"); + expect(buildSafeOutputOutcomes([{ success: true, result: { execution_trace } }])[0]).not.toHaveProperty("execution_trace"); + } + ); + + it("retains complete traces at the length limit", () => { + const execution_trace = "push_".repeat(204) + "done"; + expect(execution_trace).toHaveLength(1024); + expect(projectSafeOutputData({ execution_trace }).execution_trace).toBe(execution_trace); }); it("persists isolated handler traces through processing and unified-session collection, including failures", async () => { const root = fs.mkdtempSync("/tmp/safe-output-traces-"); vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn(), setOutput: vi.fn(), setFailed: vi.fn() }); vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); - const traces = ["AB", "AC", "AD", "AE", "AF"]; + const traces = ["init_done", "init_fail", "init_thrw", "init_noop_fail", "init_stag_done"]; try { for (const type of ["create_pull_request", "push_to_pull_request_branch"]) { const handler = async message => { @@ -91,7 +100,7 @@ describe("safe-output application outcomes", () => { }; }; // Agent-request fields must never substitute for handler execution evidence. - const messages = traces.map((_, scenario) => ({ type, scenario, execution_trace: "FORGED" })); + const messages = traces.map((_, scenario) => ({ type, scenario, execution_trace: "fake_done" })); const processing = await processMessages(new Map([[type, handler]]), messages); writeSafeOutputOutcomes(processing.results, false, path.join(root, type, "safe-output-outcomes.jsonl")); const events = writeUnifiedSession({ rootDir: path.join(root, type), graders: [] }); @@ -99,7 +108,7 @@ describe("safe-output application outcomes", () => { expect(outcomes.map(event => event.data.execution_trace)).toEqual(traces); expect(outcomes.map(event => event.data.status)).toEqual(["success", "failed", "failed", "skipped", "staged"]); const persisted = fs.readFileSync(path.join(root, type, "usage/aw_session.jsonl"), "utf8"); - expect(persisted).not.toContain("FORGED"); + expect(persisted).not.toContain("fake_done"); expect(persisted).not.toContain("PRIVATE"); expect(validateSession(persisted)).toBe(events.length); } @@ -113,10 +122,10 @@ describe("safe-output application outcomes", () => { vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); const handler = vi .fn() - .mockResolvedValueOnce({ deferred: true, execution_trace: "AB" }) - .mockRejectedValueOnce(Object.assign(new Error("PRIVATE_ERROR"), { execution_trace: "AC" })); + .mockResolvedValueOnce({ deferred: true, execution_trace: "init_wait" }) + .mockRejectedValueOnce(Object.assign(new Error("PRIVATE_ERROR"), { execution_trace: "init_thrw" })); const processing = await processMessages(new Map([["create_pull_request", handler]]), [{ type: "create_pull_request" }]); - expect(buildSafeOutputOutcomes(processing.results)[0]).toMatchObject({ status: "failed", execution_trace: "AC" }); + expect(buildSafeOutputOutcomes(processing.results)[0]).toMatchObject({ status: "failed", execution_trace: "init_thrw" }); }); it("records queued ledger appends as delegated until persistence", () => { diff --git a/actions/setup/js/safe_output_trace.cjs b/actions/setup/js/safe_output_trace.cjs index 487995b2943..7fdff1d0654 100644 --- a/actions/setup/js/safe_output_trace.cjs +++ b/actions/setup/js/safe_output_trace.cjs @@ -4,8 +4,10 @@ const MAX_EXECUTION_TRACE_LENGTH = 1024; /** * Bind the trace inside each invocation so nested helpers share only that - * invocation's trace. Markers must be stable, unique source literals per handler. + * invocation's trace. Markers must be stable, unique four-letter source mnemonics per handler. * The outer wrapper also observes failures converted by transport integrity. + * The callback receives trace first, followed by any wrapped handler arguments. + * Trace metadata is read explicitly by outcome projection, not spread into payloads. * * @param {{success: string, failure: string, error: string}} terminal * @param {(handler: (...args: any[]) => any) => (...args: any[]) => any} [wrap] @@ -17,23 +19,23 @@ function withExecutionTrace(terminal, wrap = handler => handler) { async function (...args) { let path = ""; const trace = marker => { - if (typeof marker === "string" && /^[A-Za-z]$/.test(marker) && path.length < MAX_EXECUTION_TRACE_LENGTH - 1) path += marker; + if (typeof marker === "string" && /^[A-Za-z]{4}$/.test(marker) && path.length + (path ? 5 : 4) <= MAX_EXECUTION_TRACE_LENGTH - 5) path += `${path ? "_" : ""}${marker}`; }; const attach = value => { if ((value !== null && typeof value === "object") || typeof value === "function") { try { - Object.defineProperty(value, "execution_trace", { value: path, configurable: true, enumerable: true, writable: true }); + Object.defineProperty(value, "execution_trace", { value: path, configurable: true, enumerable: false, writable: true }); } catch { // Frozen errors/results must not turn observability into a new failure. } } }; const finish = marker => { - // Reserve one byte for the terminal outcome even when a route is bounded. - if (typeof marker === "string" && /^[A-Za-z]$/.test(marker)) path += marker; + // Reserve a separator and complete terminal marker even when a route is bounded. + if (typeof marker === "string" && /^[A-Za-z]{4}$/.test(marker)) path += `${path ? "_" : ""}${marker}`; }; try { - const result = await wrap((...handlerArgs) => handler.call(this, ...handlerArgs, trace)).apply(this, args); + const result = await wrap((...handlerArgs) => handler.call(this, trace, ...handlerArgs)).apply(this, args); finish(result?.success === false || result?.skipped ? terminal.failure : terminal.success); attach(result); return result; diff --git a/actions/setup/js/safe_output_trace.test.cjs b/actions/setup/js/safe_output_trace.test.cjs index 96faa9fe985..6234a12daf8 100644 --- a/actions/setup/js/safe_output_trace.test.cjs +++ b/actions/setup/js/safe_output_trace.test.cjs @@ -1,63 +1,126 @@ -import { describe, it, expect, vi } from "vitest"; -import fs from "node:fs"; -import path from "node:path"; +import { afterEach, describe, it, expect, vi } from "vitest"; const { withExecutionTrace, MAX_EXECUTION_TRACE_LENGTH } = require("./safe_output_trace.cjs"); const { withTransportIntegrity, TransportIntegrityError } = require("./transport_integrity.cjs"); -const terminal = { success: "x", failure: "y", error: "z" }; +const { processMessages } = require("./safe_output_handler_manager.cjs"); +const { buildSafeOutputOutcomes } = require("./safe_output_outcomes.cjs"); +const terminal = { success: "done", failure: "fail", error: "thrw" }; + +afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); +}); describe("safe-output execution traces", () => { it("preserves result identity, arguments, receiver and nested branch order", async () => { const result = { success: true }; const receiver = { value: 42 }; - const handler = withExecutionTrace(terminal)(async function (message, ids, trace) { + const handler = withExecutionTrace(terminal)(async function (trace, message, ids) { expect(this).toBe(receiver); expect(message).toEqual({ value: 42 }); expect(ids).toEqual({ temp: 1 }); - trace("A"); - const nested = () => trace("b"); + trace("init"); + const nested = () => trace("bund"); nested(); - trace("C"); + trace("push"); return result; }); expect(await handler.call(receiver, { value: 42 }, { temp: 1 })).toBe(result); - expect(result.execution_trace).toBe("AbCx"); - expect(JSON.parse(JSON.stringify(result)).execution_trace).toBe("AbCx"); + expect(result.execution_trace).toBe("init_bund_push_done"); + expect({ ...result }).toEqual({ success: true }); + expect(JSON.parse(JSON.stringify(result))).toEqual({ success: true }); + expect(buildSafeOutputOutcomes([{ type: "create_pull_request", success: true, result }])[0].execution_trace).toBe("init_bund_push_done"); + }); + + it.each([false, true])("keeps tracing in a fixed position when the temporary-ID map is supplied: %s", async includeMap => { + const message = { type: "create_pull_request" }; + const ids = { temp: 1 }; + const map = new Map(Object.entries(ids)); + const handler = withExecutionTrace(terminal)(async (trace, receivedMessage, receivedIds, receivedMap) => { + expect(receivedMessage).toBe(message); + expect(receivedIds).toBe(ids); + expect(receivedMap).toBe(includeMap ? map : undefined); + trace("init"); + return { success: true }; + }); + const result = await handler(message, ids, ...(includeMap ? [map] : [])); + expect(result.execution_trace).toBe("init_done"); + }); + + it.each(["success", "failed", "skipped", "thrown"])("projects non-enumerable traces through processMessages for %s handlers", async scenario => { + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn() }); + vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); + const handler = withExecutionTrace(terminal)(async (trace, message, ids, temporaryIdMap) => { + expect(message.type).toBe("create_pull_request"); + expect(ids).toEqual({}); + expect(temporaryIdMap).toBeInstanceOf(Map); + trace("init"); + if (scenario === "thrown") throw new Error("failed"); + return { success: scenario !== "failed", ...(scenario === "skipped" ? { skipped: true } : {}) }; + }); + const processing = await processMessages(new Map([["create_pull_request", handler]]), [{ type: "create_pull_request" }]); + expect(buildSafeOutputOutcomes(processing.results)[0]).toMatchObject({ + status: scenario === "thrown" ? "failed" : scenario, + execution_trace: `init_${scenario === "success" ? "done" : scenario === "thrown" ? "thrw" : "fail"}`, + }); + }); + + it("preserves the terminal trace when a deferred retry returns an unsuccessful result", async () => { + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn() }); + vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); + let invocation = 0; + const handler = vi.fn( + withExecutionTrace(terminal)(async trace => { + trace("init"); + if (++invocation === 1) { + trace("wait"); + return { deferred: true }; + } + trace("push"); + return { success: false, error: "retry failed" }; + }) + ); + const processing = await processMessages(new Map([["create_pull_request", handler]]), [{ type: "create_pull_request" }]); + expect(handler).toHaveBeenCalledTimes(2); + expect(processing.results[0]).toMatchObject({ success: false, deferred: false, execution_trace: "init_push_fail" }); + expect(buildSafeOutputOutcomes(processing.results)[0]).toMatchObject({ status: "failed", execution_trace: "init_push_fail" }); }); it.each([{ success: false }, { skipped: true }, { success: true, skipped: true }])("marks unsuccessful/skipped returns: %j", async result => { const handler = withExecutionTrace(terminal)(async trace => { - trace("A"); + trace("init"); return result; }); expect(await handler()).toBe(result); - expect(result.execution_trace).toBe("Ay"); + expect(result.execution_trace).toBe("init_fail"); }); it("preserves thrown Error identity, type, message and cause", async () => { const cause = new Error("cause"); const error = new TypeError("original", { cause }); const handler = withExecutionTrace(terminal)(async trace => { - trace("A"); + trace("init"); await Promise.resolve(); - trace("B"); + trace("push"); throw error; }); await expect(handler()).rejects.toBe(error); - expect(error.execution_trace).toBe("ABz"); + expect(error.execution_trace).toBe("init_push_thrw"); expect(error).toBeInstanceOf(TypeError); expect(error.message).toBe("original"); expect(error.cause).toBe(cause); + expect({ ...error }).not.toHaveProperty("execution_trace"); + expect(JSON.parse(JSON.stringify(error))).not.toHaveProperty("execution_trace"); }); it("traces synchronous failures and does not replace primitive throws", async () => { const error = new Error("sync"); const handler = withExecutionTrace(terminal)(trace => { - trace("A"); + trace("init"); throw error; }); await expect(handler()).rejects.toBe(error); - expect(error.execution_trace).toBe("Az"); + expect(error.execution_trace).toBe("init_thrw"); await expect( withExecutionTrace(terminal)(() => { throw "original"; @@ -70,40 +133,62 @@ describe("safe-output execution traces", () => { const blocked = new Promise(resolve => { release = resolve; }); - const handler = withExecutionTrace(terminal)(async (first, trace) => { + const handler = withExecutionTrace(terminal)(async (trace, first) => { if (first) { - trace("A"); + trace("init"); await blocked; - trace("B"); + trace("push"); } else { - trace("C"); + trace("stag"); } return { success: true }; }); const first = handler(true); - expect((await handler(false)).execution_trace).toBe("Cx"); + expect((await handler(false)).execution_trace).toBe("stag_done"); release(); - expect((await first).execution_trace).toBe("ABx"); - expect((await handler(false)).execution_trace).toBe("Cx"); + expect((await first).execution_trace).toBe("init_push_done"); + expect((await handler(false)).execution_trace).toBe("stag_done"); }); it("bounds paths and reserves the final outcome without recording invalid markers or inputs", async () => { const secret = "untrusted-input-do-not-record"; - const handler = withExecutionTrace(terminal)(async (input, trace) => { + const handler = withExecutionTrace(terminal)(async (trace, input) => { trace(input); - trace("AB"); - trace("1"); + trace("A"); + trace("ABCDE"); + trace("ab_c"); + trace("ab12"); + trace("init\n"); trace("\n"); trace(undefined); - for (let i = 0; i < MAX_EXECUTION_TRACE_LENGTH * 2; i++) trace("A"); + for (let i = 0; i < MAX_EXECUTION_TRACE_LENGTH * 2; i++) trace("push"); return { success: true }; }); const result = await handler(secret); - expect(result.execution_trace).toBe("A".repeat(MAX_EXECUTION_TRACE_LENGTH - 1) + "x"); + expect(result.execution_trace).toBe("push_".repeat(204) + "done"); expect(result.execution_trace).toHaveLength(1024); expect(result.execution_trace).not.toContain(secret); }); + it.each([{ success: false }, new Error("failed")])("retains complete markers and the failure terminal at the length limit", async result => { + const handler = withExecutionTrace(terminal)(trace => { + for (let i = 0; i < MAX_EXECUTION_TRACE_LENGTH; i++) trace("push"); + if (result instanceof Error) throw result; + return result; + }); + if (result instanceof Error) { + await expect(handler()).rejects.toBe(result); + } else { + expect(await handler()).toBe(result); + } + expect(result.execution_trace).toBe("push_".repeat(204) + (result instanceof Error ? "thrw" : "fail")); + }); + + it("emits the terminal without a leading underscore when no branch is recorded", async () => { + const result = await withExecutionTrace(terminal)(() => ({ success: true }))(); + expect(result.execution_trace).toBe("done"); + }); + it("does not turn immutable results or errors into tracing failures", async () => { const result = Object.freeze({ success: true }); expect(await withExecutionTrace(terminal)(() => result)()).toBe(result); @@ -123,15 +208,15 @@ describe("safe-output execution traces", () => { const handler = withExecutionTrace( terminal, withTransportIntegrity - )(async (message, ids, transport, trace) => { + )(async (trace, message, ids, transport) => { expect(message).toEqual({ untouched: true }); expect(ids).toEqual({}); expect(typeof transport.assertUnchanged).toBe("function"); - trace("A"); + trace("init"); throw new TransportIntegrityError("artifact"); }); const result = await handler({ untouched: true }, {}); - expect(result).toMatchObject({ success: false, error_type: "ERR_TRANSPORT_INTEGRITY", execution_trace: "Ay" }); + expect(result).toMatchObject({ success: false, error_type: "ERR_TRANSPORT_INTEGRITY", execution_trace: "init_fail" }); expect(global.core.error).toHaveBeenCalledOnce(); } finally { global.core = previousCore; @@ -150,23 +235,29 @@ describe("safe-output execution traces", () => { terminal, wrap )(trace => { - trace("A"); + trace("init"); return { success: true }; }); await expect(handler()).rejects.toBe(error); - expect(error.execution_trace).toBe("Az"); + expect(error.execution_trace).toBe("init_thrw"); }); - it.each(["create_pull_request.cjs", "push_to_pull_request_branch.cjs"])("%s has unique, explicit, documented single-letter branch markers", filename => { - const source = fs.readFileSync(path.join(__dirname, filename), "utf8"); - const calls = [...source.matchAll(/\btrace\(([^)]*)\)/g)].map(match => match[1]); - expect(calls.length).toBeGreaterThan(40); - for (const call of calls) expect(call).toMatch(/^"[A-Za-z]"$/); - const markers = calls.map(call => JSON.parse(call)); - const outcomes = [...source.matchAll(/(?:success|failure|error): "([xyz])"/g)].map(match => match[1]); - expect(outcomes).toEqual(["x", "y", "z"]); - expect(new Set([...markers, ...outcomes]).size).toBe(markers.length + outcomes.length); - expect(source).toContain("// Trace map:"); - expect(source).toContain('require("./safe_output_trace.cjs")'); + it.each(["create_pull_request", "push_to_pull_request_branch"])("%s records a real handler invocation without Git or API effects", async type => { + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn() }); + vi.stubGlobal("github", { rest: {} }); + vi.stubGlobal("context", { repo: { owner: "org", repo: "repo" }, payload: {} }); + vi.stubGlobal("exec", { exec: vi.fn(), getExecOutput: vi.fn() }); + vi.stubEnv("GIT_CONFIG_COUNT", "0"); + vi.stubEnv("GIT_CONFIG_KEY_0", undefined); + vi.stubEnv("GIT_CONFIG_VALUE_0", undefined); + vi.stubEnv("GH_AW_SAFE_OUTPUTS_STAGED", "false"); + vi.stubEnv("GH_AW_WORKFLOW_ID", "trace-test"); + const { main } = require(`./${type}.cjs`); + const handler = await main({ target_repo: "org/repo" }); + const result = await handler({ type, repo: "not-allowed/repo", temporary_id: "aw_trace" }, {}, new Map()); + expect(result).toMatchObject({ success: false, execution_trace: "init_repo_fail" }); + expect(result.error).toContain("allowed-repos"); + expect(global.exec.exec).not.toHaveBeenCalled(); + expect(global.exec.getExecOutput).not.toHaveBeenCalled(); }); }); diff --git a/actions/setup/js/types/unified_session.d.ts b/actions/setup/js/types/unified_session.d.ts index 4269cb5ce15..5bb590696cd 100644 --- a/actions/setup/js/types/unified_session.d.ts +++ b/actions/setup/js/types/unified_session.d.ts @@ -457,9 +457,9 @@ export interface SafeOutputData { /** The operation may have applied even though processing failed to confirm it. */ operation_outcome?: "indeterminate"; /** - * Ordered branch letters from the safe-output handler source, not a correlation ID. - * @pattern ^[A-Za-z]+$ - * @minLength 1 + * Ordered four-letter branch mnemonics joined by underscores, not a correlation ID. + * @pattern ^[A-Za-z]{4}(?:_[A-Za-z]{4})*$ + * @minLength 4 * @maxLength 1024 */ execution_trace?: string; diff --git a/docs/public/schemas/unified-session.schema.json b/docs/public/schemas/unified-session.schema.json index 1abe2d0d9c2..6ef3ea55ef7 100644 --- a/docs/public/schemas/unified-session.schema.json +++ b/docs/public/schemas/unified-session.schema.json @@ -1261,7 +1261,7 @@ "messageIndex": { "type": "number" }, "staged": { "type": "boolean" }, "operation_outcome": { "type": "string", "const": "indeterminate", "description": "The operation may have applied even though processing failed to confirm it." }, - "execution_trace": { "type": "string", "description": "Ordered branch letters from the safe-output handler source, not a correlation ID.", "pattern": "^[A-Za-z]+$", "minLength": 1, "maxLength": 1024 } + "execution_trace": { "type": "string", "description": "Ordered four-letter branch mnemonics joined by underscores, not a correlation ID.", "pattern": "^[A-Za-z]{4}(?:_[A-Za-z]{4})*$", "minLength": 4, "maxLength": 1024 } }, "additionalProperties": false }, @@ -1280,7 +1280,7 @@ "messageIndex": { "type": "number" }, "staged": { "type": "boolean" }, "operation_outcome": { "type": "string", "const": "indeterminate", "description": "The operation may have applied even though processing failed to confirm it." }, - "execution_trace": { "type": "string", "description": "Ordered branch letters from the safe-output handler source, not a correlation ID.", "pattern": "^[A-Za-z]+$", "minLength": 1, "maxLength": 1024 }, + "execution_trace": { "type": "string", "description": "Ordered four-letter branch mnemonics joined by underscores, not a correlation ID.", "pattern": "^[A-Za-z]{4}(?:_[A-Za-z]{4})*$", "minLength": 4, "maxLength": 1024 }, "errors": { "type": "array", "items": { "$ref": "#/definitions/SafeOutputData" } } }, "additionalProperties": false diff --git a/docs/src/content/docs/specs/unified-agent-session-specification.md b/docs/src/content/docs/specs/unified-agent-session-specification.md index fe6155e8f66..44b89bd31b3 100644 --- a/docs/src/content/docs/specs/unified-agent-session-specification.md +++ b/docs/src/content/docs/specs/unified-agent-session-specification.md @@ -3,9 +3,9 @@ title: Unified Agent Session Specification description: Draft contract for canonical traces, full unified artifacts, and bounded compact unified sessions. sidebar: order: 1365 -version: "2.10.0" +version: "2.11.0" status: Draft -publication_date: "2026-11-12" +publication_date: "2026-11-17" editors: - name: GitHub Agentic Workflows Team organization: GitHub @@ -13,9 +13,9 @@ editors: # Unified Agent Session Specification -**Version**: 2.10.0
+**Version**: 2.11.0
**Status**: Draft
-**Publication Date**: 2026-11-12
+**Publication Date**: 2026-11-17
**Editors**: GitHub Agentic Workflows Team (GitHub)
**This Version**: [unified-agent-session-specification](/gh-aw/specs/unified-agent-session-specification/)
**Latest Version**: This document @@ -30,7 +30,7 @@ This specification defines the session traces used by GitHub Agentic Workflows: This is a **GitHub Agentic Workflows project specification**, written using W3C-inspired document conventions. It is **not an official W3C standard**, W3C publication, or W3C-endorsed recommendation. -Version 2.8.0 is a draft governed by the project's normal review process. It may be updated, replaced, or superseded. The accompanying implementation and regression suites exercise this contract, including the sampled CI sessions identified in Section 9.4. This is not a blanket declaration of conformance for every engine version or source format. Section 10 records historical pre-implementation gaps, not current defects. Approval and ongoing compliance testing remain project responsibilities. +Version 2.11.0 is a draft governed by the project's normal review process. It may be updated, replaced, or superseded. The accompanying implementation and regression suites exercise this contract, including the sampled CI sessions identified in Section 9.4. This is not a blanket declaration of conformance for every engine version or source format. Section 10 records historical pre-implementation gaps, not current defects. Approval and ongoing compliance testing remain project responsibilities. The specification version belongs to this document. The unified file's leading `session.format` record carries an independent numeric serialization-format @@ -868,15 +868,19 @@ source for opaque fields. | `session.format` | Leading collector-owned file-format metadata, distinct from source-native events with the same type. | Pull-request creation and branch-push outcomes also retain `data.execution_trace`, -an ordered sequence of source-defined branch letters (`A–Z`, `a–z`, at most -1,024 characters, retaining the prefix and terminal outcome at the limit). -Each instrumented route has a unique letter within its handler; -the source comments define the mapping. Equal paths produce equal identifiers, +an ordered sequence of self-describing four-letter branch mnemonics joined by +underscores, such as `init_bund_push_publ_capt_done`. Traces contain at most +1,024 characters, retaining complete prefix markers and the terminal outcome +(`done` for success, `fail` for unsuccessful or skipped returns, `thrw` for errors). +Each instrumented route has a unique mnemonic within its handler; +the source comments expand abbreviations. Equal paths produce equal identifiers, not unique invocation IDs. Interpret a trace with its operation `type` and the handler source revision from that run to compare hot paths and diagnose recovery or fallback routes. Traces survive skipped, staged, and failed execution, including thrown errors; absent traces mean instrumentation was unavailable or the handler -was not entered. The reusable tracing helper can instrument other safe outputs +was not entered. Handlers retain traces as non-enumerable internal result/error +metadata, explicitly projected into recorded outcomes rather than tool-response +payloads. The reusable tracing helper can instrument other safe outputs without recording request bodies, branch names, credentials, or error text. Job-result records cover only the conclusion job's direct dependencies, including @@ -2739,6 +2743,11 @@ Native IDs can collide, timestamps can be out of order, and a trace can contain ## 13. Change Log (Informative) +### Version 2.11.0 - Draft (2026-11-17) + +- Added `data.execution_trace` for pull-request creation and branch-push outcomes: underscore-separated four-letter branch mnemonics bounded to 1,024 characters, preserving complete prefix markers and terminal `done`, `fail`, or `thrw` outcomes while excluding request bodies, branch names, credentials, and error text. +- Retained serialization-format version 1. + ### Version 2.10.0 - Draft (2026-11-12) - Retained AWF's per-conversation `steering_delivery` record as `steeringDelivery` on `firewall.token_usage` and `usage.report`, and `trigger` and `conversation` on `firewall.steering`, with `null` thresholds for `little_left` notices (T-UAS-071).