Skip to content

Commit 653ec9e

Browse files
nik13claude
andcommitted
fix(devin-desktop): subshell hook guard, heredoc transport test, privacy page truth (TH-8294)
- hooks.json and the page use the subshell form `(python3 -B ".../emit.py" devin_desktop <event> 2>/dev/null || true)`. Without the parentheses a host that passes the payload as a trailing heredoc binds it to `true` and the emitter reads nothing. - test_devin_desktop_hooks_config runs every shipped command through /bin/sh and bash, with the payload on stdin and as a trailing heredoc: emitter missing and python3 missing exit 0 with empty stdout; after install.sh each hook exits 0, prints nothing and exports its span. The un-parenthesised form fails the heredoc case. - New adapter test: an MCP error and a command naming a home path with a user name are absent by default; with content on the path is kept with <user> and the cwd is never captured. Opt-in fixture paths are scrubbed. - Page: state file holds only open/incomplete turn content (core 6513a26), opted-in paths carry <user>, why pre_user_prompt is registered (exit 0 is "proceeds normally"), dashboard hooks run first. - run_e2e.sh writes no bytecode. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cb3ec5c commit 653ec9e

6 files changed

Lines changed: 121 additions & 55 deletions

File tree

‎examples/coding-agents/devin-desktop/hooks.json‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22
"hooks": {
33
"pre_user_prompt": [
44
{
5-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop pre_user_prompt 2>/dev/null || true",
5+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop pre_user_prompt 2>/dev/null || true)",
66
"show_output": false
77
}
88
],
99
"post_read_code": [
1010
{
11-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_read_code 2>/dev/null || true",
11+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_read_code 2>/dev/null || true)",
1212
"show_output": false
1313
}
1414
],
1515
"post_write_code": [
1616
{
17-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_write_code 2>/dev/null || true",
17+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_write_code 2>/dev/null || true)",
1818
"show_output": false
1919
}
2020
],
2121
"post_run_command": [
2222
{
23-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_run_command 2>/dev/null || true",
23+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_run_command 2>/dev/null || true)",
2424
"show_output": false
2525
}
2626
],
2727
"post_mcp_tool_use": [
2828
{
29-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_mcp_tool_use 2>/dev/null || true",
29+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_mcp_tool_use 2>/dev/null || true)",
3030
"show_output": false
3131
}
3232
],
3333
"post_cascade_response": [
3434
{
35-
"command": "python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_cascade_response 2>/dev/null || true",
35+
"command": "(python3 -B \"$HOME/.codeium/windsurf/fi-hook-to-otlp/emit.py\" devin_desktop post_cascade_response 2>/dev/null || true)",
3636
"show_output": false
3737
}
3838
]

‎examples/coding-agents/devin-desktop/run_e2e.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ PY
4040
sleep "${REPLAY_SLEEP:-0.3}"
4141
done
4242

43-
TRACE_ID="$(cd "$EMITTER" && python3 -c 'import sys, emit; print(emit.trace_id("devin_desktop", sys.argv[1]))' "$RUN_ID")"
43+
TRACE_ID="$(cd "$EMITTER" && python3 -B -c 'import sys, emit; print(emit.trace_id("devin_desktop", sys.argv[1]))' "$RUN_ID")"
4444
echo "trajectory_id=$RUN_ID"
4545
echo "trace_id=$TRACE_ID"

