Skip to content

Commit a589de0

Browse files
committed
Rewrite app_debug on datasette-agent browser tasks
Replaces the ask_user() workaround with the browser-task primitive from the plans/agent-browser-tasks.md spec, now implemented in datasette-agent: the tool suspends with context.browser_task(), passing a generic harness bootstrap as the task HTML and everything run-specific (frame URL, tokens, the debug script, viewport, timeout) as the payload, which the runtime hands out exactly once through its atomic claim. The harness completes via window.datasetteAgent instead of scraping the question form, and the result envelope arrives as browser_task()'s return value. - Drop the /-/apps/debug/{job}/claim and /result endpoints: one-shot claiming and result delivery are the runtime's job now - /frame and /query keep serving only while the job is pending and within its expiry window; job status simplifies to pending | completed | expired - The resumed tool call records the envelope on the job row, keeping the audit trail, and reports expired/cancelled outcomes as data - Harness HTML is a static bootstrap with nothing job-specific in it; task identity comes from the runtime's rendered task element - Playwright tests stub only the window.datasetteAgent boundary; a new integration test runs against the real ToolContext when datasette-agent is installed (skipped otherwise) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ebf3ixoxYDoGkSTJgPcAc
1 parent eb4324c commit a589de0

8 files changed

Lines changed: 532 additions & 522 deletions

File tree

‎datasette_apps/__init__.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
app_revision,
1111
apps_index,
1212
create_app,
13-
debug_claim,
1413
debug_frame,
1514
debug_query,
16-
debug_result,
1715
delete_app,
1816
edit_app,
1917
launch_app,
@@ -33,10 +31,8 @@ def register_routes():
3331
(r"^/-/apps$", apps_index),
3432
(r"^/-/apps/create$", create_app),
3533
(r"^/-/apps/recent-queries\.json$", recent_stored_queries),
36-
(r"^/-/apps/debug/(?P<job_id>[^/]+)/claim$", debug_claim),
3734
(r"^/-/apps/debug/(?P<job_id>[^/]+)/frame$", debug_frame),
3835
(r"^/-/apps/debug/(?P<job_id>[^/]+)/query$", debug_query),
39-
(r"^/-/apps/debug/(?P<job_id>[^/]+)/result$", debug_result),
4036
(r"^/-/apps/(?P<id>[^/]+)\.json$", app_json),
4137
(r"^/-/apps/(?P<id>[^/]+)/revisions/(?P<version>\d+)$", app_revision),
4238
(r"^/-/apps/(?P<id>[^/]+)/edit$", edit_app),

‎datasette_apps/agent_tools.py‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
from .csp import configured_csp_allowlist, resolve_csp_origins
99
from .debug import (
10+
BROWSER_TASK_SLACK_MS,
1011
build_debug_harness_html,
1112
cap_envelope,
13+
complete_debug_job,
1214
create_debug_job,
13-
get_debug_job,
15+
debug_task_payload,
1416
get_debug_job_for_call,
17+
normalize_envelope,
1518
)
1619
from .permissions import AppResource, AppsResource
1720
from .registry import Registry
@@ -293,12 +296,13 @@ async def _app_debug(
293296
return _error("Permission denied: edit-app", app_id=app_id)
294297
conversation_id = getattr(context, "conversation_id", None)
295298
call_key = getattr(context, "call_key", None)
296-
# Re-executing a suspended call (after the browser answers) must find
297-
# the job it already created, not create an orphan row.
299+
# Re-executing a suspended call (after its browser task finishes)
300+
# must find the job it already created, not create an orphan row. A
301+
# completed job means this is a fresh identical call: run fresh.
298302
job = await get_debug_job_for_call(datasette, conversation_id, call_key)
299303
if (
300304
job is None
301-
or job["status"] == "expired"
305+
or job["status"] != "pending"
302306
or job["app_id"] != app_id
303307
or job["javascript"] != javascript
304308
):
@@ -315,31 +319,37 @@ async def _app_debug(
315319
)
316320
except ValueError as e:
317321
return _error(str(e), app_id=app_id)
322+
app = await Registry(datasette).get_app(app_id)
318323
try:
319-
await context.ask_user(
320-
"Running the debug script against the app in your browser - this "
321-
"completes automatically.",
322-
free_text=True,
323-
html=build_debug_harness_html(datasette, job),
324+
outcome = await context.browser_task(
325+
build_debug_harness_html(),
326+
payload=debug_task_payload(datasette, job),
327+
label="Running debug script against {}".format(
328+
(app and app["name"]) or app_id
329+
),
330+
timeout_ms=job["config"]["timeout_ms"] + BROWSER_TASK_SLACK_MS,
324331
)
325332
except Exception as e:
326333
# Matched by name so datasette-agent stays an optional dependency;
327-
# QuestionPending (an llm.PauseChain) must keep propagating.
328-
if e.__class__.__name__ == "QuestionsNotSupported":
334+
# BrowserTaskPending (an llm.PauseChain) must keep propagating.
335+
if e.__class__.__name__ == "BrowserTasksNotSupported":
329336
return _error(
330337
"app_debug requires an interactive conversation with the "
331338
"chat open in a browser; it is not available here",
332339
app_id=app_id,
333340
)
334341
raise
335-
job = await get_debug_job(datasette, job["id"])
336-
if job["status"] != "completed" or not job["result"]:
337-
return _error(
338-
"Debug run did not complete (status: {})".format(job["status"]),
339-
app_id=app_id,
340-
)
341-
payload = {"app_id": app_id, "version": job["version"]}
342-
payload.update(cap_envelope(job["result"]))
342+
if not isinstance(outcome, dict):
343+
outcome = {}
344+
envelope = normalize_envelope(outcome)
345+
# The audit record: what ran and what came back
346+
await complete_debug_job(datasette, job["id"], envelope)
347+
payload = {
348+
"app_id": app_id,
349+
"version": job["version"],
350+
"outcome": outcome.get("outcome") or "completed",
351+
}
352+
payload.update(cap_envelope(envelope))
343353
if payload.get("error") is None:
344354
payload.pop("error", None)
345355
return json.dumps(payload)

‎datasette_apps/db.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@
115115
status TEXT NOT NULL,
116116
result TEXT,
117117
created_at TEXT NOT NULL,
118-
claimed_at TEXT,
119118
completed_at TEXT,
120-
CHECK (status IN ('pending', 'claimed', 'completed', 'expired'))
119+
CHECK (status IN ('pending', 'completed', 'expired'))
121120
);
122121
123122
CREATE INDEX IF NOT EXISTS idx_app_debug_jobs_call

0 commit comments

Comments
 (0)