Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Agent Note: Editor Agent Panel Live Steps, Permission Cards, and Stop

Status: implemented

## Problem

When an AI agent drives the scene editor through `editormcp`, authors need to watch each step unfold in plain words, approve or deny gated actions (such as headless playtests or edits under a cautious profile) without losing manual editing or keyboard focus, add follow-up instructions to a running request, and stop the agent from anywhere (including inside a modal dialog) while preserving completed edits for review. Although `permissions.yaml` rule evaluation (`#1010`), You-only trust boundaries (`#1015`), and the Agent panel (`#1009`, `#1019`) already exist on `main`, `editormcp.Server` previously returned an immediate `PermissionError` on `Ask` when `Options.OnPermissionAsk` was nil rather than pausing on an interactive Agent panel permission card, lacked `agent_report_step`, per-request denial memory, mid-request `"Add to this request"` follow-ups, and unified `⌘.` Stop cancellation for waiting asks and in-flight tool calls.

## Decision

Integrate live step streaming, interactive permission cards, follow-ups, and Stop directly onto the existing `Editor` agent state (`packages/game/editor/agent.go`, `agent_live_steps.go`, `agent_ui.go`, `agent_live_steps_ui.go`), the existing versioned `permissions.yaml` engine (`packages/game/editor/permissions.go`), and `editormcp.Server` (`packages/game/editor/editormcp/agent.go`, `agent_live.go`, `server.go`):

- **Unified `permissions.yaml` and You-only enforcement**: Reuse `permissions.yaml` (`editor.PermissionsVersion = 1`) and `checkYouOnlyTargets` as the single source of truth—no duplicate permissions schema or file format. Every mutating/running MCP call checks You-only targets first and evaluates `permissions.yaml` before opening any permission card or running a handler.
- **Non-blocking interactive MCP mediation & streaming**: When `Options.OnPermissionAsk` is nil and either a live Agent request is active (`Editor.HasActiveLiveAgentRequest`) or the server runs against a UI loop (`Options.Run != nil`), `editormcp.Server.runManagedToolCall` pauses `Ask` calls on the MCP goroutine (never blocking the editor UI goroutine) and renders a Spec `RoleAlertDialog` (`LiveRegionPolite`) card inside `AgentPanelView`. Headless inline callers without an active request keep returning `*PermissionError` immediately for compatibility with `#1010` callers.
- **Live steps & `agent_report_step`**: Starting a request immediately inserts a gray `AgentStepPlaceholder` step (guaranteed within 300 ms); tool calls stream plain-words steps with at most one active violet spinner and green checks when done. `agent_report_step` lets agents report steps in their own words with touched object IDs and drains queued `"Add to this request"` follow-ups.
- **No automatic retry after denial & safe Allow now**: Denying a card records `(action, target)` on the active request, renders `"You denied … · continued without it"` with `"Allow now"`, records the unchecked item (such as `"Not playtested"`) in the turn's review summary, and returns a non-fatal structured `PermissionDeniedResult` immediately on any repeated identical call in the same request without re-opening a card. Starting a new request clears per-request denials. Clicking `"Allow now"` runs the tool on demand inside a WaitGroup-tracked, server-cancellation-aware, `projectGen`-verified, You-only-checked goroutine.
- **Always persistence with Undo**: Choosing `"Always"` (`⌥↵` when focused) calls `Editor.ApproveAlwaysPermission`, inserting the escaped concrete `Allow` rule ahead of the matching `Ask` rule in `permissions.yaml` and showing the existing `"Always permission rule saved"` toast with **Show** and **Undo**.
- **Focus stability, Spec rule 05, and global Stop (`⌘.`)**: Permission cards never steal focus unless `Editor.ShouldFocusAgentPrompt()` is true; clicking the header `"Waiting for you"` chip focuses the card on demand. While focused, `⌘↵` allows once, `⌥↵` approves Always, `⌘⌫` denies, `↑`/`↓` cycle buttons, and `Esc` leaves the card unanswered while the agent keeps waiting. The working footer (`"Add to this request"` + `Stop`) mounts outside `threadBody`'s dynamic scope so streaming steps never remount or steal focus from the input. `Stop` and global `⌘.` (`OverModals: true`) cancel both CLI runners and live MCP requests, close `liveStopCh`, keep completed steps and edits pending for review (pausing autosave via `HasOpenReview()` until kept or reverted), and show the `"Agent stopped"` toast with **Review**; `Esc` never stops the agent.

## Alternatives considered

### Introducing a separate `.nsgo/permissions.yaml` and parallel `AgentPanelView`

Strongest argument: Keeping `#1012` isolated from `#1009`/`#1010` would avoid touching `agent.go` and `permissions.go`.

