Skip to content

Instantly share code, notes, and snippets.

@kimjooyoon
Last active October 13, 2026 23:44
Show Gist options
  • Select an option

  • Save kimjooyoon/af3d75e04ac625e0e08652b8b6a07726 to your computer and use it in GitHub Desktop.

Select an option

Save kimjooyoon/af3d75e04ac625e0e08652b8b6a07726 to your computer and use it in GitHub Desktop.
Gooo service-policy quickstart: typed input checks and widening witnesses in a Go program

Gooo service policy: a small Go consumer

Before changing an API or MCP service grant, distinguish malformed input from a valid denial, compare the same request against both rules, and find a request that gains access. This four-case example runs policy definitions written in Gooo through generated Go code in an ordinary Go program.

This example assumes Go 1.27.2 is already installed and on your PATH. Check GOWORK=off GOTOOLCHAIN=local go version before running. These are the same toolchain settings used by the run command below. Use Download ZIP on this Gist and extract the archive into a new directory outside another Go project. The extracted directory should contain main.go, go.mod, go.sum, and README.md. Open a terminal there and run:

GOWORK=off GOTOOLCHAIN=local go run -mod=readonly .

The first run downloads the public Go module pinned in go.mod:

github.com/kimjooyoon/gooo-go-ports/examples/agentmesh-service-policy
v0.0.0-20261013184627-743c983dfc4e

This version identifies commit 743c983dfc4e6dab75451ab14cd153df7c9a2d92. Keep go.mod and go.sum with the example to retain that version and its checksum. The program imports generated native Go code from Gooo sources; it does not need a Gooo compiler at runtime. It has no repository checkout, local replacement, or relative source-path dependency.

Expected output is four JSON lines:

{"label":"valid-denial","decision":{"domain_status":"VALID_INPUT","invalid_field":"","matched":false}}
{"label":"invalid-input","decision":{"domain_status":"INVALID_INPUT","invalid_field":"req_type","matched":false}}
{"label":"request-widened","decision":{"domain_status":"VALID_INPUT","invalid_field":"","before_match":false,"after_match":true,"change":"WIDENED"}}
{"label":"widening-witness","decision":{"domain_status":"VALID_INPUT","invalid_field":"","status":"WIDENING_WITNESS","rule_id":"CHECKED_CANDIDATE_REQUEST","supported_subset":false,"has_witness":true,"witness_type":"mcp","witness_name":"a.y","before_match":false,"after_match":true}}

The first matcher result is a valid denial: weather does not match the calculator grant. The second call has a malformed service type. Its INVALID_INPUT result is a typed refusal, and its matched: false field is a sentinel. Check domain_status before using any semantic field; an unknown status or a Go error must stop the caller's decision path. The example handles every Go error and checks the expected domain status explicitly.

The impact call shows that replacing mcp://calculator with mcp://* newly matches the weather request. The refinement call takes only the old and candidate patterns and returns mcp://a.y as a widening witness: the candidate mcp://a.* matches it, while the old mcp://*.x does not.

The represented domain is bounded canonical ASCII, with at most 128 bytes per pattern, request type, or request name. It supports the declared single-pattern scheme/name grammar, including its limited wildcard forms; it is not a general URI parser. Refinement compares one old pattern with one candidate pattern. These pure calls compare supplied facts; they do not authorize a service call, check current policy authority, apply a policy change, or establish availability preservation. No human approval is needed to compute these results, and this example dispatches no external effect.

Sources and review status

Project-affiliated example by asketeddy, prepared with Codex assistance. Gooo Go Ports is independent; no Google endorsement is claimed.

The pinned source was merged in PR #78. Its full PR verification passed. The four results above were also checked using this consumer's public module version and checksum.

module example.com/gooo-service-policy-quickstart
go 1.27.2
require github.com/kimjooyoon/gooo-go-ports/examples/agentmesh-service-policy v0.0.0-20261013184627-743c983dfc4e
github.com/kimjooyoon/gooo-go-ports/examples/agentmesh-service-policy v0.0.0-20261013184627-743c983dfc4e h1:kV9l3paATDR+VVua9scBMh/YVdFtyimh5KN8Afy4/lY=
github.com/kimjooyoon/gooo-go-ports/examples/agentmesh-service-policy v0.0.0-20261013184627-743c983dfc4e/go.mod h1:aEFFWumPpadrR0onX8impgjTr3WfhNNA+jNkg7Vv4hI=
package main
import (
"encoding/json"
"fmt"
"os"
policy "github.com/kimjooyoon/gooo-go-ports/examples/agentmesh-service-policy"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
denied, err := policy.EvaluateMatcherChecked(policy.MatcherFacts{
Pattern: "mcp://calculator", ReqType: "mcp", ReqName: "weather",
})
if err != nil {
return fmt.Errorf("matcher: %w", err)
}
// Read semantic fields only after the domain result is exactly VALID_INPUT.
if denied.Decision.DomainStatus != "VALID_INPUT" {
return fmt.Errorf("matcher: unexpected input status %q", denied.Decision.DomainStatus)
}
if err := show("valid-denial", denied.Decision); err != nil {
return err
}
malformed, err := policy.EvaluateMatcherChecked(policy.MatcherFacts{
Pattern: "*", ReqType: "1invalid", ReqName: "weather",
})
if err != nil {
return fmt.Errorf("malformed matcher: %w", err)
}
if malformed.Decision.DomainStatus != "INVALID_INPUT" || malformed.Decision.InvalidField != "req_type" {
return fmt.Errorf("malformed matcher: expected typed req_type rejection")
}
// The false matched field is a refusal sentinel; do not interpret it as denial.
if err := show("invalid-input", malformed.Decision); err != nil {
return err
}
impact, err := policy.EvaluateImpactChecked(policy.ImpactFacts{
Before: "mcp://calculator", After: "mcp://*", ReqType: "mcp", ReqName: "weather",
})
if err != nil {
return fmt.Errorf("impact: %w", err)
}
if impact.Decision.DomainStatus != "VALID_INPUT" {
return fmt.Errorf("impact: unexpected input status %q", impact.Decision.DomainStatus)
}
if err := show("request-widened", impact.Decision); err != nil {
return err
}
refinement, err := policy.EvaluateRefinementChecked(policy.RefinementFacts{
Before: "mcp://*.x", After: "mcp://a.*",
})
if err != nil {
return fmt.Errorf("refinement: %w", err)
}
if refinement.Decision.DomainStatus != "VALID_INPUT" {
return fmt.Errorf("refinement: unexpected input status %q", refinement.Decision.DomainStatus)
}
return show("widening-witness", refinement.Decision)
}
func show(label string, decision any) error {
return json.NewEncoder(os.Stdout).Encode(struct {
Label string `json:"label"`
Decision any `json:"decision"`
}{label, decision})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment