DeepSeek Harness

DeepSeek Harness: Publishing and Listening to Events

· 7 min read · 中文版

In DeepSeek Harness, declare an event's typed contract, register listeners with ctx.on, and dispatch it using the mode that contract specifies; ordering, return values and teardown depend on that choice.

DeepSeek Harness: how do you declare and send an event?

The Cordis primer describes typed events as communication between plugins. Services declare names through TypeScript declaration merging. The event system guide illustrates a synchronous ready notification with a payload. The following combines its declaration and usage into one small plugin example:

import type { Context } from '@deepseek-ai/cordis'
import '@deepseek-ai/cordis'

declare module '@deepseek-ai/cordis' {
  interface Events {
    'my-plugin/ready': (payload: { id: string }) => void
  }
}

export function apply(ctx: Context) {
  ctx.on('my-plugin/ready', ({ id }) => {
    console.log(`${id} is ready`)
  })
  ctx.emit('my-plugin/ready', { id: 'worker-1' })
}

The declaration gives the listener and dispatch a shared name and payload type. ctx.on installs the listener; ctx.emit dispatches synchronously and ignores listener return values. This illustrative notification is not a persistent message queue, and logging a payload is appropriate only if the data may be logged. A production plugin should place its listener before the event it needs to observe and use the event contract defined by the owner.

Declare an event, register a listener, dispatch by its contract, and dispose with the plugin

DeepSeek Harness: which dispatch mode should you use?

The Events API lists five dispatch methods. The primer says the mode is part of an event's public contract: check the owning subsystem's generated event signature and mode before publishing or listening. Do not choose a different method just because a different return shape appears convenient.

MethodListener behaviorResult
emitSynchronous notification in registration orderReturn values ignored
parallelListeners run concurrentlyPromise resolves when all have settled; no result value
serialAwait listeners in order until one bailsFirst bail value, if any
bailCall listeners in order synchronously until one bailsFirst bail value, if any
waterfallEach listener wraps the rest through nextOutermost listener's return value

For serial and bail, the API defines a bail value as anything other than null, false, or undefined. For example, a listener returning false does not stop the chain. parallel is useful when the event contract calls for concurrent notification and its publisher must await settlement. It should not be treated as ordered coordination.

DeepSeek Harness: how does a waterfall listener cooperate?

A waterfall is around-middleware, not merely a sequence of independent notifications. The last argument is a continuation. Calling next() invokes the next listener and eventually the supplied innermost behavior; returning without calling it vetoes the remaining chain. The guide's transform example awaits next() and then trims its result. That means a listener may affect the value seen by its caller as well as decide whether downstream work happens.

An observer or annotator that is meant to cooperate should delegate. A policy listener may intentionally short-circuit when its contract gives it responsibility for the decision. The primer notes that cooperative listeners often mutate a shared request or decision object before delegation, while a listener may instead replace the result. Document which behavior callers and other listeners should expect; an accidental missing next() can prevent later work.

Do not infer that every waterfall is asynchronous simply because one handler returns a promise. The API returns the outermost listener's value, and the event's own signature defines its result. A caller should follow that signature. Likewise, emit ignores listener returns; using an async listener there does not turn the dispatch into a completion barrier. Select the method declared for the event rather than attempting to change its semantics at the call site.

DeepSeek Harness: how do you add a listener to a plugin?

  1. Find the owning subsystem and read the event name, argument types and dispatch mode in its generated reference. For a new event, declare its typed signature and document its mode as the primer describes.
  2. Register with ctx.on(name, listener) in the relevant plugin context. Use ctx.once when the listener should run at most once; both return a disposer that can remove the listener early.
  3. Handle the actual arguments and return contract. For an existing waterfall, delegate with next() unless your listener intentionally owns a short-circuit decision. For an observer, avoid relying on a return value that the dispatcher ignores.
  4. Dispatch using the owning event's method and await only where the contract requires it. Check error and cancellation behavior in the specific event's documentation; the general API alone does not specify a universal recovery policy.
  5. Exercise the event with representative listeners, including a no-listener case and the relevant bail or continuation path. Unload or reload the plugin and check that the listener is no longer active.

ctx.on registrations are owned by the current fiber and removed when the plugin unloads. The disposer returns true if the listener was still registered. ctx.once removes itself after its first call. The API also documents prepend to place a listener before existing ones and global to receive events regardless of context filters. These options alter routing or ordering; use them only when the event's contract and plugin design call for them.

DeepSeek Harness: are Cordis events the same as session records?

No. The event system guide identifies agent/pre-step, tools/result, and session/event as Cordis events, while tool/call, tool/result, turn/* and step/* are durable session-event types rather than same-named Cordis events. To observe session records, listen to session/event and inspect event.type. A live notification is not automatically durable; choose the owning subsystem's documented mechanism when a fact must survive reload.

For a real tool-result listener, the guide imports the tool package's event declarations and registers ctx.on('tools/result', ...). It reads the execution and final result supplied by that event. That example is useful for understanding signatures and lifecycle, but copying its logging of arguments and content into a production system can expose sensitive data. Limit what you collect and decide retention according to the deployment's needs.

How should you review a listener change?

Check the publisher and consumer together. For a notification, confirm the callback does not depend on a returned value being consumed. For a decision event, test a non-bail return as well as the value that should stop the chain. In a waterfall, test both delegation and any deliberate short-circuit against the event owner's intended behavior. These checks concern the public contract, not merely whether a callback was invoked.

The API also permits explicit early disposal. If a listener is needed only during a narrower operation, keep the returned disposer and call it when that operation ends. A subsequent call can indicate that the registration was already gone. For a one-time observation, ctx.once expresses that lifetime directly and removes itself after its first invocation.

Then consider lifetime and reach. A listener attached by ctx.on belongs to its current fiber; retaining a separate manual registration outside that lifecycle would require its own cleanup. prepend changes relative order, and global changes filter behavior. Neither is a substitute for understanding the source event's scope. Test the behavior before and after the owning plugin unloads, especially if a reload can register the plugin again.

DeepSeek Harness: what are the limits and safety boundaries?

Typed declarations catch mismatched calls at development time; they do not validate the trustworthiness of payloads or guarantee listener behavior. An event listener can run plugin code and a waterfall can suppress downstream work. Review handlers, ordering and ownership when a change affects policy or external operations. global bypasses context filter checks, so its reach deserves particular attention.

The API documentation does not promise persistent delivery, retries, or a cross-process message bus for these dispatch methods. Do not assume those properties from the word “event.” Payloads and results may include private information: avoid indiscriminate logging, and sanitize diagnostics before sharing.

DeepSeek Harness: what do the terms mean?

  • Event contract: the name, typed arguments, return and dispatch mode agreed by its owner and consumers.
  • Listener: a callback registered for an event in a context.
  • Bail value: a value other than null, false, or undefined that stops serial or bail dispatch.
  • Continuation: the waterfall's next function, used to invoke downstream behavior.
  • Fiber: the lifecycle owner of a listener registration; disposal removes that effect.

Frequently asked questions

Does emit wait for asynchronous listeners?

No. It dispatches synchronously and ignores listener return values.

What happens if a waterfall listener omits next?

It short-circuits the remaining chain.

Are listeners removed when a plugin unloads?

Yes. ctx.on registers an effect owned by the current fiber.

Sources

Every claim in this article was checked against the sources below. Community discussions are user reports, not official behavior.

  1. Cordis Events API
  2. Cordis primer
  3. Event system guide

This is an independent community article, not official DeepSeek documentation. Listing a plugin in the registry is not a security review.

← Back to all articles

DeepSeek Harness: Publishing and Listening to Events