Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7280: ci(backend): cache race builds per commit and shard pkg/agent onto its own runner (#8302)

Bohan Jiangb54e66529504
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6d90f6063e..b48a8f3038 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -113,7 +113,7 @@ jobs:
- 'docker-compose.selfhost.yml'
- 'docker-compose.selfhost.build.yml'
- 'Makefile'
- # Everything the backend jobs (backend-tests,
+ # Everything the backend jobs (backend-tests, backend-agent-tests,
# go-vulnerability-scan, windows-execenv) read, which is NOT the
# same thing as server/**: four Go tests reach out of the module
# to pin a contract against a file the other side owns, across
@@ -495,6 +495,41 @@ jobs:
exit 1
fi
+ # The Go caches are handled explicitly here rather than through setup-go's
+ # `cache: true`. setup-go keys its entry on go.sum alone and shares that key
+ # with every Linux job in this workflow, so whichever job finished first,
+ # go-vulnerability-scan in under a minute, saved a ~95 MB entry holding
+ # little beyond module downloads; this job then "hit" that key on every run
+ # and never saved its own. The result was a from-scratch race-instrumented
+ # compile of the whole module on every backend run, measured at roughly
+ # half of the job's ~8 minutes (MUL-7280).
+ #
+ # Two entries now. The module cache is keyed by go.sum: immutable, saved on
+ # a miss, never grows. The build cache is keyed by commit SHA with a prefix
+ # fallback, so each run starts from the newest entry and recompiles only
+ # what its change touched. Only pushes to main publish a build-cache entry;
+ # PR runs restore and leave, which keeps storage churn at one entry per
+ # merge, the same shape as the turbo caches above. The key carries the
+ # resolved Go version because `check-latest` floats across patch releases,
+ # and the runner image (ImageOS) because cached cgo and race objects are
+ # linked against that image's glibc: when ubuntu-latest moves to a new
+ # release, an entry built on the old one must not be restored. setup-go
+ # keys on ImageOS for the same reason (actions/setup-go#368).
+ #
+ # This job is the only writer of either entry. backend-agent-tests restores
+ # both, but it downloads just pkg/agent's slice of the module graph and
+ # finishes first, so letting it save would repeat the first-writer-wins
+ # problem above: an immutable go.sum key pinned to a partial module cache.
+ #
+ # Every compile below runs in race mode so build, vet and test share one set
+ # of objects. A plain `go build` fed nothing to the -race test binaries and
+ # was pure duplicate work; nothing in server/ carries a `race` build tag, so
+ # both modes accept exactly the same code.
+ #
+ # `GOFLAGS=-count=1` because a rolling cache would otherwise let `go test`
+ # replay cached results. Migrations are read from disk at run time rather
+ # than embedded, so a schema change is invisible to a test binary's hash and
+ # a cached "ok" from an untouched package could mask a real failure.
backend-tests:
needs: changes
runs-on: ubuntu-latest
@@ -534,12 +569,14 @@ jobs:
# realtime relay + request stores); the tests talk to this Redis
# directly so they run alongside the Postgres-backed suite.
REDIS_TEST_URL: redis://localhost:6379/1
+ GOFLAGS: -count=1
steps:
- name: Checkout
if: ${{ needs.changes.outputs.backend == 'true' }}
uses: actions/checkout@v6
- name: Setup Go
+ id: go
if: ${{ needs.changes.outputs.backend == 'true' }}
uses: actions/setup-go@v5
with:
@@ -547,7 +584,29 @@ jobs:
# CI follows the newest 1.26 patch available to setup-go.
go-version: "1.26.x"
check-latest: true
- cache-dependency-path: server/go.sum
+ cache: false
+
+ - name: Restore Go module cache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/cache@v6
+ with:
+ path: ~/go/pkg/mod
+ key: go-mod-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('server/go.sum') }}
+
+ # ImageOS is a runner environment variable, not a context value, so a
+ # shell step spells the build-cache key prefix once for restore and save.
+ - name: Resolve Go build cache key
+ id: gocache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ run: echo "prefix=go-build-race-${{ runner.os }}-${{ runner.arch }}-$ImageOS-${{ steps.go.outputs.go-version }}-" >> "$GITHUB_OUTPUT"
+
+ - name: Restore Go build cache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/cache/restore@v6
+ with:
+ path: ~/.cache/go-build
+ key: ${{ steps.gocache.outputs.prefix }}${{ github.sha }}
+ restore-keys: ${{ steps.gocache.outputs.prefix }}
- name: Setup Helm
if: ${{ needs.changes.outputs.backend == 'true' }}
@@ -567,7 +626,7 @@ jobs:
- name: Build
if: ${{ needs.changes.outputs.backend == 'true' }}
- run: cd server && go build ./...
+ run: cd server && go build -race ./...
# The `agentintegration` files execute real agent CLIs, so no CI job runs
# them. Nothing compiled them either, which let them rot silently: a
@@ -577,7 +636,7 @@ jobs:
# cheapest gate that keeps them honest without needing an agent CLI.
- name: Vet tag-gated agent integration tests
if: ${{ needs.changes.outputs.backend == 'true' }}
- run: cd server && go vet -tags agentintegration ./...
+ run: cd server && go vet -race -tags agentintegration ./...
- name: Run migrations
if: ${{ needs.changes.outputs.backend == 'true' }}
@@ -589,7 +648,74 @@ jobs:
- name: Test
if: ${{ needs.changes.outputs.backend == 'true' }}
- run: bash scripts/test-go.sh --race
+ # pkg/agent runs on its own runner: backend-agent-tests below.
+ run: bash scripts/test-go.sh --race --only regular
+
+ # Publishes from main only; see the job comment. `!cancelled()` so a red
+ # main still refreshes the objects for the next run, and the key step's
+ # outcome so a run that never reached Go does not save an empty entry
+ # under a half-formed key.
+ - name: Save Go build cache
+ if: ${{ !cancelled() && github.event_name == 'push' && steps.gocache.outcome == 'success' }}
+ uses: actions/cache/save@v6
+ with:
+ path: ~/.cache/go-build
+ key: ${{ steps.gocache.outputs.prefix }}${{ github.sha }}
+
+ # pkg/agent used to run as the tail of backend-tests: ~2m20s for a single
+ # package, throttled to -parallel 2 because its subprocess-backed tests carry
+ # hard deadlines and starve when a race build competes for the runner (see
+ # scripts/test-go.sh). Those tests are timer-bound rather than CPU-bound, so
+ # the throttle stays and the package simply gets its own runner; it reads no
+ # database, so no service containers either. Same trade as the frontend
+ # split: more runner-minutes, less wall clock. Both Go caches are restored
+ # but never saved here: backend-tests owns publishing (see its comment), and
+ # this job's only unique output is the pkg/agent test binary, which is cheap
+ # to relink.
+ backend-agent-tests:
+ needs: changes
+ runs-on: ubuntu-latest
+ env:
+ # Same policy as backend-tests: CI always executes the tests it reports.
+ GOFLAGS: -count=1
+ steps:
+ - name: Checkout
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/checkout@v6
+
+ - name: Setup Go
+ id: go
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/setup-go@v5
+ with:
+ go-version: "1.26.x"
+ check-latest: true
+ cache: false
+
+ - name: Restore Go module cache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/cache/restore@v6
+ with:
+ path: ~/go/pkg/mod
+ key: go-mod-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('server/go.sum') }}
+
+ # Must match backend-tests' key step exactly.
+ - name: Resolve Go build cache key
+ id: gocache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ run: echo "prefix=go-build-race-${{ runner.os }}-${{ runner.arch }}-$ImageOS-${{ steps.go.outputs.go-version }}-" >> "$GITHUB_OUTPUT"
+
+ - name: Restore Go build cache
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ uses: actions/cache/restore@v6
+ with:
+ path: ~/.cache/go-build
+ key: ${{ steps.gocache.outputs.prefix }}${{ github.sha }}
+ restore-keys: ${{ steps.gocache.outputs.prefix }}
+
+ - name: Test agent package
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ run: bash scripts/test-go.sh --race --only agent
# Keep the live vulnerability database from hiding build and test results.
# The aggregate backend job below still treats both jobs as one merge gate.
@@ -619,20 +745,22 @@ jobs:
# Preserve the existing required-check name while allowing tests, generated
# SQL verification, and the vulnerability scan to run independently.
backend:
- needs: [backend-tests, sqlc-check, go-vulnerability-scan]
+ needs: [backend-tests, backend-agent-tests, sqlc-check, go-vulnerability-scan]
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
- name: Check backend job results
env:
TEST_RESULT: ${{ needs.backend-tests.result }}
+ AGENT_TEST_RESULT: ${{ needs.backend-agent-tests.result }}
SQLC_RESULT: ${{ needs.sqlc-check.result }}
VULNERABILITY_RESULT: ${{ needs.go-vulnerability-scan.result }}
run: |
- echo "backend-tests: $TEST_RESULT"
- echo "sqlc-check: $SQLC_RESULT"
+ echo "backend-tests: $TEST_RESULT"
+ echo "backend-agent-tests: $AGENT_TEST_RESULT"
+ echo "sqlc-check: $SQLC_RESULT"
echo "go-vulnerability-scan: $VULNERABILITY_RESULT"
- if [ "$TEST_RESULT" != "success" ] || [ "$SQLC_RESULT" != "success" ] || [ "$VULNERABILITY_RESULT" != "success" ]; then
+ if [ "$TEST_RESULT" != "success" ] || [ "$AGENT_TEST_RESULT" != "success" ] || [ "$SQLC_RESULT" != "success" ] || [ "$VULNERABILITY_RESULT" != "success" ]; then
echo "::error::backend validation failed"
exit 1
fi
diff --git a/scripts/test-go.sh b/scripts/test-go.sh
index ee88c144f3..dcae59e7b9 100644
--- a/scripts/test-go.sh
+++ b/scripts/test-go.sh
@@ -6,36 +6,54 @@ REPO_ROOT=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
GUARD_SCRIPT="$SCRIPT_DIR/go-test-with-agent-cli-guard.sh"
usage() {
- echo "usage: $0 [--race]" >&2
+ echo "usage: $0 [--race] [--only regular|agent]" >&2
}
+# The suite is two `go test` invocations: every package outside pkg/agent at
+# the default parallelism, then pkg/agent throttled (see below). `--only`
+# selects one half so CI can give each its own runner; the default still runs
+# both for `make test`, check.sh, and the release workflow.
go_test_args=(test)
-case "$#" in
- 0) ;;
- 1)
- if [ "$1" != "--race" ]; then
+only=all
+while [ "$#" -gt 0 ]; do
+ case "$1" in
+ --race)
+ go_test_args+=(-race)
+ shift
+ ;;
+ --only)
+ case "${2:-}" in
+ regular|agent) only=$2 ;;
+ *)
+ usage
+ exit 2
+ ;;
+ esac
+ shift 2
+ ;;
+ *)
usage
exit 2
- fi
- go_test_args+=(-race)
- ;;
- *)
- usage
- exit 2
- ;;
-esac
-
-cd "$REPO_ROOT/server"
-packages=$(go list ./...)
-regular_packages=()
-for package in $packages; do
- case "$package" in
- */pkg/agent|*/pkg/agent/*) ;;
- *) regular_packages+=("$package") ;;
+ ;;
esac
done
-"$GUARD_SCRIPT" -- go "${go_test_args[@]}" "${regular_packages[@]}"
-# Subprocess-backed agent tests have hard deadlines. Limit both package and
-# within-package parallelism so race builds do not starve their parent loops.
-"$GUARD_SCRIPT" -- go "${go_test_args[@]}" -p 2 -parallel 2 ./pkg/agent/...
+cd "$REPO_ROOT/server"
+
+if [ "$only" != agent ]; then
+ packages=$(go list ./...)
+ regular_packages=()
+ for package in $packages; do
+ case "$package" in
+ */pkg/agent|*/pkg/agent/*) ;;
+ *) regular_packages+=("$package") ;;
+ esac
+ done
+ "$GUARD_SCRIPT" -- go "${go_test_args[@]}" "${regular_packages[@]}"
+fi
+
+if [ "$only" != regular ]; then
+ # Subprocess-backed agent tests have hard deadlines. Limit both package and
+ # within-package parallelism so race builds do not starve their parent loops.
+ "$GUARD_SCRIPT" -- go "${go_test_args[@]}" -p 2 -parallel 2 ./pkg/agent/...
+fi
diff --git a/scripts/test-go.test.sh b/scripts/test-go.test.sh
index 10a48adb86..53a7ac4ad8 100644
--- a/scripts/test-go.test.sh
+++ b/scripts/test-go.test.sh
@@ -14,8 +14,9 @@ trap cleanup EXIT
mkdir -p "$BIN_DIR"
export MULTICA_TEST_GO_CALLS="$CALLS_FILE"
+: >"$CALLS_FILE"
-cat >"$BIN_DIR/go" <<'EOF'
+cat >"$BIN_DIR/go" <<'FAKE'
#!/usr/bin/env bash
set -eu
@@ -39,40 +40,63 @@ case "${1:-}" in
exit 2
;;
esac
-EOF
+FAKE
chmod 755 "$BIN_DIR/go"
+regular_call='test -race github.com/multica-ai/multica/server github.com/multica-ai/multica/server/internal/daemon'
+agent_call='test -race -p 2 -parallel 2 ./pkg/agent/...'
+
+# $1: case label; $2: expected go calls, one per line. Clears the log after.
+expect_calls() {
+ actual_calls=$(cat "$CALLS_FILE")
+ if [ "$actual_calls" != "$2" ]; then
+ echo "$1: unexpected go test calls:" >&2
+ printf '%s\n' "$actual_calls" >&2
+ exit 1
+ fi
+ : >"$CALLS_FILE"
+}
+
+# $1: case label; the rest are arguments for test-go.sh. Asserts the usage
+# exit status, the usage line, and that go was never invoked.
+expect_usage_failure() {
+ label=$1
+ shift
+ set +e
+ PATH="$BIN_DIR:$PATH" bash "$SCRIPT_DIR/test-go.sh" "$@" >"$OUTPUT_FILE" 2>&1
+ status=$?
+ set -e
+
+ if [ "$status" -ne 2 ]; then
+ echo "$label returned status $status, want 2" >&2
+ cat "$OUTPUT_FILE" >&2
+ exit 1
+ fi
+ if [ -s "$CALLS_FILE" ]; then
+ echo "$label invoked go:" >&2
+ cat "$CALLS_FILE" >&2
+ exit 1
+ fi
+ if ! grep -q '^usage: .*test-go.sh \[--race\] \[--only regular|agent\]$' "$OUTPUT_FILE"; then
+ echo "$label did not print usage" >&2
+ cat "$OUTPUT_FILE" >&2
+ exit 1
+ fi
+}
+
PATH="$BIN_DIR:$PATH" bash "$SCRIPT_DIR/test-go.sh" --race
+expect_calls "--race" "$regular_call
+$agent_call"
-expected_calls='test -race github.com/multica-ai/multica/server github.com/multica-ai/multica/server/internal/daemon
-test -race -p 2 -parallel 2 ./pkg/agent/...'
-actual_calls=$(cat "$CALLS_FILE")
-if [ "$actual_calls" != "$expected_calls" ]; then
- echo "unexpected go test calls:" >&2
- printf '%s\n' "$actual_calls" >&2
- exit 1
-fi
+PATH="$BIN_DIR:$PATH" bash "$SCRIPT_DIR/test-go.sh" --race --only regular
+expect_calls "--only regular" "$regular_call"
-: >"$CALLS_FILE"
-set +e
-PATH="$BIN_DIR:$PATH" bash "$SCRIPT_DIR/test-go.sh" --unknown >"$OUTPUT_FILE" 2>&1
-status=$?
-set -e
+# Option order must not matter: CI spells it one way, humans another.
+PATH="$BIN_DIR:$PATH" bash "$SCRIPT_DIR/test-go.sh" --only agent --race
+expect_calls "--only agent" "$agent_call"
-if [ "$status" -ne 2 ]; then
- echo "unknown option returned status $status, want 2" >&2
- cat "$OUTPUT_FILE" >&2
- exit 1
-fi
-if [ -s "$CALLS_FILE" ]; then
- echo "unknown option invoked go:" >&2
- cat "$CALLS_FILE" >&2
- exit 1
-fi
-if ! grep -q '^usage: .*test-go.sh \[--race\]$' "$OUTPUT_FILE"; then
- echo "unknown option did not print usage" >&2
- cat "$OUTPUT_FILE" >&2
- exit 1
-fi
+expect_usage_failure "unknown option" --unknown
+expect_usage_failure "unknown --only scope" --only everything
+expect_usage_failure "missing --only scope" --only
echo "test-go.test.sh: PASS"