import { defineTask } from "../../_vendor/ailf-core/index.js"

/**
 * The first realistic agent-harness task, run against the vendored
 * `nextjs-personal-website` template fixture.
 *
 * The agent must add an array-of-strings `technologies` field to the
 * `project` document type. That field is genuinely absent from the fixture
 * (existing fields: title, slug, overview, coverImage, duration, client,
 * site, tags, description), so the static assertions only pass if the agent
 * actually performs the edit. It exercises real Sanity-docs knowledge:
 * `defineField` plus an array-of-primitive (`of: [{type: 'string'}]`).
 *
 * Safety (audit issue 1): runs currently execute in a host tempdir with
 * `bypassPermissions` and full Bash, with no container boundary. This task is
 * deliberately file-edit-only — it has NO `command-succeeds` assertion — to
 * keep the host blast radius minimal until Docker isolation is wired
 * (W-agent-harness-sandbox-wiring). The prompt asks the agent not to run
 * build/install commands.
 *
 * @see docs/design-docs/agent-harness-traces-and-fixtures.md §6 — the task
 * @see packages/eval/fixtures/templates/nextjs-personal-website — the fixture
 */

/** The schema file the agent edits, relative to the sandbox working dir. */
const PROJECT_SCHEMA_PATH = "sanity/schemas/documents/project.ts"

export default [
  defineTask({
    mode: "agent-harness",
    id: "add-project-technologies",
    title: "Add a technologies field to the project schema",
    description:
      "Given the vendored Next.js personal-website template, the agent must " +
      "add a `technologies` field (an array of strings) to the `project` " +
      "document type, following the Sanity schema conventions already used " +
      "in the file. The field does not exist yet, so the edit is required " +
      "for the assertions to pass.",
    area: "studio",
    difficulty: "basic",
    tags: ["agent", "schema", "studio"],
    sandbox: {
      type: "tempdir",
    },
    tools: ["coding"],
    context: {
      fixtures: ["file://fixtures/templates/nextjs-personal-website/"],
    },
    prompt: {
      text:
        `In this Next.js + Sanity project, add a new field named ` +
        `"technologies" to the \`project\` document type defined in ` +
        `${PROJECT_SCHEMA_PATH}.\n\n` +
        `Requirements for the field:\n` +
        `- Name it \`technologies\`.\n` +
        `- Make it an array of strings — the technologies and tools used on ` +
        `the project (for example "Next.js", "Sanity", "TypeScript").\n` +
        `- Define it with \`defineField\` and give it an editor-friendly ` +
        `title and a short description.\n` +
        `- Follow the existing Sanity schema conventions already used in ` +
        `that file (\`defineField\`, \`of: [{type: 'string'}]\`, single ` +
        `quotes, etc.).\n` +
        `- Add it to the \`project\` type's \`fields\` array.\n\n` +
        `Edit only the schema file. Do not run build, install, or test ` +
        `commands.`,
    },
    assertions: [
      {
        type: "file-contains",
        value: { path: PROJECT_SCHEMA_PATH, content: "name: 'technologies'" },
      },
      {
        type: "file-contains",
        value: { path: PROJECT_SCHEMA_PATH, content: "defineField" },
      },
      {
        type: "file-contains",
        value: { path: PROJECT_SCHEMA_PATH, content: "type: 'array'" },
      },
      {
        // Templated LLM rubric → the agent-harness `agent-output` dimension.
        // The agent-harness assertion compiler resolves this via the shared
        // rubric-resolution module (assertions.ts → resolveTemplatedAssertion).
        type: "llm-rubric",
        template: "agent-output",
        criteria: [
          "A field named `technologies` was added to the project document's `fields` array",
          "The field is an array of strings (`of: [{type: 'string'}]`, or a defineArrayMember string member)",
          "It is declared with `defineField` and has a sensible, editor-friendly title",
          "It follows the existing Sanity schema conventions used elsewhere in the file",
        ],
      },
    ],
    options: {
      timeout: 120_000,
    },
  }),
]
