beads/docs/reference
Julian Knutsen b4c76a676b
fix(gate): ordinary commands wait for maintenance operations, fairly; init waits up to 60s (#7225)
* fix(gate): ordinary commands wait out maintenance, with writer fairness

Ordinary bd commands take the workspace (and physical-root) gate SHARED and
never waited for it, so while another process held it EXCLUSIVELY — bd init
(~8s), backup restore, migrate, bootstrap — they failed at once with "a
maintenance operation is running". In shared-server mode every project
shares one physical-root gate, so another project's bd init broke every bd
command on the machine for its duration.

Ordinary commands now wait:
- up to 15s by default; BEADS_GATE_WAIT_TIMEOUT (Go duration or bare
  seconds, same convention as BEADS_INIT_GATE_TIMEOUT) overrides it, and 0
  restores fail-fast;
- one "waiting for another bd process ..." notice after 2s (shared with bd
  init's notice);
- Ctrl-C aborts the wait. A canceled wait used to fall through to the
  shared fail-open path ("continuing ungated"); it is now a hard error;
- past the bound, the same error naming the holder, the wait, and the knob.

Git hooks stay fail-fast: `bd hooks run` and the bd export/import children
the hooks spawn run with BD_GIT_HOOK=1, treat failure as a warning, and must
not stall a commit or checkout for 15s.

Writer fairness (internal/workspacegate). flock/LockFileEx give no queueing,
and acquisition is non-blocking polling, so a steady stream of overlapping
shared holders could keep a waiting exclusive acquirer (bd init) out past
its bound. A per-gate intent lock (<gate>.intent) fixes that: a waiting
exclusive acquirer whose first try is busy takes the intent lock
exclusively while it polls, and waiting shared acquirers probe it before
every attempt and wait while it is held. The exclusive waiter then waits
only for in-flight holders to drain. The intent is an OS lock, so a crashed
waiter's intent dies with it; its advisory sidecar is ignored when the lock
is free, and intent-lock errors other than "busy" degrade to the old
behavior. Fail-fast acquisitions (Wait <= 0) ignore the queue entirely, so
hooks and BEADS_GATE_WAIT_TIMEOUT=0 behave exactly as before. The intent
lock is taken only while acquiring its gate, directly before it in
AcquireAll's sorted order, so it adds no lock-order cycles.

Nested bd (bd orphans running bd close, hooks fired by git that bd runs)
would deadlock against the queue: the child queues behind an exclusive
waiter that is waiting for the child's parent. A bd process holding its
gates shared exports BEADS_GATE_SHARED_HOLDER_PID; a child whose recorded
ancestor is alive skips the queue (it cannot help the waiter anyway).

Tests: workspacegate fairness (a continuous shared stream no longer starves
an exclusive waiter; verified to fail without the queue), intent lifecycle,
stale intent from a SIGKILLed process, cancellation while queued,
InheritedSharedHold. cmd/bd: holder released after ~3s lets a command
through with one notice; past-bound error text; fail-fast modes (0, hooks);
Ctrl-C is not fail-open; a new command queues behind a waiting bd init
while a child of a shared holder does not; env parsing.
TestSharedServerConcurrent runs a late bd init of a new project during the
client workloads, so both directions are exercised against a real server.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(gate): a queued exclusive waiter may delay shared commands, never fail them

Review follow-up (B1). A doomed exclusive waiter — bd init or restore
blocked behind a long-lived shared holder such as `bd list --watch`,
`bd events tail --follow`, an open `bd edit` editor, or a beads.OpenGated
embedder — held the writer-fairness intent lock for its whole budget, so
every NEW ordinary command deferred to it and failed with "queued for
exclusive access" even though nothing held the gate exclusively. Before
the fairness change those commands ran normally.

Two bounds fix it while keeping the fairness win:
- An exclusive waiter drops the intent lock after maxIntentHold (10s)
  without getting the gate and keeps polling unqueued. In-flight ordinary
  commands drain far faster than that.
- The final attempt of a waiting shared acquisition tries the gate even
  while intent is held, so a shared acquisition fails only when the gate
  itself is held exclusively.

With the final attempt ignoring intent, AcquireAll's 1ns floor for an
exhausted budget is dead code (a Wait-0 attempt behaves the same), so it is
back to 0. queuedDetail drops an intent sidecar left by a dead process (on
Windows its removal can fail while another process is reading it).

Tests: the doomed-waiter scenario (both bounds; each fails with its bound
removed), intent lock order across a two-gate set in both directions, an
exhausted AcquireAll budget, two exclusive waiters handing off the intent,
and BEADS_GATE_SHARED_HOLDER_PID inherited through a real subprocess.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(gate): 30s shared wait, 60s init wait, accurate errors, hook pinning

Review follow-ups for the shared gate wait (S1-S5 and nits), on top of the
workspacegate fix that keeps a queued exclusive waiter from failing
ordinary commands.

- Ordinary commands wait up to 30s by default (was 15s). Queued
  maintenance operations run one after another (two inits fanned out
  across shared-server projects hold the gate back to back, 10-15s each
  under load) and a waiting command waits for all of them. Documented in
  configuration.md and CHANGELOG, including BEADS_GATE_WAIT_TIMEOUT=0 for
  scripts that relied on fail-fast.
- bd init waits up to 60s by default (was 30s). Safe only now that a
  waiting init stops queueing others after 10s.
- Error text: a shared acquisition now fails only when the gate is held
  exclusively, so "running" is accurate; in shared-server mode it says
  "the shared server (possibly from another project)", and the detail
  still names the holder when known.
- hooks.go: the hookSubprocessEnv / bd import comments listed BD_GIT_HOOK's
  consumers without the gate wait, which now reads it to stay fail-fast.
  TestHookSubprocessEnvKeepsGateFailFast pins that hook subprocesses carry
  it and therefore never wait on the gate.
- bd edit's $EDITOR (which can outlive bd by hours) no longer inherits
  BEADS_GATE_SHARED_HOLDER_PID.
- Test hygiene: the cmd/bd test binary never advertises the marker
  (in-process command tests run beside tests that spawn bd) and clears any
  inherited one in TestMain; the notice-delay test runs in 1s, not 3s.
- TestSharedServerConcurrent's late init keeps a 30s bound so the 60s
  default cannot mask a fairness regression.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 00:03:48 -07:00
..
advanced.md docs(events): document the events journal and bd events (bd-opisf) 2026-08-09 01:58:17 +00:00
antivirus.md docs: apply PR #4722 review feedback to user docs 2026-07-12 11:06:57 -07:00
configuration.md fix(gate): ordinary commands wait for maintenance operations, fairly; init waits up to 60s (#7225) 2026-10-05 00:03:48 -07:00
events-journal.md fix(events): journal delete and cascade dep_remove rows carry the request actor; notice when reading a disabled journal (#5985) (#6061) 2026-08-29 04:14:23 -07:00
faq.md docs(troubleshooting): document the two native-Windows integration traps (#5838) 2026-08-20 05:41:39 -07:00
git-integration.md Accept uutils coreutils timeout in generated hooks, and check the shims against real implementations (#6561) 2026-09-27 03:38:45 -07:00
index.md docs(events): document the events journal and bd events (bd-opisf) 2026-08-09 01:58:17 +00:00
json-schema.md fix(contracts): JSON contract fixes before 1.3.0 freezes them — string revision tokens, --set-metadata scalar typing, create --json nano timestamps (#6053) 2026-08-29 05:20:27 +00:00
observability.md refactor(telemetry): adopt standard OTel SDK env vars with BD_OTEL_* back-compat (#3859) 2026-08-03 00:05:06 -07:00
protected-branches.md docs: apply PR #4722 review feedback to user docs 2026-07-12 11:06:57 -07:00
troubleshooting.md Accept uutils coreutils timeout in generated hooks, and check the shims against real implementations (#6561) 2026-09-27 03:38:45 -07:00
worktrees.md fix(hooks): identify the index context behind tracked-file refusals (#6457) 2026-09-27 18:07:43 -07:00