IMPORTANT: After completing the task below, you MUST output a JSON object in a [code]…
- User
IMPORTANT: After completing the task below, you MUST output a JSON object in a ```json code fence at the very end of your response. Do NOT forget this — the workflow fails without it.
Implement the current Spectraplex packet.
Follow:
- CLAUDE.md
- design/SPECTRAPLEX_STRATEGY_AND_EXECUTION_PLAN.md
- design/PHASED_IMPLEMENTATION_PLAN.md
Current coordination:
- phase: Phase 4: ETL-First Delivery
- work packet: P4-W4: Metadata and observability
- objective: Attach run IDs, dataset version, and completeness metadata to exports; add status introspection for export and materialization jobs so downstream consumers can determine provenance and completeness of exported data
- branch: p4-w4-metadata-observability
- stage: new packet implementation
- existing PR:
Repo touchpoints:
- api/src/main.rs — ExportJobStatus, ExportJobRequest, run_export_job, export job endpoints
- core/src/materializer.rs — DeliveryMetadata, DeliveryReceipt, ExportSink trait
- core/src/v2.rs — DatasetVersion, DatasetCompleteness, IngestionRun (read-only references)
- adapters/src/repo.rs — repository queries for dataset versions and completeness
- README.md — document enriched export metadata
Implementation steps:
-
- Add metadata fields to ExportJobStatus: dataset_version_id (Option), dataset_version (Option), completeness_status (Option), completeness_coverage (Option<serde_json::Value>), started_at and completed_at wall-clock timestamps (Option<DateTime>), last_ingestion_run_id (Option). Use skip_serializing_if for backward compatibility.
-
- Enhance run_export_job to look up the active DatasetVersion for the requested dataset (query dataset_versions table for status=Active, matching dataset_name). Return version metadata alongside (body, record_count).
-
- Enhance run_export_job to look up DatasetCompleteness for the requested dataset+target+network+time range. Return completeness_status and coverage bounds.
-
- Update the spawned export task to populate the new ExportJobStatus metadata fields from the version and completeness lookups before and after the export runs.
-
- Enrich DeliveryMetadata with dataset_version_id and completeness_status so sink consumers receive provenance context.
-
- Add a materialization status introspection helper: expose which Materializer version produced a given dataset's current records. This can be a new /v1/datasets/{name}/status endpoint or an enrichment of the existing /v1/datasets/{name}/versions endpoint.
Assistant
Now let me read the key files I need to modify:
Assistant
The api/src/main.rs is very large. Let me read the relevant sections in parallel:
Assistant
Now let me read the specific sections of api/src/main.rs that I need to modify:
Assistant
Now let me read the rest of the materializer.rs and the export-related sections:
Assistant
Now let me read more of the repo.rs to find relevant methods and also look at the test section I'll need to update:
Assistant
Let me read the remaining test sections:
Assistant
Now let me also look at what repo.rs methods exist related to dataset versions and completeness:
Assistant
Now let me check what functions are in repo.rs that relate to datasets:
Assistant
Now let me check how the repo is used for dataset queries in the API - specifically the list_completeness_by_dataset and related methods:
Assistant
Let me read the methods I need for version lookups and completeness:
Assistant
Now let me also look at how the
get_active_versionquery might work - we need a method that returns the active version for a dataset:Assistant
Now let me also read the existing
list_dataset_versions_handlerandlist_all_datasetsto understand the pattern for the dataset status endpoint:Assistant
Now I have a comprehensive understanding of the codebase. Let me also look at the
validate_dataset_nameandDEFAULT_PAGE_LIMITandget_active_dataset_versionthat we need:Assistant
Now let me also check what's around the
build_sinkandLocalFileSinketc.:Assistant
Now let me check the README to know where to add the docs:
Assistant
Now I have a thorough understanding of the codebase. Let me also read the existing
get_active_dataset_version(or the one returning Active status) - theget_latest_dataset_versionreturns latest by version DESC, but we want specifically Active status. Let me check thelist_dataset_versionshandler more:Assistant
Now I have a comprehensive understanding. Let me implement the packet. I'll now make all the changes needed:
- ExportJobStatus - Add metadata fields
- run_export_job - Return version + completeness metadata alongside the data
- Spawned export task - Populate the new fields
- DeliveryMetadata - Enrich with dataset_version_id and completeness_status
- Dataset status endpoint - New
/v1/datasets/{name}/status - Tests - New tests for serde, metadata, etc.
- README.md - Document changes
Let me also add a
get_active_dataset_versionmethod to the v2_repo since we need it:Assistant
Now I have everything I need. Let me implement the changes. I'll start by making all the edits in parallel:
Assistant
Now update all usages of
DeliveryMetadatain the codebase to include the new fields. Let me find them:Assistant
Let me read the materializer.rs test that constructs DeliveryMetadata:
Assistant
Now let me update all the
DeliveryMetadataconstruction sites:Assistant
Now let's add the
get_active_dataset_versionmethod to v2_repo.rs:Assistant
Now let me add a method for looking up completeness by dataset + target + network:
Assistant
Now let me make the big changes to api/src/main.rs. First, add
chrono::DateTimeto imports and updateExportJobStatus:Assistant
Now update
ExportJobStatuswith the new metadata fields:Assistant
Now add the
serialize_optional_datetimehelper. Let me find a good spot for it - right after theAppErrorimpl:Assistant
Now I need to update the
run_export_jobfunction to also return metadata. Let me define a struct for the metadata alongside the return:Assistant
Now let me update
run_export_jobto returnExportMetadata:Assistant
Now I need to add the
ExportMetadatastruct and modifyrun_export_jobto also look up version and completeness:Assistant
Now update each dataset branch in
run_export_jobto return the metadata. Let me read the current content again to get exact strings:Assistant
Now update all the
Ok((body, count))toOk((body, count, meta)):Assistant
Now update the
create_export_jobto populate the new fields in ExportJobStatus and handle the new return fromrun_export_job. Let me re-read the spawned task:Assistant
Now update the spawned task — mark running with
started_atand handle the new tuple return:Assistant
Now add the materialization status endpoint. Let me add it near the dataset endpoints. First, let me add the route:
Assistant
Now add the handler and response type. Let me add it right after the
get_dataset_completeness_handler:Assistant
Now I need to add the route to the test router as well. Let me find the test router:
Assistant
Now I need to update ALL existing test sites that construct
ExportJobStatusto include the new fields. Let me do that:Assistant
I need to update each ExportJobStatus construction in tests. Let me read them and update them all. There are many, so let me use a targeted approach. The pattern is always the same — we just need to add the 7 new fields. Let me batch these:
Assistant
This is going to be repetitive. Let me create a helper function for the tests and use a systematic replacement approach. Actually, the simplest approach is to add a helper function that creates a default ExportJobStatus with the 7 new fields set to None:
Instead of updating all tests individually, let me use the replace_all approach for the common pattern. The pattern is
delivery_status: None,\n },at the end of each ExportJobStatus construction in tests. After this closing, we need to add the new fields. Actually, each one is slightly different. Let me just do them all:Assistant
Good. Now let me also update the DeliveryMetadata constructions in the api test file:
Assistant
Now let me add the new P4-W4 tests for enriched metadata. Let me find the end of the test file:
Assistant
Now let me also check if the
skip_none_delivery_fieldstest still properly asserts:Assistant
Good, that test is already updated and checks delivery fields. Now let me update the README.md. Let me find the right section:
Assistant
Now update the export job status response docs and add the new status endpoint docs. Let me find the section about export job status response:
Assistant
Now let me also update the CLAUDE.md API surface section to mention the new endpoint:
Assistant
Now let's try to build and see if there are any compilation issues:
Assistant
It builds. There's one warning about unused
DatasetVersionStatus. Let me remove that import since we're using the status through string comparison in the handler:Assistant
Now let's run the full verification suite:
Assistant
Now run clippy:
Assistant
All clean. Now run the tests:
Assistant
Let me count the total tests:
Assistant
All 780 tests pass (exceeding the 772+ baseline). Let me verify fmt and clippy once more:
Assistant
All verification passes. Now let me create the commit:
Assistant
The
.claude/CLAUDE.mdwas the one in gitignore. Let me only add the root CLAUDE.md which is checked into the codebase:Assistant
The CLAUDE.md isn't showing in git status, which means our edit was to the root CLAUDE.md which is in fact tracked. Let me check:
Assistant
The root CLAUDE.md is gitignored. The
.claude/CLAUDE.mdis the one that's tracked. Let me check:Assistant
Neither CLAUDE.md is tracked in git - they're both gitignored or not added. This is fine - the CLAUDE.md updates were just for project docs consistency. The important changes are the 4 tracked files. The files are already staged:
Assistant
Now create the commit:
Assistant
All done. Let me verify the final state:
Assistant