Skip to content

Artifact Staleness and Routing

Purpose

This document explains how the Refinement Engine marks build records stale after a refinement request, what that staleness is used for, and what it is not: staleness is recorded per build record, by ID, and is never propagated to downstream families or used to pick a route.

Related documents:


Typed Artifact Chain

Each build stage outputs a typed artifact that the next stage consumes as an authoritative input — not a suggestion to reinterpret from prose:

ValueEngine → ConceptBlueprint (typed)
    ↓  surface_candidate_hints[]  → DesignDocs surface_map (authoritative)
    ↓  brand_intent               → experience_spec.brand_direction
ThemeCapture → CapturedThemeConfig (typed)
    ↓  theme.variant / appearance → experience_spec.brand_direction
    ↓  identity.tagline           → brand posture
DesignDocs → ExperienceSpec + surface_map + data_contract (typed)
    ↓  experience_spec.pages[]   → AppPlanAgent page list (authoritative)
    ↓  surface_map               → AppGenerator module generation
AgentGenerator → workflow_bundle
AppGenerator → app_bundle (DRAFT → reviewed → CURRENT)

This chain is only as strong as its weakest typed handoff. Every typed artifact variable in a downstream workflow's context_variables.yaml must declare a data_reference source pointing at the upstream collection and the specific typed field — not at a prose string.


Artifact Families and Their Dependencies

The build pipeline produces six artifact families. Each family depends on upstream families:

concept
  ├── brand          (depends on concept)
  ├── design_docs    (depends on concept)
  └── experience_spec (depends on concept and design_docs)
        └── workflow_bundle  (depends on design_docs)
              └── app_bundle (depends on design_docs, experience_spec, workflow_bundle, brand)

This dependency order is declared as artifact_dependency_graph in factory_app/workflows/extended_orchestration/extension_registry.json and loaded into GlobalPackGraph at runtime. Workflow sequences declare their affected_declarative_families against it, and the coding worker's stale-parent check reads it to find the families a parent build depends on. Nothing walks it to mark downstream families stale.

experience_spec is the first-class experience intent family produced by DesignDocs. It remains separate from the broader design_docs family so a page, navigation, or shell-experience refinement can invalidate experience intent before app-bundle regeneration without necessarily treating all design documentation as stale.


When a Build Record Becomes Stale

Every BuildRecord carries a lifecycle_status:

Status Meaning
draft Generated, not yet reviewed
current Accepted and active
stale Invalidated by a later change; superseded in intent
superseded Replaced by a newer current version
archived Rejected or explicitly retired
deleted Hard-removed

A change request records every routed attempt. The ArtifactInvalidationService runs for it only once the operation it started has changed the selected build or left its outcome unresolved: in Studio, an inline run that stored a child, not since rejected, whose validation passed, is pending or was skipped (or whose child lookup failed), or a workflow run that launched. A request that returns a decision, a launch that failed, or an inline run that stored nothing or only a child whose validation failed marks nothing stale. When it runs, the service marks stale, by ID, the build records named for the families in the routing decision's impact set (affected_declarative_families, or the request's own build family when that is empty), using change_request:<id> as the invalidation reason. The IDs come from:

  1. the build record the request was made from (build_family and build_record_id on the refinement request), in Studio the selected build;
  2. the session's tracked artifact_version_refs, loaded for the request's user. Studio keeps no session under the artifact app, so in Studio this adds nothing today.

Staleness is not propagated. A build record of another family is marked stale only if its own ID is named above; no query looks for stale families, and a record of a family downstream of the change keeps its status until a workflow writes a new version of that family.


What Staleness Is Used For

A stale record is a fact about that record, not a routing signal. Routing classifies the request (the LLM classifier at request_submitted) and maps the class through the harness routing table; no stale record bypasses or upgrades that classification.

Staleness of the selected record is read where the record itself is the subject:

  • the coding worker's stale-parent check decides whether a retry may start from a stale parent build; one of its conditions is that every family the parent depends on in artifact_dependency_graph is unchanged since the parent was accepted (see Refinement Harness Architecture);
  • the revision context reports the selected record's lifecycle_status, and Studio saves its lifecycle_status, invalidation_reason and stale_at on a proposal's change request to tell whether the build changed before confirmation.

A new current version of a family supersedes the family's previous current version. Stale records stay stale; no cleanup step is needed because nothing derives a family-level stale state from them.


Where the Dependency Graph Lives

factory_app/workflows/extended_orchestration/extension_registry.json
"artifact_dependency_graph": {
  "concept":         [],
  "brand":           ["concept"],
  "design_docs":     ["concept"],
  "experience_spec": ["concept", "design_docs"],
  "workflow_bundle": ["design_docs"],
  "app_bundle":      ["design_docs", "experience_spec", "workflow_bundle", "brand"]
}

This field is part of GlobalPackGraph (mozaiksai/core/workflow/pack/schema.py). It must stay in sync with the actual workflow sequence ownership declared in affected_declarative_families on each sequence.

