pi-cloudflare-agent-tracing 0.1.0 → 0.3.0
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/README.md +92 -18
- package/package.json +47 -28
- package/runtime/README.md +18 -11
- package/runtime/cloudflare-telemetry-context.d.ts +58 -0
- package/runtime/cloudflare-telemetry-context.js +139 -0
- package/runtime/cloudflare-telemetry-context.test.mjs +5 -2
- package/skills/cloudflare-agent-tracing/SKILL.md +27 -10
- package/skills/cloudflare-agent-tracing/references/report.example.json +103 -0
- package/skills/cloudflare-agent-tracing/scripts/project-profile.mjs +58 -7
- package/skills/cloudflare-agent-tracing/scripts/project-profile.test.mjs +27 -0
- package/skills/cloudflare-agent-tracing/scripts/render-report.mjs +260 -0
- package/skills/cloudflare-agent-tracing/scripts/render-report.test.mjs +66 -0
- package/runtime/cloudflare-telemetry-context.ts +0 -225
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Pi Cloudflare Agent Tracing
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A Pi package and required Worker runtime adapter for content-safe tracing of Pi Durable applications running on Cloudflare.
|
|
4
4
|
|
|
5
5
|
It adds:
|
|
6
6
|
|
|
@@ -23,13 +23,14 @@ The package registers Cloudflare's official MCP server, applies an evidence-firs
|
|
|
23
23
|
- Queries deployments, versions, bindings, logs, traces, and bounded metrics.
|
|
24
24
|
- Evaluates deterministic fulfillment from normalized, content-free evidence.
|
|
25
25
|
- Summarizes Pi Durable-shaped context exports without transcript or tool content.
|
|
26
|
-
-
|
|
26
|
+
- Emits `pi.durable.*` and `pi.ai.*` spans through Cloudflare Workers custom spans.
|
|
27
|
+
- Requires the runtime adapter on every production `Harness.open()` before investigations run.
|
|
27
28
|
|
|
28
29
|
It does **not** deploy, repair state, mutate Cloudflare resources, or create diagnostic endpoints.
|
|
29
30
|
|
|
30
31
|
## Install
|
|
31
32
|
|
|
32
|
-
### Install
|
|
33
|
+
### 1. Install the Pi investigation package
|
|
33
34
|
|
|
34
35
|
```bash
|
|
35
36
|
pi install npm:pi-cloudflare-agent-tracing
|
|
@@ -37,6 +38,44 @@ pi install npm:pi-cloudflare-agent-tracing
|
|
|
37
38
|
|
|
38
39
|
The `pi-package` keyword makes the published package eligible for discovery in the [Pi package gallery](https://pi.dev/packages).
|
|
39
40
|
|
|
41
|
+
### 2. Install the required Worker runtime dependency
|
|
42
|
+
|
|
43
|
+
From every Pi Durable Worker project that will be investigated:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pnpm add pi-cloudflare-agent-tracing
|
|
47
|
+
# or: npm install pi-cloudflare-agent-tracing
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Enable Workers observability and tracing:
|
|
51
|
+
|
|
52
|
+
```jsonc
|
|
53
|
+
{
|
|
54
|
+
"observability": {
|
|
55
|
+
"enabled": true,
|
|
56
|
+
"traces": { "enabled": true },
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Wire the package into every production `Harness.open()`:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { tracing } from "cloudflare:workers";
|
|
65
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
66
|
+
|
|
67
|
+
const harness = await Harness.open(
|
|
68
|
+
storage,
|
|
69
|
+
{
|
|
70
|
+
...harnessOptions,
|
|
71
|
+
telemetry: createCloudflareTelemetryContext(tracing),
|
|
72
|
+
},
|
|
73
|
+
context,
|
|
74
|
+
);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The profiler requires all three conditions—the dependency, Workers tracing, and `Harness.open()` wiring. Investigation commands stop with setup guidance when any condition is missing because platform traces alone do not provide Pi execution telemetry.
|
|
78
|
+
|
|
40
79
|
### Try from a local checkout
|
|
41
80
|
|
|
42
81
|
```bash
|
|
@@ -62,7 +101,7 @@ pi install /absolute/path/to/pi-cloudflare-agent-tracing
|
|
|
62
101
|
The same package can be installed from Git:
|
|
63
102
|
|
|
64
103
|
```bash
|
|
65
|
-
pi install git:github.com/oskarcode/pi-cloudflare-agent-tracing@v0.
|
|
104
|
+
pi install git:github.com/oskarcode/pi-cloudflare-agent-tracing@v0.3.0
|
|
66
105
|
```
|
|
67
106
|
|
|
68
107
|
Use a pinned release tag or commit for reproducible investigations.
|
|
@@ -92,12 +131,24 @@ Then run, for example:
|
|
|
92
131
|
/agent-incident my-worker last 15m
|
|
93
132
|
```
|
|
94
133
|
|
|
134
|
+
Add `--html` to save a detailed human-review bundle:
|
|
135
|
+
|
|
136
|
+
```text
|
|
137
|
+
/agent-health my-worker last 6h --html
|
|
138
|
+
/agent-trace ray-id --html
|
|
139
|
+
/agent-cost my-worker last 24h --html
|
|
140
|
+
/agent-eval submission 42 --html
|
|
141
|
+
/agent-incident my-worker last 30m --html
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each saved report produces matching `.md`, content-free `.json`, and standalone `.html` files under the target project's `.pi/agent-tracing-reports/`. The HTML is printable, responsive, self-contained, uses no remote assets, and renders the same bounded evidence as the Markdown and JSON versions.
|
|
145
|
+
|
|
95
146
|
## How it works
|
|
96
147
|
|
|
97
148
|
```text
|
|
98
149
|
slash command
|
|
99
150
|
→ extension dispatches /skill:cloudflare-agent-tracing
|
|
100
|
-
→ skill
|
|
151
|
+
→ skill verifies required Pi runtime instrumentation
|
|
101
152
|
→ Cloudflare MCP performs bounded GET queries
|
|
102
153
|
→ optional authorized diagnostics provide durable evidence
|
|
103
154
|
→ evaluator produces a content-free verdict
|
|
@@ -108,7 +159,7 @@ The extension and skill have different responsibilities:
|
|
|
108
159
|
- `extensions/cloudflare-agent-tracing.ts` owns executable Pi wiring, command registration, MCP registration, queue behavior, and the read-only gate.
|
|
109
160
|
- `skills/cloudflare-agent-tracing/SKILL.md` owns investigation policy, evidence order, privacy rules, and report structure.
|
|
110
161
|
- `skills/cloudflare-agent-tracing/scripts/` contains dependency-free local evidence utilities.
|
|
111
|
-
- `runtime/` contains
|
|
162
|
+
- `runtime/` contains the required Worker adapter that forwards Pi telemetry to Cloudflare custom spans.
|
|
112
163
|
|
|
113
164
|
## Project discovery
|
|
114
165
|
|
|
@@ -126,9 +177,9 @@ node skills/cloudflare-agent-tracing/scripts/project-profile.mjs \
|
|
|
126
177
|
--config apps/agent/wrangler.jsonc
|
|
127
178
|
```
|
|
128
179
|
|
|
129
|
-
Source discovery is bounded and excludes build output, dependencies, and hidden directories. It searches common TypeScript and JavaScript files for `Harness.open()` and reports whether
|
|
180
|
+
Source discovery is bounded and excludes build output, dependencies, and hidden directories. It searches common TypeScript and JavaScript files for every production `Harness.open()` and reports whether each call passes `telemetry:`. `instrumentation.ready` is true only when the package dependency and Wrangler tracing settings are also present.
|
|
130
181
|
|
|
131
|
-
The profiler is heuristic evidence
|
|
182
|
+
The profiler is heuristic setup evidence. A sampled production span remains the runtime proof.
|
|
132
183
|
|
|
133
184
|
## Fulfillment evaluation
|
|
134
185
|
|
|
@@ -146,6 +197,30 @@ node skills/cloudflare-agent-tracing/scripts/evaluate.mjs /tmp/evidence.json
|
|
|
146
197
|
|
|
147
198
|
Deterministic evidence checks submission settlement, final answer presence, required tools, required durable effects, constraints, and terminal failures. Semantic evidence is optional; without authorized semantic evidence, a deterministic pass can remain `indeterminate`.
|
|
148
199
|
|
|
200
|
+
## Human-review HTML reports
|
|
201
|
+
|
|
202
|
+
All five investigation modes use one normalized report schema. See:
|
|
203
|
+
|
|
204
|
+
```text
|
|
205
|
+
skills/cloudflare-agent-tracing/references/report.example.json
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Render a normalized report directly:
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
node skills/cloudflare-agent-tracing/scripts/render-report.mjs \
|
|
212
|
+
path/to/report.json \
|
|
213
|
+
--out path/to/report.html
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
If `--out` is omitted, the renderer writes beside the JSON input with an `.html` extension. The renderer validates the schema version and mode, caps CLI input at 2 MB, escapes every displayed value, and emits a restrictive content security policy. Unknown JSON fields are not rendered, reducing the risk of accidentally exposing unrelated diagnostic data.
|
|
217
|
+
|
|
218
|
+
Saved reports should remain content-free and should normally be ignored by Git:
|
|
219
|
+
|
|
220
|
+
```gitignore
|
|
221
|
+
.pi/agent-tracing-reports/
|
|
222
|
+
```
|
|
223
|
+
|
|
149
224
|
## Optional application diagnostics
|
|
150
225
|
|
|
151
226
|
The package intentionally has no project-specific transcript reader. A target application may expose an existing narrow, authorized diagnostic export. Keep raw output in `/tmp`, summarize it locally, and remove it.
|
|
@@ -159,19 +234,15 @@ node skills/cloudflare-agent-tracing/scripts/summarize-session-context.mjs \
|
|
|
159
234
|
|
|
160
235
|
The summary retains IDs, roles, tool names/statuses, stop reasons, token counts, and reported cost. It removes text, thinking, tool arguments, and tool results.
|
|
161
236
|
|
|
162
|
-
##
|
|
237
|
+
## Required Pi telemetry adapter
|
|
163
238
|
|
|
164
|
-
`
|
|
239
|
+
`pi-cloudflare-agent-tracing/cloudflare` structurally implements Pi's telemetry context over Cloudflare Workers custom spans without importing project packages. It must be passed to every production `Harness.open()`.
|
|
165
240
|
|
|
166
|
-
|
|
241
|
+
Pi Durable emits content-free admission, phase, task, tool, recovery, and commit evidence. Pi AI emits model, timing, usage, and cost evidence when the provider supplies it. The adapter redacts error messages by default. Workers custom spans do not expose native event and link APIs, so bounded events and links are encoded as attributes.
|
|
167
242
|
|
|
168
|
-
|
|
169
|
-
2. Cover HTTP, RPC, WebSocket, alarms, retries, and resumed tasks.
|
|
170
|
-
3. Keep error-message recording disabled unless content safety is established.
|
|
171
|
-
4. Pass the adapter as `telemetry` to `Harness.open()`.
|
|
172
|
-
5. Verify expected `pi.durable.*` and `pi.ai.*` spans in a bounded production smoke.
|
|
243
|
+
The package never persists a telemetry context in Durable Object state. Cloudflare's `tracing.enterSpan()` uses the active JavaScript async context for request-time nesting; resumed Durable Object work can begin a new trace when no caller span exists.
|
|
173
244
|
|
|
174
|
-
|
|
245
|
+
After integration, verify the adapter under workerd and then prove expected `pi.durable.*` and `pi.ai.*` spans with one bounded, authorized production smoke before relying on the reports.
|
|
175
246
|
|
|
176
247
|
## Security model
|
|
177
248
|
|
|
@@ -198,8 +269,11 @@ The suite uses Node's built-in test runner and covers extension registration, re
|
|
|
198
269
|
extensions/cloudflare-agent-tracing.ts
|
|
199
270
|
skills/cloudflare-agent-tracing/SKILL.md
|
|
200
271
|
skills/cloudflare-agent-tracing/scripts/
|
|
272
|
+
render-report.mjs
|
|
201
273
|
skills/cloudflare-agent-tracing/references/
|
|
202
|
-
|
|
274
|
+
report.example.json
|
|
275
|
+
runtime/cloudflare-telemetry-context.js
|
|
276
|
+
runtime/cloudflare-telemetry-context.d.ts
|
|
203
277
|
test/extension.test.mjs
|
|
204
278
|
```
|
|
205
279
|
|
package/package.json
CHANGED
|
@@ -1,29 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-cloudflare-agent-tracing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Required Cloudflare custom-span runtime and read-only tracing reports for Pi Durable applications.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"cloudflare",
|
|
8
|
+
"observability",
|
|
9
|
+
"pi-durable",
|
|
10
|
+
"pi-extension",
|
|
11
|
+
"pi-package"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/oskarcode/pi-cloudflare-agent-tracing#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/oskarcode/pi-cloudflare-agent-tracing/issues"
|
|
16
|
+
},
|
|
6
17
|
"license": "MIT",
|
|
7
18
|
"author": "Oskar Ablimit",
|
|
8
19
|
"repository": {
|
|
9
20
|
"type": "git",
|
|
10
21
|
"url": "git+https://github.com/oskarcode/pi-cloudflare-agent-tracing.git"
|
|
11
22
|
},
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"
|
|
23
|
+
"files": [
|
|
24
|
+
"extensions",
|
|
25
|
+
"skills",
|
|
26
|
+
"runtime",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
"./cloudflare": {
|
|
33
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
34
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
35
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
36
|
+
},
|
|
37
|
+
"./runtime/cloudflare-telemetry-context.js": {
|
|
38
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
39
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
40
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
41
|
+
},
|
|
42
|
+
"./runtime/cloudflare-telemetry-context.ts": {
|
|
43
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
44
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
45
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
15
48
|
},
|
|
16
49
|
"publishConfig": {
|
|
17
50
|
"access": "public"
|
|
18
51
|
},
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"cloudflare"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
52
|
+
"scripts": {
|
|
53
|
+
"test": "node --test test/*.test.mjs runtime/*.test.mjs skills/cloudflare-agent-tracing/scripts/*.test.mjs",
|
|
54
|
+
"profile": "node skills/cloudflare-agent-tracing/scripts/project-profile.mjs",
|
|
55
|
+
"render-report": "node skills/cloudflare-agent-tracing/scripts/render-report.mjs"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22"
|
|
59
|
+
},
|
|
27
60
|
"pi": {
|
|
28
61
|
"extensions": [
|
|
29
62
|
"./extensions/cloudflare-agent-tracing.ts"
|
|
@@ -31,19 +64,5 @@
|
|
|
31
64
|
"skills": [
|
|
32
65
|
"./skills"
|
|
33
66
|
]
|
|
34
|
-
},
|
|
35
|
-
"scripts": {
|
|
36
|
-
"test": "node --test test/*.test.mjs runtime/*.test.mjs skills/cloudflare-agent-tracing/scripts/*.test.mjs",
|
|
37
|
-
"profile": "node skills/cloudflare-agent-tracing/scripts/project-profile.mjs"
|
|
38
|
-
},
|
|
39
|
-
"files": [
|
|
40
|
-
"extensions",
|
|
41
|
-
"skills",
|
|
42
|
-
"runtime",
|
|
43
|
-
"README.md",
|
|
44
|
-
"LICENSE"
|
|
45
|
-
],
|
|
46
|
-
"engines": {
|
|
47
|
-
"node": ">=22"
|
|
48
67
|
}
|
|
49
68
|
}
|
package/runtime/README.md
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Required Worker telemetry
|
|
2
2
|
|
|
3
|
-
`cloudflare-
|
|
3
|
+
The `pi-cloudflare-agent-tracing/cloudflare` export adapts Pi's vendor-neutral telemetry contract to Cloudflare Workers custom spans. Install this package in the Worker application as well as in Pi:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add pi-cloudflare-agent-tracing
|
|
7
|
+
pi install npm:pi-cloudflare-agent-tracing
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Enable `observability.enabled` and `observability.traces.enabled` in Wrangler, then wire every production Harness:
|
|
6
11
|
|
|
7
12
|
```ts
|
|
8
13
|
import { tracing } from "cloudflare:workers";
|
|
9
|
-
import { createCloudflareTelemetryContext } from "
|
|
10
|
-
|
|
11
|
-
const telemetry = createCloudflareTelemetryContext(tracing);
|
|
14
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
12
15
|
|
|
13
16
|
const harness = await Harness.open(
|
|
14
17
|
storage,
|
|
15
|
-
{
|
|
18
|
+
{
|
|
19
|
+
...harnessOptions,
|
|
20
|
+
telemetry: createCloudflareTelemetryContext(tracing),
|
|
21
|
+
},
|
|
16
22
|
context,
|
|
17
23
|
);
|
|
18
24
|
```
|
|
19
25
|
|
|
20
|
-
Before deployment, prove under workerd that the selected Cloudflare tracing object remains valid for every invocation type that can use the Harness: HTTP, RPC, WebSockets, alarms, retries, and resumed tasks. A long-lived Durable Object must not accidentally retain an invocation-owned context beyond its valid lifetime.
|
|
21
|
-
|
|
22
26
|
The adapter defaults to content-safe behavior:
|
|
23
27
|
|
|
24
28
|
- error names are recorded;
|
|
25
29
|
- error messages are omitted;
|
|
26
30
|
- events and links are bounded;
|
|
27
|
-
- arrays are encoded as JSON attributes because Workers custom spans do not expose native event/link APIs
|
|
31
|
+
- arrays are encoded as JSON attributes because Workers custom spans do not expose native event/link APIs;
|
|
32
|
+
- telemetry contexts are process objects and are never durable application state.
|
|
33
|
+
|
|
34
|
+
Cloudflare's `tracing.enterSpan()` automatically nests spans using the active JavaScript async context and ends them when the callback's promise settles. A Pi task resumed by an alarm or another invocation may correctly start a new root trace when no caller span is active.
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
The package profiler treats the runtime dependency and `Harness.open()` wiring as required setup. Still validate the selected compatibility date under workerd and prove sampled `pi.durable.*` and `pi.ai.*` spans in the deployed Worker before making production conclusions.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type AttributeValue =
|
|
2
|
+
| string
|
|
3
|
+
| number
|
|
4
|
+
| boolean
|
|
5
|
+
| readonly string[]
|
|
6
|
+
| readonly number[]
|
|
7
|
+
| readonly boolean[];
|
|
8
|
+
|
|
9
|
+
export interface SpanAttributes {
|
|
10
|
+
[name: string]: AttributeValue | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type TelemetryStatus =
|
|
14
|
+
| { status: "ok" }
|
|
15
|
+
| { status: "error"; error?: { name: string; message: string } };
|
|
16
|
+
|
|
17
|
+
export interface TelemetryLink {
|
|
18
|
+
context: TelemetryContext;
|
|
19
|
+
attributes?: SpanAttributes;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TelemetryContext {
|
|
23
|
+
startSpan<T>(
|
|
24
|
+
options: { name: string; attributes?: SpanAttributes; links?: readonly TelemetryLink[] },
|
|
25
|
+
callback: (span: TelemetrySpan) => T | Promise<T>,
|
|
26
|
+
): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TelemetrySpan extends TelemetryContext {
|
|
30
|
+
addLink(link: TelemetryLink): void;
|
|
31
|
+
addEvent(name: string, attributes?: SpanAttributes): void;
|
|
32
|
+
setAttributes(attributes: SpanAttributes): void;
|
|
33
|
+
setStatus(status: TelemetryStatus): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CloudflareSpan {
|
|
37
|
+
setAttribute(key: string, value: string | number | boolean): this;
|
|
38
|
+
setAttributes(attributes: Record<string, string | number | boolean | undefined>): this;
|
|
39
|
+
setStatus(status: { code: "unset" | "ok" | "error"; message?: string }): this;
|
|
40
|
+
spanContext(): { traceId: string; spanId: string; traceFlags: number };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CloudflareTracing {
|
|
44
|
+
enterSpan<T>(name: string, callback: (span: CloudflareSpan) => T): T;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CloudflareTelemetryOptions {
|
|
48
|
+
/** Error messages can contain user/provider content and are redacted by default. */
|
|
49
|
+
includeErrorMessages?: boolean;
|
|
50
|
+
/** Events are encoded as bounded attributes because Workers custom spans have no event API. */
|
|
51
|
+
maxEventsPerSpan?: number;
|
|
52
|
+
maxLinksPerSpan?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export declare function createCloudflareTelemetryContext(
|
|
56
|
+
tracing: CloudflareTracing,
|
|
57
|
+
options?: CloudflareTelemetryOptions,
|
|
58
|
+
): TelemetryContext;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const DEFAULT_MAX_EVENTS = 32;
|
|
2
|
+
const DEFAULT_MAX_LINKS = 16;
|
|
3
|
+
const MAX_ERROR_MESSAGE = 500;
|
|
4
|
+
|
|
5
|
+
export function createCloudflareTelemetryContext(tracing, options = {}) {
|
|
6
|
+
return new CloudflareTelemetryContext(tracing, {
|
|
7
|
+
includeErrorMessages: options.includeErrorMessages ?? false,
|
|
8
|
+
maxEventsPerSpan: options.maxEventsPerSpan ?? DEFAULT_MAX_EVENTS,
|
|
9
|
+
maxLinksPerSpan: options.maxLinksPerSpan ?? DEFAULT_MAX_LINKS,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class CloudflareTelemetryContext {
|
|
14
|
+
constructor(tracing, options) {
|
|
15
|
+
this.tracing = tracing;
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async startSpan(options, callback) {
|
|
20
|
+
return await this.tracing.enterSpan(options.name, (nativeSpan) => {
|
|
21
|
+
const span = new CloudflareTelemetrySpan(this.tracing, nativeSpan, this.options);
|
|
22
|
+
if (options.attributes) span.setAttributes(options.attributes);
|
|
23
|
+
for (const link of options.links ?? []) span.addLink(link);
|
|
24
|
+
|
|
25
|
+
let result;
|
|
26
|
+
try {
|
|
27
|
+
result = callback(span);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
span.finishError(error);
|
|
30
|
+
return Promise.reject(error);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return Promise.resolve(result).then(
|
|
34
|
+
(value) => {
|
|
35
|
+
span.finishSuccess();
|
|
36
|
+
return value;
|
|
37
|
+
},
|
|
38
|
+
(error) => {
|
|
39
|
+
span.finishError(error);
|
|
40
|
+
throw error;
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class CloudflareTelemetrySpan {
|
|
48
|
+
settled = false;
|
|
49
|
+
explicitStatus = false;
|
|
50
|
+
eventCount = 0;
|
|
51
|
+
linkCount = 0;
|
|
52
|
+
|
|
53
|
+
constructor(tracing, nativeSpan, options) {
|
|
54
|
+
this.tracing = tracing;
|
|
55
|
+
this.nativeSpan = nativeSpan;
|
|
56
|
+
this.options = options;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
startSpan(options, callback) {
|
|
60
|
+
return new CloudflareTelemetryContext(this.tracing, this.options).startSpan(options, callback);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
addLink(link) {
|
|
64
|
+
if (this.settled || this.linkCount >= this.options.maxLinksPerSpan) return;
|
|
65
|
+
if (!(link.context instanceof CloudflareTelemetrySpan)) return;
|
|
66
|
+
|
|
67
|
+
const index = this.linkCount++;
|
|
68
|
+
const context = link.context.nativeSpan.spanContext();
|
|
69
|
+
this.nativeSpan.setAttributes({
|
|
70
|
+
[`link.${index}.trace_id`]: context.traceId,
|
|
71
|
+
[`link.${index}.span_id`]: context.spanId,
|
|
72
|
+
...prefixAttributes(`link.${index}.attribute.`, link.attributes),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
addEvent(name, attributes) {
|
|
77
|
+
if (this.settled || this.eventCount >= this.options.maxEventsPerSpan) return;
|
|
78
|
+
const index = this.eventCount++;
|
|
79
|
+
this.nativeSpan.setAttributes({
|
|
80
|
+
[`event.${index}.name`]: name,
|
|
81
|
+
...prefixAttributes(`event.${index}.attribute.`, attributes),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
setAttributes(attributes) {
|
|
86
|
+
if (this.settled) return;
|
|
87
|
+
this.nativeSpan.setAttributes(normalizeAttributes(attributes));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
setStatus(status) {
|
|
91
|
+
if (this.settled) return;
|
|
92
|
+
this.explicitStatus = true;
|
|
93
|
+
if (status.status === "ok") {
|
|
94
|
+
this.nativeSpan.setStatus({ code: "ok" });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const message = this.options.includeErrorMessages
|
|
99
|
+
? status.error?.message.slice(0, MAX_ERROR_MESSAGE)
|
|
100
|
+
: undefined;
|
|
101
|
+
this.nativeSpan.setAttributes({ "error.name": status.error?.name });
|
|
102
|
+
this.nativeSpan.setStatus({ code: "error", ...(message ? { message } : {}) });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
finishSuccess() {
|
|
106
|
+
if (this.settled) return;
|
|
107
|
+
if (!this.explicitStatus) this.nativeSpan.setStatus({ code: "ok" });
|
|
108
|
+
this.settled = true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
finishError(error) {
|
|
112
|
+
if (this.settled) return;
|
|
113
|
+
const name = error instanceof Error ? error.name : "UnknownError";
|
|
114
|
+
const message =
|
|
115
|
+
this.options.includeErrorMessages && error instanceof Error
|
|
116
|
+
? error.message.slice(0, MAX_ERROR_MESSAGE)
|
|
117
|
+
: undefined;
|
|
118
|
+
this.nativeSpan.setAttributes({ "error.name": name });
|
|
119
|
+
this.nativeSpan.setStatus({ code: "error", ...(message ? { message } : {}) });
|
|
120
|
+
this.settled = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function normalizeAttributes(attributes) {
|
|
125
|
+
const normalized = {};
|
|
126
|
+
for (const [name, value] of Object.entries(attributes ?? {})) {
|
|
127
|
+
normalized[name] =
|
|
128
|
+
value !== undefined && typeof value === "object" ? JSON.stringify(value) : value;
|
|
129
|
+
}
|
|
130
|
+
return normalized;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function prefixAttributes(prefix, attributes) {
|
|
134
|
+
const prefixed = {};
|
|
135
|
+
for (const [name, value] of Object.entries(normalizeAttributes(attributes))) {
|
|
136
|
+
prefixed[`${prefix}${name}`] = value;
|
|
137
|
+
}
|
|
138
|
+
return prefixed;
|
|
139
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import { createCloudflareTelemetryContext } from "
|
|
3
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
4
4
|
|
|
5
5
|
class NativeSpan {
|
|
6
6
|
attributes = {};
|
|
@@ -79,7 +79,10 @@ test("encodes links using trace and span identifiers", async () => {
|
|
|
79
79
|
await telemetry.startSpan({ name: "parent" }, async (span) => {
|
|
80
80
|
parent = span;
|
|
81
81
|
});
|
|
82
|
-
await telemetry.startSpan(
|
|
82
|
+
await telemetry.startSpan(
|
|
83
|
+
{ name: "child", links: [{ context: parent, attributes: { reason: "queued" } }] },
|
|
84
|
+
async () => {},
|
|
85
|
+
);
|
|
83
86
|
|
|
84
87
|
assert.deepEqual(tracing.spans[1].attributes, {
|
|
85
88
|
"link.0.trace_id": "trace-1",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: cloudflare-agent-tracing
|
|
3
|
-
description: Investigate Pi Durable
|
|
4
|
-
compatibility: Requires Node.js 22+,
|
|
3
|
+
description: Investigate instrumented Pi Durable applications on Cloudflare using Workers observability, Pi telemetry, Durable Object evidence, model usage, and bounded fulfillment evaluation. Use for traces, health, incidents, latency, model cost, errors, recovery, or whether an agent fulfilled a request.
|
|
4
|
+
compatibility: Requires Node.js 22+, the pi-cloudflare-agent-tracing runtime dependency wired into every production Harness.open(), Workers tracing enabled, and Cloudflare available through the official MCP.
|
|
5
5
|
metadata:
|
|
6
6
|
author: oskarcode
|
|
7
|
-
version: "0.
|
|
7
|
+
version: "0.3.0"
|
|
8
8
|
---
|
|
9
9
|
|
|
10
10
|
# Cloudflare agent tracing
|
|
@@ -15,17 +15,18 @@ Resolve relative paths in this skill against the directory containing this `SKIL
|
|
|
15
15
|
|
|
16
16
|
## Inputs
|
|
17
17
|
|
|
18
|
-
Interpret the first argument as one of `trace`, `health`, `cost`, `evaluate`, or `incident`. The rest identifies a Worker, request/Ray/trace/conversation/submission ID, deployment, or bounded time range. If the target is ambiguous, profile the local project before asking one focused question.
|
|
18
|
+
Interpret the first argument as one of `trace`, `health`, `cost`, `evaluate`, or `incident`. The rest identifies a Worker, request/Ray/trace/conversation/submission ID, deployment, or bounded time range. `--html` requests a saved human-review bundle and is not part of the target. If the target is ambiguous, profile the local project before asking one focused question.
|
|
19
19
|
|
|
20
20
|
## Start locally
|
|
21
21
|
|
|
22
22
|
1. Run `node <skill-directory>/scripts/project-profile.mjs --cwd <project-directory>`.
|
|
23
23
|
2. If the profiler reports multiple Wrangler configurations, select the relevant one from the user's target or ask one focused question. Re-run with `--config <path>` when needed.
|
|
24
|
-
3.
|
|
25
|
-
4.
|
|
26
|
-
5.
|
|
27
|
-
6.
|
|
28
|
-
7.
|
|
24
|
+
3. Require `instrumentation.ready: true`. This means the application depends on `pi-cloudflare-agent-tracing`, Workers observability and traces are enabled, and every discovered production `Harness.open()` passes `telemetry`.
|
|
25
|
+
4. If instrumentation is not ready, stop before production analysis. Report the missing requirements and point to the package's required Worker setup. Platform-only traces are not a substitute for Pi telemetry.
|
|
26
|
+
5. Read only the selected Wrangler configuration, relevant composition root, and installed Pi versions as needed.
|
|
27
|
+
6. Use the Cloudflare MCP namespace. Read its namespace instructions before querying unfamiliar tools.
|
|
28
|
+
7. Search the Cloudflare OpenAPI schema before an unfamiliar API call.
|
|
29
|
+
8. Use only Cloudflare GET requests. The extension blocks `cloudflare_execute` unless all statically visible requests are explicit GETs.
|
|
29
30
|
|
|
30
31
|
## Evidence order
|
|
31
32
|
|
|
@@ -124,4 +125,20 @@ Return a concise report with:
|
|
|
124
125
|
|
|
125
126
|
Distinguish observations from hypotheses. Every conclusion must name its evidence. Local source inspection alone is not production verification.
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
Always return the concise report in the conversation. When the command includes `--html`, or the user asks to save the report:
|
|
129
|
+
|
|
130
|
+
1. Create `.pi/agent-tracing-reports/` under the target project.
|
|
131
|
+
2. Use one collision-resistant basename: `<UTC timestamp>-<mode>-<safe target slug>`.
|
|
132
|
+
3. Write the concise Markdown report to `<basename>.md`.
|
|
133
|
+
4. Write normalized, content-free JSON to `<basename>.json` using `references/report.example.json` as the schema example. Use `schemaVersion: 1` and the current mode. Omit unavailable sections rather than inventing values.
|
|
134
|
+
5. Render the standalone HTML report:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
node <skill-directory>/scripts/render-report.mjs \
|
|
138
|
+
<project>/.pi/agent-tracing-reports/<basename>.json \
|
|
139
|
+
--out <project>/.pi/agent-tracing-reports/<basename>.html
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
6. Return all three paths and state that the HTML contains the same bounded evidence, not additional private data.
|
|
143
|
+
|
|
144
|
+
The HTML renderer supports every report mode, is printable, embeds no remote assets, applies a restrictive content security policy, and escapes all report values. Never put prompts, answers, thinking, tool arguments/results, credentials, headers, attachment contents, or raw transcript text in any report format. Recommend ignoring `.pi/agent-tracing-reports/` in Git.
|