Skip to content

email/toolloop: fix PR #101 review — UNSEEN-only, fallback model, worker join - #103

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr101-review-fixes
May 31, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr101-review-fixes

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

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 TotalMsgs and Retrieved every message every poll, ignoring the Seen flag. Even with the allowlist filter, per-poll Retrieve grew linearly with mailbox size.

Fix: IMAP.SearchMailBox([{skUnseen}]) 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.

Fix: FallbackModel := Cfg.Fallbacks[fbi].GetDefaultModel; (fall back to Cfg.Model only when the provider returns ''). A per-fallback override (Cfg.FallbackModels) 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.

Fix: track ReplySent + ReplyAttempted; only Seen-flag when (not ReplyAttempted) or ReplySent. Truth table:

ReplyAttempted ReplySent StoreFlags?
False (no reply) — Yes — don't loop on poison
True True Yes — delivered
True False No — transient SMTP, retry next poll

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 tore them down.

Fix: 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.

Fix: WaitedMS counts milliseconds, compared to FPollSec * 1000. PollTickMS = 500 preserves the ≤500ms FStop responsiveness.

Verification

make clean && make smoke 11/11 green.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

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
@FMXExpress
FMXExpress merged commit 43599c1 into main May 31, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +379 to +380
if (not ReplyAttempted) or ReplySent then
IMAP.StoreFlags([SeqNum], sdAdd, [mfSeen]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +469 to +470
FStop := True;
FWorker.WaitFor;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

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.

2 participants