aft icon indicating copy to clipboard operation
aft copied to clipboard

subc: head-scope lookup retries forever when the scope service is absent, once per session in each transport

Open iceteaSA opened this issue 1 month ago • 3 comments

Summary

When the head-scope service (prefrontal-core, method session.scope_identity) is absent, or does not implement the method, the OpenCode plugin keeps asking for it. It does not stop. Each session in each plugin transport has its own retry state, so one host process sends hundreds of lookups each minute. Each lookup opens a management route, sends one request and closes the route.

Observed

On one OpenCode 2 service process with aft-opencode 0.63.5, in about 30 minutes of the plugin log:

  • About 16,000 subc head scope: … lookup=started reason=cache_miss, against about 105 lookup=skipped reason=backoff.
  • All results were errors: code=unknown_method while the module ran (its build did not implement session.scope_identity). After the module was disabled, the results were code=target_unavailable (supervised_not_registered).
  • For a single session, lookups started in bursts of 20 in the same second, and each burst came back every 300 s (for example 16:38:58, 16:43:58, 16:48:58, 16:53:59). The daemon counted about 600,000 session.scope_identity requests in two days, and about 500 refusals each minute after the module was disabled.

Cause

packages/aft-bridge/src/subc-transport.ts on main (d2ad051b9):

  1. Only scope and no_scope end the retries (:2960). Any error result, including unknown_method and a target that is not registered, is treated as temporary: the state backs off from 5 s, doubles, and stops at 300 s (:2972). scheduleHostScopeRetry (:3018) then retries without end, while an unscoped head record or a background subscription exists for the session (:2984–2995).
  2. The retry state is per session and per transport (hostScopeCache, :1515). A host process that holds many transports, about 20 here, gets one retry loop for each session in each transport. This gives the 20 starts in the same second. Nothing shares "the scope service is not available" between sessions or transports.
  3. The log label is misleading. lookupReason starts as "cache_miss" (:2902) and changes only after a bind refusal, so a retry when the backoff is over also logs cache_miss. The small reason=backoff count only shows that few new binds came during a backoff. It does not show that the backoff failed.

Proposal

  • Treat unknown_method, and a target that is not configured or not registered, as "no scope service". Keep that state for the transport, or better for the process, not for each session. Then send one probe at the cap interval, or when the daemon reports that the module registered. Do not retry each session.
  • Optional: share the result between the transports of one process.
  • Log reason=retry_due (or backoff_expired) when a state exists and its backoff is over. Keep cache_miss for a session that has no state.

Expected effect: with no scope service, about one lookup each 300 s for each process, not one for each session in each transport.

iceteaSA avatar Nov 03 '26 16:11 iceteaSA

Confirmed: unavailable-service errors still enter the per-session retry loop and can repeat in every retained transport. The 300-second backoff is a cap per loop, not a process-wide cap on probes; due retries also retain the cache_miss log label. This remains open.

aft-alfonso[bot] avatar Nov 11 '26 19:11 aft-alfonso[bot]

New data on v0.68.0. In v0.68.0 the lookup goes to a reserved, disabled module id. The daemon answers each lookup route with target_unavailable.

After a service restart, the number of lookups goes up each hour. These counts are the lookup=started lines in aft-plugin.log, for one OpenCode 2 service:

Hour (UTC) Lookups Sessions with a lookup
14 (restart) 2,518 218
15 2,943 245
16 3,356 279
17 3,829 325
18 4,263 359
19 4,762 396
20 5,988 534

At about 12 lookups per session per hour, each session is at the 300-second cap. In hour 20, 534 sessions had a lookup. In the same hour, the module log shows only 139 sessions that bound a route and 104 sessions with a logged tool call. The number of sessions goes up because the bridge does not release them.

The cause of that growth is #1533. After one foreground bash command, the idle reaper does not remove a session record. stopUnusedHostScopeRetry stops the retry loop only when the route closes. Thus each session that ran a foreground command keeps its own lookup loop until the plugin stops.

A repair of #1533 limits the loops to sessions that are in use. This issue still applies to those sessions. Also, the daemon's refusal does not tell the bridge whether the service is absent or restarting.

iceteaSA avatar Nov 27 '26 21:11 iceteaSA

Thanks for the hourly counts. They showed both halves of the problem. With #1533 fixed, a session that ran a foreground command goes idle again, so the retry loops are limited to sessions in use.

For those sessions, once a lookup confirms the scope service is unavailable (target_unavailable, unknown_method or unknown_module), every transport in the process shares one retry schedule. That's one probe at a time, starting at 5 seconds and doubling to the 300-second cap. When a probe succeeds, each waiting session looks up its own scope again. First lookups stay independent, so a healthy session never waits behind another session's lookup. Retries after a backoff now log reason=retry_due, and cache_miss means only a session with no state yet. With your numbers, the 12 retries per backoff step across 12 sessions in three transports drop to one. It will ship in the next AFT release, and I'll update here when it's out.

aft-alfonso[bot] avatar Nov 28 '26 09:11 aft-alfonso[bot]