# Sermon Capture — Worker Contract
# Constructed by: Systems Architect specialist
# Dispatch path: Path C → Dorian → Systems Architect → Worker
# Version: 1.1 | 2026-05-24

worker_contract:
  task_id: ""  # injected by Dorian
  origin_thread: "telegram weekly sermon capture design review"

  objective: |
    Run the weekly sermon capture workflow for a dispatcher-supplied execution
    date: fetch the Church Center home page, extract downstream sermon resource
    URLs, fetch all available resource content, run exactly one combined LLM
    pass, create exactly one Notion Notes DB page under Area = Bible, and emit
    structured completion data for a separate Telegram relay.

  inputs:
    required:
      execution_date:
        description: "Sunday date in YYYY-MM-DD format"
        source: "Injected by cron dispatcher args or cron runner native date mechanism"
      church_center_url:
        description: "Church Center home page URL"
        default: "https://richfieldcc.churchcenter.com/home"
      NOTION_BIBLE_DB_ID:
        description: "Target Notion Notes DB data source id"
        source: "Preconfigured environment"
    prohibited:
      - "Do not derive execution_date inside the workflow via $(date ...) or similar shell evaluation"

  scope: |
    In scope:
      - Fetch Church Center home page HTML
      - Extract sermon notes, life group questions, and worship folder links
      - Resolve relative links against the Church Center base URL
      - Fetch all available downstream resource content
      - Persist temporary artifacts under /tmp for safe prompt transport
      - Run one combined LLM pass only
      - Validate/normalize LLM JSON output
      - Create one Notion page in the Notes DB with Area = Bible
      - Emit structured completion JSON for separate Telegram relay handling

    Out of scope:
      - Installing Lobster, Node, helpers, or any new packages
      - Sending Telegram messages directly
      - Modifying existing Notion pages
      - More than one Notion page creation
      - Environment mutation, auth changes, or connector reconfiguration

  constraints:
    - Hold for review only at this stage; this contract draft is not authorization to execute
    - Keep exactly one combined LLM call
    - Keep exactly one Notion page creation
    - Partial success is acceptable when some resource links are missing or some fetched resources are unsupported
    - Failure is required when no usable downstream sermon content is available at all
    - Do not modify NOTION_BIBLE_DB_ID or any environment variable
    - Do not send any user-facing output directly; only emit completion JSON
    - Do not respond to self-authored downstream webhook noise if page creation later triggers follow-on systems

  permissions:
    filesystem:
      level: "write-limited"
      allowed_paths:
        - "/tmp/weekly-sermon-*"
      purpose:
        - "home page fetch output"
        - "resource bundle staging"
        - "LLM input artifact staging"
        - "validated JSON staging"
        - "Notion payload staging"

    network:
      level: "external"
      allowed_targets:
        - "Church Center home page and discovered downstream sermon resources"
        - "Configured OpenClaw/Lobster LLM provider endpoint"
        - "Configured OpenClaw gateway endpoint for tool invocation"

    shell:
      level: "standard-non-destructive"
      allowed_operations:
        - "curl"
        - "node inline scripts / pipeline evaluation"
        - "cat"
        - "which"
        - "strings"
        - "pdftotext if already present"
      prohibited_operations:
        - "package installation"
        - "rm outside workflow scratch paths"
        - "git mutation"
        - "service/process reconfiguration"

    runtime:
      lobster:
        allowed: true
        modes:
          - "run steps"
          - "pipeline steps"
          - "stdin data passing"
      llm:
        allowed: true
        mode: "single combined call only"
        preferred_interface: "native Lobster llm_task.invoke / llm.invoke pipeline stage"
      openclaw_tool_invocation:
        allowed: true
        interfaces:
          - "openclaw.invoke for Notion create-pages call"
        note: "Requires gateway/auth context to be valid in the worker runtime"

    notion:
      level: "read-write-limited"
      allowed_actions:
        - "create one page in the configured Notes DB only"
      prohibited_actions:
        - "update existing pages"
        - "delete pages"
        - "schema changes"

    telegram: none
    git: none

  execution_assumptions: |
    Runtime assumptions that must be validated before deployment:
      - Lobster supports run/pipeline steps and stdin references as drafted
      - llm_task.invoke accepts stdin artifact input safely
      - openclaw.invoke is callable from the worker environment
      - Nested OpenClaw tool invocation from the chosen Lobster runtime has valid gateway auth context
      - The Notion data source schema matches Name / Area / userDefined:URL / Archive exactly

  model_recommendation: "openai-codex/gpt-5.3-codex"
  rationale: |
    This is execution-oriented workflow logic with shell/Node staging, one LLM
    pass, and one connector write. gpt-5.3-codex is an appropriate default for
    deterministic workflow execution and validation.

  rollback_required: false
  rollback_plan: |
    If a malformed Notion page is created during live execution, manual deletion
    in Notion UI is the rollback path. No automated rollback is defined because
    the action surface is intentionally limited to a single reversible page create.

  output_contract:
    stdout_json_schema: |
      {
        "status": "success|partial|failure",
        "execution_date": "YYYY-MM-DD",
        "source_url": "https://...",
        "page_url": "https://notion.so/...|unknown",
        "fetched_resources": ["sermonNotes", "lifeGroupQuestions", "worshipFolder"],
        "problem_resources": [
          { "name": "sermonNotes", "status": "missing|fetch_failed|unsupported_content", "warning": "..." }
        ],
        "warnings": ["..."],
        "scripture_count": 0,
        "question_count": 0,
        "notes_preview": "..."
      }

    semantics: |
      - success: page created and all targeted resources fetched into usable text
      - partial: page created, but one or more resource links were missing, failed, or were unsupported
      - failure: workflow could not produce a valid page or had no usable sermon source content

  done_criteria: |
    The task is done when all of the following are true:
      1. execution_date was injected by dispatcher input, not computed inside the YAML/workflow
      2. Church Center home page was fetched successfully
      3. Relative downstream links were normalized to absolute URLs
      4. At least one downstream resource produced usable text content
      5. Exactly one combined LLM call ran
      6. LLM output was validated into structured fields scripture / notes / questions
      7. Exactly one Notion page was created in the configured Notes DB with Area = Bible
      8. Structured completion JSON was emitted to stdout

systems_context:
  target_system: "Lobster workflow + OpenClaw gateway + Notion connector"
  draft_artifacts:
    - "sermon-automation.lobster"
    - "sermon-worker-contract.yaml"
  intended_locations:
    - "~/.openclaw/workflows/sermon-automation.lobster"
    - "~/.openclaw/workspace/contracts/sermon-worker-contract.yaml"
  deployment_status: "draft-only / hold-for-review"
