# Spec: One Autofill module with honest outcomes

Status: resolved

## Problem Statement

When I click an Autofill Runnable on the Floating Action Bar, it often tells me it "executed successfully" even when it left part of the Survey unanswered. A drifted question label, a Store that has no Store Preset, or a write that didn't take all get swallowed into the console, and the console is cleared on my next click. I only find out by scrolling through the Survey and spotting blanks.

When an answer early in a chain fails, the Dependent Answers that rest on it are dropped, while unrelated later questions still get filled. That leaves gaps scattered through the Survey that take real effort to reason about. A deliberate Skipped Question and a real error look the same in the logs.

The beauty Runnables and the Vital Farms Runnable each have their own fill-engine, and they identify the Store in different ways (full Store Label vs Store Number). Adding a new Store or a new Runnable means knowing which convention applies where.

## Solution

There is one Autofill module that every Autofill Runnable uses. A Runnable hands it a Store Preset table and a list of Answer Preset sets. The module then:

- looks up the current Store by its Store Number,
- refuses to start if the Store isn't configured or a preset set is wired wrong, before touching the Survey,
- computes every answer, including Dependent Answers, from its own state rather than reading the page back,
- writes the answers to the Survey,
- reports, per question, whether it was filled, skipped, blocked or failed.

If any question failed, or the run couldn't start, the Floating Action Bar shows the run as an error rather than a success. The console shows a clear log per question and one summary line.

## User Stories

1. As a merchandiser, I want an Autofill run to show as an error on the Floating Action Bar when any question failed, so that I don't submit a half-filled Survey thinking it is complete.
2. As a merchandiser, I want an Autofill run to stop before answering anything when the current Store has no Store Preset, so that I never get a Survey filled with values meant for a different Store or with nothing at all.
3. As a merchandiser, I want the error for an unconfigured Store to name the Store Number it looked for, so that I know which Store Preset to add.
4. As a merchandiser, I want a run to stop before answering anything when a Dependent Answer depends on a question key that doesn't exist in its preset set, so that a wiring mistake is caught up front instead of producing scattered gaps.
5. As a merchandiser, I want a run to stop before answering anything when a Dependent Answer depends on a question that comes after it, so that ordering mistakes (including fractional question keys) are caught up front.
6. As a merchandiser, I want Dependent Answers to be computed from the values the Autofill just produced, so that a dependency that failed to write doesn't silently turn its dependents into blanks.
7. As a merchandiser, I want the questions that depend on a failed question to be left unanswered, so that I never have a later answer resting on a missing earlier one.
8. As a merchandiser, I want questions that don't depend on a failed question to still be answered, so that one bad label doesn't cost me the whole Survey.
9. As a merchandiser, I want a question left unanswered because its dependency failed to be reported as a Blocked Question, so that I can tell it apart from a deliberate skip.
10. As a merchandiser, I want each Blocked Question's log to name the failed question it was waiting on, so that I fix the root cause and not the symptom.
11. As a merchandiser, I want questions that depend on a Skipped Question to be skipped as well, so that deliberate skips don't show up as errors.
12. As a merchandiser, I want Skipped Questions logged at a normal level, failures logged as errors and Blocked Questions logged as warnings, so that the console's colours tell me where to look.
13. As a merchandiser, I want one summary line at the end of a run with the counts of filled, skipped, blocked and failed questions, so that I can see at a glance how the run went.
14. As a merchandiser, I want a failed question's log to include its question label and the cause, so that I can find it on the page and understand what went wrong.
15. As a merchandiser, I want a beauty Runnable that covers several Beauty Categories to run as a single Autofill run with one combined outcome, so that the Floating Action Bar reports once per click.
16. As a merchandiser, I want the boilerplate questions shared across beauty Projects to be part of that same single run, so that their failures are reported alongside the rest.
17. As a merchandiser, I want checkbox questions to accept a list of choices, even when only one is ticked, so that multi-select questions are answered correctly.
18. As a merchandiser, I want already-checked checkboxes left alone when the Autofill answers a checkbox question, so that existing selections aren't undone.
19. As a merchandiser, I want a Store's Vital Farms answers that are intentionally left out (e.g. no backstock location) to be treated as Skipped Questions, so that they don't show up as failures.
20. As a merchandiser, I want the Vital Farms Runnable to behave the same way as the beauty Runnables (same up-front checks, same outcome reporting), so that I only have to learn one behaviour.
21. As the maintainer, I want every Store Preset table keyed by Store Number, so that I add a Store the same way for every Runnable.
22. As the maintainer, I want a Direct Answer to receive the current Store's Store Preset directly, so that Answer Presets don't need to index global tables themselves.
23. As the maintainer, I want Answer Presets to stay plain declarative data that returns a value, or nothing to skip, so that writing a new preset doesn't require learning error-handling machinery.
24. As the maintainer, I want an Answer Preset that throws to be recorded as a failed question rather than crashing the run, so that one buggy preset doesn't stop unrelated questions.
25. As the maintainer, I want one question-type vocabulary (text, radio, checkbox) across all Runnables, so that I don't maintain parallel type lists.
26. As the maintainer, I want the types to stop me from giving a list of values to a text or radio question, or a single value to a checkbox question, so that mistakes are caught at build time.
27. As the maintainer, I want the Vital Farms answers expressed as Answer Presets with numeric keys, so that there is one preset-set convention.
28. As the maintainer, I want the Autofill module to take the Survey as a dependency it is handed, so that I can test the Controller without the RMS page.
29. As the maintainer, I want tests that drive the Autofill module with an in-memory Survey and fixed presets, so that Dependent Answer bounds and chain behaviour are verified without randomness or a DOM.
30. As the maintainer, I want the Autofill module to report configuration problems as a distinct error result, separate from a run's per-question outcome, so that callers can tell "couldn't start" from "ran with problems".
31. As the maintainer, I want the two existing fill-engines deleted once Runnables use the Autofill module, so that there is only one place to fix fill behaviour.
32. As the maintainer, I want the Runnable-to-Floating-Action-Bar contract left unchanged for now, so that the UPC Pickers don't need to change in this piece of work.
33. As a future agent working on this codebase, I want the Autofill module to follow the MVC shape recorded in ADR-0001, so that I don't reintroduce page reads into the Controller.

