Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667package session
import ( "fmt" "regexp" "strings"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/dataplane")
// safeScope is a scope that may fill a clone URL: names of letters,// digits, dots, dashes, and underscores, each starting with a letter,// a digit, or an underscore, joined by slashes, such as// example/neweather.org. A scope comes from an event's payload, so one// that could reach another host or another path, or that git could// read as an option, is refused. No name is then . or ..var safeScope = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9_.-]*(/[A-Za-z0-9_][A-Za-z0-9_.-]*)*$`)
// safeDID is a scope that is an AT Protocol DID, such as// did:plc:olwvpwv4t3realdbcqrpdxqq, which names a repository on// Tangled. Its method is lowercase letters and digits, and its// identifier is names of the same characters as safeScope's, joined by// colons. It has no slash, so it stays one segment of a URL's path, and// no percent sign, so no escape can decode to a slash or a dot.var safeDID = regexp.MustCompile(`^did:[a-z0-9]+:[A-Za-z0-9_][A-Za-z0-9_.-]*(:[A-Za-z0-9_][A-Za-z0-9_.-]*)*$`)
// maxScope is the longest scope that fills a clone URL. It is well// within the 2048 characters that the Session's origin.scope allows.const maxScope = 1024
// Workspace answers the clone URLs of a Session's turns, the working// directory first. A Session that an event of a Signal in the// Mandate's fromScope started clones the URL with the event's scope// first, then the Mandate's repositories, less any whose name is// already cloned. Any other Session clones the repositories only. A// Session with nothing to clone is an error.func Workspace(mandate *v1alpha1.Mandate, origin *v1alpha1.Origin) ([]string, error) { fixed := mandate.Spec.Workspace.Repositories var template string if origin != nil && origin.Scope != "" { template = mandate.Spec.Workspace.FromScope[origin.Signal] } if template == "" { if len(fixed) == 0 { return nil, fmt.Errorf("the Mandate %s has no repository for this Session to clone", mandate.Metadata.Name) } return fixed, nil } if len(origin.Scope) > maxScope || !(safeScope.MatchString(origin.Scope) || safeDID.MatchString(origin.Scope)) { return nil, fmt.Errorf("the scope %q cannot fill a clone URL", origin.Scope) } first := strings.ReplaceAll(template, "{scope}", origin.Scope) name, err := dataplane.RepositoryName(first) if err != nil { return nil, err } repositories := []string{first} for _, repository := range fixed { if other, err := dataplane.RepositoryName(repository); err == nil && other == name { continue } repositories = append(repositories, repository) } return repositories, nil}