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,29 @@
# Agent Note: Editor persistent turn checkpoints under `.nsgo/checkpoints/`

Status: implemented

## Problem

The editor's in-memory `History` command stack only tracks linear undo steps for the current session and only covers commands executed inside the open scene tab. When an agent turn edits multiple files across a project (scenes, prefabs, data tables, `game.yaml`, Go code, and binary media), or when an autonomous task or automation runs, restarting the editor drops the undo stack and leaves no built-in way to preview or restore every file a turn touched back to its pre-turn bytes.

## Decision

The editor stores a persistent checkpoint for every agent turn, autonomous task, and automation under `.nsgo/checkpoints/<id>/` inside the project root, ignored by Git (writing `.nsgo/checkpoints/.gitignore` and `.nsgo/.gitignore` when absent):

- **On-disk layout**: each checkpoint directory holds a strict versioned YAML manifest (`manifest.yaml`, `version: 1`, `editor.CheckpointVersion`, rejecting unknown fields, duplicate keys, trailing documents, invalid paths, and invalid SHA-256 digests) plus verbatim pre-turn file bytes under `files/<relative-path>`. Binary media is copied verbatim with no conversion, and non-existent files created by a turn are recorded with `existed: false` so restoring removes them.
- **Lazy first-write capture**: before a turn first writes any project file (either via `WriteAgentFile`/`DeleteAgentFile`/`RecordCheckpointFile` or via `History.beforeCommand` intercepting scene/project commands and `editormcp` `ActionEdit` calls), the editor captures that file's pre-turn bytes (verbatim disk bytes when clean, or canonical in-memory YAML if an open document already has unsaved edits). Subsequent writes to the same file in the same turn are no-ops, preserving the original pre-turn state. You-only files (`MatchYouOnlyPath` / `CheckYouOnlyTarget`), `.nsgo/`, `.git/`, symlinks, and files exceeding `32 MiB` are strictly refused.
- **Pre-start checkpoints for tasks and automations**: `TakeCheckpointBeforeTask`, `RunTaskWithCheckpoint`, `launchTaskRunner` in `tasks.go`, `TakeCheckpointBeforeAutomation`, `RunAutomationWithCheckpoint`, `editormcp`'s `game_playtest`, and automation-mode edit calls persist a checkpoint before work begins so partial or crashed runs remain restorable.
- **Size cap**: `Editor.CheckpointMaxBytes` (default `128 MiB`) prunes the oldest completed checkpoints while keeping at least the newest checkpoint and never deleting an active turn.
- **History panel and one-step undoable restore**: `HistoryPanelView` (`⌘Y`, `CmdHistoryPanel`) renders `UnifiedHistoryEntries` combining persisted checkpoints, current-session edits, saved points, and Git milestones by day with `All` / `You` / `Agent` filters, read-only viewport preview (`ActiveScene()` shows the checkpoint's scene snapshot while editing is off; `Esc` exits preview), and `RestoreCheckpoint` executed as a single `fallibleCommand` (`checkpointRestoreCmd`) that verifies byte length and SHA-256 digests, atomically restores all touched files on disk, synchronizes open scene tabs and `game.yaml` without triggering disk-conflict banners, and rolls back every file and open editor state on one `Undo()`.
- **MCP surface**: `editormcp` exposes `checkpoint_list` (`ActionRead`) and `checkpoint_restore` (`ActionEdit`, evaluating per-file `edit`/`code`/`delete` permission rules and You-only boundaries via `targets`) and automatically wraps file-mutating `ActionEdit` calls in panic- and cancel-safe turn checkpoints (preserving partial checkpoints on error/panic and discarding empty turns).

## Alternatives considered

- **Storing checkpoints in Git stash or shadow commits**: Git repositories may not be initialized in new projects, and creating Git objects/refs can interfere with hooks, worktrees, or user workflows. Plain files under `.nsgo/checkpoints/` work identically with or without Git and stay strictly ignored.
- **Content-addressed custom binary pack or `encoding/gob` archive**: Violates the repository's plain-text format rule (`AGENTS.md`), prevents human/AI inspection and diffing of manifests, and adds custom binary parsing risk. A versioned YAML manifest alongside verbatim files under `files/` keeps manifests human-readable and media byte-exact.
- **Full-project snapshots per turn**: Copying the entire project on every turn would quickly blow through the disk budget on media-heavy projects. Capturing each file lazily before its first write in a turn keeps checkpoints tiny and proportional to the turn's actual footprint.

## Consequences

- **Benefits**: Every agent turn, task, and automation is restorable byte-for-byte across editor restarts, across all file types (YAML, Go, and binary media), and `Restore` integrates cleanly into `Undo`/`Redo` as a single step. Checkpoints are inspectable YAML manifests with JSON Schema validation (`checkpoint.v1.json`) and SHA-256 integrity checks.
- **Costs**: Restoring a checkpoint rewrites touched files on disk immediately (to support non-scene files like prefabs and Go code) and synchronizes open tabs; if disk writes fail mid-restore, `checkpointRestoreCmd` rolls back already-written files. Large media files touched repeatedly in separate turns consume disk space until the size cap drops older checkpoints.
77 changes: 77 additions & 0 deletions apps/website/public/schemas/checkpoint.v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://game.nsgo.dev/schemas/checkpoint.v1.json",
"title": "NSGo game editor checkpoint manifest (version 1)",
"description": "CheckpointManifest is the strict versioned YAML manifest stored at .nsgo/checkpoints/<id>/manifest.yaml.",
"type": "object",
"properties": {
"version": {
"description": "Version is the schema version; it must equal CheckpointVersion (1).",
"type": "integer",
"const": 1
},
"id": {
"description": "ID is the unique checkpoint directory name (for example \"20261126T144100Z-0001\").",
"type": "string"
},
"createdAt": {
"description": "CreatedAt is the RFC3339 UTC timestamp when the checkpoint was started.",
"type": "string"
},
"who": {
"description": "Who identifies who initiated the checkpoint (\"agent\", \"you\", or \"git\").",
"type": "string"
},
"kind": {
"description": "Kind identifies why the checkpoint was taken (\"turn\", \"task\", or \"automation\").",
"type": "string"
},
"title": {
"description": "Title is the short human-readable summary shown in the History panel.",
"type": "string"
},
"detail": {
"description": "Detail is optional secondary context (such as change counts or trigger details).",
"type": "string"
},
"files": {
"description": "Files lists every project file captured before its first write in this checkpoint, sorted by path.",
"type": "array",
"items": {
"$ref": "#/definitions/CheckpointFile"
}
}
},
"additionalProperties": false,
"definitions": {
"CheckpointFile": {
"description": "CheckpointFile records one project file captured before its first write in a checkpoint.",
"type": "object",
"properties": {
"path": {
"description": "Path is the slash-separated path from the project root (for example \"scenes/level_01.scene.yaml\").",
"type": "string"
},
"existed": {
"description": "Existed reports whether the file existed before the turn wrote it; false means restoring deletes the file.",
"type": "boolean"
},
"size": {
"description": "Size is the saved file size in bytes when Existed is true.",
"type": "integer",
"minimum": -9223372036854776000,
"maximum": 9223372036854776000
},
"sha256": {
"description": "SHA256 is the lowercase hex SHA-256 digest of the saved bytes when Existed is true.",
"type": "string"
},
"mode": {
"description": "Mode is the optional 4-digit octal Unix file mode (for example \"0755\") when non-default.",
"type": "string"
}
},
"additionalProperties": false
}
}
}
3 changes: 3 additions & 0 deletions changelog.d/editor-turn-checkpoints-1018.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Editor: persistent per-turn checkpoints stored as strict versioned YAML manifests and verbatim pre-turn files under `.nsgo/checkpoints/` (ignored by git, enforcing You-only boundaries, symlink refusal, and automatic size-cap pruning), pre-start checkpoints for autonomous tasks and automations, unified History panel (`⌘Y`) integration with cross-session checkpoint preview and one-step undoable multi-file restore, plus `checkpoint_list` and permission-checked `checkpoint_restore` MCP tools.
10 changes: 10 additions & 0 deletions docs/en/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,16 @@ The History panel (`HistoryPanelView`), styled with `docs/design/editor/Spec.dc.
- **Non-destructive Restore (`RestoreHistoryEntry`)**: clicking **Restore** rolls the active scene, project, and every extra touched file back to that point as **one new undoable step** in `History`. Later steps remain in the timeline and can still be previewed or restored, and a single `Undo` returns immediately to the latest state.
- **Agent read access (`editormcp`)**: the read-only `history_list` tool (`ActionRead`) exposes the same newest-first timeline to connected agents with optional `author` (`all`, `you`, `agent`) and `limit` filters plus unsaved and active preview metadata.

## Persistent per-turn checkpoints (#1018)

Agent turns, autonomous tasks, and automations persist across editor restarts inside the project's `.nsgo/checkpoints/` directory and appear alongside session edits in the unified History panel (`⌘Y`, `CmdHistoryPanel`):

- **Lazy first-write capture (`.nsgo/checkpoints/`)**: before an agent turn (`BeginCheckpointTurn` / `RunCheckpointTurn`, or any file-mutating `editormcp` `ActionEdit` tool call) first modifies a project file—scenes, prefabs, data tables, `game.yaml`, Go code, or verbatim binary media—the editor snapshots its pre-turn bytes under `.nsgo/checkpoints/<id>/files/<path>` and writes a strict versioned YAML manifest (`manifest.yaml`, `editor.CheckpointVersion = 1`, JSON Schema `https://game.nsgo.dev/schemas/checkpoint.v1.json`). Files created by the turn record `existed: false` so restoring deletes them. `.nsgo/checkpoints/.gitignore` and `.nsgo/.gitignore` are written automatically so checkpoints stay ignored by Git, and `Editor.CheckpointMaxBytes` (default `128 MiB`) prunes the oldest completed checkpoints when total size exceeds the cap.
- **Tasks and automations**: autonomous background tasks (`TakeCheckpointBeforeTask`, `RunTaskWithCheckpoint`, and `SubmitTask` runners) and automations (`TakeCheckpointBeforeAutomation`, `RunAutomationWithCheckpoint`, `game_playtest`, and automation-mode MCP edits) always persist a checkpoint before they start.
- **Security and integrity**: checkpoint capture, writes, and restores strictly exclude `.git/`, `.nsgo/`, and all You-only configuration files (`MatchYouOnlyPath` / `CheckYouOnlyTarget`), refuse symlinks along every path component, cap manifest and file sizes (`2 MiB` / `32 MiB`), and verify both byte length and SHA-256 digests before writing.
- **Unified History preview and one-step undoable restore**: `OpenProject` reloads persisted checkpoints into `HistoryPanelView` (`UnifiedHistoryEntries`). Clicking a checkpoint previews its scene snapshot read-only (`PreviewCheckpoint`), and **Restore** (`RestoreCheckpoint`) atomically restores every touched file on disk, synchronizes open tabs and `game.yaml`, and records one undoable step (`checkpointRestoreCmd`) so `Undo` (`⌘Z`) puts the entire turn back byte-for-byte.
- **Matching MCP tools (`editormcp`)**: `checkpoint_list` (`ActionRead`) lists unified history and persisted checkpoints; `checkpoint_restore` (`ActionEdit`) evaluates permission rules (`edit`, `code`, `delete`) and You-only boundaries against every file recorded in the checkpoint manifest before restoring as one undoable step.

