Skip to content

feat(datasets,presentation): export any frame to a lasting link, read frames by spec_id, and roll a pairing over both sides - #194

Merged
jm-rivera merged 27 commits into
devfrom
feat/public-exports
Oct 30, 2026
Merged

jm-rivera merged 27 commits into
devfrom
feat/public-exports

Conversation

@jm-rivera

@jm-rivera jm-rivera commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Adds a lasting download link for any frame, fetched or derived, and fixes how rolling analysis counts the two sides of a pairing.

Exports

  • POST /api/datasets/exports takes {frame: {spec_id, content_version}, format: csv|xlsx|parquet, bundle}. It renders the file through the existing file writers, pins it, and records it under export_id = exp_ + the first 32 hex of the SHA-256 of the RFC 8785 form of the request. A repeat request returns the stored record, so it renders nothing and spends nothing. Callers are admitted as the pin door admits them. A new pinned copy spends the caller's pin allowance; a file already pinned spends nothing, even when two prepares race for the last unit. Each export holds an artifact_reference row of its own kind, so the reconciliation sweep counts it. A rendering linked incomplete refuses retryably, with nothing pinned or recorded.
  • GET /api/datasets/exports/{export_id} answers 302 to a five-minute signed URL on the pinned tier, with Cache-Control: no-store. It never renders. One guard runs on a prepare, a repeat prepare and the link: a version revoked under any bound id, a quarantined identity, or a revoked root of a derived frame refuses. The response to a prepare never holds a signed URL.
  • Both routes check licence_restrictions.json, a deny-list keyed by source ref. It ships empty: no source's terms are recorded yet, and today's file routes already serve every dataset.
  • csv and xlsx stop at 250,000 rows (over_ceiling). An ops_ frame with no stored recipe, or with a root that cannot be cited, refuses provenance_unavailable.
  • Migration 0044_export.sql adds the exports table. The website's datasets proxy already forwards /api/datasets/*, so the public link is https://developmentfinance.org/api/datasets/exports/{export_id}.
  • spec-kit is pinned to DevelopmentFinanceObservatory/spec-kit 2dd2400 (the datasets.exports and datasets.export_link codes, with the rebuild refusals on datasets.exports and identity_quarantined on the link); merge spec-kit#37 with a merge commit so the pin stays on its dev history, or re-pin to the squash commit.

Frames by spec_id

  • GET /api/presentation/frames/{spec_id} resolves the newest served version as the analysis runner resolves a Spec root (LiveRootSource.resolve(SpecRoot(spec_id))), honouring a revocation under the id sent, and answers the pair route's body over that version's canonical frame, plus the spec_id and content_version it read. A version still building answers 503 frame_unavailable; a rebuild that failed for good answers 500. A rebuild larger than the route's wait goes to an operation, so a retry finds it. The route runs on its own executor (PRESENTATION_FRAMES_BARE_MAX_CONCURRENCY, default 2), so a slow read cannot take the pair route's permits.
  • POST /api/datasets/exports accepts frame: {spec_id} alone and resolves it through analysis_roots.newest_spec_version, the same resolve the bare frames route uses. The export record keeps the id sent, so a later takedown of that id reaches the link. A rebuild refusal registered on datasets.exports passes through unchanged.
  • frame_unavailable carries its reason as cause.code, so a client can tell a frame it may rebuild from a revoked or quarantined one.
  • POST /api/presentation/components accepts an intent whose frame_ref holds only a spec_id and stores the resolved pair. A create retried with its write token replays its first reply.

Default enrichment

  • datasets_api.default_enrichment holds one rule for a frame's default type and party columns: the _type column of each of observationAbout, financingProvider and financingRecipient among the version's coverage dimensions, plus party_kind, party_class and party_whole when a party role is present. It reads the roles the version holds, so a flow identity that binds no role gets its party columns where the identity-based rule asked for observationAbout_type and refused column_unknown. Frames that resolved before keep their columns and names.
  • A mint takes options.enrichment: "default", resolved against the served version before the rendering is keyed, so the answer equals a mint that names those columns. Unset options dump and key as before. Spec roots send it and serve once. dfo-ai's public get_data sends it.
  • A fetched-frame export renders with the default enrichment, so the file carries each role's _type column beside its _label and _kind cells.

Rolling over a whole tree

  • dfo_analysis_engine.rolling._sides found a node's frame slots from its *_frame_ref parameter names. The analysis runner removes those from its working tree, so a compare or join took its rolling populations and latest period from the left side alone. It now reads the slots from the node's inputs too, and ENGINE_VERSION moves to 0.9.1, so an answer stored before this fix is computed again. A narrowing above a two-sided verb crosses the verb's key map to the other side. A join with no join_keys counts each side flat, as compare and sum_of with no on do, because no tree holds the frames the engine detects keys from. A stored recipe names its slots in its parameters, so a recipe the runner stores with this fix replays on latest to the same frame (test_a_rolling_step_over_a_two_sided_verb_stores_its_slots_and_replays_on_latest). A two-sided rolling recipe the runner stored before this fix, since feat: run analysis on DFO's engine and serve cards and shares from dfo-apis #170 deployed, can hold a left-only population list; its latest replay refuses rolling_anchor_unresolved on dev today and still does after this change.
  • POST /api/datasets/analysis takes rolling: "tree". With it, each node expanded from a derived root drops its overlay population, its anchor_roots and the literals its overlay governs, so the whole tree takes one population list and one latest period. dfo-ai's public analysis tools send it. Without it a request is unchanged.

…ved inputs follow the tree

The engine found a node's frame slots from its *_frame_ref parameter names, which the analysis runner removes from its working tree, so a compare or join took its rolling populations and latest period from the left side alone. rolling._sides now reads the slots from the node's inputs as well.

POST /api/datasets/analysis takes rolling: "tree". With it, each node expanded from a derived root drops its overlay population, its anchor_roots and the literals its overlay governs, so the whole tree is filled with one population list and one latest period. Without it the request is unchanged.
POST /api/datasets/exports renders a fetched or derived frame as csv, xlsx or parquet, optionally bundled, pins the file and records it under an export_id derived from the request, so a repeat prepare renders and spends nothing. GET /api/datasets/exports/{export_id} answers 302 to a five-minute signed URL with Cache-Control: no-store and never renders. Both routes check licence_restrictions.json, which starts empty. Pins spec-kit 831e1d5 for the datasets.exports and datasets.export_link codes.
…d the served id

The export link reads the version's revocation before it signs and refuses snapshot_revoked. A prepare by a posted id records the served id the version is stored under, as the pin door does, so the row meets its foreign key. Pins spec-kit a588976.
… alone

GET /api/presentation/frames/{spec_id} reads the newest served version in its default enrichment, resolved as the analysis runner resolves a Spec root, and names the pair it read. Component create accepts an intent frame_ref holding only a spec_id and stores the resolved pair.
@jm-rivera jm-rivera changed the title feat(datasets): export any frame to a lasting link, and roll a pairing over both sides feat(datasets,presentation): export any frame to a lasting link, read frames by spec_id, and roll a pairing over both sides Oct 29, 2026
POST /api/datasets/exports accepts frame {spec_id} and resolves it through analysis_roots.newest_spec_version, the resolve the bare presentation frame route also uses, so the export record and its id name the resolved pair. Registered rebuild refusals pass through unchanged. The spec-kit pin moves to 0041a4a, which registers them on datasets.exports.
# Conflicts:
#	packages/dfo-analysis-engine/pyproject.toml
#	packages/dfo-data-tools/pyproject.toml
#	pyproject.toml
#	uv.lock
… registry predicate

The bare-spec resolve and the exports door share exports.passes_through, so a refusal is classified once and a server fault logs one line.
…lumns

A fetched-frame export renders the version with the enrichment columns analysis_roots.enrichment_columns names, the columns a Spec root and the public MCP get_data read, so the file carries each role's _type column beside its _label and _kind cells.
… version holds

enrichment_columns read the identity, so a flow identity that binds no role asked for observationAbout_type and refused column_unknown. analysis_roots.coverage_roles reads the role dimensions the version's coverage records, and the type and party columns follow from those; exports, DatasetRoot and Spec roots all use it.
options.enrichment "default" resolves to the requested or default columns plus the type and party columns for the roles the served version holds, before the rendering is keyed, so the answer equals a mint that names those columns. Spec roots send it and serve once; the rule lives in datasets_api.default_enrichment.
…literal codes

The registry sweep reads every code= statically, so the three exemplars are written out.
…swers current