Rejected because `main` already ships `permissions.yaml`, `Settings › Permissions`, `You-only` holds, and `RightRailView`/`AgentPanelView`. Duplicating them would create two conflicting permission files, break `Settings › Permissions`, and bypass You-only boundaries.

### Blocking on permission answers inside `Server.Editor` on the UI goroutine

Strongest argument: Evaluating and waiting in one `Server.Editor` closure would simplify state transitions.

Rejected because blocking the UI goroutine freezes the editor so the user could neither click Allow/Deny/Stop nor continue manual scene editing while the agent waits. Waiting on channels on the MCP goroutine keeps the editor responsive.

## Consequences

- **Benefits**: Authors watch every MCP tool call and `agent_report_step` live in plain words, answer Spec permission cards without focus theft, persist `"Always"` rules directly into the project's canonical `permissions.yaml`, steer running requests with `"Add to this request"` without input remounts, edit the scene concurrently, and stop safely with `⌘.` from anywhere including modal dialogs.
- **Costs**: Adds one new built-in read-only MCP tool (`agent_report_step`) to `editormcp.Server` and `editormcp.Proxy`, and couples `AgentTurn` review state into `HasOpenReview()` so unkept scene edits pause autosave until reviewed.
- **Verification**: Verified with `go tool nsgo check`, `gameschema -check`, `tools/changelog check`, `bun run verify-notes`, `-race` unit/UI tests in `packages/game/editor` (placeholder, live steps, input focus stability, Spec rule 05 card focus/shortcuts, `⌘.` inside modal dialogs, `Esc` non-stop/non-deny, autosave pause), scripted MCP client integration tests in `packages/game/editor/editormcp` (Allow once, Always + `permissions.yaml` + Undo, non-retried Deny + Allow now + new-request re-ask, follow-ups, Stop cancellation, You-only enforcement), and real macOS `nsgotest` screenshots in Dark and Light themes.
3 changes: 3 additions & 0 deletions changelog.d/editor-agent-live-steps-1012.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Add live agent step streaming, interactive Spec permission cards (`Allow once`, `Always`, `Deny`) backed by `permissions.yaml`, `agent_report_step` MCP tool, `"Add to this request"` follow-ups, concurrent status-bar activity, and global `⌘.` / Stop cancellation with pending review preservation and `"Agent stopped"` toast in the editor Agent panel.
11 changes: 10 additions & 1 deletion docs/en/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,6 @@ Every semantic overlay drawn over the 2D and 3D viewports passes through one sha
- **Top-left toggle legend (`viewport-overlay-legend`)**: Every edit-mode viewport (both 2D and 3D) mounts the same top-left legend card (`viewportOverlayLegendView`) with six accessible `role="switch"` rows (**Selection**, **Agent working**, **Added**, **Changed**, **Removed**, **Problem**), each showing its drawn line style and second cue. Toggling any row (`Editor.ToggleOverlayKind`, `Editor.SetOverlayKindVisible`, `Editor.SetOverlayLegendKinds`) hides or shows that kind across all viewports immediately and persists `overlayLegend` in the per-user `EditorSettings` YAML file (`Editor.SettingsPath`, outside the project, never modifying project or scene files).
- **Live scene overlay API and Problems object attribution**: Live agent edits, reviews, and plans attach overlays to objects (or removed ghost bounds) via `Editor.SetSceneOverlays`, `AddSceneOverlay`, `RemoveSceneOverlay`, and `ClearSceneOverlays` (`SceneOverlayEntry`). `assetcheck.Problem` now carries `Object` (also returned by `editormcp`'s `game_check`) for scene object, component, override, and connection diagnostics so Problems naming a live object in the open scene automatically draw the Spec **Problem** overlay on that object in both 2D and 3D viewports.
- **Neutral viewport guides**: The camera frame, camera bounds, camera tether, tile grid, and snap grid use neutral `SpecPalette` tokens (`TextMuted` / `TextFaint` 1 px hairline and `2, 4` dotted lines) that no semantic legend kind uses.

## Agent composer: @-mentions, context chips, and context meter (#1020)

Inside the unified right-rail **Agent** tab (`RightRailView` / `AgentPanelView`, switchable with `⌘1` / `⌘2` via `CmdAgentTab` / `CmdInspectorTab`), `AgentComposerView` (`docs/design/editor/Threads.dc.html`, styled strictly with `Spec.dc.html` tokens) adds `@`-mentions, explicit context chips, exact hover previews, and a live context meter:
Expand All @@ -1614,3 +1613,13 @@ The editor's main window (`main_layout_ui.go`, `ui.go`, `agent_ui.go`, `overlays
- **32 px status bar (`mainStatusBarView`)**: renders muted text on `bg` with the active layer button (toggling Layers), live 2D pointer coordinates, current tool readout, problem count pill (opening Problems), Output, and Debugger. Secondary views previously hosted on the old toolbar (Settings, Audio Mixer, Input Map, Localization/Strings, Export, Library, Data Table, Project Search, Files, Assets, Animation, Particles, Material, HUD Layout, Terrain, NavMesh) remain reachable through the Command Palette (`⌘K`), Menu bar, Settings, and Status bar.
- **Designed empty, loading, and error states (`WorkspaceState`, `SceneOpenError`)**: every region renders a dedicated state for `WorkspaceNoProject`, `WorkspaceNoScene`, `WorkspaceLoading` (`SetProjectLoading`), and `WorkspaceSceneError` (`SceneOpenError`, `RetryOpenScene`, `ClearSceneOpenError`). Opening a missing or malformed scene YAML renders an alert card with file path, line/column, message, fix guidance, **Retry**, and **Dismiss** instead of a blank viewport.
- **Spec controls and keyboard navigation**: preserves Spec heights (`28 px` rows/tool buttons, `30–32 px` buttons, `44 px` header, `32 px` status bar, no interactive hit area under `28 × 28`), spacing (`4 / 8 / 12 / 16 / 24`), radii (`6 / 8`), `base.RadioGroup` / `base.Radio` and `base.Switch` semantics, 2 px Spec focus rings, accessible names, and `F6` cycling across all five regions (`Header`, `Scene tree`, `Viewport`, `Agent / Inspector`, `Status bar`).

## Agent panel live steps, permission cards, and Stop (#1012)

The **Agent** panel (`AgentPanelView` inside `RightRailView`, `⌘1` / `⌘2`) streams live progress, interactive permission cards, follow-up instructions, and global Stop cancellation using `docs/design/editor/Spec.dc.html` tokens (`Working.dc.html`, `Permissions.dc.html`, `Main.dc.html`, `AgentStates.dc.html`) on top of the project's `permissions.yaml` rules and You-only boundaries:

- **Live steps and `agent_report_step`**: starting a request (`Editor.StartAgentRequest` or `Editor.SubmitAgentPrompt`) immediately renders a gray placeholder step (within 300 ms). As `editormcp.Server` executes tool calls, each step streams in plain words with a violet spinner on the single active step and a green check once done. Agents can also call `agent_report_step` (`text`, optional `objects`, optional `status`) to report steps in their own words, name touched scene object IDs, and receive queued `"Add to this request"` follow-ups.
- **Interactive permission cards over `permissions.yaml`**: when a tool call evaluates to `ask` against `permissions.yaml` (after You-only boundary enforcement), `editormcp.Server` pauses that call on its goroutine without blocking the editor UI and renders an `alertdialog` permission card (`LiveRegionPolite`) in the Agent panel showing the title, why the agent wants it, its effect (such as *"Nothing is saved."*), the rule preview and source, and **Allow once**, **Always**, and **Deny**.
- **Focus and keyboard rules (Spec rules 02, 04, 05)**: a permission card never steals focus unless focus is already inside the Agent panel (`Editor.ShouldFocusAgentPrompt`); clicking the header **Waiting for you** chip focuses it on demand. While focused, `⌘↵` allows once, `⌥↵` persists an **Always** rule to `permissions.yaml` via `Editor.ApproveAlwaysPermission` (with the **Always permission rule saved** toast offering **Show** and **Undo**) so future identical calls run without asking, `⌘⌫` denies, `↑`/`↓` cycle buttons, and `Esc` leaves the card unanswered while the agent keeps waiting.
- **No automatic retry after denial**: denying a permission card records its `(action, target)` key on the active request, renders *"You denied … · continued without it"* with an explicit **Allow now** action, notes the unchecked item in the turn's review summary (such as *"Not playtested"*), and immediately returns a structured non-fatal `denied` response for any repeated identical call in the same request without re-opening a card. Starting a new request clears per-request denials.
- **Add to this request, Stop (`⌘.`), and concurrent manual editing**: while the agent works or waits, manual edits stay available and the bottom status bar reports what the agent is doing (*"manual edits stay available"*). The working footer keeps the **Add to this request** input focus-stable while steps stream (`Editor.AddToAgentRequest`). Pressing **Stop** or `⌘.` from anywhere—modal dialogs included—cancels the running request and any waiting Ask (`Editor.StopAgent`), keeps completed steps and scene edits pending for review (pausing autosave until kept or reverted), and shows the **Agent stopped** toast with **Review**; `Esc` never stops the agent.
Loading
Loading