From db343ac052d584df5d01278f91c8219e12ce9ddc Mon Sep 17 00:00:00 2001 From: Anthony Alaribe Date: Thu, 22 Oct 2026 16:42:27 +0200 Subject: [PATCH] Resume host-owned agent VMs from leased prepared checkpoints Every host-owned role now leases its pinned checkpoint in-process, resumes the agent VM from the private copy, fetches only the pinned revision, and replays regressions against the same baseline. A configured dependency archive without a prepared hit fails before model work. The npm spec now proves the agent VM resumed by reading installed dependencies in the guest, replacing a child stderr event. --- shared/src/Data/Effectful/AgentWorkspace.hs | 11 +++++-- src/Data/Effectful/CodeReview.hs | 17 +++++++---- src/Data/Effectful/CodeReview/Guest.hs | 32 ++++++++++++++++----- test/integration/Pages/ImpactReviewsSpec.hs | 12 ++++---- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/shared/src/Data/Effectful/AgentWorkspace.hs b/shared/src/Data/Effectful/AgentWorkspace.hs index b08e3083d..7c856f7af 100644 --- a/shared/src/Data/Effectful/AgentWorkspace.hs +++ b/shared/src/Data/Effectful/AgentWorkspace.hs @@ -1,4 +1,4 @@ -module Data.Effectful.AgentWorkspace (Workspace, Options (..), PruneOptions (..), PruneReport, pruneBudgetMet, pruneWorkspaces, Operation (..), Prepared, Leased, RegressionOptions (..), RegressionRequest, RegressionResult (..), RegressionReceipt, RegressionReport, RegressionError (..), verifyRegression, regressionLogs, regressionErrorText, runWorkspace, withWorkspace, runRegression, runWorkspaceReal, runWorkspaceTest, RPC, withSandbox, Command (..), Sandbox (..), Isolation (..), GuestCommand (..), Copy (..), LoggedCommand (..), Overflow (..), Execution (..), LogExit (..), Image (..), ImageId (..), SandboxFailure (..), agentCommand, publish) where +module Data.Effectful.AgentWorkspace (Workspace, Options (..), PruneOptions (..), PruneReport, pruneBudgetMet, pruneWorkspaces, Operation (..), Prepared, Leased (..), Request, Baseline, leaseWorkspace, leasedBaseline, RegressionOptions (..), RegressionRequest, RegressionResult (..), RegressionReceipt, RegressionReport, RegressionError (..), verifyRegression, regressionLogs, regressionErrorText, runWorkspace, withWorkspace, runRegression, runWorkspaceReal, runWorkspaceTest, RPC, withSandbox, Command (..), Sandbox (..), Isolation (..), GuestCommand (..), Copy (..), LoggedCommand (..), Overflow (..), Execution (..), LogExit (..), Image (..), ImageId (..), SandboxFailure (..), agentCommand, publish) where import Conduit (runConduit, (.|)) import Control.Monad (foldM) @@ -283,12 +283,17 @@ instance AE.FromJSON Artifact where data Snapshot = Snapshot {path :: FilePath, sha256 :: Fingerprint} deriving stock (Generic) - deriving anyclass (AE.FromJSON) + deriving anyclass (AE.FromJSON, AE.ToJSON) data Baseline = Pristine | PreparedCheckpoint Snapshot deriving stock (Generic) - deriving anyclass (AE.FromJSON) + deriving anyclass (AE.FromJSON, AE.ToJSON) + + +-- | Verifiers replay against the same immutable checkpoint the leased agent VM resumed from. +leasedBaseline :: Maybe Leased -> Baseline +leasedBaseline = maybe Pristine \leased -> PreparedCheckpoint $ Snapshot leased.prepared.path leased.prepared.manifest.sha256 data Side = Base | Head diff --git a/src/Data/Effectful/CodeReview.hs b/src/Data/Effectful/CodeReview.hs index 33b9b4963..9a8b4f8e2 100644 --- a/src/Data/Effectful/CodeReview.hs +++ b/src/Data/Effectful/CodeReview.hs @@ -89,7 +89,7 @@ makeEffect ''CodeReview -- | A role whose VM the host owns: who is billed for it, the image it must use, how its guest is prepared from -- recorded evidence, and the fields the agent wraps around its submission. -data Ownership = Ownership {host :: Guest.Host, attempt :: UUIDId "agent_attempt", image :: Maybe (UUIDId "guest_image"), setup :: Map Text ByteString -> Guest.Setup, envelope :: Maybe AE.Value} +data Ownership = Ownership {host :: Guest.Host, attempt :: UUIDId "agent_attempt", image :: Maybe (UUIDId "guest_image"), setup :: Map Text ByteString -> Guest.Setup, envelope :: Maybe AE.Value, lease :: Maybe Guest.Lease} data Task = AdvisoryTask (Request "pr_review") | FixTask FixRequest | AdmissionTask AdmissionRequest | PeerTask FixReviewRequest @@ -203,15 +203,20 @@ runCodeReviewReal artifactStore host executable skills = interpret \env -> \case role = case scope of Outbox.Advisory _ -> Policy.Advisory; Outbox.IssueFix _ -> Policy.Fix; Outbox.Admission _ -> Policy.Admission; Outbox.PeerReview _ -> Policy.Peer -- Roles migrate to a host-owned VM one at a time; the rest still create theirs in the child. owned = host >>= \configured -> case task of - AdvisoryTask request | isNothing configured.workspace -> Just $ Ownership configured request.attemptId Nothing (const Guest.Explore) Nothing - AdmissionTask request -> Just $ Ownership configured request.fix.request.attemptId (Just request.fix.imageBuildId) (const $ Guest.InspectAdmission request.artifact) (Just $ AE.object ["artifact" AE..= request.artifact]) - PeerTask request -> Just $ Ownership configured request.fix.request.attemptId (Just request.fix.imageBuildId) Guest.ReviewEvidence (Just $ AE.object ["result" AE..= request.artifacts.result]) + AdvisoryTask request -> Just $ Ownership configured request.attemptId Nothing (const Guest.Explore) Nothing (checkpoint request Nothing) + AdmissionTask request -> Just $ Ownership configured request.fix.request.attemptId (Just request.fix.imageBuildId) (const $ Guest.InspectAdmission request.artifact) (Just $ AE.object ["artifact" AE..= request.artifact]) (checkpoint request.fix.request $ Just request.fix.imageBuildId) + PeerTask request -> Just $ Ownership configured request.fix.request.attemptId (Just request.fix.imageBuildId) Guest.ReviewEvidence (Just $ AE.object ["result" AE..= request.artifacts.result]) (checkpoint request.fix.request $ Just request.fix.imageBuildId) _ -> Nothing + -- Prepared checkpoints are keyed by the pinned project, repository and base, never by the reviewed head. + checkpoint :: Request run -> Maybe (UUIDId "guest_image") -> Maybe Guest.Lease + checkpoint request pinned = do + store <- artifactStore + Guest.Lease store.cacheDir <$> AET.parseMaybe AE.parseJSON (AE.object ["projectId" AE..= request.projectId, "repository" AE..= request.repository, "base" AE..= request.base, "expectedImageBuildId" AE..= pinned, "expectedCheckpoint" AE..= AE.Null, "ownerPid" AE..= AE.Null]) honours invocation = invocation.name `elem` Policy.toolsForRole role && null (Schema.validateJSON mempty (Policy.toolSchema invocation.name) invocation.arguments) -- Fresh verifier VMs replay the frozen artifact; their receipts are already journaled beside the outbox. regress session checkout outbox (advisory :: Request "pr_review") arguments = do let options = session.verifier (Outbox.outboxPath outbox "checks") - case AE.fromJSON @Workspace.RegressionRequest $ AE.object ["projectId" AE..= advisory.projectId, "runId" AE..= advisory.runId, "attemptId" AE..= advisory.attemptId, "sessionId" AE..= verifierSession, "base" AE..= advisory.base, "imageBuildId" AE..= session.imageBuildId, "workspace" AE..= AE.object ["tag" AE..= ("Pristine" :: Text)], "artifact" AE..= (arguments :: AE.Value), "revisions" AE..= ([("base", advisory.base), ("head", advisory.head)] :: [(Text, Text)]), "ownerPid" AE..= AE.Null] of + case AE.fromJSON @Workspace.RegressionRequest $ AE.object ["projectId" AE..= advisory.projectId, "runId" AE..= advisory.runId, "attemptId" AE..= advisory.attemptId, "sessionId" AE..= verifierSession, "base" AE..= advisory.base, "imageBuildId" AE..= session.imageBuildId, "workspace" AE..= session.baseline, "artifact" AE..= (arguments :: AE.Value), "revisions" AE..= ([("base", advisory.base), ("head", advisory.head)] :: [(Text, Text)]), "ownerPid" AE..= AE.Null] of AE.Error _ -> pure $ Left "Regression arguments do not match the host contract" AE.Success regression -> tryAny (Workspace.verifyRegression options regression) >>= \case @@ -275,7 +280,7 @@ runCodeReviewReal artifactStore host executable skills = interpret \env -> \case case owned of Nothing -> deliver checkout Just ownership -> - join <$> Guest.withGuest ownership.host ownership.image checkout.directory checkout.head (ownership.setup evidence) ownership.attempt (void . allocate journal) (\receipt -> for_ journal (`Outbox.journal` Outbox.Allocation receipt)) \session -> + join <$> Guest.withGuest ownership.host ownership.image checkout.directory checkout.head (ownership.setup evidence) ownership.lease ownership.attempt (void . allocate journal) (\receipt -> for_ journal (`Outbox.journal` Outbox.Allocation receipt)) \session -> writeIORef guest (Just session) >> deliver checkout{Checkout.guest = Just session.skills, Checkout.envelope = ownership.envelope} pure $ join prepared _ -> pure $ Left "Repository checkout was unavailable or already supplied" diff --git a/src/Data/Effectful/CodeReview/Guest.hs b/src/Data/Effectful/CodeReview/Guest.hs index d455310b2..1e5a5018d 100644 --- a/src/Data/Effectful/CodeReview/Guest.hs +++ b/src/Data/Effectful/CodeReview/Guest.hs @@ -1,9 +1,10 @@ -module Data.Effectful.CodeReview.Guest (Host (..), Workspace (..), Setup (..), Exec (..), Operation (..), Session (..), withGuest) where +module Data.Effectful.CodeReview.Guest (Host (..), Workspace (..), Lease (..), Setup (..), Exec (..), Operation (..), Session (..), withGuest) where import Data.Aeson qualified as AE import Data.Base64.Types (extractBase64) import Data.ByteString qualified as BS import Data.ByteString.Base64 qualified as Base64 +import Data.Effectful.AgentWorkspace qualified as AgentWorkspace import Data.Effectful.AgentWorkspace (Command (..), Copy (..), Execution (..), GuestCommand (..), Image (..), ImageId (..), Isolation (..), LogExit (..), LoggedCommand (..), Overflow (..), RegressionOptions (..), Sandbox (..), SandboxFailure (..), agentCommand, withSandbox) import Data.Effectful.CodeReview.Types (AllocationOutcome (..), AllocationReceipt, Revision, TestArtifact (..), agentResources, hostAllocation, revisionText) import Data.Map.Strict qualified as Map @@ -29,6 +30,10 @@ data Host = Host {image :: Text, bridge :: FilePath, skills :: [FilePath], works data Workspace = Workspace {script :: FilePath, dependencyArchive :: Maybe FilePath} +-- | Where this run's prepared checkpoint is cached, and which pinned project/repository/base it must match. +data Lease = Lease {cacheDir :: FilePath, request :: AgentWorkspace.Request} + + -- | Role-specific guest preparation that runs before the agent sees the workspace. data Setup = Explore | InspectAdmission TestArtifact | ReviewEvidence (Map Text ByteString) @@ -45,14 +50,14 @@ data Operation = Run Exec | Read Text | Access Text -- | A running agent VM: Pi's file and shell calls, root copies into the guest, and verifier settings for fresh VMs. -data Session = Session {skills :: FilePath, imageBuildId :: ImageId, operate :: Operation -> IO (Either Text AE.Value), copyIn :: FilePath -> Text -> IO (), verifier :: FilePath -> RegressionOptions} +data Session = Session {skills :: FilePath, imageBuildId :: ImageId, baseline :: AgentWorkspace.Baseline, operate :: Operation -> IO (Either Text AE.Value), copyIn :: FilePath -> Text -> IO (), verifier :: FilePath -> RegressionOptions} -- | Owns the agent VM for the action's lifetime and records its allocation however it ends: @allocate@ after a -- completed run, @retain@ (journal only) when interrupted. Preparation failures are reported before the action -- starts; the action's own exceptions propagate. -withGuest :: Host -> Maybe (UUIDId "guest_image") -> FilePath -> Revision -> Setup -> UUIDId "agent_attempt" -> (AllocationReceipt -> IO ()) -> (AllocationReceipt -> IO ()) -> (Session -> IO a) -> IO (Either Text a) -withGuest host expected checkout revision setup attempt allocate retain action = withSystemTempDirectory "monoscope-agent-guest" \temporary -> do +withGuest :: Host -> Maybe (UUIDId "guest_image") -> FilePath -> Revision -> Setup -> Maybe Lease -> UUIDId "agent_attempt" -> (AllocationReceipt -> IO ()) -> (AllocationReceipt -> IO ()) -> (Session -> IO a) -> IO (Either Text a) +withGuest host expected checkout revision setup lease attempt allocate retain action = withSystemTempDirectory "monoscope-agent-guest" \temporary -> withLease \leased -> do let skills = temporary "skills" logs = temporary "logs" traverse_ createDirectory [skills, logs] @@ -101,10 +106,20 @@ withGuest host expected checkout revision setup attempt allocate retain action = prepared <- tryAny do image <- rpc @Image $ Resolve host.image for_ expected \(UUIDId build) -> unless (image.buildId == ImageId build) $ failure "Configured guest image differs from the pinned image" - rpc @() $ Create $ uncurry (Sandbox image.assetDir) agentResources OfflineCow $ Map.fromList [("/checkout", checkout), ("/run/monoscope-skills", skills)] + let sandbox = uncurry (Sandbox image.assetDir) agentResources OfflineCow $ Map.fromList [("/checkout", checkout), ("/run/monoscope-skills", skills)] + case leased of + Just held -> rpc @() $ Resume (sandbox, held.resumePath) + Nothing | any (isJust . (.dependencyArchive)) host.workspace -> failure "Configured dependency workspace has not been prepared for these pinned inputs" + Nothing -> rpc @() $ Create sandbox rpc @Text (GetIdentity ()) >>= writeIORef vmIdentity . fmap UUIDId . UUID.fromText -- Gondolin mounts are root-only, so the agent account reads guest-owned copies. - traverse_ (run "/" . fromList) [["/bin/mkdir", "-p", "/workspace", "/skills", "/run/monoscope-review-output"], ["/bin/cp", "-R", "/checkout/.", "/workspace"], ["/bin/cp", "-R", "/run/monoscope-skills/.", "/skills"], ["/bin/chmod", "-R", "a+rX,go-w", "/skills"]] + traverse_ (run "/" . fromList) [["/bin/mkdir", "-p", "/workspace", "/skills", "/run/monoscope-review-output"], ["/bin/cp", "-R", "/run/monoscope-skills/.", "/skills"], ["/bin/chmod", "-R", "a+rX,go-w", "/skills"]] + -- A resumed checkpoint keeps its built tree and only fetches the pinned revision's objects. + if isJust leased + then do + for_ ["/workspace", "/checkout/.git"] \path -> run "/" $ "/usr/bin/git" :| ["config", "--system", "--add", "safe.directory", path] + void $ git ["fetch", "--quiet", "--no-tags", "--", "/checkout", revisionText revision] + else void $ run "/" $ "/bin/cp" :| ["-R", "/checkout/.", "/workspace"] void $ git ["checkout", "--quiet", "--detach", revisionText revision] void $ workspace ["/bin/chown", "-hR", "1000:1000", "/workspace"] account <- workspace $ toList $ agentCommand ["/usr/bin/id", "-u"] @@ -125,9 +140,12 @@ withGuest host expected checkout revision setup attempt allocate retain action = for_ (Map.toList files) \(destination, bytes) -> upload destination bytes >> workspace ["/bin/chmod", "0444", destination] pure image.buildId whenRight (Left "Agent sandbox could not be prepared") prepared \imageBuildId -> - Right <$> action (Session skills imageBuildId operate copyIn \artifacts -> RegressionOptions artifacts checkout host.image host.bridge) + Right <$> action (Session skills imageBuildId (AgentWorkspace.leasedBaseline leased) operate copyIn \artifacts -> RegressionOptions artifacts checkout host.image host.bridge) measured (case outcome of Left failed | Just SandboxCleanupFailed <- fromException failed -> ReleaseFailed; _ -> Released) >>= traverse_ (void . tryAny . allocate) either throwIO pure outcome where failure = throwIO . userError + withLease use = case (host.workspace, lease) of + (Just configured, Just held) -> AgentWorkspace.leaseWorkspace (AgentWorkspace.Options held.cacheDir host.image host.bridge configured.script configured.dependencyArchive) held.request use + _ -> use Nothing diff --git a/test/integration/Pages/ImpactReviewsSpec.hs b/test/integration/Pages/ImpactReviewsSpec.hs index a535569cc..581b6f973 100644 --- a/test/integration/Pages/ImpactReviewsSpec.hs +++ b/test/integration/Pages/ImpactReviewsSpec.hs @@ -1188,9 +1188,12 @@ spec = around withTestResources $ describe "Production impact reviews" do let link = directory "archive-link" createFileLink archive link isLeft <$> lookupPrepared options{Workspace.dependencyArchive = Just link} `shouldReturn` True + host <- sdkHost [] profile let next results = pure $ case length results of - 0 -> SdkTool "run_regression" (AE.object ["script" AE..= replay, "failureMessage" AE..= ("dependency echo lost" :: Text)]) - 1 -> SdkTool "submit_review" (AE.object ["findings" AE..= ([] :: [AE.Value]), "coverage" AE..= ([] :: [AE.Value])]) + -- Only the prepared checkpoint has installed dependencies; a pristine agent VM would not print this. + 0 -> SdkTool "bash" (AE.object ["command" AE..= ("test -d node_modules/monoscope-dependency-fixture && printf 'agent-%s\\n' resumed-checkpoint" :: Text)]) + 1 -> SdkTool "run_regression" (AE.object ["script" AE..= replay, "failureMessage" AE..= ("dependency echo lost" :: Text)]) + 2 -> SdkTool "submit_review" (AE.object ["findings" AE..= ([] :: [AE.Value]), "coverage" AE..= ([] :: [AE.Value])]) _ -> SdkText "Fixture finished." withSdkModel directory "dependency-fixture" next \models requests -> do let request = Agent.Request{projectId = testPid, runId = uid 1, attemptId = uid 2, repository = toText repository, base, head = headRevision, gitToken = "", prompt = "Check actual offline dependency behavior.", provider = "dependency-fixture", model = "fixture", thinkingLevel = Agent.Off} @@ -1198,7 +1201,7 @@ spec = around withTestResources $ describe "Production impact reviews" do checks <- newIORef ([] :: [Agent.CheckReceipt]) result <- runEff - $ Agent.runCodeReviewReal (Just store) Nothing (Just "test/fixtures/native-sdk-worker.py") [models, store.usageDir, store.cacheDir, profile, diagnostic, "--prepare", trustedScript, "--dependencies", archive, "--cli", cli] + $ Agent.runCodeReviewReal (Just store) (Just host{Guest.workspace = Just $ Guest.Workspace trustedScript (Just archive)}) (Just "test/fixtures/native-sdk-worker.py") [models, store.usageDir, store.cacheDir, "/nonexistent-guest-image", diagnostic, "--cli", cli] $ Agent.reviewRepository request{Agent.runId = uid n} (const $ pure $ Left "Unexpected query") (const $ pure $ Left "Unexpected knowledge") (const pass) (const pass) (const pass) (\receipt -> liftIO $ modifyIORef' checks (<> [receipt])) (const $ pure $ Right ()) (result,) <$> readIORef checks writeFileBS archive $ bytes <> "changed" @@ -1214,8 +1217,7 @@ spec = around withTestResources $ describe "Production impact reviews" do AE.eitherDecodeStrict @AE.Value (encodeUtf8 encoded) `shouldBe` Right (AE.object ["findings" AE..= ([] :: [AE.Value]), "coverage" AE..= ([] :: [AE.Value])]) fingerprint <- parse (field "sha256") manifest :: IO Text map (\receipt -> (receipt.side, receipt.status, AE.toJSON receipt.workspace)) checks `shouldBe` [(Agent.Base, Agent.Passed, AE.toJSON fingerprint), (Agent.Head, Agent.Failed, AE.toJSON fingerprint)] - events <- mapMaybe (AE.decodeStrict @AE.Value) . BSC.lines <$> readFileBS diagnostic - events `shouldSatisfy` any (\value -> AET.parseEither (field "type") value == Right ("workspace_cache" :: Text) && AET.parseEither (field "status") value == Right ("hit" :: Text) && AET.parseEither (field "sha256") value == Right fingerprint) + decodeUtf8 @Text . AE.encode <$> requests >>= (`shouldSatisfy` T.isInfixOf "agent-resumed-checkpoint") it "runs frozen regressions natively, preserves receipts and rejects weakened candidate tests" \_tr -> withSystemTempDirectory "native-regression" \temporary -> do directory <- canonicalizePath temporary