Repository navigation
email/toolloop: fix PR #101 review — UNSEEN-only, fallback model, worker join - #103
Conversation
Five Codex findings on the merged PR #101, all legitimate. P1 — Email: iterate UNSEEN only, not every message in INBOX. The poll did `for i := 1 to TotalMsgs` and Retrieved every message every poll, ignoring the Seen flag. Even with the allowlist filter, the per-poll Retrieve grew linearly with mailbox size. Replaced with `IMAP.SearchMailBox([{skUnseen}])` which makes the server filter — we iterate `MailBox.SearchResult` and only those. Non-allowed senders now get Seen-flagged immediately so the same disallowed messages don't re-Retrieve on every poll either. P1 — ToolLoop: fallback used the primary's model. Passing Cfg.Model = 'claude-opus-4-7' to an OpenAI fallback failed at the remote API and triggered the next fallback even when the chain was otherwise healthy. Now: FallbackModel := Cfg.Fallbacks[fbi].GetDefaultModel; (fall back to Cfg.Model only when the provider returns ''). A per-fallback override (Cfg.FallbackModels: array of string) is a clean follow-up but GetDefaultModel handles the catalog-provider case out of the box. P2 — Email: failed SMTP reply still marked Seen. The Push result was logged but the message was unconditionally StoreFlags-Seen on the next line, so an SMTP outage at reply time silently dropped the answer. Now: track ReplySent + ReplyAttempted; only Seen-flag when (not ReplyAttempted) or ReplySent. Transient SMTP failures leave the message Unseen so the next poll retries; agent-loop failures (poison messages with no reply produced) still get Seen so they don't loop forever. Documented the truth-table inline. P2 — Gateway: email worker never stopped on shutdown. Spawn returned with FreeOnTerminate := True so the gateway's cleanup block couldn't join the worker — it kept running against freed FProvider / FRegistry / FCfg after the gateway torn them down. Now: TEmailChannel keeps an FWorker reference, Spawn assigns it, Stop signals FStop + WaitFor + FreeAndNil(FWorker), Destroy calls Stop defensively. The gateway's finally block calls Email.Stop + Email.Free before tearing down Provider / Reg / Cfg. P2 — Email: poll delay was half the configured interval. The inner wait counted in 500ms ticks but compared to FPollSec (whole seconds), so PASCLAW_EMAIL_POLL=30s slept 15 seconds before the next poll. Now: WaitedMS counts milliseconds, compared to FPollSec * 1000. PollTickMS = 500 preserves the ≤500ms FStop responsiveness. Verified `make clean && make smoke` 11/11 green. https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 13aba29bba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (not ReplyAttempted) or ReplySent then | ||
| IMAP.StoreFlags([SeqNum], sdAdd, [mfSeen]); |
There was a problem hiding this comment.
Retrieve mail without marking it seen before delivery
When SMTP delivery fails, this branch intentionally skips StoreFlags so the message can be retried, but the earlier call to IMAP.Retrieve has already marked the message as read. Indy's TIdIMAP4.Retrieve delegates to InternalRetrieve(..., False, False, ...); the non-peek retrieval marks the message read, while RetrievePeek is the variant that preserves \Seen. As a result, an SMTP outage still removes the message from the next SEARCH UNSEEN result and silently drops the reply. Use RetrievePeek until the reply has been delivered and then add mfSeen explicitly.
Useful? React with 👍 / 👎.
| FStop := True; | ||
| FWorker.WaitFor; |
There was a problem hiding this comment.
Interrupt active email I/O before joining the worker
When shutdown occurs during RunOnePoll, this unconditional join is not bounded by the advertised 500 ms: FStop is checked only outside the active poll and between messages, while IMAP connect/login/search/retrieve and SMTP operations in this class have no configured connect or read timeouts, and an agent call may also still be running. If an email server stalls, gateway shutdown can block indefinitely in WaitFor. Signal or disconnect the active transport, configure finite timeouts, or otherwise make the join bounded before waiting for the worker.
Useful? React with 👍 / 👎.
email: fix PR #103 review — RetrievePeek + bounded shutdown timeouts
Summary
All five Codex findings on merged PR #101, addressed in one commit.
P1 — Email: iterate UNSEEN only
The poll did
for i := 1 to TotalMsgsandRetrievedevery message every poll, ignoring theSeenflag. Even with the allowlist filter, per-poll Retrieve grew linearly with mailbox size.Fix:
IMAP.SearchMailBox([{skUnseen}])makes the server filter — we iterateMailBox.SearchResultand only those. Non-allowed senders now get Seen-flagged immediately so the same disallowed messages don't re-Retrieve on every poll either.P1 — ToolLoop: fallback used the primary's model
Passing
Cfg.Model = 'claude-opus-4-7'to an OpenAI fallback failed at the remote API and triggered the next fallback even when the chain was otherwise healthy.Fix:
FallbackModel := Cfg.Fallbacks[fbi].GetDefaultModel;(fall back toCfg.Modelonly when the provider returns''). A per-fallback override (Cfg.FallbackModels) is a clean follow-up butGetDefaultModelhandles the catalog-provider case out of the box.P2 — Email: failed SMTP reply still marked Seen
The
Pushresult was logged but the message was unconditionallyStoreFlags-Seen on the next line, so an SMTP outage at reply time silently dropped the answer.Fix: track
ReplySent+ReplyAttempted; only Seen-flag when(not ReplyAttempted) or ReplySent. Truth table:P2 — Gateway: email worker never stopped on shutdown
Spawnreturned withFreeOnTerminate := Trueso the gateway's cleanup block couldn't join the worker — it kept running against freedFProvider/FRegistry/FCfgafter the gateway tore them down.Fix:
TEmailChannelkeeps anFWorkerreference,Spawnassigns it,StopsignalsFStop + WaitFor + FreeAndNil(FWorker),DestroycallsStopdefensively. The gateway'sfinallyblock callsEmail.Stop+Email.Freebefore tearing down Provider / Reg / Cfg.P2 — Email: poll delay was half the configured interval
The inner wait counted in 500ms ticks but compared to
FPollSec(whole seconds), soPASCLAW_EMAIL_POLL=30sslept 15 seconds before the next poll.Fix:
WaitedMScounts milliseconds, compared toFPollSec * 1000.PollTickMS = 500preserves the ≤500msFStopresponsiveness.Verification
make clean && make smoke11/11 green.https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code