update_cmd_pause_gate.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """The paused gateways' tree gate for the update's checkout-writing steps (Windows pause, #132338).
  2. ``_checkout_move`` binds one git step (switch, merge, reset, upstream sync, rollback, stash, churn
  3. cleanup) to the pause record BEFORE git writes a file; ``update_cmd`` re-exports these names.
  4. """
  5. from contextlib import contextmanager
  6. from hermes_cli import update_pause_record as _pause_record
  7. @contextmanager
  8. def _checkout_move(token, *targets, paths=(), revert=False):
  9. """Run one checkout-writing git step (switch, merge, reset, upstream sync, rollback, stash) with
  10. the paused gateways' tree gate bound to it BEFORE git writes a file: the commit(s) it can move
  11. to and the tracked *paths* it rewrites in place (stash push/apply, ``reset --hard``,
  12. ``checkout -- <paths>``; *revert*: every tracked change it finds, put back to HEAD), on a
  13. baseline taken at the HEAD it starts from. Without that, a step
  14. git leaves half-written at an unmoved HEAD is judged against whatever refs a later fetch left,
  15. or against no baseline at all once an earlier step moved HEAD. A failed record write raises:
  16. the step must not run.
  17. The first step from the pause's own HEAD extends the pause's baseline (it knows the edits that
  18. were already there). A later step gets a baseline of its own, retired once git returns with the
  19. tree whole: HEAD moved and every file the move wrote holds the new commit, the tree exactly as
  20. the step found it, or the step reported ``whole`` on the dict this yields (a stash restore that
  21. applied cleanly changes the tree by design). A move is judged the moment git returns (see
  22. ``_settle_checkout_move``)."""
  23. step: dict = {}
  24. targets = sorted({t for t in targets if t})
  25. pause_id = (token or {}).get("pause_id")
  26. root = _pause_record.install_root() if pause_id and (targets or paths or revert) else None
  27. found = _pause_record.tree_state(root) if root else None
  28. if found is None: # nothing paused, nothing bound, or not a git checkout: no tree gate to bind
  29. yield step
  30. return
  31. paths = {*paths, *(found["dirty_at_pause"] or [] if revert else ())}
  32. baselines = token.setdefault("baselines", [])
  33. move_id = f"{pause_id}@{found['pre_sha']}"
  34. baseline = next((b for b in baselines if b.get("pre_sha") == found["pre_sha"]
  35. and b.get("pause_id") in (pause_id, move_id)), None)
  36. if baseline is None:
  37. baseline = {"pause_id": move_id, **found}
  38. baselines.append(baseline)
  39. for key, bound in (("move_targets", targets), ("move_paths", paths)):
  40. if bound:
  41. baseline[key] = sorted({*baseline.get(key, []), *bound})
  42. _pause_record.write({**token, "resume_needed": True})
  43. try:
  44. yield step
  45. finally:
  46. _settle_checkout_move(token, baseline, found, root, step)
  47. def _moves_for(token):
  48. """``_checkout_move`` bound to *token*: the ``checkout_move`` the git and stash helpers take."""
  49. return lambda *targets, **bound: _checkout_move(token, *targets, **bound)
  50. def _settle_checkout_move(token: dict, baseline: dict, found: dict, root, step: dict) -> None:
  51. """Record what the returned step left. A moved HEAD gets its verdict now: git can move HEAD
  52. past a file it failed to write, and only now are the move's paths untouched by later steps
  53. (dependency syncs, stash restores). A step's own baseline is retired when the tree is whole."""
  54. now = _pause_record.tree_state(root)
  55. moved = now["pre_sha"] != found["pre_sha"]
  56. torn = _pause_record.torn_by_move(root, found, now["dirty_at_pause"]) if moved else None
  57. if moved and torn is not None: # unknown stays unjudged: the gate judges it at resume time
  58. baseline.setdefault("landed", {})[now["pre_sha"]] = torn
  59. whole = torn == [] if moved else step.get("whole") or all(now[key] == found[key] for key in found)
  60. if baseline["pause_id"] != token["pause_id"] and whole:
  61. token["baselines"] = [b for b in token["baselines"] if b is not baseline]
  62. _pause_record.write({**token, "resume_needed": True})