Stabilize guarded model delivery

Add validated OpenCode-only delivery capabilities, canonical linked-worktree access checks, isolated candidate verification, and guarded main settlement.

Wire the bounded reconnect-foreground controlled HTTP fixture through ordinary server construction so target verification obtains server-controlled provenance and rejects unknown or incompatible fixture evidence.

Checked packaged templates and packaged authoring skill references; no updates are needed because this draft-only infrastructure activates no definition. Recorded the stale reference and activation guidance in docs/notes/delivery-stabilization.md for the docs fold.
This commit is contained in:
2026-10-18 14:50:18 +01:00
parent 66b5f60b28
commit 2cdb9a9e93
15 changed files with 1335 additions and 5 deletions
+1
View File
@@ -19,6 +19,7 @@ type AgentClient struct {
// ArtifactAccess is supplied only by the runner. Write is this visit's
// directory; Read contains this and completed earlier visits in the same run.
ArtifactAccess ArtifactAccess
LandingAccess LandingAccess
OnSession func(string, bool) error
SessionID string
Transcript string
+14 -2
View File
@@ -59,7 +59,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for block := range def.Blocks {
switch block {
case "output", "prompt", "edges", "configs", "tools", "permissions":
case "output", "prompt", "edges", "configs", "tools", "permissions", "checks", "verification":
default:
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
}
@@ -70,6 +70,9 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if _, err := StagePermissions(def); err != nil {
return ModelSpec{}, err
}
if _, err := VerificationTargetFor(def); err != nil {
return ModelSpec{}, err
}
for _, key := range []string{"provider", "model"} {
if def.Configs[key] == "" {
return ModelSpec{}, fmt.Errorf("missing config %s", key)
@@ -77,7 +80,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for key := range def.Configs {
switch key {
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness":
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness", "delivery":
default:
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
}
@@ -92,6 +95,15 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if backend != "opencode" && backend != "pi" {
return ModelSpec{}, fmt.Errorf("harness must be opencode or pi")
}
if delivery := def.Configs["delivery"]; delivery != "" && delivery != "true" {
return ModelSpec{}, fmt.Errorf("delivery must be true when set")
}
if def.Configs["delivery"] == "true" && backend != "opencode" {
return ModelSpec{}, fmt.Errorf("delivery is supported only by the opencode harness")
}
if _, present := def.Blocks["checks"]; present && def.Configs["delivery"] != "true" {
return ModelSpec{}, fmt.Errorf("checks block is supported only by delivery stages")
}
if backend == "pi" {
tools, _, _ := ToolNames(def)
if _, err := piTools(tools); err != nil {
+75
View File
@@ -14,6 +14,77 @@ type ArtifactAccess struct {
Read []string
}
// LandingAccess is the server-derived external capability for a verified
// delivery operation. Empty or malformed values grant nothing.
type LandingAccess struct {
MainWorktree string
GitMetadata string
LockPath string
}
func (a LandingAccess) valid() bool {
for _, path := range []string{a.MainWorktree, a.GitMetadata, a.LockPath} {
if path == "" || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.ContainsAny(path, "*?\\") {
return false
}
}
// The server grants the canonical repository metadata directory as OpenCode
// V2's smallest external-directory unit. Keep the lock inside that directory
// so a caller cannot turn a valid metadata capability into access elsewhere.
return a.GitMetadata == filepath.Join(a.MainWorktree, ".git") &&
a.LockPath == filepath.Join(a.GitMetadata, "pipelines-landing.lock")
}
// DeliveryEnabled is the narrow, declarative capability check used before the
// runner supplies any landing paths. Only a validated model definition can ask
// for it; runtime paths are still derived by the server.
func DeliveryEnabled(def Definition) (bool, error) {
delivery := def.Configs["delivery"]
if delivery == "" {
return false, nil
}
if delivery != "true" {
return false, fmt.Errorf("delivery must be true when set")
}
if EffectiveType(def.Type) != ModelType {
return false, fmt.Errorf("delivery is supported only by model stages")
}
if _, err := (ModelStage{}).Spec(def); err != nil {
return false, fmt.Errorf("invalid delivery model stage: %w", err)
}
if _, err := DeliveryChecks(def); err != nil {
return false, err
}
return true, nil
}
// DeliveryChecks returns the bounded, definition-declared checks that the
// runner executes against a committed delivery candidate before canonical main
// is changed. They
// are intentionally a block rather than a generic runner setting: each
// workflow owns its repository-specific verification commands.
func DeliveryChecks(def Definition) ([]string, error) {
text := strings.TrimSpace(def.Blocks["checks"])
if text == "" {
return nil, fmt.Errorf("delivery stages require a nonempty checks block")
}
var checks []string
for _, line := range strings.Split(text, "\n") {
check := strings.TrimSpace(line)
if check == "" {
continue
}
if len(check) > 4096 {
return nil, fmt.Errorf("delivery check exceeds 4096 bytes")
}
checks = append(checks, check)
}
if len(checks) == 0 || len(checks) > 20 {
return nil, fmt.Errorf("delivery stages require 1 to 20 checks")
}
return checks, nil
}
// ToolNames validates model-stage permissions, not general graph semantics.
// A present empty block explicitly denies all tools.
func ToolNames(def Definition) ([]string, bool, error) {
@@ -155,6 +226,10 @@ func (c *AgentClient) permissionRules() []map[string]string {
allow("external_directory", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
allow("edit", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
}
if c.LandingAccess.valid() {
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.MainWorktree, "*")))
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.GitMetadata, "*")))
}
for _, pattern := range []string{"../*", "*/../*"} {
rules = append(rules, map[string]string{"action": "external_directory", "resource": pattern, "effect": "deny"})
}
+104
View File
@@ -174,3 +174,107 @@ func TestArtifactPermissionsAreExactAndToolScoped(t *testing.T) {
t.Fatal("missing artifact scope enabled an external directory")
}
}
func TestLandingPermissionsAreExactAndServerDerived(t *testing.T) {
access := LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/repos/pipelines/.git/pipelines-landing.lock"}
client := &AgentClient{Tools: []string{"read", "shell"}, LandingAccess: access}
rules := client.permissionRules()
for _, path := range []string{"/repos/pipelines/go.mod", "/repos/pipelines/.git/worktrees/item/HEAD", "/repos/pipelines/.git/pipelines-landing.lock"} {
if permissionEffect(rules, "external_directory", path) != "allow" {
t.Fatalf("verified landing path denied: %q", path)
}
}
for _, path := range []string{"/repos/other/go.mod", "/repos/pipelines/../other/go.mod", "/Users/alan/.git/config", "/.git/config"} {
if permissionEffect(rules, "external_directory", path) != "deny" {
t.Fatalf("unverified landing path allowed: %q", path)
}
}
client.LandingAccess.MainWorktree = "/repos/pipelines/../other"
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/go.mod") != "deny" {
t.Fatal("non-canonical landing capability allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/tmp/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/tmp/pipelines-landing.lock") != "deny" {
t.Fatal("landing lock outside verified metadata allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/other/.git", LockPath: "/repos/other/.git/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/.git/config") != "deny" {
t.Fatal("unrelated Git metadata allowed")
}
}
func TestDeliveryEnabledRequiresValidatedModelStage(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Blocks["checks"] = "go test ./..."
if enabled, err := DeliveryEnabled(def); err != nil || !enabled {
t.Fatalf("valid delivery stage = %v, %v", enabled, err)
}
for name, change := range map[string]func(*Definition){
"malformed value": func(d *Definition) { d.Configs["delivery"] = "yes" },
"non-model": func(d *Definition) { d.Type = HumanType },
"invalid model": func(d *Definition) { d.Configs["unexpected"] = "value" },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Configs = map[string]string{}
for key, value := range def.Configs {
copy.Configs[key] = value
}
change(&copy)
if enabled, err := DeliveryEnabled(copy); err == nil || enabled {
t.Fatalf("invalid delivery stage = %v, %v", enabled, err)
}
})
}
}
func TestDeliveryRejectsPiBeforeAgentExecution(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Configs["harness"] = "pi"
def.Blocks["checks"] = "true"
if err := (ModelStage{}).Validate(def); err == nil || !strings.Contains(err.Error(), "opencode harness") {
t.Fatal("Pi delivery was accepted", err)
}
delete(def.Configs, "delivery")
delete(def.Blocks, "checks")
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal("Pi non-delivery was changed", err)
}
}
func TestDeliveryChecksRequireBoundedDeliveryOnlyDeclaration(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
for name, mutate := range map[string]func(*Definition){
"missing": func(d *Definition) {},
"empty": func(d *Definition) { d.Blocks["checks"] = "\n" },
"non delivery": func(d *Definition) { d.Blocks["checks"] = "go test ./..." },
"too many checks": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("true\n", 21) },
"oversized check": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("x", 4097) },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Blocks = map[string]string{}
copy.Configs = map[string]string{}
for key, value := range def.Blocks {
copy.Blocks[key] = value
}
for key, value := range def.Configs {
copy.Configs[key] = value
}
mutate(&copy)
if name == "non delivery" {
delete(copy.Configs, "delivery")
if _, err := (ModelStage{}).Spec(copy); err == nil {
t.Fatal("non-delivery checks block was accepted")
}
return
}
if _, err := DeliveryEnabled(copy); err == nil {
t.Fatal("invalid delivery checks were accepted")
}
})
}
}
+97
View File
@@ -0,0 +1,97 @@
package stage
import (
"context"
"fmt"
"strings"
)
// VerificationTarget declares the controlled fixture that can substantiate a
// target-specific behavior claim. It is intentionally data-only: the runner
// does not execute fixture input supplied by a stage result.
type VerificationTarget struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationEvidence is returned by a server-controlled fixture workflow,
// never parsed from a model's result.
type VerificationEvidence struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationRunner is the narrow server seam for a controlled fixture or
// workflow. The runner selects it during server setup; model output cannot
// select a backend, revision, fixture, or executable.
type VerificationRunner func(context.Context, VerificationTarget) (VerificationEvidence, error)
// VerificationTargetFor parses an optional, bounded verification block. A
// declared target requires all provenance fields so an old or other-backend
// fixture cannot be presented as evidence for the current target.
func VerificationTargetFor(def Definition) (*VerificationTarget, error) {
text, declared := def.Blocks["verification"]
if !declared {
return nil, nil
}
if len(text) > 4096 {
return nil, fmt.Errorf("verification block exceeds 4096 bytes")
}
values := map[string]string{}
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
key, value, ok := strings.Cut(line, "=")
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
if !ok || value == "" || len(value) > 256 {
return nil, fmt.Errorf("invalid verification entry %q", line)
}
switch key {
case "backend", "revision", "capability", "fixture":
default:
return nil, fmt.Errorf("unknown verification entry %q", key)
}
if _, exists := values[key]; exists {
return nil, fmt.Errorf("duplicate verification entry %q", key)
}
values[key] = value
}
target := &VerificationTarget{Backend: values["backend"], Revision: values["revision"], Capability: values["capability"], Fixture: values["fixture"]}
if target.Backend == "" || target.Revision == "" || target.Capability == "" || target.Fixture == "" {
return nil, fmt.Errorf("verification requires backend, revision, capability, and fixture")
}
return target, nil
}
// VerifyTargetEvidence compares server-collected controlled-fixture provenance
// with the workflow declaration. It deliberately accepts no model result: a
// stage cannot substantiate its own target-specific behavior claim.
func VerifyTargetEvidence(target VerificationTarget, evidence VerificationEvidence) error {
for key, want := range map[string]string{
"backend": target.Backend, "revision": target.Revision, "capability": target.Capability, "fixture": target.Fixture,
} {
got := map[string]string{
"backend": evidence.Backend, "revision": evidence.Revision, "capability": evidence.Capability, "fixture": evidence.Fixture,
}[key]
if got != want {
return fmt.Errorf("verification evidence %s does not match declared target", key)
}
}
return nil
}
// VerifyTargetResult is retained for callers that only need declaration
// validation. Result-provided provenance is intentionally not evidence.
func VerifyTargetResult(def Definition, _ Values) error {
target, err := VerificationTargetFor(def)
if err != nil || target == nil {
return err
}
return fmt.Errorf("verification target %q requires a server-controlled fixture runner", target.Fixture)
}
+27
View File
@@ -0,0 +1,27 @@
package stage
import "testing"
func TestTargetVerificationRejectsWrongBackendEvidence(t *testing.T) {
def := modelDefinition()
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground"
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal(err)
}
target, err := VerificationTargetFor(def)
if err != nil {
t.Fatal(err)
}
good := VerificationEvidence{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, good); err != nil {
t.Fatalf("matching fixture evidence rejected: %v", err)
}
wrongBackend := VerificationEvidence{Backend: "old-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, wrongBackend); err == nil {
t.Fatal("wrong-backend fixture evidence accepted")
}
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123"
if err := (ModelStage{}).Validate(def); err == nil {
t.Fatal("incomplete target verification declaration accepted")
}
}