Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/developers/harness-tool-context.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: "Running the CLI from an agent harness"
description: "How an agent harness declares what it supports through HF_TOOL_CONTEXT, and what the CLI reports back."
---

An agent harness (Claude Code, Codex, your own) runs `hyperframes` commands on the agent's behalf.
`HF_TOOL_CONTEXT` lets the harness tell one invocation what it supports, so the CLI can do more for
it: report the images it produced, or wait its turn instead of racing another command in the same
project. Without the variable, every command behaves exactly as it does in a terminal.

```bash
export HF_TOOL_CONTEXT='{"version":1,"capabilities":{
"events": {"transport": {"type": "ndjson-file", "path": "/tmp/call-42/events.ndjson"}},
"project-lock": {"waitMs": 90000}
}}'
npx hyperframes snapshot --at 1,3
```

The harness sets the variable for one tool call, reads what the call produced, and removes its files.

## The declaration

| Field | Value |
| --- | --- |
| `version` | `1`. Any other version, or a value that is not JSON, is treated as no declaration. |
| `capabilities` | An object keyed by capability name. Each value configures that capability. |

The CLI ignores capability names it does not know, and a known capability whose value is invalid, so
a harness can declare capabilities newer than the CLI it runs. The variable carries data only: no
capability runs code the harness names.

## `events`: what a command produced

```json
{ "transport": { "type": "ndjson-file", "path": "/absolute/events.ndjson" } }
```

The CLI appends one JSON object per line to `path`. It never creates the parent directory, and a
write that fails prints a warning without failing the command. Read the file after the command
exits; several commands in one shell call append to the same file.

Each line is an event:

```json
{
"version": 1,
"type": "artifact.created",
"command": "snapshot",
"artifact": { "role": "contact-sheet", "path": "/project/snapshots/contact-sheet.jpg", "mediaType": "image/jpeg" }
}
```

| Command | `role` | What it is |
| --- | --- | --- |
| `snapshot` | `frame` | Each captured frame (PNG) |
| `snapshot` | `contact-sheet` | Each page of the grid of frames made for review (JPEG) |
| `check` | `frame` | Each frame the browser check captured |
| `check` | `finding` | Each crop around a finding |

Paths are absolute and stay where the command saved them. What to do with an artifact is the
harness's decision: attach the contact sheet to the tool result, or ignore it.
Ignore event types and roles you do not know; new ones are added without a version change.

## `project-lock`: one browser command at a time

```json
{ "waitMs": 90000 }
```

`check` and `snapshot` take a lock on the project before they start a browser, and release it when
they finish. A second command waits up to `waitMs` milliseconds for the lock, then fails, naming the
command and process that hold it. A lock whose process has exited is taken over. A
`project.lock.break` left by a killed process is not: the timeout names that file and its process
id, and says to delete it to continue. The lock is the file `.hyperframes/project.lock`.

Choose `waitMs` below your tool call's own timeout, so a wait ends in the CLI's message rather than
the harness killing the call.

## Hooks that run code

Running your own command before or after a CLI command is not a `HF_TOOL_CONTEXT` capability and
never will be: anything that sets an environment variable could then run code. Such hooks, when they
exist, come only from configuration the person controls.

## Conformance

`packages/cli/src/utils/toolContext.test.ts` and `projectLock.test.ts` pin the parsing, event and
lock behavior above. A harness is correct when it sets the variable for each call it wants the
behavior on, reads `events` only after the call exits, and works the same when the CLI ignores a
capability.
1 change: 1 addition & 0 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@
"developers/cli",
"packages/cli",
"developers/catalog-search",
"developers/harness-tool-context",
"packages/lint"
]
},
Expand Down
32 changes: 31 additions & 1 deletion packages/cli/src/commands/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { runCommand } from "citty";
import { readFileSync } from "node:fs";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";

const trackCheckReport = vi.fn();
Expand Down Expand Up @@ -530,6 +532,34 @@ it("threads --no-proxy into the browser check options", async () => {
expect(runPipeline).toHaveBeenCalledWith(PROJECT, expect.objectContaining({ autoProxy: false }));
});

it("waits for the project lock declared in HF_TOOL_CONTEXT before running the pipeline", async () => {
const dir = mkdtempSync(join(tmpdir(), "hf-check-lock-"));
const lock = join(dir, ".hyperframes", "project.lock");
mkdirSync(join(dir, ".hyperframes"));
writeFileSync(lock, JSON.stringify({ pid: process.pid, command: "snapshot", startedAt: 1 }));
vi.stubEnv(
"HF_TOOL_CONTEXT",
JSON.stringify({ version: 1, capabilities: { "project-lock": { waitMs: 0 } } }),
);
const { report } = await runScenario(fakeDriver());
const runPipeline = vi.fn(async () => report);
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
const command = createCheckCommand({
resolveProject: () => ({ ...PROJECT, dir }),
runPipeline,
withMeta: (value) => value,
});
try {
await runCommand(command, { rawArgs: ["--json"] });
expect(runPipeline).not.toHaveBeenCalled();
expect(consumeCommandResult().exitCode).toBe(1);
expect(String(log.mock.calls[0]?.[0])).toContain("hyperframes snapshot");
} finally {
vi.unstubAllEnvs();
rmSync(dir, { recursive: true, force: true });
}
});

it("rejects malformed caption-zone specs instead of silently disabling the gate", async () => {
const { report } = await runScenario(fakeDriver());
const runPipeline = vi.fn(async () => report);
Expand Down
15 changes: 13 additions & 2 deletions packages/cli/src/commands/check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { resolve } from "node:path";
import { formatFindingTimes } from "../utils/checkFindings.js";
import { withProjectLock } from "../utils/projectLock.js";
import { reportArtifacts } from "../utils/toolContext.js";
import { defineCommand, parseArgs } from "citty";
import type { ArgsDef } from "citty";
import type { Example } from "./_examples.js";
Expand Down Expand Up @@ -37,7 +40,13 @@ export interface CheckCommandDependencies {

const DEFAULT_COMMAND_DEPENDENCIES: CheckCommandDependencies = {
resolveProject,
runPipeline: runCheckPipeline,
async runPipeline(project, options) {
const report = await runCheckPipeline(project, options);
const images = (files: readonly string[]) => files.map((file) => resolve(project.dir, file));
reportArtifacts("check", "frame", images(report.snapshots.files));
reportArtifacts("check", "finding", images(report.snapshots.findingFiles));
return report;
},
withMeta,
};

Expand Down Expand Up @@ -148,7 +157,9 @@ export function createCheckCommand(
if (!asJson) {
console.log(`${c.accent("◆")} Checking ${c.accent(project.name)}`);
}
const report = await dependencies.runPipeline(project, options);
const report = await withProjectLock(project.dir, "check", () =>
dependencies.runPipeline(project, options),
);
if (asJson) {
console.log(JSON.stringify(dependencies.withMeta(report), null, 2));
} else {
Expand Down
Loading
Loading