## You-only trust boundary and proposed deletions (#1015)

Settings that govern what an AI agent is allowed to touch — **Permissions** (`permissions.yaml`), **Tools** (`tools.yaml` / `mcp.yaml`), **Models** (`models.yaml`), and **Usage** (`usage.yaml`) — follow the **You-only rule** (`docs/design/editor/Permissions.dc.html`, `Settings.dc.html`):
Expand Down
30 changes: 30 additions & 0 deletions docs/en/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,36 @@ requests:
status: done
```

<!-- format: editor#checkpoint -->
### Editor turn checkpoints

| Files | Version | Read and written by | Fields |
| --- | --- | --- | --- |
| `.nsgo/checkpoints/<id>/manifest.yaml` (`editor.CheckpointsDir`, `editor.CheckpointManifestFile`), with saved pre-turn file contents stored verbatim under `.nsgo/checkpoints/<id>/files/<path>` and ignored by Git | `version: 1` | `editor.ParseCheckpointManifest`, `editor.EncodeCheckpointManifest`, `Editor.BeginCheckpointTurn`, `Editor.RestoreCheckpoint` | [Editor & Scene Format](editor.md#persistent-per-turn-checkpoints-1018) |

Each agent turn, autonomous task, or automation records the pre-turn bytes of every project file before its first write (scenes, prefabs, data tables, `game.yaml`, Go code, and binary media alike). `files` is sorted by slash-separated `path` relative to the project root. When `existed` is `true`, `size` and `sha256` describe the raw bytes stored under `files/<path>`, with optional 4-digit octal `mode` when non-default; when `existed` is `false`, restoring deletes the file that the turn created. A size cap (`Editor.CheckpointMaxBytes`, defaulting to 128 MiB) drops the oldest completed checkpoints.

```yaml
version: 1
id: 20261126T144100Z-0001
createdAt: "2026-11-26T14:41:00Z"
who: agent
kind: turn
title: Coin spin, bob and sparkle
detail: 2 files
files:
- path: fx/sparkle.particles.yaml
existed: false
- path: prefabs/coin.prefab.yaml
existed: true
size: 142
sha256: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
- path: scenes/level_01.scene.yaml
existed: true
size: 512
sha256: fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
```

<!-- format: tooling/assetbuild -->
### Asset build cache

Expand Down
10 changes: 10 additions & 0 deletions docs/zh/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,16 @@ func main() {
- **非破坏性恢复(`RestoreHistoryEntry`)**:点击 **Restore** 会将当前场景、项目以及期间改动的全部额外文件作为**一个新的可撤销步骤**回滚到该时间点;后续历史步骤全部保留且仍可预览或恢复,执行一次 `Undo` 即可立即回到恢复前的最新状态。
- **智能体读取接口(`editormcp`)**:只读工具 `history_list`(`ActionRead`)向已连接的智能体暴露同一份按最新优先排列的历史列表,支持 `author`(`all`、`you`、`agent`)与 `limit` 筛选以及未保存和当前预览状态。

## 持久化回合检查点(#1018)

智能体回合、自主后台任务与自动化流程的检查点会持久化在项目的 `.nsgo/checkpoints/` 目录中,重启编辑器后仍可保留,并与当前会话编辑一同显示在统一历史面板(`⌘Y`,`CmdHistoryPanel`)中:

- **首次写入前快照(`.nsgo/checkpoints/`)**:在智能体回合(`BeginCheckpointTurn` / `RunCheckpointTurn`,或任意会修改文件的 `editormcp` `ActionEdit` 工具调用)首次修改项目文件——包括场景、预制体、数据表、`game.yaml`、Go 代码及按原字节保存的二进制媒体——之前,编辑器会将其修改前的字节原样保存到 `.nsgo/checkpoints/<id>/files/<path>`,并写入严格版本化的 YAML 清单(`manifest.yaml`,`editor.CheckpointVersion = 1`,JSON Schema `https://game.nsgo.dev/schemas/checkpoint.v1.json`)。回合新建的文件记为 `existed: false`,恢复时会将其删除。编辑器会自动写入 `.nsgo/checkpoints/.gitignore` 与 `.nsgo/.gitignore` 以保持被 Git 忽略,并在总占用超过 `Editor.CheckpointMaxBytes`(默认 `128 MiB`)时自动清理最旧的已完成检查点。
- **后台任务与自动化**:自主后台任务(`TakeCheckpointBeforeTask`、`RunTaskWithCheckpoint` 及 `SubmitTask` 启动的执行器)与自动化流程(`TakeCheckpointBeforeAutomation`、`RunAutomationWithCheckpoint`、`game_playtest` 及自动化模式下的 MCP 编辑)在开始前总会先持久化创建一个检查点。
- **安全与完整性保护**:检查点的记录、写入与恢复严格排除 `.git/`、`.nsgo/` 以及全部仅限用户配置文件(`MatchYouOnlyPath` / `CheckYouOnlyTarget`),拒绝路径上任何层级的符号链接,限制清单与单文件大小上限(`2 MiB` / `32 MiB`),并在恢复写入前同时校验文件字节数与 SHA-256 摘要。
- **统一历史预览与单步可撤销恢复**:`OpenProject` 会将已持久化的检查点重新载入 `HistoryPanelView`(`UnifiedHistoryEntries`)。点击任意检查点可只读预览该时刻的场景快照(`PreviewCheckpoint`),点击 **恢复(Restore)**(`RestoreCheckpoint`)会原子恢复所有受影响文件、同步打开的标签页与 `game.yaml`,并将其作为单个可撤销步骤(`checkpointRestoreCmd`)记入 `History`,按一次 `Undo`(`⌘Z`)即可逐字节还原整个回合。
- **配套 MCP 工具(`editormcp`)**:`checkpoint_list`(`ActionRead`)列出统一历史与持久化检查点;`checkpoint_restore`(`ActionEdit`)会先针对检查点清单中记录的每一个文件评估权限规则(`edit`、`code`、`delete`)及仅限用户边界,通过后再作为单个可撤销步骤执行恢复。

