- Find the highest numbered story file in `docs/stories/`, ensure it is marked done OR alert user.
- **If a highest story file exists ({lastEpicNum}.{lastStoryNum}.story.md):**
- Review this file for developer updates/notes.
- Check `docs/epic{lastEpicNum}.md` for a story numbered `{lastStoryNum + 1}`.
- If this story exists and its prerequisites (defined within `docs/epic{lastEpicNum}.md`) are 'Done': This is the next story.
- Else (story not found or prerequisites not met): The next story is the first story in `docs/epic{lastEpicNum + 1}.md` (then `docs/epic{lastEpicNum + 2}.md`, etc.) whose prerequisites are 'Done'.
- **If no story files exist in `docs/stories/`:**
- The next story is the first story in `docs/epic1.md` (then `docs/epic2.md`, etc.) whose prerequisites are 'Done'.
- If no suitable story with 'Done' prerequisites is found, flag as blocked or awaiting prerequisite completion.
- Store original epic requirements for later comparison.
3.**Gather Technical Context:**
- **Ancillary Docs:** Consult `docs/index.md` for relevant, unlisted documents. Note any that sound useful.
- **Architecture:** Comprehend `docs/architecture.md` (and `docs/front-end-architecture.md` if UI story) for task formulation. These docs may reference others.
- **Content Extraction:** From standard refs (`docs/tech-stack.md`, `docs/api-reference.md`, `docs/data-models.md`, `docs/environment-vars.md`, `docs/testing-strategy.md`, `docs/ui-ux-spec.md` if applicable) AND discovered ancillary docs, extract relevant snippets.
- (Dev Agent has direct access to full `docs/project-structure.md`, general `docs/coding-standards.md`. Note specific `docs/front-end-coding-standards.md` details if relevant and not universally applied by Dev Agent).
- Review notes from previous 'Done' story, if applicable.
- **Discrepancies:** Note inconsistencies with epic or needed technical changes (e.g., to data models, architectural deviations) for "Deviation Analysis."
- **Detailed Tasks:** Generate based on architecture, epic. For UI stories, also use `docs/style-guide.md`, `docs/component-guide.md`, and `docs/front-end-coding-standards.md`.
- **Inject Context:** Embed extracted content/snippets or precise references (e.g., "Task: Implement `User` model from `docs/data-models.md#User-Model`" or copy if concise).
- **UI Stories Note for Dev Agent:** "Consult `docs/style-guide.md`, `docs/component-guide.md`, and `docs/front-end-coding-standards.md` for UI tasks."
- Detail testing requirements. Include project structure alignment notes.
- Prepare noted discrepancies (Step 4) for "Deviation Analysis."
6.**Deviation Analysis:**
- Compare story with original epic. Document deviations (ACs, requirements, implementation, structure).
- If deviations, add "Deviations from Epic" section detailing: original, modified, justification, impact.
7.**Generate Output:**
- Save to `docs/stories/{epicNumber}.{storyNumber}.story.md`. Set `Status: Draft`.
8.**Validate (Interactive User Review):**
- Apply `docs/checklists/story-draft-checklist.md` to draft story.
- Ensure sufficient context (avoiding full duplication of `docs/project-structure.md` and `docs/coding-standards.md`).
- Verify project structure alignment. Resolve gaps or note for user.
- If info missing agent can't derive, set `Status: Draft (Needs Input)`. Flag unresolved conflicts.