Skip to content
Python

Inside OpenShell, ask about a blocked site and wait

Rohit C Prasad09dc46575ae3
diff --git a/coworker/agent.py b/coworker/agent.py
index 5da0637c..3dc8833a 100644
--- a/coworker/agent.py
+++ b/coworker/agent.py
@@ -671,7 +671,10 @@ def build_engine(
parts.append(text)
# OpenWorker itself inside an OpenShell sandbox: what a blocked request looks like
# and what to do. Read each turn, since the policy skill can appear mid-session.
- openshell_ctx = _openshell.context()
+ eng = _engine_box[0] if _engine_box else None
+ openshell_ctx = _openshell.context(
+ nobody_answers=eng is not None and eng._auto_answering()
+ )
if openshell_ctx:
parts.append(openshell_ctx)
# Live skill menu (SKILLS-SPEC §4.1): recomputed every turn like the roots list, so
@@ -686,7 +689,6 @@ def build_engine(
# steering the model even after the skill is turned off/deleted — history can't be
# un-read. So a loaded-but-no-longer-available skill gets an explicit stop note,
# recomputed fresh each turn (re-enable → the note disappears; never persisted).
- eng = _engine_box[0] if _engine_box else None
if eng is not None:
available = set(skill_loader.names()) if allowed is None else set(allowed)
for name in sorted(_loaded_skill_names(eng.messages) - available):
diff --git a/coworker/sandbox/inside.py b/coworker/sandbox/inside.py
index 3872a9da..ed8f5c93 100644
--- a/coworker/sandbox/inside.py
+++ b/coworker/sandbox/inside.py
@@ -25,6 +25,16 @@ SKILLS_DIR = Path("/etc/openshell/skills")
POLICY_SKILL = "openshell-policy-advisor"
_POLICY_SKILL_FILE = Path("policy-advisor") / "SKILL.md"
+# How a person allows a site from outside. Seen on OpenShell 0.1.2: without `--binary` the
+# rule names no program and lets nothing through. The sandbox's name is not known inside.
+_ALLOW_COMMAND = (
+ "they can allow it from outside the sandbox, while it is still running, with "
+ "`openshell policy update SANDBOX_NAME --add-endpoint HOST:PORT --binary '/**' --wait`. "
+ "Give the command with the real host and port. Say that SANDBOX_NAME is the sandbox's "
+ "name, which `openshell sandbox list` shows, and that `--binary` is required: without "
+ "it the rule lets no program through."
+)
+
def inside_openshell() -> bool:
return os.environ.get("OPENSHELL_SANDBOX", "").strip() == "1"
@@ -37,8 +47,12 @@ def policy_skill_present() -> bool:
return False
-def context() -> str:
- """The lines the agent reads each turn. Empty outside OpenShell."""
+def context(nobody_answers: bool = False) -> str:
+ """The lines the agent reads each turn. Empty outside OpenShell.
+
+ `nobody_answers` is the run's auto-answer: with a person there the agent asks and
+ waits while they change the policy; with nobody there it reports the block and goes on.
+ """
if not inside_openshell():
return ""
lines = [
@@ -56,13 +70,21 @@ def context() -> str:
"it: it explains how to ask for the access, which a person outside the sandbox "
"approves."
)
+ elif nobody_answers:
+ lines.append(
+ "If the task needs a blocked request, nobody is there to allow it during this "
+ "run. Finish what you can without it, and in your answer tell the user which "
+ f"host and port were blocked and that {_ALLOW_COMMAND}"
+ )
else:
lines.append(
- "If the task needs a blocked request, tell the user which host and port were "
- "blocked. They can allow it from outside the sandbox, while it is still running, "
- "with `openshell policy update SANDBOX_NAME --add-endpoint HOST:PORT --binary '/**' "
- "--wait`. Give the command with the real host and port, and say that it needs the "
- "sandbox's name and that `--binary` is required: without it the rule lets no "
- "program through."
+ "If the task needs a blocked request, ask the user with `ask_user` and wait for "
+ "the answer. Do not end your turn with only a report of the block. In the "
+ f"question, name the host and port that were blocked and say that {_ALLOW_COMMAND} "
+ "Offer two choices: they have allowed it, or leave it blocked. If they allowed "
+ "it, make the request again. If they leave it blocked, do not ask again for that "
+ "host: finish what you can without it, or stop if the task cannot go on, and say "
+ "what was left out. If asking is not available, put the same information in your "
+ "answer."
)
return " ".join(lines)
diff --git a/docs/openshell-inside.md b/docs/openshell-inside.md
index a568b953..1bfa416a 100644
--- a/docs/openshell-inside.md
+++ b/docs/openshell-inside.md
@@ -118,9 +118,15 @@ name one, such as `/usr/bin/curl`, to keep it to that program. Do not leave `--b
OpenShell then adds a rule that lets no program through. The change reaches the running
sandbox in a few seconds, with no restart.
-The sandbox has to be running to be changed. A run started with `--no-keep` (step 3) is
-gone when it ends, so for work that needs sites use a sandbox that stays up (step 4),
-allow the sites, and run the task again.
+When a task needs a blocked site and you are at the terminal, the coworker asks you and
+waits. The question names the site and gives this command. Run it in another terminal,
+with the sandbox's name from `openshell sandbox list`, then answer that you allowed it and
+the coworker tries again. Answer "leave it blocked" and it finishes without the site.
+
+With `--auto-answer`, or with no terminal, nobody is there to ask. The coworker finishes
+what it can and its answer says which site was blocked. A run started with `--no-keep`
+(step 3) is gone when it ends, so allow the site on a sandbox that stays up (step 4) and
+run the task again.
OpenShell can also draft these rules from the requests it blocked, for you to approve. That
is its policy advisor; see OpenShell's
diff --git a/tests/test_openshell_inside.py b/tests/test_openshell_inside.py
index 9c243997..b93263cc 100644
--- a/tests/test_openshell_inside.py
+++ b/tests/test_openshell_inside.py
@@ -88,6 +88,23 @@ def test_inside_the_agent_is_told_what_a_blocked_request_looks_like(in_openshell
assert "openshell-policy-advisor" not in text
+def test_with_a_person_there_the_agent_asks_and_waits_for_the_site(in_openshell: Path, tmp_path: Path) -> None:
+ # Seen live: told only to report the block, the agent ended the run with the command
+ # as its answer, and the sandbox was gone before anyone could run it.
+ text = _engine(tmp_path).context_provider()
+ assert "ask the user with `ask_user` and wait" in text
+ assert "they have allowed it, or leave it blocked" in text
+ assert "`openshell sandbox list`" in text # the sandbox's name is not known inside
+
+
+def test_with_auto_answer_the_agent_reports_the_block_and_does_not_ask(in_openshell: Path, tmp_path: Path) -> None:
+ engine = _engine(tmp_path)
+ engine.attendance = lambda: "auto"
+ text = engine.context_provider()
+ assert "nobody is there to allow it" in text and "ask_user" not in text.split("OpenShell sandbox", 1)[1].split("\n\n", 1)[0]
+ assert "openshell policy update SANDBOX_NAME --add-endpoint HOST:PORT --binary '/**' --wait" in text
+
+
def test_with_proposals_on_the_agent_is_pointed_at_openshells_skill(in_openshell: Path, tmp_path: Path) -> None:
_turn_on_proposals(in_openshell)
engine = _engine(tmp_path)