Repository navigation
workspace: add WorkspaceManager snapshot pre-warming and status lifecycle - #592
Conversation
…ycle Part of google#563 - pkg/apis/v1alpha1: add WorkspacePhase, WorkspaceCondition, and WorkspaceReason constants. - internal/substrate: add SuspendActorAndWaitForSnapshot (DURABILITY_MODE_SYNCHRONOUS), retry codes.Aborted in EnsureActorEgressPolicy, include codes.Aborted in IsTransientError, and populate completed TagStatus.Snapshot in substratetest. - internal/store: add UpdateWorkspaceStatus across Store, MemoryStore, and PostgresStore. - internal/controller: add WorkspaceManager (Prepare, EnsureSnapshotTag with per-tag lock coalescing and fresh setup actor retries, Delete prefix cleanup), NeedsPrewarmSnapshot, and SetupCoordinator interface. - internal/server, cmd: wire WorkspaceManager into Server and ax-server, guard CreateTask against Preparing/Failed Workspaces, and surface PHASE in ax get workspaces.
I quickly compared the two prs, the good thing is that I don't think we have too many conflicts and I can easily have a follow up pr to connect #591.
And #591 can be too big to review, as I already extracted the workspace specific part out, can I focus on this one to remove some noise from #591? |
…s-setup- prefix - Align SetupActorPrefix to "ax-ws-setup-" so the runner control plane can route temporary setup actor claims and events distinctly from Task actors. - Split SetupCoordinator into RegisterSetup (invoked after EnsureActor with actorUID and before ResumeActor to eliminate the boot race), AwaitSetup, and DeleteSetup (for multi-replica store-backed coordination). - Populate default actor Metadata.Uid in substratetest.CreateActor and verify pre-resume registration and cleanup in controller tests.
|
|
||
| // Step 3: Cold miss (or incomplete stranded Tag) — acquire a per-Tag | ||
| // distributed lock so concurrent requests coalesce onto one setup actor. | ||
| unlock, err := m.locker.Lock(ctx, "workspace-tag", atespace, tagName) |
There was a problem hiding this comment.
this holds lock only for the ws tag, while DeleteWorkspace holds lock for the ws. They are not mutually exclusive. DeleteWorkspace could delete the tag while some lazy task is holding the tag lock.
There was a problem hiding this comment.
Good catch, if both holds the "workspace" lock, we will prevent the task with customized profile to start in parallel. So I think a better and most correct way is to have the RLock to allow parallel code start. I will fix it in the follow up pr.
Also added the todo in the code and documented it as a known gap.
- Wire --default-task-image / AX_DEFAULT_TASK_IMAGE through WorkspaceManagerOptions into ResolveProfileTemplate so omitted profile.image falls back to the server default task image. - Move SetupActorPrefix to internal/apivalidation and reject Task names starting with ax-ws-setup- in ValidateTask. - Persist WorkspaceStatus in prepareWorkspace with an independent context.WithoutCancel(ctx) timeout and map context.Canceled to codes.Canceled. - Assign Metadata.Uid in defaultMetadata and key snapshot Tag and setup actor names by 16-hex SHA-256(uid), eliminating name truncation and cross-generation collisions on delete-and-recreate.
- Unexport needsPrewarmSnapshot and test it through WorkspaceManager.Prepare. - Move ensureProfileTemplate to internal/controller/profile.go for sharing with TaskReconciler. - Check existing Workspace in saveOrLoadWorkspace before resolving spec.setup.gateway so idempotent re-applies of Ready Workspaces succeed even if the setup gateway was deleted.
- Unexport needsPrewarmSnapshot and test it through WorkspaceManager.Prepare. - Move ensureProfileTemplate to internal/controller/profile.go for sharing with TaskReconciler. - Check existing Workspace in saveOrLoadWorkspace before resolving spec.setup.gateway so idempotent re-applies of Ready Workspaces succeed even if the setup gateway was deleted.
2b8d5bf to
557784a
Compare
yongruilin
left a comment
There was a problem hiding this comment.
2 findings I feel critical.
|
/lgtm |
… is on (#607) ax-server rejects spec.profile on a Workspace as not supported yet until workspace pre-warming is enabled (#592), so Step 2 of the hero demo failed at ax apply in live mode. Leave a TODO to add the profile back. Verification: ./hack/hero-demo/run.sh # prints the scoreboard, exit 0 workspace.yaml, task-starter.yaml and task-custom.yaml decode with the strict v1alpha1 YAML decoder and pass ValidateWorkspace/ValidateTask and ax-server's unimplemented-field checks; main's workspace.yaml fails the latter with spec.profile.
- Drop queue, lease, sweep, and timer types (Action, ActionKind, SuspendReason, NextReconcileAt, ErrLockTimeout, reconciler/sweep inputs) so this phase reuses AX's synchronous Substrate lifecycle path (google#515, google#468, google#592). - Extract server.storedStatus into store.StoredStatus and use it in TaskRow.SetLifecycleState so stored TaskStatus.state covers only AX-owned states while live actor state continues to be read from Substrate. - Track Runnable, ClaimNonce, RunnerToken*, ActiveRunID, RunSeq, and LastAckedRunnerSeq on lifecycle.State for pull-based runner assignment and event fencing. Signed-off-by: siyuanfoundation <[email protected]>
- Drop queue, lease, sweep, and timer types (Action, ActionKind, SuspendReason, NextReconcileAt, ErrLockTimeout, reconciler/sweep inputs) so this phase reuses AX's synchronous Substrate lifecycle path (google#515, google#468, google#592). - Extract server.storedStatus into store.StoredStatus and use it in TaskRow.SetLifecycleState so stored TaskStatus.state covers only AX-owned states while live actor state continues to be read from Substrate. - Track Runnable, ClaimNonce, RunnerToken*, ActiveRunID, RunSeq, and LastAckedRunnerSeq on lifecycle.State for pull-based runner assignment and event fencing. Signed-off-by: siyuanfoundation <[email protected]>
* store, lifecycle: add TaskRow, TaskApplier, and lifecycle state/action contracts Establish the minimal shared seam between the Task Reconciliation Loop and the Task Runner + ax-server workstreams: - Add transactional TaskApplier (TaskKey, TaskRow, DeleteTaskRow sentinel, ApplyFunc, LifecycleState / SetLifecycleState, ScrubStoredTask) and ErrLockTimeout in internal/store. - Add pure Task lifecycle contract types (ActionKind, SuspendReason, Action, State, sealed Input variants, Output, TransitionFunc, IsTerminal), Transition contract stub, and NextReconcileAt in internal/lifecycle. - Sync AGENTS.md and skills/substrate/SKILL.md. * store, lifecycle: address review comments on contracts and tests - Clarify on CreateInput that CreateTask always initializes new tasks in TASK_STATE_QUEUED (starting directly in TASK_STATE_SUSPENDED is unsupported). - Guard nil receiver in (*TaskRow).SetLifecycleState and document Task.Status initialization in ApplyFunc. - Guard non-zero ClaimDeadline in NextReconcileAt so a zero time cannot schedule a QUEUED task as permanently overdue. - Expand lifecycle and store unit tests for past NotBefore, zero ClaimDeadline, terminal SUSPEND_ACTOR_SYNC, LastResumedAt, and nil-field/nil-receiver isolation. Signed-off-by: siyuanfoundation <[email protected]> * lifecycle: remove MaxQueueWaitDuration and Transition stub - Drop MaxQueueWaitDuration so RESOURCE_EXHAUSTED backs off RESUME_ACTOR while keeping the task in TASK_STATE_QUEUED rather than failing after 30m (#468). - Remove the ErrUnimplemented Transition stub (transition.go) so the state-machine PR adds Transition alongside its real implementation; preserve pure-transition contract invariants on TransitionFunc. Signed-off-by: siyuanfoundation <[email protected]> * store, lifecycle: trim seam to minimal sync-path runner contract - Drop queue, lease, sweep, and timer types (Action, ActionKind, SuspendReason, NextReconcileAt, ErrLockTimeout, reconciler/sweep inputs) so this phase reuses AX's synchronous Substrate lifecycle path (#515, #468, #592). - Extract server.storedStatus into store.StoredStatus and use it in TaskRow.SetLifecycleState so stored TaskStatus.state covers only AX-owned states while live actor state continues to be read from Substrate. - Track Runnable, ClaimNonce, RunnerToken*, ActiveRunID, RunSeq, and LastAckedRunnerSeq on lifecycle.State for pull-based runner assignment and event fencing. Signed-off-by: siyuanfoundation <[email protected]> * store: consolidate runner seam onto TaskRow and defer internal/lifecycle - Move runner control fields directly onto store.TaskRow and remove internal/lifecycle from this seam so the runner + ax-server PR introduces internal/lifecycle alongside its real Transition implementation and tests. Signed-off-by: siyuanfoundation <[email protected]> --------- Signed-off-by: siyuanfoundation <[email protected]>
* store, lifecycle: add TaskRow, TaskApplier, and lifecycle state/action contracts Establish the minimal shared seam between the Task Reconciliation Loop and the Task Runner + ax-server workstreams: - Add transactional TaskApplier (TaskKey, TaskRow, DeleteTaskRow sentinel, ApplyFunc, LifecycleState / SetLifecycleState, ScrubStoredTask) and ErrLockTimeout in internal/store. - Add pure Task lifecycle contract types (ActionKind, SuspendReason, Action, State, sealed Input variants, Output, TransitionFunc, IsTerminal), Transition contract stub, and NextReconcileAt in internal/lifecycle. - Sync AGENTS.md and skills/substrate/SKILL.md. * store, lifecycle: address review comments on contracts and tests - Clarify on CreateInput that CreateTask always initializes new tasks in TASK_STATE_QUEUED (starting directly in TASK_STATE_SUSPENDED is unsupported). - Guard nil receiver in (*TaskRow).SetLifecycleState and document Task.Status initialization in ApplyFunc. - Guard non-zero ClaimDeadline in NextReconcileAt so a zero time cannot schedule a QUEUED task as permanently overdue. - Expand lifecycle and store unit tests for past NotBefore, zero ClaimDeadline, terminal SUSPEND_ACTOR_SYNC, LastResumedAt, and nil-field/nil-receiver isolation. Signed-off-by: siyuanfoundation <[email protected]> * lifecycle: remove MaxQueueWaitDuration and Transition stub - Drop MaxQueueWaitDuration so RESOURCE_EXHAUSTED backs off RESUME_ACTOR while keeping the task in TASK_STATE_QUEUED rather than failing after 30m (#468). - Remove the ErrUnimplemented Transition stub (transition.go) so the state-machine PR adds Transition alongside its real implementation; preserve pure-transition contract invariants on TransitionFunc. Signed-off-by: siyuanfoundation <[email protected]> * store, lifecycle: trim seam to minimal sync-path runner contract - Drop queue, lease, sweep, and timer types (Action, ActionKind, SuspendReason, NextReconcileAt, ErrLockTimeout, reconciler/sweep inputs) so this phase reuses AX's synchronous Substrate lifecycle path (#515, #468, #592). - Extract server.storedStatus into store.StoredStatus and use it in TaskRow.SetLifecycleState so stored TaskStatus.state covers only AX-owned states while live actor state continues to be read from Substrate. - Track Runnable, ClaimNonce, RunnerToken*, ActiveRunID, RunSeq, and LastAckedRunnerSeq on lifecycle.State for pull-based runner assignment and event fencing. Signed-off-by: siyuanfoundation <[email protected]> * store: consolidate runner seam onto TaskRow and defer internal/lifecycle - Move runner control fields directly onto store.TaskRow and remove internal/lifecycle from this seam so the runner + ax-server PR introduces internal/lifecycle alongside its real Transition implementation and tests. Signed-off-by: siyuanfoundation <[email protected]> * runner, lifecycle: add runner.v1 wire protocol and pure task/runner transitions --------- Signed-off-by: siyuanfoundation <[email protected]> Co-authored-by: siyuanfoundation <[email protected]>
Part of #584
Summary
Adds the control-plane
WorkspaceManager, store status persistence, synchronous actor snapshotting, and gRPC server / CLI wiring for Workspace snapshot pre-warming and lazy snapshot Tag caching, gated onSetupCoordinatoruntil the pull-based runner channel is wired intoax-server.Key Changes
pkg/apis/v1alpha1: AddWorkspacePhase(Preparing,Ready,Failed),WorkspaceCondition(SetupComplete,SnapshotReady), andWorkspaceReasonconstants.internal/substrate:SuspendActorAndWaitForSnapshot(DURABILITY_MODE_SYNCHRONOUS) soCreateTagruns only after the setup actor's durable snapshot is recorded.codes.AbortedinEnsureActorEgressPolicyand includecodes.AbortedinIsTransientError.TagStatus.Snapshotinsubstratetest.CreateTag.internal/store: AddUpdateWorkspaceStatusacrossStore,MemoryStore, andPostgresStore.internal/controller:WorkspaceManager(Prepare,EnsureSnapshotTagwith per-tag distributed lock coalescing, incomplete-tag recovery, and fresh-actor bounded retries, plusDeleteprefix cleanup for default + lazy tags and stranded setup actors).NeedsPrewarmSnapshot(skipping setup actors for empty or pure-MCP workspaces) and theSetupCoordinatorinterface.internal/server&cmd:WorkspaceManagerintoServer(CreateWorkspace/DeleteWorkspace) andcmd/ax-server, preservingcodes.Unimplementedonspec.profileandspec.setup.gatewaywhileSetupCoordinatoris nil.CreateTaskwithcodes.FailedPreconditionwhen the bound Workspace is inPreparingorFailed.PHASEcolumn toax get workspacesand reportconfiguredwhen re-applying aPreparingWorkspace.No breaking runtime behavior change in this PR: SetupCoordinator is not yet wired in cmd/ax-server/main.go (PrewarmEnabled() == false), so CreateWorkspace continues to reject spec.profile and spec.setup.gateway with codes.Unimplemented and skips setup actor provisioning for existing Workspaces.
Next pr will wire production SetupCoordinator over the pull-based runner channel and wire TaskReconciler to resolve (Workspace, EffectiveProfile) via WorkspaceManager.EnsureSnapshotTag and boot Task actors from the snapshot Tag once the channel and task reconciler changes are ready.
Known gap (fix it follow up pr):
DeleteWorkspace and EnsureSnapshotTag are not mutually exclusively as they are holding different mutex locks.
#592 (comment)