Merge branch 'main' into validation/web-transcript-selection
This commit is contained in:
@@ -44,20 +44,24 @@ class _PipelineDefinitionValidationState
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _load({bool retry = false}) {
|
void _load({bool retry = false}) {
|
||||||
if (widget.source.trim().isEmpty) {
|
final source = widget.source;
|
||||||
|
final gateway = widget.gateway;
|
||||||
|
if (source.trim().isEmpty) {
|
||||||
_result = Future.value({'type': 'validated'});
|
_result = Future.value({'type': 'validated'});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final cache = _checks[widget.gateway] ??= {};
|
final cache = _checks[gateway] ??= {};
|
||||||
if (retry) cache.remove(widget.source);
|
if (retry) cache.remove(source);
|
||||||
if (!cache.containsKey(widget.source) && cache.length >= 16) {
|
if (!cache.containsKey(source) && cache.length >= 16) {
|
||||||
cache.remove(cache.keys.first);
|
cache.remove(cache.keys.first);
|
||||||
}
|
}
|
||||||
_result = cache.putIfAbsent(
|
_result = cache.putIfAbsent(
|
||||||
widget.source,
|
source,
|
||||||
() => Future.sync(
|
// Primary summary sections enqueue their reads while mounting. The
|
||||||
() => widget.gateway.call('validate', {'source': widget.source}),
|
// transport serializes replies, so optional validation waits until those
|
||||||
),
|
// reads have been queued. A microtask adds no lingering timer on dispose.
|
||||||
|
() =>
|
||||||
|
Future.microtask(() => gateway.call('validate', {'source': source})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,3 +34,25 @@ The initial capture attempt used waitfor on a static heading; the kit does not
|
|||||||
index that static label, although the saved PNG visibly showed the warning.
|
index that static label, although the saved PNG visibly showed the warning.
|
||||||
Final captures use screenshots and a real Copy Input interaction instead.
|
Final captures use screenshots and a real Copy Input interaction instead.
|
||||||
No physical device or Safari claim is made here. Nothing was deployed.
|
No physical device or Safari claim is made here. Nothing was deployed.
|
||||||
|
|
||||||
|
## Summary request ordering follow-up
|
||||||
|
|
||||||
|
The integrated application test exposed a real ordering dependency: the shared
|
||||||
|
PipelinesClient serializes each request until its reply. Definition validation
|
||||||
|
was enqueued immediately while its widget mounted, before the schedule section
|
||||||
|
had enqueued `list_schedules`, so a delayed or unanswered optional validation
|
||||||
|
held up primary Summary content.
|
||||||
|
|
||||||
|
Validation now starts in a microtask after synchronous Summary mounting. This
|
||||||
|
lets the schedule request enter the existing transport queue first, without
|
||||||
|
adding a timer or changing the client's serialization contract. The selected
|
||||||
|
source and gateway are captured before deferral, so a later selection cannot
|
||||||
|
change a queued request's source. The application fixture answers the new
|
||||||
|
validation request and asserts that schedules and launch controls are available
|
||||||
|
while validation is still unanswered. No selection, backend or transport code
|
||||||
|
was changed. Focused application/definition-warning tests and analysis are
|
||||||
|
recorded in the follow-up handoff; no full suite or deployment was run here.
|
||||||
|
|
||||||
|
Follow-up result: 149 application/definition-warning tests pass through the
|
||||||
|
memory watchdog (`/tmp/memwatch/out.35VDe8`, peak 2105 MB, 49 s). Focused analysis
|
||||||
|
is clean (`flutter-analyze.NVN15m`).
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Artifact browser interaction verification — 2026-11-05
|
||||||
|
|
||||||
|
Verified existing HTML artifact behavior in an isolated Chrome fixture built
|
||||||
|
from the warning-display branch, using `tool/capture/`. No production chat/run
|
||||||
|
was changed and no artifact-viewer source changes were needed.
|
||||||
|
|
||||||
|
At 1100×900: opened run 7 → Artifacts → release-dashboard.html, entered Full
|
||||||
|
screen, scrolled to Section 8 and the bottom heading, returned to the top, then
|
||||||
|
panned horizontally to the table's RIGHT EDGE column. Inspected all PNGs under
|
||||||
|
`/private/tmp/artifact-fullscreen-check/` (inline, fullscreen, bottom, right edge).
|
||||||
|
|
||||||
|
At 560×1000: entered Full screen, clicked Zoom in, asserted the sandboxed frame
|
||||||
|
has a scale transform, clicked Actual size, asserted that transform is reset,
|
||||||
|
and exited fullscreen. Inspected all PNGs under
|
||||||
|
`/private/tmp/artifact-zoom-check/`. Both capture scripts completed and cleaned up
|
||||||
|
their browser/server processes. The listing capture in
|
||||||
|
`/private/tmp/artifact-interaction-check/` was also inspected.
|
||||||
|
|
||||||
|
This proves desktop Chrome wheel scrolling/panning and the viewer's zoom
|
||||||
|
controls, including a narrow viewport. It does not prove physical touch/pinch,
|
||||||
|
Safari link/clipboard policy, or native-device behavior. Web artifact zoom uses
|
||||||
|
the existing buttons: the sandboxed document does not receive native pinch zoom.
|
||||||
|
No Android devices were attached (`adb devices -l` returned an empty list).
|
||||||
|
A real Safari fixture rendered, but further native automation failed with
|
||||||
|
`cgWindowNotFound`; Safari interaction acceptance remains unverified. The owned
|
||||||
|
loopback fixture server was terminated and awaited; no personal Fold was used.
|
||||||
@@ -93,10 +93,19 @@ void main() {
|
|||||||
await tester.tap(find.byKey(const Key('pipeline-row-1')));
|
await tester.tap(find.byKey(const Key('pipeline-row-1')));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
await replyPipelineDetail(tester, channel, fixture);
|
await replyPipelineDetail(tester, channel, fixture);
|
||||||
|
// Optional validation must not get ahead of the Summary's primary read.
|
||||||
|
expect(requestsOf(channel, 'validate'), isEmpty);
|
||||||
await reply(tester, channel, 'list_schedules', 1, {
|
await reply(tester, channel, 'list_schedules', 1, {
|
||||||
'type': 'schedules',
|
'type': 'schedules',
|
||||||
'schedules': <dynamic>[],
|
'schedules': <dynamic>[],
|
||||||
});
|
});
|
||||||
|
await waitForCount(tester, channel, 'validate', 1);
|
||||||
|
// Schedule content and launch controls remain available while the optional
|
||||||
|
// validation response is still outstanding.
|
||||||
|
expect(find.text('Loading schedules…'), findsNothing);
|
||||||
|
expect(find.byKey(runPipelineActionKey), findsOneWidget);
|
||||||
|
expect(find.text('Checking definition…'), findsOneWidget);
|
||||||
|
await reply(tester, channel, 'validate', 1, {'type': 'validated'});
|
||||||
|
|
||||||
await tester.tap(find.byKey(runPipelineActionKey));
|
await tester.tap(find.byKey(runPipelineActionKey));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|||||||
@@ -82,7 +82,8 @@ changes Codex settings. Helpers are scoped by database path and upstream socket,
|
|||||||
with mode-0600 Unix sockets and singleton locks under the mode-0700 directory
|
with mode-0600 Unix sockets and singleton locks under the mode-0700 directory
|
||||||
`<user cache dir>/pipelines/codex-hosts/<database hash>/`.
|
`<user cache dir>/pipelines/codex-hosts/<database hash>/`.
|
||||||
|
|
||||||
On reconnect the helper returns its cached initialization handshake and replays
|
The helper completes upstream initialize -> initialized exactly once before
|
||||||
|
accepting clients. On reconnect it returns the cached initialization result and replays
|
||||||
only unanswered server requests, preserving exact callback IDs and parameters.
|
only unanswered server requests, preserving exact callback IDs and parameters.
|
||||||
Responses and `serverRequest/resolved` remove pending entries. Old/new client RPC
|
Responses and `serverRequest/resolved` remove pending entries. Old/new client RPC
|
||||||
IDs are remapped so a delayed response for an old Pipelines process cannot answer
|
IDs are remapped so a delayed response for an old Pipelines process cannot answer
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# Budget accounting reads under CPU load
|
||||||
|
|
||||||
|
Mobile run 249 stopped before Code edits because a usage GET's two-second
|
||||||
|
context expired while its OpenCode CLI subprocess was starting under load. This
|
||||||
|
was an accounting transport deadline, not evidence that the configured token cap
|
||||||
|
had been reached. The accounting failure correctly failed closed, but one short
|
||||||
|
startup deadline was enough to reject a healthy stage.
|
||||||
|
|
||||||
|
The independent usage-read path now allows ten seconds per attempt and one
|
||||||
|
retry after 100 ms, only when the failure is a context deadline or a transport
|
||||||
|
error reporting Timeout(). Parent cancellation stops the current read or retry
|
||||||
|
backoff. Non-timeout transport failures are not retried. This is read-only
|
||||||
|
accounting retry; no session creation, generation, prompt or tool operation is
|
||||||
|
replayed. Initial, polling and final usage reads use the same policy.
|
||||||
|
|
||||||
|
A successful read still passes through the existing identity/shape/known-usage
|
||||||
|
checks and accumulated cap enforcement. Missing or malformed accounting, a
|
||||||
|
mismatched session, and exceeded caps are not retried or bypassed. Two persistent
|
||||||
|
timeouts still produce ErrBudgetAccounting and cancel work. Final accounting
|
||||||
|
retains its cancellation-independent context and must still report valid usage;
|
||||||
|
its failure is joined to the stage error. The finite transport grace can delay a
|
||||||
|
poll's accounting failure by at most approximately 20.1 seconds, or the parent's
|
||||||
|
shorter deadline; it does not change either configured cap or final totals.
|
||||||
|
|
||||||
|
Focused race tests use injected millisecond read timeouts. They cover transient
|
||||||
|
context/wrapped/network timeouts, independent retry contexts, one-retry bounds,
|
||||||
|
permanent failure and cancellation, cancelled backoff, initial/final retries,
|
||||||
|
malformed/missing/mismatched accounting, persistent polling failure, and final
|
||||||
|
cap enforcement. Existing live-agent no-replay and accounting-ledger tests are
|
||||||
|
included. No live service, run, workflow definition or budget cap was changed.
|
||||||
|
|
||||||
|
Validation: `go test -race ./internal/stage` passes (53.952s), as do the focused
|
||||||
|
race regression set, `go vet ./internal/stage`, gofmt and diff checks.
|
||||||
@@ -47,3 +47,9 @@ The initial deployment cannot rescue callbacks already owned by the old direct
|
|||||||
connection; pending interactions must settle before that first replacement.
|
connection; pending interactions must settle before that first replacement.
|
||||||
A helper crash, upstream App Server restart, or expiry breaks callback identity
|
A helper crash, upstream App Server restart, or expiry breaks callback identity
|
||||||
and remains an explicit limitation. No disk reconstruction is claimed.
|
and remains an explicit limitation. No disk reconstruction is claimed.
|
||||||
|
|
||||||
|
Handshake follow-up: the bridge now sends the upstream `initialized` notification
|
||||||
|
once after a successful initialize response and before declaring readiness.
|
||||||
|
Downstream initialized notifications remain suppressed on every attach. A real
|
||||||
|
socket fixture verifies initialize -> initialized ordering and exactly one of
|
||||||
|
each across client replacement, and the adapter integration checks both counts.
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ func TestHostedDetachRetainsAnswerableInteractions(t *testing.T) {
|
|||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
var upstream *websocket.Conn
|
var upstream *websocket.Conn
|
||||||
var initialized atomic.Int32
|
var initialized atomic.Int32
|
||||||
|
var handshakeCompleted atomic.Int32
|
||||||
responses := make(chan rpcMessage, 8)
|
responses := make(chan rpcMessage, 8)
|
||||||
readyUpstream := make(chan struct{})
|
readyUpstream := make(chan struct{})
|
||||||
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -63,6 +64,10 @@ func TestHostedDetachRetainsAnswerableInteractions(t *testing.T) {
|
|||||||
if msg.Method == "initialize" {
|
if msg.Method == "initialize" {
|
||||||
initialized.Add(1)
|
initialized.Add(1)
|
||||||
}
|
}
|
||||||
|
if msg.Method == "initialized" {
|
||||||
|
handshakeCompleted.Add(1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
err = wsjson.Write(ctx, conn, map[string]any{"id": msg.ID, "result": map[string]any{}})
|
err = wsjson.Write(ctx, conn, map[string]any{"id": msg.ID, "result": map[string]any{}})
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
@@ -161,8 +166,8 @@ func TestHostedDetachRetainsAnswerableInteractions(t *testing.T) {
|
|||||||
if !seen["7"] || !seen["8"] {
|
if !seen["7"] || !seen["8"] {
|
||||||
t.Fatalf("callback identities changed: %+v", seen)
|
t.Fatalf("callback identities changed: %+v", seen)
|
||||||
}
|
}
|
||||||
if initialized.Load() != 1 {
|
if initialized.Load() != 1 || handshakeCompleted.Load() != 1 {
|
||||||
t.Fatalf("upstream initialized %d times", initialized.Load())
|
t.Fatalf("upstream handshake counts: initialize=%d initialized=%d", initialized.Load(), handshakeCompleted.Load())
|
||||||
}
|
}
|
||||||
if err := after.Answer(ctx, protocol.ChatAnswerRequest{SessionID: "thread-a", FormID: "question", Answers: map[string]any{"q": "no"}}); err == nil {
|
if err := after.Answer(ctx, protocol.ChatAnswerRequest{SessionID: "thread-a", FormID: "question", Answers: map[string]any{"q": "no"}}); err == nil {
|
||||||
t.Fatal("question was answered twice")
|
t.Fatal("question was answered twice")
|
||||||
|
|||||||
@@ -117,6 +117,11 @@ func Serve(ctx context.Context, options Options) error {
|
|||||||
if failure := h.initialize["error"]; len(failure) > 0 && string(failure) != "null" {
|
if failure := h.initialize["error"]; len(failure) > 0 && string(failure) != "null" {
|
||||||
return errors.New("Codex App Server refused initialization")
|
return errors.New("Codex App Server refused initialization")
|
||||||
}
|
}
|
||||||
|
// Complete the upstream handshake once before accepting Pipelines clients.
|
||||||
|
// Reattached clients only initialize their downstream connection.
|
||||||
|
if err := h.write(upstream, message{"method": json.RawMessage(`"initialized"`)}); err != nil {
|
||||||
|
return fmt.Errorf("complete Codex App Server initialization: %w", err)
|
||||||
|
}
|
||||||
server := &http.Server{Handler: http.HandlerFunc(h.accept), ReadHeaderTimeout: 5 * time.Second}
|
server := &http.Server{Handler: http.HandlerFunc(h.accept), ReadHeaderTimeout: 5 * time.Second}
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
go func() { <-child.Done(); _ = server.Close() }()
|
go func() { <-child.Done(); _ = server.Close() }()
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type fakeServer struct {
|
type fakeServer struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
received chan message
|
received chan message
|
||||||
ready chan struct{}
|
handshakes chan string
|
||||||
|
ready chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan error) {
|
func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan error) {
|
||||||
@@ -31,7 +32,7 @@ func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan err
|
|||||||
t.Cleanup(func() { os.RemoveAll(dir) })
|
t.Cleanup(func() { os.RemoveAll(dir) })
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
t.Cleanup(cancel)
|
t.Cleanup(cancel)
|
||||||
fake := &fakeServer{ctx: ctx, received: make(chan message, 64), ready: make(chan struct{})}
|
fake := &fakeServer{ctx: ctx, received: make(chan message, 64), handshakes: make(chan string, 8), ready: make(chan struct{})}
|
||||||
listener, err := net.Listen("unix", filepath.Join(dir, "upstream.sock"))
|
listener, err := net.Listen("unix", filepath.Join(dir, "upstream.sock"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -58,7 +59,10 @@ func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan err
|
|||||||
var method string
|
var method string
|
||||||
_ = json.Unmarshal(msg["method"], &method)
|
_ = json.Unmarshal(msg["method"], &method)
|
||||||
if method == "initialize" {
|
if method == "initialize" {
|
||||||
|
fake.handshakes <- method
|
||||||
fake.send(t, message{"id": msg["id"], "result": json.RawMessage(`{"userAgent":"fixture"}`)})
|
fake.send(t, message{"id": msg["id"], "result": json.RawMessage(`{"userAgent":"fixture"}`)})
|
||||||
|
} else if method == "initialized" {
|
||||||
|
fake.handshakes <- method
|
||||||
} else {
|
} else {
|
||||||
select {
|
select {
|
||||||
case fake.received <- msg:
|
case fake.received <- msg:
|
||||||
@@ -239,3 +243,32 @@ func TestResolvedNotificationClearsOnlyMatchingThread(t *testing.T) {
|
|||||||
t.Fatalf("resolved callback replayed: %s", got)
|
t.Fatalf("resolved callback replayed: %s", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBridgeCompletesUpstreamHandshakeExactlyOnce(t *testing.T) {
|
||||||
|
fake, socket, _, _ := testHost(t)
|
||||||
|
first := attach(t, socket)
|
||||||
|
writeClient(t, first, `{"method":"initialized"}`)
|
||||||
|
writeClient(t, first, `{"method":"initialized"}`)
|
||||||
|
first.CloseNow()
|
||||||
|
second := attach(t, socket)
|
||||||
|
writeClient(t, second, `{"method":"initialized"}`)
|
||||||
|
writeClient(t, second, `{"id":2,"method":"barrier"}`)
|
||||||
|
if got := fake.next(t); method(got) != "barrier" {
|
||||||
|
t.Fatalf("unexpected upstream call: %s", got)
|
||||||
|
}
|
||||||
|
for _, want := range []string{"initialize", "initialized"} {
|
||||||
|
select {
|
||||||
|
case got := <-fake.handshakes:
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("handshake order: got %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatalf("missing upstream %s", want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case got := <-fake.handshakes:
|
||||||
|
t.Fatalf("upstream handshake repeated after downstream reconnect: %s", got)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ func TestEnsureSpawnsReusableDetachedHost(t *testing.T) {
|
|||||||
if wsjson.Read(ctx, conn, &msg) != nil {
|
if wsjson.Read(ctx, conn, &msg) != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if method(msg) == "initialized" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err := wsjson.Write(ctx, conn, message{"id": msg["id"], "result": json.RawMessage(`{}`)}); err != nil {
|
if err := wsjson.Write(ctx, conn, message{"id": msg["id"], "result": json.RawMessage(`{}`)}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -266,15 +266,12 @@ func (c OpenCodeClient) watchBudget(ctx context.Context, budget *Budget, id stri
|
|||||||
reader.Operations = nil
|
reader.Operations = nil
|
||||||
w := &budgetWatch{stop: make(chan struct{}), done: make(chan struct{})}
|
w := &budgetWatch{stop: make(chan struct{}), done: make(chan struct{})}
|
||||||
w.read = func(parent context.Context, final bool) error {
|
w.read = func(parent context.Context, final bool) error {
|
||||||
readCtx, cancel := context.WithTimeout(parent, 2*time.Second)
|
data, err := readBudgetUsage(parent, reader.budgetReadPolicy, func(readCtx context.Context) ([]byte, error) {
|
||||||
defer cancel()
|
if reader.connection != nil && reader.Backend == "v1" {
|
||||||
var data []byte
|
return reader.connection.BudgetMessages(readCtx, id)
|
||||||
var err error
|
}
|
||||||
if reader.connection != nil && reader.Backend == "v1" {
|
return reader.call(readCtx, "get", "/api/session/"+id, nil)
|
||||||
data, err = reader.connection.BudgetMessages(readCtx, id)
|
})
|
||||||
} else {
|
|
||||||
data, err = reader.call(readCtx, "get", "/api/session/"+id, nil)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w: usage read failed: %v", ErrBudgetAccounting, err)
|
return fmt.Errorf("%w: usage read failed: %v", ErrBudgetAccounting, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package stage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A usage GET can include CLI startup. Two seconds was shorter than startup
|
||||||
|
// under CPU load, falsely making otherwise healthy accounting unavailable.
|
||||||
|
// Retry only this read, never the generation or tool work it accounts for.
|
||||||
|
type budgetReadPolicy struct {
|
||||||
|
timeout time.Duration
|
||||||
|
retryDelay time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func readBudgetUsage(ctx context.Context, policy budgetReadPolicy, read func(context.Context) ([]byte, error)) ([]byte, error) {
|
||||||
|
if policy.timeout <= 0 {
|
||||||
|
policy.timeout = 10 * time.Second
|
||||||
|
}
|
||||||
|
if policy.retryDelay <= 0 {
|
||||||
|
policy.retryDelay = 100 * time.Millisecond
|
||||||
|
}
|
||||||
|
for attempt := 0; ; attempt++ {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
readCtx, cancel := context.WithTimeout(ctx, policy.timeout)
|
||||||
|
data, err := read(readCtx)
|
||||||
|
deadline := errors.Is(readCtx.Err(), context.DeadlineExceeded)
|
||||||
|
cancel()
|
||||||
|
if err == nil {
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
if parentErr := ctx.Err(); parentErr != nil {
|
||||||
|
return nil, parentErr
|
||||||
|
}
|
||||||
|
var transport net.Error
|
||||||
|
timeout := deadline || errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &transport) && transport.Timeout())
|
||||||
|
if attempt == 1 || !timeout {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
timer := time.NewTimer(policy.retryDelay)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
timer.Stop()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
case <-timer.C:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package stage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var tinyBudgetReadPolicy = budgetReadPolicy{timeout: 5 * time.Millisecond, retryDelay: time.Nanosecond}
|
||||||
|
|
||||||
|
type timeoutReadError struct{}
|
||||||
|
|
||||||
|
func (timeoutReadError) Error() string { return "temporary transport timeout" }
|
||||||
|
func (timeoutReadError) Timeout() bool { return true }
|
||||||
|
func (timeoutReadError) Temporary() bool { return true }
|
||||||
|
|
||||||
|
func TestBudgetUsageReadRetriesOnlyTransientTimeout(t *testing.T) {
|
||||||
|
for _, kind := range []string{"context deadline", "wrapped deadline", "transport timeout"} {
|
||||||
|
t.Run(kind, func(t *testing.T) {
|
||||||
|
calls := 0
|
||||||
|
got, err := readBudgetUsage(context.Background(), tinyBudgetReadPolicy, func(ctx context.Context) ([]byte, error) {
|
||||||
|
calls++
|
||||||
|
if calls == 1 {
|
||||||
|
switch kind {
|
||||||
|
case "context deadline":
|
||||||
|
<-ctx.Done()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
case "wrapped deadline":
|
||||||
|
return nil, fmt.Errorf("CLI startup: %w", context.DeadlineExceeded)
|
||||||
|
default:
|
||||||
|
return nil, timeoutReadError{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
t.Fatalf("retry inherited expired read context: %v", err)
|
||||||
|
}
|
||||||
|
return []byte("usage"), nil
|
||||||
|
})
|
||||||
|
if err != nil || string(got) != "usage" || calls != 2 {
|
||||||
|
t.Fatalf("retry: data=%s calls=%d err=%v", got, calls, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetUsageReadStopsAfterOneRetry(t *testing.T) {
|
||||||
|
calls := 0
|
||||||
|
_, err := readBudgetUsage(context.Background(), tinyBudgetReadPolicy, func(ctx context.Context) ([]byte, error) { calls++; <-ctx.Done(); return nil, ctx.Err() })
|
||||||
|
if !errors.Is(err, context.DeadlineExceeded) || calls != 2 {
|
||||||
|
t.Fatalf("persistent timeout: calls=%d err=%v", calls, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetUsageReadDoesNotRetryPermanentOrCancelledRead(t *testing.T) {
|
||||||
|
permanent := errors.New("authorization refused")
|
||||||
|
for _, failure := range []error{permanent, context.Canceled} {
|
||||||
|
calls := 0
|
||||||
|
_, err := readBudgetUsage(context.Background(), tinyBudgetReadPolicy, func(context.Context) ([]byte, error) { calls++; return nil, failure })
|
||||||
|
if !errors.Is(err, failure) || calls != 1 {
|
||||||
|
t.Fatalf("retried permanent failure: calls=%d err=%v", calls, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
calls := 0
|
||||||
|
_, err := readBudgetUsage(ctx, tinyBudgetReadPolicy, func(context.Context) ([]byte, error) { calls++; cancel(); return nil, context.DeadlineExceeded })
|
||||||
|
if !errors.Is(err, context.Canceled) || calls != 1 {
|
||||||
|
t.Fatalf("retried after parent cancelled: calls=%d err=%v", calls, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetUsageReadCancellationStopsBackoff(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
first := make(chan struct{})
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
_, err := readBudgetUsage(ctx, budgetReadPolicy{timeout: time.Second, retryDelay: time.Hour}, func(context.Context) ([]byte, error) { close(first); return nil, context.DeadlineExceeded })
|
||||||
|
done <- err
|
||||||
|
}()
|
||||||
|
<-first
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
if !errors.Is(err, context.Canceled) {
|
||||||
|
t.Fatalf("cancelled retry: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("parent cancellation did not stop retry backoff")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetWatchRetriesInitialAndFinalReadsWithoutReplayingWork(t *testing.T) {
|
||||||
|
budget := NewBudget(0, 10)
|
||||||
|
budget.pollInterval = time.Hour
|
||||||
|
var reads atomic.Int32
|
||||||
|
client := OpenCodeClient{Backend: "v2", budgetReadPolicy: tinyBudgetReadPolicy, command: func(ctx context.Context, _ string, args ...string) ([]byte, error) {
|
||||||
|
if args[1] != "get" || args[2] != "/api/session/ses_budget" {
|
||||||
|
t.Errorf("budget retry called non-accounting operation: %v", args)
|
||||||
|
}
|
||||||
|
switch reads.Add(1) {
|
||||||
|
case 1, 3:
|
||||||
|
<-ctx.Done()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
case 2:
|
||||||
|
return usageResponse("ses_budget", 0, 0), nil
|
||||||
|
default:
|
||||||
|
return usageResponse("ses_budget", 0, 9), nil
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
ctx, watch, err := client.watchBudget(context.Background(), budget, "ses_budget")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var terminal error
|
||||||
|
watch.stopWatch(ctx, &terminal)
|
||||||
|
cancelled, cancel := context.WithCancel(ctx)
|
||||||
|
cancel()
|
||||||
|
watch.finish(cancelled, &terminal) // final read retains its independent bounded context
|
||||||
|
if terminal != nil || reads.Load() != 4 || budget.Snapshot().UsedTokens != 9 {
|
||||||
|
t.Fatalf("final accounting: reads=%d budget=%+v err=%v", reads.Load(), budget.Snapshot(), terminal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetWatchRejectsMalformedAndMissingAccountingWithoutRetry(t *testing.T) {
|
||||||
|
for _, body := range []string{`garbage`, `{"data":{"id":"ses_budget"}}`, string(usageResponse("ses_other", 0, 1))} {
|
||||||
|
calls := 0
|
||||||
|
client := OpenCodeClient{Backend: "v2", budgetReadPolicy: tinyBudgetReadPolicy, command: func(context.Context, string, ...string) ([]byte, error) { calls++; return []byte(body), nil }}
|
||||||
|
_, watch, err := client.watchBudget(context.Background(), NewBudget(0, 10), "ses_budget")
|
||||||
|
if !errors.Is(err, ErrBudgetAccounting) || watch != nil || calls != 1 {
|
||||||
|
t.Fatalf("invalid accounting was retried or bypassed: calls=%d watch=%v err=%v", calls, watch, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBudgetWatchPersistentReadTimeoutCancelsAndFinalReadStillEnforcesCap(t *testing.T) {
|
||||||
|
budget := NewBudget(0, 10)
|
||||||
|
budget.pollInterval = time.Millisecond
|
||||||
|
var reads atomic.Int32
|
||||||
|
client := OpenCodeClient{Backend: "v2", budgetReadPolicy: tinyBudgetReadPolicy, command: func(ctx context.Context, _ string, args ...string) ([]byte, error) {
|
||||||
|
switch reads.Add(1) {
|
||||||
|
case 1:
|
||||||
|
return usageResponse("ses_budget", 0, 0), nil
|
||||||
|
case 2, 3:
|
||||||
|
<-ctx.Done()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
default:
|
||||||
|
return usageResponse("ses_budget", 0, 12), nil
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
ctx, watch, err := client.watchBudget(context.Background(), budget, "ses_budget")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("persistent missing accounting did not stop work")
|
||||||
|
}
|
||||||
|
var terminal error
|
||||||
|
watch.stopWatch(ctx, &terminal)
|
||||||
|
watch.finish(ctx, &terminal)
|
||||||
|
if !errors.Is(terminal, ErrBudgetAccounting) || !errors.Is(terminal, ErrBudgetExceeded) || budget.Snapshot().UsedTokens != 12 || reads.Load() != 4 {
|
||||||
|
t.Fatalf("failed closed/final cap: calls=%d budget=%+v err=%v", reads.Load(), budget.Snapshot(), terminal)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,7 +38,8 @@ type OpenCodeClient struct {
|
|||||||
OnGenerationSession func(id, backend, reference string, active bool) error
|
OnGenerationSession func(id, backend, reference string, active bool) error
|
||||||
connection *opencode.Connection
|
connection *opencode.Connection
|
||||||
command func(context.Context, string, ...string) ([]byte, error)
|
command func(context.Context, string, ...string) ([]byte, error)
|
||||||
sessionPollInterval time.Duration // test seam; defaults to defaultSessionPollInterval
|
sessionPollInterval time.Duration // test seam; defaults to defaultSessionPollInterval
|
||||||
|
budgetReadPolicy budgetReadPolicy // test seam; zero uses bounded production defaults
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArtifactsEnv names the environment variable carrying a stage's artifacts
|
// ArtifactsEnv names the environment variable carrying a stage's artifacts
|
||||||
|
|||||||
@@ -45,7 +45,10 @@ func TestReadOnlyAgentRetriesPostAdmissionDeadline(t *testing.T) {
|
|||||||
def := turnDefinition(t)
|
def := turnDefinition(t)
|
||||||
def.Blocks["tools"] = "read"
|
def.Blocks["tools"] = "read"
|
||||||
def.Configs["retries"] = "1"
|
def.Configs["retries"] = "1"
|
||||||
def.Configs["timeout"] = "30ms"
|
// The first prompt waits on ctx.Done, so a longer allowance does not weaken
|
||||||
|
// the deadline assertion. It leaves the successful retry time to finish on
|
||||||
|
// hosts concurrently compiling/running the integration suites.
|
||||||
|
def.Configs["timeout"] = "500ms"
|
||||||
var sessions, prompts atomic.Int64
|
var sessions, prompts atomic.Int64
|
||||||
agent := &AgentClient{OpenCodeClient: OpenCodeClient{command: func(ctx context.Context, _ string, args ...string) ([]byte, error) {
|
agent := &AgentClient{OpenCodeClient: OpenCodeClient{command: func(ctx context.Context, _ string, args ...string) ([]byte, error) {
|
||||||
path := args[2]
|
path := args[2]
|
||||||
|
|||||||
Reference in New Issue
Block a user