pi-cloudflare-agent-tracing 0.2.0 → 0.4.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 +68 -41
- package/extensions/cloudflare-agent-tracing.ts +16 -45
- package/package.json +47 -29
- 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 +35 -33
- package/skills/cloudflare-agent-tracing/references/report.example.json +4 -5
- 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 +143 -48
- package/skills/cloudflare-agent-tracing/scripts/render-report.test.mjs +49 -35
- package/runtime/cloudflare-telemetry-context.ts +0 -225
|
@@ -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.4.0"
|
|
8
8
|
---
|
|
9
9
|
|
|
10
10
|
# Cloudflare agent tracing
|
|
@@ -15,17 +15,26 @@ Resolve relative paths in this skill against the directory containing this `SKIL
|
|
|
15
15
|
|
|
16
16
|
## Inputs
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Treat the arguments as one target plus optional controls. A target can be a Worker, request/Ray/trace/submission/task/conversation ID, deployment, or bounded time range. `--html` requests a saved human-review bundle. `--scope turn|thread|service` overrides automatic scope selection. If the target is ambiguous, profile the local project before asking one focused question.
|
|
19
|
+
|
|
20
|
+
Infer one report scope:
|
|
21
|
+
|
|
22
|
+
- **turn** — one request, trace, submission, or task and its nested Pi work;
|
|
23
|
+
- **thread** — related turns for one conversation;
|
|
24
|
+
- **service** — one Worker or deployment over a bounded time window.
|
|
25
|
+
|
|
26
|
+
Use the narrowest scope supported by exact identifiers. A Worker plus time range is `service`; a conversation is `thread`; request, Ray, trace, submission, and task IDs are `turn`. Never make the user choose separate health, cost, evaluation, or incident modes.
|
|
19
27
|
|
|
20
28
|
## Start locally
|
|
21
29
|
|
|
22
30
|
1. Run `node <skill-directory>/scripts/project-profile.mjs --cwd <project-directory>`.
|
|
23
31
|
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.
|
|
32
|
+
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`.
|
|
33
|
+
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.
|
|
34
|
+
5. Read only the selected Wrangler configuration, relevant composition root, and installed Pi versions as needed.
|
|
35
|
+
6. Use the Cloudflare MCP namespace. Read its namespace instructions before querying unfamiliar tools.
|
|
36
|
+
7. Search the Cloudflare OpenAPI schema before an unfamiliar API call.
|
|
37
|
+
8. Use only Cloudflare GET requests. The extension blocks `cloudflare_execute` unless all statically visible requests are explicit GETs.
|
|
29
38
|
|
|
30
39
|
## Evidence order
|
|
31
40
|
|
|
@@ -46,42 +55,35 @@ Prefer exact joins in this order: trace ID, Ray ID, deployment/version ID, reque
|
|
|
46
55
|
|
|
47
56
|
For Dynamic Workers, inspect dispatcher and dispatched Worker separately and verify propagation. For Durable Objects, inspect RPC/invocation evidence and bounded application diagnostics. Never invent or expose arbitrary remote SQL or transcript endpoints.
|
|
48
57
|
|
|
49
|
-
##
|
|
58
|
+
## Unified trace analysis
|
|
50
59
|
|
|
51
|
-
|
|
60
|
+
Produce one report from the same correlated evidence. Every report includes the sections below when evidence exists and marks unavailable sections explicitly.
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
### Execution path
|
|
54
63
|
|
|
55
|
-
|
|
64
|
+
Reconstruct edge request, Worker, Durable Object/RPC, Pi submission or task, model calls, tools/subrequests, storage/commit, recovery, and streaming. Preserve parent-child relationships; label timestamp-only joins as inferences.
|
|
56
65
|
|
|
57
|
-
|
|
66
|
+
### Reliability and performance
|
|
58
67
|
|
|
59
|
-
|
|
68
|
+
Measure outcomes, exceptions, cancellations, latency, CPU/wall time, deployment boundaries, authorization failures, task faults, retries, recovery, and alarm gaps. Distinguish successful Pi Durable recovery from terminal failure and identify the largest evidenced bottleneck.
|
|
60
69
|
|
|
61
|
-
|
|
70
|
+
### Usage and cost
|
|
62
71
|
|
|
63
|
-
|
|
72
|
+
Use provider- or Pi-reported tokens and `cost.total` when present. Label catalog estimates separately. Never present a precise total from sampled or incomplete telemetry.
|
|
64
73
|
|
|
65
|
-
|
|
74
|
+
### Outcome and fulfillment
|
|
75
|
+
|
|
76
|
+
Tracing cannot establish semantic fulfillment by itself. For an exact turn with normalized evidence, run:
|
|
66
77
|
|
|
67
78
|
```bash
|
|
68
79
|
node <skill-directory>/scripts/evaluate.mjs /tmp/agent-evaluation.json
|
|
69
80
|
```
|
|
70
81
|
|
|
71
|
-
Use `references/evaluation-input.example.json` as the content-free contract. Check
|
|
72
|
-
|
|
73
|
-
- submission settled `done`, not `unanswered`;
|
|
74
|
-
- final committed answer exists;
|
|
75
|
-
- required tools succeeded;
|
|
76
|
-
- required durable side effects exist;
|
|
77
|
-
- explicit constraints were met;
|
|
78
|
-
- no terminal failure invalidated the outcome.
|
|
79
|
-
|
|
80
|
-
Only use semantic evidence when the user authorized content inspection and an existing authorized application path provides it. If semantic evidence is unavailable, return `indeterminate` when appropriate.
|
|
82
|
+
Use `references/evaluation-input.example.json` as the content-free contract. Check settlement, final committed answer, required tools, required durable effects, explicit constraints, and terminal failures. Only use semantic evidence when the user authorized content inspection and an existing authorized application path provides it. Otherwise report `indeterminate` when appropriate.
|
|
81
83
|
|
|
82
|
-
###
|
|
84
|
+
### Anomalies and improvements
|
|
83
85
|
|
|
84
|
-
|
|
86
|
+
Explain errors, retries, recovery, user impact, and prioritized evidence-linked improvements. Do not perform repairs. An ordinary successful trace still includes a short anomaly section stating what was checked.
|
|
85
87
|
|
|
86
88
|
## Optional application evidence
|
|
87
89
|
|
|
@@ -127,9 +129,9 @@ Distinguish observations from hypotheses. Every conclusion must name its evidenc
|
|
|
127
129
|
Always return the concise report in the conversation. When the command includes `--html`, or the user asks to save the report:
|
|
128
130
|
|
|
129
131
|
1. Create `.pi/agent-tracing-reports/` under the target project.
|
|
130
|
-
2. Use one collision-resistant basename: `<UTC timestamp
|
|
132
|
+
2. Use one collision-resistant basename: `<UTC timestamp>-pi-trace-<safe target slug>`.
|
|
131
133
|
3. Write the concise Markdown report to `<basename>.md`.
|
|
132
|
-
4. Write normalized, content-free JSON to `<basename>.json` using `references/report.example.json` as the schema example. Use `schemaVersion:
|
|
134
|
+
4. Write normalized, content-free JSON to `<basename>.json` using `references/report.example.json` as the schema example. Use `schemaVersion: 2`, `kind: "pi-agent-trace"`, and the resolved `scope.level`. Omit unavailable sections rather than inventing values.
|
|
133
135
|
5. Render the standalone HTML report:
|
|
134
136
|
|
|
135
137
|
```bash
|
|
@@ -140,4 +142,4 @@ node <skill-directory>/scripts/render-report.mjs \
|
|
|
140
142
|
|
|
141
143
|
6. Return all three paths and state that the HTML contains the same bounded evidence, not additional private data.
|
|
142
144
|
|
|
143
|
-
The HTML renderer
|
|
145
|
+
The HTML renderer presents the unified trace, 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.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"schemaVersion":
|
|
3
|
-
"
|
|
2
|
+
"schemaVersion": 2,
|
|
3
|
+
"kind": "pi-agent-trace",
|
|
4
4
|
"title": "Checkout agent incident",
|
|
5
5
|
"generatedAt": "2026-12-02T17:30:00Z",
|
|
6
6
|
"verdict": {
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"summary": "Requests recovered after a bounded model-provider failure, but two submissions were cancelled."
|
|
10
10
|
},
|
|
11
11
|
"scope": {
|
|
12
|
+
"level": "service",
|
|
12
13
|
"worker": "checkout-agent",
|
|
13
14
|
"environment": "production",
|
|
14
15
|
"deployment": "version-id",
|
|
@@ -57,9 +58,7 @@
|
|
|
57
58
|
"score": 100,
|
|
58
59
|
"semanticEvidence": "unavailable",
|
|
59
60
|
"summary": "Deterministic checks passed; semantic request content was not inspected.",
|
|
60
|
-
"checks": [
|
|
61
|
-
{ "name": "submission settled", "passed": true, "evidence": "E3" }
|
|
62
|
-
]
|
|
61
|
+
"checks": [{ "name": "submission settled", "passed": true, "evidence": "E3" }]
|
|
63
62
|
},
|
|
64
63
|
"durableState": [
|
|
65
64
|
{
|
|
@@ -35,11 +35,16 @@ export async function profileProject(options = {}) {
|
|
|
35
35
|
const packagePath = configPath
|
|
36
36
|
? await findUp(dirname(configPath), "package.json", cwd)
|
|
37
37
|
: await findUp(cwd, "package.json", cwd);
|
|
38
|
-
const packageJson = packagePath
|
|
39
|
-
? JSON.parse(await readFile(packagePath, "utf8"))
|
|
40
|
-
: undefined;
|
|
38
|
+
const packageJson = packagePath ? JSON.parse(await readFile(packagePath, "utf8")) : undefined;
|
|
41
39
|
const sourceRoot = configPath ? dirname(configPath) : cwd;
|
|
42
40
|
const sourceEvidence = await inspectSource(sourceRoot);
|
|
41
|
+
const tracingPackage = dependencyVersion(packageJson, "pi-cloudflare-agent-tracing");
|
|
42
|
+
const instrumentationReady =
|
|
43
|
+
Boolean(tracingPackage) &&
|
|
44
|
+
configBoolean(configText, "observability", "enabled") &&
|
|
45
|
+
configBoolean(configText, "traces", "enabled") &&
|
|
46
|
+
sourceEvidence.calls.length > 0 &&
|
|
47
|
+
sourceEvidence.allHarnessCallsWired;
|
|
43
48
|
|
|
44
49
|
return {
|
|
45
50
|
projectRoot: relative(cwd, sourceRoot) || ".",
|
|
@@ -54,6 +59,7 @@ export async function profileProject(options = {}) {
|
|
|
54
59
|
durable: dependencyVersion(packageJson, "@earendil-works/pi-durable"),
|
|
55
60
|
ai: dependencyVersion(packageJson, "@earendil-works/pi-ai"),
|
|
56
61
|
agents: dependencyVersion(packageJson, "agents"),
|
|
62
|
+
tracingPackage,
|
|
57
63
|
},
|
|
58
64
|
observability: {
|
|
59
65
|
enabled: configBoolean(configText, "observability", "enabled"),
|
|
@@ -61,6 +67,15 @@ export async function profileProject(options = {}) {
|
|
|
61
67
|
},
|
|
62
68
|
bindings: extractBindings(configText),
|
|
63
69
|
harness: sourceEvidence,
|
|
70
|
+
instrumentation: {
|
|
71
|
+
ready: instrumentationReady,
|
|
72
|
+
requirements: {
|
|
73
|
+
tracingPackageInstalled: Boolean(tracingPackage),
|
|
74
|
+
workersObservabilityEnabled: configBoolean(configText, "observability", "enabled"),
|
|
75
|
+
workersTracingEnabled: configBoolean(configText, "traces", "enabled"),
|
|
76
|
+
harnessTelemetryWired: sourceEvidence.allHarnessCallsWired,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
64
79
|
notes: [
|
|
65
80
|
"Configuration and telemetry detection are source heuristics; confirm relevant source before concluding.",
|
|
66
81
|
"Wrangler tracing does not by itself emit Pi Durable or Pi AI custom spans.",
|
|
@@ -89,8 +104,8 @@ async function inspectSource(root) {
|
|
|
89
104
|
if (!text.includes("Harness.open")) continue;
|
|
90
105
|
const positions = [...text.matchAll(/Harness\.open\s*\(/gu)].map((match) => match.index ?? 0);
|
|
91
106
|
for (const position of positions) {
|
|
92
|
-
const
|
|
93
|
-
const hasTelemetry = /\btelemetry\s
|
|
107
|
+
const call = extractCall(text, position, 4_000);
|
|
108
|
+
const hasTelemetry = /\btelemetry\s*(?::|[,}])/u.test(call);
|
|
94
109
|
telemetryWired ||= hasTelemetry;
|
|
95
110
|
harnessCalls.push({ path: relative(root, path), telemetryOptionDetected: hasTelemetry });
|
|
96
111
|
}
|
|
@@ -99,10 +114,38 @@ async function inspectSource(root) {
|
|
|
99
114
|
filesScanned: files.length,
|
|
100
115
|
calls: harnessCalls.slice(0, 20),
|
|
101
116
|
telemetryWired,
|
|
117
|
+
allHarnessCallsWired:
|
|
118
|
+
harnessCalls.length > 0 && harnessCalls.every((call) => call.telemetryOptionDetected),
|
|
102
119
|
truncated: files.length >= MAX_SOURCE_FILES,
|
|
103
120
|
};
|
|
104
121
|
}
|
|
105
122
|
|
|
123
|
+
function extractCall(source, start, maxLength) {
|
|
124
|
+
const endLimit = Math.min(source.length, start + maxLength);
|
|
125
|
+
const open = source.indexOf("(", start);
|
|
126
|
+
if (open < 0 || open >= endLimit) return source.slice(start, endLimit);
|
|
127
|
+
|
|
128
|
+
let depth = 0;
|
|
129
|
+
let quote;
|
|
130
|
+
let escaped = false;
|
|
131
|
+
for (let index = open; index < endLimit; index += 1) {
|
|
132
|
+
const character = source[index];
|
|
133
|
+
if (quote) {
|
|
134
|
+
if (escaped) escaped = false;
|
|
135
|
+
else if (character === "\\") escaped = true;
|
|
136
|
+
else if (character === quote) quote = undefined;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (character === '"' || character === "'" || character === "`") {
|
|
140
|
+
quote = character;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (character === "(") depth += 1;
|
|
144
|
+
if (character === ")" && --depth === 0) return source.slice(start, index + 1);
|
|
145
|
+
}
|
|
146
|
+
return source.slice(start, endLimit);
|
|
147
|
+
}
|
|
148
|
+
|
|
106
149
|
async function findSourceFiles(root) {
|
|
107
150
|
const results = [];
|
|
108
151
|
const queue = [root];
|
|
@@ -151,7 +194,12 @@ async function findUp(start, name, boundary) {
|
|
|
151
194
|
const stop = resolve(boundary);
|
|
152
195
|
while (directory.startsWith(stop)) {
|
|
153
196
|
const candidate = join(directory, name);
|
|
154
|
-
if (
|
|
197
|
+
if (
|
|
198
|
+
await stat(candidate)
|
|
199
|
+
.then((value) => value.isFile())
|
|
200
|
+
.catch(() => false)
|
|
201
|
+
)
|
|
202
|
+
return candidate;
|
|
155
203
|
if (directory === stop) break;
|
|
156
204
|
directory = dirname(directory);
|
|
157
205
|
}
|
|
@@ -179,7 +227,10 @@ function configBoolean(text, section, key) {
|
|
|
179
227
|
const jsonSection = text.match(
|
|
180
228
|
new RegExp(`["']${escapeRegExp(section)}["']\\s*:\\s*\\{([\\s\\S]*?)\\n\\s*\\}`, "u"),
|
|
181
229
|
)?.[1];
|
|
182
|
-
if (
|
|
230
|
+
if (
|
|
231
|
+
jsonSection &&
|
|
232
|
+
new RegExp(`["']${escapeRegExp(key)}["']\\s*:\\s*true`, "u").test(jsonSection)
|
|
233
|
+
) {
|
|
183
234
|
return true;
|
|
184
235
|
}
|
|
185
236
|
for (const block of tomlSections(text)) {
|
|
@@ -24,6 +24,7 @@ test("profiles a JSONC Pi Durable Worker without project-specific paths", async
|
|
|
24
24
|
dependencies: {
|
|
25
25
|
"@earendil-works/pi-durable": "1.5.2",
|
|
26
26
|
"@earendil-works/pi-ai": "1.5.2",
|
|
27
|
+
"pi-cloudflare-agent-tracing": "0.3.0",
|
|
27
28
|
},
|
|
28
29
|
}),
|
|
29
30
|
);
|
|
@@ -46,11 +47,36 @@ test("profiles a JSONC Pi Durable Worker without project-specific paths", async
|
|
|
46
47
|
assert.equal(profile.worker, "portable-worker");
|
|
47
48
|
assert.equal(profile.pi.durable, "1.5.2");
|
|
48
49
|
assert.equal(profile.harness.telemetryWired, true);
|
|
50
|
+
assert.equal(profile.harness.allHarnessCallsWired, true);
|
|
51
|
+
assert.equal(profile.instrumentation.ready, true);
|
|
49
52
|
assert.deepEqual(profile.bindings, ["SESSIONS"]);
|
|
50
53
|
assert.equal(profile.ambiguousConfiguration, false);
|
|
51
54
|
});
|
|
52
55
|
});
|
|
53
56
|
|
|
57
|
+
test("requires every production Harness.open call to wire the tracing package", async () => {
|
|
58
|
+
await fixture(async (root) => {
|
|
59
|
+
await mkdir(join(root, "src"), { recursive: true });
|
|
60
|
+
await writeFile(
|
|
61
|
+
join(root, "package.json"),
|
|
62
|
+
JSON.stringify({ dependencies: { "pi-cloudflare-agent-tracing": "0.3.0" } }),
|
|
63
|
+
);
|
|
64
|
+
await writeFile(
|
|
65
|
+
join(root, "wrangler.jsonc"),
|
|
66
|
+
'{ "name": "partial", "observability": { "enabled": true, "traces": { "enabled": true } } }',
|
|
67
|
+
);
|
|
68
|
+
await writeFile(
|
|
69
|
+
join(root, "src", "session.ts"),
|
|
70
|
+
`await Harness.open(storage, { telemetry }, context);\nawait Harness.open(other, options, context);`,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const profile = await profileProject({ cwd: root });
|
|
74
|
+
assert.equal(profile.harness.telemetryWired, true);
|
|
75
|
+
assert.equal(profile.harness.allHarnessCallsWired, false);
|
|
76
|
+
assert.equal(profile.instrumentation.ready, false);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
54
80
|
test("supports an explicit TOML configuration in a monorepo", async () => {
|
|
55
81
|
await fixture(async (root) => {
|
|
56
82
|
await mkdir(join(root, "apps", "first"), { recursive: true });
|
|
@@ -83,6 +109,7 @@ class_name = "Agent"
|
|
|
83
109
|
assert.equal(selected.worker, "second");
|
|
84
110
|
assert.equal(selected.main, "src/index.ts");
|
|
85
111
|
assert.equal(selected.observability.enabled, true);
|
|
112
|
+
assert.equal(selected.instrumentation.ready, false);
|
|
86
113
|
assert.deepEqual(selected.bindings, ["AGENT"]);
|
|
87
114
|
});
|
|
88
115
|
});
|