Skip to content

Fix alarm() to call super for init#1050

Merged
threepointone merged 2 commits intomainfrom
opencode/issue1048-20260302215009
Mar 2, 2026
Merged

Fix alarm() to call super for init#1050
threepointone merged 2 commits intomainfrom
opencode/issue1048-20260302215009

Conversation

@ask-bonk
Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk bot commented Mar 2, 2026

All done. Here's a summary of the changes:

Fix: Agent alarm() override bypasses PartyServer initialization

Root cause

The Agent class defined alarm as a public readonly arrow function property (packages/agents/src/index.ts:2432), which completely shadowed PartyServer's alarm() prototype method. When a Durable Object woke via alarm (e.g. from scheduleEvery), PartyServer's #ensureInitialized() was never called, so:

  • this.name would throw ("Attempting to read .name before it was set")
  • onStart() was never called

Fix (3 changes in packages/agents/src/index.ts)

  1. Converted alarm from an arrow function property to a regular async alarm() method — this allows proper prototype-chain method override instead of instance-level shadowing.

  2. Added await super.alarm() as the first line of the new alarm() method — this calls PartyServer's #ensureInitialized(), which hydrates this.name from durable storage and calls onStart() if needed.

  3. Added onAlarm(): void {} no-op override — PartyServer's super.alarm() calls this.onAlarm() after initialization, and the default implementation logs a warning. The no-op suppresses this.

Tests added

  • TestAlarmInitAgent — a test agent with onStart() tracking and a nameCheckCallback that reads this.name during alarm execution
  • Two new test cases in alarms.test.ts:
    • this.name is accessible during scheduled callback (captures the agent name correctly)
    • onStart is called before executing scheduled callbacks

Verification

  • All 41 test files pass (669 tests)
  • Full CI checks pass (npm run check: sherif, export checks, formatting, linting, typecheck across all 46 projects)
  • Build succeeds for all packages

Closes #1048

github run

ask-bonk bot and others added 2 commits March 2, 2026 22:02
…ialization

The Agent class defined alarm as a public readonly arrow function property,
which completely shadowed PartyServer's alarm() prototype method. This meant
#ensureInitialized() was never called when a Durable Object woke via alarm
(e.g. from scheduleEvery), causing this.name to throw and onStart to never run.

Converted alarm from an arrow function property to a regular async method that
calls super.alarm() before processing scheduled tasks. Also added an onAlarm()
no-op override to suppress PartyServer's default warning log.

Added TestAlarmInitAgent and tests verifying this.name is accessible and onStart
is called during alarm-triggered scheduled callback execution.
Co-authored-by: threepointone <threepointone@users.noreply.github.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 2, 2026

🦋 Changeset detected

Latest commit: 77eadbe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 2, 2026

Open in StackBlitz

npm i https://pkg.pr.new/agents@1050
npm i https://pkg.pr.new/@cloudflare/ai-chat@1050
npm i https://pkg.pr.new/@cloudflare/codemode@1050
npm i https://pkg.pr.new/hono-agents@1050

commit: 3416d9e

@threepointone threepointone merged commit 6157741 into main Mar 2, 2026
3 checks passed
@threepointone threepointone deleted the opencode/issue1048-20260302215009 branch March 2, 2026 22:11
@github-actions github-actions bot mentioned this pull request Mar 2, 2026
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.

Agent alarm() override bypasses PartyServer

1 participant