diff --git a/reviewer/GIT-SERVICE.md b/reviewer/GIT-SERVICE.md new file mode 100644 index 00000000..3660ac99 --- /dev/null +++ b/reviewer/GIT-SERVICE.md @@ -0,0 +1,90 @@ +# Git reviewer service composition + +The private Linux service owner joins the concrete Git source factory, +credential owner, polling controller and scoped GitHub publisher. This is an +internal assembly for the trusted startup layer. It provides no installed +daemon, public startup command, credential import or activation authority. +The existing owner gates still apply. + +## Startup and ownership + +Inputs come from the trusted release layer. They include the durable controller +journal, already opened unit/archive/runtime owners, helper and fixed manifest, +qualified release/hash, source work directory, explicit transports, eligibility +checks and trigger policy. Preserve the trigger policy's original `EnabledAt` +across restarts; resetting it would change which events polling can discover. + +Construction opens an unqualified credential owner and completes the concrete +[source factory's recovery](GIT-FACTORY.md) before assembling the controller. +It performs no signing, token mint, GitHub request, model invocation or polling. +The controller uses comment-command and automatic-event discovery, with the +same discovery reader supplying publication provenance. Reader and publisher +HTTP access use separate roles of the same credential owner. Raw capture and +restore callbacks cannot replace the source factory in this composition. + +A nil service with an error means source ownership did not transfer. The +caller retains its unit, archive, runtime, helper and journal. The newly opened +unqualified credential owner is disposed. If that disposal fails, the returned +service is retained instead. A nonnil service with any error must be kept for +explicit `Recover` or `Close`. Once the source factory returns a nonnil handle, +it owns the unit, archive and runtime. The helper, transports and controller +store/journal remain caller-owned. + +`Recover` is local and never refreshes credentials. Unknown old units or +unreturned work keep admission fenced and retain the original owners. Recovery +during shutdown may complete old physical-stop evidence but cannot restart the +controller or reopen admission. + +## Explicit qualification and work + +Only `Refresh` signs or mints. It holds the source factory's operation token +through readiness, root validation and credential qualification, so a new +capture cannot introduce an uncertain acquisition between those checks and +signing. Credential generation shutdown remains independent of that token. +Eligibility checks are repeated by credential and runtime operations. Checks +are trusted, read-only callbacks, must honor cancellation, and must not acquire +the service/factory operation token recursively. + +`Run` requires a successful explicit refresh. It never refreshes automatically +and starts its controller at most once. Cancellation or a returned run error +does not dispose the source and credential owners. The caller must close the +service and retain it if that close cannot prove completion. + +The concrete native runtime takes a bounded, copied catalog of explicitly +qualified releases and providers. A durable job selects its original release, +configured providers and identities. There is no default provider or fallback +release. Each provider gets its own reader bound to that job's admitted +snapshot and packet. The session gate binds job ID, snapshot, packet, config +hash, configured identity and round, then rechecks release and provider +eligibility. Native asset verification and containment still precede auth +acquisition. Catalog construction performs structural checks only. + +An explicit alternate runtime callback supports fake tests and trusted alternate +assemblies. Supplying both that callback and the native catalog is rejected. + +## Shutdown + +`Close` first closes admission and cancels any active lifecycle phase and run. +It waits for the run to return, closes the controller, closes the concrete source +factory, then closes the credential owner. A failed earlier step retains later +owners. Its outer deadline is 30 seconds or the caller's earlier deadline. +Refresh and recovery have three-minute ceilings. A callback that ignores its +cancelled context remains owned; a timed-out close does not free its locks or +claim it stopped. Retrying `Close` operates on the same retained owners. + +## Evidence and remaining gates + +Service tests use measured inert acquisition assets, fake unit/kernel evidence, +synthetic credentials and local TLS GitHub fixtures. They cover startup without +signing, both HTTP roles, constructor ownership, uncertain recovery, cancelled +qualification, generation drain, native release selection and a model-free +polling run with both caller and service shutdown. Native session tests use +closed gates before unopened asset/auth paths. No model is invoked. + +This assembly does not qualify a deployment. The public trusted operator entry +point, release packaging/promotion/rollback, native control tasks and durable +evaluation runner remain source work. Complete host containment qualification, +provider policy/entitlement and quota evidence, final App/trigger evidence and +explicit owner activation remain separate gates tracked in +[issue #176](https://github.com/hemsoft-dev/set-it-free-loop/issues/176). +Existing failed physical containment qualifications remain failed. diff --git a/reviewer/git_service_linux.go b/reviewer/git_service_linux.go new file mode 100644 index 00000000..592f7e7d --- /dev/null +++ b/reviewer/git_service_linux.go @@ -0,0 +1,315 @@ +//go:build linux + +package reviewer + +import ( + "context" + "errors" + "os" + "sync" + "time" +) + +// All inputs belong to the trusted service/release layer. Runtime must select +// the immutable job's explicitly qualified release, never candidate settings. +// Store/journal, helper and HTTP transports remain caller-owned. Unit, archive +// and runtime transfer only when the concrete factory returns a nonnil handle. +type gitReviewerServiceOptions struct { + Credentials GitHubCredentialOwnerOptions + Unit *gitUnitOwner + Archive *gitExportArchive + RuntimeRoot *gitRuntimeRoot + Helper *os.File + Manifest gitGrantManifest + Triggers TriggerPolicy + Release, ReleaseHash, Directory string + NativeReleases map[string]map[Provider]NativeVMOptions + // Runtime is an explicit trusted fixture/alternate runtime seam. Supplying + // it together with the concrete native release catalog is rejected. + Runtime func(context.Context, Job, *JobEvidence) (Engine, error) + Check func(context.Context, Enrollment, string) error + Interval, MaxBackoff, TickTimeout time.Duration +} + +// This owner never installs credentials, chooses an account or starts work at +// construction. A nonnil handle returned with an error must be retained for +// explicit Recover/Close. No method silently recreates or replaces its owners. +type gitReviewerService struct { + options gitReviewerServiceOptions + credentials *GitHubCredentialOwner + source *gitSourceFactory + controller *PollingController + operation chan struct{} + mu sync.Mutex + startup bool + closing bool + closed bool + ran bool + phaseCancel context.CancelFunc + runCancel context.CancelFunc + runDone chan struct{} +} + +func (*gitReviewerService) String() string { return "gitReviewerService{redacted}" } +func (*gitReviewerService) GoString() string { return "gitReviewerService{redacted}" } +func (*gitReviewerService) MarshalJSON() ([]byte, error) { + return []byte(`"gitReviewerService{redacted}"`), nil +} + +func newGitReviewerService(ctx context.Context, o gitReviewerServiceOptions) (*gitReviewerService, error) { + if ctx == nil || ctx.Err() != nil || !o.Credentials.Journal.valid() || o.Credentials.Check == nil || o.Unit == nil || o.Archive == nil || o.RuntimeRoot == nil || o.Helper == nil || o.Triggers.validate() != nil || !objectID.MatchString(o.Release) || !validAuthHash(o.ReleaseHash) || (o.Runtime == nil) == (o.NativeReleases == nil) || o.Check == nil || o.Interval < time.Second || o.Interval > 5*time.Minute || o.MaxBackoff < o.Interval || o.MaxBackoff > time.Hour || o.TickTimeout < time.Second || o.TickTimeout > 5*time.Minute { + return nil, ErrLiveBlocked + } + s := &gitReviewerService{options: o, operation: make(chan struct{}, 1)} + s.operation <- struct{}{} + if o.NativeReleases != nil { + if o.NativeReleases[o.Release] == nil { + return nil, ErrLiveBlocked + } + runtime, err := newGitServiceNativeRuntime(o.NativeReleases, func(ctx context.Context, release string) error { + return s.check(ctx, o.Credentials.Journal.enrollment, release) + }) + if err != nil { + return nil, err + } + s.options.Runtime = runtime + } + credentials := o.Credentials + credentials.Check = func(ctx context.Context, e Enrollment) error { return s.check(ctx, e, o.Release) } + var err error + s.credentials, err = OpenGitHubCredentialOwner(ctx, credentials) + if err != nil { + return nil, err + } + s.source, err = newGitSourceFactory(ctx, s.credentials, o.Unit, o.Archive, o.RuntimeRoot, o.Helper, o.Manifest, o.Release, o.ReleaseHash, o.Directory) + if s.source == nil { + // No factory ownership transferred, and this unexposed credential owner + // has never refreshed or admitted work. Dispose only that local owner. + cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) + defer cancel() + if closeErr := s.credentials.Close(cleanup); closeErr != nil { + return s, errors.Join(err, closeErr) + } + return nil, err + } + if err != nil { + return s, err + } + if err = s.composeController(ctx); err != nil { + return s, err + } + s.startup = true + return s, nil +} + +func (s *gitReviewerService) composeController(ctx context.Context) error { + if s.controller != nil { + return nil + } + o := s.options + e := o.Credentials.Journal.enrollment + reader, err := NewGitHubTriggerDiscoveryHTTP(e, o.Credentials.Journal.store, s.credentials.Reader(), o.Triggers) + if err != nil { + return err + } + publisher, err := NewGitHubPublicationHTTP(e, reader, s.credentials.Publisher()) + if err != nil { + return err + } + s.controller, err = OpenPollingController(ctx, PollOptions{ + Store: o.Credentials.Journal.store, Enrollment: e, Release: o.Release, + Discovery: reader, Publisher: Publisher{Store: o.Credentials.Journal.store, Remote: publisher}, + Source: s.source, Runtime: o.Runtime, Check: s.check, + Now: o.Credentials.Journal.now, Interval: o.Interval, MaxBackoff: o.MaxBackoff, TickTimeout: o.TickTimeout, + }) + return err +} + +func (s *gitReviewerService) check(ctx context.Context, e Enrollment, release string) error { + if s == nil || ctx == nil || ctx.Err() != nil || e != s.options.Credentials.Journal.enrollment || !objectID.MatchString(release) { + return ErrLiveBlocked + } + s.mu.Lock() + ready := s.startup && !s.closing && !s.closed + s.mu.Unlock() + // Do not take the factory token here: Capture already holds it when it + // calls credential Ready. Current factory state fences Capture and Refresh. + if !ready || s.options.Credentials.Check(ctx, e) != nil || ctx.Err() != nil || s.options.Check(ctx, e, release) != nil || ctx.Err() != nil { + return ErrLiveBlocked + } + return nil +} + +func (s *gitReviewerService) enter(ctx context.Context) bool { + if s == nil || ctx == nil || ctx.Err() != nil || s.operation == nil { + return false + } + select { + case <-ctx.Done(): + return false + case <-s.operation: + if ctx.Err() != nil { + s.leave() + return false + } + return true + } +} +func (s *gitReviewerService) leave() { s.operation <- struct{}{} } + +func (s *gitReviewerService) begin(ctx context.Context, limit time.Duration, allowClosing bool) (context.Context, func(), error) { + if ctx == nil { + return nil, nil, ErrLiveBlocked + } + phase, cancel := context.WithTimeout(ctx, limit) + if !s.enter(phase) { + err := phase.Err() + cancel() + return nil, nil, errors.Join(ErrWorkerStopPending, err) + } + s.mu.Lock() + if s.closed || s.closing && !allowClosing { + s.mu.Unlock() + s.leave() + cancel() + return nil, nil, ErrFenced + } + s.phaseCancel = cancel + s.mu.Unlock() + return phase, func() { + cancel() + s.mu.Lock() + s.phaseCancel = nil + s.mu.Unlock() + s.leave() + }, nil +} + +// Recover is local, explicit and never refreshes credentials. During shutdown +// it may finish old stop evidence but cannot reopen admission or a controller. +func (s *gitReviewerService) Recover(ctx context.Context) error { + phase, finish, err := s.begin(ctx, 3*time.Minute, true) + if err != nil { + return err + } + defer finish() + if s.source == nil || s.source.Recover(phase) != nil { + return ErrWorkerStopPending + } + s.mu.Lock() + closing := s.closing + s.mu.Unlock() + if closing { + return nil + } + if err := s.composeController(phase); err != nil { + return err + } + s.mu.Lock() + defer s.mu.Unlock() + if s.closing || phase.Err() != nil { + return ErrFenced + } + s.startup = true + return nil +} + +// Refresh is the only signing/minting entry point. Keeping the concrete factory +// token through qualification prevents Capture from introducing uncertainty +// between the readiness check and signing. A run's stop path needs no such token. +func (s *gitReviewerService) Refresh(ctx context.Context) error { + phase, finish, err := s.begin(ctx, 3*time.Minute, false) + if err != nil { + return err + } + defer finish() + if s.source == nil || !s.source.enter(phase) { + return ErrLiveBlocked + } + defer s.source.leave() + if s.source.closed || !s.source.ready || s.source.checkRoot() != nil || s.check(phase, s.options.Credentials.Journal.enrollment, s.options.Release) != nil { + return ErrLiveBlocked + } + return s.credentials.Refresh(phase) +} + +// Run never refreshes. The caller must explicitly qualify credentials first. +// Once a run starts, its controller is not restarted; retained owners still need +// Close, including after Run returns an uncertain worker/recovery error. +func (s *gitReviewerService) Run(ctx context.Context) error { + phase, finish, err := s.begin(ctx, 5*time.Second, false) + if err != nil { + return err + } + if s.controller == nil || s.credentials.Ready(phase) != nil { + finish() + return ErrLiveBlocked + } + s.mu.Lock() + if s.ran || s.closing || phase.Err() != nil { + s.mu.Unlock() + finish() + return ErrFenced + } + run, cancel := context.WithCancel(ctx) + done := make(chan struct{}) + s.ran, s.runCancel, s.runDone = true, cancel, done + s.mu.Unlock() + finish() + defer func() { + cancel() + s.mu.Lock() + s.runCancel = nil + close(done) + s.mu.Unlock() + }() + return s.controller.Run(run) +} + +func (s *gitReviewerService) Close(ctx context.Context) error { + if s == nil || ctx == nil { + return ErrWorkerStopPending + } + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + s.mu.Lock() + s.closing, s.startup = true, false + if s.phaseCancel != nil { + s.phaseCancel() + } + if s.runCancel != nil { + s.runCancel() + } + done := s.runDone + s.mu.Unlock() + if !s.enter(ctx) { + return errors.Join(ErrWorkerStopPending, ctx.Err()) + } + defer s.leave() + if done != nil { + select { + case <-done: + case <-ctx.Done(): + return errors.Join(ErrWorkerStopPending, ctx.Err()) + } + } + if s.controller != nil { + if err := s.controller.Close(ctx); err != nil { + return err + } + } + if s.source != nil { + if err := s.source.Close(ctx); err != nil { + return err + } + } + if s.credentials != nil { + if err := s.credentials.Close(ctx); err != nil { + return err + } + } + s.mu.Lock() + s.closed = true + s.mu.Unlock() + return nil +} diff --git a/reviewer/git_service_linux_test.go b/reviewer/git_service_linux_test.go new file mode 100644 index 00000000..cc5e3315 --- /dev/null +++ b/reviewer/git_service_linux_test.go @@ -0,0 +1,326 @@ +//go:build linux + +package reviewer + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "strings" + "sync" + "sync/atomic" + "testing" + "time" +) + +func gitServiceFixture(t *testing.T) (gitReviewerServiceOptions, gitUnitFixture, *installationFixture, *installationFixture, *atomic.Int32) { + t.Helper() + r, f, runtime := gitFactoryAssetsFixture(t) + helper, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { helper.Close() }) + if r.stop(context.Background()) != nil || r.close() != nil || f.owner.Close() != nil { + t.Fatal("prepared fixture disposal") + } + unit, err := newGitUnitOwner(context.Background(), f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { unit.Close() }) + owner, credentials, reader, publisher, signs := credentialOwnerFixture(t) + if owner.Close(context.Background()) != nil { + t.Fatal("unqualified fixture credential owner close") + } + return gitReviewerServiceOptions{ + Credentials: credentials, Unit: unit, Archive: r.archive, RuntimeRoot: runtime, + Helper: helper, Manifest: r.manifest, Triggers: TriggerPolicy{EnabledAt: controlTime}, + Release: testJob().Release, ReleaseHash: r.binding.ReleaseHash, Directory: gitFactoryWorkRoot(t), + Runtime: func(context.Context, Job, *JobEvidence) (Engine, error) { return Engine{}, ErrLiveBlocked }, + Check: func(context.Context, Enrollment, string) error { return nil }, + Interval: time.Second, MaxBackoff: time.Minute, TickTimeout: time.Second, + }, f, reader, publisher, signs +} + +func TestGitReviewerServiceConstructionBeforeExplicitRefresh(t *testing.T) { + o, _, reader, publisher, signs := gitServiceFixture(t) + s, err := newGitReviewerService(context.Background(), o) + if err != nil { + t.Fatal(err) + } + defer s.Close(context.Background()) + rCalls, rMints := credentialCounts(reader) + pCalls, pMints := credentialCounts(publisher) + if signs.Load() != 0 || rCalls != 0 || pCalls != 0 || rMints != 0 || pMints != 0 || !s.source.ready || s.controller.options.Source != s.source { + t.Fatal("construction acquired credentials or skipped concrete startup") + } + discovery, ok := s.controller.options.Discovery.(*GitHubTriggerDiscoveryHTTP) + if !ok || discovery.policy != o.Triggers { + t.Fatal("selected trigger policy not composed") + } + read := discovery.reader.http.client.Transport.(githubReadTransport).inner.(*GitHubCredentialRoleTransport) + write := s.controller.options.Publisher.Remote.(*GitHubPublicationHTTP) + publish := write.client.Transport.(*GitHubCredentialRoleTransport) + if read.owner != s.credentials.state || read.role != GitHubReader || publish.owner != s.credentials.state || publish.role != GitHubPublisher || write.reader != discovery { + t.Fatal("source/publication roles or trigger provenance mixed") + } + if s.Run(context.Background()) == nil || signs.Load() != 0 { + t.Fatal("Run implicitly acquired credentials") + } + if err := s.Refresh(context.Background()); err != nil { + t.Fatal(err) + } + _, rMints = credentialCounts(reader) + _, pMints = credentialCounts(publisher) + if signs.Load() != 1 || rMints != 1 || pMints != 1 { + t.Fatal("explicit qualification replayed or omitted a role") + } + response, err := read.RoundTrip(installationQuery(t, "GET", githubRepoPath)) + if err != nil { + t.Fatal(err) + } + response.Body.Close() + var acknowledgment struct{ ID int64 } + if _, err := write.request(context.Background(), "POST", githubIssue(alphaScope())+"/comments", map[string]string{"body": "model-free service fixture"}, http.StatusCreated, &acknowledgment); err != nil || acknowledgment.ID != 91 { + t.Fatal("composed publisher failed its qualified synthetic write", err) + } + _, pMints = credentialCounts(publisher) + if signs.Load() != 1 || pMints != 1 { + t.Fatal("publisher silently refreshed credentials") + } + if s.Close(context.Background()) != nil || !s.source.closed || !s.credentials.state.closed { + t.Fatal("successful shutdown lost an owner") + } + reopened, err := OpenGitHubCredentialOwner(context.Background(), o.Credentials) + if err != nil { + t.Fatal("credential lock retained after successful service close", err) + } + defer reopened.Close(context.Background()) + if reopened.Ready(context.Background()) == nil || signs.Load() != 1 { + t.Fatal("replacement inherited readiness or signed") + } +} + +func TestGitReviewerServiceInvalidFactoryKeepsCallerOwnership(t *testing.T) { + o, _, _, _, signs := gitServiceFixture(t) + o.Directory = "relative-invalid-work-root" + if s, err := newGitReviewerService(context.Background(), o); err == nil || s != nil || signs.Load() != 0 || o.Unit.closed || o.Archive.closed { + t.Fatal("invalid factory transferred inputs or kept a credential owner", err) + } + owner, err := OpenGitHubCredentialOwner(context.Background(), o.Credentials) + if err != nil { + t.Fatal("constructor leaked its otherwise unqualified credential lock", err) + } + defer owner.Close(context.Background()) + fd, err := o.RuntimeRoot.directoryFD() + if err != nil { + t.Fatal("invalid constructor disposed the caller's runtime", err) + } + fd.Close() +} + +func TestGitReviewerServiceRevokedEligibilityDoesNotSign(t *testing.T) { + o, _, reader, publisher, signs := gitServiceFixture(t) + o.Check = func(context.Context, Enrollment, string) error { return ErrLiveBlocked } + s, err := newGitReviewerService(context.Background(), o) + if err != nil { + t.Fatal("local construction should not activate gates", err) + } + defer s.Close(context.Background()) + if s.Refresh(context.Background()) == nil { + t.Fatal("revoked release acquired credentials") + } + rCalls, _ := credentialCounts(reader) + pCalls, _ := credentialCounts(publisher) + if signs.Load() != 0 || rCalls != 0 || pCalls != 0 { + t.Fatal("release denial reached signing or HTTP") + } +} + +func TestGitReviewerServiceUnknownStartupRetainsOwners(t *testing.T) { + o, f, reader, publisher, signs := gitServiceFixture(t) + r, err := o.Unit.read(f.binding.Attempt) + if err != nil { + t.Fatal(err) + } + r.Started, r.Stopped, r.Revoked = true, false, false + r.StartJob = "/org/freedesktop/systemd1/job/1" + if o.Unit.write(r) != nil { + t.Fatal("synthetic unknown start receipt") + } + f.manager.observeErr = errors.New("synthetic missing old unit") + s, err := newGitReviewerService(context.Background(), o) + if err == nil || s == nil || s.source == nil || s.source.ready || s.controller != nil { + t.Fatal("uncertain startup abandoned ownership or started polling", err) + } + if s.Refresh(context.Background()) == nil || signs.Load() != 0 { + t.Fatal("unrecovered service acquired credentials") + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) + defer cancel() + if other, err := OpenGitHubCredentialOwner(ctx, o.Credentials); err == nil || other != nil { + t.Fatal("failed startup released its credential owner lock") + } + if s.Close(context.Background()) == nil || s.source.unit.closed || s.credentials.state.closed { + t.Fatal("uncertain close disposed later owners") + } + // Explicit fake changed-boot proof retires only this fixture's old record. + // It is not evidence that a real worker or host reboot was qualified. + f.manager.kernel.boot = "synthetic new boot for explicit service recovery" + if s.Recover(context.Background()) != nil || s.Refresh(context.Background()) == nil || s.controller != nil { + t.Fatal("shutdown recovery reopened admission") + } + if s.Close(context.Background()) != nil || !s.closed { + t.Fatal("recovered shutdown could not finish") + } + rCalls, _ := credentialCounts(reader) + pCalls, _ := credentialCounts(publisher) + if signs.Load() != 0 || rCalls != 0 || pCalls != 0 { + t.Fatal("local startup/recovery dispatched credentials") + } +} + +func TestGitReviewerServiceCloseCancelsRefreshButRetainsUnreturnedCallback(t *testing.T) { + o, _, reader, publisher, signs := gitServiceFixture(t) + entered, release := make(chan struct{}), make(chan struct{}) + var once sync.Once + unblock := func() { once.Do(func() { close(release) }) } + defer unblock() + o.Credentials.JWT = func(context.Context) ([]byte, error) { + signs.Add(1) + close(entered) + <-release + return []byte("synthetic-app-jwt"), nil + } + s, err := newGitReviewerService(context.Background(), o) + if err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { result <- s.Refresh(context.Background()) }() + select { + case <-entered: + case <-time.After(time.Second): + t.Fatal("signing callback not reached") + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) + if s.source.enter(ctx) { + s.source.leave() + t.Fatal("refresh released factory fence before qualification") + } + cancel() + ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond) + if !errors.Is(s.Close(ctx), ErrWorkerStopPending) || s.source.closed || s.credentials.state.closed { + t.Fatal("unreturned callback lost source/credential ownership") + } + cancel() + unblock() + if err := <-result; err == nil { + t.Fatal("canceled refresh reported success") + } + if s.Close(context.Background()) != nil { + t.Fatal("explicit close retry failed") + } + rCalls, _ := credentialCounts(reader) + pCalls, _ := credentialCounts(publisher) + if signs.Load() != 1 || rCalls != 0 || pCalls != 0 { + t.Fatal("late callback caused credential HTTP or replay") + } +} + +func TestGitReviewerServiceRefreshDrainsGenerationWithoutFactoryDeadlock(t *testing.T) { + o, _, _, _, signs := gitServiceFixture(t) + s, err := newGitReviewerService(context.Background(), o) + if err != nil { + t.Fatal(err) + } + defer s.Close(context.Background()) + if s.Refresh(context.Background()) != nil { + t.Fatal("initial explicit refresh") + } + stops := 0 + g, err := s.credentials.retainReaderGeneration(context.Background(), func(context.Context) error { stops++; return nil }) + if err != nil { + t.Fatal(err) + } + s.credentials.state.mu.Lock() + changed := s.credentials.state.changed + s.credentials.state.mu.Unlock() + result := make(chan error, 1) + go func() { result <- s.Refresh(context.Background()) }() + select { + case <-changed: + case <-time.After(time.Second): + t.Fatal("replacement did not begin draining") + } + if g.stop(context.Background()) != nil { + t.Fatal("generation stop needed the outer factory token") + } + select { + case err := <-result: + if err != nil || signs.Load() != 2 || stops != 1 { + t.Fatal("drained refresh did not commit exactly once", err, signs.Load(), stops) + } + case <-time.After(time.Second): + t.Fatal("refresh deadlocked while holding source ownership") + } +} + +func TestGitReviewerServiceModelFreeRunAndExplicitClose(t *testing.T) { + for _, viaService := range []bool{false, true} { + t.Run(fmt.Sprint("service-close-", viaService), func(t *testing.T) { + o, _, _, _, signs := gitServiceFixture(t) + transport := o.Credentials.ReaderTransport + listed := make(chan struct{}) + var once sync.Once + var modelCalls atomic.Int32 + o.Credentials.ReaderTransport = githubResponseTransport(func(q *http.Request) (*http.Response, error) { + if q.URL.Path == githubRepoPath+"/pulls" { + if q.Header.Get("Authorization") != "Bearer synthetic-installation-reader" { + t.Error("polling did not use the reader role") + } + once.Do(func() { close(listed) }) + return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("[]")), Request: q}, nil + } + return transport.RoundTrip(q) + }) + o.Runtime = func(context.Context, Job, *JobEvidence) (Engine, error) { + modelCalls.Add(1) + return Engine{}, ErrLiveBlocked + } + s, err := newGitReviewerService(context.Background(), o) + if err != nil || s.Refresh(context.Background()) != nil { + t.Fatal("synthetic service qualification", err) + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + result := make(chan error, 1) + go func() { result <- s.Run(ctx) }() + select { + case <-listed: + case <-time.After(2 * time.Second): + cancel() + t.Fatal("composed polling reader did not list empty fixture scope") + } + if viaService { + if err := s.Close(context.Background()); err != nil { + t.Fatal("Close could not join Run", err) + } + } else { + cancel() + } + if err := <-result; !errors.Is(err, context.Canceled) { + t.Fatal("service run did not retain cancellation", err) + } + if s.source.closed != viaService || s.credentials.state.closed != viaService || signs.Load() != 1 || modelCalls.Load() != 0 { + t.Fatal("model-free Run renewed or discarded retained owners") + } + if s.Close(context.Background()) != nil || s.Run(context.Background()) == nil { + t.Fatal("explicit shutdown failed or reopened the service") + } + }) + } +} diff --git a/reviewer/git_service_runtime_linux.go b/reviewer/git_service_runtime_linux.go new file mode 100644 index 00000000..b77adadc --- /dev/null +++ b/reviewer/git_service_runtime_linux.go @@ -0,0 +1,78 @@ +//go:build linux + +package reviewer + +import "context" + +// A bounded explicit catalog keeps older qualified releases available for their +// immutable durable jobs. No default release, provider, account or transport is +// selected. The native reviewer still verifies assets/containment before auth. +func newGitServiceNativeRuntime(catalog map[string]map[Provider]NativeVMOptions, gate func(context.Context, string) error) (func(context.Context, Job, *JobEvidence) (Engine, error), error) { + if gate == nil || len(catalog) == 0 || len(catalog) > 8 { + return nil, ErrLiveBlocked + } + releases := map[string]map[Provider]NativeVMOptions{} + for release, providers := range catalog { + if !objectID.MatchString(release) || len(providers) == 0 || len(providers) > 2 { + return nil, ErrLiveBlocked + } + releases[release] = map[Provider]NativeVMOptions{} + for provider, options := range providers { + if !provider.Valid() || options.Provider != provider || options.Reader != nil || nilGitHubCredentialTransport(options.Transport) { + return nil, ErrLiveBlocked + } + // Reuse the native module's structural checks without a filesystem, + // account or credential read. The real reader comes only from a job. + probe := options + probe.Reader = &Reader{} + if _, err := NewNativeVMReviewer(probe); err != nil { + return nil, err + } + options.Policy.Routes = append([]EgressRoute(nil), options.Policy.Routes...) + options.Policy.Headers = append([]string(nil), options.Policy.Headers...) + options.Runtime.BootstrapLibraries = append([]VMBootAsset(nil), options.Runtime.BootstrapLibraries...) + releases[release][provider] = options + } + } + return func(ctx context.Context, job Job, bound *JobEvidence) (Engine, error) { + if ctx == nil || ctx.Err() != nil || bound.validateJob(job) != nil { + return Engine{}, ErrLiveBlocked + } + providers, ok := releases[job.Release] + if !ok || gate(ctx, job.Release) != nil || ctx.Err() != nil { + return Engine{}, ErrLiveBlocked + } + engine := Engine{Reviewers: map[Provider]Reviewer{}, Reader: bound.Reader} + for _, provider := range job.Config.Providers() { + options, ok := providers[provider] + if !ok { + return Engine{}, ErrLiveBlocked + } + reader, err := bound.Reader(job.Snapshot) + if err != nil { + return Engine{}, err + } + options.Reader = reader + identity := job.Config.Codex + if provider == Claude { + identity = job.Config.Claude + } + providerCheck := options.Check + options.Check = func(ctx context.Context, session Session) error { + if ctx == nil || ctx.Err() != nil || session.Validate() != nil || session.JobID != job.ID || session.Snapshot != job.Snapshot || session.Packet != job.Packet || session.ConfigHash != job.Config.Hash() || session.Identity != identity || session.Round > job.Config.Rounds() { + return ErrLiveBlocked + } + if gate(ctx, job.Release) != nil || ctx.Err() != nil || providerCheck(ctx, session) != nil || ctx.Err() != nil { + return ErrLiveBlocked + } + return nil + } + worker, err := NewNativeVMReviewer(options) + if err != nil { + return Engine{}, err + } + engine.Reviewers[provider] = worker + } + return engine, nil + }, nil +} diff --git a/reviewer/git_service_runtime_linux_test.go b/reviewer/git_service_runtime_linux_test.go new file mode 100644 index 00000000..85610d82 --- /dev/null +++ b/reviewer/git_service_runtime_linux_test.go @@ -0,0 +1,196 @@ +//go:build linux + +package reviewer + +import ( + "context" + "errors" + "net/http" + "strings" + "sync/atomic" + "testing" +) + +func gitServiceNativeOptions(provider Provider, checks, calls *atomic.Int32) NativeVMOptions { + policy := egressTestPolicy(provider) + policy.ResponsePolicy = CodexSourceResponses + if provider == Claude { + policy.ResponsePolicy = ClaudeSourceMessages + } + return NativeVMOptions{ + Provider: provider, Root: "/unopened/" + string(provider), Stub: "/unopened/native-stub", StubHash: strings.Repeat("a", 64), + Auth: &AuthStore{provider: provider, directory: "/unopened/auth/" + string(provider)}, Policy: policy, + Transport: egressRoundTrip(func(*http.Request) (*http.Response, error) { calls.Add(1); return nil, ErrLiveBlocked }), + Check: func(context.Context, Session) error { checks.Add(1); return ErrLiveBlocked }, + } +} + +func gitServiceRuntimeJob(t *testing.T) (Job, *SnapshotEvidence, *JobEvidence) { + t.Helper() + git := newSnapshotGitFixture(t) + evidence, err := CaptureSnapshotEvidence(context.Background(), git.store(), git.snapshot) + if err != nil { + t.Fatal(err) + } + job := testJob() + job.Snapshot, job.Packet = git.snapshot, evidence.Packet() + bound, err := evidence.Bind(job) + if err != nil { + t.Fatal(err) + } + return job, evidence, bound +} + +func TestGitServiceNativeRuntimeUsesAuditedWorkersAndExactJobSource(t *testing.T) { + job, _, bound := gitServiceRuntimeJob(t) + var providerChecks, calls atomic.Int32 + gateCalls := 0 + providers := map[Provider]NativeVMOptions{Codex: gitServiceNativeOptions(Codex, &providerChecks, &calls), Claude: gitServiceNativeOptions(Claude, &providerChecks, &calls)} + build, err := newGitServiceNativeRuntime(map[string]map[Provider]NativeVMOptions{job.Release: providers}, func(_ context.Context, release string) error { + if release != job.Release { + t.Error("runtime qualified a different release") + } + gateCalls++ + return nil + }) + if err != nil { + t.Fatal(err) + } + engine, err := build(context.Background(), job, bound) + if err != nil || len(engine.Reviewers) != 2 { + t.Fatal(err) + } + limits, _ := protocolLimits(job) + readers := map[*Reader]bool{} + for _, provider := range job.Config.Providers() { + worker, ok := engine.Reviewers[provider].(*NativeVMReviewer) + if !ok { + t.Fatal("runtime did not compose the concrete native VM reviewer") + } + if _, ok := engine.Reviewers[provider].(AuditedReviewer); !ok { + t.Fatal("native source/provider evidence contract lost") + } + if worker.options.Reader.store != bound.evidence.store || worker.options.Reader.snapshot != job.Snapshot || readers[worker.options.Reader] { + t.Fatal("runtime reused a reader or changed source") + } + readers[worker.options.Reader] = true + session, _ := NewSession(job.ID, job.Snapshot, job.Config.Hash(), provider, 1, job.Packet, nil, nil, limits) + if _, _, _, err := worker.ReviewAudited(context.Background(), session); !errors.Is(err, ErrLiveBlocked) { + t.Fatal("closed native gate reached an unopened profile/runtime", err) + } + before := providerChecks.Load() + session.JobID = "another_valid_job" + if _, _, _, err := worker.ReviewAudited(context.Background(), session); !errors.Is(err, ErrLiveBlocked) || providerChecks.Load() != before { + t.Fatal("another job reached provider authority", err) + } + } + if calls.Load() != 0 || providerChecks.Load() != 2 || gateCalls != 3 { + t.Fatal("native runtime skipped gates or dispatched inference", calls.Load(), providerChecks.Load(), gateCalls) + } +} + +func TestGitServiceNativeRuntimeFreezesCatalogAndSelectsOlderRelease(t *testing.T) { + job, evidence, _ := gitServiceRuntimeJob(t) + var checks, calls atomic.Int32 + current := gitServiceNativeOptions(Codex, &checks, &calls) + old := current + old.Root = "/unopened/older-release" + oldRelease := strings.Repeat("e", 40) + catalog := map[string]map[Provider]NativeVMOptions{job.Release: {Codex: current}, oldRelease: {Codex: old}} + allowed := true + qualified := "" + build, err := newGitServiceNativeRuntime(catalog, func(_ context.Context, release string) error { + qualified = release + if !allowed { + return ErrLiveBlocked + } + return nil + }) + if err != nil { + t.Fatal(err) + } + // Mutating the caller's maps and slice backing must not change the + // trusted options already captured for durable older jobs. + catalog[oldRelease][Codex] = NativeVMOptions{} + current.Policy.Routes[0].Authority = "untrusted-changed-host" + delete(catalog, job.Release) + job.Release, job.Config.Mode = oldRelease, CodexOnly + bound, err := evidence.Bind(job) + if err != nil { + t.Fatal(err) + } + engine, err := build(context.Background(), job, bound) + if err != nil || len(engine.Reviewers) != 1 || qualified != oldRelease { + t.Fatal("older release was replaced or padded", err, qualified) + } + worker := engine.Reviewers[Codex].(*NativeVMReviewer) + if worker.options.Root != old.Root || worker.options.Policy.Routes[0].Authority == "untrusted-changed-host" { + t.Fatal("catalog mutation reached native options") + } + allowed = false + limits, _ := protocolLimits(job) + session, _ := NewSession(job.ID, job.Snapshot, job.Config.Hash(), Codex, 1, job.Packet, nil, nil, limits) + if _, _, _, err := worker.ReviewAudited(context.Background(), session); !errors.Is(err, ErrLiveBlocked) || checks.Load() != 0 || calls.Load() != 0 { + t.Fatal("revoked older release read auth or reached provider", err) + } + job.Release = strings.Repeat("f", 40) + bound, _ = evidence.Bind(job) + qualified = "" + if engine, err := build(context.Background(), job, bound); err == nil || len(engine.Reviewers) != 0 || qualified != "" { + t.Fatal("unknown release used a fallback runtime") + } +} + +func TestGitServiceNativeRuntimeRefusesPartialModeAndInvalidCatalog(t *testing.T) { + job, _, bound := gitServiceRuntimeJob(t) + var checks, calls atomic.Int32 + o := gitServiceNativeOptions(Codex, &checks, &calls) + build, err := newGitServiceNativeRuntime(map[string]map[Provider]NativeVMOptions{job.Release: {Codex: o}}, func(context.Context, string) error { return nil }) + if err != nil { + t.Fatal(err) + } + if engine, err := build(context.Background(), job, bound); err == nil || len(engine.Reviewers) != 0 || checks.Load() != 0 || calls.Load() != 0 { + t.Fatal("dual mode silently became single-provider") + } + for _, kind := range []string{"reader", "typed-nil-transport", "wrong-provider", "missing-provider-check"} { + t.Run(kind, func(t *testing.T) { + bad := o + switch kind { + case "reader": + bad.Reader = &Reader{} + case "typed-nil-transport": + var transport *http.Transport + bad.Transport = transport + case "wrong-provider": + bad.Provider = Claude + case "missing-provider-check": + bad.Check = nil + } + if f, err := newGitServiceNativeRuntime(map[string]map[Provider]NativeVMOptions{job.Release: {Codex: bad}}, func(context.Context, string) error { return nil }); err == nil || f != nil { + t.Fatal("invalid native release capability accepted") + } + }) + } +} + +func TestGitReviewerServiceComposesNativeCatalogWithoutAuthOrInference(t *testing.T) { + o, _, reader, publisher, signs := gitServiceFixture(t) + var checks, calls atomic.Int32 + o.Runtime = nil + o.NativeReleases = map[string]map[Provider]NativeVMOptions{o.Release: {Codex: gitServiceNativeOptions(Codex, &checks, &calls), Claude: gitServiceNativeOptions(Claude, &checks, &calls)}} + s, err := newGitReviewerService(context.Background(), o) + if err != nil { + t.Fatal(err) + } + defer s.Close(context.Background()) + job, _, bound := gitServiceRuntimeJob(t) + engine, err := s.controller.options.Runtime(context.Background(), job, bound) + if err != nil || len(engine.Reviewers) != 2 { + t.Fatal("concrete service did not compose its native catalog", err) + } + rCalls, _ := credentialCounts(reader) + pCalls, _ := credentialCounts(publisher) + if signs.Load() != 0 || rCalls != 0 || pCalls != 0 || checks.Load() != 0 || calls.Load() != 0 { + t.Fatal("native composition read credentials or ran providers") + } +}