Time-ranged activity narrative: on-demand, arbitrary-window reporting
engine/time_ranged_activity_narrative.py's TimeRangedActivityNarrativeTask narrates a Node's activity over any caller-supplied (start, end) window, on demand. It generalizes the shift-handoff summary drafting in engine/shift_handoff.py past "a shift".
What it covers
A manufacturing or quality engineer asks for a report over any window they name (a day, a week, "March 3 to March 10") and gets back the same kind of narrative that shift handoff drafts, live.
That is everything this Task does. It does not provide:
- a proactively delivered recurring digest, such as a standing daily report;
- longer-term multi-week or multi-month trend narration.
It only narrates patterns across records that are already logged and already decided: a deviation that was flagged, a disposition that was drafted, a reconciliation that occurred. It never computes a new statistical baseline over raw numeric data.
1. Load and version-check the prompt asset
from pathlib import Path
from engine.prompt_assets import assert_version, load_prompt_asset
narrative_asset = load_prompt_asset(Path("path/to/the/narrative/prompt/asset.md"))
assert_version(narrative_asset, "0.2.0")
2. Build the Task over already-constructed collaborators
from engine.mandatory_human_signoff import SignoffStore
from engine.time_ranged_activity_narrative import TimeRangedActivityNarrativeTask
signoff_store = SignoffStore()
signoff_store.ensure_schema()
task = TimeRangedActivityNarrativeTask(
inference_client, # engine.inference.InferenceClient
narrative_asset,
signoff_store,
activity_log, # engine.activity_log.ActivityLog
"MES", # this Node's own node_type
node_instance_id, # this Node's own instance id
)
signoff_store is only ever read here, through list_pending. See "Never submits for sign-off" below.
An optional keyword, inference_enabled (default True), turns the model call off. See "With inference disabled" below.
3. Ask for a narrative over any window
from datetime import datetime, UTC
result = task.query(
datetime(2026, 3, 3, tzinfo=UTC),
datetime(2026, 3, 10, tzinfo=UTC),
)
# result.narrative, result.activity_count, result.pending_case_count
Retrieval reuses shift handoff's own pattern. It reads two things:
- the Activity Log entries over the window;
- the drafted entries that are still pending and were submitted within the window.
Both filters run in the SQL WHERE clause, and neither fetches an unbounded table or backlog to discard most of it in Python. They are ActivityLog.query's own start/end filter (engine/activity_log.py) and SignoffStore.list_pending's matching start/end filter (engine/mandatory_human_signoff.py), the same two that engine/shift_handoff.py uses.
The drafting call is also shared with shift handoff, through draft_narrative in engine/narrative_drafting.py, so the two share one copy. The call builds the window's data into a JSON payload, calls the model, and raises when the retry budget is exhausted. draft_narrative returns the text together with the prompt's name and version. The result and the Activity Log entry carry them as prompt_name and prompt_version.
With inference disabled
With inference_enabled=False the model is never called. The narrative is a conservative, fixed, still-complete listing built from the counts already computed, and it carries no prompt_name or prompt_version. A real Node wires the flag from engine.inference_operating_mode.inference_is_enabled(config), as CaseTriage and ShiftHandoffTask do.
It describes this Node only
Every read of the Activity Log is filtered by this Node's own node type and instance. The narrative therefore covers what the Node it runs on did, whatever else shares the window.
A site's activity across Nodes is the job of the order trail and the genealogy (docs/operations/order-trail-assembly.md, docs/operations/traceability.md), which ask each declared peer for its entries.
Request/response shape, not a scheduled roll-up
TimeRangedActivityNarrativeTask.query mirrors OrderStatusQueryTask.query in engine/order_status_query.py: it is on demand, not scheduled, and returns directly to the caller. There is no run_scheduled, no ScheduledTask wiring and no "since last run" state to track. Every call is a fresh, caller-scoped window.
Never submits for sign-off
Unlike the scheduled roll-up in engine/shift_handoff.py, query() never calls SignoffStore.submit. The narrative is returned directly, the way OrderStatusQueryTask returns its abnormal-case reasoning directly instead of queueing it. This is a live report a caller asks for at will, not content meant to become a canonical, sign-off-gated record.
signoff_store is passed in only so that this Task can read still-pending drafted entries through list_pending.
Activity Log wiring
Every query() call writes exactly one ActivityLogEntry, through the shared Activity Log wiring (docs/operations/wiring-task-execution-to-activity-log.md):
triggeris always"time_ranged_activity_narrative_query"andtask_typeis always"correlation-time explanation"(engine.time_ranged_activity_narrative.TRIGGERandTASK_TYPE). This is the same trigger-to-task-type mapping for a correlation request (an order-trail or genealogy query) thatengine/order_status_query.pyandengine/order_narrative.pyuse.outcomeis always"drafted".
Pitfalls
window_endmust come afterwindow_start, orquery()raisesValueErrorimmediately. The window here is caller-supplied and arbitrary, unlike the internally computed window ofengine/shift_handoff.py. A swapped or equal pair is rejected before it reachesActivityLog.queryorSignoffStore.list_pending. Otherwise it would match zero rows and draft a false "nothing happened" narrative from an empty range.window_endis exclusive.ActivityLog.query'sstart/endfilter is half-open,[start, end), matching every other window this codebase filters.- A genuine call failure raises; it never returns a blank narrative. When every retry ends in a transport error or a blank response,
engine.inference.raise_if_failedraisesTimeRangedActivityNarrativeError. This is the same fail-closed stance thatShiftHandoffSummaryErrortakes for shift handoff's drafting call. - There is no recurring digest or multi-week trend report. A caller asking for one is asking for something this Task, and no other code in this repository, produces.
Release: v0.11.0