Files
zeekay 3bb86da715 Add sdk/: the shared framework, from the private build
The code is the private build's sdk/ with the module path swapped and the
hostnames, private issue refs and incident prose taken out. Its self-contained
tests come with it; the framework's own tests need the private test kits and stay
behind. Builds and vets in this module; needs luxfi/kms v1.12.26.

Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV
2026-11-20 20:25:04 -08:00

119 lines
5.6 KiB
Go

package cloud
import (
"context"
"errors"
)
// sync.go is the inversion layer between the universal sync ENGINE (apps/sync)
// and the webhook triggers that fire it (apps/integrations for the GitHub App,
// the forge's own push webhook). It is the same idiom as git_import.go's
// GitImporter: the engine registers itself here at Mount; triggers call the
// package funcs below with NO import of the engine package, so nothing imports
// the sync package except apps (which mounts it). One direction, no cycles.
// SyncEvent is a provider-agnostic sync trigger. A webhook (GitHub push, Hanzo Git
// push) or a manual run builds one and hands it to the registered engine via Sync;
// the engine resolves the Syncs whose SOURCE matches (Provider, Locator/Repo)
// for the org and applies each. Flat + string-typed so it crosses the trigger→
// engine without importing the engine.
type SyncEvent struct {
Kind string // sync kind, e.g. "git"
Provider string // endpoint the event came from: "github" | "gitlab" | "hanzo-git"
Org string // tenant (from the signed installation / gateway identity)
Locator string // source repo locator (a clone URL, or "<org>/<repo>")
Repo string // short repo name (git)
Ref string // FULL ref: refs/heads/<branch> or refs/tags/<tag>
Before string
After string
Actor string // who made the upstream push — the loop guard compares it to the sync's own Actor
Token string // OPTIONAL short-lived credential the trigger already minted (git: installation token); never logged
Manual bool // a manual /run or an initial sync reconcile (no specific push)
Hop int // chained-propagation depth (bounded by the engine's hop limit)
}
// SyncResult is the outcome of dispatching one SyncEvent across the resolved syncs.
type SyncResult struct {
Ran int // syncs that reconciled a change
Skipped int // syncs resolved but skipped (loop guard / idempotent / direction off)
}
// SyncFunc is the reconcile entry the universal sync engine registers — a function,
// not an engine noun. The one implementation (clients/sync) registers it at
// Mount; triggers reach it via Sync.
type SyncFunc func(ctx context.Context, ev SyncEvent) (SyncResult, error)
// syncFn is the registered reconcile func (nil until the sync engine mounts). Read on the
// hot webhook path, written once at Mount — a plain var is fine (Mount happens
// before serving, like RegisterGitImporter).
var syncFn SyncFunc
// RegisterSync installs the sync reconcile func. nil-safe.
func RegisterSync(fn SyncFunc) { syncFn = fn }
// ErrSyncUnavailable is returned when the engine is not mounted — a fail-closed
// sentinel, never a silent success, so a trigger logs precisely rather than
// pretending it synced.
var ErrSyncUnavailable = errors.New("cloud: sync engine not registered")
// Sync dispatches one event to the registered reconcile func.
//
// Co-resident, it is a Go call. Otherwise it asks the sync app over the plane —
// because the TRIGGERS and the ENGINE are different apps and therefore different
// processes. A webhook lands on integrations, a push lands on git, and neither
// of them is where the engine runs, so the in-process call is nil on every path
// that actually fires. It answered ErrSyncUnavailable for all of them while the
// engine was up in the sibling process: every mirror and every chained
// propagation silently stopped happening, reported as "not registered" rather
// than as the reachable call it was.
//
// ErrSyncUnavailable is kept for the case it always described honestly — a
// registration this process was supposed to have and does not.
func Sync(ctx context.Context, ev SyncEvent) (SyncResult, error) {
if syncFn != nil {
return syncFn(ctx, ev)
}
if run := peersOf().Sync; run != nil {
return run(ctx, ev)
}
return SyncResult{}, ErrNoPeer
}
// ── git object-plane control (outbound mirror ensure/remove) ────────────────
// GitMirrorController ENSURES or REMOVES a repo's outbound mirror target.
// enabled=true declares the target (idempotent); enabled=false withdraws it.
// Declaring is all it does: the pushing happens on the next advance of that
// repository, fast-forward only like every other write this estate makes to a
// git host, so a target that exists is a fact about the repo rather than a job
// Somebody has to keep running.
type GitMirrorController interface {
EnsureMirror(ctx context.Context, org, project, repo, url string, enabled bool) error
}
var gitMirrorCtl GitMirrorController
// RegisterGitMirrorController installs the mirror controller. nil-safe.
func RegisterGitMirrorController(c GitMirrorController) { gitMirrorCtl = c }
// ErrGitMirrorControllerUnavailable is returned when the sync engine is not
// mounted — fail-closed, never a silent success.
var ErrGitMirrorControllerUnavailable = errors.New("cloud: git mirror controller not registered")
// EnsureGitMirror declares (enabled) or withdraws (disabled) the outbound target
// url on repo. Idempotent.
//
// Absent co-residency this asks the sync app over the plane rather than
// reporting a controller that was never going to be in this process. Declaring a
// mirror is the whole point of configuring one; refusing it here meant a sync
// that reconciled inbound forever and never pushed anything back.
func EnsureGitMirror(ctx context.Context, org, project, repo, url string, enabled bool) error {
if gitMirrorCtl != nil {
return gitMirrorCtl.EnsureMirror(ctx, org, project, repo, url, enabled)
}
if ensure := peersOf().EnsureGitMirror; ensure != nil {
return ensure(ctx, org, project, repo, url, enabled)
}
return ErrNoPeer
}