From df5918255f6ecde7a25bd20ef85e6943aa852d0b Mon Sep 17 00:00:00 2001 From: "Jon Radoff (Metavert)" Date: Mon, 9 Nov 2026 21:55:14 -0500 Subject: [PATCH 1/4] Merge only what was reviewed, and only with permission - Through an API key or OAuth connection, a merge needs "Can merge its own forks" on that credential, an owner whose role can merge, and a fork the same credential created. People merging in the admin UI are unaffected. The audit entry marks a self-merge. - ForkService.Review compares every field a merge writes, in full (a change past 2,000 bytes used to be invisible), lists what a new page sets, and returns a digest of the fork and live pages. Merge requires the digest and refuses with ErrForkChanged if anything changed. get_fork_diff returns it and merge_fork requires it. - Ending a sandbox with "submit" freezes its fork for keys and connections; a person can still edit it. - /cm/forks/{id} shows the review field by field and its merge form carries the digest of what was shown. - Merges write a version for every live page they create or change, and session rollback undoes a session's merge. A merged page's static file is rendered from the whole merged page. Co-Authored-By: Claude Opus 5.5 --- internal/apiclient/client.go | 12 +- internal/apiclient/client_coverage_test.go | 4 +- internal/apiclient/types.go | 1 + internal/auth/permissions.go | 15 +- internal/handlers/admin_csrf_ui_guard_test.go | 8 +- internal/handlers/admin_templates.go | 53 ++- internal/handlers/api_forks.go | 81 ++++- internal/handlers/api_identity_test.go | 75 +++++ internal/handlers/api_merge_test.go | 272 ++++++++++++++++ internal/handlers/api_sandbox.go | 24 +- internal/handlers/api_sandbox_test.go | 9 +- internal/handlers/draft_safety_test.go | 20 +- internal/handlers/handlers_fork.go | 72 +++- internal/mcp/dataformat_test.go | 2 +- internal/mcp/draft_safety_test.go | 20 +- internal/mcp/fork_tools.go | 14 +- internal/mcp/mcp_extra_test.go | 2 +- internal/mcp/sandbox_tools.go | 12 +- internal/models/models.go | 5 + internal/services/agentsession.go | 59 +++- internal/services/fork.go | 271 ++++++++++++---- internal/services/fork_merge_test.go | 27 +- internal/services/fork_review_test.go | 307 ++++++++++++++++++ internal/services/services_more_test.go | 2 +- 24 files changed, 1223 insertions(+), 144 deletions(-) create mode 100644 internal/handlers/api_merge_test.go create mode 100644 internal/services/fork_review_test.go diff --git a/internal/apiclient/client.go b/internal/apiclient/client.go index 6d2816c..3c2d8aa 100644 --- a/internal/apiclient/client.go +++ b/internal/apiclient/client.go @@ -982,12 +982,14 @@ func (c *Client) RemoveForkPage(ctx context.Context, forkID, pageID string) erro return c.do(ctx, "DELETE", "/forks/"+forkID+"/pages/"+pageID, nil, nil) } -// MergeFork merges all fork pages into live content. With publishNew, pages -// the merge creates are also published (held pages stay drafts). -func (c *Client) MergeFork(ctx context.Context, forkID string, publishNew bool) (*ForkMergeResult, error) { - var body interface{} +// MergeFork merges all fork pages into live content, exactly as reviewed: +// digest is the one GetForkDiff returned, and the merge is refused if the fork +// or a page it changes was edited since. With publishNew, pages the merge +// creates are also published (held pages stay drafts). +func (c *Client) MergeFork(ctx context.Context, forkID string, publishNew bool, digest string) (*ForkMergeResult, error) { + body := map[string]interface{}{"digest": digest} if publishNew { - body = map[string]bool{"publish_new": true} + body["publish_new"] = true } var result ForkMergeResult if err := c.do(ctx, "POST", "/forks/"+forkID+"/merge", body, &result); err != nil { diff --git a/internal/apiclient/client_coverage_test.go b/internal/apiclient/client_coverage_test.go index a9872ce..afb918e 100644 --- a/internal/apiclient/client_coverage_test.go +++ b/internal/apiclient/client_coverage_test.go @@ -85,8 +85,8 @@ func TestClientWrappers(t *testing.T) { "ForkPage": func(c *Client) error { _, err := c.ForkPage(ctx, "f", "c", "/p"); return err }, "ListForkPages": func(c *Client) error { _, err := c.ListForkPages(ctx, "f"); return err }, "RemoveForkPage": func(c *Client) error { return c.RemoveForkPage(ctx, "f", "p") }, - "MergeFork": func(c *Client) error { _, err := c.MergeFork(ctx, "f", false); return err }, - "MergeForkPublishNew": func(c *Client) error { _, err := c.MergeFork(ctx, "f", true); return err }, + "MergeFork": func(c *Client) error { _, err := c.MergeFork(ctx, "f", false, "d"); return err }, + "MergeForkPublishNew": func(c *Client) error { _, err := c.MergeFork(ctx, "f", true, "d"); return err }, "PurgeForkCopies": func(c *Client) error { _, err := c.PurgeForkCopies(ctx, "f", false); return err }, "PurgeForkCopiesDry": func(c *Client) error { _, err := c.PurgeForkCopies(ctx, "f", true); return err }, "SearchContentForks": func(c *Client) error { _, err := c.SearchContent(ctx, "q", "", false, true); return err }, diff --git a/internal/apiclient/types.go b/internal/apiclient/types.go index 35fe489..637f146 100644 --- a/internal/apiclient/types.go +++ b/internal/apiclient/types.go @@ -55,6 +55,7 @@ type ForkPageDiff struct { type ForkDiff struct { ForkID string `json:"fork_id"` Pages []ForkPageDiff `json:"pages"` + Digest string `json:"digest,omitempty"` // merge_fork needs it: it pins the merge to this review Status string `json:"status,omitempty"` // Set on merged forks: their copies are gone, so Pages is empty. MergedCreated int `json:"merged_created,omitempty"` diff --git a/internal/auth/permissions.go b/internal/auth/permissions.go index 3858c50..7ecdbda 100644 --- a/internal/auth/permissions.go +++ b/internal/auth/permissions.go @@ -174,13 +174,18 @@ var sandboxAllowedPerms = map[string]bool{ // UserHasPermission checks a permission against the user's role, then // narrows by the API key's scopes (when set) and sandbox-only restriction. func UserHasPermission(u *SessionUser, perm string) bool { - if u == nil { - return false - } - if !HasPermission(u.Role, perm) { + if u.Sandboxed() && !sandboxAllowedPerms[perm] { return false } - if u.Sandboxed() && !sandboxAllowedPerms[perm] { + return RoleAndScopesAllow(u, perm) +} + +// RoleAndScopesAllow checks a permission against the user's role and the +// API key's scopes, leaving the sandbox out. It is for the one step a +// separate grant opens to a sandboxed caller: merging a fork it created +// when CanMergeOwnForks is set. +func RoleAndScopesAllow(u *SessionUser, perm string) bool { + if u == nil || !HasPermission(u.Role, perm) { return false } if len(u.Scopes) > 0 { diff --git a/internal/handlers/admin_csrf_ui_guard_test.go b/internal/handlers/admin_csrf_ui_guard_test.go index a00f0e9..8b6b5a2 100644 --- a/internal/handlers/admin_csrf_ui_guard_test.go +++ b/internal/handlers/admin_csrf_ui_guard_test.go @@ -249,7 +249,8 @@ func TestAdminLayout_ConfirmDeleteAndRevertAreTextSafe(t *testing.T) { } // Delete in the admin is a soft delete with Restore: no dialog may call it -// irreversible. (Merging a fork really cannot be undone and still says so.) +// irreversible. Since 7.5 a merge keeps each page's previous version, so its +// confirmation says that instead of calling it irreversible. func TestAdminTemplates_DeleteWordingMatchesSoftDelete(t *testing.T) { undone := regexp.MustCompile(`(?i)cannot be undone|can't be undone|permanently delet`) for _, name := range sortedAdminTemplateNames() { @@ -266,7 +267,8 @@ func TestAdminTemplates_DeleteWordingMatchesSoftDelete(t *testing.T) { if !strings.Contains(form, "can be restored") { t.Error("the Delete Page confirmation no longer says the page can be restored") } - if !strings.Contains(adminTemplates["fork_detail"], "cannot be undone") { - t.Error("the fork merge confirmation lost its warning; a merge really cannot be undone") + merge := adminTemplates["fork_detail"] + if strings.Contains(merge, "cannot be undone") || !strings.Contains(merge, "previous version stays in its history") { + t.Error("the fork merge confirmation should say each page's previous version is kept, not that a merge cannot be undone") } } diff --git a/internal/handlers/admin_templates.go b/internal/handlers/admin_templates.go index eef30eb..fa3f929 100644 --- a/internal/handlers/admin_templates.go +++ b/internal/handlers/admin_templates.go @@ -7919,14 +7919,17 @@ async function doSearch(q) { {{if eq .Fork.Status "active"}} 👁 Start Preview {{if .CanMerge}} -
+ {{if .ReviewDigest}} + {{.CSRFField}} +
+ {{end}}
{{.CSRFField}} @@ -7973,6 +7976,54 @@ async function doSearch(q) { {{end}} + {{if .Fork.SubmittedAt}} +
Submitted for review by {{.Fork.SubmittedByEmail}} on {{.Fork.SubmittedAt.Format "Jan 2, 2006 15:04"}}. The agent that made it can't change it any more.
+ {{end}} + {{if .ReviewError}}
{{.ReviewError}}
{{end}} + {{if .Review}} + +

Everything a merge would change on the live site, field by field. Merging applies exactly this: if the fork or one of these live pages changes before you merge, the merge is refused and you review again.

+ + {{range .Review}} +
+

{{.Path}} + {{if eq .Status "added"}}new page{{else}}changed{{end}} + {{if .Conflict}}live page edited since it was forked{{end}} +

+ {{if .Fields}} +
+ + {{if ne .Status "added"}}{{end}} + + {{range .Fields}} + + + {{if not .IsNew}} + + {{end}} + + + {{end}} + +
FieldLive nowAfter merge
{{.Name}}{{if .LiveLong}}
{{.LivePreview}}
Show all
{{.Live}}
{{else}}
{{.Live}}
{{end}}
{{if .ForkLong}}
{{.ForkPreview}}
Show all
{{.Fork}}
{{else}}
{{.Fork}}
{{end}}
+
+ {{else}} +

Nothing differs from the live page.

+ {{end}} +
+ {{end}} + {{end}} +