Skip to content

Commit aaac455

Browse files
roberttwangclaude
andcommitted
fix(assets): ignore other projects' global rules when exporting a LOOP
Another project may bind its own global rule to a shared LOOP. Export walked every rule on the platform, so the source project got a 400 for a rule it can neither see nor remove. Only this project's global rules now travel with the LOOP; runtime injection already ignores the others. Also from review: - Preview runs the full save in a transaction and rolls it back, so role hooks and artifact paths that only PutAgent checks fail the preview too. - A dependency that is another project's shared asset or a platform template says it must be copied into the project first. A private asset of another project still reads like a missing one. - Import returns bindings that point at the new copies, and the dialog lists each asset as name -> new ID with its follow-up items. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent e2787a7 commit aaac455

7 files changed

Lines changed: 278 additions & 24 deletions

File tree

‎docs/api/openapi.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ paths:
19441944
post:
19451945
tags: [Assets]
19461946
operationId: exportProjectAssetBundle
1947-
description: Export the selected assets and their project-owned dependencies. Requires a project owner, maintainer, or platform administrator.
1947+
description: Export the selected assets and their project-owned dependencies. Global rules that other projects bind to a shared LOOP are not included. Requires a project owner, maintainer, or platform administrator.
19481948
requestBody:
19491949
required: true
19501950
content:
@@ -1989,7 +1989,7 @@ paths:
19891989
post:
19901990
tags: [Assets]
19911991
operationId: previewProjectAssetBundle
1992-
description: Validate a bundle and preview assets and connection setup without writing to the project. Requires a project owner, maintainer, or platform administrator.
1992+
description: Validate a bundle and preview assets and connection setup. The import runs in a transaction that is rolled back, so nothing is written to the project. Requires a project owner, maintainer, or platform administrator.
19931993
requestBody:
19941994
required: true
19951995
content:
@@ -2022,7 +2022,7 @@ paths:
20222022
post:
20232023
tags: [Assets]
20242024
operationId: importProjectAssetBundle
2025-
description: Create the project-owned copies in one transaction and rebind their references. LOOPs start as drafts. Requires a project owner, maintainer, or platform administrator.
2025+
description: Create the project-owned copies in one transaction and rebind their references. LOOPs start as drafts. Each asset reports its new targetId and each binding assetId names the imported copy. Requires a project owner, maintainer, or platform administrator.
20262026
requestBody:
20272027
required: true
20282028
content:

