Skip to content

feat(email-joke): pre-check emails before agent start + context injection#574

Merged
zbigniewsobiecki merged 1 commit intodevfrom
feat/email-joke-pre-check-context-injection
Feb 27, 2026
Merged

feat(email-joke): pre-check emails before agent start + context injection#574
zbigniewsobiecki merged 1 commit intodevfrom
feat/email-joke-pre-check-context-injection

Conversation

@zbigniewsobiecki
Copy link
Copy Markdown
Member

Summary

  • Pre-check phase: before the email-joke agent starts, the manual runner now searches for unread emails (filtered by senderEmail when configured). If the inbox is empty the run is skipped cleanly — no wasted agent session.
  • Context injection: when emails are found they are pre-injected via a new prefetchedEmails context pipeline step, so the agent skips its redundant SearchEmails call and goes straight to reading + replying.
  • Bug fix: the no-provider fallback previously returned true (letting the agent start with a self-contradictory prompt); it now logs a warning and aborts (return false).
  • Code-review cleanups: consolidated EmailSearchCriteria import to the email barrel, removed redundant as EmailSummary[] / as string casts (now that AgentInput has properly typed named fields), and renamed the registry key from fetchEmailsFromInputprefetchedEmails to match the existing noun-phrase convention.

Files changed

File Change
src/types/index.ts Add preFoundEmails?: EmailSummary[] to AgentInput
src/triggers/shared/manual-runner.ts prefetchEmailsForJokeAgent pre-check; no-provider abort fix; import consolidation; redundant cast removed
src/agents/definitions/contextSteps.ts New fetchEmailsFromInputStep; redundant casts removed
src/agents/definitions/schema.ts Add prefetchedEmails to context-pipeline enum
src/agents/definitions/strategies.ts Register prefetchedEmails in CONTEXT_STEP_REGISTRY
src/agents/definitions/email-joke.yaml Switch contextPipeline to [prefetchedEmails]
src/agents/prompts/task-templates/emailJoke.eta Update task prompt — reference pre-fetched results, drop redundant search step
tests/unit/triggers/manual-runner.test.ts New email-joke pre-check describe block (5 cases)
tests/unit/agents/definitions/contextSteps.test.ts New file — 6 focused tests for fetchEmailsFromInputStep

Test plan

  • npm test — all 3441 tests pass (including 11 new ones)
  • npm run typecheck — zero errors
  • npm run lint — clean

🤖 Generated with Claude Code

…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>
@zbigniewsobiecki zbigniewsobiecki merged commit 06c148a into dev Feb 27, 2026
6 checks passed
@zbigniewsobiecki zbigniewsobiecki deleted the feat/email-joke-pre-check-context-injection branch February 27, 2026 16:25
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.

1 participant