Skip to content

drop한 lock을 descriptor를 닫기 전에 풀어 다른 thread의 child가 lock을 붙잡지 않는다 - #821

Merged
yansfil merged 1 commit into
mainfrom
fix/delivery-claim-lock-flaky
Oct 8, 2026
Merged

yansfil merged 1 commit into
mainfrom
fix/delivery-claim-lock-flaky

Conversation

@yansfil

@yansfil yansfil commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Closes #820

Summary

Unix의 flock은 open file에 붙고, 다른 thread가 시작하는 child는 자기 프로그램을 시작할 때까지 descriptor 사본을 쥡니다.
hide_platform::fs::lock::Lock은 drop할 때 descriptor를 닫기만 해서 그 순간 lock이 남았고, 0초 대기로 lock을 잡는 delivery::claim이 그것을 바쁜 lock으로 읽어 unit test가 가끔 실패했습니다.
이제 Lock은 drop할 때 lock을 먼저 풀고 닫습니다. herdr-core의 label generator lock이 같은 이유로 이미 하던 일을 공용 lock으로 옮긴 것입니다.
lock 테스트가 이 현상을 흡수하려고 두던 10초 재획득 대기(RELEASED_WITHIN)는 지웠습니다.

Review

  • 조심해서 볼 파일: hide-platform/src/fs/lock.rs - 모든 fs::lock 사용자(listener, save, kit account lock, device helper sync)의 해제 시점이 "child exec 뒤"에서 "drop 즉시"로 당겨집니다. lock을 child에 넘겨 쥐게 하는 호출자는 없습니다(descriptor는 close-on-exec).
  • 판단해 주세요: unlock 실패는 무시하고 뒤따르는 close에 맡깁니다. 바뀌기 전 동작과 같고, drop에는 알릴 호출자가 없습니다.

Evidence

  • 확인한 것: 새 계약 테스트 fs::a_dropped_lock_is_free_while_a_child_started_meanwhile_still_holds_its_descriptor가 child를 fork 뒤 exec 전(pre_exec)에 pipe로 붙잡은 채 lock을 drop하고 0초로 다시 잡습니다. unlock을 빼면 expected the lock, got TimedOut로 실패하는 것을 먼저 확인했습니다.
  • 확인한 것: verify-cargo.sh test-scoped -p hide-platform -p hide-agent-hooks -p hide-kit 3번(load 21-31), nextest 같은 세 crate 568개 통과(load 33), verify-cargo.sh lint 통과.
  • 확인하지 못한 것: 원래 실패(delivery.rs:303)는 드물어 수정 전후 빈도를 비교하지 못했습니다. 원인 모델은 이슈의 추정과 새 계약 테스트로만 뒷받침됩니다.
  • 확인하지 못한 것: 이 수정 중 load 30에서 spawn_guard::a_daemon_that_cannot_be_asked_lets_the_call_run_and_leaves_one_diagnostic이 "nothing on the hook's stderr either"에서 한 번 실패했고 8번 다시 돌려 재현하지 못했습니다. 이 PR과 무관한 부류로 보이며 메시지를 잡지 못했습니다.
  • 확인하지 못한 것: hide-platform 밖 crate의 전체 suite와 Linux, Windows는 CI에 맡깁니다.

Under flock a lock belongs to the open file, and a child another thread
starts holds a copy of the descriptor until it starts its program, so a
Lock that only closed its descriptor stayed held for that moment and a
one-try lock elsewhere read it as busy. The delivery claim takes its lock
with no wait, so its unit test failed when another test thread started a
child between two claims (issue 820).

Lock now unlocks when it is dropped. A contract test holds a child after
its fork and before its program while the lock is dropped and retaken with
one try; without the unlock it reads TimedOut. The lock tests' 10 s retake
wait, which only absorbed this, is gone.
@yansfil
yansfil merged commit b33fb57 into main Oct 8, 2026
20 checks passed
@yansfil
yansfil deleted the fix/delivery-claim-lock-flaky branch October 8, 2026 19:50
yansfil added a commit that referenced this pull request Oct 8, 2026
Related: #769 (adapter declarations this builds on), #810 (no agent row
draws an in-process subagent count yet)

