diff --git a/priv/python/albedo_kernel.py b/priv/python/albedo_kernel.py index 72b0882d9..542027ff5 100644 --- a/priv/python/albedo_kernel.py +++ b/priv/python/albedo_kernel.py @@ -735,6 +735,7 @@ class Cells: outer.write( f"[image from cell {cell['id']} dropped: {dropped}]\n" ) + deliver_trace(capture) _ = await host( "cells.finish", { @@ -1232,6 +1233,12 @@ async def serve_invoke(message: albedo_api.Invoke) -> None: send(reply) +def deliver_trace(capture: Capture) -> None: + payload = capture.trace.finish() + send({"type": "trace", "id": capture.id, "trace": payload}) + capture.trace.release() + + async def serve(): global active, active_capture while True: @@ -1276,7 +1283,7 @@ async def serve(): active_capture = None CELL.reset(token) duration = round(LOOP.time() - began, 3) - send({"type": "trace", "id": capture.id, "trace": capture.trace.finish()}) + deliver_trace(capture) send( { "type": "done", diff --git a/priv/python/albedo_trace.py b/priv/python/albedo_trace.py index 957e1d8f6..221ae52f4 100644 --- a/priv/python/albedo_trace.py +++ b/priv/python/albedo_trace.py @@ -56,8 +56,11 @@ class Trace: self.activities: dict[tuple[str, str], dict[str, str]] = {} self.before: dict[str, str | None] = {} self.truncated: bool = False + self.sealed: bool = False def activity(self, kind: str, target: object) -> None: + if self.sealed: + return if len(self.activities) >= 64: self.truncated = True else: @@ -65,7 +68,7 @@ class Trace: self.activities[(kind, target)] = {"kind": kind, "target": target} def renamed(self, source: str, destination: str) -> None: - if source == destination: + if self.sealed or source == destination: return # Atomic editors write a temporary file then replace the real target. # The temporary file is not an edit; snapshot the destination before rename. @@ -73,7 +76,7 @@ class Trace: self.before.pop(source, None) def writing(self, path: str) -> None: - if path in self.before: + if self.sealed or path in self.before: return if len(self.before) >= 16: self.truncated = True @@ -81,6 +84,9 @@ class Trace: self.before[path] = snapshot(path) def finish(self) -> dict[str, object]: + if self.sealed: + raise RuntimeError("trace is already sealed") + self.sealed = True with unobserved(): changes: list[dict[str, str | int]] = [] for path, before in self.before.items(): @@ -122,11 +128,19 @@ class Trace: } ) return { - "activities": list(self.activities.values()), + "activities": [ + activity.copy() for activity in self.activities.values() + ], "changes": changes, "truncated": self.truncated, } + def release(self) -> None: + if not self.sealed: + raise RuntimeError("trace must be sealed before release") + self.before.clear() + self.activities.clear() + def snapshot(path: str) -> str | None: try: @@ -170,7 +184,7 @@ def install(get_capture: Callable[[], TracedCapture | None]) -> None: def audit(event: str, args: tuple[object, ...]) -> None: capture = get_capture() - if capture is None or GUARD.get(): + if capture is None or capture.trace.sealed or GUARD.get(): return token = GUARD.set(True) try: @@ -200,7 +214,8 @@ def install(get_capture: Callable[[], TracedCapture | None]) -> None: elif event == "subprocess.Popen": capture.trace.activity("run", command(args[1])) except Exception: - capture.trace.truncated = True + if not capture.trace.sealed: + capture.trace.truncated = True finally: GUARD.reset(token) diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index 7f6cc5a59..46a35712a 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -115,6 +115,10 @@ on the wire, responses sends the images inside `function_call_output.output`, af Retained kernel captures release image bytes after delivery, including error and interrupted results. Top-level delivery completes when the control-stream write returns. Nested `cells.run` delivery completes when the host acknowledges `cells.finish`, after images propagate to the parent. Failed delivery keeps the capture's images. Retained text and durable image results keep their existing lifetimes; durable images remain available after daemon restart. +The kernel seals each trace after execution cleanup, including failed and interrupted cells. Sealing permanently stops observation, even for background tasks that inherit the cell context. Finalization returns a detached payload and cannot run again. The kernel sends that payload through the existing `trace` event, then releases snapshots and activities when the synchronous control-stream write returns. Finalization or write failure leaves the trace sealed with its buffers retained; there is no retry. Release preserves truncation metadata. + +Top-level traces emit before `done`. Nested `cells.run` traces emit before waiting for the host's `cells.finish` acknowledgement and remain separate from the parent trace. The daemon persists these events when the enclosing top-level execution completes, after which `cells.trace` can read the results independently of kernel buffers. This lifecycle applies to newly started kernels and does not change output retention, image delivery, or background task execution. + interfaces: [`PythonApi`](../priv/python/albedo_api.py), [`work`](../src/albedo/harness/work.gleam), [`compaction strategy`](../src/albedo/harness/compaction.gleam). Kernel control messages are checked by `priv/python/albedo_protocol.py` before delivery to the event loop. Invalid JSON, oversized frames, unknown message types, and invalid fields enter the kernel shutdown path, including plugin cleanup and child-process cleanup. Host replies are checked before their pending futures are removed or completed. Extra fields on known messages are accepted. Missing methods, expired references, and tool exceptions remain call errors.