‎docs/api/registry.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ LOOP 库右上角的「资产包」可单选或批量选择本项目的 Skill、
181181
{"assets":[{"kind":"sop","id":"review-sop"},{"kind":"skill","id":"check-rules"}]}
182182
```
183183

184-
返回 `loopforge.project-assets/v1` JSON 文件,包含当前定义、附件和同项目依赖,包括角色引用的规则、LOOP 引用的子 LOOP,以及自定义沙箱及其 Dockerfile 构建上下文。平台内置沙箱使用目标环境中的对应配置。其他项目资产和平台通用模板需要先复制到当前项目。
184+
返回 `loopforge.project-assets/v1` JSON 文件,包含当前定义、附件和同项目依赖,包括角色引用的规则、LOOP 引用的子 LOOP,以及自定义沙箱及其 Dockerfile 构建上下文。平台内置沙箱使用目标环境中的对应配置。其他项目资产和平台通用模板需要先复制到当前项目,否则返回 `400` 并指出是哪一项。LOOP 只带上本项目绑定到它的全局规则,其他项目绑定到共享 LOOP 的全局规则不会进入资产包,也不影响导出。
185185

186186
### `POST /api/projects/{id}/asset-bundles/preview`
187187

188-
请求体为整个资产包。成功时返回 `assets` 清单和 `bindings` 重配项(包含 `kind` 和 `assetId`),此请求不写入项目。附件校验 SHA‑256 和路径。包最多包含 200 项资产及依赖、64 MiB 附件和 96 MiB JSON。
188+
请求体为整个资产包。成功时返回 `assets` 清单和 `bindings` 重配项(包含 `kind` 和 `assetId`)。预览会在事务中完整执行一次导入再回滚,导入时会失败的定义(如角色 Hook 语言、产物路径)在预览时同样返回 `400`,此请求不写入项目。附件校验 SHA‑256 和路径。包最多包含 200 项资产及依赖、64 MiB 附件和 96 MiB JSON。
189189

190190
### `POST /api/projects/{id}/asset-bundles/import`
191191

192-
请求体与预览相同。成功返回 `201`,`assets` 中包含各项的 `targetId`。资产包整体导入为项目私有副本并自动重绑包内引用。LOOP 保存为草稿,有子 LOOP 时按依赖顺序重配连接和发布。每次导入创建一套新副本。
192+
请求体与预览相同。成功返回 `201`,`assets` 中包含各项的 `targetId`,`bindings` 的 `assetId` 改为导入后副本的新 ID。资产包整体导入为项目私有副本并自动重绑包内引用。LOOP 保存为草稿,有子 LOOP 时按依赖顺序重配连接和发布。每次导入创建一套新副本。
193193

194194
项目 MCP 的 Headers、环境变量值、URL 中的认证信息和查询参数,以及项目凭据、知识库和通知绑定需要在目标项目重配。Skill 与 MCP 附件按原始字节导出。只在受信任项目间交付经过审查的资产包,正文和可执行文件中的自带内容会随包迁移。
195195

‎internal/httpapi/asset_bundle.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ func (h *assetBundleHandlers) importBundle(c *gin.Context, save bool) {
112112
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
113113
return
114114
}
115+
bindings := bundle.Bindings
116+
if save {
117+
bindings = repo.ImportedBundleBindings(assets, bindings)
118+
}
115119
c.Header("Cache-Control", "private, no-store")
116-
c.JSON(status, gin.H{"assets": assets, "bindings": bundle.Bindings})
120+
c.JSON(status, gin.H{"assets": assets, "bindings": bindings})
117121
}
118122

119123
func readAssetBundleJSON(c *gin.Context, value any, limit int64) bool {

‎internal/httpapi/asset_bundle_test.go‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,143 @@ func TestProjectAssetBundlePreservesAutomaticExecutionSettings(t *testing.T) {
249249
}
250250
}
251251
}
252+
253+
func TestProjectAssetBundleExportIgnoresOtherProjectsGlobalRules(t *testing.T) {
254+
f := cvBNewHTTPFixture(t)
255+
save := func(err error) {
256+
t.Helper()
257+
if err != nil {
258+
t.Fatal(err)
259+
}
260+
}
261+
save(f.s.PutProject(domain.Project{ID: "bundle-shared-src", Name: "Source", OwnerID: f.user.ID}))
262+
save(f.s.PutProject(domain.Project{ID: "bundle-other", Name: "Other", OwnerID: f.user.ID + 99}))
263+
save(f.s.PutAgent(domain.AgentDefinition{ID: "shared-src-role", Name: "Reviewer", ProjectID: "bundle-shared-src"}))
264+
save(f.s.PutWorkflow(domain.WorkflowDefinition{
265+
ID: "shared-src-loop", Name: "Shared LOOP", ProjectID: "bundle-shared-src", Shared: true, AutoSelectSandbox: true,
266+
Nodes: []domain.WorkflowNode{{ID: "review", Name: "Review", Type: "agent", AgentID: "shared-src-role"}},
267+
}))
268+
save(f.s.PutRule(domain.RuleDefinition{
269+
ID: "shared-src-own-rule", Name: "Own", ProjectID: "bundle-shared-src", Global: true,
270+
WorkflowIDs: []string{"shared-src-loop"}, Content: "Own rule.",
271+
}))
272+
// Another project binds its own global rule to the shared LOOP. The source
273+
// project cannot see or remove it, so it must not block or join the export.
274+
save(f.s.PutRule(domain.RuleDefinition{
275+
ID: "other-global-rule", Name: "Other", ProjectID: "bundle-other", Global: true,
276+
WorkflowIDs: []string{"shared-src-loop"}, Content: "Other project rule.",
277+
}))
278+
res := cvBRequest(t, f, "POST", "/api/projects/bundle-shared-src/asset-bundles/export",
279+
map[string]any{"assets": []domain.BundleAssetRef{{Kind: "sop", ID: "shared-src-loop"}}}, true)
280+
if res.Code != http.StatusOK {
281+
t.Fatalf("export: %d %s", res.Code, res.Body.String())
282+
}
283+
var bundle domain.ProjectAssetBundle
284+
if err := json.Unmarshal(res.Body.Bytes(), &bundle); err != nil {
285+
t.Fatal(err)
286+
}
287+
if len(bundle.Rules) != 1 || bundle.Rules[0].ID != "shared-src-own-rule" || strings.Contains(res.Body.String(), "other-global-rule") {
288+
t.Fatalf("export must carry only this project's global rules: %+v", bundle.Rules)
289+
}
290+
}
291+
292+
func TestProjectAssetBundleExplainsForeignDependencies(t *testing.T) {
293+
f := cvBNewHTTPFixture(t)
294+
save := func(err error) {
295+
t.Helper()
296+
if err != nil {
297+
t.Fatal(err)
298+
}
299+
}
300+
save(f.s.PutProject(domain.Project{ID: "bundle-borrower", Name: "Borrower", OwnerID: f.user.ID}))
301+
save(f.s.PutAgent(domain.AgentDefinition{ID: "lender-role", Name: "Shared role", ProjectID: "bundle-lender", Shared: true}))
302+
save(f.s.PutWorkflow(domain.WorkflowDefinition{
303+
ID: "borrower-loop", Name: "Borrower LOOP", ProjectID: "bundle-borrower", AutoSelectSandbox: true,
304+
Nodes: []domain.WorkflowNode{{ID: "review", Name: "Review", Type: "agent", AgentID: "lender-role"}},
305+
}))
306+
path := "/api/projects/bundle-borrower/asset-bundles/export"
307+
res := cvBRequest(t, f, "POST", path, map[string]any{"assets": []domain.BundleAssetRef{{Kind: "sop", ID: "borrower-loop"}}}, true)
308+
if res.Code != http.StatusBadRequest || !strings.Contains(res.Body.String(), "agent/lender-role") ||
309+
!strings.Contains(res.Body.String(), "请先复制到当前项目") {
310+
t.Fatalf("a shared role from another project needs a copy hint: %d %s", res.Code, res.Body.String())
311+
}
312+
// A private asset of another project must read exactly like a missing one.
313+
save(f.s.PutSkill(domain.SkillDefinition{ID: "lender-private", Name: "Private", ProjectID: "bundle-lender"}))
314+
errorFor := func(id string) string {
315+
t.Helper()
316+
res := cvBRequest(t, f, "POST", path, map[string]any{"assets": []domain.BundleAssetRef{{Kind: "skill", ID: id}}}, true)
317+
if res.Code != http.StatusBadRequest {
318+
t.Fatalf("export skill %s: %d %s", id, res.Code, res.Body.String())
319+
}
320+
return strings.ReplaceAll(res.Body.String(), id, "<id>")
321+
}
322+
if hidden, missing := errorFor("lender-private"), errorFor("missing-skill"); hidden != missing || strings.Contains(hidden, "复制") {
323+
t.Fatalf("a private foreign asset is distinguishable from a missing one: %s vs %s", hidden, missing)
324+
}
325+
}
326+
327+
func TestProjectAssetBundlePreviewRunsTheSave(t *testing.T) {
328+
f := cvBNewHTTPFixture(t)
329+
if err := f.s.PutProject(domain.Project{ID: "bundle-dry-run", Name: "Dry run", OwnerID: f.user.ID}); err != nil {
330+
t.Fatal(err)
331+
}
332+
tgt := "/api/projects/bundle-dry-run/asset-bundles/"
333+
bundle := func(role domain.AgentDefinition) domain.ProjectAssetBundle {
334+
return domain.ProjectAssetBundle{
335+
Format: domain.ProjectAssetBundleFormat,
336+
Agents: []domain.AgentDefinition{role},
337+
Workflows: []domain.WorkflowDefinition{{
338+
ID: "dry-loop", Name: "Dry LOOP", AutoSelectSandbox: true,
339+
Nodes: []domain.WorkflowNode{{ID: "review", Name: "Review", Type: "agent", AgentID: role.ID}},
340+
}},
341+
Bindings: []domain.BundleBinding{{Kind: "loop-integrations", AssetID: "dry-loop"}},
342+
}
343+
}
344+
for name, role := range map[string]domain.AgentDefinition{
345+
"hook language": {ID: "dry-role", Name: "Role", Hooks: []domain.AgentHook{{ID: "h1", Name: "bad", Event: "Stop", Language: "ruby", Script: "puts 1"}}},
346+
"artifact path": {ID: "dry-role", Name: "Role", ArtifactOutputs: []domain.ArtifactContract{{Name: "x", Path: "../escape"}}},
347+
} {
348+
res := cvBRequest(t, f, "POST", tgt+"preview", bundle(role), true)
349+
if res.Code != http.StatusBadRequest {
350+
t.Fatalf("preview accepted an invalid %s: %d %s", name, res.Code, res.Body.String())
351+
}
352+
}
353+
valid := bundle(domain.AgentDefinition{ID: "dry-role", Name: "Role"})
354+
res := cvBRequest(t, f, "POST", tgt+"preview", valid, true)
355+
if res.Code != http.StatusOK || strings.Contains(res.Body.String(), "targetId") {
356+
t.Fatalf("preview: %d %s", res.Code, res.Body.String())
357+
}
358+
for _, workflow := range f.s.Workflows() {
359+
if workflow.ProjectID == "bundle-dry-run" {
360+
t.Fatal("preview persisted a LOOP")
361+
}
362+
}
363+
for _, role := range f.s.Agents() {
364+
if role.ProjectID == "bundle-dry-run" {
365+
t.Fatal("preview persisted a role")
366+
}
367+
}
368+
res = cvBRequest(t, f, "POST", tgt+"import", valid, true)
369+
if res.Code != http.StatusCreated {
370+
t.Fatalf("import: %d %s", res.Code, res.Body.String())
371+
}
372+
var result struct {
373+
Assets []domain.BundleAssetSummary
374+
Bindings []domain.BundleBinding
375+
}
376+
if err := json.Unmarshal(res.Body.Bytes(), &result); err != nil {
377+
t.Fatal(err)
378+
}
379+
loopID := ""
380+
for _, asset := range result.Assets {
381+
if asset.Kind == "sop" {
382+
loopID = asset.TargetID
383+
}
384+
}
385+
if loopID == "" || len(result.Bindings) != 1 || result.Bindings[0].AssetID != loopID {
386+
t.Fatalf("import bindings must point at the imported copy: %+v", result)
387+
}
388+
if workflow, ok := f.s.Workflow(loopID); !ok || workflow.ProjectID != "bundle-dry-run" {
389+
t.Fatal("import did not save the LOOP")
390+
}
391+
}

‎internal/repo/asset_bundle_export.go‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,28 @@ func (x *bundleExport) add(kind, id string) error {
7575
}
7676
x.seen[ref] = true
7777
missing := fmt.Errorf("资产或依赖 %s/%s 不属于当前项目或已不存在", kind, id)
78+
// owned rejects assets of other projects. Only assets this project can
79+
// already see (shared or platform-wide) get the copy hint; a private asset
80+
// of another project reads exactly like a missing one.
81+
owned := func(projectID string, visible bool) error {
82+
if projectID == x.project {
83+
return nil
84+
}
85+
if visible {
86+
return fmt.Errorf("资产或依赖 %s/%s 来自其他项目的共享资产或平台通用模板,请先复制到当前项目再导出", kind, id)
87+
}
88+
return missing
89+
}
7890
r := x.catalog
7991
switch kind {
8092
case "skill":
8193
v, ok := r.Skill(id)
82-
if !ok || v.ProjectID != x.project {
94+
if !ok {
8395
return missing
8496
}
97+
if err := owned(v.ProjectID, v.Shared || v.ProjectID == ""); err != nil {
98+
return err
99+
}
85100
files, err := collectBundleFiles(x, v.FileRefs, r.SkillFileContents(id), func(ref domain.SkillFileRef) (string, string, uint32) {
86101
return ref.Path, ref.SHA256, ref.Mode
87102
})
@@ -92,9 +107,12 @@ func (x *bundleExport) add(kind, id string) error {
92107
x.bundle.Skills = append(x.bundle.Skills, domain.BundleSkill{SkillDefinition: v, Attachments: files})
93108
case "mcp":
94109
v, ok := r.MCPServer(id)
95-
if !ok || v.ProjectID != x.project || v.Scope == "personal" {
110+
if !ok || v.Scope == "personal" {
96111
return missing
97112
}
113+
if err := owned(v.ProjectID, v.ProjectID == ""); err != nil {
114+
return err
115+
}
98116
files, err := collectBundleFiles(x, v.FileRefs, r.MCPFileContents(id), func(ref domain.MCPFileRef) (string, string, uint32) {
99117
return ref.Path, ref.SHA256, ref.Mode
100118
})
@@ -108,9 +126,12 @@ func (x *bundleExport) add(kind, id string) error {
108126
x.bundle.MCPServers = append(x.bundle.MCPServers, domain.BundleMCPServer{MCPServerDefinition: v, Attachments: files})
109127
case "agent":
110128
v, ok := r.Agent(id)
111-
if !ok || v.ProjectID != x.project {
129+
if !ok {
112130
return missing
113131
}
132+
if err := owned(v.ProjectID, v.Shared || v.ProjectID == ""); err != nil {
133+
return err
134+
}
114135
for _, dependencies := range []struct {
115136
kind string
116137
ids []string
@@ -130,9 +151,15 @@ func (x *bundleExport) add(kind, id string) error {
130151
x.bundle.Agents = append(x.bundle.Agents, v)
131152
case "sop":
132153
v, ok := r.Workflow(id)
133-
if !ok || v.ProjectID != x.project || v.Generic {
154+
if !ok {
134155
return missing
135156
}
157+
if v.Generic {
158+
return owned("", true)
159+
}
160+
if err := owned(v.ProjectID, v.Shared || v.ProjectID == ""); err != nil {
161+
return err
162+
}
136163
if !v.AutoSelectSandbox && !builtinBundleSandbox(v.SandboxProfileID) {
137164
if err := x.add("sandbox", v.SandboxProfileID); err != nil {
138165
return err
@@ -150,8 +177,11 @@ func (x *bundleExport) add(kind, id string) error {
150177
}
151178
}
152179
}
180+
// Other projects may bind their own global rules to a shared LOOP.
181+
// Those rules belong to the other project, so only this project's
182+
// global rules travel with the LOOP.
153183
for _, rule := range r.Rules() {
154-
if rule.Global && slices.Contains(rule.WorkflowIDs, id) {
184+
if rule.Global && rule.ProjectID == x.project && slices.Contains(rule.WorkflowIDs, id) {
155185
if err := x.add("rule", rule.ID); err != nil {
156186
return err
157187
}
@@ -164,16 +194,22 @@ func (x *bundleExport) add(kind, id string) error {
164194
x.bundle.Workflows = append(x.bundle.Workflows, v)
165195
case "rule":
166196
v, ok := r.Rule(id)
167-
if !ok || v.ProjectID != x.project {
197+
if !ok {
168198
return missing
169199
}
200+
if err := owned(v.ProjectID, v.Shared || v.ProjectID == ""); err != nil {
201+
return err
202+
}
170203
v.ProjectID, v.Shared = "", false
171204
x.bundle.Rules = append(x.bundle.Rules, v)
172205
case "sandbox":
173206
v, ok := r.Sandbox(id)
174-
if !ok || v.ProjectID != x.project {
207+
if !ok {
175208
return missing
176209
}
210+
if err := owned(v.ProjectID, v.Shared || v.ProjectID == ""); err != nil {
211+
return err
212+
}
177213
if len(v.Environment) != 0 {
178214
x.binding("sandbox-environment", id)
179215
}

0 commit comments

Comments
 (0)