Files
weatherreporter/docs/internal/state.md

125 lines
4.1 KiB
Markdown
Raw Normal View History

2026-05-29 17:40:38 +00:00
# State Internals
2026-05-29 19:54:22 +00:00
This document describes filesystem state in `internal/state`.
2026-05-29 17:40:38 +00:00
## Purpose
2026-05-29 20:49:19 +00:00
`internal/state` owns managed workspace paths, atomic JSON writes, persisted
2026-05-29 19:54:22 +00:00
metadata, prior snapshot lookup, and read-only artifact inspection helpers.
2026-05-29 17:40:38 +00:00
2026-05-29 19:54:22 +00:00
## Inputs And Outputs
2026-05-29 17:40:38 +00:00
Inputs:
- workspace configuration
- resolved report definition and valid period
- module snapshot
2026-05-29 17:40:38 +00:00
- prompt input data package
2026-05-29 20:33:35 +00:00
- preflight artifact
2026-05-29 19:54:22 +00:00
- rendered report path preparation request
- RunID for inspection lookups
2026-05-29 17:40:38 +00:00
Outputs:
- module snapshot JSON path
- prompt input data package YAML path
2026-05-29 19:54:22 +00:00
- render preflight JSON path
- managed Markdown report path
- metadata JSON path
- prior comparable snapshot metadata
- loaded module snapshot or data package
- recent report records for inspection
2026-05-29 17:40:38 +00:00
## Boundaries
2026-05-29 20:49:19 +00:00
`internal/state` owns local filesystem layout, path validation, durable writes,
metadata reads, prior lookup, and report listing. It does not fetch weather
data, derive forecasts, build prompt input content, compare module contents,
2026-05-29 20:49:19 +00:00
invoke Scriptorium, import adapter result types, or parse CLI flags.
Preflight persistence uses the state-owned `PreflightArtifact` shape. The app
converts adapter render results into that shape before saving.
2026-05-29 17:40:38 +00:00
## Config Fields Used
- `workspace.root`
- `workspace.snapshots_dir`
- `workspace.reports_dir`
- `workspace.data_packages_dir`
- `workspace.preflight_dir`
- `workspace.notifications_dir`
2026-05-29 17:40:38 +00:00
Workspace subdirectories must be relative paths that stay under
`workspace.root`.
2026-05-29 20:49:19 +00:00
## Managed Layout
Paths are derived from the resolved report definition's artifact group, the
2026-06-09 21:08:17 +00:00
valid-period start date for dated artifacts, and the RunID.
2026-05-29 20:49:19 +00:00
```text
<workspace.root>/
snapshots/<artifact_group>/<YYYY-MM-DD>/<run_id>.modules.json
2026-05-29 20:49:19 +00:00
snapshots/<artifact_group>/<YYYY-MM-DD>/<run_id>.metadata.json
2026-06-09 21:08:17 +00:00
data-packages/<artifact_group>/<YYYY-MM-DD>/<run_id>.data_package.yaml
2026-05-29 20:49:19 +00:00
preflight/<artifact_group>/<YYYY-MM-DD>/<run_id>.render.json
notifications/<artifact_group>/<YYYY-MM-DD>/<run_id>.distributor.json
2026-05-29 20:49:19 +00:00
reports/<artifact_group>/<run_id>.md
```
Metadata is stored beside module snapshots and links the module snapshot, data
package, preflight, report paths, notification path when attempted, and
configured prompt location. Report listing walks metadata files under the
snapshots directory.
2026-05-29 17:40:38 +00:00
2026-05-29 20:49:19 +00:00
## Prior Lookup
2026-05-29 19:54:22 +00:00
2026-05-29 20:49:19 +00:00
Prior snapshot lookup reads stored metadata through the shared lookup path and
selects the latest earlier snapshot whose report ID is compatible with the
current report definition.
2026-05-29 19:54:22 +00:00
2026-05-29 20:49:19 +00:00
- Daily Today and Daily Tomorrow are compatible with each other for the same
valid local date.
- 3-Day Outlook compares with prior 3-Day snapshots for the same valid local
date.
- Weekend Outlook compares with prior Weekend snapshots for the same weekend
window.
2026-06-09 21:42:38 +00:00
- Storm Report has no prior lookup because explicit event-window comparison is
not searched by the filesystem store.
2026-05-29 20:28:06 +00:00
2026-05-29 20:49:19 +00:00
## Writes And Inspection
2026-05-29 19:54:22 +00:00
2026-05-29 20:49:19 +00:00
Durable JSON writes use shared atomic file helpers. Managed Markdown reports are
prepared by creating their parent directory; Scriptorium writes the report body
to the prepared path. Extra Markdown copies are handled by app orchestration.
Distributor notification debug artifacts are written atomically when
notification is attempted and include rendered distributor pipeline ID, bundle
ID, idempotency key, bundle paths, upload status, latest run status, and
redacted errors.
2026-05-29 19:54:22 +00:00
2026-06-09 21:37:28 +00:00
Inspection helpers read existing metadata, module snapshot, and data package
files. Missing metadata directories return no inspection records or no prior
snapshot rather than creating state.
2026-05-29 17:40:38 +00:00
## Failure Behavior
2026-05-29 19:54:22 +00:00
- Invalid workspace paths return validation errors.
- Missing required metadata fields prevent metadata writes.
- JSON writes use a temporary file followed by rename where practical.
- Read and decode failures include path context.
- Unknown RunIDs produce an actionable lookup error.
2026-05-29 17:40:38 +00:00
## Tests
Inspect:
- `internal/state/filesystem_test.go`
- `internal/app/app_test.go`
## Invariants
- Managed paths stay under the configured workspace root.
2026-05-29 20:49:19 +00:00
- Artifact grouping comes from report definitions.
2026-05-29 19:54:22 +00:00
- Metadata links artifacts produced for a run.
2026-05-29 17:40:38 +00:00
- Prior lookup is based on structured metadata, not rendered report text.