‎examples/coding-agents/hook-to-otlp/adapters/devin_desktop.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
are not mapped. ``post_cascade_response_with_transcript`` is never mapped
3535
and its ``transcript_path`` is never opened. ``respond`` prints nothing:
3636
Cascade decides on the exit code, and the hook command in hooks.json is
37-
guarded (``2>/dev/null || true``) so it exits 0 even without the emitter.
37+
guarded (``(python3 -B ... 2>/dev/null || true)``) so it exits 0 even
38+
without the emitter.
3839
This adapter covers Cascade only, not the Devin Local agent or hosted
3940
Devin sessions.
4041
"""

‎examples/coding-agents/hook-to-otlp/tests/test_devin_desktop_adapter.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ def test_capture_content_opt_in(self):
106106
self.assertIn(marker, encoded, marker)
107107
for marker in NEVER:
108108
self.assertNotIn(marker, encoded, marker)
109+
# Paths inside opted-in tool input keep the path but not the account name.
110+
self.assertIn("/Users/<user>/project", encoded)
111+
self.assertNotIn("/Users/fixture/", encoded)
112+
113+
def test_error_text_with_path_and_user_is_content(self):
114+
# An MCP tool error comes back in mcp_result; a command line can name a home path.
115+
records = [
116+
{"event": "pre_user_prompt", "payload": {"agent_action_name": "pre_user_prompt", "trajectory_id": "err-traj",
117+
"execution_id": "err-exec", "tool_info": {"user_prompt": "p"}}},
118+
{"event": "post_mcp_tool_use", "payload": {
119+
"agent_action_name": "post_mcp_tool_use", "trajectory_id": "err-traj", "execution_id": "err-exec",
120+
"tool_info": {"mcp_server_name": "fs", "mcp_tool_name": "read_file",
121+
"mcp_tool_arguments": {"path": "/Users/alice-canary/secret.txt"},
122+
"mcp_result": "Error: ENOENT: no such file '/Users/alice-canary/secret.txt'"}}},
123+
{"event": "post_run_command", "payload": {
124+
"agent_action_name": "post_run_command", "trajectory_id": "err-traj", "execution_id": "err-exec",
125+
"tool_info": {"command_line": "cat /home/alice-canary/.netrc", "cwd": "/home/alice-canary/repo"}}},
126+
{"event": "post_cascade_response", "payload": {"agent_action_name": "post_cascade_response",
127+
"trajectory_id": "err-traj", "execution_id": "err-exec",
128+
"tool_info": {"response": "r"}}},
129+
]
130+
default = json.dumps(replay(devin_desktop, config("devin_desktop", self.tmp.name), records))
131+
for text in ("alice-canary", "ENOENT", "/Users/", "/home/"):
132+
self.assertNotIn(text, default, text)
133+
with tempfile.TemporaryDirectory() as tmp:
134+
opted = json.dumps(replay(devin_desktop, config("devin_desktop", tmp, FI_HOOK_CAPTURE_CONTENT="1"), records))
135+
self.assertIn("ENOENT", opted)
136+
self.assertIn("/Users/<user>/secret.txt", opted)
137+
self.assertNotIn("alice-canary", opted) # user name scrubbed, cwd never captured
109138

110139
def test_transcript_path_is_never_opened(self):
111140
opened = []

‎examples/coding-agents/hook-to-otlp/tests/test_devin_desktop_hooks_config.py‎

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
"""The shipped Devin Desktop hooks.json, run the way Cascade runs it (bash -c).
1+
"""The shipped Devin Desktop hooks.json, run the way Cascade runs it.
22
3-
Cascade treats exit code 2 from a pre-hook as "block" (pre_user_prompt
4-
blocks the prompt). Every shipped command must exit 0 with empty stdout,
5-
also when the emitter or python3 is missing.
3+
Cascade runs a hook's ``command`` with ``bash -c`` and passes the event JSON on
4+
stdin (https://docs.devin.ai/desktop/cascade/hooks, Configuration Options). It
5+
treats exit code 2 from a pre-hook as "block" (pre_user_prompt blocks the
6+
prompt). Every shipped command must exit 0 with empty stdout, also when the
7+
emitter or python3 is missing.
8+
9+
Each command is also run with the payload as a trailing heredoc
10+
(``<command> <<'EOF'``), the transport some hook hosts use: the subshell guard
11+
``( ... || true)`` keeps the heredoc on the emitter instead of on ``true``.
612
"""
713
from __future__ import annotations
814

@@ -14,26 +20,37 @@
1420
import unittest
1521
from pathlib import Path
1622

17-
from tests.helpers import ROOT, Receiver, all_spans
18-
19-
BASH = shutil.which("bash") or "/bin/bash"
23+
from tests.helpers import ROOT, Receiver
2024