A prepare, a repeat prepare and the export link now run one guard, so a revoked or quarantined version, a revocation under any bound id, and a revoked recipe root all refuse. Each pinned export holds an artifact_reference row, a file already pinned spends no allowance, and a rendering linked incomplete refuses retryably with nothing pinned. A derived frame with an uncited root refuses provenance_unavailable.

ENGINE_VERSION moves to 0.9.1, since the two-sided rolling walk changes a tree's population and an answer stored under the old key would otherwise be served.

A bare spec_id read scales its inline budgets to the wait, so a rebuild longer than the wait goes to an operation a retry finds, and a refusal the analysis endpoint does not register maps to frame_unavailable. The spec-kit pin moves to 52e4e42.
…cause

frame_unavailable carries its reason as cause.code, so a client can tell a frame it may rebuild (unknown, expired, ops_unsupported) from one it must not (revoked, quarantined, digest_mismatch, missing_rendering).
…oins flat

A Spec root's serve honours a revocation under the id it names, and a bare export records that id, so a later takedown or quarantine reaches the link. The export guard refuses when a version record or recipe is missing, and a revoked head reads as cause revoked on the bare route.

A rolling step over a join with no join_keys counts each side flat, as compare and sum_of with no on do, so a valid join no longer refuses op_spec_invalid. A default-enrichment head hit reads its version once, and artifact_rendering refuses an unresolved enrichment. The spec-kit pin moves to 2dd2400.
# Conflicts:
#	src/datasets_api/reference_store.py
#	src/presentation_api/README.md
…are frame reads

A narrowing above a two-sided verb crosses the verb's key map to the other side, and a level held there applies only where that root carries the column, so a rolling filter above a keyed quotient no longer refuses.

The bare frames route runs on its own executor (PRESENTATION_FRAMES_BARE_MAX_CONCURRENCY), so a slow rebuild cannot take the pair route's permits. A lasting export link reads only the stored fields it needs, so a later change to the reply cannot break it.
… key columns

A verb's second slot comes from the verb, so a one-frame compare or share_of counts both sides in either tree shape. Only a binding on a key column, under its mapped name, and a level on a key's enrichment column cross a pairing, so a numerator's filter no longer narrows a denominator it does not reach.

A bare create resolves on the frames-bare executor, so a slow rebuild cannot take the component routes' permits, and a repeat prepare needs no caller.
…e engine

