# yaml-language-server: $schema=../automation-document.schema.json
# Dreaming: every night, read the Sessions since the last run, have the Curator find
# lessons, check they are safe and useful, and if Yes write them to the Support memory store.
# Create /lessons.md in the Support memory store once (it can be empty); each night updates the version it read.
# When the safety check says No, the run still ends normally, so those Sessions count as processed
# and their lessons are dropped. Wire the No exit somewhere visible if you want to review them.
# Fill the ids with --var:
#   checkfu automations create -f dreaming.yaml --var SUPPORT_BOT=prin_… \
#     --var SUPPORT_AGENT=agent_… --var CURATOR_AGENT=agent_… --var SUPPORT_MEMORY=memstore_… \
#     --var ENVIRONMENT=env_…
# Delete the environment_id line when the Workspace has exactly one Environment.
name: Nightly lessons for support
runs_as: "${SUPPORT_BOT}"
environment_id: "${ENVIRONMENT}"
blocks:
  - id: nightly
    type: trigger
    trigger: { type: schedule, cron: "0 3 * * *", timezone: UTC }
  - id: recent
    type: resource
    resource: { type: sessions, agent: "${SUPPORT_AGENT}", limit: 50 }
  - id: current
    type: resource
    resource: { type: memory_store, id: "${SUPPORT_MEMORY}", action: read, path: /lessons.md }
  - id: lessons
    type: agent
    agent: "${CURATOR_AGENT}"
    prompt: "Current lessons: {{current.content}} Add durable new lessons from these Sessions and return the full updated document: {{recent.sessions}}"
    output_schema:
      { type: object, properties: { markdown: { type: string } }, required: [markdown] }
  - id: safe
    type: model
    route: { profile: fast-judge }
    prompt: "Are these lessons safe, accurate and free of private data? {{lessons.output.markdown}}"
    format: { type: yes_no }
  - id: save
    type: resource
    resource:
      {
        type: memory_store,
        id: "${SUPPORT_MEMORY}",
        action: write,
        mode: update,
        path: /lessons.md,
        expected_sha256: "{{current.sha256}}",
        content: "{{lessons.output.markdown}}",
      }
wires:
  - { from: nightly, to: recent }
  - { from: recent, to: current }
  - { from: current, to: lessons }
  - { from: lessons, to: safe }
  - { from: safe, port: "yes", to: save }
