DiagnosticContext
CORE · interface · Type contract
Recording scope bound to one execution identity.
A child scope inherits the sink and observer of its parent; only identity changes.
Import
import type { DiagnosticContext } from '@jangts/agent-core/diagnostics';/** @typedef {import('@jangts/agent-core/diagnostics').DiagnosticContext} DiagnosticContext */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/diagnostics.
Definition
interface DiagnosticContextField structure
| Field | Type | Required | Description |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | Optional | Read-only. Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | Required | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | Required | Records one fact. |
deliver | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void> | Required | Ordered content delivery uses the same fact, while consumer failures propagate. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | Required | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | Required | Bind live observation without changing the log destination or request identity. |
Member index
| Member | Purpose |
|---|---|
enabled | Cheap pre-check so callers can skip building expensive payloads. |
emit | Records one fact. |
deliver | Ordered content delivery uses the same fact, while consumer failures propagate. |
child | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | Bind live observation without changing the log destination or request identity. |
Member details
enabled
Cheap pre-check so callers can skip building expensive payloads.
DiagnosticContext.enabled(event: CoreEventName, content?: "metadata" | "content"): booleanCheap pre-check so callers can skip building expensive payloads.
| Parameter | Type | Passing | Description |
|---|---|---|---|
event | keyof CoreEventPayloads | Required | — |
content | "metadata" | "content" | undefined | Optional | — |
Returns: boolean.
emit
Records one fact.
DiagnosticContext.emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput): voidRecords one fact.
| Type parameter | Constraint | Default type |
|---|---|---|
K | keyof CoreEventPayloads | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
event | K | Required | — |
payload | () => CoreEventPayloads[K] | Required | — |
debug | DebugStepsOutput | undefined | Optional | — |
Optional shapes of Parameter debug: choose one shape; required fields for each branch are listed separately.
Branch 1
undefined
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
usedModel | boolean | undefined | Optional | Read-only. Model use reported by this handler; omitted when unknown. |
recordedSubSteps | readonly Readonly<RecordedSubStep>[] | undefined | Optional | Read-only. Runtime-owned immutable snapshot, committed when a logger writes or fixes a step. |
Returns: void.
deliver
Ordered content delivery uses the same fact, while consumer failures propagate.
DiagnosticContext.deliver<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>): Promise<void>Ordered content delivery uses the same fact, while consumer failures propagate.
| Type parameter | Constraint | Default type |
|---|---|---|
K | keyof CoreEventPayloads | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
event | K | Required | — |
payload | () => CoreEventPayloads[K] | Required | — |
sink | (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void> | Required | — |
Returns: Promise<void>.
Awaited value: void.
child
Derives a child span; fields fills values the child knows but the parent does not.
DiagnosticContext.child(spanId: string, fields?: Readonly<DiagnosticSpanFields>): DiagnosticContextDerives a child span; fields fills values the child knows but the parent does not.
| Parameter | Type | Passing | Description |
|---|---|---|---|
spanId | string | Required | — |
fields | Readonly<DiagnosticSpanFields> | undefined | Optional | — |
Optional shapes of Parameter fields: choose one shape; required fields for each branch are listed separately.
Branch 1
undefined
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
parentInvocationId | string | undefined | Optional | Read-only. Enclosing invocation, when one invocation triggers another. |
invocationId | string | undefined | Optional | Read-only. Invocation that produced the fact, when the fact belongs to one. |
nodeRole | "invocation" | "workflow-node" | "workflow-boundary" | "loop-step" | undefined | Optional | Read-only. Role the emitting span plays in the execution graph. |
agentId | string | undefined | Optional | Read-only. App-owned agent identity. |
branchId | string | undefined | Optional | Read-only. App-owned branch identity. |
agentVersionId | string | undefined | Optional | Read-only. App-owned immutable version identity of the agent definition. |
parentBranchId | string | undefined | Optional | Read-only. Branch this branch was forked from, when applicable. |
parentAgentVersionId | string | undefined | Optional | Read-only. Version of the parent branch at the fork point. |
forkPointId | string | undefined | Optional | Read-only. App-owned fork marker shared by the forked branch and its origin. |
workflowInvocationId | string | undefined | Optional | Read-only. Workflow invocation the fact belongs to. |
workflowNodeId | string | undefined | Optional | Read-only. Workflow node invocation the fact belongs to. |
definitionKind | string | undefined | Optional | Read-only. Kind of the workflow/agent definition that dispatched the node. |
definitionId | string | undefined | Optional | Read-only. Id of that definition. |
definitionRevision | string | undefined | Optional | Read-only. Revision of that definition; distinguishes executions of different definition states. |
nodeId | string | undefined | Optional | Read-only. Node id inside the workflow. |
nodeName | string | undefined | Optional | Read-only. Human-readable node name. |
nodeKind | string | undefined | Optional | Read-only. Node kind as declared by the App. |
nodeDescription | string | undefined | Optional | Read-only. Optional node description for diagnostics display. |
stageId | string | undefined | Optional | Read-only. Model stage that produced the fact. |
modelId | string | undefined | Optional | Read-only. Model binding used, when the fact concerns a model call. |
Returns: DiagnosticContext.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | Optional | Read-only. Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | Required | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | Required | Records one fact. |
deliver | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void> | Required | Ordered content delivery uses the same fact, while consumer failures propagate. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | Required | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | Required | Bind live observation without changing the log destination or request identity. |
withObserver
Bind live observation without changing the log destination or request identity.
DiagnosticContext.withObserver(observer: import("./execution/events.js").ExecutionObserver): DiagnosticContextBind live observation without changing the log destination or request identity.
| Parameter | Type | Passing | Description |
|---|---|---|---|
observer | ExecutionObserver | Required | — |
Fields of observer
| Field | Type | Required | Description |
|---|---|---|---|
accepts | ((descriptor: Readonly<ExecutionEventDescriptor>) => boolean) | undefined | Optional | Metadata-only by default. Content requires an explicit accepts decision. |
event | (event: Readonly<CoreEvent>) => void | Promise<void> | Required | Handles one fact; a rejected promise propagates to the emitting call. |
Returns: DiagnosticContext.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | Optional | Read-only. Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | Required | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | Required | Records one fact. |
deliver | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void> | Required | Ordered content delivery uses the same fact, while consumer failures propagate. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | Required | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | Required | Bind live observation without changing the log destination or request identity. |
Examples
- Process observation and node reports: The App subscribes to Core execution facts; nodes report their own usedModel and use an instance sub-logger to record intent first and then results, while the Loop owns identity and logging.
Observe the nodes and model events a task passes through
Beginner · Node.js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({
id: 'greeting',
nodes: [{
id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
run: state => ({ answer: '你好,' + state.name }),
routes: [route.deliver('answer')],
}],
});
const agent = new AgentCoreEngine({ workflow, execution: {
observe: { event(event) { console.log(event.type, event.eventId); } },
} });
console.log(await agent.run({ name: 'Ivan' }));import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
id: 'greeting',
nodes: [{
id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
run: state => ({ answer: '你好,' + state.name }),
routes: [route.deliver('answer')],
}],
});
const agent = new AgentCoreEngine({ workflow, execution: {
observe: { event(event) { console.log(event.type, event.eventId); } },
} });
console.log(await agent.run({ name: 'Ivan' }));Report a business stage while the node executes
Configuration · Node.js
The node only reports facts it knows; Core owns node identity and execution association.
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'greeting', nodes: [{
id: 'greet', description: '生成问候', startsRound: true,
run(state, context) {
context.report({ payload: { phase: 'formatting' } });
return { answer: '你好,' + state.name };
}, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, execution: {
observe: { event(event) { console.log(event.type); } },
} });
console.log(await agent.run({ name: 'Ivan' }));import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'greeting', nodes: [{
id: 'greet', description: '生成问候', startsRound: true,
run(state, context) {
context.report({ payload: { phase: 'formatting' } });
return { answer: '你好,' + state.name };
}, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, execution: {
observe: { event(event) { console.log(event.type); } },
} });
console.log(await agent.run({ name: 'Ivan' }));