@senpi-ai/runtime 3.0.178 → 3.0.188
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/close-position/close-position.action.d.ts.map +1 -1
- package/dist/actions/close-position/close-position.action.js +6 -1
- package/dist/actions/close-position/close-position.action.js.map +1 -1
- package/dist/actions/llm-decision/openclaw-llm-decision.d.ts +8 -0
- package/dist/actions/llm-decision/openclaw-llm-decision.d.ts.map +1 -1
- package/dist/actions/llm-decision/openclaw-llm-decision.js +56 -1
- package/dist/actions/llm-decision/openclaw-llm-decision.js.map +1 -1
- package/dist/actions/position-tracker/position-tracker.action.d.ts.map +1 -1
- package/dist/actions/position-tracker/position-tracker.action.js +1 -0
- package/dist/actions/position-tracker/position-tracker.action.js.map +1 -1
- package/dist/approvals/approval-store.d.ts +37 -7
- package/dist/approvals/approval-store.d.ts.map +1 -1
- package/dist/approvals/approval-store.js +127 -23
- package/dist/approvals/approval-store.js.map +1 -1
- package/dist/approvals/telemetry.d.ts +28 -3
- package/dist/approvals/telemetry.d.ts.map +1 -1
- package/dist/approvals/telemetry.js +8 -4
- package/dist/approvals/telemetry.js.map +1 -1
- package/dist/approvals/tool-approval-gate.d.ts +85 -0
- package/dist/approvals/tool-approval-gate.d.ts.map +1 -1
- package/dist/approvals/tool-approval-gate.js +197 -15
- package/dist/approvals/tool-approval-gate.js.map +1 -1
- package/dist/approvals/turn-context.d.ts +296 -0
- package/dist/approvals/turn-context.d.ts.map +1 -0
- package/dist/approvals/turn-context.js +594 -0
- package/dist/approvals/turn-context.js.map +1 -0
- package/dist/cli/guide-commands.d.ts.map +1 -1
- package/dist/cli/guide-commands.js +18 -3
- package/dist/cli/guide-commands.js.map +1 -1
- package/dist/cli/senpi-commands.d.ts.map +1 -1
- package/dist/cli/senpi-commands.js +80 -5
- package/dist/cli/senpi-commands.js.map +1 -1
- package/dist/deploy/job.d.ts +32 -0
- package/dist/deploy/job.d.ts.map +1 -1
- package/dist/deploy/job.js +72 -7
- package/dist/deploy/job.js.map +1 -1
- package/dist/deploy/register.d.ts +12 -0
- package/dist/deploy/register.d.ts.map +1 -1
- package/dist/deploy/register.js +85 -2
- package/dist/deploy/register.js.map +1 -1
- package/dist/dsl/events/build-closed-event.d.ts +11 -0
- package/dist/dsl/events/build-closed-event.d.ts.map +1 -1
- package/dist/dsl/events/build-closed-event.js +13 -0
- package/dist/dsl/events/build-closed-event.js.map +1 -1
- package/dist/dsl/events/handlers.d.ts.map +1 -1
- package/dist/dsl/events/handlers.js +9 -5
- package/dist/dsl/events/handlers.js.map +1 -1
- package/dist/dsl/monitor/dsl-failure-events.d.ts +38 -2
- package/dist/dsl/monitor/dsl-failure-events.d.ts.map +1 -1
- package/dist/dsl/monitor/dsl-failure-events.js +67 -4
- package/dist/dsl/monitor/dsl-failure-events.js.map +1 -1
- package/dist/dsl/monitor/process-one-position.d.ts.map +1 -1
- package/dist/dsl/monitor/process-one-position.js +37 -9
- package/dist/dsl/monitor/process-one-position.js.map +1 -1
- package/dist/host/openclaw-host.d.ts +138 -0
- package/dist/host/openclaw-host.d.ts.map +1 -0
- package/dist/host/openclaw-host.js +177 -0
- package/dist/host/openclaw-host.js.map +1 -0
- package/dist/host/plugin-logger.d.ts +99 -0
- package/dist/host/plugin-logger.d.ts.map +1 -0
- package/dist/host/plugin-logger.js +142 -0
- package/dist/host/plugin-logger.js.map +1 -0
- package/dist/index.d.ts +57 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +719 -162
- package/dist/index.js.map +1 -1
- package/dist/prompt/session-context-seed-hook.d.ts +7 -0
- package/dist/prompt/session-context-seed-hook.d.ts.map +1 -1
- package/dist/prompt/session-context-seed-hook.js +1 -1
- package/dist/prompt/session-context-seed-hook.js.map +1 -1
- package/dist/prompt/skill-router-hook.d.ts +24 -0
- package/dist/prompt/skill-router-hook.d.ts.map +1 -0
- package/dist/prompt/skill-router-hook.js +381 -0
- package/dist/prompt/skill-router-hook.js.map +1 -0
- package/dist/runtime/auto-update/coordinator.d.ts +220 -0
- package/dist/runtime/auto-update/coordinator.d.ts.map +1 -1
- package/dist/runtime/auto-update/coordinator.js +715 -22
- package/dist/runtime/auto-update/coordinator.js.map +1 -1
- package/dist/runtime/auto-update/gateway-restart.d.ts +60 -1
- package/dist/runtime/auto-update/gateway-restart.d.ts.map +1 -1
- package/dist/runtime/auto-update/gateway-restart.js +95 -21
- package/dist/runtime/auto-update/gateway-restart.js.map +1 -1
- package/dist/runtime/auto-update/idle-gate.d.ts +175 -0
- package/dist/runtime/auto-update/idle-gate.d.ts.map +1 -0
- package/dist/runtime/auto-update/idle-gate.js +229 -0
- package/dist/runtime/auto-update/idle-gate.js.map +1 -0
- package/dist/runtime/auto-update/install-results.d.ts +130 -0
- package/dist/runtime/auto-update/install-results.d.ts.map +1 -0
- package/dist/runtime/auto-update/install-results.js +264 -0
- package/dist/runtime/auto-update/install-results.js.map +1 -0
- package/dist/runtime/auto-update/money-call-tracker.d.ts +36 -0
- package/dist/runtime/auto-update/money-call-tracker.d.ts.map +1 -0
- package/dist/runtime/auto-update/money-call-tracker.js +129 -0
- package/dist/runtime/auto-update/money-call-tracker.js.map +1 -0
- package/dist/runtime/auto-update/register-idle-gate.d.ts +70 -0
- package/dist/runtime/auto-update/register-idle-gate.d.ts.map +1 -0
- package/dist/runtime/auto-update/register-idle-gate.js +127 -0
- package/dist/runtime/auto-update/register-idle-gate.js.map +1 -0
- package/dist/runtime/auto-update/run-update-command.d.ts +13 -0
- package/dist/runtime/auto-update/run-update-command.d.ts.map +1 -1
- package/dist/runtime/auto-update/run-update-command.js +18 -0
- package/dist/runtime/auto-update/run-update-command.js.map +1 -1
- package/dist/runtime/auto-update/state-store.d.ts +14 -1
- package/dist/runtime/auto-update/state-store.d.ts.map +1 -1
- package/dist/runtime/auto-update/state-store.js +37 -3
- package/dist/runtime/auto-update/state-store.js.map +1 -1
- package/dist/runtime/auto-update/types.d.ts +52 -0
- package/dist/runtime/auto-update/types.d.ts.map +1 -1
- package/dist/runtime/auto-update/types.js.map +1 -1
- package/dist/runtime/auto-update/update-marker.d.ts +95 -0
- package/dist/runtime/auto-update/update-marker.d.ts.map +1 -0
- package/dist/runtime/auto-update/update-marker.js +185 -0
- package/dist/runtime/auto-update/update-marker.js.map +1 -0
- package/dist/runtime/auto-update/update-schedule.d.ts +67 -0
- package/dist/runtime/auto-update/update-schedule.d.ts.map +1 -0
- package/dist/runtime/auto-update/update-schedule.js +91 -0
- package/dist/runtime/auto-update/update-schedule.js.map +1 -0
- package/dist/runtime/decision-errors.d.ts +7 -0
- package/dist/runtime/decision-errors.d.ts.map +1 -1
- package/dist/runtime/decision-errors.js +15 -0
- package/dist/runtime/decision-errors.js.map +1 -1
- package/dist/runtime/process-state.d.ts +168 -0
- package/dist/runtime/process-state.d.ts.map +1 -0
- package/dist/runtime/process-state.js +81 -0
- package/dist/runtime/process-state.js.map +1 -0
- package/dist/runtime/reconcile-state.d.ts.map +1 -1
- package/dist/runtime/reconcile-state.js +21 -4
- package/dist/runtime/reconcile-state.js.map +1 -1
- package/dist/runtime/strategy-registry.d.ts +16 -0
- package/dist/runtime/strategy-registry.d.ts.map +1 -1
- package/dist/runtime/strategy-registry.js +7 -3
- package/dist/runtime/strategy-registry.js.map +1 -1
- package/dist/runtime/update-apply-deps.d.ts +5 -3
- package/dist/runtime/update-apply-deps.d.ts.map +1 -1
- package/dist/runtime/update-apply-deps.js +15 -3
- package/dist/runtime/update-apply-deps.js.map +1 -1
- package/dist/senpi/client.d.ts +29 -0
- package/dist/senpi/client.d.ts.map +1 -1
- package/dist/senpi/client.js +71 -2
- package/dist/senpi/client.js.map +1 -1
- package/dist/skill-router/catalog.d.ts +30 -0
- package/dist/skill-router/catalog.d.ts.map +1 -0
- package/dist/skill-router/catalog.js +131 -0
- package/dist/skill-router/catalog.js.map +1 -0
- package/dist/skill-router/classify-client.d.ts +80 -0
- package/dist/skill-router/classify-client.d.ts.map +1 -0
- package/dist/skill-router/classify-client.js +283 -0
- package/dist/skill-router/classify-client.js.map +1 -0
- package/dist/skill-router/prompt.d.ts +36 -0
- package/dist/skill-router/prompt.d.ts.map +1 -0
- package/dist/skill-router/prompt.js +318 -0
- package/dist/skill-router/prompt.js.map +1 -0
- package/dist/skill-router/resolve.d.ts +41 -0
- package/dist/skill-router/resolve.d.ts.map +1 -0
- package/dist/skill-router/resolve.js +43 -0
- package/dist/skill-router/resolve.js.map +1 -0
- package/dist/skills-manager/version-manager.d.ts +14 -0
- package/dist/skills-manager/version-manager.d.ts.map +1 -1
- package/dist/skills-manager/version-manager.js +31 -3
- package/dist/skills-manager/version-manager.js.map +1 -1
- package/dist/strategy/strategy-state.d.ts +11 -3
- package/dist/strategy/strategy-state.d.ts.map +1 -1
- package/dist/strategy/strategy-state.js +18 -8
- package/dist/strategy/strategy-state.js.map +1 -1
- package/dist/telemetry/event-builders.d.ts +4 -2
- package/dist/telemetry/event-builders.d.ts.map +1 -1
- package/dist/telemetry/event-builders.js +4 -2
- package/dist/telemetry/event-builders.js.map +1 -1
- package/dist/telemetry/metrics.d.ts +15 -1
- package/dist/telemetry/metrics.d.ts.map +1 -1
- package/dist/telemetry/metrics.js +25 -1
- package/dist/telemetry/metrics.js.map +1 -1
- package/dist/telemetry/otel-sdk.d.ts +12 -0
- package/dist/telemetry/otel-sdk.d.ts.map +1 -1
- package/dist/telemetry/otel-sdk.js +47 -1
- package/dist/telemetry/otel-sdk.js.map +1 -1
- package/dist/telemetry/spans/span-hooks.d.ts +18 -1
- package/dist/telemetry/spans/span-hooks.d.ts.map +1 -1
- package/dist/telemetry/spans/span-hooks.js +315 -96
- package/dist/telemetry/spans/span-hooks.js.map +1 -1
- package/dist/telemetry/tool-plane.d.ts +2 -0
- package/dist/telemetry/tool-plane.d.ts.map +1 -1
- package/dist/telemetry/tool-plane.js +1 -0
- package/dist/telemetry/tool-plane.js.map +1 -1
- package/dist/test-setup.js +7 -1
- package/dist/test-setup.js.map +1 -1
- package/dist/tool-surface/crib-cache.d.ts +21 -0
- package/dist/tool-surface/crib-cache.d.ts.map +1 -0
- package/dist/tool-surface/crib-cache.js +54 -0
- package/dist/tool-surface/crib-cache.js.map +1 -0
- package/dist/tool-surface/crib-hook.d.ts +78 -0
- package/dist/tool-surface/crib-hook.d.ts.map +1 -0
- package/dist/tool-surface/crib-hook.js +225 -0
- package/dist/tool-surface/crib-hook.js.map +1 -0
- package/dist/tool-surface/crib-sheet.d.ts +56 -0
- package/dist/tool-surface/crib-sheet.d.ts.map +1 -0
- package/dist/tool-surface/crib-sheet.js +129 -0
- package/dist/tool-surface/crib-sheet.js.map +1 -0
- package/dist/tool-surface/mcp-tool-source.d.ts +64 -0
- package/dist/tool-surface/mcp-tool-source.d.ts.map +1 -0
- package/dist/tool-surface/mcp-tool-source.js +189 -0
- package/dist/tool-surface/mcp-tool-source.js.map +1 -0
- package/dist/utils/event-catalog.d.ts +228 -6
- package/dist/utils/event-catalog.d.ts.map +1 -1
- package/dist/utils/event-catalog.js +32 -0
- package/dist/utils/event-catalog.js.map +1 -1
- package/dist/widgets/tool.d.ts +9 -0
- package/dist/widgets/tool.d.ts.map +1 -1
- package/dist/widgets/tool.js +1 -0
- package/dist/widgets/tool.js.map +1 -1
- package/openclaw.plugin.json +11 -1
- package/package.json +5 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"span-hooks.d.ts","sourceRoot":"","sources":["../../../src/telemetry/spans/span-hooks.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"span-hooks.d.ts","sourceRoot":"","sources":["../../../src/telemetry/spans/span-hooks.ts"],"names":[],"mappings":"AAmFA,gEAAgE;AAChE,MAAM,WAAW,kBAAkB;IACjC,EAAE,CACA,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EACrD,IAAI,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,GAC3B,IAAI,CAAC;CACT;AAED,MAAM,WAAW,wBAAwB;IACvC,GAAG,EAAE,kBAAkB,CAAC;IACxB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAWD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAG5D;AAED;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAW5C;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,MAAM,CA8wB3E"}
|
|
@@ -8,6 +8,21 @@
|
|
|
8
8
|
* - subagent_* lifecycle → `invoke_agent` (INTERNAL), parented to the model span
|
|
9
9
|
* - agent_end → ends any still-open spans for the run
|
|
10
10
|
*
|
|
11
|
+
* On OpenClaw 10.x (`modernHost`):
|
|
12
|
+
* - there is no `subagent_spawning` hook, so `subagent_spawned` opens the `invoke_agent` span;
|
|
13
|
+
* - the tool-search tools (`tool_search`, `tool_describe`, and the dispatcher `dispatch_action`,
|
|
14
|
+
* `tool_call` on 10.4) get `execute_tool` spans with `senpi.tool.bridge = true` and count on
|
|
15
|
+
* `tool.bridge.calls`, not `tool.calls`. The inner tool a dispatcher runs (id
|
|
16
|
+
* `tool_call:<sanitized outer id>:…`) nests under the dispatcher's span. Its own
|
|
17
|
+
* `after_tool_call` normally closes it (10.10 `embedded-agent-subscribe.handlers.tools.completion.ts:603-626`);
|
|
18
|
+
* any inner span still open when the dispatcher's `after_tool_call` fires is closed there with
|
|
19
|
+
* the outer outcome, and a late inner `after_tool_call` for it is then ignored, so each inner call
|
|
20
|
+
* is counted and evented once under its own name.
|
|
21
|
+
*
|
|
22
|
+
* On both hosts each `chat` span carries `senpi.llm.calls` (the run's `model_call_ended` count),
|
|
23
|
+
* and the drift alarm skips any unoffered name the host ran in the run (a direct call to a deferred
|
|
24
|
+
* tool is legal in 10.x tool-search directory mode).
|
|
25
|
+
*
|
|
11
26
|
* Open and finalize are separate callbacks, so we can't use `withSpan`; we `startSpan`
|
|
12
27
|
* and hold the handle in a session-keyed registry until the finalize hook, recording
|
|
13
28
|
* errors directly on the held span (it's never the active span, so `logger.error` won't).
|
|
@@ -21,10 +36,32 @@ import { SpanKind, context as otelContext, propagation, trace, } from "@opentele
|
|
|
21
36
|
import { getTracer } from "../../telemetry/tracing.js";
|
|
22
37
|
import { HEADER_ADDRESS, HEADER_CORRELATION_ID, HEADER_RECIPE_NAME, HEADER_RUNTIME_ID, HEADER_RUNTIME_VERSION, } from "./constants.js";
|
|
23
38
|
import { GEN_AI, errorTypeOf, firstNumber, normalizeGenAiMessages, recordSpanError, setDefinedAttributes, setGenAiContentAttribute, toFinishReasons, } from "./gen-ai.js";
|
|
24
|
-
import { elapsedSeconds, recordToolCall, recordInvokeAgentDuration, recordOperationDuration, recordTokenUsage, } from "../../telemetry/metrics.js";
|
|
39
|
+
import { elapsedSeconds, recordToolBridgeCall, recordToolCall, recordInvokeAgentDuration, recordOperationDuration, recordTokenUsage, } from "../../telemetry/metrics.js";
|
|
25
40
|
import { emitToolCalled, offeredToolNames, requestedToolNames, } from "../../telemetry/tool-plane.js";
|
|
26
41
|
import { createLogger } from "../../utils/logger.js";
|
|
27
42
|
const logger = createLogger("[senpi-core][span-hooks]");
|
|
43
|
+
/**
|
|
44
|
+
* OpenClaw's per-run `runId` on the chat and execute_tool spans: the join key the skill router's
|
|
45
|
+
* `skill_router.decision` event shares (`src/utils/event-catalog.ts`). Distinct from `senpi.run.id`.
|
|
46
|
+
*/
|
|
47
|
+
const OPENCLAW_RUN_ID_ATTR = "senpi.openclaw.run_id";
|
|
48
|
+
/** The tool-search bridge tools and their `senpi.tool.bridge_kind` (10.10 `src/agents/tool-search-types.ts:10-12`; `tool_call` is the 10.4 dispatcher). */
|
|
49
|
+
const BRIDGE_TOOL_KINDS = {
|
|
50
|
+
tool_search: "search",
|
|
51
|
+
tool_describe: "describe",
|
|
52
|
+
dispatch_action: "call",
|
|
53
|
+
tool_call: "call",
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Port of OpenClaw's inner-call id sanitizer
|
|
57
|
+
* (`openclaw-worktrees/v2026.10.10/src/agents/tool-search-runtime.ts:211-214`): the inner
|
|
58
|
+
* call a dispatcher runs gets the id `tool_call:${sanitizeToolCallIdPart(outerToolCallId)}:…`
|
|
59
|
+
* (the literal `tool_call:` prefix is kept on 10.10, where only the dispatcher was renamed).
|
|
60
|
+
*/
|
|
61
|
+
export function sanitizeToolCallIdPart(value) {
|
|
62
|
+
const safe = value.trim().replace(/[^A-Za-z0-9_.:-]+/g, "_");
|
|
63
|
+
return safe.slice(0, 120) || "call";
|
|
64
|
+
}
|
|
28
65
|
/**
|
|
29
66
|
* Grace window the `agent_end` sweep holds a finished model span before ending it, so a
|
|
30
67
|
* trailing `llm_output` (observed ~1 ms later in the codex/harness flow, but timed generously)
|
|
@@ -32,9 +69,20 @@ const logger = createLogger("[senpi-core][span-hooks]");
|
|
|
32
69
|
* process open; an orphan run with no `llm_output` lets the timer end the span.
|
|
33
70
|
*/
|
|
34
71
|
export const MODEL_SPAN_END_GRACE_MS = 2000;
|
|
72
|
+
/**
|
|
73
|
+
* Registers the span hooks.
|
|
74
|
+
*
|
|
75
|
+
* @returns How many hooks were registered (10 on 5.x; 9 on 10.x, which has no `subagent_spawning`).
|
|
76
|
+
*/
|
|
35
77
|
export function registerSpanHooks(options) {
|
|
36
78
|
const { api } = options;
|
|
79
|
+
const modernHost = options.modernHost === true;
|
|
37
80
|
const tracer = getTracer();
|
|
81
|
+
let hookCount = 0;
|
|
82
|
+
const on = (hookName, handler, opts) => {
|
|
83
|
+
api.on(hookName, handler, opts);
|
|
84
|
+
hookCount += 1;
|
|
85
|
+
};
|
|
38
86
|
// Cap on drift events per turn so a flood of invented tool calls can't balloon the log.
|
|
39
87
|
const MAX_DRIFT_EVENTS_PER_TURN = 8;
|
|
40
88
|
// Held model spans, keyed by (sessionKey, runId). The tool/subagent spans parent
|
|
@@ -49,13 +97,23 @@ export function registerSpanHooks(options) {
|
|
|
49
97
|
// (agentId → sessionKey/runId) from llm_input, plus the last-seen pair, so tool
|
|
50
98
|
// hooks can resolve their host session.
|
|
51
99
|
const sessionByAgentId = new Map();
|
|
100
|
+
// Latest run per session, for a 10.x subagent_spawned whose ctx names the requester session but
|
|
101
|
+
// not its run (ctx.runId is the child run there).
|
|
102
|
+
const latestRunBySession = new Map();
|
|
52
103
|
let lastActive = null;
|
|
53
104
|
const rememberSession = (agentCtx, sessionKey, runId) => {
|
|
54
105
|
const agentId = nonEmptyString(asRecord(agentCtx)?.agentId);
|
|
55
106
|
if (agentId)
|
|
56
107
|
sessionByAgentId.set(agentId, { sessionKey, runId });
|
|
108
|
+
latestRunBySession.set(sessionKey, runId);
|
|
57
109
|
lastActive = { sessionKey, runId };
|
|
58
110
|
};
|
|
111
|
+
/** Stamps the run's model-call count on a `chat` span about to end. */
|
|
112
|
+
const stampLlmCalls = (held) => {
|
|
113
|
+
if (held.llmCalls && held.llmCalls > 0) {
|
|
114
|
+
setDefinedAttributes(held.span, { "senpi.llm.calls": held.llmCalls });
|
|
115
|
+
}
|
|
116
|
+
};
|
|
59
117
|
const resolveToolSession = (ctx) => {
|
|
60
118
|
const direct = {
|
|
61
119
|
sessionKey: readSessionKey(ctx, undefined),
|
|
@@ -81,7 +139,7 @@ export function registerSpanHooks(options) {
|
|
|
81
139
|
return model ? trace.setSpan(otelContext.active(), model.span) : otelContext.active();
|
|
82
140
|
};
|
|
83
141
|
// ─── LLM (`chat` span) ───────────────────────────────────────────────────
|
|
84
|
-
|
|
142
|
+
on("llm_input", (event, agentCtx) => {
|
|
85
143
|
try {
|
|
86
144
|
const identity = readIdentity(agentCtx);
|
|
87
145
|
const sessionKey = readSessionKey(agentCtx, event);
|
|
@@ -95,8 +153,13 @@ export function registerSpanHooks(options) {
|
|
|
95
153
|
// A duplicate llm_input for the same (session, run) means a new turn opened
|
|
96
154
|
// without the previous one closing — end the prior span before replacing it.
|
|
97
155
|
const prior = modelSpans.get(key);
|
|
98
|
-
if (prior)
|
|
156
|
+
if (prior) {
|
|
157
|
+
// Its agent_end grace timer would end the replaced span a second time.
|
|
158
|
+
if (prior.endTimer)
|
|
159
|
+
clearTimeout(prior.endTimer);
|
|
160
|
+
stampLlmCalls(prior);
|
|
99
161
|
endSpan(prior.span);
|
|
162
|
+
}
|
|
100
163
|
const ev = asRecord(event) ?? {};
|
|
101
164
|
const requestModel = nonEmptyString(ev.model);
|
|
102
165
|
const provider = nonEmptyString(ev.provider);
|
|
@@ -121,6 +184,9 @@ export function registerSpanHooks(options) {
|
|
|
121
184
|
// Session key as the conversation id, so consecutive turns of one session correlate (a
|
|
122
185
|
// single per-turn trace otherwise can't be tied to its neighbours — e.g. a runaway loop).
|
|
123
186
|
[GEN_AI.CONVERSATION_ID]: sessionKey,
|
|
187
|
+
// OpenClaw's own run id, so the skill router's decision event joins this turn exactly.
|
|
188
|
+
// Not `senpi.run.id`, which is the trading run id from the correlation header.
|
|
189
|
+
[OPENCLAW_RUN_ID_ATTR]: runId,
|
|
124
190
|
[GEN_AI.PROVIDER_NAME]: provider,
|
|
125
191
|
[GEN_AI.REQUEST_MODEL]: requestModel,
|
|
126
192
|
[GEN_AI.REQUEST_TEMPERATURE]: firstNumber(ev.temperature),
|
|
@@ -140,7 +206,7 @@ export function registerSpanHooks(options) {
|
|
|
140
206
|
logger.warn("llm_input handler failed", { err: errMsg(err) });
|
|
141
207
|
}
|
|
142
208
|
});
|
|
143
|
-
|
|
209
|
+
on("llm_output", (event, agentCtx) => {
|
|
144
210
|
try {
|
|
145
211
|
const sessionKey = readSessionKey(agentCtx, event);
|
|
146
212
|
const runId = readRunId(agentCtx, event);
|
|
@@ -159,6 +225,7 @@ export function registerSpanHooks(options) {
|
|
|
159
225
|
// Cancel any deferred teardown the agent_end sweep scheduled — we finalize the span here.
|
|
160
226
|
if (held.endTimer)
|
|
161
227
|
clearTimeout(held.endTimer);
|
|
228
|
+
stampLlmCalls(held);
|
|
162
229
|
const ev = asRecord(event) ?? {};
|
|
163
230
|
const usage = asRecord(ev.usage);
|
|
164
231
|
// Token field naming varies by provider/wrapper; try every known spelling.
|
|
@@ -193,9 +260,14 @@ export function registerSpanHooks(options) {
|
|
|
193
260
|
// deduped + capped so a flood of invented calls can't balloon the log.
|
|
194
261
|
if (held.offeredTools) {
|
|
195
262
|
const offered = held.offeredTools;
|
|
263
|
+
// A name the host ran in this run is not invented: in 10.x tool-search directory mode a
|
|
264
|
+
// direct call to any deferred tool (a `senpi__` MCP tool or a deferred OpenClaw tool) is
|
|
265
|
+
// legal though not in the offered set (`tool-search-directory.ts:194-197` on 10.10). An
|
|
266
|
+
// unoffered name that never ran is drift on every host and in every mode.
|
|
267
|
+
const ran = held.ranTools;
|
|
196
268
|
const seen = new Set();
|
|
197
269
|
for (const name of requestedToolNames(outputMessages)) {
|
|
198
|
-
if (offered.has(name) || seen.has(name))
|
|
270
|
+
if (offered.has(name) || seen.has(name) || ran?.has(name))
|
|
199
271
|
continue;
|
|
200
272
|
if (seen.size >= MAX_DRIFT_EVENTS_PER_TURN)
|
|
201
273
|
break;
|
|
@@ -229,7 +301,7 @@ export function registerSpanHooks(options) {
|
|
|
229
301
|
// The provider-call result: `outcome`, `failureKind`, timing. This is the ONLY
|
|
230
302
|
// signal for a failed/aborted turn — `llm_output` never fires when the call
|
|
231
303
|
// errors, so without this a wedged turn is indistinguishable from a healthy one.
|
|
232
|
-
|
|
304
|
+
on("model_call_ended", (event, agentCtx) => {
|
|
233
305
|
try {
|
|
234
306
|
const sessionKey = readSessionKey(agentCtx, event);
|
|
235
307
|
const runId = readRunId(agentCtx, event);
|
|
@@ -238,6 +310,7 @@ export function registerSpanHooks(options) {
|
|
|
238
310
|
const held = modelSpans.get(sessionLlmKey(sessionKey, runId));
|
|
239
311
|
if (!held)
|
|
240
312
|
return; // span already finalized, or not one we opened — best effort.
|
|
313
|
+
held.llmCalls = (held.llmCalls ?? 0) + 1;
|
|
241
314
|
const ev = asRecord(event) ?? {};
|
|
242
315
|
const outcome = nonEmptyString(ev.outcome);
|
|
243
316
|
const failureKind = nonEmptyString(ev.failureKind);
|
|
@@ -262,7 +335,59 @@ export function registerSpanHooks(options) {
|
|
|
262
335
|
}
|
|
263
336
|
});
|
|
264
337
|
// ─── Tool (`execute_tool` span) ────────────────────────────────────────────
|
|
265
|
-
|
|
338
|
+
// Inner calls a dispatcher's after_tool_call closed, so their own late after_tool_call is not
|
|
339
|
+
// counted again; the value says whether that close already evented a failure (the outer call
|
|
340
|
+
// errored). FIFO-bounded: a host that never sends that late hook must not grow it.
|
|
341
|
+
const closedByDispatcher = new Map();
|
|
342
|
+
const CLOSED_BY_DISPATCHER_MAX = 512;
|
|
343
|
+
const innerKey = (sessionKey, runId, toolCallId) => `${sessionKey} ${runId} ${toolCallId}`;
|
|
344
|
+
const markClosedByDispatcher = (key, failureEvented) => {
|
|
345
|
+
closedByDispatcher.set(key, failureEvented);
|
|
346
|
+
while (closedByDispatcher.size > CLOSED_BY_DISPATCHER_MAX) {
|
|
347
|
+
const oldest = closedByDispatcher.keys().next().value;
|
|
348
|
+
closedByDispatcher.delete(oldest);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* A 10.x dispatcher's open `execute_tool` span in this run whose inner-call prefix
|
|
353
|
+
* `tool_call:${sanitize(outer id)}:` starts `innerToolCallId`, for parenting the inner span.
|
|
354
|
+
*/
|
|
355
|
+
const findDispatcherSpanFor = (sessionKey, runId, innerToolCallId) => {
|
|
356
|
+
for (const held of toolSpans.values()) {
|
|
357
|
+
if (held.bridgeKind !== "call" || !held.toolCallId)
|
|
358
|
+
continue;
|
|
359
|
+
if (held.sessionKey !== sessionKey || held.runId !== runId)
|
|
360
|
+
continue;
|
|
361
|
+
if (innerToolCallId.startsWith(`tool_call:${sanitizeToolCallIdPart(held.toolCallId)}:`))
|
|
362
|
+
return held;
|
|
363
|
+
}
|
|
364
|
+
return undefined;
|
|
365
|
+
};
|
|
366
|
+
/** Records the outcome on a held tool span and ends it, after its args stamping settles. */
|
|
367
|
+
const finalizeToolSpan = (held, errored, error, result) => {
|
|
368
|
+
const span = held.span;
|
|
369
|
+
void (async () => {
|
|
370
|
+
try {
|
|
371
|
+
if (held.pending)
|
|
372
|
+
await held.pending;
|
|
373
|
+
if (errored) {
|
|
374
|
+
recordSpanError(span, error);
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
await setGenAiContentAttribute(span, GEN_AI.TOOL_CALL_RESULT, result);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
finally {
|
|
381
|
+
try {
|
|
382
|
+
span.end();
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
/* best effort */
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
})();
|
|
389
|
+
};
|
|
390
|
+
on("before_tool_call", (event, toolCtx) => {
|
|
266
391
|
try {
|
|
267
392
|
const identity = readIdentity(toolCtx);
|
|
268
393
|
const resolved = resolveToolSession(toolCtx);
|
|
@@ -275,21 +400,36 @@ export function registerSpanHooks(options) {
|
|
|
275
400
|
const toolCallId = nonEmptyString(ev?.toolCallId) ??
|
|
276
401
|
nonEmptyString(asRecord(toolCtx)?.toolCallId) ??
|
|
277
402
|
null;
|
|
403
|
+
const model = modelSpans.get(sessionLlmKey(resolved.sessionKey, resolved.runId));
|
|
404
|
+
if (model)
|
|
405
|
+
(model.ranTools ??= new Set()).add(toolName);
|
|
406
|
+
const bridgeKind = modernHost ? BRIDGE_TOOL_KINDS[toolName] : undefined;
|
|
407
|
+
// An inner call a 10.x dispatcher runs nests under the dispatcher's span.
|
|
408
|
+
const dispatcher = modernHost && !bridgeKind && toolCallId?.startsWith("tool_call:")
|
|
409
|
+
? findDispatcherSpanFor(resolved.sessionKey, resolved.runId, toolCallId)
|
|
410
|
+
: undefined;
|
|
278
411
|
const key = toolKey(resolved.sessionKey, resolved.runId, toolCallId, toolName, ++toolSeq);
|
|
279
|
-
const span = tracer.startSpan(`execute_tool ${toolName}`, { kind: SpanKind.INTERNAL },
|
|
412
|
+
const span = tracer.startSpan(`execute_tool ${toolName}`, { kind: SpanKind.INTERNAL }, dispatcher
|
|
413
|
+
? trace.setSpan(otelContext.active(), dispatcher.span)
|
|
414
|
+
: parentContextForModel(resolved.sessionKey, resolved.runId));
|
|
280
415
|
const held = {
|
|
281
416
|
span,
|
|
282
417
|
sessionKey: resolved.sessionKey,
|
|
283
418
|
runId: resolved.runId,
|
|
284
419
|
startMs: Date.now(),
|
|
285
420
|
toolName,
|
|
421
|
+
...(toolCallId ? { toolCallId } : {}),
|
|
422
|
+
...(bridgeKind ? { bridgeKind } : {}),
|
|
423
|
+
...(bridgeKind === "call" ? { requestedId: nonEmptyString(asRecord(ev?.params)?.id) } : {}),
|
|
286
424
|
};
|
|
287
425
|
toolSpans.set(key, held);
|
|
288
426
|
setDefinedAttributes(span, {
|
|
289
427
|
[GEN_AI.OPERATION_NAME]: "execute_tool",
|
|
290
428
|
[GEN_AI.CONVERSATION_ID]: resolved.sessionKey,
|
|
429
|
+
[OPENCLAW_RUN_ID_ATTR]: resolved.runId,
|
|
291
430
|
[GEN_AI.TOOL_NAME]: toolName,
|
|
292
431
|
[GEN_AI.TOOL_CALL_ID]: toolCallId ?? undefined,
|
|
432
|
+
"senpi.tool.bridge": bridgeKind ? true : undefined,
|
|
293
433
|
// Best-effort skill attribution from a skill-file read (no first-class skill hook exists).
|
|
294
434
|
"senpi.skill.name": skillNameFromToolArgs(ev?.params ?? ev?.input),
|
|
295
435
|
...identityAttributes(identity),
|
|
@@ -302,42 +442,117 @@ export function registerSpanHooks(options) {
|
|
|
302
442
|
logger.warn("before_tool_call handler failed", { err: errMsg(err) });
|
|
303
443
|
}
|
|
304
444
|
});
|
|
305
|
-
|
|
445
|
+
on("after_tool_call", (event, toolCtx) => {
|
|
306
446
|
try {
|
|
307
447
|
const resolved = resolveToolSession(toolCtx);
|
|
308
|
-
if (!resolved) {
|
|
309
|
-
logger.debug("after_tool_call session unresolved; skipping");
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
448
|
const ev = asRecord(event);
|
|
313
449
|
const toolName = nonEmptyString(ev?.toolName) ?? nonEmptyString(ev?.name) ?? "unknown";
|
|
314
450
|
const error = ev?.error;
|
|
315
451
|
const errored = error !== undefined && error !== null;
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
452
|
+
const result = ev?.result;
|
|
453
|
+
const toolCallId = nonEmptyString(ev?.toolCallId) ??
|
|
454
|
+
nonEmptyString(asRecord(toolCtx)?.toolCallId) ??
|
|
455
|
+
null;
|
|
456
|
+
const bridgeKind = modernHost ? BRIDGE_TOOL_KINDS[toolName] : undefined;
|
|
457
|
+
const emitFailure = (name, bridge) => {
|
|
321
458
|
emitToolCalled({
|
|
322
|
-
name
|
|
459
|
+
name,
|
|
323
460
|
origin: "agent",
|
|
324
461
|
outcome: "error",
|
|
325
462
|
errorType: errorTypeOf(error),
|
|
326
463
|
errorMessage: error instanceof Error ? error.message : String(error),
|
|
464
|
+
...(bridge ? { bridge: true } : {}),
|
|
327
465
|
});
|
|
466
|
+
};
|
|
467
|
+
const foundKey = resolved
|
|
468
|
+
? findToolKey(toolSpans, resolved.sessionKey, resolved.runId, toolCallId, toolName)
|
|
469
|
+
: undefined;
|
|
470
|
+
const held = foundKey ? toolSpans.get(foundKey) : undefined;
|
|
471
|
+
if (foundKey)
|
|
472
|
+
toolSpans.delete(foundKey);
|
|
473
|
+
if (bridgeKind) {
|
|
474
|
+
// Inner calls of this dispatcher still open: their own after_tool_call normally closed them
|
|
475
|
+
// first (10.10/10.4), so these are the ones it missed. They take the outer outcome here,
|
|
476
|
+
// count once under their own names, and a late after_tool_call for them is ignored.
|
|
477
|
+
const inner = [];
|
|
478
|
+
if (bridgeKind === "call" && resolved && toolCallId) {
|
|
479
|
+
const prefix = `tool_call:${sanitizeToolCallIdPart(toolCallId)}:`;
|
|
480
|
+
for (const [k, h] of toolSpans) {
|
|
481
|
+
if (h.sessionKey !== resolved.sessionKey || h.runId !== resolved.runId)
|
|
482
|
+
continue;
|
|
483
|
+
if (!h.toolCallId?.startsWith(prefix))
|
|
484
|
+
continue;
|
|
485
|
+
toolSpans.delete(k);
|
|
486
|
+
markClosedByDispatcher(innerKey(h.sessionKey, h.runId, h.toolCallId), errored);
|
|
487
|
+
inner.push(h);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (errored) {
|
|
491
|
+
if (inner.length > 0) {
|
|
492
|
+
for (const h of inner)
|
|
493
|
+
emitFailure(h.toolName ?? "unknown");
|
|
494
|
+
}
|
|
495
|
+
else if (bridgeKind === "call" && held?.innerErrored) {
|
|
496
|
+
// The inner call's own after_tool_call already reported this failure.
|
|
497
|
+
}
|
|
498
|
+
else if (bridgeKind === "call") {
|
|
499
|
+
// No inner call, or one that succeeded: the dispatcher's own failure (a cap, a result
|
|
500
|
+
// post-processing error) is still a failure the agent saw, so it is always evented.
|
|
501
|
+
const requested = held?.requestedId ?? nonEmptyString(asRecord(ev?.params)?.id);
|
|
502
|
+
emitFailure(requested ?? toolName, requested !== undefined);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
emitFailure(toolName);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
for (const h of inner) {
|
|
509
|
+
recordToolCall({
|
|
510
|
+
toolName: h.toolName,
|
|
511
|
+
origin: "agent",
|
|
512
|
+
outcome: errored ? "error" : "ok",
|
|
513
|
+
errorType: errored ? errorTypeOf(error) : undefined,
|
|
514
|
+
durationSec: elapsedSeconds(h.startMs),
|
|
515
|
+
});
|
|
516
|
+
finalizeToolSpan(h, errored, error, result);
|
|
517
|
+
}
|
|
518
|
+
recordToolBridgeCall({ kind: bridgeKind, outcome: errored ? "error" : "ok" });
|
|
519
|
+
if (held)
|
|
520
|
+
finalizeToolSpan(held, errored, error, result);
|
|
521
|
+
return;
|
|
328
522
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
523
|
+
// A late after_tool_call for an inner call its dispatcher already closed and counted. Its
|
|
524
|
+
// span and metric are done; a failure it reports is still evented unless the dispatcher's
|
|
525
|
+
// close already did (a dispatcher can return an inner error to the model as a result).
|
|
526
|
+
if (modernHost && resolved && toolCallId) {
|
|
527
|
+
const k = innerKey(resolved.sessionKey, resolved.runId, toolCallId);
|
|
528
|
+
const failureEvented = closedByDispatcher.get(k);
|
|
529
|
+
if (failureEvented !== undefined) {
|
|
530
|
+
closedByDispatcher.delete(k);
|
|
531
|
+
if (errored && !failureEvented)
|
|
532
|
+
emitFailure(toolName);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
// Tool-plane event (narrative): a failed tool call — the agent-blind hole, greppable instead
|
|
537
|
+
// of span-only. A successful call already rides its span + (state-changing) its domain event,
|
|
538
|
+
// so it is not re-evented here. Emitted before the span check so a failure is never lost when
|
|
539
|
+
// the matching before_tool_call span wasn't registered.
|
|
540
|
+
if (errored)
|
|
541
|
+
emitFailure(toolName);
|
|
542
|
+
// Tell the still-open dispatcher that its inner call's failure is already reported.
|
|
543
|
+
if (errored && modernHost && resolved && toolCallId?.startsWith("tool_call:")) {
|
|
544
|
+
const dispatcher = findDispatcherSpanFor(resolved.sessionKey, resolved.runId, toolCallId);
|
|
545
|
+
if (dispatcher)
|
|
546
|
+
dispatcher.innerErrored = true;
|
|
547
|
+
}
|
|
548
|
+
if (!resolved) {
|
|
549
|
+
logger.debug("after_tool_call session unresolved; skipping");
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (!held) {
|
|
334
553
|
logger.debug("after_tool_call: no open tool span", { toolName, toolCallId });
|
|
335
554
|
return;
|
|
336
555
|
}
|
|
337
|
-
const held = toolSpans.get(foundKey);
|
|
338
|
-
const span = held.span;
|
|
339
|
-
toolSpans.delete(foundKey);
|
|
340
|
-
const result = ev?.result;
|
|
341
556
|
recordToolCall({
|
|
342
557
|
toolName: held.toolName,
|
|
343
558
|
origin: "agent",
|
|
@@ -345,83 +560,83 @@ export function registerSpanHooks(options) {
|
|
|
345
560
|
errorType: errored ? errorTypeOf(error) : undefined,
|
|
346
561
|
durationSec: elapsedSeconds(held.startMs),
|
|
347
562
|
});
|
|
348
|
-
|
|
349
|
-
try {
|
|
350
|
-
if (held.pending)
|
|
351
|
-
await held.pending;
|
|
352
|
-
if (errored) {
|
|
353
|
-
recordSpanError(span, error);
|
|
354
|
-
}
|
|
355
|
-
else {
|
|
356
|
-
await setGenAiContentAttribute(span, GEN_AI.TOOL_CALL_RESULT, result);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
finally {
|
|
360
|
-
try {
|
|
361
|
-
span.end();
|
|
362
|
-
}
|
|
363
|
-
catch {
|
|
364
|
-
/* best effort */
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
})();
|
|
563
|
+
finalizeToolSpan(held, errored, error, result);
|
|
368
564
|
}
|
|
369
565
|
catch (err) {
|
|
370
566
|
logger.warn("after_tool_call handler failed", { err: errMsg(err) });
|
|
371
567
|
}
|
|
372
568
|
});
|
|
373
|
-
// ─── Subagent (`invoke_agent` span
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
569
|
+
// ─── Subagent (`invoke_agent` span) ────────────────────────────────────────
|
|
570
|
+
// 5.x: subagent_spawning opens, subagent_spawned enriches. 10.x has no subagent_spawning, so
|
|
571
|
+
// subagent_spawned opens the span when none is held for the child.
|
|
572
|
+
/** Opens and holds the `invoke_agent` span for `childSessionKey`, hosted by the given run. */
|
|
573
|
+
const openSubagentSpan = (params) => {
|
|
574
|
+
const { childSessionKey, hostSessionKey, hostRunId, ev } = params;
|
|
575
|
+
const prior = subagentSpans.get(childSessionKey);
|
|
576
|
+
if (prior)
|
|
577
|
+
endSpan(prior.span);
|
|
578
|
+
const agentName = nonEmptyString(ev?.label) ?? nonEmptyString(ev?.agentId) ?? "unknown";
|
|
579
|
+
const span = tracer.startSpan(`invoke_agent ${agentName}`, { kind: SpanKind.INTERNAL }, hostSessionKey && hostRunId
|
|
580
|
+
? parentContextForModel(hostSessionKey, hostRunId)
|
|
581
|
+
: otelContext.active());
|
|
582
|
+
// sessionKey/runId carry the HOST run so the agent_end sweep can reach this span.
|
|
583
|
+
const held = {
|
|
584
|
+
span,
|
|
585
|
+
sessionKey: hostSessionKey,
|
|
586
|
+
runId: hostRunId,
|
|
587
|
+
startMs: Date.now(),
|
|
588
|
+
agentName,
|
|
589
|
+
};
|
|
590
|
+
subagentSpans.set(childSessionKey, held);
|
|
591
|
+
setDefinedAttributes(span, {
|
|
592
|
+
[GEN_AI.OPERATION_NAME]: "invoke_agent",
|
|
593
|
+
[GEN_AI.CONVERSATION_ID]: nonEmptyString(hostSessionKey),
|
|
594
|
+
[GEN_AI.AGENT_NAME]: agentName,
|
|
595
|
+
"senpi.subagent.mode": nonEmptyString(ev?.mode),
|
|
596
|
+
"senpi.subagent.requester": nonEmptyString(ev?.requester),
|
|
597
|
+
"senpi.subagent.thread_requested": typeof ev?.threadRequested === "boolean" ? ev.threadRequested : undefined,
|
|
598
|
+
...identityAttributes(params.identity),
|
|
599
|
+
});
|
|
600
|
+
return held;
|
|
601
|
+
};
|
|
602
|
+
if (!modernHost) {
|
|
603
|
+
on("subagent_spawning", (event, subagentCtx) => {
|
|
604
|
+
try {
|
|
605
|
+
const ev = asRecord(event);
|
|
606
|
+
const ctxObj = asRecord(subagentCtx);
|
|
607
|
+
const hostSessionKey = nonEmptyString(ctxObj?.requesterSessionKey) ??
|
|
608
|
+
nonEmptyString(ctxObj?.sessionKey) ??
|
|
609
|
+
"";
|
|
610
|
+
const hostRunId = nonEmptyString(ctxObj?.runId) ?? nonEmptyString(ctxObj?.requesterRunId) ?? "";
|
|
611
|
+
const childSessionKey = nonEmptyString(ev?.childSessionKey) ?? nonEmptyString(ctxObj?.childSessionKey);
|
|
612
|
+
if (!childSessionKey) {
|
|
613
|
+
logger.warn("subagent_spawning missing childSessionKey; skipping span");
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
openSubagentSpan({ childSessionKey, hostSessionKey, hostRunId, ev, identity: readIdentity(subagentCtx) });
|
|
387
617
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
: otelContext.active());
|
|
395
|
-
// sessionKey/runId carry the HOST run so the agent_end sweep can reach this span.
|
|
396
|
-
subagentSpans.set(childSessionKey, {
|
|
397
|
-
span,
|
|
398
|
-
sessionKey: hostSessionKey,
|
|
399
|
-
runId: hostRunId,
|
|
400
|
-
startMs: Date.now(),
|
|
401
|
-
agentName,
|
|
402
|
-
});
|
|
403
|
-
setDefinedAttributes(span, {
|
|
404
|
-
[GEN_AI.OPERATION_NAME]: "invoke_agent",
|
|
405
|
-
[GEN_AI.CONVERSATION_ID]: nonEmptyString(hostSessionKey),
|
|
406
|
-
[GEN_AI.AGENT_NAME]: agentName,
|
|
407
|
-
"senpi.subagent.mode": nonEmptyString(ev?.mode),
|
|
408
|
-
"senpi.subagent.requester": nonEmptyString(ev?.requester),
|
|
409
|
-
"senpi.subagent.thread_requested": typeof ev?.threadRequested === "boolean" ? ev.threadRequested : undefined,
|
|
410
|
-
...identityAttributes(identity),
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
catch (err) {
|
|
414
|
-
logger.warn("subagent_spawning handler failed", { err: errMsg(err) });
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
api.on("subagent_spawned", (event, subagentCtx) => {
|
|
618
|
+
catch (err) {
|
|
619
|
+
logger.warn("subagent_spawning handler failed", { err: errMsg(err) });
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
on("subagent_spawned", (event, subagentCtx) => {
|
|
418
624
|
try {
|
|
419
625
|
const ev = asRecord(event);
|
|
420
626
|
const ctxObj = asRecord(subagentCtx);
|
|
421
627
|
const childSessionKey = nonEmptyString(ev?.childSessionKey) ?? nonEmptyString(ctxObj?.childSessionKey);
|
|
422
628
|
if (!childSessionKey)
|
|
423
629
|
return;
|
|
424
|
-
|
|
630
|
+
let held = subagentSpans.get(childSessionKey);
|
|
631
|
+
if (!held && modernHost) {
|
|
632
|
+
// 10.x ctx: `runId` is the CHILD run; the host is the requester session, whose run is
|
|
633
|
+
// `requesterRunId` when given, else that session's latest run seen at llm_input.
|
|
634
|
+
const hostSessionKey = nonEmptyString(ctxObj?.requesterSessionKey) ?? nonEmptyString(ctxObj?.sessionKey) ?? "";
|
|
635
|
+
const hostRunId = nonEmptyString(ctxObj?.requesterRunId) ??
|
|
636
|
+
(hostSessionKey ? latestRunBySession.get(hostSessionKey) : undefined) ??
|
|
637
|
+
"";
|
|
638
|
+
held = openSubagentSpan({ childSessionKey, hostSessionKey, hostRunId, ev, identity: readIdentity(subagentCtx) });
|
|
639
|
+
}
|
|
425
640
|
if (!held)
|
|
426
641
|
return;
|
|
427
642
|
const span = held.span;
|
|
@@ -435,7 +650,7 @@ export function registerSpanHooks(options) {
|
|
|
435
650
|
logger.warn("subagent_spawned handler failed", { err: errMsg(err) });
|
|
436
651
|
}
|
|
437
652
|
});
|
|
438
|
-
|
|
653
|
+
on("subagent_delivery_target", (event, subagentCtx) => {
|
|
439
654
|
try {
|
|
440
655
|
const ev = asRecord(event);
|
|
441
656
|
const ctxObj = asRecord(subagentCtx);
|
|
@@ -459,7 +674,7 @@ export function registerSpanHooks(options) {
|
|
|
459
674
|
logger.warn("subagent_delivery_target handler failed", { err: errMsg(err) });
|
|
460
675
|
}
|
|
461
676
|
});
|
|
462
|
-
|
|
677
|
+
on("subagent_ended", (event, subagentCtx) => {
|
|
463
678
|
try {
|
|
464
679
|
const ev = asRecord(event);
|
|
465
680
|
const ctxObj = asRecord(subagentCtx);
|
|
@@ -500,7 +715,7 @@ export function registerSpanHooks(options) {
|
|
|
500
715
|
}
|
|
501
716
|
});
|
|
502
717
|
// ─── Agent end (orphan sweep) ──────────────────────────────────────────────
|
|
503
|
-
|
|
718
|
+
on("agent_end", (event, agentCtx) => {
|
|
504
719
|
try {
|
|
505
720
|
const sessionKey = readSessionKey(agentCtx, event);
|
|
506
721
|
const runId = readRunId(agentCtx, event);
|
|
@@ -533,6 +748,7 @@ export function registerSpanHooks(options) {
|
|
|
533
748
|
model.endTimer = setTimeout(() => {
|
|
534
749
|
if (modelSpans.get(llmKey) === model)
|
|
535
750
|
modelSpans.delete(llmKey);
|
|
751
|
+
stampLlmCalls(model);
|
|
536
752
|
endSpan(model.span);
|
|
537
753
|
}, MODEL_SPAN_END_GRACE_MS);
|
|
538
754
|
model.endTimer.unref?.();
|
|
@@ -559,11 +775,14 @@ export function registerSpanHooks(options) {
|
|
|
559
775
|
if (lastActive && lastActive.sessionKey === sessionKey && lastActive.runId === runId) {
|
|
560
776
|
lastActive = null;
|
|
561
777
|
}
|
|
778
|
+
if (latestRunBySession.get(sessionKey) === runId)
|
|
779
|
+
latestRunBySession.delete(sessionKey);
|
|
562
780
|
}
|
|
563
781
|
catch (err) {
|
|
564
782
|
logger.warn("agent_end handler failed", { err: errMsg(err) });
|
|
565
783
|
}
|
|
566
784
|
});
|
|
785
|
+
return hookCount;
|
|
567
786
|
}
|
|
568
787
|
// ─── Span key helpers ───────────────────────────────────────────────────────
|
|
569
788
|
const SEP = " ";
|