# Second-pass thematic layers after a book digest completes

Use this pattern when a daily book cron reaches `status=done` after a first linear pass, but the user wants to continue deeper (e.g. “проходить слоями”, “раздевать каждую тему подробно”, “с цитатами и исследованиями”).

## Goal

Convert the completed first-pass pipeline into a second-pass thematic pipeline without asking the user to re-upload the book or creating a separate one-off job.

## Durable pattern

1. Keep the same project directory, manifest, state file, accumulating Markdown file, and cron job.
2. Extend the existing `next_fragment` script with a second phase:
   - If `next_fragment <= total_fragments`, emit the normal fragment as before.
   - If the first pass is complete, read `state.next_layer` and emit one thematic layer from a predefined `LAYER_PLAN`.
   - If all layers are complete, return `status=done` with suggestions for a third cycle (methodology, practices, content, productization), not a dead-end “everything is done”.
3. Build each `LAYER_PLAN` item with:
   - `number`
   - `title`
   - `focus`
   - `keywords` for selecting relevant paragraphs from the full text
   - `science_queries` for future web research
   - `cosmos_lens` or another user-specific application lens
4. Include a `--peek` mode for verification, and do not advance `next_layer` in peek mode.
5. Update the cron prompt so it knows about both phases and the JSON payload fields.
6. Enable the toolsets needed by the second pass. If the prompt asks for scientific articles / current research links, include `web` in addition to `file`.
7. Keep `attach_to_session=true` for conversational recurring digests the user may reply to.

## Prompt requirements for a deep thematic pass

The cron prompt should explicitly ask the future agent to:

- use `layer_title`, `layer_focus`, `source_excerpts`, `science_queries`, and the user-specific lens;
- quote only short fragments from the copyrighted source and paraphrase transformatively;
- search for 3–6 real scientific sources with links/DOI/PMID/arXiv when available;
- state boundaries honestly when research supports only part of the idea or the book uses a metaphor;
- save the full Markdown report to `output_path`;
- append a compact tools/questions/research/synthesis section to the accumulating file;
- make the final delivered message the full user-facing digest, without technical logs.

## Output shape for the script

Recommended JSON fields for a layer payload:

```json
{
  "status": "ok",
  "phase": "deep_thematic_layer",
  "book": "...",
  "layer_number": 1,
  "total_layers": 12,
  "layer_title": "...",
  "layer_focus": "...",
  "keywords": ["..."],
  "science_queries": ["..."],
  "cosmos_lens": "...",
  "source_excerpts_note": "Use these excerpts for close reading. Quote only short fragments from the copyrighted book; paraphrase the rest transformatively.",
  "source_excerpts": "...",
  "full_text_path": "/path/to/book_full.txt",
  "output_path": "/path/to/daily_outputs/book_layer_001.md",
  "accumulating_file": "/path/to/TOOLS_AND_INSIGHTS.md",
  "project_dir": "/path/to/project",
  "generated_at_utc": "..."
}
```

## Pitfalls

- Do not create a brand-new flat skill or cron job for every second pass if the existing daily-book pipeline can be extended.
- Do not let `status=done` become the final user experience when the user asks for a next layer; convert the state machine.
- Do not promise “citations from the book” as long excerpts. Use short quotes plus close reading.
- Do not ask for scientific links and leave the job with only the `file` toolset.
- Do not hard-code only one user’s ontology into the generic script; keep user-specific lenses in the layer data or prompt.
