-
Notifications
You must be signed in to change notification settings - Fork 637
fix(agents): initialize cold native RPC calls #2005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cjol
wants to merge
4
commits into
main
Choose a base branch
from
fix-1990-think-cold-rpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
869b3b6
This PR initializes Agent and Think instances before native Durable O…
cjol e40e4e9
fix(agents): preserve cold deferred destruction
cjol f46e6b8
test(agents): isolate managed recovery retries
cjol 379b5ec
fix(agents): preserve synchronous policy hooks
cjol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "agents": patch | ||
| "@cloudflare/think": patch | ||
| --- | ||
|
|
||
| Initialize Agent and Think instances before native Durable Object RPC methods execute on a cold instance. |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { Agent } from "../../index.ts"; | ||
| import type { AgentContext } from "../../index.ts"; | ||
|
|
||
| class NativeRpcBaseAgent extends Agent { | ||
| shadowedMethod(): string { | ||
| return "inherited method"; | ||
| } | ||
| } | ||
|
|
||
| export class TestNativeRpcAgent extends NativeRpcBaseAgent { | ||
| private constructionFinished = false; | ||
| private fieldInitializerObservation = this.observeConstruction("field"); | ||
| private constructorObservation: string; | ||
| private ready = false; | ||
|
|
||
| constructor(ctx: AgentContext, env: Cloudflare.Env) { | ||
| super(ctx, env); | ||
| this.constructorObservation = this.observeConstruction("constructor"); | ||
| this.constructionFinished = true; | ||
| } | ||
|
|
||
| private observeConstruction(location: string): string { | ||
| return `${location}:${this.constructionFinished ? "finished" : "constructing"}`; | ||
| } | ||
|
|
||
| override async onStart(): Promise<void> { | ||
| const starts = | ||
| (await this.ctx.storage.get<number>("test_start_count")) ?? 0; | ||
| await this.ctx.storage.put("test_start_count", starts + 1); | ||
| this.ready = true; | ||
| } | ||
|
|
||
| async applicationRpc(): Promise<{ | ||
| name: string; | ||
| ready: boolean; | ||
| startCount: number; | ||
| fieldInitializerObservation: string; | ||
| constructorObservation: string; | ||
| shadowedValue: string; | ||
| }> { | ||
| return { | ||
| name: this.name, | ||
| ready: this.ready, | ||
| startCount: (await this.ctx.storage.get<number>("test_start_count")) ?? 0, | ||
| fieldInitializerObservation: this.fieldInitializerObservation, | ||
| constructorObservation: this.constructorObservation, | ||
| shadowedValue: (this as unknown as { shadowedMethod: string }) | ||
| .shadowedMethod | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // The nearest descriptor must remain authoritative during RPC wrapper discovery. | ||
| // Defining this after the class avoids TypeScript treating the intentional | ||
| // method-to-getter shadow as an invalid override. | ||
| Object.defineProperty(TestNativeRpcAgent.prototype, "shadowedMethod", { | ||
| configurable: true, | ||
| enumerable: false, | ||
| get() { | ||
| return "nearest getter"; | ||
| } | ||
| }); |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Non-async agent methods can hand back an unfinished value on the very first call after wake-up
On the first call to a public agent method after the object wakes up, the framework hands back a pending value instead of the real result (
.then(...)cast withas ReturnType<T>atpackages/agents/src/index.ts:1626-1630), so callers of methods that were previously instant get a placeholder they must wait on.Impact: Code inside the agent (or in the same isolate) that uses the result of a normally-instant method directly can silently work with an unusable placeholder value the first time the agent is touched after a restart.
Wrapper turns synchronous methods into promise-returning ones while initialization is pending
withAgentContextpreviously always returnedmethod.apply(this, args)directly, so a synchronous public method kept synchronous semantics. The new branch returnsthis.__unsafe_ensureInitialized().then(() => method.apply(...))whenever_rpcInitializationState === "pending", i.e. on the first invocation after construction/eviction that is not already inside the agent's invocation context. Because the declared signature is(...args) => ReturnType<T>, the implementation needs an explicitas ReturnType<T>cast (packages/agents/src/index.ts:1630) to hide the mismatch from the type checker.This matters more than before because
_autoWrapRpcMethods(packages/agents/src/index.ts:3653-3696) no longer excludesAgent/Serverbase methods, so inherited synchronous helpers such asgetMcpServers()— which the framework itself consumes synchronously, e.g.mcp: this.getMcpServers()insideJSON.stringifyatpackages/agents/src/index.ts:2744-2748— are now wrapped. Any caller that reaches such a method while the state is still"pending"and without the agent already being the current agent receives a promise where a plain value is expected, and the value is serialized/used as an empty object rather than the intended data.A safer shape would be to only apply the initialization branch to methods that are known to be async (or to keep the synchronous fall-through when
methodis not an async function), and to avoid theas ReturnType<T>cast so the type system reflects the actual contract.Was this helpful? React with 👍 or 👎 to provide feedback.