[GH-ISSUE #4705] [Issue]: Server autonomous scheduler full-sweeps chats.list() + metadata JSON.parse every 60s with no opt-in gate #2582

Closed
opened 2026-10-21 05:50:39 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4705

Originally assigned to: @SpicyMarinara, @kolacheee on GitHub.

Summary

The server-side autonomous scheduler runs a 60-second full sweep forever, with no opt-in gate: every tick calls chats.list() and JSON.parses every conversation chat's metadata, even when zero chats have autonomous messaging enabled and zero clients are connected. On a Termux self-host this is a permanent idle cost. Same shape as the Noodler reconciler fixed in PR #4635 — this is the remaining unconditional sweep.

Follow-up from the mobile-battery investigation (the Termux reporter's "passive discharge" round).

Expected behavior

The 60s sweep short-circuits cheaply when nothing is enabled, and a chat becoming autonomous-enabled (from any device) is still picked up within a bounded window (≤ one sweep interval after the enabling write).

Actual behavior

packages/server/src/services/conversation/server-autonomous-scheduler.service.ts:19 (SERVER_AUTONOMOUS_POLL_MS = 60_000), :368-385 — poll() calls chats.list() every tick unconditionally; shouldConsiderChat (:81-86) JSON.parses each conversation chat's metadata. chats.list() sorts all chats per call (chats.storage.ts:462-465). Runs from ~20s after boot for the process lifetime.

Steps to reproduce

  1. Run the server with zero autonomous-enabled chats and zero clients.
  2. Instrument poll() — full list + parse sweep every 60s indefinitely.

Environment

  • Marinara Engine version: staging d6275fa0b
  • Install type: any; battery-relevant on Termux (phone-hosted server)
  • OS + version: any

Logs, screenshots, or video

Code trace, line references against staging d6275fa0b.

Additional context

Regression-safe fix (designed + red-teamed)

The Noodler pattern (cheap probe before the heavy scan) doesn't transfer directly: the file-native store has no indexes, so "does ANY chat have autonomousMessages: true" has no cheap query — and a substring probe over raw metadata couples to JSON serialization details (a false negative silently kills the feature: the exact regression class to avoid). Instead, gate on a store-level write-generation counter:

  1. In file-backed-store.ts, add a per-table write-generation counter bumped inside markDirty(table) (:1242-1251) — the single choke point every insert/update/delete already funnels through, including transactional writes, cascades, and the capability-package direct writers. Deliberately do not roll the counter back on transaction rollback (a spurious sweep is safe; a missed one is not). Expose getTableWriteGeneration(table).
  2. In the scheduler: skip the sweep when the chats-table generation is unchanged since the last sweep and that sweep concluded no eligible chats exist. Any chats-table write (including an autonomous-enable toggle from another device — all writes come through this server) bumps the generation and re-arms the sweep, so pickup stays ≤ one interval. Server restart naturally re-arms (counter starts fresh ≠ recorded state → sweep runs).

Amendment from the adversarial pass (must be in the implementation): a sweep that exits via the concurrency-cap break (:375, which fires before shouldConsiderChat, so eligibility was never evaluated) or the stopped-return (:374) is inconclusive — never record "no eligible chats" from it. Otherwise: two eligible chats hold runningChats slots inside minutes-scale busy-delay timers (:251-284, slot released in the timer finally), the sweep exits at iteration 1 having considered zero chats, records foundNone, both delayed generations then terminate through paths that only touch the in-memory progress map (presence/backoff/409/discard → clearGenerationInProgress, autonomous.service.ts:244-249, no chats-table write) — and the scheduler goes dormant with enabled chats until an unrelated write. Only a sweep that actually evaluated every chat may record the none-eligible conclusion.

Related (same subsystem, client side): the 30s full-/chats background poll is #4704.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Aug 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4705 Originally assigned to: @SpicyMarinara, @kolacheee on GitHub. ## Summary The server-side autonomous scheduler runs a **60-second full sweep forever, with no opt-in gate**: every tick calls `chats.list()` and `JSON.parse`s every conversation chat's metadata, even when zero chats have autonomous messaging enabled and zero clients are connected. On a Termux self-host this is a permanent idle cost. Same shape as the Noodler reconciler fixed in PR #4635 — this is the remaining unconditional sweep. Follow-up from the mobile-battery investigation (the Termux reporter's "passive discharge" round). ## Expected behavior The 60s sweep short-circuits cheaply when nothing is enabled, and a chat becoming autonomous-enabled (from any device) is still picked up within a bounded window (≤ one sweep interval after the enabling write). ## Actual behavior `packages/server/src/services/conversation/server-autonomous-scheduler.service.ts:19` (`SERVER_AUTONOMOUS_POLL_MS = 60_000`), `:368-385` — `poll()` calls `chats.list()` every tick unconditionally; `shouldConsiderChat` (`:81-86`) JSON.parses each conversation chat's metadata. `chats.list()` sorts all chats per call (`chats.storage.ts:462-465`). Runs from ~20s after boot for the process lifetime. ## Steps to reproduce 1. Run the server with zero autonomous-enabled chats and zero clients. 2. Instrument `poll()` — full list + parse sweep every 60s indefinitely. ## Environment - Marinara Engine version: staging d6275fa0b - Install type: any; battery-relevant on Termux (phone-hosted server) - OS + version: any ## Logs, screenshots, or video Code trace, line references against staging d6275fa0b. ## Additional context ### Regression-safe fix (designed + red-teamed) The Noodler pattern (cheap probe before the heavy scan) doesn't transfer directly: the file-native store has no indexes, so "does ANY chat have `autonomousMessages: true`" has no cheap query — and a substring probe over raw metadata couples to JSON serialization details (a false negative silently kills the feature: the exact regression class to avoid). Instead, gate on a **store-level write-generation counter**: 1. In `file-backed-store.ts`, add a per-table write-generation counter bumped inside `markDirty(table)` (`:1242-1251`) — the single choke point every insert/update/delete already funnels through, including transactional writes, cascades, and the capability-package direct writers. Deliberately do **not** roll the counter back on transaction rollback (a spurious sweep is safe; a missed one is not). Expose `getTableWriteGeneration(table)`. 2. In the scheduler: skip the sweep when the chats-table generation is unchanged since the last sweep **and** that sweep concluded no eligible chats exist. Any chats-table write (including an autonomous-enable toggle from another device — all writes come through this server) bumps the generation and re-arms the sweep, so pickup stays ≤ one interval. Server restart naturally re-arms (counter starts fresh ≠ recorded state → sweep runs). **Amendment from the adversarial pass (must be in the implementation):** a sweep that exits via the concurrency-cap break (`:375`, which fires **before** `shouldConsiderChat`, so eligibility was never evaluated) or the stopped-return (`:374`) is **inconclusive — never record "no eligible chats" from it.** Otherwise: two eligible chats hold `runningChats` slots inside minutes-scale busy-delay timers (`:251-284`, slot released in the timer finally), the sweep exits at iteration 1 having considered zero chats, records `foundNone`, both delayed generations then terminate through paths that only touch the in-memory progress map (presence/backoff/409/discard → `clearGenerationInProgress`, `autonomous.service.ts:244-249`, no chats-table write) — and the scheduler goes dormant with enabled chats until an unrelated write. Only a sweep that actually evaluated every chat may record the none-eligible conclusion. Related (same subsystem, client side): the 30s full-`/chats` background poll is #4704. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:50:39 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#2582
No description provided.