## Implementation Decisions

- **New Autofill module.** A new `autofill` module at the top of the source tree owns the whole pipeline: Store lookup, configuration checks, answer resolution, Survey writes, pacing between writes, outcome collection and logging. It replaces both existing fill-engines (beauty and Vital Farms), which are deleted.
- **MVC shape (ADR-0001).** Answer Presets and Store Presets are the Model. The Autofill module is the Controller. The Survey page, reached only through the existing `Survey` module, and the Floating Action Bar are the Views.
- **Interface, in prose.** One entry point takes:
  - the Survey, handed in as a dependency (production passes the real `Survey` instance),
  - the Runnable's Store Preset table, keyed by Store Number,
  - an ordered list of Answer Preset sets.

  It returns a `ResultAsync` (neverthrow) of either a run outcome or a configuration error. The current Store Number is read once at the start of the run.
- **Configuration error** (returned as `err`, before any write):
  - no Store Preset for the current Store Number,
  - a Dependent Answer depends on a key missing from its preset set,
  - a Dependent Answer depends on a key that comes at or after it in execution order.
- **Run outcome** (returned as `ok`): the questions grouped as filled, skipped, blocked and failed, each with enough to identify the question (label, and section name where present). A blocked entry names the failed question it waited on. A failed entry carries its cause.
- **Store identity.** Store Number (the "T" plus four digits, uppercased) is the one Store key everywhere. The beauty Store Preset table is re-keyed from full Store Labels to Store Numbers. The existing helper that extracts the Store Number from the Store Label is reused.
- **Direct Answers** receive the current Store's Store Preset (not a Store key) and return a value, or `null` to skip.
- **Dependent Answers** receive the values the module itself computed for their dependencies, in dependency-array order, and return a value or `null` to skip. They no longer read the page.
- **Dependency scope.** Dependencies resolve only within the same Answer Preset set. Numeric keys are reused across sets. Named dependencies are out of scope.
- **Outcome propagation**:
  - preset returns `null` → Skipped Question, and every question that depends on it (directly or transitively) is also skipped,
  - preset throws, or the Survey write throws → failed, and every question that depends on it (directly or transitively) becomes a Blocked Question,
  - questions with no failed or skipped ancestor run normally.