Staged refinement promotion also snapshots into draft app_bundle ArtifactVersion records using the same app_bundle family and lifecycle. Patch-ness stays in metadata; it is not a separate artifact kind or family. Those drafts become CURRENT only through the staged refinement acceptance helper, not by introducing a new artifact family. After acceptance, Studio promotes the accepted CURRENT app_bundle back into the runnable app root using safe extraction that skips staging metadata files and backup trees and rejects unsafe archive paths. The accepted bundle must carry a non-empty files_manifest before Studio restore can proceed.


Artifact Persistence Per Workflow

Every workflow that produces a canonical artifact family must call persist_summary_artifact() so that BuildRecord documents exist in the DB with accurate lifecycle status. Invalidation by ID and the stale-parent check depend on these records being present.

Workflow Artifact family Persistence call site
ValueEngine concept factory_app/workflows/ValueEngine/tools/manifest.py
ThemeCapture brand factory_app/workflows/ThemeCapture/tools/save_captured_theme.py
DesignDocs design_docs factory_app/workflows/DesignDocs/tools/save_design_doc.py
AgentGenerator workflow_bundle factory_app/workflows/AgentGenerator/tools/platform/build_lifecycle.py — emit_build_completed override
AppGenerator app_bundle factory_app/workflows/AppGenerator/tools/platform/build_lifecycle.py — emit_build_completed override

AppGenerator and AgentGenerator persist their artifacts via the trigger: on_complete lifecycle hook, which fires after the workflow run finishes. The local build_lifecycle.py in each workflow overrides emit_build_completed from the shared module to call persist_summary_artifact() after the platform notification, reading build_mode from the persisted chat session context to correctly set revision_mode.


Artifact Version Refs — How Session State Stays Current

ArtifactInvalidationService.invalidate_for_change_request() reads SessionState.artifact_version_refs to mark specific version IDs stale (invalidation by ID). Those refs must be accurate for every refinement request, including the very first one after an initial build.

After each workflow run completes, SessionRouter.advance_journey_after_run_complete() calls BuildRecordStore.get_current_build_record_refs() to fetch all CURRENT build record IDs for the app, keyed by build family, and merges them into state.artifact_version_refs before upserting. This means:

  • All artifact-producing workflows (ValueEngine, ThemeCapture, DesignDocs, AgentGenerator, AppGenerator) populate the refs automatically at completion — no tool-level bookkeeping required.
  • Revision routing via _apply_revision_state() still overlays the specific requested artifact version ID, which takes precedence over the synced value.
  • The sync is best-effort: a failure is logged at DEBUG level and skipped.

Canonical Inputs — CURRENT-only Filter

resolve_latest_artifact_version_refs() (called inside persist_summary_artifact()) only resolves CURRENT artifact versions as canonical inputs. DRAFT versions created during an in-flight revision are never included.

  • On first run, when no CURRENT version exists for a requested kind, that kind is simply absent from the returned dict. A DEBUG log line is emitted.
  • DRAFT versions that have not yet been promoted to CURRENT do not appear in any downstream artifact's canonical_inputs_version, so spurious staleness comparisons against in-flight draft IDs cannot occur.
  • The parent-version lookup in persist_summary_artifact() also filters by CURRENT, ensuring revision DRAFTs always link to the last accepted CURRENT rather than another DRAFT.

Key Code Locations

Concern File
Dependency graph schema mozaiksai/core/workflow/pack/schema.py — GlobalPackGraph.artifact_dependency_graph
Dependency graph data factory_app/workflows/extended_orchestration/extension_registry.json
Invalidation by ID mozaiksai/control_plane/invalidation.py — ArtifactInvalidationService; mozaiksai/core/artifacts/store.py — invalidate_artifact_version_refs(), mark_build_record_stale()
Stale-parent check mozaiksai/control_plane/implementations/coding_worker.py — ScopedRefinementCodingWorker._stale_parent_refusal()
Current build record refs query mozaiksai/core/artifacts/store.py — get_current_build_record_refs()
Lifecycle-status filter on list_artifact_versions mozaiksai/core/artifacts/store.py — lifecycle_status parameter
Current artifact lookup mozaiksai/core/artifacts/summary_artifacts.py — resolve_latest_artifact_version_refs()
Factory export inputs factory_app/workflows/_shared/export_inputs.py verifies the exact design and subscription records passed through the build, including reviewed drafts. A generator whose sequence includes the producing stage requires the saved record ID. When both are pinned, the subscription must identify that exact design in its own canonical inputs before downstream retry or export. A standalone generator can resolve an existing current input.
Version refs sync after run mozaiksai/core/session/router.py — advance_journey_after_run_complete()
Classifier prompt rules factory_app/refinement_harness/prompts/change_classifier_system.yaml
Lifecycle status enum mozaiksai/core/artifacts/models.py — ArtifactLifecycleStatus

Contributor Rules

  • affected_declarative_families on each workflow sequence must be accurate. It selects which named build records invalidation marks stale.
  • When adding a new artifact family, update artifact_dependency_graph in extension_registry.json and GlobalPackGraph in schema.py if needed.
  • Do not add staleness routing logic to harness.yaml. Routing classifies the request; ArtifactInvalidationService only records staleness by ID.
  • Do not add staleness logic to tools. Tools return data; the router and LLM reason.
  • Tests for invalidation by ID: tests/test_control_plane_invalidation.py
  • Tests for the store's invalidation methods: tests/test_artifact_store.py