Skip to content

bug(company-os): a heartbeat run key is revoked seconds after start, while the agent is still working #20466

Open
0 of 1 issue completed

Description

@mrveiss

Host evidence, 2026-11-01, on 45f10d1773, from a scheduled CEO run (#20247).

The heartbeat's run key was revoked while the run was still working. Times are from the run's own output:

Time (Riga) Call Result
10:15:12 GET /llc/agent/work-items/next OK
10:15:16 GET /llc/agent/context/<id> OK
10:15:24 GET /llc/agent/work-items/<id> 401
10:21:51 POST /llc/agent/comments 401
10:21:57 GET /llc/agent/context/<id> 401, the same call that worked at 10:15:16

The run itself ended at 10:22:11 with is_error=false. So the agent worked for 7 minutes and could not report its result: its findings reached only its own output.

The key is revoked in one place on the dispatch path: the finally in _dispatch_registry_adapter, after _await_adapter_completion returns. So the likely reading is that the scheduler saw the run as terminal within seconds, while the process was alive. That would also mean billing and run status are read before the run ends. The cause is not established. One thing to test: the base status check and its process identity check (spawn_create_time records psutil's create_time), which may mismatch on this host.

Note the third call's path isn't a route in agent_api.py, but the auth middleware answers first, and the later 401 on a working route shows the key itself was dead.

Acceptance criteria

  • The cause is established, on the host's real behaviour.
  • A run key stays valid until the process has exited (test with a process that outlives several polls).
  • Run status and usage are read after the process has exited (test).
  • Host evidence: a scheduled run posts its comment through the API.

Activity

  1. mrveiss commented on Nov 7, 2026

    @mrveiss
    OwnerAuthor

    Read-only investigation on current main and the install, 2026-11-07. Cause: still not established, so AC1 stays unticked.

    Chain (main)

    1. Spawn: claude_code_adapter._build_state records create_time from spawn_create_time (subprocess_support.py).
    2. Poll: _await_adapter_completion (heartbeat_scheduler.py) checks every 5 s.
    3. Status: subprocess_base checks liveness with probe_pid_identity(pid, create_time). On a mismatch or NoSuchProcess it returns COMPLETED silently.
    4. Revoke: the finally in _dispatch_registry_adapter revokes the run key. Usage is ingested after that.

    So a false COMPLETED revokes the key while the process is still alive, and also reads usage too early.

    Hypotheses

    • H1, process identity drift: most probable, not confirmed. Before the fix, the stamp was the absolute create_time, which is derived from /proc/stat btime.
    • H2, a wrapper PID instead of the CLI's: unlikely. spawn_detached uses start_new_session=True, and proc.pid is the spawned process. I did not check the command for an sh/npx wrapper.
    • H3, a sweep revoking the key: ruled out by code. The only revoke_key callers are run_key.py, the manual API-key route and org deactivation. The key TTL is at least 7800 s.

    Logs: the 2026-11-01 backend log shows the run's creation and spawn, but nothing from 08:15 to 08:25. The pre-fix code logged neither an identity mismatch nor a revoke, so the logs cannot confirm or refute H1. There has been no scheduled spawn since 11-02, so there is no post-fix host run yet.

    Next (one PR):

    • probe_pid_identity logs a WARNING (pid, expected, actual) when it judges a mismatch COMPLETED, so this class becomes visible.
    • AC2 test: a scheduler-level fake adapter stays RUNNING for N polls, and the key is not revoked before the terminal poll.
    • AC3 test: status and usage are read only after the process exits, using a real subprocess that writes its result late.

    AC4 (host evidence) needs a scheduled run on the install that posts its comment.

  2. mrveiss commented on Nov 8, 2026

    @mrveiss
    OwnerAuthor

    Part landed: PR #21068 merged to main via merge train #21184 (merge commit 209d547): run-identity mismatch is logged and the run-key and usage ordering are pinned. This PR referenced the issue (Refs), it did not close it; the issue is open and its remaining acceptance criteria are not claimed as delivered.

  3. added a commit that references this issue on Nov 8, 2026
  4. github-actions commented on Nov 8, 2026

    @github-actions
    Contributor

    Landed in #21201, merge 085943d594

  5. mrveiss commented on Nov 9, 2026

    @mrveiss
    OwnerAuthor

    Progress check on main and on the local install, which was updated to main 85d5d2e at 21:13 Riga.

    AC Evidence Met
    A run key stays valid until the process has exited llc/tests/test_run_key_outlives_process_20466.py::test_run_key_is_revoked_once_and_only_after_the_terminal_poll: 4 RUNNING polls, then the terminal poll, then exactly one revoke. Also test_run_identity_survives_clock_drift_20466.py::test_exited_process_is_completed_after_several_running_polls. yes (ticked)
    Run status and usage are read after the process has exited llc/adapters/tests/test_usage_read_after_exit_20466.py::test_status_is_running_while_alive_and_usage_arrives_only_after_exit: a real child blocks until released, then the exact [RUNNING]*3 + [COMPLETED] sequence; usage arrives only on the terminal poll. yes (ticked)
    The cause is established on the host's real behaviour Since the update, 5 scheduled wakes (21:15 to 22:30) were all "no eligible work (idle wake recorded)", with no run created. Nothing to observe yet. Backend log: 0 run-key revocation lines. not yet
    Host evidence: a scheduled run posts its comment through the API No scheduled run has had work since the update, so there is no comment to show. not yet

    The two host ACs stay open until a scheduled run actually picks up work on the updated install.

  6. mrveiss commented on Nov 20, 2026

    @mrveiss
    OwnerAuthor

    AC audit against main 13685f3 (2026-11-20). A branch deleted as ZERO means its content is on main; it does not by itself meet the ACs.

    PR #21068 closed (ZERO); #21201 merged (poll test); #20475 closed.

    • AC2, AC3 already ticked.
    • Open: AC1 (cause established on the host's real behaviour) and AC4 (host: a scheduled run posts its comment through the API) need host evidence, which a merge does not give. Left unticked.
  7. mrveiss commented on Nov 21, 2026

    @mrveiss
    OwnerAuthor

    Read-only host check, 2026-11-21 02:43 Riga. The deployed commit is bcd41c623d, which includes the #20466 AC2/AC3 code and the probe_pid_identity mismatch logging.

    • probe_pid_identity mismatch warnings over 7 days of logs: 0. Mentions of "revok": 0. Instrument check: the same log carries 148 WARNING lines, so warnings are being captured.
    • Scheduled heartbeat runs that picked up work in 7 days: 0 (634 wakes, all skipped with "no eligible work").

    AC1 (cause): cannot tell. The revocation hasn't reproduced since the update, but no run has had the chance. AC4 (a scheduled run posts its comment): not met. No scheduled run picked up work, so no comment could be posted. Run and comment rows were not queried (no read-only database credential).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions