Spec and stack¶
This page is the API reference for Spec, Inputs, Credentials, Stack and HookDef. Field-by-field
behaviour per harness: Spec, Inputs, Credentials and Stacks.
Summary¶
Spec¶
Spec(harness, model, stack, cwd, credentials, harness_config={}, project_config="ignore", autonomy="restricted", unsafe=False, secrets={}, timeout_s=1800.0, budget_usd=None, output_schema=None, resumable=True, harness_builtins=False, idle_timeout_s=None, schema_repair=False, chrome=False)
Mutable dataclass describing what runs, where and how; validates everything in its constructor.
Inputs¶
Inputs(brief=None, msg=None, rules=None)
Frozen dataclass: the task for one run. Resolves brief / rules to absolute paths; needs a brief or a non-blank msg.
Credentials¶
| Member | Purpose |
|---|---|
Credentials.claude_oauth_token(token) -> Credentials |
claude, token from claude setup-token โ CLAUDE_CODE_OAUTH_TOKEN |
Credentials.anthropic_api_key(key) -> Credentials |
claude, API key โ ANTHROPIC_API_KEY |
Credentials.claude_host_login(path=None) -> Credentials |
claude, host login file copied into the run home (default ~/.claude/.credentials.json) |
Credentials.codex_auth_file(path=None) -> Credentials |
codex, auth.json copied into the run home (default ~/.codex/auth.json) |
Credentials.opencode_auth_file(path=None, providers=()) -> Credentials |
opencode, auth JSON (filtered) โ OPENCODE_AUTH_CONTENT |
Credentials.from_host(harness) -> Credentials |
the host login for any harness at its default path |
.opencode_auth_content() -> str |
the opencode auth file as JSON text, filtered to providers |
fields harness, kind, secret, path, providers |
frozen; secret hidden from repr() |
Stack¶
| Member | Purpose |
|---|---|
Stack.load(root) -> Stack |
load and validate a stack directory |
.ref -> str |
"<name>@<version>" |
.check_harness(harness) -> None |
raise StackIncompatible if harnesses excludes it |
.content_hash() -> str |
"sha256:<hex>" over the stack's files |
fields root, name, version, description, harnesses, default_models, agent_name, identity, skills, mcp_servers, hooks |
frozen dataclass |
HookDef¶
HookDef(event, command, tool=None, timeout_s=30.0), .as_dict() -> dict
Frozen dataclass: one neutral hook.
Full reference¶
Rendered from the docstrings (MkDocs site only).
Spec
dataclass
¶
Spec(harness: str, model: str, stack: Stack, cwd: Path, credentials: Credentials, harness_config: dict[str, Any] = dict(), project_config: str = 'ignore', autonomy: str = 'restricted', unsafe: bool = False, secrets: dict[str, str] = dict(), timeout_s: float = 1800.0, budget_usd: float | None = None, output_schema: dict | None = None, resumable: bool = True, harness_builtins: bool = False, idle_timeout_s: float | None = None, schema_repair: bool = False, chrome: bool = False)
What to run, where, and how: everything about a run except the task itself.
All validation happens in the constructor, so a Spec that exists is internally consistent.
cwd is resolved to an absolute path. Per-harness effects of each field are listed in
docs/concepts/spec-and-inputs.md and docs/concepts/combinations.md.
Attributes:
| Name | Type | Description |
|---|---|---|
harness |
str
|
|
model |
str
|
The model name passed to the harness ( |
stack |
Stack
|
The agent: identity, skills, MCP servers and hooks ( |
cwd |
Path
|
The agent's working directory. Must exist. |
credentials |
Credentials
|
How the run authenticates; must be for the same harness. |
harness_config |
dict[str, Any]
|
Extra settings deep-merged into the harness's own config file
( |
project_config |
str
|
|
autonomy |
str
|
|
unsafe |
bool
|
Must be |
secrets |
dict[str, str]
|
Values for |
timeout_s |
float
|
Wall-clock limit for the run, including writing the prompt. On expiry the
process tree is killed and the result has status |
budget_usd |
float | None
|
Cost cap. claude gets |
output_schema |
dict | None
|
JSON Schema for a structured final answer (claude |
resumable |
bool
|
Keep the run home after the run so its session can be resumed
( |
harness_builtins |
bool
|
Keep the harness's own bundled skills and plugins. Default |
idle_timeout_s |
float | None
|
Stop the run when the harness prints nothing for this many seconds
( |
schema_repair |
bool
|
With |
chrome |
bool
|
claude only: pass |
Raises:
| Type | Description |
|---|---|
UnknownHarness
|
|
InvalidSpec
|
Blank |
CwdNotFound
|
|
UnsafeAutonomy
|
|
UnsupportedFeature
|
|
MissingCredentials
|
|
StackIncompatible
|
The stack's |
MissingSecret
|
The stack references |
Example
spec = Spec(harness="claude", model="sonnet", stack=Stack.load("config"),
cwd="path/to/repo", credentials=Credentials.from_host("claude"))
Inputs
dataclass
¶
Inputs(brief: Path | None = None, msg: str | None = None, rules: Path | None = None)
The task for one run: a brief file, a message, and an optional rules file.
Paths are expanded (~) and resolved to absolute paths at construction. The brief and the
message become the first user message (on stdin); the rules file is appended to the system
prompt. See docs/concepts/prompt-assembly.md.
Attributes:
| Name | Type | Description |
|---|---|---|
brief |
Path | None
|
A task brief file of any name. Inlined as |
msg |
str | None
|
A free-form message. Inlined as |
rules |
Path | None
|
A binding rules file. Appended to the system prompt as
|
Raises:
| Type | Description |
|---|---|
InputNotFound
|
|
EmptyTask
|
Neither |
Example
Inputs(brief="task.md", rules="RULES.md", msg="focus on the parser")
Inputs(msg="List your skills.")
Credentials
dataclass
¶
Credentials(harness: str, kind: str, secret: str | None = None, path: Path | None = None, providers: tuple[str, ...] = ())
How one run authenticates.
Build it with one of the classmethod constructors rather than the raw dataclass. The value
names a credential kind; file-based kinds only remember the path, and the file is read when
the run is materialized (Dispatcher.plan / start / spawn) and again on every resume.
Each run gets the credential injected into its own environment or config home; the host's
login files are never modified.
Only kind is recorded in RunResult.credential_kind; secret is excluded from repr().
Attributes:
| Name | Type | Description |
|---|---|---|
harness |
str
|
The harness this credential is for ( |
kind |
str
|
One of |
secret |
str | None
|
The token or API key for the token kinds; |
path |
Path | None
|
The auth file for the file kinds; |
providers |
tuple[str, ...]
|
For |
Example
from phd import Credentials
creds = Credentials.from_host("claude") # ~/.claude/.credentials.json
creds = Credentials.claude_oauth_token(token) # token from `claude setup-token`
claude_oauth_token
classmethod
¶
claude_oauth_token(token: str) -> Credentials
Use a long-lived OAuth token from claude setup-token.
Preferred for claude: no refresh-token rotation, and it works on macOS where the host
login lives in the Keychain. Injected as CLAUDE_CODE_OAUTH_TOKEN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The token string. |
required |
Returns:
| Type | Description |
|---|---|
Credentials
|
A |
anthropic_api_key
classmethod
¶
anthropic_api_key(key: str) -> Credentials
Use an Anthropic API key for claude. Injected as ANTHROPIC_API_KEY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The API key. |
required |
Returns:
| Type | Description |
|---|---|
Credentials
|
An |
claude_host_login
classmethod
¶
claude_host_login(path: str | Path | None = None) -> Credentials
Use the host's claude auth login session.
The file is copied verbatim (byte for byte, refresh token included) into the run home as
.credentials.json (mode 0600 on POSIX) at materialize time and again on resume, and deleted
when the turn ends. PHD never writes to path. This is the full login that Spec.chrome
(claude --chrome) needs; the token kinds do not work with it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
The credentials file. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Credentials
|
A |
Raises:
| Type | Description |
|---|---|
MissingCredentials
|
The file does not exist (for example on macOS, where the login is
kept in the Keychain; use |
codex_auth_file
classmethod
¶
codex_auth_file(path: str | Path | None = None) -> Credentials
Use a codex auth.json (from codex login).
The file is copied verbatim (byte for byte) into the run home as auth.json (mode 0600 on POSIX) at
materialize time and again on resume, and deleted when the turn ends. PHD never writes to path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
The auth file. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Credentials
|
A |
Raises:
| Type | Description |
|---|---|
MissingCredentials
|
The file does not exist. |
opencode_auth_file
classmethod
¶
opencode_auth_file(path: str | Path | None = None, providers: tuple[str, ...] | list[str] = ()) -> Credentials
Use an opencode auth.json (from opencode auth login).
At materialize time the file is parsed, filtered to providers and passed to opencode as
the OPENCODE_AUTH_CONTENT environment variable; no auth file is written to the run home.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
The auth file. Defaults to |
None
|
providers
|
tuple[str, ...] | list[str]
|
Provider keys to keep (for example |
()
|
Returns:
| Type | Description |
|---|---|
Credentials
|
An |
Raises:
| Type | Description |
|---|---|
MissingCredentials
|
The file does not exist. |
from_host
classmethod
¶
from_host(harness: str) -> Credentials
Use the host CLI's own login at its default location.
Equivalent to claude_host_login(), codex_auth_file() or opencode_auth_file().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
harness
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
Credentials
|
The matching credential. |
Raises:
| Type | Description |
|---|---|
UnknownHarness
|
|
MissingCredentials
|
The host login file does not exist. |
opencode_auth_content ¶
opencode_auth_content() -> str
Return the auth file as JSON text, filtered to providers (used for opencode_auth_file).
Raises:
| Type | Description |
|---|---|
MissingCredentials
|
The file cannot be read or parsed, or lacks a requested provider. |
Stack
dataclass
¶
Stack(root: Path, name: str, version: str, description: str, harnesses: tuple[str, ...] | None, default_models: dict[str, str], agent_name: str, identity: str, skills: tuple[Path, ...], mcp_servers: dict[str, dict], hooks: tuple[HookDef, ...], store: StoreEntry | None = None)
A harness-neutral agent: one identity plus skills, MCP servers and hooks.
Load it from a config/ directory with Stack.load. A stack is read-only: every run copies
it into its own run home (home/stack/) and builds the harness config from it, so many runs
can share one stack. Layout and validation rules: docs/concepts/stacks.md.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
Path
|
Absolute path of the stack directory. |
name |
str
|
|
version |
str
|
|
description |
str
|
|
harnesses |
tuple[str, ...] | None
|
|
default_models |
dict[str, str]
|
|
agent_name |
str
|
File stem of the single |
identity |
str
|
Text of that file; becomes the start of the system prompt. |
skills |
tuple[Path, ...]
|
Skill directories under |
mcp_servers |
dict[str, dict]
|
|
hooks |
tuple[HookDef, ...]
|
Parsed |
store |
StoreEntry | None
|
Where the stack lives in the stack store, or None for Stack.load(dir). |
load
classmethod
¶
load(root: str | Path) -> Stack
Load and validate a stack directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str | Path
|
The stack directory ( |
required |
Returns:
| Type | Description |
|---|---|
Stack
|
The loaded stack. |
Raises:
| Type | Description |
|---|---|
StackInvalid
|
Missing or malformed |
HookUnsupported
|
A hook names an event outside the neutral set. |
Example
stack = Stack.load("tests/fixtures/stack")
stack.ref # 'fixture-helper@0.1.0'
check_harness ¶
check_harness(harness: str) -> None
Raise StackIncompatible if harnesses is set and excludes harness.
content_hash ¶
content_hash() -> str
Return a SHA-256 over every file in the stack, as "sha256:<hex>".
Files are visited in sorted order; each contributes its POSIX relative path and raw bytes
(no line-ending normalization). Skipped, relative to the stack root: anything named .git
or __pycache__ (a folder, so its files, or a file) and *.pyc files. These are what stack
assembly leaves out when it copies; other dot-folders and dot-files such as .mcp.json are
hashed. The dispatcher computes it at plan time and records it as RunResult.stack_hash.
HookDef
dataclass
¶
HookDef(event: str, command: str, tool: str | None = None, timeout_s: float = 30.0)
One neutral hook from hooks/hooks.json.
Attributes:
| Name | Type | Description |
|---|---|---|
event |
str
|
|
command |
str
|
A shell command line. May contain |
tool |
str | None
|
Neutral tool to match ( |
timeout_s |
float
|
Seconds the hook command may run. Default 30. |
as_dict ¶
as_dict() -> dict
Return the hook as a plain dict with keys event, command, tool, timeout_s.
Next: Dispatcher ยท See also: API overview, Stacks