2125
APP_DIR = ROOT.parent / "devin-desktop"
2226
HOOKS = json.loads((APP_DIR / "hooks.json").read_text(encoding="utf-8"))["hooks"]
2327
EXPECTED = {"pre_user_prompt", "post_read_code", "post_write_code", "post_run_command",
2428
"post_mcp_tool_use", "post_cascade_response"}
29+
# Cascade uses bash; /bin/sh is the POSIX baseline.
30+
SHELLS = [sh for sh in ("/bin/sh", shutil.which("bash") or "/bin/bash") if os.path.exists(sh)]
31+
TRANSPORTS = ("stdin", "heredoc")
32+
# Span each hook exports once the emitter is installed.
33+
SPAN_FOR = {"pre_user_prompt": None, "post_read_code": "read_code", "post_write_code": "write_code",
34+
"post_run_command": "run_command", "post_mcp_tool_use": "list_commits",
35+
"post_cascade_response": "Cascade turn"}
2536

2637

27-
def payload(event: str) -> str:
28-
return json.dumps({"agent_action_name": event, "trajectory_id": "cfg-traj", "execution_id": "cfg-exec",
29-
"model_name": "Claude Sonnet 4", "tool_info": {"file_path": "/tmp/x"}})
38+
def payload(event: str, trajectory: str = "cfg-traj") -> str:
39+
info = {"file_path": "/tmp/x", "edits": [], "command_line": "ls", "mcp_server_name": "github",
40+
"mcp_tool_name": "list_commits", "user_prompt": "p", "response": "r"}
41+
return json.dumps({"agent_action_name": event, "trajectory_id": trajectory, "execution_id": "cfg-exec",
42+
"model_name": "Claude Sonnet 4", "tool_info": info})
3043

3144

32-
def run(command: str, home: str, stdin: str, **env: str) -> subprocess.CompletedProcess:
45+
def run(command: str, shell: str, transport: str, home: str, stdin: str, **env: str) -> subprocess.CompletedProcess:
3346
full = {"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": home}
3447
full.update(env)
35-
return subprocess.run([BASH, "-c", command], input=stdin, capture_output=True, text=True,
36-
env=full, timeout=30)
48+
if transport == "heredoc":
49+
script, stdin = f"{command} <<'HOOK_EOF'\n{stdin}\nHOOK_EOF\n", ""
50+
else:
51+
script = command
52+
return subprocess.run([shell, "-c", script], input=stdin, capture_output=True, text=True,
53+
env=full, cwd=home, timeout=30)
3754

3855

3956
class DevinDesktopHooksConfigTest(unittest.TestCase):
@@ -43,54 +60,73 @@ def setUp(self):
4360
def tearDown(self):
4461
shutil.rmtree(self.home, ignore_errors=True)
4562

46-
def test_registered_hooks(self):
63+
def test_registered_hooks_are_guarded(self):
4764
self.assertEqual(set(HOOKS), EXPECTED)
4865
self.assertNotIn("post_cascade_response_with_transcript", HOOKS)
4966
for event, entries in HOOKS.items():
5067
self.assertEqual(len(entries), 1, event)
5168
entry = entries[0]
5269
self.assertIs(entry["show_output"], False, event)
53-
self.assertTrue(entry["command"].startswith("python3 -B "), event)
54-
self.assertTrue(entry["command"].endswith(f" devin_desktop {event} 2>/dev/null || true"), event)
70+
self.assertTrue(entry["command"].startswith("(python3 -B "), event)
71+
self.assertTrue(entry["command"].endswith(f" devin_desktop {event} 2>/dev/null || true)"), event)
5572

5673
def test_page_shows_the_shipped_hooks_json(self):
5774
page = (ROOT.parents[2] / "src/pages/docs/integrations/coding-agents/devin-desktop.mdx").read_text(encoding="utf-8")
5875
block = page.split("```json\n", 1)[1].split("\n```", 1)[0]
5976
self.assertEqual(json.loads(block)["hooks"], HOOKS)
6077

6178
def test_missing_emitter_never_blocks(self):
62-
for event, entries in HOOKS.items():
63-
result = run(entries[0]["command"], self.home, payload(event))
64-
self.assertEqual((result.returncode, result.stdout, result.stderr), (0, "", ""), event)
79+
for shell in SHELLS:
80+
for transport in TRANSPORTS:
81+
for event, entries in HOOKS.items():
82+
result = run(entries[0]["command"], shell, transport, self.home, payload(event))
83+
self.assertEqual((result.returncode, result.stdout, result.stderr), (0, "", ""),
84+
f"{shell} {transport} {event}")
6585

