setup:
  cleanup_on_exit: false
  default_mode: default
  env:
    pi_auth_json: RHO_PI_AUTH_JSON
  generate:
    dataset_uuid: uuid
  vars:
    owner: test-agent1
    agent_writer: test-agent2
  paths:
    sandbox_dir: "{{repo_root}}/sandbox/two-console-demo-agent"
    users_root: "{{sandbox_dir}}/users"
    shared_root: "{{sandbox_dir}}/shared"
    share_stage_dir: "{{users_root}}/{{owner}}/datasets/share/{{dataset_uuid}}"
    private_stage_dir: "{{users_root}}/{{owner}}/datasets/private/{{dataset_uuid}}"
    published_dir: "{{shared_root}}/datasets/{{dataset_uuid}}"
    workspace_dir: "{{shared_root}}/workspace"
    workspace_script: "{{workspace_dir}}/sum_prices.py"
    mock_csv: "{{published_dir}}/mock/prices-mock.csv"
    agent_prompt_path: "{{sandbox_dir}}/run/agent-write-workspace.prompt"
    agent_config_path: "{{sandbox_dir}}/run/agent-write-workspace.yaml"
  cleanup:
    - "{{sandbox_dir}}"

steps:
  - name: Create twin dataset
    actor: "{{owner}}"
    summary: Create private and share-stage copies of the twin dataset inside the sandbox.
    run: |
      ./rho-dataset \
        --name prices-demo-agent \
        --real "{{repo_root}}/examples/datasets/prices-real.csv" \
        --mock "{{repo_root}}/examples/datasets/prices-mock.csv" \
        --owner "{{owner}}" \
        --share-dir "{{users_root}}/{{owner}}/datasets/share" \
        --private-dir "{{users_root}}/{{owner}}/datasets/private" \
        --uuid "{{dataset_uuid}}" \
        --description "Agent-authored workspace script test"
    assert_files:
      - "{{share_stage_dir}}/dataset.yaml"
      - "{{private_stage_dir}}/dataset.yaml"

  - name: Publish mock twin
    actor: "{{owner}}"
    summary: Publish the mock dataset from the owner's share stage into shared space.
    run: |
      ./publish "{{owner}}" "{{dataset_uuid}}" \
        --source-root "{{users_root}}" \
        --target-root "{{shared_root}}/datasets"
    assert_files:
      - "{{published_dir}}/dataset.yaml"
      - "{{mock_csv}}"

  - name: Prepare agent-run files
    actor: test-runner
    summary: Render the Pi prompt and Gondolin config that tell the agent where /input and /output live.
    mkdirs:
      - "{{workspace_dir}}"
      - "{{sandbox_dir}}/run"
    render:
      - template: "{{assets_dir}}/agent-write-workspace.prompt.tmpl"
        to: "{{agent_prompt_path}}"
      - template: "{{assets_dir}}/agent-write-workspace.yaml.tmpl"
        to: "{{agent_config_path}}"
    assert_files:
      - "{{agent_prompt_path}}"
      - "{{agent_config_path}}"

  - name: Run Pi in Gondolin to write workspace script
    actor: "{{agent_writer}}"
    summary: Run Pi inside Gondolin so the agent reads /input and writes /output/sum_prices.py.
    run: ./rho agent-run --config "{{agent_config_path}}"
    inspect:
      - "{{agent_prompt_path}}"
      - "{{agent_config_path}}"
      - "{{workspace_dir}}"

  - name: Assert workspace script exists
    actor: test-runner
    summary: Confirm the generated Python file was written to the shared workspace.
    assert_files:
      - "{{workspace_script}}"

  - name: Run generated script against mock data
    actor: test-runner
    summary: Execute the generated script against the published mock CSV and verify the expected total.
    run: python3 "{{workspace_script}}" "{{mock_csv}}"
    capture: mock_output
    expect_stdout: "555.00"