A join with no join_keys has keys the engine detects, so a narrowing above it crosses under its own column names, with levels optional and a binding on a column the other root lacks dropped.
@jm-rivera
jm-rivera merged commit a6edda0 into dev Oct 30, 2026
17 checks passed
jm-rivera added a commit to DevelopmentFinanceObservatory/ai that referenced this pull request Oct 30, 2026
…ears, one refusal envelope and export_data (#124)

The public MCP server at `/mcp` lists 16 tools at 18,018 o200k tokens, down from 19 tools at 27,054. It adds `export_data`, takes a bare `spec_id` wherever a frame goes, rolls windows over relative years, and gives every refusal one envelope whose `fix` names only arguments the tool accepts.

## Surface

- `compare` takes `operation` (`difference` or `sum`) and `share_of` takes `operation` (`share` or `quotient`). Each form runs today's `sum_of` or `quotient_of` path with the same frame id, digest and refusal codes. A share refused `cross_frame_unit_mismatch` between two plain units sets `fix` to `operation: "quotient"`.
- `get_skill` takes `path`, `offset` and `digest`, and with a `path` returns what `get_skill_file` returns.
- `sum_of`, `quotient_of`, `get_skill_file` and `resolve_anchor` stay on the per-server mounts the Studio harness calls, and leave the public list. A call to one of the first three refuses `tool_not_found` with a `fix` naming the merged tool and the argument to set.
- `common.declaration` applies the declaration rules to every tool at registration. It removes `$comment`, empty `required` and the null branch of a property that states `default: null`, inlines every definition, and keeps each sentence once. It refuses a schema whose references it cannot inline.
- Each child server declares the public shape of the tools whose public form differs (`PUBLIC_SHAPES`): paragraphs the public description adds, properties it hides and arguments it forces. The combined server applies them to every tool alike. A public call that sends a hidden, unforced argument refuses it as unknown; a forced argument the caller sends is accepted and overwritten.
- The public `get_data` forces `place_types` and asks the dfo-apis mint for `enrichment: "default"`, so every public frame carries the type and party columns of the roles its version holds; dfo-ai reads the columns from the rendering the mint served. `rank`, `aggregate` and `share_of` take `place_level`, sent to dfo-apis as a filter step under the verb.
- A test bounds the public list in o200k tokens (`_PUBLIC_LIST_TOKEN_BOUND`), and another holds every public schema to the declaration rules. A test holds every code the servers write to its `mcp.tools` mapping in the pinned registry.

## Frames

- Frame arguments are typed objects: `spec_id` required, `content_version` optional. A bare `spec_` id reads the newest version, the one the dfo-apis analysis runner's Spec root reads: the verbs send it as a Spec root and record the pair it names; `describe_frame`, `write_component` and `export_data` send it to the dfo-apis routes that resolve it. The public server registers a bare id's stored definition first, so an id fresh from `search` works on all four paths. A bare id of any other kind refuses `frame_ref_incomplete`.
- On the public server a verb rebuilds every lost input the runner names (a `spec_` frame by fetching it again in the rendering its mint record states, an `ops_` frame by replaying its recipe with `latest: true`) and retries; `describe_frame` and `write_component` create do the same for a 404 `frame_unavailable` whose `cause.code` is `unknown`, `expired` or `ops_unsupported`, and each reply names the pair asked for and the one read. `common.materialisation.rendering` answers a frame's rendering for all three. A frame whose rendering cannot be established, and in presentation-mcp a summed or cut frame, refuses `rendering_not_recorded` with a `fix` naming `get_data`, since dfo-apis has no route that reads a Spec at a rendering outside an analysis tree. A bare read records the pair it resolved. The harness mounts keep `frame_not_held` and `frame_unavailable`, so Studio runs its own replay. A hidden `rebuild_lost_frames` argument, forced true by `PUBLIC_SHAPES`, carries the difference; `common.derived_replay` holds the one replay call.

## Rolling periods

- `growth` and `rank` window bounds and the `filter` threshold value take `{"anchor": "latest", "offset": n}` beside a four-digit year. dfo-apis resolves the latest year from the tree, and the digest names it and the years it resolved.
- A public call whose tree holds an `ops_` input sends `rolling: "tree"`, so dfo-apis rolls those inputs' periods over the whole tree; the digest names any input computed again. A derived input with no stored recipe refuses `rolling_reanchor_unavailable`. The harness sends no `rolling` field and keeps its own populations.

## Refusals

- presentation-mcp refusals carry `code`, `retry_mode`, `message` and `fix` in `structuredContent` and the same facts in `content`, with `isError`, as data-mcp and analysis-mcp do. Advice moves into the message; `_meta` keeps the assembler's refusal for the harness, so Studio's refusal is unchanged.
- `request_invalid` carries `fix` `{tool, problems, accepted}` on every server, with dfo-apis pointers rebased onto the tool's own arguments. Tests sample every refusal code of data-mcp, analysis-mcp and presentation-mcp and check each `fix` against the tool's declared schema.

## export_data

`export_data(frame_ref, format, bundle)` prepares the file through dfo-apis `POST /api/datasets/exports`, forwarding the caller, and returns `https://developmentfinance.org/api/datasets/exports/{export_id}`. That link answers 302 to a five-minute signed URL and does not expire. The result and its digest carry the file's name, size, rows, columns, SHA-256, citation and licences. A `get_data` frame exports the stored version its mint record names, and the result names both. A summed or broken-down `get_data` frame refuses `capability_unavailable` and names the `aggregate` route, because the export route takes no groupby. A frame with no mint record refuses `rendering_not_recorded`. A frame dfo-apis no longer holds refuses `frame_unavailable` with the call that registers it again; an export never moves to another version by itself.

## Order

1. Merge DevelopmentFinanceObservatory/spec-kit#37 with a merge commit, which registers the new codes and keeps `2dd2400`, the commit this PR and DevelopmentFinanceObservatory/apis#194 pin, in spec-kit's dev history. A squash merge drops it, and both PRs then need re-pinning to the squash commit.
2. Merge apis#194 and let it deploy to dev. Its analysis route accepts `rolling: "tree"`, and its export and bare-`spec_id` routes are what `export_data`, `describe_frame` and `write_component` call, and its `cause.code` on `frame_unavailable` is what presentation-mcp's rebuild reads; dev dfo-apis refuses the `rolling` field until then.
3. Merge this PR.
@jm-rivera
jm-rivera deleted the feat/public-exports branch November 27, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant