feat(email-joke): pre-check emails before agent start + context injection#574
Merged
zbigniewsobiecki merged 1 commit intodevfrom Feb 27, 2026
Merged
Conversation
…tion Before this change the email-joke agent always started regardless of whether any matching emails existed — if the INBOX was empty the agent would search, find nothing, and call Finish immediately, wasting a full agent run. This PR adds a pre-check phase: before the agent starts, the manual runner searches for unread emails (filtered by senderEmail when configured). If none are found the run is skipped cleanly; if emails are found they are injected into the agent's initial context via the new `prefetchedEmails` pipeline step, so the agent can skip the redundant SearchEmails call and go straight to reading and replying. Changes: - src/types/index.ts: add `preFoundEmails?: EmailSummary[]` to AgentInput - src/triggers/shared/manual-runner.ts: add `prefetchEmailsForJokeAgent` pre-check inside withEmailIntegration; fix no-provider path to abort (return false) instead of silently proceeding with a broken prompt - src/agents/definitions/contextSteps.ts: add `fetchEmailsFromInputStep` that injects pre-fetched emails as a synthetic SearchEmails tool result - src/agents/definitions/schema.ts: register `prefetchedEmails` enum value - src/agents/definitions/strategies.ts: wire `prefetchedEmails` registry key - src/agents/definitions/email-joke.yaml: use `prefetchedEmails` pipeline step - src/agents/prompts/task-templates/emailJoke.eta: update task prompt to reference the pre-fetched results and drop the now-redundant search step Code-review fixes applied in the same commit: - Consolidate EmailSearchCriteria import to email barrel (email/index.js) - Remove redundant `as EmailSummary[]` and `as string` casts now that AgentInput has properly typed named fields - Registry key follows the existing noun-phrase convention (`prefetchedEmails` not `fetchEmailsFromInput`) Tests: - tests/unit/triggers/manual-runner.test.ts: new `email-joke pre-check` describe block (5 cases: no provider, zero emails, emails found, non-email agent skips pre-check, senderEmail → criteria.from filter) - tests/unit/agents/definitions/contextSteps.test.ts: new file with 6 focused tests for fetchEmailsFromInputStep (undefined/empty, injection shape, senderEmail filter, multi-email numbering, UTC date extraction) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
senderEmailwhen configured). If the inbox is empty the run is skipped cleanly — no wasted agent session.prefetchedEmailscontext pipeline step, so the agent skips its redundantSearchEmailscall and goes straight to reading + replying.true(letting the agent start with a self-contradictory prompt); it now logs a warning and aborts (return false).EmailSearchCriteriaimport to the email barrel, removed redundantas EmailSummary[]/as stringcasts (now thatAgentInputhas properly typed named fields), and renamed the registry key fromfetchEmailsFromInput→prefetchedEmailsto match the existing noun-phrase convention.Files changed
src/types/index.tspreFoundEmails?: EmailSummary[]toAgentInputsrc/triggers/shared/manual-runner.tsprefetchEmailsForJokeAgentpre-check; no-provider abort fix; import consolidation; redundant cast removedsrc/agents/definitions/contextSteps.tsfetchEmailsFromInputStep; redundant casts removedsrc/agents/definitions/schema.tsprefetchedEmailsto context-pipeline enumsrc/agents/definitions/strategies.tsprefetchedEmailsinCONTEXT_STEP_REGISTRYsrc/agents/definitions/email-joke.yamlcontextPipelineto[prefetchedEmails]src/agents/prompts/task-templates/emailJoke.etatests/unit/triggers/manual-runner.test.tsemail-joke pre-checkdescribe block (5 cases)tests/unit/agents/definitions/contextSteps.test.tsfetchEmailsFromInputStepTest plan
npm test— all 3441 tests pass (including 11 new ones)npm run typecheck— zero errorsnpm run lint— clean🤖 Generated with Claude Code