## Summary

OpenCode has no command hook, so inside Hide it got none of Hide's
collaboration: no letters on its prompt, no spawn guard, no Memory, no
subagent count.
This adds a Hide-owned OpenCode plugin,
`~/.config/opencode/plugins/hide.js` beside Herdr's, written and removed
only by `hide-agent-hooks` and installed as OpenCode's hook piece in the
kit.
Inside a Hide-managed Herdr pane it adds guidance, Project Memory and
waiting letters to a root session's prompt as one synthetic part, and
confirms the letters only after OpenCode reports that part stored.
It also refuses `herdr agent start` with the spawn guard's reason,
refuses `question` in a Factory worker pane, and reports subagent counts
from child-session events.
OpenCode stays unbelled, since its resume picker reads `done`. Outside a
managed pane the plugin registers nothing.

Settings now name OpenCode's piece `Plugin`, drop its `Basic` chip, and
say on any agent's row why its hook could not be put in (for example,
the agent's config folder does not exist yet).

## Review

- **판단해 주세요**: the agent row now shows one quiet line when a hook is
Absent with a reason, for every agent (B14). On Windows, Cursor's row
says its hook is not written there.
- **판단해 주세요**: the subagent count reaches Herdr's pane tokens and the
snapshot (`children.subagents`), as Claude Code's does. No row draws it
yet for any agent: #810.
- **조심해서 볼 파일**: `hide-agent-hooks/src/opencode/plugin.js` - runs inside
the operator's OpenCode. Every hook has a budget and fails open except
the refusal.
- **조심해서 볼 파일**: `herdr-core/src/delivery/mailbox.rs` - an unbelled kind
with a prompt hook now takes letter bodies on any prompt. The plugin
confirms later.
- **조심해서 볼 파일**: `herdr-core/src/runtime/memory.rs` - receipts from
OpenCode's label read are recorded on one owned thread off the lock,
with queues capped at 64 and 256.
- **조심해서 볼 파일**: `.github/workflows/pr.yml`, `herdr-update.yml` - the
rust lane installs Node 22, because `hide-agent-hooks` runs the plugin
under Node in its tests.
- **비용 검토**:
- Per OpenCode prompt: one helper process, plus one count report after
it.
- Per child status change: at most one running report, with only the
newest waiting.
- Session bookkeeping is capped at 512, evicting finished children
first.
- Nothing is added on hided's high-frequency paths. The lineage pass
reads one kit status instead of cloning the kit.

## Evidence

- **확인한 것**: sealed suite `verify-cargo.sh test` and `lint` PASS on the
final head, merged with main at #821.
- **확인한 것**: Node plugin test, run with OpenCode 1.18.30's hook and
event shapes:
  - root and child prompts;
  - deferred confirm and an unstored prompt;
  - budgets, plus slow, failing and garbled helpers;
  - spawn and question refusal, including a subagent's question;
  - background child counts;
  - more than 512 sessions.
- **확인한 것**: `web/e2e/opencode-plugin.spec.ts` on a private pinned Herdr
0.9.3, where the kit writes the plugin and a stand-in OpenCode in two
panes loads it:
  - a letter rides the next prompt and is confirmed only once stored;
  - `herdr agent start` is refused;
