Headless and scheduled agent runs: what a cron or CI agent starts with, and where last night's result has to live
A run with nobody watching starts from what is on that machine: no terminal to ask, often no home directory, no memory of the run before. What Claude Code, Codex and Cursor document about instruction files, MCP servers, approvals, harness memory and sessions in non-interactive and scheduled runs, two small tests we ran, a 15-minute drill and where a nightly job has to keep what it found.
A nightly job triages failing tests. You wrote it on your laptop: claude -p with a prompt file, a memory server you added last month so the agent could look up what it had already reported, and a line in your own CLAUDE.md saying the payments migration tests are quarantined until the 12th. On the laptop it behaved. On Monday you moved it to CI. By Friday it had opened the same issue about the same flaky test five times and had "fixed" a quarantined test twice. Every run exited 0.
Nothing was wrong with the prompt. The runner had no home directory with your CLAUDE.md in it, the memory server was an entry in a config file on your laptop, and Tuesday's run had no way to know what Monday's run found. The CLI did what its documentation says it does: it started with what was there and carried on.
This guide puts side by side what Claude Code, Codex and Cursor document about a run with nobody at the keyboard, shows two small tests we ran, gives you a drill to see what your own job starts with, and shows where the result of one run has to be written so the next one reads it.
Three places an unattended run can execute
- A cron entry or a timer on a machine you own. Your home directory is there, so the run finds your user-level instruction file, the MCP servers you added, your saved login, the harness's own memory and yesterday's session files.
- A CI runner. A fresh machine or container. It has the checkout and what the job installs. Nothing from anyone's home directory.
- The vendor's scheduler. A fresh clone or a fresh chat on the vendor's side. It has what is committed to the repository and what is attached to the vendor account. Nothing from your local configuration.
The same prompt gives three different agents. Most jobs are written in the first place and moved to the second or third.
What each harness documents
| Claude Code | Codex | Cursor | |
|---|---|---|---|
| Instruction files | claude -p loads what an interactive session would, "including anything configured in the working directory or ~/.claude". --bare skips CLAUDE.md, and the docs say bare mode "will become the default for -p in a future release". In GitHub Actions the root CLAUDE.md is read "on every run" | The instruction chain is built "once per run": one global file from the Codex home (~/.codex by default), then the files from the project root down to the working directory, 32 KiB combined by default | The CLI applies .cursor/rules like the editor and "also reads AGENTS.md and CLAUDE.md at the project root" |
| MCP servers | Without --bare: servers from every scope. A -p run cannot show the approval prompt for project servers in .mcp.json, so "it loads project-scoped servers without asking". With --bare: only what you pass with --mcp-config. A server added with claude mcp add at the default scope is stored in ~/.claude.json on that machine | ~/.codex/config.toml, or a project .codex/config.toml "(trusted projects only)". codex exec loads project-defined servers "only for trusted projects"; --trust saves trust. --ignore-user-config skips the user file | .cursor/mcp.json in the project or ~/.cursor/mcp.json. A server has to be on the approved list (agent mcp enable, or --approve-mcps), and print mode rejects "shell commands, MCP tools, and web fetches that aren't allowed" |
| A server that does not load | An invalid --mcp-config entry is skipped: "The run continues and exits cleanly". When stderr is captured, the only trace is mcp_server_errors in the system/init event. A server that loaded has a status there: pending, connected, failed, needs-auth or disabled | A server with required = true that fails to initialize makes codex exec exit with an error "instead of continuing without that server" | Not stated for the CLI. For cloud Automations: when a server rejects the automation's credentials, "the run continues without that server's tools" and the run page shows an event |
| Nobody to approve | From v2.1.302, a -p run starts in Manual mode when nothing sets a mode: a call that needs approval is denied and the run goes on. --permission-prompts none tells Claude nobody can answer. Denials are listed in permission_denials on the final result | codex exec runs in a read-only sandbox by default; --sandbox workspace-write allows edits. Scheduled tasks in the app "run unattended with your default sandbox settings" | Print mode "can't stop to ask you": workspace edits run, anything that would need approval is rejected. --force runs everything except what permissions.deny and team policies block |
| The harness's own memory | Auto memory is "machine-local" and files "are not shared across machines or cloud environments". --bare skips it | Local memories are off by default and live under the Codex home. The memories page itself says: "Keep required team guidance in AGENTS.md or checked-in documentation" | Automations have a Memories tool: notes "across runs for the same automation", on by default, with a warning for automations that handle untrusted input. Nothing like it is documented for agent -p |
| Yesterday's session | A new session per run unless you pass --continue or --resume. Session files are on that machine's disk; --no-session-persistence writes none | codex exec resume --last or resume <SESSION_ID>. --ephemeral writes no session files | --resume [chatId] or --continue |
| The vendor's scheduler | Cloud scheduled tasks: each repository is "cloned on every run", the run is "a new one for each run" and the prompt "must be self-contained". MCP comes from your claude.ai connectors or a committed .mcp.json; servers added with claude mcp add "do not appear in the connectors list". Desktop tasks run on your machine and start "a fresh session", only while the app is open and the computer awake | A standalone scheduled task starts "a new chat for each scheduled run". One scheduled inside a chat returns to that chat. On the web, tasks "don't keep a local folder or worktree available between runs" | Automations run cloud agents. Hosted cloud agents "don't read ~/.cursor/mcp.json"; they use the servers configured at cursor.com/agents and the team's servers |
Two rows deserve a second read.
Instruction files. All three load the project file from the checkout, so a rule committed to the repository reaches the job wherever it runs. The user-level file reaches it only on your own machine. And Claude Code's recommended mode for scripts loads neither: "In CI or other scripted environments, add --bare so Claude Code starts without loading the host's hooks, plugins, auto memory, or CLAUDE.md". In bare mode you pass the file yourself with --append-system-prompt-file.
A server that does not load. This is the row that produces the green job with the wrong result. Codex has a switch that turns a missing server into a failed run. Claude Code reports it in the first event of the output stream and exits cleanly. An agent that cannot reach its memory does not stop: it answers from the repository alone, and that answer looks the same in the job log.
Two small tests
We ran the Claude Code case, on version 2.1.329, with an MCP config holding two broken entries: one with a url and no type, one pointing at a local port that refuses connections.
claude -p "Reply with the single word ok." \
--mcp-config ./mcp-bad.json --strict-mcp-config \
--permission-prompts none --no-session-persistence \
--output-format stream-json --verbose > run.jsonl 2> run.err
echo "exit=$?" # exit=0The system/init event, cut to the MCP fields, and the final result:
{"mcp_servers":[{"name":"unreachable","status":"failed","source":"dynamic"}],
"mcp_server_errors":[{"name":"no-type","type":"url_missing_type",
"message":"Skipped — MCP server \"no-type\" has a \"url\" but no \"type\"; add \"type\": \"http\" (or \"sse\" / \"ws\") to this entry"}]}
{"subtype":"success","is_error":false,"result":"ok","permission_denials":[]}Neither server was there, the result was success and the exit code was 0. Nothing about the servers was in run.err.
For Codex (codex-cli 0.156.1) we passed the same unreachable server on the command line, once with required = true and once without:
codex exec --ephemeral --skip-git-repo-check \
-c 'mcp_servers.unreachable.url="http://127.0.0.1:9/mcp"' \
-c 'mcp_servers.unreachable.required=true' \
"Reply with the single word ok."
echo "exit=$?" # exit=1With required = true the run stopped at startup with the MCP initialize error and made no model request. Without it, the run went past the dead server to the model request. That request then failed for a reason of ours: the Codex CLI on the test machine is not signed in. So we saw where each run stopped, and we did not see an optional-server run finish.
Four things that stayed on the laptop
- The user-level instruction file.
~/.claude/CLAUDE.md,~/.codex/AGENTS.md, your global Cursor rules. A runner has none of them. - The MCP server you added from the command line. Claude Code's default scope and the Codex and Cursor user configs are files in your home directory.
- The harness's memory. Machine-local in Claude Code, local and off by default in Codex. Whatever the agent "learned" in your sessions, the runner starts without it.
- Yesterday's session.
--resumeandcodex exec resumeread session files from disk. On a fresh runner there is nothing to resume, and a vendor-scheduled task starts a new session by design.
The first two have a fix inside the repository: commit the rule to the project instruction file, and declare the server where the job reads it. The last two do not. What one run found has to be written somewhere the next run reads.
Make the job fail when its memory is missing
For Claude Code, write the MCP config from the CI secret, pass it explicitly and check the first event. The memory tools need an allow rule, because in an unattended run a call that needs approval is denied:
set -euo pipefail
umask 077
cat > "$RUNNER_TEMP/mcp.json" <<EOF
{ "mcpServers": { "valorbrain": {
"type": "http",
"url": "https://mcpbrain.valor.digital/mcp",
"headers": {
"Authorization": "Bearer ${VALORBRAIN_MCP_TOKEN}",
"X-Source-Agent": "nightly-triage",
"X-Run-Kind": "routine" } } } }
EOF
claude --bare -p "$(cat .ci/nightly-triage.md)" \
--append-system-prompt-file ./CLAUDE.md \
--mcp-config "$RUNNER_TEMP/mcp.json" --strict-mcp-config \
--allowedTools "Read,Bash(npm test *),mcp__valorbrain__memory_prepare,mcp__valorbrain__task_state,mcp__valorbrain__memory_store" \
--permission-prompts none \
--output-format stream-json --verbose > run.jsonl
# the memory server loaded and connected, or the job fails
jq -e -s '(map(select(.type=="system" and .subtype=="init"))[0]) as $i
| (($i.mcp_server_errors // []) | length) == 0
and ([$i.mcp_servers[]? | select(.name=="valorbrain" and .status=="connected")] | length) == 1' \
run.jsonl > /dev/null
# nothing the run tried was denied
jq -e -s '(map(select(.type=="result"))[-1].permission_denials // []) | length == 0' run.jsonl > /dev/nullWe ran the first jq check against the output of the test above, where it exits 1, and both checks against a hand-written stream with a connected server, where they exit 0. We did not run the full script on a CI runner. Two notes from the documentation: bare mode does not use your subscription login, so the job needs ANTHROPIC_API_KEY or an apiKeyHelper; and a remote server with a cached tool list can show pending in system/init and connect on its first call, which on a machine that keeps its cache would fail the first check. The same flags go in claude_args when you use the GitHub Action.
For Codex, the switch is in the server's table:
# .codex/config.toml, committed. Loaded only when the project is trusted.
[mcp_servers.valorbrain]
url = "https://mcpbrain.valor.digital/mcp"
bearer_token_env_var = "VALORBRAIN_MCP_TOKEN"
http_headers = { "X-Source-Agent" = "nightly-triage", "X-Run-Kind" = "routine" }
required = truecodex exec --trust --sandbox workspace-write "$(cat .ci/nightly-triage.md)"The same table in the runner's $CODEX_HOME/config.toml needs no trust; the GitHub Action takes a codex-home input for that. The pages we read do not say what codex exec does with an MCP tool call that would ask for approval. The documented settings are default_tools_approval_mode on the server and approval_mode per tool.
For Cursor, the server goes in the project's .cursor/mcp.json, with the token interpolated from the environment, and the allow rule in .cursor/cli.json:
{ "mcpServers": { "valorbrain": {
"url": "https://mcpbrain.valor.digital/mcp",
"headers": {
"Authorization": "Bearer ${env:VALORBRAIN_MCP_TOKEN}",
"X-Source-Agent": "nightly-triage",
"X-Run-Kind": "routine" } } } }{ "permissions": { "allow": ["Mcp(valorbrain:*)"] } }agent mcp list # the server shows as loaded, or stop here
agent -p --approve-mcps --trust "$(cat .ci/nightly-triage.md)"The CLI pages do not say what agent -p does when a configured server fails to connect, so check agent mcp list in a step before the run and make the prompt itself stop when the tool is missing (below).
A 15-minute drill
Plant four markers, then ask the job what it has.
- In your user-level instruction file:
The canary word for user rules is HERON. - In the project instruction file, committed:
The canary word for project rules is OTTER. - In an interactive session on your laptop, say:
Remember that the canary word for this project is MARTEN.Close the session. - Make sure the MCP server the job depends on is configured the way you configured it the first time.
Now run the job's exact command where the job runs (the CI runner, the cron environment, the vendor's scheduler) with this prompt:
List every canary word you were given and the file or source each one came from.
Then list the MCP servers you can call and three tools from each.
If you have no canary word or no MCP server, say so. Do not guess.Run the same prompt in an interactive session on the laptop and put the two answers side by side. From the documentation, a CI runner should return OTTER only, a bare Claude Code run should return none unless you passed the file, and the server from step 4 should be missing wherever it was added from the command line. The drill tells you what your versions and your setup do, which is the part the documentation cannot.
Where last night's result can live
- A file the job commits.
STATE.mdin the repository, rewritten every night. It works for one job on one repository. It puts a commit in the history every night, two jobs racing on it conflict, and a vendor-scheduled task pushes to its own branch (claude/-prefixed in Claude Code) unless the prompt says otherwise, so the next run, which starts from the default branch, does not see it until someone merges. - The CI cache or an artifact. No commit noise. It is keyed to that workflow on that CI, so the same job run from your laptop or from another harness does not read it, and caches are evicted.
- The issue tracker. The job searches for its own open issues before opening one. This is the right place for the findings a person has to act on, and it already deduplicates the "same issue five times" case if the prompt tells the agent to search first. It is a poor place for the job's own working state: what it checked, what it could not settle, what it ruled out.
- A record outside the runner that any run reads at start. A memory server, a database, a ticket the job owns. It survives the runner, and a person or another agent can read the same record. It is one more service the job depends on, which is why the previous section exists.
Standing rules are a different thing and still belong in the committed instruction file. The record is for what changes between runs.
The same record with ValorBrain
A CI runner has no home directory of yours, so nothing @valorbrain/connect installs is there: no hooks, no rules block, no saved credential. What the job needs is the MCP entry from the previous section and a few lines in its prompt. The two extra headers on that entry are contract headers: X-Source-Agent names the caller, and X-Run-Kind: routine says the call does not come from a person's session. By default, what the job writes with it stays out of other agents' feeds (the recent work in their memory_prepare, the team briefing); a search still finds it, and the job's own runs always see it. Which credential a job should carry is in Tokens: a personal token identifies a person, and gateway and integration credentials exist for systems without one.
The prompt lines:
## Before you start
- Call `memory_prepare` with this task as the message. If the tool is not available, stop and say so. Do not continue from the repository alone.
- Read the open ledger `nightly-triage`: what the last run verified and what it left open.
## Before you finish
- `task_state` with `session: "nightly-triage"`: a `ledger_checkpoint` for what you verified tonight (`verified_by`, `coverage`), a `ledger_open_question` for each thing you could not settle, and `ledger_next`.
- A finding the team has to act on goes in `memory_store` with `type: "problem"`. Do not open an issue the ledger says is already open.task_state is a ledger on the server. Without a session argument, a call writes to the default ledger of the credential and agent making it. Name the ledger after the job, and every run of that job, from any runner or harness, reads and writes the same one.
This page was written by a scheduled run, with nobody at the keyboard. These are the three calls it made (our entries are in Portuguese; translated here):
{ "action": "ledger_open", "session": "guide-headless-runs",
"description": "EN+PT guide on headless and scheduled runs (cron, CI, cloud scheduled task): what the agent starts with in Claude Code, Codex and Cursor. Done = PR open with green CI, reviewed, pages return 200.",
"next": "write both .mdx files and open the PR" }
{ "action": "ledger_checkpoint", "session": "guide-headless-runs",
"what": "Research closed: what the three vendors document about what a run with nobody watching starts with (instruction file, MCP servers, harness memory, permissions, session) and what happens when an MCP server does not come up.",
"verified_by": "30 vendor documentation pages downloaded on Nov 29, 2026 (all HTTP 200), quotes checked with grep against the downloaded text",
"coverage": "documentation only; no headless run was executed in any of the three harnesses" }
{ "action": "ledger_open_question", "session": "guide-headless-runs",
"question": "What does Cursor's `agent -p` do when a configured MCP server fails to connect: go on without it or exit with an error? The CLI docs do not say; the Automations docs say the run continues without the server's tools.",
"settled_by": "run `agent -p` with an MCP server pointing at a URL that refuses connections and read the exit code" }The checkpoint's coverage was true when it was written. The two tests above came after it, which is the kind of thing the next checkpoint records. One minute later, memory_prepare with the task as the message returned the ledger with the rest of the context (translated, shortened):
### session guide-headless-runs (as of 2026-11-29)
- goal: EN+PT guide on headless and scheduled runs (cron, CI, cloud scheduled task) […] Done = PR open with green CI, reviewed, pages return 200.
- last verified: ✓01 Research closed […] — verified by: 30 vendor documentation pages downloaded on Nov 29, 2026 […] (coverage: documentation only; no headless run was executed in any of the three harnesses)
- open: ?01 What does Cursor's `agent -p` do when a configured MCP server fails to connect […] — settled by: run `agent -p` with an MCP server pointing at a URL that refuses connections and read the exit code
- next: write both .mdx files and open the PR
- origin: last write by lucas less than a minute ago with this same credential (lucas (persona token)) — an earlier session of yours, most likely on this computer; still verify live state before acting.That block is what Tuesday's run needs from Monday's: what was verified and by what, what is still open and the cheapest test that would settle it, and the next step. memory_prepare returns open ledgers with the relevant memory, two at most, the most recently written first. A ledger last written by the same agent always qualifies; one written by another agent comes back when the message names its subject, and it arrives labeled as another agent's session. In the same response we got a second ledger, last written by a different agent, with that label.
On a machine where connect is installed. A cron entry on your own machine that runs claude -p without --bare does run the hooks connect wired. They mark the run: the hook requests carry X-Run-Kind: routine when the run is unattended or scripted. And an unattended run never uploads its transcript, even with the transcript switch on: not claude -p, not codex exec, not cursor-agent -p. The details per harness are in What leaves your machine. With --bare, Claude Code runs no hooks at all, so the run reaches ValorBrain only through the MCP calls the agent makes.
What ValorBrain does not do
- It does not start by itself on a runner. No hook is there. The agent calls
memory_preparebecause the prompt says so, and the gate is what catches the night the server did not load. - It does not make the agent obey the ledger. A run that reads "issue already open" and opens it again is a model failure the record makes visible, not one it prevents. We did not measure how often an unattended run follows those prompt lines. The drill and a week of ledger entries are how you measure it on your job.
- It does not replace the instruction file. Standing rules belong in the file the harness loads from the checkout.
- It does not schedule, run or sandbox anything. That is cron, your CI and the harness.
- It needs the network. A runner or a cloud environment with a restricted network policy has to allow
mcpbrain.valor.digital. - What we ran and what we did not. One
claude -pcall and twocodex execcalls on our own machine, as shown, and thejqchecks against that output. We did not run a job on a CI runner, a vendor-scheduled task, bare mode or Cursor's CLI. Everything else about harness behavior is what each vendor's documentation said on the date below. The ledger calls are real and came from one credential in one scheduled run.
Checklist
- You ran the job's exact command where it runs and read what it reported having.
- Every rule the job depends on is in the project instruction file, committed.
- The MCP servers the job needs are declared where that run reads them, with the token from a secret.
- The job fails when a server it needs did not load, and when a call it needed was denied.
- The tools the job has to call are allowed explicitly. Nothing waits for an approval nobody will give.
- Each run reads one named record at start and writes to it before it ends: what it verified, what it left open, the next step.
- Findings for people go to the tracker, after a search for the one already open.
Sources, read on November 29, 2026: Claude Code, Run Claude Code programmatically, CLI reference, MCP, MCP in the Agent SDK, Memory, Cloud scheduled tasks, Desktop scheduled tasks and GitHub Actions; Codex, Non-interactive mode, GitHub Action, Scheduled tasks, AGENTS.md, Memories and MCP; Cursor, Headless CLI, Using the CLI, CLI parameters, CLI permissions, MCP in the CLI, MCP and Automations.
Next: Subagents and worktrees for what parallel workers share inside one session, Handing work from one coding agent to the next for the note that crosses sessions and harnesses, and CLAUDE.md, AGENTS.md and .cursor/rules in one repo for the rules every run loads from the checkout.
Subagents and worktrees: what parallel coding agents share, and where a decision has to live so every worker sees it
A subagent starts with a clean context and hands back a summary. A worktree isolates files and nothing else. What Claude Code, Codex and Cursor document about what a parallel worker starts with, what comes back and which hooks run when one starts, a 20-minute drill with four planted facts, and where to keep the decisions that have to reach every worker.
One MCP config in the repo, no token in git: sharing a server across Claude Code, Codex and Cursor
A committed MCP config either carries someone's token or names a variable that half the team never set. What Claude Code, Codex and Cursor document about project config files, variable expansion, trust and approval, what we measured against a local probe server when the variable is set, unset, empty or has a default, and the files, the preflight and the gate that keep the token out of git and make a missing one loud.