diff --git a/brokkr-ctl/src/blender.rs b/brokkr-ctl/src/blender.rs index e2a7a40..36e7245 100644 --- a/brokkr-ctl/src/blender.rs +++ b/brokkr-ctl/src/blender.rs @@ -267,18 +267,34 @@ fn wait_for_receipt( id: &str, limit: Duration, ) -> Result { - let started = Instant::now(); - loop { - if let Some(receipt) = read_receipt(store, id)? { + match poll_until(limit, POLL, || read_receipt(store, id))? { + Some(receipt) => { print_fields(&receipt); let status = receipt.get("status").and_then(Value::as_str).unwrap_or(""); - return Ok(Outcome::from_status(status).code()); + Ok(Outcome::from_status(status).code()) + } + None => { + println!("status: timeout"); + Ok(Outcome::Timeout.code()) + } + } +} + +/// Reads until `read` yields a value or `limit` passes, sleeping `poll` between reads. +fn poll_until( + limit: Duration, + poll: Duration, + mut read: impl FnMut() -> Result>, +) -> Result> { + let started = Instant::now(); + loop { + if let Some(found) = read()? { + return Ok(Some(found)); } if started.elapsed() >= limit { - println!("status: timeout"); - return Ok(Outcome::Timeout.code()); + return Ok(None); } - thread::sleep(POLL); + thread::sleep(poll); } } @@ -617,6 +633,25 @@ mod tests { assert!(build_intent("../x", "abc", "a/b", "", &[]).is_err()); } + #[test] + fn polling_sleeps_between_reads_and_stops_at_the_limit() { + let mut reads = 0; + let found: Option = poll_until(Duration::from_millis(400), Duration::from_millis(100), || { + reads += 1; + Ok(None) + }) + .unwrap(); + assert_eq!(found, None); + assert!((2..=6).contains(&reads), "{reads} reads: the sleep paces them"); + let mut reads = 0; + let found = poll_until(Duration::from_secs(5), Duration::from_millis(100), || { + reads += 1; + Ok((reads == 3).then_some("receipt")) + }) + .unwrap(); + assert_eq!((found, reads), (Some("receipt"), 3)); + } + #[test] fn statuses_map_to_exit_codes() { for (status, code) in [("accepted", 0), ("denied", 2), ("failed", 2), ("stopped", 3), ("weird", 2)] { diff --git a/brokkr-ctl/tests/cli.rs b/brokkr-ctl/tests/cli.rs index c3df5a6..6161f6b 100644 --- a/brokkr-ctl/tests/cli.rs +++ b/brokkr-ctl/tests/cli.rs @@ -105,6 +105,16 @@ fn a_missing_store_is_not_created() { assert!(!path.exists()); } +#[test] +fn wait_matches_the_exact_receipt_key() { + let dir = tempfile::tempdir().unwrap(); + // receipts whose keys merely end with, or start with, the waited id are other commands' receipts + let path = store_with_receipts(dir.path(), &[("xr1", "accepted"), ("r1x", "accepted")]); + let output = ctl(&path, &["wait", "r1", "--timeout", "0.3"], None); + assert_eq!(output.status.code(), Some(4), "{}", stdout(&output)); + assert!(stdout(&output).contains("status: timeout")); +} + #[test] fn exit_code_follows_the_receipt() { let dir = tempfile::tempdir().unwrap(); diff --git a/docs/architecture.md b/docs/architecture.md index 9b102a9..843dbeb 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -100,7 +100,7 @@ Derived State: - Unity package settings are adapter configuration, not Verse authority. - Blender add-on preferences are adapter configuration, not Verse authority. -- Blender JSON debug exports are inspection/import probes, not mirror authority. +- Blender JSON debug exports are export-only inspection output, not mirror authority and never read back as intents. - Unity and Blender sync UI fields are command/edit affordances; the `brokkr.sync.*` documents are the shared sync policy surface. - Ad hoc syncvar editors write `brokkr.sync.var.v0` policy records only; they diff --git a/docs/live-collab-target.md b/docs/live-collab-target.md index fcfedaa..2853e5a 100644 --- a/docs/live-collab-target.md +++ b/docs/live-collab-target.md @@ -99,7 +99,7 @@ resident service on that number); reading a record's stored version through Aeth **One agent turn primitive.** Every agent mutation in Blender goes through one function in the add-on: admit (sink enabled, `requestedBy` present, id unused, action typed and valid), run the action with every operator called `undo=False`, push exactly one undo -step labelled `Brokkr : `, write the receipt. A +step labelled `Brokkr : `, write the receipt. A refusal caught before any write pushes nothing and leaves the scene unchanged; an action that fails after writing pushes one step labelled `(failed)`, so the partial write is one undoable step. There is no second @@ -254,7 +254,7 @@ are the primitive for the push. | Blender command intent `brokkr.blender.command_intent.v0` (gains `requestedBy`, `note`) | `blender/commands/{commandId}`; ids single-use, minted by the writer (`brush-tree:ruling:blender-sink-mirrors-unity`) | Written once by a caller. Executed at most once or refused. Removed when its receipt is written. Never rewritten. | Callers write. The agent turn primitive alone decides admission and execution. | | Blender command receipt `brokkr.blender.command_receipt.v0` (gains `requestedBy`, `outcome` in accepted, denied, stopped, failed, `undoLabel`, `lookPath`) | `blender/receipts/{commandId}`, one per intent | Written once, after the primitive decides. Pruned by count with Unity's cap and prune rule. | The primitive alone. Callers and the panel read. | | Agent identity `requestedBy` | `/` stated by the caller | Carried on each intent into its receipt and undo label; not stored on its own. | The caller states it; the primitive refuses an intent without it. The session token proves admission, not identity. | -| Agent undo step | Blender undo entry `Brokkr : ` | One per accepted mutating intent; lives as Blender's stack does; never persisted by Brokkr. | Blender owns the stack; the primitive pushes; only the human undoes or redoes. | +| Agent undo step | Blender undo entry `Brokkr : ` | One per accepted mutating intent; lives as Blender's stack does; never persisted by Brokkr. | Blender owns the stack; the primitive pushes; only the human undoes or redoes. | | Blender session id | uuid minted at add-on register and on each `load_post` | Replaced when Blender restarts or a file loads. | The add-on alone. | | Human-edit sequences (snapshot: `humanEditSeq`, per-object `lastHumanEditSeq`) | Integers within a session id | Monotonic within a session; reset with it. Information for the agent; they gate nothing. | The add-on's depsgraph and undo handlers alone. | | Host snapshot `brokkr.blender.host_snapshot.v0` (gains Edit Mode selection, session id, sequences, sink state, authoring binding summary) | `blender/host/current`, overwritten | Refreshed on depsgraph update and after each intent. | The add-on alone. | diff --git a/surfaces/blender/brokkr_bridge/blender_target.py b/surfaces/blender/brokkr_bridge/blender_target.py index 8684e8e..8302a69 100644 --- a/surfaces/blender/brokkr_bridge/blender_target.py +++ b/surfaces/blender/brokkr_bridge/blender_target.py @@ -542,7 +542,7 @@ class BrokkrBlenderTarget: self._receipted.add(command_id) return self._receipt(command, "denied", denial, "") - label = f"Brokkr {requested_by}: {action} {command_id[:8]}" + label = f"Brokkr {requested_by}: {action} {command_id[-8:]}" # the tail: uuid v7 ids lead with a timestamp, one value per minute push = action not in READ_ACTIONS and action not in NO_SCENE_CHANGE_ACTIONS self.agent_turn = True self._wrote = True diff --git a/surfaces/blender/brokkr_bridge/node_actions.py b/surfaces/blender/brokkr_bridge/node_actions.py index aba236d..389a536 100644 --- a/surfaces/blender/brokkr_bridge/node_actions.py +++ b/surfaces/blender/brokkr_bridge/node_actions.py @@ -254,6 +254,7 @@ class NodeActions: doomed = [l for l in tree.links if l.to_socket == into and (out is None or l.from_socket == out)] for link in doomed: tree.links.remove(link) + self.changed = bool(doomed) # no link matched: no undo step return f"Removed {len(doomed)} link(s).", tree.name def _linkSockets(self, command: dict[str, Any]) -> tuple[str, str]: diff --git a/surfaces/blender/tests/live/agent_turn_queue.py b/surfaces/blender/tests/live/agent_turn_queue.py index f2b2d3c..1521ba0 100644 --- a/surfaces/blender/tests/live/agent_turn_queue.py +++ b/surfaces/blender/tests/live/agent_turn_queue.py @@ -39,6 +39,8 @@ bt = importlib.import_module(MODULE + ".blender_target") try: import msgpack R["msgpack"] = msgpack.__file__ + R["msgpack_packer"] = msgpack.Packer.__module__ # msgpack.fallback: the pure-Python codec, no C extension + R["msgpack_purepython_env"] = os.environ.get("MSGPACK_PUREPYTHON", "") except ImportError: R["msgpack"] = None prefs = bpy.context.preferences.addons[MODULE].preferences @@ -144,7 +146,7 @@ def q1_check(): R["receipts"]["q1"] = r S["q1_label"] = r and r.get("undoLabel") check("queue: createObject drained by the add-on timer, accepted, labelled, attributed", - r and r["status"] == "accepted" and r["undoLabel"] == "Brokkr nibu/hands: createObject aaaaaaaa" + r and r["status"] == "accepted" and r["undoLabel"] == "Brokkr nibu/hands: createObject aaa-0001" # the id's last 8 characters and r["requestedBy"] == "nibu/hands" and r["note"] == "queue test" and "QCube" in names(), r) check("queue: a command JSON under the debug root is inert (kept, never run, never receipted); the receipt export still lands", (SCRATCH / "debug" / "blender" / "commands" / "door-0001.json").exists() and "QDoor" not in names() diff --git a/surfaces/blender/tests/live/agent_turn_queue.result.json b/surfaces/blender/tests/live/agent_turn_queue.result.json index e76bb3f..e1c803e 100644 --- a/surfaces/blender/tests/live/agent_turn_queue.result.json +++ b/surfaces/blender/tests/live/agent_turn_queue.result.json @@ -15,8 +15,8 @@ "objectName": "QCube", "requestedBy": "nibu/hands", "note": "queue test", - "undoLabel": "Brokkr nibu/hands: createObject aaaaaaaa", - "observedAt": "2026-11-27T19:32:00.583673+00:00" + "undoLabel": "Brokkr nibu/hands: createObject aaa-0001", + "observedAt": "2026-11-27T20:02:43.826604+00:00" } }, { @@ -46,8 +46,8 @@ "objectName": "QCube", "requestedBy": "nibu/hands", "note": "queue test", - "undoLabel": "Brokkr nibu/hands: createObject aaaaaaaa", - "observedAt": "2026-11-27T19:32:00.583673+00:00" + "undoLabel": "Brokkr nibu/hands: createObject aaa-0001", + "observedAt": "2026-11-27T20:02:43.826604+00:00" } }, { @@ -63,7 +63,7 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:02.587784+00:00" + "observedAt": "2026-11-27T20:02:45.831074+00:00" } }, { @@ -79,7 +79,7 @@ "requestedBy": "", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:04.590532+00:00" + "observedAt": "2026-11-27T20:02:47.837505+00:00" } }, { @@ -134,8 +134,8 @@ "objectName": "QCube", "requestedBy": "nibu/hands", "note": "queue test", - "undoLabel": "Brokkr nibu/hands: createObject aaaaaaaa", - "observedAt": "2026-11-27T19:32:00.583673+00:00" + "undoLabel": "Brokkr nibu/hands: createObject aaa-0001", + "observedAt": "2026-11-27T20:02:43.826604+00:00" }, "q2_original": { "schema": "brokkr.blender.command_receipt.v0", @@ -146,8 +146,8 @@ "objectName": "QCube", "requestedBy": "nibu/hands", "note": "queue test", - "undoLabel": "Brokkr nibu/hands: createObject aaaaaaaa", - "observedAt": "2026-11-27T19:32:00.583673+00:00" + "undoLabel": "Brokkr nibu/hands: createObject aaa-0001", + "observedAt": "2026-11-27T20:02:43.826604+00:00" }, "q2_denial": { "schema": "brokkr.blender.command_receipt.v0", @@ -159,7 +159,7 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:02.587784+00:00" + "observedAt": "2026-11-27T20:02:45.831074+00:00" }, "q3": { "schema": "brokkr.blender.command_receipt.v0", @@ -171,12 +171,12 @@ "requestedBy": "", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:04.590532+00:00" + "observedAt": "2026-11-27T20:02:47.837505+00:00" }, "q6": [ { "schema": "brokkr.blender.command_receipt.v0", - "commandId": "8fe2e26fece047bcaca1b9938f1a030c", + "commandId": "3414cf427aa549ea9e9a52c4cfc61908", "action": "createObject", "status": "denied", "message": "Intent has no commandId of 1 to 128 letters, digits, '_' or '-'. (queue key blender/commands/bad-int)", @@ -184,11 +184,11 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:08.599229+00:00" + "observedAt": "2026-11-27T20:02:51.875508+00:00" }, { "schema": "brokkr.blender.command_receipt.v0", - "commandId": "b1a5c7e22158468bb06f6b500c4fa2a7", + "commandId": "3c53313e68aa4d07ab6e3e330396567d", "action": "createObject", "status": "denied", "message": "Intent has no commandId of 1 to 128 letters, digits, '_' or '-'. (queue key blender/commands/bad-list)", @@ -196,7 +196,7 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:08.621069+00:00" + "observedAt": "2026-11-27T20:02:51.901697+00:00" } ], "q5": [ @@ -210,7 +210,7 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:10.606424+00:00" + "observedAt": "2026-11-27T20:02:53.881094+00:00" }, { "schema": "brokkr.blender.command_receipt.v0", @@ -222,7 +222,7 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:10.629698+00:00" + "observedAt": "2026-11-27T20:02:53.905589+00:00" }, { "schema": "brokkr.blender.command_receipt.v0", @@ -234,10 +234,12 @@ "requestedBy": "nibu/hands", "note": "", "undoLabel": "", - "observedAt": "2026-11-27T19:32:10.665150+00:00" + "observedAt": "2026-11-27T20:02:53.944684+00:00" } ] }, - "msgpack": "F:\\aeth-wt\\brokkr-live-1.e2e\\res\\extensions\\.local\\lib\\python3.13\\site-packages\\msgpack\\__init__.py", + "msgpack": "F:\\aeth-wt\\brokkr-live-pins.e2e\\res\\extensions\\.local\\lib\\python3.13\\site-packages\\msgpack\\__init__.py", + "msgpack_packer": "msgpack.fallback", + "msgpack_purepython_env": "", "ok": true } \ No newline at end of file diff --git a/surfaces/blender/tests/test_agent_turn.py b/surfaces/blender/tests/test_agent_turn.py index 02706de..cf78e9b 100644 --- a/surfaces/blender/tests/test_agent_turn.py +++ b/surfaces/blender/tests/test_agent_turn.py @@ -56,12 +56,24 @@ class AgentTurnTests(unittest.TestCase): def test_accepted_receipt_carries_undo_label(self): receipt = self.run_action(action="createObject", objectType="EMPTY", commandId="abcdef0123456789", requestedBy="nibu/hands", note="x" * 300) - label = "Brokkr nibu/hands: createObject abcdef01" + label = "Brokkr nibu/hands: createObject 23456789" # the id's tail: uuid v7 ids lead with a timestamp self.assertEqual(self.bpy.undo.pushes, [label]) self.assertEqual(receipt["undoLabel"], label) self.assertEqual(receipt["requestedBy"], "nibu/hands") self.assertEqual(len(receipt["note"]), 200) + def test_two_sends_a_minute_apart_get_distinct_labels(self): + # brokkr-ctl ids are uuid v7: the first 8 hex are one value per 65 seconds, the tail is random + self.run_action(action="createObject", objectType="EMPTY", commandId="01a21e88" + "0" * 16 + "aaaa1111") + self.run_action(action="createObject", objectType="EMPTY", commandId="01a21e88" + "0" * 16 + "bbbb2222") + self.assertEqual(len(set(self.bpy.undo.pushes)), 2, self.bpy.undo.pushes) + + def test_architecture_page_does_not_call_the_debug_json_an_import_path(self): + page = (SOURCE.parents[3] / "docs" / "architecture.md").read_text(encoding="utf-8") + for line in page.splitlines(): + if "debug" in line.lower() and "json" in line.lower(): + self.assertNotRegex(line.lower(), r"import", line) + def test_refusal_pushes_nothing(self): receipt = self.run_action(action="createObject", objectType="BOGUS") self.assertEqual(receipt["status"], "failed") @@ -79,7 +91,7 @@ class AgentTurnTests(unittest.TestCase): self.bpy.ops.object.empty_add = boom receipt = self.run_action(action="createObject", objectType="EMPTY", commandId="deadbeef99") self.assertEqual(receipt["status"], "failed") - self.assertEqual(self.bpy.undo.pushes, ["Brokkr nibu/hands: createObject deadbeef (failed)"]) + self.assertEqual(self.bpy.undo.pushes, ["Brokkr nibu/hands: createObject adbeef99 (failed)"]) def test_agent_turn_flag_is_scoped(self): seen = [] diff --git a/surfaces/blender/tests/test_node_actions.py b/surfaces/blender/tests/test_node_actions.py index 6aeee39..f69cc07 100644 --- a/surfaces/blender/tests/test_node_actions.py +++ b/surfaces/blender/tests/test_node_actions.py @@ -559,7 +559,11 @@ class NodeActionTests(unittest.TestCase): self.accepted(action="linkSockets", treeKey="G", fromNode="a", fromSocket="Out", toNode="c", toSocket="A") self.accepted(action="linkSockets", treeKey="G", fromNode="b", fromSocket="Out", toNode="c", toSocket="Size") self.refused(action="unlinkSockets", treeKey="G", fromNode="zzz", fromSocket="Out", toNode="c", toSocket="A") - self.assertIn("Removed 0", self.accepted(action="unlinkSockets", treeKey="G", fromNode="b", fromSocket="Out", toNode="c", toSocket="A")["message"]) + pushes = len(self.bpy.undo.pushes) + empty = self.accepted(action="unlinkSockets", treeKey="G", fromNode="b", fromSocket="Out", toNode="c", toSocket="A") + self.assertIn("Removed 0", empty["message"]) + self.assertEqual(len(self.bpy.undo.pushes), pushes) # nothing changed: no undo step + self.assertEqual(empty["undoLabel"], "") self.assertEqual(len(tree.links), 2) self.assertIn("Removed 1", self.accepted(action="unlinkSockets", treeKey="G", fromNode="a", fromSocket="Out", toNode="c", toSocket="A")["message"]) self.assertEqual(len(tree.links), 1)