- Herdr's tokens and the snapshot's `children.subagents.working` go 1
then 0.
- 9/9 over 3 repeats, 6/6 over 2 repeats after the last fix; with
`agent-adapter-layer.spec.ts` (OpenCode no longer Basic) 4/4 on the
merged head.
- **확인한 것**: unit tests for every file state (current, stale, edited,
foreign, config folder absent, Windows); kit apply, Reinstall and
switch-off; Memory receipts from synthetic parts; mailbox next-prompt
pull.
- **확인하지 못한 것**: the live check with a real OpenCode (PRD D-13) ran but
measured nothing (exit 3, every cell UNKNOWN):
- run: `scripts/agent-live-check.py --agents opencode`, OpenCode
1.18.30, `opencode-go/deepseek-v4.1-flash`, private pinned Herdr 0.9.3,
this branch's hided;
- Herdr saw OpenCode idle and ready in every scene, but every screen
read came back empty, so all ten scenes timed out at arrival; OpenCode
draws a full-screen interface the tool's read does not return;
- the tool loads Herdr's integration, not Hide's plugin, so the letter
cell is UNKNOWN as well;
- nothing unsafe was seen, cleanup was confirmed, and no login or
operator setting was changed;
- still unproven live: a letter and a refusal reaching the model, B8
after repeated refusals, coexistence with Herdr's plugin, Bun.
- The event samples in `tests/fixtures/opencode/events-1.18.30.json` are
built from OpenCode's source, not captured.
- **확인하지 못한 것**: a machine whose OpenCode reads `$XDG_CONFIG_HOME` loads
neither Hide's nor Herdr's plugin from `~/.config/opencode` (a
limitation shared with Herdr).

<details><summary>Follow-up improvements (not needed for this
contract)</summary>

- Claude Code's hook is silenced inside OpenCode even where Hide's
plugin is not working (Windows, a foreign `hide.js`).
- `hide inbox` offers no `ack_command` to OpenCode; letters are
confirmed by the plugin only.
- Guidance is read once when OpenCode loads the plugin.
- A failed confirm can deliver a letter again (at least once); SIGKILL
of a helper can orphan its grandchildren; no symlink tests for
`hide.js`.
- `done` stops growing once finished children are evicted at 512.
- No e2e for the question guard (B19) or for an idle pane receiving no
input (B5); both are covered below e2e.
- A bash command over the helper's 256 KiB input, or a call past the 8
running helpers, passes the spawn guard unchecked (it guards accidents,
not a sandbox).
- A subagent's `question` from a session the plugin never saw created
asks about the subagent's own id, which the Factory guard does not know.
- On a timeout the helper is killed but the `hide` call it started ends
at its own deadline.
- Letter ids are not checked for a leading `-` on pull or confirm; they
come from Hide's own ledger.
- `herdr-update.yml` takes `actions/setup-node@v4` by tag, as the rest
of the repository does.

</details>

<details><summary>Verification record</summary>

- PRD: `agents/prd/opencode-plugin/prd.md` (sha256
91a061f42e55e18f8f0834a17d9b98c8623e8e8cdf3e8d579208af0e8b9d9448)
- Base b33fb57 → head bb7bf9d, verified 2026-10-08T20:08:54.127Z: PASS
- Verification report:
`agents/runs/opencode-plugin/verification-report.json` and
`agents/runs/opencode-plugin/verification-report.md`; source fingerprint
01745cef3e161c615ddbf9ebbd8a6d16fb839f090be81b6040ff408874b81036
- Suites:
  - S1: GREEN `bash scripts/verify-cargo.sh test` (exit 0)
  - S2: GREEN `bash scripts/verify-cargo.sh lint` (exit 0)
- Reviews:
- Fidelity (full contract, earlier head): Fix now items fixed before the
follow-up rounds; closure confirmed, nothing blocking.
- Code (full contract, then follow-ups through the merge with main): Fix
now items fixed (plugin eviction, report coalescing, root lookup for
question, receipts off the lock); nothing blocking on the merged head.
- Security (on the merged head): Fix now 1, a letter or Memory line
could close the plugin's own reminder tags; fixed with a test that fails
without the fix. Six hardening follow-ups listed above.
- Code follow-up (after merging #815/#816/#817): Fix now 1, the OpenCode
tests copied or wrote the programs they start, against TESTING.md rule
8; converted to the build's stand-ins. Merge resolutions confirmed.
- Evidence: none registered separately.
- Changed paths: 63 (see Files changed)
- Delivery: mode pr, branch `feat/opencode-plugin`, base `main`

</details>
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.

drop한 lock이 다른 thread가 시작한 child에 남아 0초 대기 claim이 바쁜 lock으로 실패한다

1 participant