## 仅限用户信任边界与提议删除(#1015)

控制 AI 智能体行为边界的四项设置——**权限**(`permissions.yaml`)、**工具**(`tools.yaml` / `mcp.yaml`)、**模型**(`models.yaml`)与**用量**(`usage.yaml`)——严格遵循**仅限用户(You only)规则**(对应 `docs/design/editor/Permissions.dc.html`、`Settings.dc.html`):
Expand Down
30 changes: 30 additions & 0 deletions docs/zh/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,36 @@ requests:
status: done
```

<!-- format: editor#checkpoint -->
### 编辑器回合检查点

| 文件 | 版本 | 读写函数 | 字段 |
| --- | --- | --- | --- |
| `.nsgo/checkpoints/<id>/manifest.yaml`(`editor.CheckpointsDir`、`editor.CheckpointManifestFile`),写入前的原始文件内容按原样保存在 `.nsgo/checkpoints/<id>/files/<path>` 下并由 Git 忽略 | `version: 1` | `editor.ParseCheckpointManifest`、`editor.EncodeCheckpointManifest`、`Editor.BeginCheckpointTurn`、`Editor.RestoreCheckpoint` | [编辑器与场景格式](editor.md#持久化回合检查点1018) |

每个智能体回合、自主任务或自动化在首次写入任何项目文件之前都会记录其原始字节(场景、预制体、数据表、`game.yaml`、Go 代码与二进制媒体一视同仁)。`files` 按相对项目根目录的斜杠路径 `path` 排序。当 `existed` 为 `true` 时,`size` 与 `sha256` 描述保存在 `files/<path>` 下的原始字节,非默认权限通过 4 位八进制 `mode` 记录;当 `existed` 为 `false` 时,恢复操作会删除该回合新建的文件。容量上限(`Editor.CheckpointMaxBytes`,默认 128 MiB)会自动清理最旧的已完成检查点。

```yaml
version: 1
id: 20261126T144100Z-0001
createdAt: "2026-11-26T14:41:00Z"
who: agent
kind: turn
title: Coin spin, bob and sparkle
detail: 2 files
files:
- path: fx/sparkle.particles.yaml
existed: false
- path: prefabs/coin.prefab.yaml
existed: true
size: 142
sha256: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
- path: scenes/level_01.scene.yaml
existed: true
size: 512
sha256: fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
```

<!-- format: tooling/assetbuild -->
### 资源构建缓存

Expand Down
Loading
Loading