From bddb2dfb117931424e5c81fd8965f6b22c36233d Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Sat, 21 Nov 2026 16:20:22 +0800 Subject: [PATCH 1/2] docs(tracing): clarify recorder ownership and lifecycle --- docs/content/1.guide/24.tracing-channels.md | 16 +++++++++------- docs/content/8.references/3.events.md | 2 +- docs/content/8.references/4.node-api.md | 4 ++-- packages/devframe/src/types/tracing.ts | 14 +++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/docs/content/1.guide/24.tracing-channels.md b/docs/content/1.guide/24.tracing-channels.md index beff6db8..8468f378 100644 --- a/docs/content/1.guide/24.tracing-channels.md +++ b/docs/content/1.guide/24.tracing-channels.md @@ -53,7 +53,9 @@ export default defineDevframe({ }) ``` -`tracePromise` writes the resolved value to `context.result` and a rejection to `context.error`, so a record shows both. Publishing costs nothing while nobody records: `TracingChannel` skips its work when `hasSubscribers` is `false`. +For the query above, Node emits `end` when the synchronous part of the call finishes. The trace can already show `ok` while the database query is still running. When the promise settles, the same record updates with the result or changes to `error` on rejection. For a known `tracePromise` call, the `asyncEnd` event marks the end of its asynchronous work. + +`TracingChannel` skips publishing while `hasSubscribers` is `false`. ## Recording @@ -89,7 +91,7 @@ The context, result and error pass through a serializer that caps depth at 4, tr ## Attaching a tool's own recorder -A tool that already traces on its own does not need to move its recording into `ctx.tracing`. It keeps its own recorder and, when a devframe mounts it, attaches the recorder so the channels and records show up there. Devframe relays them. It does not subscribe to the channels itself. +A tool can keep its own recorder and attach it to `ctx.tracing` when a devframe mounts it. Devframe then relays the tool's records. `createTracingRecorder()` from `devframe/node` returns a recorder with the same recording methods as `ctx.tracing`, but without the wire: @@ -111,11 +113,9 @@ export default defineDevframe({ }) ``` -`attach()` returns a function that detaches the recorder and removes its channels from the list. While the recorder is attached: +When several recorders declare the same channel name, the most recently attached source takes precedence. The channel's `recording` flag and count describe that source. `records(name)` reads its buffer, and `onRecord(name, fn)` receives its next updates. Calls to `record(name)`, `stop(name)` and `clear(name)`, including their RPC equivalents, go to it. -- `list()`, `records(name)` and `onRecord(name)` include its channels. -- `record(name)`, `stop(name)` and `clear(name)`, and the matching RPC actions, call the recorder's own methods. Devframe never records an attached channel itself. -- When the recorder and `ctx.tracing` both know a name, the recorder wins. Use a prefix such as `my-tool:` to keep names apart. +For example, if `ctx.tracing` was already recording `module.require` when a tool attached a recorder with that name, `stop('module.require')` stops the tool's recorder. The local recorder keeps running. After you detach the tool, `records('module.require')` includes the calls the local recorder captured during that time. Use a prefix such as `my-tool:` for channels your tool defines. The recorder does not have to come from devframe. Any object with the `DevframeTracingSource` shape works, so the tool can keep devframe out of its runtime dependencies: @@ -149,7 +149,9 @@ for await (const record of rpc.streaming.subscribe('devframe:tracing', streamId) } ``` -`record()` and `clear()` issue a fresh `streamId`, so watch the shared state and re-subscribe when it changes. The RPC actions `devframe:tracing:record`, `devframe:tracing:stop` and `devframe:tracing:clear` take the channel name. +Watch the shared state and subscribe whenever a new `streamId` appears. With the built-in recorder, calling `record()` again while recording keeps the current stream. A clear during recording gives it a new ID. Stopping removes the ID, and restarting creates a new one. An attached source can implement controls differently. Its recording state and clear notifications determine the published ID. When two sources are already recording the same name, switching between them keeps the same stream. A new subscription can replay updates from the previous source. + +The RPC actions `devframe:tracing:record`, `devframe:tracing:stop` and `devframe:tracing:clear` take the channel name. ## Runtimes diff --git a/docs/content/8.references/3.events.md b/docs/content/8.references/3.events.md index 55378614..f4451f32 100644 --- a/docs/content/8.references/3.events.md +++ b/docs/content/8.references/3.events.md @@ -120,7 +120,7 @@ Published by the core node side; read them with `rpc.sharedState.get(key)` and ` | Name | Kind | Carries | |---|---|---| | `devframe:tracing:channels` | shared state | Known [Tracing Channels](/guide/tracing-channels) keyed by base name (`DevframeTracingChannelInfo`): source, description, `recording`, `count`, and the live `streamId` while recording. | -| `devframe:tracing` | streaming channel | Trace records (`DevframeTraceRecord`) of one recording Tracing Channel per stream; the stream id is the `streamId` from `devframe:tracing:channels`. A fresh id is issued on every `record()` and `clear()`. | +| `devframe:tracing` | streaming channel | Trace records (`DevframeTraceRecord`) of one recording Tracing Channel per stream; the stream id is the `streamId` from `devframe:tracing:channels`. Use the advertised ID. Devframe creates a stream when the channel starts recording and replaces the stream when the selected source reports a clear during recording. Repeating `record()` on the built-in recorder keeps the current stream. | ### In-page channel notifications (page script → panel) diff --git a/docs/content/8.references/4.node-api.md b/docs/content/8.references/4.node-api.md index 7be6c845..5829e0e5 100644 --- a/docs/content/8.references/4.node-api.md +++ b/docs/content/8.references/4.node-api.md @@ -133,7 +133,7 @@ The methods on `ctx.services`: [Cross-Devframe Services](/guide/services#the-dev ## `DevframeTracingHost` -The methods on `ctx.tracing`: [Tracing Channels](/guide/tracing-channels). +The table describes the built-in recorder on `ctx.tracing`. For a channel supplied by an attached source, that source provides the records and decides which controls it supports. See [Tracing Channels](/guide/tracing-channels). | Method | Signature | Role | |--------|-----------|------| @@ -144,7 +144,7 @@ The methods on `ctx.tracing`: [Tracing Channels](/guide/tracing-channels). | `records` | `(name) => DevframeTraceRecord[]` | Buffered records, oldest first, at most 500 per channel. | | `clear` | `(name) => void` | Drop the buffer and, while recording, start a fresh stream. | | `onRecord` | `(name, fn) => unsubscribe` | Run `fn` with the whole record on every lifecycle update. | -| `attach` | `(source) => detach` | Show a tool's own recorder (`createTracingRecorder()` or any `DevframeTracingSource`). Devframe relays its channels and records and routes `record` / `stop` / `clear` to it, but never records them itself. The source attached last wins a name. See [Attaching a tool's own recorder](/guide/tracing-channels#attaching-a-tools-own-recorder). | +| `attach` | `(source) => detach` | Show a tool's own recorder (`createTracingRecorder()` or any `DevframeTracingSource`). For a shared name, the source attached last handles controls and supplies the buffer read by `records(name)`. Earlier recorders keep their own recording state. See [Attaching a tool's own recorder](/guide/tracing-channels#attaching-a-tools-own-recorder). | ## Service tiers diff --git a/packages/devframe/src/types/tracing.ts b/packages/devframe/src/types/tracing.ts index 481fcc5c..e127894f 100644 --- a/packages/devframe/src/types/tracing.ts +++ b/packages/devframe/src/types/tracing.ts @@ -29,13 +29,13 @@ export interface DevframeTracingChannelInfo { name: string description?: string source: DevframeTracingChannelSource - /** `true` while devframe is subscribed to the Node channel. */ + /** Whether the selected source is recording this channel. */ recording: boolean /** * Id of the live stream on the `devframe:tracing` streaming channel that - * carries this channel's records. Set while recording; a fresh id is - * issued on every `record()` and `clear()`, so a client re-subscribes - * when it changes. + * carries this channel's records. Published in `devframe:tracing:channels` while recording. Devframe starts + * a new stream when recording starts or the selected source reports a + * clear during recording. Subscribe again when the published ID changes. */ streamId?: string /** Records currently held in the ring buffer. */ @@ -91,7 +91,7 @@ export type DevframeTracingSourceEvent * Anything that owns Tracing Channels and their records, outside of * `ctx.tracing`. A tool that keeps its own tracing instance attaches it with * `ctx.tracing.attach(source)`: devframe then shows the source's channels and - * relays its records, but never subscribes to them itself. The source records. + * relays its records. Other recorders keep their own recording state. * * `createTracingRecorder()` from `devframe/node` returns one. Any object with * this shape works too, so a tool does not need devframe at runtime. @@ -161,8 +161,8 @@ export interface DevframeTracingHost { onRecord: (name: string, fn: (record: DevframeTraceRecord) => void) => () => void /** * Show the channels and records of a tool's own tracing instance. Devframe - * relays them over the wire but does not record them; the source does. - * When two sources own the same name, the one attached last wins. + * relays records from the source attached last for each name. Attaching + * or detaching a source preserves the other recorders' recording state. * Returns a function that detaches the source. */ attach: (source: DevframeTracingSource) => () => void From 02a059e752a03fea5904f442d5e9f142bb122d91 Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Sun, 22 Nov 2026 12:42:39 +0800 Subject: [PATCH 2/2] docs(tracing): clarify record and stream lifetimes --- docs/content/1.guide/24.tracing-channels.md | 8 ++++++-- docs/content/8.references/4.node-api.md | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/content/1.guide/24.tracing-channels.md b/docs/content/1.guide/24.tracing-channels.md index 8468f378..52614597 100644 --- a/docs/content/1.guide/24.tracing-channels.md +++ b/docs/content/1.guide/24.tracing-channels.md @@ -53,7 +53,9 @@ export default defineDevframe({ }) ``` -For the query above, Node emits `end` when the synchronous part of the call finishes. The trace can already show `ok` while the database query is still running. When the promise settles, the same record updates with the result or changes to `error` on rejection. For a known `tracePromise` call, the `asyncEnd` event marks the end of its asynchronous work. +For the query above, Node emits `end` when the synchronous part of the call finishes. The trace can already show `ok` while the database query is still running. For this `tracePromise` call, `asyncEnd` marks the end of its asynchronous work. + +If you keep recording without clearing the buffer, the same record shows the result or changes to `error` when the promise settles. If you call `ctx.tracing.clear('my-tool:query')` while the query is pending, its next recorded event starts a record with a new ID. `TracingChannel` skips publishing while `hasSubscribers` is `false`. @@ -149,7 +151,9 @@ for await (const record of rpc.streaming.subscribe('devframe:tracing', streamId) } ``` -Watch the shared state and subscribe whenever a new `streamId` appears. With the built-in recorder, calling `record()` again while recording keeps the current stream. A clear during recording gives it a new ID. Stopping removes the ID, and restarting creates a new one. An attached source can implement controls differently. Its recording state and clear notifications determine the published ID. When two sources are already recording the same name, switching between them keeps the same stream. A new subscription can replay updates from the previous source. +Watch the shared state and subscribe whenever a new `streamId` appears. With the built-in recorder, calling `record()` again while recording keeps the current stream. A clear during recording gives it a new ID. Stopping removes the ID, and restarting creates a new one. An attached source can implement controls differently. Its recording state and clear notifications determine the published ID. + +If two sources are recording the same name, attaching the new source before detaching the old one keeps the stream open. A new subscription can then replay updates from the previous source. If detaching the old source first leaves the channel absent or idle, attaching the new recording source starts a stream with a new ID. The RPC actions `devframe:tracing:record`, `devframe:tracing:stop` and `devframe:tracing:clear` take the channel name. diff --git a/docs/content/8.references/4.node-api.md b/docs/content/8.references/4.node-api.md index 5829e0e5..82c9bacd 100644 --- a/docs/content/8.references/4.node-api.md +++ b/docs/content/8.references/4.node-api.md @@ -133,7 +133,7 @@ The methods on `ctx.services`: [Cross-Devframe Services](/guide/services#the-dev ## `DevframeTracingHost` -The table describes the built-in recorder on `ctx.tracing`. For a channel supplied by an attached source, that source provides the records and decides which controls it supports. See [Tracing Channels](/guide/tracing-channels). +This table lists the methods on `ctx.tracing`. The recording controls and buffer limits describe the built-in recorder. An attached source supplies its own records and decides which controls it supports. See [Tracing Channels](/guide/tracing-channels). | Method | Signature | Role | |--------|-----------|------|