AGENT SKILL

query-analytics

Query Pie's canonical normalized analytics store (run/provider usage, costs, coverage, execution/tool/activity/feature facts). Use when a question needs settled per-invocation usage or cost numbers from real session history — never guess and never read the legacy JSONL ledger or raw session logs for aggregates

License 许可
AGPL-3.0
所属仓库
OwanL/pie
最近更新
2026-11-02
Stars
★ 1
Forks
0

01 前言 Front Matter

name
query-analytics
description
Query Pie's canonical normalized analytics store (run/provider usage, costs, coverage, execution/tool/activity/feature facts). Use when a question needs settled per-invocation usage or cost numbers from real session history — never guess and never read the legacy JSONL ledger or raw session logs for aggregates

03 SKILL.md 正文

Pie Canonical Analytics Queries

The authoritative analytics root is one normalized SQLite database:

<canonical-data-root>/analytics/analytics.sqlite

The canonical data root comes from PIE_DATA_DIR (absolute, or relative rooted at the agent directory); with no override it defaults to %LOCALAPPDATA%\pie\data on the supported Windows host.

Rules of engagement:

  • Open the database read-only (sqlite3 CLI, node:sqlite with readOnly: true, or the pie query helper). Never write, migrate, or delete.
  • Never derive aggregates from the legacy JSONL invocation ledger (billable-invocations.jsonl), raw session logs, or detail payloads. Only the normalized canonical store answers "how much".
  • Missing usage channels are NULL (never 0); a stored 0 is a real provider-reported zero. Report unknown ≠ zero.
  • Integer values (timestamps, token counts, revisions) are stored as decimal strings; compare/aggregate them as integers, not strings.
  • The compatibility session-usage UI has numeric token fields. A canonical int64 outside JavaScript's safe-integer range stays exact in query results and is exposed to that UI as an explicitly unknown channel; never convert it with Number(...) or present the rounded value.

Commands and bounds

The pie read helper exposes four logical commands — schema, query, detail, storage — as one disposable read-only process per query with a default 10 s inactivity timeout (caller-cancellable), a default 200-row / 256 KiB result bound (maximum 10 000 rows / 16 MiB), and 64 KiB detail ranges. Every logical-command result carries metadata read in the same SQLite snapshot as its payload: database schema version, projection revision, snapshot watermark (commit_sequence coverage), generation IDs, pendingDetailCoverage (delivery-history coverage, complete-detail watermark, and retained detail bytes), and explicit truncation flags (rowLimit, byteLimit, cellLimit, generationIdsTruncated). Detail reads return nextOffset + truncated; page with offset instead of raising bounds. Queries are cancelled by aborting the caller; only that helper fork is terminated.

The host-side adapter is CanonicalAnalyticsReadModel (analytics/queries/query-entry.ts). Production capture is canonical-only and automatic for fresh history. Read-only inspection adopts the store's sole capture namespace, or canonical for a new/empty store; old activation artifacts are inert. Helper failure pauses capture, never selects legacy capture. Treat every explicit error (missing database, ambiguous namespace, schema-version mismatch) as a hard stop — there is no legacy fallback.

Legacy run JSONL/checkpoints, billable ledgers, timeline persistence, automatic and command exports, historical migration/healing, and DuckDB paths are retired. Old files are preserved, not imported or backfilled; they do not establish canonical history coverage. Privacy scrubbing and explicit forget remain. Live projections, browser behavior, pruning, and the separately gated session-storage cutoff are unchanged; see the analytics contract.

Primary usage view

analytics_provider_usage_v1 — one row per settled provider invocation:

Column Meaning
invocation_id stable billable-invocation identity
generation_id persisted capture namespace; not activation evidence; process identity is separate producer evidence
owning_root_session_id root session that owns the settlement (branch/copy work is attributed to the owning root)
execution_id, branch_id captured execution and entry-ancestry anchors; NULL means unavailable
provider, effective_model, purpose, outcome attribution dimensions
input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, reasoning_tokens, provider_total_tokens decimal strings; NULL = unknown channel
normalized_base_input_tokens, normalized_output_tokens, normalized_cache_read_tokens, normalized_cache_write_tokens, normalized_total_tokens, normalized_usage_complete, reasoning_included_in_output engine-normalized channels + completeness flag
reported_cost_usd, calculated_cost_usd, calculated_cost_complete, effective_cost_usd, effective_cost_source, effective_cost_coverage provider-reported vs catalog-calculated cost with explicit coverage
settled_at_ms source settlement time, epoch ms decimal string; NULL is undated, while 0 is a real epoch timestamp
projection_revision revision that committed this row

