From 5cda543a6ebbe44d3a6bac2dd3de100c98b0d46d Mon Sep 17 00:00:00 2001 From: Lebedev Nikita Date: Mon, 26 Oct 2026 21:11:54 +0400 Subject: [PATCH 1/2] fix: preserve matching lab authority lookup errors --- docs/guest-desktop.md | 7 ++ internal/app/console_lab.go | 24 ++++-- internal/app/console_lab_active.go | 25 ++++-- internal/app/console_lab_guest_test.go | 75 +++++++++++++++-- internal/app/console_lab_lookup_test.go | 105 ++++++++++++++++++++++++ 5 files changed, 219 insertions(+), 17 deletions(-) create mode 100644 internal/app/console_lab_lookup_test.go diff --git a/docs/guest-desktop.md b/docs/guest-desktop.md index 5a5d87c..d2695b8 100644 --- a/docs/guest-desktop.md +++ b/docs/guest-desktop.md @@ -64,6 +64,13 @@ when the same VM is enrolled again. Other VMs, host administration, enrollment c configuration, and approval issuance receive no authority from this mode. Without `--persistent-vm`, the existing bounded console/desktop grant behavior is unchanged. +If verification of a matching stored grant fails, discovery reports the lookup error and +dispatch remains blocked. A provider or protected-state read failure does not require a renewed +operator grant. Inspect the reported cause and current grant status after recovery. An earlier +denial remains cached under its original retry key; reconcile that receipt before using a new +key for a confirmed undispatched operation. Invalid, revoked, inactive, or replaced-enrollment +grants still confer no authority. + ## Agent interaction loop 1. Call `desktop_observe`. It returns a PNG content block, frame identity, native and diff --git a/internal/app/console_lab.go b/internal/app/console_lab.go index e08c208..e2a1834 100644 --- a/internal/app/console_lab.go +++ b/internal/app/console_lab.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "errors" + "fmt" "os" "time" @@ -94,7 +95,10 @@ func (s *ConsoleService) labIdentity(ctx context.Context, reference string) (Tar return resolution, "", err } identity, err := s.labEnrollment(ctx, domain.MachineRef(resolution.Locator.String())) - if err != nil || !validLabDigest(identity) { + if err != nil { + return resolution, "", fmt.Errorf("read lab enrollment identity: %w", err) + } + if !validLabDigest(identity) { return resolution, "", ErrInvalidConsoleLabGrant } return resolution, identity, nil @@ -123,7 +127,11 @@ func (s *ConsoleService) resolveActiveLabGrant(ctx context.Context, grant Consol if err != nil { return TargetResolution{}, err } - return resolution, s.labActivation(ctx, grant.GrantID) + err = s.labActivation(ctx, grant.GrantID) + if errors.Is(err, os.ErrNotExist) { + return resolution, ErrInvalidConsoleLabGrant + } + return resolution, err } func (s *ConsoleService) resolveLabBinding(ctx context.Context, grant ConsoleLabGrant) (TargetResolution, error) { @@ -136,7 +144,10 @@ func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, gr return TargetResolution{}, ErrInvalidConsoleLabGrant } revoked, err := s.labRevoked(ctx, grant.GrantID) - if err != nil || revoked { + if err != nil { + return TargetResolution{}, fmt.Errorf("read lab revocation: %w", err) + } + if revoked { return TargetResolution{}, ErrInvalidConsoleLabGrant } var resolution TargetResolution @@ -149,11 +160,14 @@ func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, gr return TargetResolution{}, ErrInvalidConsoleLabGrant } identity, err = s.labEnrollment(ctx, domain.MachineRef(resolution.Locator.String())) - if !validLabDigest(identity) { + if err == nil && !validLabDigest(identity) { return TargetResolution{}, ErrInvalidConsoleLabGrant } } - if err != nil || resolution.Locator.String() != string(grant.Target) || identity != grant.EnrollmentIdentity { + if err != nil { + return TargetResolution{}, fmt.Errorf("resolve lab enrollment: %w", err) + } + if resolution.Locator.String() != string(grant.Target) || identity != grant.EnrollmentIdentity { return TargetResolution{}, ErrInvalidConsoleLabGrant } return resolution, s.requireGrantSafety(ctx, resolution, grant.Mode, grant.AcknowledgeExternalEffects) diff --git a/internal/app/console_lab_active.go b/internal/app/console_lab_active.go index 7f6be05..a221061 100644 --- a/internal/app/console_lab_active.go +++ b/internal/app/console_lab_active.go @@ -3,6 +3,7 @@ package app import ( "context" "errors" + "fmt" "io" "os" "strings" @@ -68,12 +69,16 @@ func (s *ConsoleService) selectActiveLabGrant(ctx context.Context, actor domain. continue } grant, err := s.loadLabGrant(ctx, id) - if err != nil || (persistentOnly && grant.Mode != ConsoleLabModePersistentVM) || grant.Beneficiary != actor.EffectiveActor || grant.Target != canonical || s.requireActiveLabGrant(ctx, grant) != nil { + if err != nil || (persistentOnly && grant.Mode != ConsoleLabModePersistentVM) || grant.Beneficiary != actor.EffectiveActor || grant.Target != canonical { continue } - if selected.GrantID == "" || grant.IssuedAt.After(selected.IssuedAt) || (grant.IssuedAt.Equal(selected.IssuedAt) && grant.GrantID > selected.GrantID) { - selected = grant + if err := s.requireActiveLabGrant(ctx, grant); err != nil { + if errors.Is(err, ErrInvalidConsoleLabGrant) { + continue + } + return ConsoleLabGrant{}, fmt.Errorf("validate matching lab authority: %w", err) } + selected = newestLabGrant(selected, grant) } return selected, nil } @@ -89,9 +94,7 @@ func (s *ConsoleService) scanActiveLabGrants(ctx context.Context, file *os.File, if err != nil { return ConsoleLabGrant{}, err } - if candidate.GrantID != "" && (selected.GrantID == "" || candidate.IssuedAt.After(selected.IssuedAt) || (candidate.IssuedAt.Equal(selected.IssuedAt) && candidate.GrantID > selected.GrantID)) { - selected = candidate - } + selected = newestLabGrant(selected, candidate) if errors.Is(readErr, io.EOF) { break } @@ -99,3 +102,13 @@ func (s *ConsoleService) scanActiveLabGrants(ctx context.Context, file *os.File, return selected, nil } + +func newestLabGrant(selected, candidate ConsoleLabGrant) ConsoleLabGrant { + if candidate.GrantID == "" { + return selected + } + if selected.GrantID == "" || candidate.IssuedAt.After(selected.IssuedAt) || (candidate.IssuedAt.Equal(selected.IssuedAt) && candidate.GrantID > selected.GrantID) { + return candidate + } + return selected +} diff --git a/internal/app/console_lab_guest_test.go b/internal/app/console_lab_guest_test.go index c3a8d83..427aeb4 100644 --- a/internal/app/console_lab_guest_test.go +++ b/internal/app/console_lab_guest_test.go @@ -1,14 +1,17 @@ package app import ( + "bytes" "context" - "github.com/Horcag/agent-machine-control/internal/domain" - "github.com/Horcag/agent-machine-control/internal/statedir" - "github.com/Horcag/agent-machine-control/internal/target" "path/filepath" "strings" "testing" "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" ) func TestPersistentVMGrantCoordinatesUncontainedGuestAndSurvivesRestart(t *testing.T) { @@ -23,17 +26,77 @@ func TestPersistentVMGrantCoordinatesUncontainedGuestAndSurvivesRestart(t *testi } // A new service must discover protected on-disk authority without a console backend. coordinator.labAuthority = NewConsoleService(nil, authority.recovery, authority.target, authority.framesDir, WithConsoleLabEnrollmentIdentity(authority.labEnrollment)) - if result, err := h.service.Execute(t.Context(), h.params("prepare", "persistent-prepare"), nil, nil); err != nil || result.Receipt == nil || h.transport.executed != 1 { - t.Fatalf("persistent guest: %+v %v effects=%d", result, err, h.transport.executed) + if !grant.ExpiresAt.IsZero() { + t.Fatal("persistent guest authority acquired a TTL") + } + var artifact GuestArtifact + for i, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + t.Run(operation, func(t *testing.T) { + artifact = exercisePersistentGuestOperation(t, h, operation, artifact) + if h.transport.executed != i+1 { + t.Fatalf("guest dispatched %d times; expected %d", h.transport.executed, i+1) + } + }) + if t.Failed() { + t.FailNow() + } } if _, err := authority.RevokeLabGrant(t.Context(), operator, ConsoleLabGrantRevokeRequest{GrantID: grant.GrantID, Reason: "end guest lab", IdempotencyKey: "revoke-guest", Deadline: time.Now().Add(time.Minute).Format(time.RFC3339Nano)}); err != nil { t.Fatal(err) } - if _, err := h.service.Execute(t.Context(), h.params("prepare", "revoked-prepare"), nil, nil); err == nil || h.transport.executed != 1 { + if _, err := h.service.Execute(t.Context(), h.params("prepare", "revoked-prepare"), nil, nil); err == nil || h.transport.executed != 5 { t.Fatalf("revoked guest dispatched: %v effects=%d", err, h.transport.executed) } } +func exercisePersistentGuestOperation(t *testing.T, h *guestHarness, operation string, artifact GuestArtifact) GuestArtifact { + t.Helper() + p := h.params(operation, "persistent-"+operation) + var output bytes.Buffer + payload := stream.BytesInput([]byte("data")) + sink := stream.BufferOutput(&output) + fixtures := map[string]struct { + size int64 + sha string + artifacts []string + maxOutput int64 + input stream.ContextReader + output stream.ContextWriter + }{ + "prepare": {}, + "put": {size: 4, sha: guestSHA("data"), input: payload}, + "get": {artifacts: []string{artifact.ID}, maxOutput: 10, output: sink}, + "exec": {size: 4, sha: guestSHA("data"), maxOutput: 10, input: payload, output: sink}, + "remove-owned": {artifacts: []string{artifact.ID}}, + } + fixture := fixtures[operation] + p.Size, p.SHA256, p.ArtifactIDs, p.MaxOutput = fixture.size, fixture.sha, fixture.artifacts, fixture.maxOutput + result, err := h.service.Execute(t.Context(), p, fixture.input, fixture.output) + if err != nil || result.Receipt == nil || result.Receipt.Outcome.Status != domain.OutcomeSuccess { + t.Fatalf("persistent guest: %+v %v", result, err) + } + switch operation { + case "put": + if len(result.Artifacts) != 1 { + t.Fatalf("put artifacts: %+v", result.Artifacts) + } + return result.Artifacts[0] + case "get": + if output.String() != "data" || result.SHA256 != artifact.SHA256 { + t.Fatalf("get payload: %+v %q", result, output.String()) + } + case "exec": + if output.String() != "ok" { + t.Fatalf("exec output: %q", output.String()) + } + case "remove-owned": + if len(result.Removed) != 1 || !result.Removed[0].Absent || !result.Removed[0].Accepted { + t.Fatalf("remove outcome: %+v", result.Removed) + } + } + return artifact +} + func persistentGuestAuthority(t *testing.T, h *guestHarness) (*ConsoleService, domain.ActorContext) { t.Helper() coordinator := h.service.session diff --git a/internal/app/console_lab_lookup_test.go b/internal/app/console_lab_lookup_test.go new file mode 100644 index 0000000..40997cd --- /dev/null +++ b/internal/app/console_lab_lookup_test.go @@ -0,0 +1,105 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestPersistentGuestGrantReportsSecondInspectionFailureAndRecovers(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + authority, operator := persistentGuestAuthority(t, h) + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "exercise provider recovery", IdempotencyKey: "lookup-grant", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + h.service.session.labAuthority = authority + resolution := h.service.resolver.(guestTestResolver).resolution + observation := targetObservation(t, domain.LocalHostID, resolution.ProviderVMID, "synthetic-lab") + lookupErr := &os.PathError{Op: "inspect", Path: "synthetic-provider", Err: os.ErrNotExist} + observer := &exactTargetObserver{} + observer.inspect = func(context.Context, string) (domain.MachineObservation, error) { + if observer.calls == 2 { + return domain.MachineObservation{}, lookupErr + } + return observation, nil + } + authority.target.observer = observer + _, err = h.service.Execute(t.Context(), h.params("prepare", "failed-lookup"), nil, nil) + if !errors.Is(err, lookupErr) || observer.calls != 2 || h.transport.prepared != 0 || h.transport.executed != 0 { + t.Fatalf("lookup cause lost or dispatched: error=%v queries=%d prepare=%d execute=%d", err, observer.calls, h.transport.prepared, h.transport.executed) + } + result, err := h.service.Execute(t.Context(), h.params("prepare", "recovered-lookup"), nil, nil) + if err != nil || result.Receipt == nil || h.transport.executed != 1 { + t.Fatalf("recovery required new authority: %+v %v effects=%d", result, err, h.transport.executed) + } + status, err := authority.ActiveLabGrant(t.Context(), h.actor, "default") + if err != nil || status.Grant.GrantID != grant.GrantID || !status.Grant.ExpiresAt.IsZero() { + t.Fatalf("persistent authority changed: %+v %v", status, err) + } +} + +func TestPersistentGrantDiscoveryReportsEnrollmentReadFailure(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + lookupErr := errors.New("synthetic enrollment read failure") + original := authority.labEnrollment + authority.labEnrollment = func(context.Context, domain.MachineRef) (string, error) { return "", lookupErr } + status, err := authority.ActiveLabGrant(t.Context(), op.Actor, "default") + if !errors.Is(err, lookupErr) || status.State == "active" { + t.Fatalf("enrollment cause lost: %+v %v", status, err) + } + authority.labEnrollment = original + assertRecoveredLabGrant(t, authority, op.Actor, grant) +} + +func TestPersistentGrantDiscoveryRejectsInvalidProtectedDocuments(t *testing.T) { + for _, suffix := range []string{".revoked", ".active"} { + t.Run(suffix, func(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + restore := replaceLabDocumentWithDirectory(t, authority, grant, suffix) + status, err := authority.ActiveLabGrant(t.Context(), op.Actor, "default") + if err != nil || status.State != "disabled" { + t.Fatalf("invalid protected document conferred authority: %+v %v", status, err) + } + restore() + assertRecoveredLabGrant(t, authority, op.Actor, grant) + }) + } +} + +func replaceLabDocumentWithDirectory(t *testing.T, authority *ConsoleService, grant ConsoleLabGrant, suffix string) func() { + t.Helper() + path := filepath.Join(authority.labDirectory(), grant.GrantID+suffix) + if suffix == ".active" { + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + } + if err := os.Mkdir(path, 0700); err != nil { + t.Fatal(err) + } + return func() { + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if suffix == ".active" { + if err := authority.activateLabGrant(t.Context(), grant.GrantID); err != nil { + t.Fatal(err) + } + } + } +} + +func assertRecoveredLabGrant(t *testing.T, authority *ConsoleService, actor domain.ActorContext, grant ConsoleLabGrant) { + t.Helper() + status, err := authority.ActiveLabGrant(t.Context(), actor, "default") + if err != nil || status.State != "active" || status.Grant.GrantID != grant.GrantID { + t.Fatalf("verification recovery required renewal: %+v %v", status, err) + } +} From 45998041ce403dc4042d9b575a7292ada3f6930e Mon Sep 17 00:00:00 2001 From: Lebedev Nikita Date: Mon, 26 Oct 2026 23:11:51 +0400 Subject: [PATCH 2/2] test(daemon): improve retirement fixture deadline diagnostics --- .../daemon/guest_retirement_loader_test.go | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/internal/daemon/guest_retirement_loader_test.go b/internal/daemon/guest_retirement_loader_test.go index 5dfb33b..1acfb7d 100644 --- a/internal/daemon/guest_retirement_loader_test.go +++ b/internal/daemon/guest_retirement_loader_test.go @@ -5,7 +5,10 @@ import ( "context" "encoding/binary" "encoding/json" + "io" + "os" "os/exec" + "path/filepath" "strings" "testing" "time" @@ -28,7 +31,7 @@ func runRetirementLoader(t *testing.T, source string, input []byte) ([]byte, err if err != nil { t.Skip("PowerShell unavailable for local loader semantics") } - ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + ctx, cancel := context.WithTimeout(t.Context(), 90*time.Second) defer cancel() command := guestRetirementCommand(source) if len(utf16.Encode([]rune(command))) > 7900 { @@ -36,8 +39,34 @@ func runRetirementLoader(t *testing.T, source string, input []byte) ([]byte, err } token := strings.TrimPrefix(command, "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ") process := exec.CommandContext(ctx, pwsh, "-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand", token) + stagePath := filepath.Join(t.TempDir(), "retirement-stage.txt") + process.Env = append(os.Environ(), "AMC_RETIREMENT_TEST_STAGE="+stagePath) process.Stdin = bytes.NewReader(input) - return process.CombinedOutput() + started := time.Now() + output, err := process.CombinedOutput() + t.Logf("retirement fixture: stage=%s elapsed=%s context=%v", retirementLoaderStage(stagePath), time.Since(started).Round(time.Millisecond), ctx.Err()) + return output, err +} + +func retirementLoaderStage(path string) string { + file, err := os.Open(path) + if err != nil { + if os.IsNotExist(err) { + return "not-entered" + } + return "unavailable" + } + defer file.Close() + data, err := io.ReadAll(io.LimitReader(file, 32)) + if err != nil { + return "unavailable" + } + switch stage := string(data); stage { + case "entered", "add-type-start", "add-type-done", "inventory-done": + return stage + default: + return "invalid" + } } func TestGuestRetirementLoaderPreservesFullInteger(t *testing.T) { @@ -87,8 +116,13 @@ func TestGuestRetirementNativeInventoryFailsClosed(t *testing.T) { t.Fatal("native probe terminator missing") } // Compile the actual embedded class, then invoke only its pure census decision. - source := "Add-Type -TypeDefinition @'\n" + strings.TrimSpace(native) + "\n'@\n" + - "$ids=[uint32[]]@(1,2,0);if([AMCOriginalProcessProbe]::InventoryMissing(1,$ids,8)){throw 'present_pid_absent'};if(-not[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,8)){throw 'missing_pid_not_observed'};$refused=0;foreach($size in @(0,1,12,16)){try{$null=[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,[uint32]$size);throw 'invalid_census_accepted'}catch{if($_.Exception.Message-eq'invalid_census_accepted'){throw};$refused++}};@{refused=$refused}|ConvertTo-Json -Compress" + source := "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'entered')\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'add-type-start')\n" + + "Add-Type -TypeDefinition @'\n" + strings.TrimSpace(native) + "\n'@\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'add-type-done')\n" + + "$ids=[uint32[]]@(1,2,0);if([AMCOriginalProcessProbe]::InventoryMissing(1,$ids,8)){throw 'present_pid_absent'};if(-not[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,8)){throw 'missing_pid_not_observed'};$refused=0;foreach($size in @(0,1,12,16)){try{$null=[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,[uint32]$size);throw 'invalid_census_accepted'}catch{if($_.Exception.Message-eq'invalid_census_accepted'){throw};$refused++}}\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'inventory-done')\n" + + "@{refused=$refused}|ConvertTo-Json -Compress" output, err := runRetirementLoader(t, source, retirementLoaderPayload([]byte(`{}`), []byte(source))) if err != nil { t.Fatalf("native inventory decision: %v %s", err, output)