diff --git a/apps/albedo/src/albedo/http/api_digest.gleam b/apps/albedo/src/albedo/http/api_digest.gleam index 5de492634..a423a18e6 100644 --- a/apps/albedo/src/albedo/http/api_digest.gleam +++ b/apps/albedo/src/albedo/http/api_digest.gleam @@ -2,4 +2,4 @@ //// GET /server reports it so a client built from another contract can tell. pub const digest = - "f232497ce0f3d13a04d4e730e54122cd2c141cb5f26131e164e60407174adae4" + "7ec3fe9ae9c123976ec8ae30a093bca4199323910aa727aab1abea13c6bed30a" diff --git a/apps/albedo/src/albedo/kernel/jobs.gleam b/apps/albedo/src/albedo/kernel/jobs.gleam index afba40392..620ea8a02 100644 --- a/apps/albedo/src/albedo/kernel/jobs.gleam +++ b/apps/albedo/src/albedo/kernel/jobs.gleam @@ -8,6 +8,7 @@ import albedo/kernel.{type Kernel} import albedo/kernel/driver import albedo/kernel/job_window +import albedo_bytes/scalars import albedo_sdk/kernel.{ type JobFailure, type JobPage, type JobRecord, Exited, JobPage, JobRecord, NoSuchJob, OutputLost, Rejected, Running, Unavailable, @@ -84,7 +85,12 @@ fn read( use offset <- decode.field("offset", decode.int) use next_offset <- decode.field("next_offset", decode.int) use text <- decode.field("text", decode.string) - decode.success(JobPage(job, offset, next_offset, text)) + use source <- decode.optional_field( + "source", + None, + decode.optional(decode.string), + ) + decode.success(JobPage(job, offset, next_offset, text, bound_source(source))) }) } @@ -153,17 +159,23 @@ fn saved_read( limit, ) { - Ok(#(start, end, text)) -> Ok(JobPage(job.record, start, end, text)) + Ok(#(start, end, text)) -> + Ok(JobPage(job.record, start, end, text, job.source)) Error(_) -> Error(OutputLost("requested output was not retained")) } } +fn bound_source(source: Option(String)) -> Option(String) { + option.map(source, scalars.take(_, sdk_kernel.max_job_source)) +} + type Saved { Saved( record: JobRecord, spill: Option(String), spilled: Int, tail: Option(BitArray), + source: Option(String), ) } @@ -181,6 +193,11 @@ fn saved(home: String, session: String) -> Result(List(Saved), JobFailure) { use spill <- decode.field("spill", decode.optional(decode.string)) use spilled <- decode.field("spilled", decode.int) use tail <- decode.field("tail", decode.optional(decode.string)) + use source <- decode.optional_field( + "source", + None, + decode.optional(decode.string), + ) decode.success(Saved( record, spill, @@ -188,6 +205,7 @@ fn saved(home: String, session: String) -> Result(List(Saved), JobFailure) { option.then(tail, fn(text) { bit_array.base64_decode(text) |> option.from_result }), + bound_source(source), )) } json.parse( diff --git a/cli/internal/daemon/protocol/api.gen.go b/cli/internal/daemon/protocol/api.gen.go index 028d46ca6..bea84eab1 100644 --- a/cli/internal/daemon/protocol/api.gen.go +++ b/cli/internal/daemon/protocol/api.gen.go @@ -1831,6 +1831,9 @@ type JobOutputPage struct { // Offset Byte the page starts at. Offset Counter `json:"offset"` + + // Source Full cell source when `job.kind` is `cell`, bounded to 16384 characters. + Source *string `json:"source" nullable:"true"` Text string `json:"text"` } diff --git a/cli/internal/daemon/protocol/digest.gen.go b/cli/internal/daemon/protocol/digest.gen.go index 26abfe755..fe7bfc0ca 100644 --- a/cli/internal/daemon/protocol/digest.gen.go +++ b/cli/internal/daemon/protocol/digest.gen.go @@ -5,4 +5,4 @@ package protocol // APIDigest identifies the wire contract these types were generated from. // The daemon reports its own in GET /server; a different value means this // client's decoders may reject what the daemon sends. -const APIDigest = "f232497ce0f3d13a04d4e730e54122cd2c141cb5f26131e164e60407174adae4" +const APIDigest = "7ec3fe9ae9c123976ec8ae30a093bca4199323910aa727aab1abea13c6bed30a" diff --git a/docs/openapi.yaml b/docs/openapi.yaml index a252510c9..4457231c9 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -12707,7 +12707,15 @@ components: text: type: string maxLength: 65536 - required: [job, offset, next_offset, text] + source: + anyOf: + - type: string + maxLength: 16384 + - type: 'null' + description: + Full cell source when `job.kind` is `cell`, bounded to 16384 + characters. + required: [job, offset, next_offset, text, source] additionalProperties: false JobStop: type: object diff --git a/e2e/cell_background_test.py b/e2e/cell_background_test.py index ac649be3c..f6893075f 100644 --- a/e2e/cell_background_test.py +++ b/e2e/cell_background_test.py @@ -38,6 +38,8 @@ class CellBackgroundTests(unittest.TestCase): self.with_job = False self.stop_job_on_release = False self.cell_title = "" + self.cell_padding = "" + self.cell_code = None self.cell_id = None def script(request): @@ -71,8 +73,11 @@ class CellBackgroundTests(unittest.TestCase): code = "held = run('sleep', '30')\n" + code if self.cell_title: code = f"# {self.cell_title}\n" + code + if self.cell_padding: + code = code + f"\n# {self.cell_padding}" if self.synchronous: code = "import threading\nprint('before detach')\nthreading.Event().wait(10)\nprint('after detach')" + self.cell_code = code if self.deadline: return Reply( "python", tool_arguments={"code": code, "timeout_ms": 1800} @@ -292,6 +297,7 @@ class CellBackgroundTests(unittest.TestCase): by_id = {item["id"]: item for item in recent} self.assertEqual(by_id[self.cell_id]["kind"], "cell") self.assertEqual(by_id[self.cell_id]["state"], "running") + self.assertNotIn("source", by_id[self.cell_id]) self.assertEqual(by_id[children[0]["id"]]["parent_id"], self.cell_id) # reading output through /jobs/{id}/output must not suppress the wake @@ -302,6 +308,14 @@ class CellBackgroundTests(unittest.TestCase): ) self.assertIn("before detach", page["text"]) self.assertEqual(page["job"]["kind"], "cell") + self.assertEqual(page["source"], self.cell_code) + + child_page = json.loads( + self.app.api( + f"/extensions/run/sessions/{session}/jobs/{children[0]['id']}/output" + ).read() + ) + self.assertIsNone(child_page["source"]) self.prompt(session, "release cell") wait_for(lambda: self.wakes()) @@ -319,6 +333,25 @@ class CellBackgroundTests(unittest.TestCase): self.assertEqual(finished[self.cell_id]["state"], "exited") self.assertEqual(finished[self.cell_id]["exit_code"], 0) self.assertEqual(finished[children[0]["id"]]["parent_id"], self.cell_id) + finished_page = json.loads( + self.app.api( + f"/extensions/run/sessions/{session}/jobs/{self.cell_id}/output" + ).read() + ) + self.assertEqual(finished_page["source"], self.cell_code) + + def test_background_cell_source_on_output_page_is_bounded(self): + self.cell_title = "long quiet cell" + self.cell_padding = "x" * 20_000 + session = self.start() + page = json.loads( + self.app.api( + f"/extensions/run/sessions/{session}/jobs/{self.cell_id}/output" + ).read() + ) + assert self.cell_code is not None + self.assertEqual(page["source"], self.cell_code[:16_384]) + self.assertEqual(len(page["source"]), 16_384) def test_stopping_a_background_cell_promotes_its_surviving_child(self): self.with_job = True diff --git a/packages/albedo_sdk/src/albedo_sdk/kernel.gleam b/packages/albedo_sdk/src/albedo_sdk/kernel.gleam index 0a56e9410..bad65e266 100644 --- a/packages/albedo_sdk/src/albedo_sdk/kernel.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/kernel.gleam @@ -88,9 +88,19 @@ pub type JobRecord { ) } -/// A page of a job's output: the bytes it spans and the text they hold. +/// The longest cell source `JobPage` carries, in Unicode scalars. +pub const max_job_source = 16_384 + +/// A page of a job's output: the bytes it spans, the text they hold, and +/// a backgrounded cell's bounded source. pub type JobPage { - JobPage(job: JobRecord, offset: Int, next_offset: Int, text: String) + JobPage( + job: JobRecord, + offset: Int, + next_offset: Int, + text: String, + source: Option(String), + ) } /// Why a job book read failed. @@ -124,6 +134,7 @@ pub fn page_json(page: JobPage) -> json.Json { #("offset", json.int(page.offset)), #("next_offset", json.int(page.next_offset)), #("text", json.string(page.text)), + #("source", json.nullable(page.source, json.string)), ]) } diff --git a/python/albedo_cells.py b/python/albedo_cells.py index b9f417ace..183c21a0a 100644 --- a/python/albedo_cells.py +++ b/python/albedo_cells.py @@ -54,6 +54,7 @@ CLEANUP_DEADLINE = ( 1.5 # seconds: plugins must finish cleanup inside the supervisor's patience ) LIMITS = {"cell": 16, "job": 64, "native": 1} # retained captures per kind +MAX_JOB_SOURCE = 16_384 CELL: contextvars.ContextVar[Capture | None] = contextvars.ContextVar( "cell", default=None ) @@ -1983,7 +1984,7 @@ async def serve_job_stop(job_id: str) -> None: def cell_job_record(entry: dict[str, object]) -> dict[str, object]: - return {k: v for k, v in entry.items() if k != "capture"} | { + return {k: v for k, v in entry.items() if k not in ("capture", "source")} | { "bytes": cast(Capture, entry["capture"]).seen } @@ -2008,6 +2009,7 @@ def record_backgrounded(capture: Capture, code: str, started_at: int) -> None: BACKGROUNDED[capture.id] = { "id": capture.id, "command": title or "python cell", + "source": code[:MAX_JOB_SOURCE], "state": "running", "exit_code": None, "timed_out": False, @@ -2056,6 +2058,7 @@ async def view_background_cells(msg: dict[str, object]) -> dict[str, object] | N "offset": s, "next_offset": n, "text": t, + "source": e.get("source"), } @@ -2066,10 +2069,12 @@ def save_backgrounded_jobs(path: str) -> list[dict[str, object]]: for e in entries: if p := cast(Capture, e["capture"]).persist(): rec, tail = cell_job_record(e), p.get("tail") - saved.append(rec) + source = e.get("source") + saved.append({**rec, "source": source} if isinstance(source, str) else rec) jobs.append( { **rec, + **({"source": source} if isinstance(source, str) else {}), "spill": p["spill"], "spilled": p["spilled"], "tail": base64.b64encode(tail).decode() diff --git a/robot-docs/kernel.md b/robot-docs/kernel.md index 10fb03a15..eabf35ac6 100644 --- a/robot-docs/kernel.md +++ b/robot-docs/kernel.md @@ -220,10 +220,11 @@ recorded kernel-process attribution to the agents extension as the Cells that outlast the foreground timeout (`status=backgrounded`) also appear in the job observation (`running_jobs`, `/jobs`, and `/jobs/recent`) with `kind: "cell"` and their title comment or first line, expose their live output -through the job-output route without withdrawing their completion notice, and -can be stopped through the job-stop route. Live `run` jobs started inside a -still-running backgrounded cell carry `parent_id` naming that cell and promote -to top-level if they outlive it. `live_job_count` counts top-level items. +and bounded source (`source`, up to 16,384 characters) through the job-output +route without withdrawing their completion notice, and can be stopped through +the job-stop route. Live `run` jobs started inside a still-running backgrounded +cell carry `parent_id` naming that cell and promote to top-level if they outlive +it. `live_job_count` counts top-level items. ## Unresponsive kernels diff --git a/web/src/lib/api.gen.d.ts b/web/src/lib/api.gen.d.ts index d3c0f5afd..41e5bb235 100644 --- a/web/src/lib/api.gen.d.ts +++ b/web/src/lib/api.gen.d.ts @@ -3971,6 +3971,8 @@ export interface components { /** @description Byte to continue from; equal to `offset` when there is nothing more yet. */ next_offset: components["schemas"]["Counter"]; text: string; + /** @description Full cell source when `job.kind` is `cell`, bounded to 16384 characters. */ + source: string | null; }; /** * @example {