Skip to content

refactor: let letter delivery read the turn the row is drawn from - #1954

Merged
yansfil merged 6 commits into
mainfrom
refactor/turn-authority
Nov 1, 2026
Merged

yansfil merged 6 commits into
mainfrom
refactor/turn-authority

Conversation

@yansfil

@yansfil yansfil commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

화면의 상태(멈춤/작업 중/승인 대기)와 편지 초인종이 같은 사실을 따로 계산해서, 둘이 어긋나면 화면은 멈췄다고 하는데 편지는 "작업 중"으로 붙잡히는 일이 생겼습니다.
이제 에이전트의 차례(작업 중인지, 승인 프롬프트가 떠 있는지, 에이전트 자신의 질문·계획 승인이 기다리는지)는 agent_state::turn_projection 한 곳이 정하고, 화면 행·초인종·도구 편지 전달이 모두 그 답을 읽습니다.
입력해도 되는지(Herdr의 blocked 프롬프트와 준비 상태, 초안·조용한 시간, 컴포저 읽기)는 차례와 따로 남은 별도 관문입니다. "멈춤으로 보임"을 "입력해도 됨"으로 읽지 않습니다.
화면 변경 없음 (core와 문서만).

어긋남과 고른 답:

  1. 훅은 턴 끝, Herdr는 아직 working: 화면 멈춤, 편지는 5분 작업 대기 → 이제 쉬는 규칙(조용한 시간·초안)으로 울림.
  2. 훅은 working, Herdr는 idle(성능과 멀티에이전트 설계를 단순하게 지키도록 설계 문서 가이드를 보강한다 #348 백그라운드 대기): 화면 작업 중, 편지는 쉬는 것처럼 울림 → 이제 작업 중 규칙.
  3. 훅이 턴을 보고했는데 Herdr는 blocked: 화면엔 승인 없음, 편지는 blocked로 보류 → 계속 보류하되 not_ready(Herdr가 거기서 agent.prompt를 거부함)로 이름 붙임.
  4. 훅이 working인데 세션 읽기에 지난 턴의 계획 승인이 남음: 화면엔 승인 없음, 편지는 awaiting_operator → 차례를 따름.

리뷰에서 나온 두 가지도 고쳤습니다.

  • 세션 읽기가 새 질문을 찾았는데 Herdr 상태는 그대로일 때, 사실을 깔기 전의 짧은 틈에 지난 차례(질문 없음)로 편지가 질문 위에 입력될 수 있었습니다. 이제 관측이 그 pass의 토큰을 돌려주고, 그 토큰을 관측한 payload·라벨 overlay와 함께 들고 오는 그 pass의 ingest만 차례를 깝니다. 다른 ingest(예: 에디터 닫기 상태가 이전 overlay로 읽은 것)는 아무것도 인증하지 않으며, 그 틈에는 어떤 편지도(--now·--force 포함) turn_unprojected로 기다립니다.
  • 같은 턴에 다시 울릴지를 Herdr 상태 문자열로 정하던 것을 코어의 차례로 바꿨습니다. Herdr가 idle인 채 훅만 턴을 시작·끝내도 다음 편지가 울리고, 같은 턴에서 건너뛴 편지는 announced로 이름이 남습니다.

Review

  • 판단해 주세요: 어긋남 1에서 Claude Code·Codex는 Herdr가 working이라 해도 훅이 턴 끝을 말하면 5분을 기다리지 않고 울립니다. 훅 없이 시작된 실제 턴(백그라운드 작업 깨우기)이면 그 줄은 기존 턴 중 전달처럼 현재 도구 호출 뒤에 들어갑니다.
  • 조심해서 볼 파일: herdr-core/src/delivery/doorbell.rs judge - 보류 순서와 관문 분리(failure path). herdr-core/src/runtime/delivery.rs observe_delivery·lay_delivery_turns·projected_turn - 관측이 ObservedPass 토큰을 돌려주고, 그 토큰을 든 깔기만 같은 번호의 관측을 인증. herdr-core/src/runtime/session.rs ingest_observed_session - 토큰·overlay·payload를 한 번에 받는 유일한 local 깔기 경로, 나머지 ingest(ingest_session*, 에디터 닫기 상태)는 깔지 않음. herdr-core/src/session_sync/coordinator.rs - 기기 패인은 coordinator가 훅·세션 사실을 깐 뒤 다시 잡은 잠금에서 깜. herdr-core/src/delivery/doorbell.rs Episode - 다시 울릴지의 기준이 Herdr 상태 대신 코어 차례와 그 변경 시각.
  • 테스트 픽스처 변경: hided/tests/it/remote_delivery.rs 여정은 Claude Code의 프롬프트 훅만 돌리고 턴을 끝내는 Stop 훅은 돌리지 않아, 훅 기준으로 턴이 영원히 working이었습니다(어긋남 2의 규칙대로 재연결 편지가 5분 대기). 실제 에이전트처럼 각 턴 뒤에 Stop 훅을 돌리게 했습니다. Claude Code의 Stop 훅은 편지를 가져가지 않으므로 여정이 확인하는 전달 경로는 그대로입니다.
  • 비용 검토: 세션 ingest마다 에이전트당 turn_projection 한 번(메모리만, 행 projection이 이미 하는 계산)과 관측 맵 갱신. I/O·알림·새 타이머 없음, 런타임 잠금 안의 일은 기존 ingest 안의 메모리 작업뿐.
  • turn_unprojected가 영구 보류가 되지 않는 이유: 관측과 깔기는 같은 sync pass에서 이어지며 그 사이 출구는 죽은 런타임(로컬)이나 교체된 기기 coordinator(후임이 다시 관측하고 깜)뿐. 길어질 수 있는 유일한 경우는 이 기기의 패인 이동 중이며, 그동안 화면도 이동 전 패인을 그리고 이동의 publication barrier가 풀리면 끝남.

Evidence

  • 확인한 것: 리뷰 P1 - a_question_read_under_an_unchanged_herdr_state_never_rings_on_the_last_turn: 같은 Herdr 순번에서 관측과 ingest 사이를 멈추고, 보통·--now·--force 편지 모두 turn_unprojected. 그 틈에 이전 overlay로 같은 payload를 다른 ingest가 먹어도 그대로 turn_unprojected, 그 pass의 ingest가 커밋한 뒤에야 행은 question이고 편지는 awaiting_operator. 깔기를 소비 시점 번호로 찍던 이전 방식으로 되돌리면 Ok(())(입력됨)로 실패함을 확인.
  • 확인한 것: 리뷰 P2 - a_turn_the_hook_alone_moves_decides_the_next_bell_while_herdr_stays_idle: Doorbell::pass로 Herdr idle·순번 2 고정, 같은 턴 announced, 훅만의 턴 시작·끝 뒤 울림, 훅 턴 중 앞 편지 수신 뒤 다음 편지 울림. 옛 상태 기준 Episode로 되돌리면 실패함을 확인.
  • 확인한 것: runtime::tests::turn_authority 10개 - 훅·Herdr·세션 읽기를 교차시켜 실제 경로(훅 보고 → 관측 → 세션 ingest)로 행의 activity/demand/blocked와 초인종 보류 이름을 함께 단언(어긋남 1~4, 무훅 blocked, 훅 승인 대기, 비동기 질문, 초안·조용한 시간·Grok working 관문, 다음 상태가 같은 pass ingest에서 깔림).
  • 확인한 것: scripts/verify-cargo.sh nextest -p herdr-core 3726개 통과, nextest -p hided 전달 관련 11개 통과, clippy -p herdr-core -p hided --all-targets -D warnings 통과, cargo fmt.
  • 확인한 것: 수면은 이미 행의 demand/activity를 읽음(agent_sleep.rs); 끝내기 직전 agent.get 재확인은 native fence로 유지. typed-input·unreadable 보류는 그대로.
  • 확인한 것: remote mailbox 레인의 remote_delivery·held_letter_notice 5개(--run-ignored only, 고정 Herdr 0.9.5, 격리 픽스처)를 로컬 macOS에서 통과 - 기기 패인 초인종이 coordinator가 깐 차례로 울림. Stop 훅 추가 전에는 재연결 편지가 working으로 보류되어 실패함을 확인.
  • 확인하지 못한 것: 실제 TUI(격리 Herdr)에서 어긋남 1·2를 재현한 실측은 하지 않음. Linux·Windows는 CI에 맡김(플랫폼 분기 없는 core 변경).
Verification record

A pass observes Herdr's list under the lock and lays the hook's and the session read's facts after it, so a turn an earlier pass laid stood for a question read while Herdr's state stayed the same. Each observation now carries its pass's number and only that pass's laid turn is read; between the two steps a letter holds as turn_unprojected.

The bell's episode is the core's turn and when it last moved instead of Herdr's status, so a turn the hook alone runs takes the next letter once the last is taken, and a letter skipped within a turn is held as announced.
The observation hands back its pass's token, and only the ingest that carries it, with the payload and label overlay that pass observed, lays turns; any other ingest, such as an editor's close status read with the overlay the runtime held before, certifies nothing.
@yansfil
yansfil merged commit 49b510c into main Nov 1, 2026
33 checks passed
@yansfil
yansfil deleted the refactor/turn-authority branch November 1, 2026 00:39
yansfil added a commit that referenced this pull request Nov 1, 2026
…w status (#1967)

## Summary

Hide checks a brief or letter it typed against the agent's own record.
If the agent never records it, Hide tells the parent and types it again
once someone answers the screen.
That check decided "the agent is working" from Herdr's raw status. The
row, the doorbell and sleep all read the core's one turn instead (hook
first, then session file, then Herdr, #1954).
When the two disagreed, the typed-input decision disagreed with the row:
- **hook says working, Herdr says idle**: the parent got a false "has
not confirmed" notice, and once the screen was answered the brief could
be typed into a working agent a second time.
- **hook says the turn ended, Herdr still says working**: the record
wait never started, so the notice and the retype never came.

Now the confirmation, the composer read before a retype, and omp's "a
turn after the typing is its record" all read the same projected turn.
The composer read goes through the same gate a bell is typed through,
now shared with the doorbell (`doorbell::unheld_turn`,
`doorbell::takes_keys`).

One more rule for the same decision: a brief also counts as taken when
the agent's own hook reports a turn that started after the typing, and
no other input or bell could have started it.
It is confirmed `by: turn`, as omp's already is: the parent is not told,
the row asks nothing, and it is never typed again, even if the prompt
digest never matches.
Today a long brief to Claude Code reads unconfirmed though it was taken
(its digest is being fixed separately), and nothing may type a brief
twice (principle 11).

## Review

- **Judgement call**: when the core has no turn for the pane (a stale
read, or the moment between a pass's observation and the turn it lays),
nothing moves, and the row and `hide agent show` keep the last verdict
for that typing (`plans.input_verdicts`).
A state ends this, never a timer: the next agent list (asked every
second) lays a fresh turn; a stale read ends with the list that confirms
the agents again, and until then the row also shows activity unknown; a
pane Herdr no longer lists drops its input.
Without this, the doorbell's 250 ms pass falls into that moment on every
session pass. It would drop the asked reread (so the verdict starts
over) and make the row's ask blink.
  A stale read used to drop the row's ask; it now keeps it.
- **Behavior change for tool-letter agents** (OpenCode, Pi, omp,
Antigravity, Grok): when their hook says idle and Herdr still says
working, they are now judged at rest, so after the record wait the
parent gets the "has not confirmed" notice.
Hide still does not retype or read the composer while Herdr reads them
working (`takes_keys`, the same rule the bell follows).
- **Judgement call**: the turn rule counts only a turn the hook reports
(`TurnProjection::reported`), never Herdr's reading alone. It misses a
turn shorter than one pass, input during the turn, a core restart (the
latch is memory until the store confirms), and agents whose hook reports
no turn. Those keep the record-only rule.
- **Files to watch**:
- `herdr-core/src/delivery/doorbell.rs`: `judge` is split into two
helpers with the same holds in the same order; the bell's behavior must
not change.
- `herdr-core/src/delivery/typed.rs`: `composer_readable` is now the
doorbell's gate, so for a kind without tool letters, a turn at rest that
Herdr still reads as `working` gets its composer read. The off-lock
`readiness.accepts_input()` check still holds before any key is sent.
- **Cost**: no new work per input. Each doorbell pass rebuilds
`plans.input_verdicts`, one entry per unconfirmed input (bounded by open
inputs), under the lock it already holds, with no I/O.
- **Left on purpose**: `delivery/watch.rs` reads Herdr status changes as
an inactivity clock for quiet-child warnings, and `worker.rs`
`same_target` compares samples. Neither decides whether the agent works
now, so neither is changed.

## Evidence

- **Checked**: two runtime tests cross the hook and Herdr and assert
what the doorbell receives (`typed_input_work` notice and retype,
`input_asks` row). Both fail against the old raw-status read and pass
now.
- **Checked**:
`a_stale_read_keeps_the_briefs_verdict_until_a_fresh_turn_is_laid`
drives `telemetry_stale` through Unsettled and back: across a 10-minute
stale read the row's ask and `agent show`'s `unconfirmed` stay, nothing
is sent, and the reread is not asked again. A fresh turn continues the
verdict, an unlaid pass holds it the same way, and the pane leaving
drops it. It fails when a stale read falls back to Waiting, as before
this PR.
- **Checked**:
`a_brief_whose_hook_reports_a_turn_after_the_typing_is_confirmed_by_that_turn`:
during the turn and after it ends (record still unmatched, screen
answered), the doorbell gets only `recorded: turn`, with no notice,
reread or resubmit; the row has no ask and `agent show` lists no pending
brief.
- **Checked**:
`a_turn_herdr_alone_reads_or_other_input_started_is_no_record_of_the_brief`:
Herdr-only working, a key after the typing, and a letter bell after the
typing each still end in the parent's notice.
- **Checked**: omp's turn record now follows the core's turn (an omp
hook turn while Herdr reads idle confirms the brief). The existing
typed, doorbell and turn_authority tests pass unchanged except fixtures
that now lay a turn.
- **Checked locally**: full `herdr-core` nextest, clippy `-p herdr-core
--all-targets`, `cargo fmt`, `check-engine-boundary.py`,
`check-core-touches-no-machine.py`,
`check-capability-readers-off-lock.sh`.
- **Not checked**: no live run with a real agent at a hooks-review or
trust screen. The change is a pure decision over facts the core already
holds, so it was proven at the runtime layer. Linux and Windows run on
PR CI only.

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

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

1 participant