Skip to content

Commit dfd5238

Browse files
authored
fix(gator): make supervised lifecycle sandbox-native (#3343)
* fix(gator): run supervisor as sandbox main process Signed-off-by: John Myers <johntmyers@users.noreply.github.com> * feat(gator): persist supervised state history Signed-off-by: John Myers <johntmyers@users.noreply.github.com> * refactor(gator): remove obsolete background launch mode Signed-off-by: John Myers <johntmyers@users.noreply.github.com> --------- Signed-off-by: John Myers <johntmyers@users.noreply.github.com> Co-authored-by: John Myers <johntmyers@users.noreply.github.com>
1 parent 481ce56 commit dfd5238

13 files changed

Lines changed: 309 additions & 92 deletions

File tree

‎.agents/skills/launch-openshell-gator/SKILL.md‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ For gator's PR/issue validation policy, load `gator-gate` inside the launched sa
1313

1414
## Non-Negotiable Rules
1515

16-
- Keep normal gator launches supervised: use `--watch --background` and let the in-sandbox supervisor own sleeping and relaunching bounded cycles.
16+
- Keep normal gator launches supervised: use `--watch` and let the in-sandbox supervisor own sleeping and relaunching bounded cycles.
1717
- Do not add passive `sleep` loops in the operator session to watch gator. Check logs or status once, then report the current state or launch a proper watcher outside the model session only when explicitly asked.
1818
- Do not change the default gator model in `scripts/agents/gator/agent.yaml` for experiments. Use `CODEX_MODEL=...` and, if needed, a temporary `--from` Docker context or `--codex-bin` override.
1919
- Do not push to contributor branches, approve, merge, post `/ok to test`, or broaden gator scope unless the operator explicitly authorized that action.
@@ -34,7 +34,6 @@ For gator's PR/issue validation policy, load `gator-gate` inside the launched sa
3434
| `scripts/agents/gator/bin/validate-review-findings` | Enforces the blocker evidence schema and downgrades unsupported hypotheses. |
3535
| `scripts/agents/gator/prompts/gator.md` | Rendered top-level prompt template baked into the payload. |
3636
| `scripts/agents/gator/skills/gator-gate/SKILL.md` | In-sandbox gator state-machine skill. |
37-
| `scripts/agents/gator/logs/` | Background launch and supervisor logs. |
3837

3938
## Preflight
4039

@@ -156,11 +155,12 @@ sandbox_name="gator-pr-${pr_number}-supervised"
156155
--gateway "$gateway_name" \
157156
--name "$sandbox_name" \
158157
--watch \
159-
--background \
160158
"Review and monitor PR #${pr_number} through the gator-gate workflow. Scope this invocation only to PR #${pr_number}."
161159
```
162160

163-
The launcher queries gateway's selected compute driver, builds gator image in matching Docker or Podman image store, stages immutable payload, imports provider profiles, configures provider credentials and refresh, creates and uploads sandbox payload, then starts agent supervisor with `sandbox exec`. It writes a background log under `scripts/agents/gator/logs/`. `CONTAINER_ENGINE`, when set, must match gateway driver.
161+
The launcher queries the gateway's selected compute driver, builds the gator image in the matching Docker or Podman image store, stages the immutable payload, imports provider profiles, configures provider credentials and refresh, and starts the agent supervisor as the sandbox's canonical main process. The detached main process survives loss of the host CLI connection and reconnects to a restarted gateway. Unless `--keep` is set, the sandbox is marked ephemeral so the gateway deletes it after the supervisor exits. `CONTAINER_ENGINE`, when set, must match the gateway driver.
162+
163+
The launcher streams image-build and provisioning output until the detached workload is ready, then exits. Use `openshell logs <sandbox-name>` or the TUI for runtime output.
164164

165165
### Launch An Issue Or Issue/PR Pair
166166

@@ -177,7 +177,6 @@ sandbox_name="gator-issue-${issue_number}-supervised"
177177
--gateway "$gateway_name" \
178178
--name "$sandbox_name" \
179179
--watch \
180-
--background \
181180
"Run gator on issue #${issue_number}. Scope this invocation only to issue #${issue_number}."
182181
```
183182

@@ -198,7 +197,6 @@ sandbox_name="gator-pr-${pr_number}-supervised"
198197
--gateway "$gateway_name" \
199198
--name "$sandbox_name" \
200199
--watch \
201-
--background \
202200
"Review and monitor PR #${pr_number} with linked issue #${issue_number} through the gator-gate workflow. Scope this invocation only to PR #${pr_number} and issue #${issue_number}."
203201
```
204202

@@ -219,7 +217,6 @@ sandbox_name="gator-pr-${pr_number}-supervised"
219217
--gateway "$gateway_name" \
220218
--name "$sandbox_name" \
221219
--watch \
222-
--background \
223220
"Review and monitor PR #${pr_number} through the gator-gate workflow. Scope this invocation only to PR #${pr_number}. The operator explicitly authorizes applying the test:e2e label, posting /ok to test for the current head SHA, and rerunning the relevant current-head workflow when the E2E Label Help bot says that is required."
224221
```
225222

@@ -241,7 +238,6 @@ CODEX_MODEL=gpt-5.6-sol \
241238
--gateway "$gateway_name" \
242239
--name "$sandbox_name" \
243240
--watch \
244-
--background \
245241
"Review and monitor PR #${pr_number} through the gator-gate workflow. Scope this invocation only to PR #${pr_number}. This launch is intentionally testing Codex model gpt-5.6-sol via the CLI launcher."
246242
```
247243

@@ -266,27 +262,22 @@ CODEX_MODEL=gpt-5.6-sol \
266262
--name "$sandbox_name" \
267263
--from "$tmp_context" \
268264
--watch \
269-
--background \
270265
"Review and monitor PR #${pr_number} through the gator-gate workflow. Scope this invocation only to PR #${pr_number}."
271266
```
272267

273268
## Monitoring
274269

275270
### Read The Launch Result
276271

277-
The launcher prints the log path when `--background` is used:
278-
279-
```text
280-
Started in background. Log: scripts/agents/gator/logs/<sandbox-name>.log
281-
```
282-
283-
Read that file directly. Important markers:
272+
The launcher streams image-build and provisioning output to the terminal. Important markers:
284273

285274
- `Built image ...` means the local image build completed.
286275
- `Created sandbox: <name>` means OpenShell accepted the sandbox.
287276
- `openshell-agent: starting watch cycle` means the in-sandbox supervisor began a bounded cycle.
288277
- `OpenAI Codex v...` plus `model: ...` confirms the Codex CLI and model actually used.
289278
- `OPENSHELL_AGENT_RESULT {...}` is the bounded-cycle sentinel. In watch mode, the supervisor sleeps and relaunches after this line.
279+
- `/sandbox/.openshell-agent/status.json` is the atomic current state snapshot. Its `result.notes` field is Gator's plain-language diagnosis and next action for that cycle.
280+
- `/sandbox/.openshell-agent/history.jsonl` contains the latest 100 supervisor transitions, including active-cycle starts and completed cycle results.
290281
- `openshell-agent: still running watch cycle ...` is a heartbeat during long active model cycles.
291282
- `review_feedback_lookup_failed` means Gator could not build the required cross-SHA feedback ledger and deliberately skipped a context-free review.
292283

@@ -314,6 +305,11 @@ If `sandbox get` is not supported by the local CLI shape, use `openshell sandbox
314305
| `status=terminal_failure` | Unrecoverable or stale immutable payload. | Inspect the reason; rebuild/relaunch for `stale_gator_payload`. |
315306
| `status=complete` | Target closed, merged, or one-shot complete. | Delete sandbox if no longer needed. |
316307

308+
Prefer the state snapshot over scraping transient `/tmp` cycle output. Use the
309+
history file to tell whether a failure is repeating or whether the supervisor
310+
has begun a fresh cycle. Runtime logs remain useful for full command output and
311+
transport diagnostics.
312+
317313
## Restarting A Gator
318314

319315
Restart when the payload must change, the sandbox is wedged without a sentinel, the model/tooling version changed, or a transient failure repeats past the useful retry point.
@@ -339,7 +335,6 @@ openshell --gateway "$gateway_name" sandbox delete "$sandbox_name"
339335
--gateway "$gateway_name" \
340336
--name "$sandbox_name" \
341337
--watch \
342-
--background \
343338
"<same scoped operator prompt, updated only with the reason for relaunch>"
344339
```
345340

@@ -401,7 +396,6 @@ When you launch or inspect gator, report:
401396

402397
- Sandbox name.
403398
- Gateway name.
404-
- Log path.
405399
- Target issue/PR scope.
406400
- Harness and model when relevant.
407401
- Whether image build and sandbox creation succeeded.

‎crates/openshell-cli/src/main.rs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ enum SandboxCommands {
13721372
no_tty: bool,
13731373

13741374
/// Start the canonical main process without attaching to it.
1375-
#[arg(long, conflicts_with_all = ["editor", "no_keep"])]
1375+
#[arg(long, conflicts_with = "editor")]
13761376
detach: bool,
13771377

13781378
/// Auto-create missing providers from local credentials.
@@ -5461,10 +5461,35 @@ mod tests {
54615461
}
54625462

54635463
#[test]
5464-
fn sandbox_create_detach_rejects_ephemeral_sandbox() {
5465-
let result =
5466-
Cli::try_parse_from(["openshell", "sandbox", "create", "--detach", "--no-keep"]);
5467-
assert!(result.is_err());
5464+
fn sandbox_create_detach_accepts_ephemeral_sandbox() {
5465+
let cli = Cli::try_parse_from([
5466+
"openshell",
5467+
"sandbox",
5468+
"create",
5469+
"--detach",
5470+
"--no-keep",
5471+
"--",
5472+
"worker",
5473+
])
5474+
.expect("detached ephemeral sandbox should parse");
5475+
5476+
match cli.command {
5477+
Some(Commands::Sandbox {
5478+
command:
5479+
Some(SandboxCommands::Create {
5480+
detach,
5481+
no_keep,
5482+
command,
5483+
..
5484+
}),
5485+
..
5486+
}) => {
5487+
assert!(detach);
5488+
assert!(no_keep);
5489+
assert_eq!(command, ["worker"]);
5490+
}
5491+
other => panic!("expected SandboxCommands::Create, got: {other:?}"),
5492+
}
54685493
}
54695494

54705495
#[test]

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,46 @@ async fn detached_command_does_not_declare_main_process_attachment() {
17001700
assert!(!requests[0].await_main_process_attachment);
17011701
}
17021702

1703+
#[tokio::test]
1704+
async fn detached_ephemeral_command_delegates_cleanup_to_gateway() {
1705+
let server = run_server().await;
1706+
let fake_ssh_dir = tempfile::tempdir().unwrap();
1707+
let xdg_dir = tempfile::tempdir().unwrap();
1708+
let _env = test_env(&fake_ssh_dir, &xdg_dir);
1709+
let tls = test_tls(&server);
1710+
install_fake_ssh(&fake_ssh_dir);
1711+
1712+
run::sandbox_create(
1713+
&server.endpoint,
1714+
"openshell",
1715+
run::SandboxCreateConfig {
1716+
name: Some("detached-ephemeral-main"),
1717+
keep: false,
1718+
command: &["worker".into()],
1719+
detach: true,
1720+
..test_config()
1721+
},
1722+
"default",
1723+
&tls,
1724+
)
1725+
.await
1726+
.expect("detached ephemeral sandbox create should succeed");
1727+
1728+
let requests = create_requests(&server).await;
1729+
assert!(!requests[0].await_main_process_attachment);
1730+
assert_eq!(
1731+
requests[0]
1732+
.annotations
1733+
.get("openshell.nvidia.com/retention")
1734+
.map(String::as_str),
1735+
Some("ephemeral")
1736+
);
1737+
assert!(
1738+
deleted_names(&server).await.is_empty(),
1739+
"the gateway owns cleanup after a detached canonical process exits"
1740+
);
1741+
}
1742+
17031743
#[tokio::test]
17041744
async fn sandbox_create_sends_driver_config_json() {
17051745
let server = run_server().await;

‎docs/sandboxes/manage-sandboxes.mdx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ captures the command result, and deletes the sandbox after the command exits:
4646
openshell sandbox create --no-keep -- sh -c 'echo done; exit 0'
4747
```
4848

49+
Combine `--detach` and `--no-keep` for a background workload whose lifecycle
50+
belongs entirely to the gateway:
51+
52+
```shell
53+
openshell sandbox create --detach --no-keep -- ./worker
54+
```
55+
56+
The create command returns after the workload is ready. The gateway keeps the
57+
canonical process running without a host-side attachment and deletes the
58+
sandbox after that process exits.
59+
4960
`--upload` cannot yet be combined with a trailing main command because uploads
5061
finish after the canonical process starts. Create a scratch sandbox, upload the
5162
files, then launch the workload with `sandbox exec`, or build the files into the

‎scripts/agents/README.md‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ Each agent has an `agent.yaml` manifest. The launcher currently reads these
3434
sections:
3535

3636
- `id`, `display_name`, `description`: human and runtime identity.
37-
- `sandbox`: default sandbox name prefix, gateway, source image or Dockerfile,
38-
and background log directory.
37+
- `sandbox`: default sandbox name prefix, gateway, and source image or Dockerfile.
3938
- `harness`: default harness and per-harness settings such as model and
4039
reasoning effort.
4140
- `runtime`: in-sandbox run mode (`once` or `watch`), watch poll interval, and
@@ -86,14 +85,17 @@ Manifest paths support these prefixes:
8685
to the sandbox.
8786
15. Configure and rotate refresh-backed provider credentials when declared by
8887
the manifest.
89-
16. Run `openshell sandbox create` from the resulting image reference.
90-
17. Inside the sandbox, run `/etc/openshell/agent-payload/runtime/entrypoint.sh`.
91-
18. The runtime entrypoint starts
88+
16. Run `openshell sandbox create` from the resulting image reference, with the
89+
runtime entrypoint persisted as the detached canonical main process.
90+
17. Mark the sandbox ephemeral unless `--keep` was supplied, allowing the
91+
gateway to delete it after the canonical process exits.
92+
18. Inside the sandbox, run `/etc/openshell/agent-payload/runtime/entrypoint.sh`.
93+
19. The runtime entrypoint starts
9294
`/etc/openshell/agent-payload/runtime/supervisor.sh`.
93-
19. The supervisor invokes
95+
20. The supervisor invokes
9496
`/etc/openshell/agent-payload/runtime/harnesses/<harness>/exec.sh` as a
9597
bounded child execution.
96-
20. Harness adapters prepare harness-local auth/config and execute the agent
98+
21. Harness adapters prepare harness-local auth/config and execute the agent
9799
prompt headlessly.
98100

99101
The payload directory is baked into the image under `/etc/openshell`, which the
@@ -113,7 +115,7 @@ bounded harness cycles. The harness must not sleep or poll indefinitely. Instead
113115
it performs one reconciliation cycle, then prints a final-line sentinel:
114116

115117
```text
116-
OPENSHELL_AGENT_RESULT {"status":"waiting","next_poll_seconds":900,"reason":"checks_pending"}
118+
OPENSHELL_AGENT_RESULT {"status":"waiting","next_poll_seconds":900,"reason":"checks_pending","notes":"Required checks are still running. The agent will inspect them next cycle."}
117119
```
118120

119121
Supported statuses are `complete`, `waiting`, `blocked`, `transient_failure`, and
@@ -129,6 +131,17 @@ retried indefinitely with bounded backoff; only `complete` and
129131
to upstream model errors while leaving durable state ownership to the agent
130132
domain.
131133

134+
The supervisor writes an atomic current snapshot to
135+
`/sandbox/.openshell-agent/status.json` and keeps the latest 100 supervisor
136+
transitions in `/sandbox/.openshell-agent/history.jsonl`. Each record identifies
137+
the cycle, supervisor state, harness exit code, and structured agent result.
138+
Agents should include a concise `notes` field in every result to capture their
139+
human-readable current diagnosis, notable issues or questions, and next useful
140+
action. The supervisor caps notes at 2,048 characters and records a diagnostic
141+
placeholder when an agent omits them. Override the state directory, history
142+
limit, or notes limit with `OPENSHELL_AGENT_STATE_DIR`,
143+
`OPENSHELL_AGENT_STATE_HISTORY_LIMIT`, or `OPENSHELL_AGENT_MAX_NOTES_LENGTH`.
144+
132145
The shared runtime does not prescribe the durable state store. Gator uses GitHub
133146
labels, comments, reviews, and checks. Other agents can use a repository branch,
134147
issue tracker, object store, database, or another domain-specific store as long

‎scripts/agents/gator/agent.yaml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
id: gator
5-
payload_version: 8
5+
payload_version: 9
66
display_name: Gator Gate Agent
77
description: Validate and monitor OpenShell GitHub issues and pull requests through the gator state machine.
88

99
sandbox:
1010
name_prefix: gtr
1111
from: agent://.
1212
gateway: docker-dev
13-
background_log_dir: logs
1413

1514
harness:
1615
default: codex

‎scripts/agents/gator/bin/review_feedback_ledger_test.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ rg -q 'COPY bin/validate-review-findings /usr/local/bin/validate-review-findings
349349
"$GATOR_DIR/Dockerfile"
350350
ruby -ryaml -e '
351351
manifest = YAML.load_file(ARGV.fetch(0))
352-
abort unless manifest.fetch("payload_version") == 7
352+
abort unless manifest.fetch("payload_version") == 9
353353
resource = manifest.fetch("resources").find {
354354
|entry| entry.fetch("id") == "gator-review-findings-schema"
355355
}

0 commit comments

Comments
 (0)