diff --git a/docs/release/README.md b/docs/release/README.md index 882bcc1f92..e81c03e4d8 100644 --- a/docs/release/README.md +++ b/docs/release/README.md @@ -97,6 +97,24 @@ The JSON fields remain `name`, `systemPrompt` and rely on the removed reply type's rejection of unknown fields. There is no App Server protocol, event or persistence-format change. +## Method progress type compatibility + +The progress callback a `dimcode/plugin` method handler receives returns +`Promise`; its declaration was `() => void`. The host already supplies an +asynchronous callback, so `await progress(...)` and `progress(...).catch(...)` +now type-check, and no host build is required. The change breaks code that calls +an exported `MethodHandler` or `SessionMethodHandler` directly: a synchronous +argument such as `() => {}`, as a unit-test mock uses, fails with TS2345. Pass +`async () => {}` instead. Handlers registered through +`managementMethods.register` or `sessionMethods.register` receive the same +callback at runtime. Awaiting it orders the host's send attempts and does not +acknowledge client delivery; the App Server's progress allowance can drop a +notice while the call still resolves, whereas a frame over the rutis session's +transfer limit fails the call and rejects the promise. The +[authoring guide](../typescript-plugin-authoring.md#session-tools-and-client-methods) +states the callback's delivery and rejection behaviour. There is no App Server +protocol, event or persistence-format change. + ## File access Under `read-only` and `workspace-write`, a file tool read (`read`, `grep` or `glob`) whose diff --git a/docs/typescript-plugin-authoring.md b/docs/typescript-plugin-authoring.md index 7c3c25b55c..0d1cf4d84d 100644 --- a/docs/typescript-plugin-authoring.md +++ b/docs/typescript-plugin-authoring.md @@ -581,7 +581,7 @@ export const sessionExample = definePlugin({ 'session.example', [{ name: 'session.example.echo', mutating: false, interruption: 'report', progress: true }], async (call, _cancelled, progress) => { - progress({ phase: 'started' }); + await progress({ phase: 'started' }); return { kind: 'value', value: call.params }; }, ); @@ -646,7 +646,23 @@ under the `hello`, `daemon`, `agent` or `approval` roots; an `apply` that awaits fails its part. The registration lasts until the part unloads, and `session.methods` lists the methods registered at that call. `call` is asynchronous and receives an opaque runtime `callId`, the method and original params containing `sessionId` and -optional `branchId`; it also receives cancellation and a per-call progress callback. +optional `branchId`; it also receives cancellation and a per-call progress callback. The callback +returns a promise, and awaiting it is what keeps your notices in order: each call is a frame the host +runs on its own task, so two calls without a wait between them can arrive in either order. Await it +as well when a notice has to be queued before the method returns, because the reply closes the +progress channel and drops a send that has not run yet. + +Awaiting that promise orders the host's send attempts; it does not acknowledge +delivery to the client. The App Server's progress allowance is best effort: it +drops progress a method does not declare, progress sent after it hands over the +reply, and progress its connection cannot take, including after a disconnect, +while the call itself still resolves. The rutis transfer limit is not: a call +whose encoded frame exceeds the session's `max_transfer` fails with +`bad-value`, and a call on a closed bridge fails too, so a progress call you do +not await can reject. Attach `.catch(...)` to a detached call. A unit test that +calls a `MethodHandler` or `SessionMethodHandler` directly must pass +`async () => {}` as the progress callback; the type requires a promise. + Return `{kind: 'value', value}`, return a failure as data, `{code, message}`, or rethrow the service error you received. An omitted value becomes JSON null. Rpc maps a failure to the wire error: `code` becomes `appError`, with JSON-RPC code -32602 for `invalid_params` and diff --git a/npm/plugin.d.ts b/npm/plugin.d.ts index df3457e9bd..5920d13eb1 100644 --- a/npm/plugin.d.ts +++ b/npm/plugin.d.ts @@ -565,7 +565,7 @@ export type MethodReply = { kind: 'value'; value?: T } | Me export type CheckedData = T extends BridgeJsonValue | undefined ? T : T extends ((...args: never[]) => unknown) | (abstract new (...args: never[]) => unknown) ? never : T extends object ? keyof T extends never ? never : { [K in keyof T]: CheckedData } : never; -export type MethodProgress = (data: T & CheckedData) => void; +export type MethodProgress = (data: T & CheckedData) => Promise; export type SessionMethodDeclaration = MethodDeclaration; export type SessionMethodReply = MethodReply; /** How a registered management namespace serves one call. */ diff --git a/runtime/plugin-sdk.test.mjs b/runtime/plugin-sdk.test.mjs index 9b3b70d566..8c9303a163 100644 --- a/runtime/plugin-sdk.test.mjs +++ b/runtime/plugin-sdk.test.mjs @@ -93,7 +93,8 @@ void management.register('fixture', [], typedHandler); void methods.register('fixture', [], async () => initialReply); void management.register('fixture', [], async () => configuredReply); void methods.register('fixture', [], async (call, cancelled, progress) => { - progress(configured); + const sent: Promise = progress(configured); + await sent; progress(entry); progress({}); // @ts-expect-error progress carries data, not callbacks. @@ -120,7 +121,7 @@ void management.register('fixture', [], async () => ({ kind: 'other', value: con const declarations: MethodDeclaration[] = [{ name: 'example.status', mutating: false, interruption: 'report', progress: false }]; void management.register('example', declarations, handler); -void handler({ callId: 'call', method: 'example.status', params: [1, true] }, Promise.resolve(), () => {}); +void handler({ callId: 'call', method: 'example.status', params: [1, true] }, Promise.resolve(), async () => {}); // @ts-expect-error a declaration states its interruption and progress. void management.register('example', [{ name: 'example.status', mutating: false }], handler); const at = { sessionId: 'session', branchId: 'main' }; @@ -240,11 +241,11 @@ void execute({ }, Promise.resolve()); void methods.register('session.example', [{ name: 'session.example.echo', mutating: false, interruption: 'drain', progress: true }], sessionHandler); void sessionHandler({ callId: 'call', method: 'session.example.echo', params: { sessionId: 'session' } }, - Promise.resolve(), () => {}); + Promise.resolve(), async () => {}); // @ts-expect-error a Session method call names its Session. -void sessionHandler({ callId: 'call', method: 'session.example.echo', params: {} }, Promise.resolve(), () => {}); +void sessionHandler({ callId: 'call', method: 'session.example.echo', params: {} }, Promise.resolve(), async () => {}); void handler({ callId: 'call', method: 'example.status', params: [1, true], - origin: { connectionId: 'connection' } }, Promise.resolve(), () => {}); + origin: { connectionId: 'connection' } }, Promise.resolve(), async () => {}); const notificationOrigin: MethodDeclaration = { name: 'example.status', mutating: false, interruption: 'report', progress: false, notifications: ['example.changed'] }; void notificationOrigin;