6686
def test_missing_python_never_blocks(self):
6787
empty_bin = tempfile.mkdtemp()
6888
try:
69-
for event, entries in HOOKS.items():
70-
result = run(entries[0]["command"], self.home, payload(event), PATH=empty_bin)
71-
self.assertEqual((result.returncode, result.stdout), (0, ""), event)
89+
for shell in SHELLS:
90+
for transport in TRANSPORTS:
91+
for event, entries in HOOKS.items():
92+
result = run(entries[0]["command"], shell, transport, self.home, payload(event),
93+
PATH=empty_bin)
94+
self.assertEqual((result.returncode, result.stdout), (0, ""), f"{shell} {transport} {event}")
7295
finally:
7396
shutil.rmtree(empty_bin, ignore_errors=True)
7497

75-
def test_installed_commands_export_and_print_nothing(self):
76-
install = subprocess.run([BASH, str(APP_DIR / "install.sh")], capture_output=True, text=True,
98+
def test_installed_commands_read_the_payload_and_print_nothing(self):
99+
install = subprocess.run([shutil.which("bash") or "/bin/bash", str(APP_DIR / "install.sh")],
100+
capture_output=True, text=True,
77101
env={"PATH": os.environ.get("PATH", ""), "HOME": self.home}, timeout=30)
78102
self.assertEqual(install.returncode, 0, install.stderr)
79103
installed = json.loads(Path(self.home, ".codeium/windsurf/hooks.json").read_text())["hooks"]
80104
self.assertEqual(installed, HOOKS)
81-
receiver = Receiver()
82-
try:
83-
for event in ("pre_user_prompt", "post_read_code", "post_cascade_response"):
84-
result = run(HOOKS[event][0]["command"], self.home, payload(event),
85-
FI_API_KEY="k", FI_SECRET_KEY="s", FI_HOOK_SYNC="1",
86-
FI_OTLP_ENDPOINT=f"{receiver.url}/v1/traces",
87-
FI_HOOK_STATE_DIR=os.path.join(self.home, "state"))
88-
self.assertEqual((result.returncode, result.stdout), (0, ""), event)
89-
receiver.wait_for(2)
90-
names = {s["name"] for _, _, body in receiver.requests for s in all_spans(body)}
91-
self.assertEqual(names, {"read_code", "Cascade turn", "Cascade session"})
92-
finally:
93-
receiver.close()
105+
for shell in SHELLS:
106+
for transport in TRANSPORTS:
107+
receiver = Receiver()
108+
trajectory = f"cfg-{Path(shell).name}-{transport}"
109+
try:
110+
for event in HOOKS:
111+
before = len(receiver.requests)
112+
result = run(HOOKS[event][0]["command"], shell, transport, self.home,
113+
payload(event, trajectory),
114+
FI_API_KEY="k", FI_SECRET_KEY="s", FI_HOOK_SYNC="1",
115+
FI_OTLP_ENDPOINT=f"{receiver.url}/v1/traces",
116+
FI_HOOK_STATE_DIR=os.path.join(self.home, "state"))
117+
label = f"{shell} {transport} {event}"
118+
self.assertEqual((result.returncode, result.stdout), (0, ""), label)
119+
expected = SPAN_FOR[event]
120+
if expected is None: # the turn start exports nothing yet
121+
self.assertEqual(len(receiver.requests), before, label)
122+
continue
123+
receiver.wait_for(before + 1)
124+
names = {s["name"] for _, _, body in receiver.requests[before:]
125+
for rs in body["resourceSpans"] for ss in rs["scopeSpans"] for s in ss["spans"]}
126+
self.assertIn(expected, names, label)
127+
finally:
128+
receiver.close()
129+
self.assertEqual(list(Path(self.home).rglob("__pycache__")), [], "-B writes no bytecode")
94130

95131

96132
if __name__ == "__main__":

0 commit comments

Comments
 (0)