- **neverthrow usage.** neverthrow is added as a runtime dependency. It is used inside the Autofill module only, wrapping preset calls and Survey writes, and as its return type. Answer Presets do not return `Result`s.
- **Question types.** One union for every Runnable: text, radio, checkbox. The answer type is tied to the question type: checkbox answers are always a list of strings, and text and radio answers are always a single value. The module maps each question type to the matching `Survey` write method, so callers never choose a write method.
- **Vital Farms conversion.** The Vital Farms questions and per-Store answers become a preset set of Direct Answers with numeric keys, plus a Vital Farms Store Preset table keyed by Store Number. A value intentionally missing for a Store makes its Direct Answer return `null`, which gives a Skipped Question.
- **Beauty Runnables** each make one Autofill call with their full list of preset sets, including the boilerplate set where they include it today. They do not make one call per set.
- **Runnable contract unchanged.** Runnables still return `Promise<void>` to the Floating Action Bar. An Autofill Runnable throws when the module returns a configuration error, or when the outcome has any failed question, so the Floating Action Bar shows its existing error state.
- **Logging.** Failed → `console.error` with label and cause. Blocked → `console.warn` naming the failed dependency. Skipped → `console.log`. One summary line with the four counts at the end. Clearing the console at click start stays as is.
- **Defaults taken without explicit confirmation**:
  - Vital Farms moves to numeric keys, rather than allowing named keys for sets with no dependencies.
  - A missing per-Store value is a Skipped Question.

  Both were the recommended answers in the design session. Revisit if the maintainer disagrees.

## Testing Decisions

- **One seam: the Autofill module's interface.** Tests hand it an in-memory Survey fake and fixed Store Presets and Answer Presets (no `randInt`), then assert on the returned result and on what the fake recorded as written. Production uses the real `Survey`, so there are two adapters at this seam.
- **What makes a good test here:** it exercises behaviour through the module's interface only, meaning which questions got written with which values and what outcome came back. It does not assert on internal helpers, call order of private functions, or log text beyond what a user would rely on.
- **Behaviours to cover**:
  - Direct Answers written with the Store's preset values,
  - Dependent Answer receives computed dependency values in order,
  - Dependent Answer bounded by its dependency (e.g. After count never exceeds Before count),
  - Skipped Question propagates to dependents as skipped,
  - failure propagates to dependents as blocked, and blocked entries name the failed question,
  - unrelated questions still filled after a failure,
  - Survey write throwing becomes failed,
  - preset throwing becomes failed,
  - unknown Store returns configuration error with no writes,
  - missing dependency key returns configuration error with no writes,
  - dependency at or after its dependent returns configuration error with no writes,
  - checkbox list written as each choice,
  - multiple preset sets in one run produce one combined outcome,
  - numeric keys reused across sets don't collide.
- **No DOM environment is needed.** The vitest config stays as is.
- **Prior art:** the only existing test validates the ICU UPC list with plain vitest assertions. These are the first behaviour tests, and they set the pattern.

## Out of Scope

- How the Floating Action Bar *displays* a run outcome (counts, failed labels). This is the View side of the outcome work. For now the Floating Action Bar only reflects success vs error through its existing state.
- Changing the Runnable-to-Floating-Action-Bar contract, and the UPC Pickers.
- Named dependencies between Answer Presets, and any other renumbering or restructuring of preset files beyond re-keying the Store Preset table and converting Vital Farms.
- Deepening the `Survey` module, consolidating RMS selectors, and DOM-fixture tests.
- Declarative Runnable registration and the overlapping Project title patterns.
- Runnable mount lifecycle.
- Reviewing whether existing presets give reasonable answers (CLAUDE.md Task 1).

## Further Notes

- Glossary terms used and recently added: Store Number, Store Label, Floating Action Bar, Blocked Question, Skipped Question, Store Preset, Answer Preset, Direct Answer, Dependent Answer.
- ADR-0001 (MVC shape for Autofill) records why the Controller must not read answers back from the page.
- Today's preset sets contain no fractional keys, but question keys are allowed to be fractional. The up-front ordering check is what protects against `Object.entries` placing non-integer keys after integer ones.
- Work lands on `dev`. Never touch `master`.