Scope semantics: global/root-session totals count each owning invocation once. Selected-branch scope follows analytics_current_branch_selections through analytics_branch_edges; filtering on one branch_id alone omits ancestors. analytics_session_copies links inherited source work without adding another global settlement. The host's readScopedProviderSettlements helper exposes selectionCoverage and inheritanceCoverage; missing/scrubbed ancestry stays unknown. Keep expectedRevision fixed when paging that helper. Raw SQL states its own scope and is never silently rewritten into a selected-branch query.

Calendar semantics: state the IANA timezone. Today starts at local midnight; the live week includes today and the preceding six local dates, including DST boundaries. Canonical facts retain UTC source timestamps. Small provider/model/day summaries are maintained in analytics_provider_model_daily, keyed by the writer's active timezone/window in analytics_provider_daily_state; these are derived live summaries, not a complete historical calendar table. Arbitrary historical calendar queries use source settled_at_ms with explicit timezone-aware boundaries. Keep undated known usage in a separate bucket.

Examples

Total effective cost and coverage by provider and model:

SELECT provider, effective_model,
       COUNT(*) AS invocations,
       COUNT(effective_cost_usd) AS known_cost_invocations,
       COUNT(*) - COUNT(effective_cost_usd) AS unknown_cost_invocations,
       COALESCE(SUM(effective_cost_usd), 0) AS known_cost_usd,
       CASE WHEN COUNT(*) = COUNT(effective_cost_usd)
            THEN SUM(effective_cost_usd) ELSE NULL END AS complete_cost_usd
FROM analytics_provider_usage_v1
GROUP BY provider, effective_model
ORDER BY known_cost_usd DESC;

One root session's channel coverage (unknown stays unknown):

SELECT invocation_id, purpose, outcome, input_tokens, output_tokens,
       cache_read_tokens, cache_write_tokens, provider_total_tokens,
       effective_cost_usd, effective_cost_source, effective_cost_coverage
FROM analytics_provider_usage_v1
WHERE owning_root_session_id = ?
ORDER BY CAST(settled_at_ms AS INTEGER);

Current projection revision (cheap refresh check):

SELECT revision FROM analytics_projection_state WHERE singleton = 1;

Detail completeness / pending-coverage state via the storage command (delivery accounting): deliveryHistoryCoverage (complete vs retained_only), completeDetailWatermark, and retained byte counts. A retained_only value means pre-v3 history has no replay/deletion outcome — say so instead of implying complete history coverage.

Maintained activity and tool-facet projections

Two maintained projection reads answer activity and file-facet questions without history scans (host adapter readActivityProjection / readToolFacetProjection, or equivalent raw SQL below):

  • analytics_activity_summary — one additive row per (scope_kind, scope_key, activity_kind); scope key * is the global scope, a root session ID is the session scope. Columns are decimal-string counts (span_count, observed_count, estimated_count, unknown_count, measured_known_count, measured_unknown_count) plus measured_total_ms REAL. measured_total_ms is the sum of delivered span durations — additive measured work. Parallel or nested spans can exceed elapsed wall time; it is never a wall-clock union. Wall-union questions must query the member anchors in analytics_activity_projection_members (real started_at_ms/ended_at_ms anchors and owning clock domain) with explicit SQL. Unmeasured spans stay in measured_unknown_count with zero contribution, never a zero duration.
  • analytics_tool_facet_v1 — one row per terminal tool's file-activity facet state (view over analytics_tool_facet_states), revision-ordered: generation_id, facet_id, tool_call_id, root_session_id, commands_json, cwd, observed_paths_json, attempted_added_lines, attempted_removed_lines, verification. Line counts are patch/input- derived attempted proxies: verification is unverified whenever line activity evidence exists, and NULL counts mean the tool has no line- activity concept — never an invented empty change. Commands are literal shell-text evidence, not a process census. The schema description (schema command) discovers this view by name.

Example — additive activity work per kind for one root session:

SELECT activity_kind, span_count, observed_count, estimated_count, unknown_count,
       measured_known_count, measured_unknown_count, measured_total_ms
FROM analytics_activity_summary
WHERE scope_kind = 'session' AND scope_key = ?
ORDER BY activity_kind;

Reading results

  • Trust truncation metadata: when rowLimit/byteLimit/truncated is set, page or refine the query; never silently widen bounds.
  • Historical dimension membership also has per-category row bounds and a shared byte budget. Its truncated groups cannot establish complete membership/counts.
  • detail payloads are node-v8 representations of captured tool results and subagent results (application/x-pie-tool-observation, application/x-pie-subagent-result); omission_reason and capture_stage explain absent content — pending details are not silently zero.
  • Reconcile producers with analytics_producer_reconciliation / analytics_producer_sequences (visible sequence gaps mean missing upstream facts, not zeros).
  • Deleted subjects live in analytics_deleted_subjects; their observations are durably removed, not scrubbed-in-place.

04 同仓库更多 Skills