Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions workers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,18 @@ Time filter example: WHERE timestamp > NOW() - INTERVAL '30' DAY`,
openWorldHint: true,
},
async ({ sql }) => {
const startTime = Date.now();

if (!env.CF_ACCOUNT_ID || !env.CF_API_TOKEN) {
return {
content: [{
type: "text" as const,
text: JSON.stringify({
action: "telemetry_public",
result: { error: "Telemetry queries not configured. CF_ACCOUNT_ID and CF_API_TOKEN required." },
server_time: new Date().toISOString(),
assistant_text: "Telemetry queries not configured. Set CF_ACCOUNT_ID and CF_API_TOKEN.",
debug: { duration_ms: Date.now() - startTime },
}, null, 2),
}],
};
Expand All @@ -482,12 +487,26 @@ Time filter example: WHERE timestamp > NOW() - INTERVAL '30' DAY`,
result = { error: "Query execution failed." };
}

// Derive row count for assistant_text when the result carries data rows
let assistantText = "Telemetry query completed.";
if (typeof result === 'object' && result !== null) {
if ('error' in result) {
assistantText = "Telemetry query completed with errors.";
} else if ('data' in result && Array.isArray((result as Record<string, unknown>).data)) {
const rows = ((result as Record<string, unknown>).data as unknown[]).length;
assistantText = `Telemetry query returned ${rows} row${rows === 1 ? "" : "s"}.`;
}
}

return {
content: [{
type: "text" as const,
text: JSON.stringify({
action: "telemetry_public",
result: { data: result, generated_at: new Date().toISOString() },
server_time: new Date().toISOString(),
assistant_text: assistantText,
debug: { duration_ms: Date.now() - startTime },
}, null, 2),
}],
};
Expand Down
3 changes: 2 additions & 1 deletion workers/src/orchestrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,8 @@ async function runCatalog(
debug: {
knowledge_base_url: knowledgeBaseUrl,
baseline_url: index.baseline_url,
generated_at: index.generated_at,
generated_at: new Date().toISOString(), // response time — consistent with all other handlers
index_built_at: index.generated_at, // preserve cache-freshness diagnostic under accurate name
duration_ms: Date.now() - startMs,
},
};
Expand Down
31 changes: 30 additions & 1 deletion workers/test/canon-tool-envelope.smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ async function run() {
`got: ${policyOverride.debug?.knowledge_base_url}`,
);

// Tool 4: oddkit_encode — canon-driven, DOLCHEO-aware. Full envelope +
// Tool 4: telemetry_public — should have full envelope (no governance_source)
console.log("\n─── Testing: telemetry_public ───");
const telemetryPublicResult = await callTool("telemetry_public", {
sql: "SELECT 1 AS probe FROM oddkit_telemetry WHERE timestamp > NOW() - INTERVAL '1' HOUR LIMIT 1"
});
expectFullEnvelope("telemetry_public", telemetryPublicResult);

// Tool 5: oddkit_encode — canon-driven, DOLCHEO-aware. Full envelope +
// governance_source + DOLCHEO prefix-tag batch mode + Open facet + back-
// compat for unprefixed input.
console.log(`\n─── oddkit_encode: envelope + governance_source ───`);
Expand Down Expand Up @@ -596,6 +603,28 @@ async function run() {
`got: met=${prereqPass.result?.prerequisites?.required_met} total=${prereqPass.result?.prerequisites?.required_total}`,
);

// Tool: oddkit_catalog — debug.generated_at must be response time, not cached index time
console.log("\n─── Testing: oddkit_catalog ───");
const catalogResult = await callTool("oddkit", { action: "catalog", input: "recent" });
expectFullEnvelope("oddkit_catalog", catalogResult);

// debug.generated_at must be recent (under 60s) — not the cached index build time
const generatedAt = catalogResult.debug?.generated_at;
ok(`oddkit_catalog: debug.generated_at present`,
typeof generatedAt === "string" && /^\d{4}-\d{2}-\d{2}T/.test(generatedAt),
`got: ${generatedAt}`);
if (typeof generatedAt === "string") {
const ageMs = Date.now() - Date.parse(generatedAt);
ok(`oddkit_catalog: debug.generated_at is response time (age < 60s)`,
ageMs < 60_000 && ageMs >= 0,
`ageMs=${ageMs}`);
}

// index_built_at is the separate, accurately-named cache-freshness diagnostic
ok(`oddkit_catalog: debug.index_built_at present`,
typeof catalogResult.debug?.index_built_at === "string",
`got: ${catalogResult.debug?.index_built_at}`);

console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed === 0 ? 0 : 1);
}
Expand Down
Loading