Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/adaptive-routing-observe.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ pkg/workflow/actionslanguageservice/testdata/conformance.json
# Verbatim routing-analytics run artifacts (github/gh-aw#70668)
actions/setup/js/fixtures/routing-analytics/**/*.json

# Adaptive routing oracle vectors, generated from the reference bandit and compared numerically
actions/setup/js/fixtures/routing-history/*.json

# Pinned upstream Harbor golden fixtures (Git blob checksum verified)
pkg/cli/testdata/harbor/*.json
1 change: 1 addition & 0 deletions actions/setup/js/fixtures/routing-history/aw2-reducer.json

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions actions/setup/js/push_repo_memory.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { loadTemporaryIdMapFromFile, replaceTemporaryIdReferencesInPatch } = requ
// Matches .jsonl in any letter case, like the persistence filters do.
const JSONL_MERGE_ATTRIBUTE = "*.[jJ][sS][oO][nN][lL] merge=union";
const JSONL_PATH_PATTERN = /\.jsonl$/i;
const ADAPTIVE_ROUTING_BRANCH_ROOT = "memory/adaptive-routing";

function setOutput(name, value) {
if (typeof core.setOutput === "function") core.setOutput(name, value);
Expand Down Expand Up @@ -386,6 +387,9 @@ function planRepoMemoryReplay({ files, baseCommit, workspaceDir }) {
* @param {string} [opts.originUrlForPush]
* @param {string} [opts.repoUrlForRetry]
* @param {() => void} [opts.validateBeforePush]
* @param {(context: {remoteHead: string}) => (boolean | Promise<boolean>)} [opts.reapply] For gh-aw's own state writers, not agent memory:
* when the branch head moved, check out the newest head and call this to rebuild and commit the change from the newest files,
* instead of rebasing with this run's copy winning. It returns false when there is nothing left to commit.
* @returns {Promise<string | undefined | false>} Remote commit, unchanged replay, or failure.
*/
async function pushRepoMemoryChangesWithRetry({
Expand All @@ -404,6 +408,7 @@ async function pushRepoMemoryChangesWithRetry({
originUrlForPush,
repoUrlForRetry,
validateBeforePush,
reapply,
}) {
const retryUrl = repoUrlForRetry || `https://${serverHost}/${targetRepo}.git`;

Expand Down Expand Up @@ -442,9 +447,9 @@ async function pushRepoMemoryChangesWithRetry({
baseRef: currentBaseRef,
cwd: workspaceDir,
gitAuthEnv: getGitAuthEnv(ghToken),
allowGitPushFallback: !validateBeforePush,
allowGitPushFallback: !validateBeforePush && !reapply,
// Commit only on the head this save was built on; a moved branch is
// rebased below with the JSONL union policy and retried.
// rebased below with the JSONL union policy, or re-applied, and retried.
requireBaseRefMatch: true,
});
core.info(commit ? `Successfully pushed changes to ${branchName} branch` : `No changes left to push to ${branchName} branch`);
Expand Down Expand Up @@ -477,14 +482,25 @@ async function pushRepoMemoryChangesWithRetry({
currentBaseRef = remoteHead;
core.info(`Refreshed baseRef for retry: ${currentBaseRef}`);
try {
reconcileRepoMemoryRetry({
workspaceDir,
branchName,
repoUrl: retryUrl,
previousBaseRef,
remoteHead,
gitAuthEnv: getGitAuthEnv(ghToken),
});
if (reapply) {
// A clean textual merge of a derived state file can still be wrong, so rebuild
// the change from the newest files whether or not the files conflict.
execGitSync(["fetch", retryUrl, `refs/heads/${branchName}`], { cwd: workspaceDir, env: { ...process.env, ...getGitAuthEnv(ghToken) }, stdio: "pipe", suppressLogs: true });
execGitSync(["reset", "--hard", remoteHead], { cwd: workspaceDir, stdio: "pipe" });
if (!(await reapply({ remoteHead }))) {
core.info(`No changes left to push to ${branchName} branch`);
return undefined;
}
} else {
reconcileRepoMemoryRetry({
workspaceDir,
branchName,
repoUrl: retryUrl,
previousBaseRef,
remoteHead,
gitAuthEnv: getGitAuthEnv(ghToken),
});
}
} catch (reconcileError) {
await setPushRepoMemoryFailure(`Failed to reconcile repo-memory changes onto refreshed head before retry: ${getErrorMessage(reconcileError)}`);
return false;
Expand Down Expand Up @@ -741,6 +757,12 @@ async function main() {
await setPushRepoMemoryFailure(`ERR_VALIDATION: Branch name "${branchName}" is only valid for wiki repositories (TARGET_REPO must end with ".wiki", got "${targetRepo}")`);
return;
}
// Adaptive routing histories are written only by push_routing_history and gh aw routing reset; the
// compiler rejects literal branch names there, and this catches expressions that resolve to one.
if (branchName === ADAPTIVE_ROUTING_BRANCH_ROOT || branchName.startsWith(`${ADAPTIVE_ROUTING_BRANCH_ROOT}/`)) {
await setPushRepoMemoryFailure(`ERR_VALIDATION: Branch "${branchName}" is reserved for adaptive routing histories; repo-memory cannot write it`);
return;
}

// Validate target repository against allowlist
const allowedReposEnv = process.env.REPO_MEMORY_ALLOWED_REPOS?.trim();
Expand Down
16 changes: 16 additions & 0 deletions actions/setup/js/push_repo_memory.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,22 @@ describe("push_repo_memory.cjs - shell injection security tests", () => {
expect(mockExecGitSync).not.toHaveBeenCalled();
});

it("should refuse an adaptive routing history branch, as an expression could resolve to one", async () => {
process.env.BRANCH_NAME = "memory/adaptive-routing/triage";
process.env.TARGET_REPO = "test-owner/test-repo";

mockFs.existsSync.mockReturnValue(false);

vi.doMock("fs", () => mockFs);
vi.doMock("./git_helpers.cjs", () => ({ execGitSync: mockExecGitSync }));

const { main } = await import("./push_repo_memory.cjs");
await main();

expect(mockCore.setFailed).toHaveBeenCalledWith(expect.stringContaining("reserved for adaptive routing histories"));
expect(mockExecGitSync).not.toHaveBeenCalled();
});

it("should accept custom branch-prefix branches like campaigns/metrics", () => {
// The Go compiler supports custom branch-prefix values (e.g. "campaigns") so
// generated branch names like "campaigns/metrics" must pass validation.
Expand Down
Loading
Loading