Spaces:
Running
Running
File size: 7,341 Bytes
a9e27c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """Deterministic JSONL peer for native transport tests; never executes tools."""
import json
import sys
import time
from pathlib import Path
def emit(value):
encoded = (json.dumps(value, ensure_ascii=False) + "\n").encode("utf-8")
# Exercise framing across both large messages and UTF-8 boundaries.
for start in range(0, len(encoded), 32767):
sys.stdout.buffer.write(encoded[start : start + 32767])
sys.stdout.buffer.flush()
mode = sys.argv[1]
defaults = {
"approvalPolicy": "on-request",
"approvalsReviewer": "user",
"sandbox": {"type": "workspaceWrite"},
"activePermissionProfile": {"id": ":workspace"},
}
creation_id = None
turn = "turn-1"
for line in sys.stdin.buffer:
request = json.loads(line)
method = request.get("method")
request_id = request.get("id")
if method == "initialize":
if mode == "factory-wait":
continue
if mode == "factory-fail":
emit({"id": request_id, "error": {"message": "Initialization rejected"}})
else:
emit({"id": request_id, "result": {"userAgent": "codex/0.153.0"}})
elif method == "initialized":
pass
elif method in {"thread/start", "thread/resume"}:
if mode == "delayed-create":
creation_id = request_id
else:
emit(
{
"id": request_id,
"result": {"thread": {"id": "native-1", "turns": []}, **defaults},
}
)
elif method == "test/barrier":
emit({"id": request_id, "result": {}})
elif method == "test/release-create":
emit(
{
"id": creation_id,
"result": {"thread": {"id": "native-1", "turns": []}, **defaults},
}
)
emit({"id": request_id, "result": {}})
elif method == "turn/start":
if mode == "child-warning-on-close":
turn = request["params"]["clientUserMessageId"]
emit(
{
"method": "turn/started",
"params": {
"threadId": "native-1",
"turn": {"id": turn, "status": "inProgress"},
},
}
)
if mode == "ordered-usage":
emit(
{
"method": "thread/tokenUsage/updated",
"params": {
"threadId": "native-1",
"turnId": turn,
"tokenUsage": {"last": {"totalTokens": 12_438}},
},
}
)
emit(
{
"method": "turn/completed",
"params": {
"threadId": "native-1",
"turn": {"id": turn, "status": "completed"},
},
}
)
elif mode in {"prompt", "child-prompt"}:
if mode == "child-prompt":
emit(
{
"method": "thread/started",
"params": {
"thread": {
"id": "child-1",
"source": {
"subAgent": {
"thread_spawn": {
"parent_thread_id": "native-1",
"depth": 1,
}
}
},
}
},
}
)
emit(
{
"method": "item/commandExecution/requestApproval",
"id": 0,
"params": {
"threadId": "child-1" if mode == "child-prompt" else "native-1",
"turnId": turn,
"itemId": "command",
"command": "echo test",
"availableDecisions": ["accept", "decline", "cancel"],
},
}
)
else:
emit(
{
"method": "item/agentMessage/delta",
"params": {
"threadId": "native-1",
"turnId": turn,
"itemId": "answer",
"delta": "snow ☃ " * 16000,
},
}
)
emit(
{
"method": "item/completed",
"params": {
"threadId": "native-1",
"turnId": turn,
"item": {
"id": "answer",
"type": "agentMessage",
"text": "snow ☃ " * 16000,
},
},
}
)
emit(
{
"method": "turn/completed",
"params": {
"threadId": "native-1",
"turn": {"id": turn, "status": "completed"},
},
}
)
emit(
{"id": request_id, "result": {"turn": {"id": turn, "status": "inProgress"}}}
)
elif method is None and request_id == 0:
emit(
{
"method": "serverRequest/resolved",
"params": {"threadId": "native-1", "requestId": 0},
}
)
emit(
{
"method": "item/completed",
"params": {
"threadId": "native-1",
"turnId": turn,
"item": {
"id": "answer",
"type": "agentMessage",
"text": request["result"]["decision"],
},
},
}
)
emit(
{
"method": "turn/completed",
"params": {
"threadId": "native-1",
"turn": {"id": turn, "status": "completed"},
},
}
)
elif method == "test/eof":
break
elif method == "test/malformed":
sys.stdout.buffer.write(b"invalid-json\n")
sys.stdout.buffer.flush()
elif method == "test/malformed-flood":
sys.stdout.buffer.write(b"invalid-json\n" + b"x" * 500_000 + b"\n")
sys.stdout.buffer.flush()
else:
emit({"id": request_id, "result": {}})
if mode.startswith("factory-"):
# The catalog must still be readable when the native process exits.
catalog = json.loads(Path(sys.argv[2]).read_text()) if sys.argv[2] else None
Path(sys.argv[3]).write_text(json.dumps(catalog))
if mode == "child-warning-on-close":
emit(
{
"method": "guardianWarning",
"params": {"threadId": "child", "message": "Child is shutting down"},
}
)
# Keep stdout alive until the parent has processed this close-time event.
while not Path(sys.argv[2]).exists():
time.sleep(0.01)
|