Skip to content

fix(ci): stash local changes before git pull on dev deploy - #943

Merged
vybe merged 2 commits into
devfrom
fix/deploy-dev-stash-before-pull
May 26, 2026
Merged

vybe merged 2 commits into
devfrom
fix/deploy-dev-stash-before-pull

Conversation

@dolho

@dolho dolho commented May 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Every deploy to dev has been failing since 2026-05-13 (#826). This patch unjams it.

Failure timeline

Date Failure Resolution
2026-05-13 → 2026-05-26 error: missing server host — PR #826 moved DEV_HOST / DEV_USER to repo secrets but they were never set. Secrets configured 2026-05-26.
2026-05-26 12:35 UTC Your local changes to the following files would be overwritten by merge: docker-compose.prod.yml — git pull aborts because the dev VM's checkout has hand-applied changes. This PR.

Root cause

The dev deploy is a pull-style model: the dev VM owns a persistent source checkout (~/trinity) that the SSH script git pulls on every deploy. Build happens on the VM, not on CI. Any local modifications to tracked files (intentional hot-fixes, leftover state from a failed deploy, hand-applied prod tweaks) block the pull and break every subsequent deploy.

Change

Stash dirty changes under a deploy-run-keyed message before pulling, so the deploy proceeds without losing the hand-applied edits.

STASH_MSG="auto-stash-deploy-${GITHUB_RUN_ID:-manual}"
git stash push -u -m "${STASH_MSG}" || echo "No local changes to stash"
git pull --ff-only origin dev
  • Stash includes untracked files (-u).
  • Stash entries survive in git stash list (and the reflog ~90 days) on the VM — recoverable via git stash show stash@{0} + git stash apply stash@{N}.
  • --ff-only makes divergent history fail fast instead of producing a silent merge commit.
  • Stash is a no-op (exit 0) when the working tree is clean.

Why not other options

Option Why not (for this PR)
git reset --hard origin/dev Destructive — silently discards any in-flight hot-fix. No audit trail.
git stash without --ff-only Same risk as before; deploys could silently merge a divergent history.
Move build to CI + image registry (atomic deploy) The correct long-term fix. Trinity's dev VM should be stateless: pull pre-built images instead of running a build on the box. Separate scope — file follow-up issue.

Test plan

  • YAML syntax valid (yamllint .github/workflows/deploy-dev.yml)
  • Merge to dev → push triggers Deploy to Dev → stash + pull + build succeeds
  • After deploy succeeds: SSH to dev VM, run git stash list and verify the previously-modified docker-compose.prod.yml is recoverable as auto-stash-deploy-<run_id>
  • Optional but recommended: inspect the stashed change (git stash show -p stash@{0}) and decide whether it should be committed back to the repo or discarded permanently

Follow-up issue to file

"Move dev deploy to image-registry pull (stateless dev VM)" — eliminates this whole class of failure. CI builds + pushes images to GHCR, dev runs only docker compose pull && up. No git on dev. ~1–2 day rewrite.

🤖 Generated with Claude Code

Every deploy to dev since 2026-05-13 (#826) failed.

Sequence of failures:
1. PR #826 moved DEV_HOST/DEV_USER hardcoded refs to repo secrets.
   Secrets weren't set → 7 weeks of "missing server host" failures
   (resolved 2026-05-26 — secrets configured).
2. Once secrets worked, SSH connection succeeded but `git pull`
   aborted at "Your local changes to the following files would be
   overwritten by merge: docker-compose.prod.yml". The dev VM has
   a persistent checkout that accumulated hand-applied tweaks (or
   leftover state from a prior failed deploy).

Root cause is the pull-style deploy model itself: dev VM owns a
long-lived source checkout that the SSH script `git pull`s on every
deploy. Build happens on the VM, not on CI. Local edits to tracked
files block the pull.

This patch:
- Stashes dirty changes under a deploy-run-keyed message before
  pulling, so the deploy proceeds without losing the hand-applied
  edits. They survive in `git stash list` + the reflog (~90 days)
  on the VM.
- Switches `git pull` to `--ff-only` so a divergent history fails
  fast instead of producing a silent merge commit.
- Comments the long-term fix (move build to CI + image registry =>
  stateless dev VM; tracked as a follow-up — file an issue).

After merge, the next push to dev triggers a deploy that:
  - Stashes the existing docker-compose.prod.yml modification under
    `auto-stash-deploy-<run_id>`
  - Pulls cleanly (fast-forward)
  - Resumes normal build/restart
The hand-applied change is recoverable with
`git stash list` + `git stash show` + `git stash apply` on the VM.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@dolho
dolho requested a review from AndriiPasternak31 as a code owner May 26, 2026 13:52
Comment thread .github/workflows/deploy-dev.yml Resolved
Comment thread .github/workflows/deploy-dev.yml Resolved
@dolho

dolho commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/code-review (extra-high recall, medium-confidence gate)

Reviewed the 14-line workflow change end-to-end. Two findings posted as inline comments:

# Sev File:line Issue
1 CRITICAL .github/workflows/deploy-dev.yml:47 $GITHUB_RUN_ID isn't available on the remote shell; stash name silently falls back to auto-stash-deploy-manual every deploy, defeating the audit-trail intent. Fix: ${{ github.run_id }} workflow-syntax interpolation.
2 MINOR .github/workflows/deploy-dev.yml:42 Comment references #942 but that's an unrelated issue. Rephrase or file the right follow-up.

Verified (no findings)

  • Stash semantics: git stash push -u -m "..." correctly stashes both tracked-dirty and untracked files. || echo defends against the (rare) non-zero exit on empty tree. set -e interaction is fine — the || makes the command a list, exempt from errexit.
  • --ff-only choice: divergent local history now fails fast instead of producing a silent merge commit. Loud failure on a polluted dev box is the right call.
  • Variable expansion: ${STASH_MSG} (between git stash push -m and the message) interpolates correctly on bash and POSIX sh. (But see finding Fix: Add missing Docker labels to system agent container #1 — the content of STASH_MSG is the bug, not the expansion.)
  • Stash naming collision: GH run IDs are unique, so once finding Fix: Add missing Docker labels to system agent container #1 is fixed, parallel deploys won't collide.

Not introduced by this PR but worth flagging

  • Submodule update missing: the workflow does git pull but not git submodule update --init --recursive. The dev branch has .claude submodule today; PR feat(enterprise): audit log dashboard + entitlement seam (#941, #847) #910 adds src/backend/enterprise. When either pointer moves on a deploy, the dev VM won't sync. Stale submodule = stale code in build. Recommend adding git submodule update --init --recursive after the pull as part of this PR (small enough scope, eliminates a foot-gun the audit-dashboard merge will hit immediately).
  • No concurrency: group: two pushes to dev in quick succession can interleave SSH sessions on the same checkout. Existing behavior; not made worse by this PR but worth a separate hardening PR.
  • Stash growth: no cleanup story. Manageable for now (recoverable via git stash drop on the VM), but a cron-based cleaner is worth ~10 LOC.

Verdict

Once finding #1 is fixed, this PR is mergeable. The ${{ github.run_id }} swap is a one-character-spirit change and makes the audit trail actually work. Finding #2 is cosmetic; the PR can ship with either fix.

🤖 /code-review at extra-high recall effort. False-positive rate biased toward over-reporting per skill spec.

…n id

Review feedback on the previous commit: `$GITHUB_RUN_ID` lives on the
GH Actions runner, not on the dev VM where the SSH script runs.
`${GITHUB_RUN_ID:-manual}` silently fell back to "manual" every time,
defeating the deploy-run-keyed audit trail intent.

`${{ github.run_id }}` is interpolated at YAML render time, so the
literal run id is baked into the script before drone-ssh delivers it
to the remote.

Added an inline comment explaining the trap so the next person to
touch the script doesn't re-introduce it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@dolho

dolho commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/code-review (re-review after fix commit 6582ca65)

Status of prior findings

# Status Notes
1 ✅ RESOLVED ${{ github.run_id }} interpolation correctly applied at line 47. Workflow-syntax expansion happens at YAML render time; the literal run id is baked into the script before drone-ssh ships it. Added an inline comment block warning future contributors about the $GITHUB_RUN_ID trap.
2 🟡 UNCHANGED #942 reference still points at the wrong issue. Author opted to defer — acceptable, but worth a one-line fix before merge (rephrase to (separate followup) or file the real issue + update the number).

Fresh pass on the updated diff (5 angles, extra-high recall)

Line-by-line: STASH_MSG="auto-stash-deploy-${{ github.run_id }}" — workflow expression yields a numeric string; double-quoted bash assignment of a digit-only string is unambiguous. Clean.

Removed-behavior: The previous version had a :-manual fallback. The new version doesn't. If github.run_id were ever empty, the script would assign STASH_MSG="auto-stash-deploy-". In practice GH always populates run_id for workflow-triggered runs (this workflow is on: push only — never manual workflow_dispatch without a run), so this is theoretical. Not a finding.

Cross-file: Nothing in the codebase parses stash messages or git stash list output. The message is a human-debugging aid only — no automated consumer can break on the format change.

Language pitfalls: ${{ ... }} (workflow expression) vs ${VAR} (bash parameter expansion) are syntactically distinct via the double curlies — no parser ambiguity. The added comment block flags the trap clearly for future contributors.

Wrapper/proxy: N/A — not a wrapper change.

Sweep for gaps

Looked specifically for what the first-pass tends to miss on workflow YAML changes:

  • YAML block scalar boundary issues — none, indentation preserved
  • Trailing-whitespace gotchas in | block — none introduced
  • Quote escaping in nested contexts (YAML → bash) — clean, single-level double quotes
  • Race with set -e and || — already verified clean in first review
  • Effect on the concurrency: story — workflow still has no group, but the fix doesn't make this worse

No new findings.

Verdict

Ready to merge once finding #2 is either fixed or explicitly accepted as merge-time-cleanup. The CI itself can't validate this workflow change pre-merge (no syntax-checker for workflow expressions in the existing CI matrix), so the real verification is the next push to dev:

  • Stash named auto-stash-deploy-<run_id> (numeric run id, not "manual")
  • git pull --ff-only succeeds
  • Deploy completes
  • git stash list on the dev VM shows the entry with the actual run id

🤖 /code-review at extra-high recall effort, 2nd pass.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved per /validate-pr. Tight hotfix, CI green, no security exposure. Minor: the inline (see #942) comment points at an unrelated issue — fix in a follow-up.

@vybe
vybe merged commit d356671 into dev May 26, 2026
12 checks passed
anubis770 pushed a commit to anubis770/trinity that referenced this pull request Jul 16, 2026
… break it

PR Abilityai#943 added `git stash push -u -m "..."` before the dev pull. First
deploy after merge (run 26454209415) failed:

  Saved working directory and index state On dev: auto-stash-deploy-...
  warning: failed to remove trinity-data/archives/: Permission denied
  warning: failed to remove trinity-data/avatars/...: Permission denied
  warning: failed to remove trinity-data/q.py: Permission denied
  No local changes to stash         ← our `|| echo` fallback, MISLEADING
  ...
  error: Your local changes to the following files would be overwritten
  by merge: docker-compose.prod.yml

The dev VM has Docker-volume directories (trinity-data/, archives/,
avatars/) populated by containers running as root. The trinity user
can't unlink those files. `git stash -u` tries to remove ALL untracked
files as part of its working-tree-cleanup phase; the rm fails on the
Docker-owned ones; stash aborts mid-cleanup, leaves the stash entry
created but the tracked-file changes NOT reverted in the working tree;
exits non-zero; our `|| echo` masks the failure as "no changes"; the
subsequent `git pull --ff-only` aborts on the still-modified tracked
file. Same symptom as the original bug, different root cause.

Tracked-only stash (no `-u`) does what we actually want: capture the
docker-compose.prod.yml modification, revert it in the working tree,
let pull proceed. Untracked Docker-owned files are irrelevant to git
pull — they don't block it and don't need to move.

Stash entry from the failed run (`auto-stash-deploy-26454209415`)
still exists on the dev VM and contains the docker-compose.prod.yml
change. Inspect via `git stash list` + `git stash show -p stash@{N}`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants