Skip to content
73 changes: 54 additions & 19 deletions cli/commands/root/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -207,9 +208,9 @@ func deployShardImages(
// record the instance IDs handed to WaitForShardsReady and return canned
// ready/expected counts.
type shardRoller interface {
shardReadinessWaiter
PruneOrphanedShards(ctx context.Context) error
Deploy(ctx context.Context, opts local.DeployOpts) error
WaitForShardsReady(ctx context.Context, instanceIDs []string, onProgress func(ready, expected int)) (int, int)
}

// rollShards recreates the targeted shards' containers, one dashboard row per shard.
Expand Down Expand Up @@ -248,34 +249,63 @@ func rollShards(
if waitReady {
// Deploy recreates every instance in each targeted pool, so wait for the whole pool.
waitInstanceIDs := targets.poolInstances(opts.ShardIDs)
return waitForShardsReadyResult(ctx, sess, rt, waitInstanceIDs)
}

var ready, expected int
waitForShardsReadyRow(sess, func(onProgress func(ready, expected int)) {
ready, expected = rt.WaitForShardsReady(ctx, waitInstanceIDs, onProgress)
})
return nil
}

if ready < expected {
// Returned, not just shown: the row is already Failed, but the summary and the
// exit code are what a script or an agent reads, and plain progress has no rows.
return &shardsNotReadyError{ready: ready, expected: expected}
}
// shardReadinessWaiter is the subset of local.Runtime the readiness wait uses;
// *local.Runtime satisfies it.
type shardReadinessWaiter interface {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: embed shardReadinessWaiter in shardRoller so the identical WaitForShardsReady signature has one declaration to maintain.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 0d21203. shardRoller now embeds shardReadinessWaiter, so the WaitForShardsReady signature has one declaration.

WaitForShardsReady(
ctx context.Context,
instanceIDs []string,
onProgress func(ready, expected int),
) (ready, expected int)
}

// waitForShardsReadyResult waits for the targeted instances to serve and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this comment can just state the invariant: the waiter returns counts without an error, cancellation stays cancellation, and caller deadlines remain readiness failures. The branch-by-branch explanation below repeats the implementation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8b119d9. Trimmed the comment to the invariant: the waiter returns counts without an error, cancellation stays cancellation (passed through for phasebox to silence at the section boundary), and caller deadlines remain readiness failures.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: keep the counts-only cancellation invariant, but trim the repeated explanation and the interface's testability narration. The row comment below can likewise keep cancellation precedence and the section-lifecycle constraint without walking through the switch.

Also, the no-progress guard now handles a documented cancellation path, so calling it defensive is misleading.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 318e645. Trimmed the waitForShardsReadyResult comment to the counts-only/cancellation-stays-cancellation/deadline-stays-failure invariant and dropped the interface testability narration. The row comment keeps cancellation precedence and the section-lifecycle constraint without walking the switch, and the no-progress guard comment no longer calls it "defensive" (it handles a documented cancellation path).

// classifies the outcome: the waiter returns counts without an error,
// cancellation returns [context.Canceled], and incomplete counts return
// shardsNotReadyError (complete counts still succeed with an expired

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a case with complete counts and an expired caller deadline? This comment promises success for that combination, but the current deadline test only covers incomplete counts. It would catch a future blanket ctx.Err() check that changes this distinction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 1e10f00.

// deadline).
func waitForShardsReadyResult(
ctx context.Context, sess phasebox.Session, waiter shardReadinessWaiter, instanceIDs []string,
) error {
var ready, expected int
waitForShardsReadyRow(ctx, sess, func(onProgress func(ready, expected int)) {
ready, expected = waiter.WaitForShardsReady(ctx, instanceIDs, onProgress)
})

if errors.Is(ctx.Err(), context.Canceled) {
return ctx.Err()
}

if ready < expected {
// Returned, not just shown: the row, if any, is already Failed, but the
// summary and the exit code are what a script or an agent reads, and plain
// progress has no rows.
return &shardsNotReadyError{ready: ready, expected: expected}
}

return nil
}

// waitForShardsReadyRow drives the "ready" progress row through WaitForShardsReady
// to a terminal state. waitForReady performs the actual wait and invokes
// onProgress for every poll tick (local.Runtime.WaitForShardsReady). The row is
// set Active on every tick, then closed to Done when ready >= expected or Failed
// ("timed out …") otherwise. The progressed guard is defensive: WaitForShardsReady
// always ticks at least once because its counter starts below zero, so it only
// protects a future waitForReady that could return without reporting anything.
// to a terminal state. Cancellation takes precedence even when the counts are
// complete: a Ctrl+C landed after the last probe round is a cancel, not a silent
// success. The progressed guard handles cancellation before the first completed
// probe round, where WaitForShardsReady returns (0, expected) without invoking
// onProgress — the row stays absent rather than rendering a terminal claim with
// no evidence.
//
// Must run before the enclosing section finishes: phasebox drops upserts to a
// finished section (see Model.upsert), so a terminal update after dash.Run
// returns would be silently lost and the row would keep rendering its spinner.
func waitForShardsReadyRow(sess phasebox.Session, waitForReady func(onProgress func(ready, expected int))) {
func waitForShardsReadyRow(
ctx context.Context, sess phasebox.Session, waitForReady func(onProgress func(ready, expected int)),
) {
var lastReady, expected int

progressed := false
Expand All @@ -284,19 +314,24 @@ func waitForShardsReadyRow(sess phasebox.Session, waitForReady func(onProgress f
lastReady, expected = ready, exp
progressed = true

sess.UpsertRow("ready", "Waiting for shards ready", fmt.Sprintf("%d/%d", ready, exp), phasebox.Active)
sess.UpsertRow("ready", "Waiting for shards to be ready", fmt.Sprintf("%d/%d", ready, exp), phasebox.Active)
})

if !progressed {
return
}

if errors.Is(ctx.Err(), context.Canceled) {
sess.Fail("ready", "Waiting for shards to be ready", ctx.Err())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canceled readiness row may never reach the terminal: phasebox gives Bubble Tea this same context, and Program.Send can discard messages once it's canceled. Plain mode also drops rows.

Could we attach TTY output showing this row changing to canceled? The fake-session test proves dispatch, but can't establish that the update renders.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: the row reaching the terminal in dashboard mode — the PR body's "How did you verify?" section already documents the end-to-end dashboard capture (Ctrl+C during world reload readiness wait: box shows "canceled", no "0/N instances are serving" message). At the unit level, TestWaitForShardsReadyResult_CancelThroughPlainSectionIsCanceled drives a real phasebox.Plain section and asserts the captured stdout contains "canceled" (reload_internal_test.go:495). The sess.Fail upsert happens inside dash.Run's section callback, before the section ends (reload.go:349-351), so phasebox's finished-section guard does not drop it. A fresh TTY recording can't be attached from this environment without a live Docker world; the dashboard-mode Program.Send-while-canceled race is phasebox's contract (cli/internal/tui/component/phasebox), outside this PR's diff.

return
}

state, detail := phasebox.Done, fmt.Sprintf("%d/%d", lastReady, expected)
if lastReady < expected {
state, detail = phasebox.Failed, fmt.Sprintf("timed out (%d/%d ready)", lastReady, expected)
}

sess.UpsertRow("ready", "Waiting for shards ready", detail, state)
sess.UpsertRow("ready", "Waiting for shards to be ready", detail, state)
}

// shardsNotReadyError reports instances that were deployed but never answered. It is its
Expand Down
Loading
Loading