mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
9da0cfe34b
P2: - CRF-7: effectiveAllowedRoots now appends the current working directory lazily so the manager picks up the resolved path after the manifest loads. - CRF-8: walkDir error path computes Kind from the basename (kindFromFilename) so .mcp.json and SKILL.md keep a stable resource ID across permission flips. - CRF-9: ComputeAggregateHash / writeLengthPrefixed docs now describe the Netstring-style encoding accurately. - CRF-10: resolveAndBroadcast snapshots inputs under the lock, releases it for the filesystem walk, then re-acquires to swap. - CRF-11: api.go SnapshotResource comment no longer references a nonexistent PayloadBase64 field. P3: - CRF-12: WatcherOptions.MaxDepth; the watcher now mirrors the resolver's depth instead of hardcoding DefaultMaxScanDepth. - CRF-13: drop obsolete "follow-up" sentence in doc.go. - CRF-14: Pusher doc says Agent API v2.10, not proto v30. - CRF-15: Resolver.ResolveContext threads ctx through walk so resolveAndBroadcast can cancel between roots. - CRF-16: new tests cover StatusUnreadable for instruction and MCP-config files (skipped on Windows and root). - CRF-17: TestRunPush_ClosesOnManagerClose covers closedCh path. - CRF-18: drop dead !ok branch on the subscriber channel. - CRF-19: extract readFileResource; readInstructionFile and readMCPConfig share the plumbing. - CRF-24: DefaultPushInitialBackoff / DefaultPushMaxBackoff. - CRF-25: subsequent-push test asserts AggregateHash and Version advance, not just the count. P4: - CRF-26: TestRunPush_RejectedResponseProceeds covers the Accepted=false fast-path. Nit: - CRF-27: slices.SortFunc/SortStableFunc replace sort.Slice. - CRF-28: firstLine uses strings.SplitSeq. - CRF-29: sync.OnceFunc replaces the sync.Once+closure wrapper. - CRF-30: atomic.Int32 replaces bare int32 in watch_test. - CRF-31: redundant sort in resolveLocked already gone with CRF-10. - CRF-32: drop redundant "agentcontext:" prefix from log messages. - CRF-33: resolveAndBroadcast doc no longer claims conditional. Note: - CRF-34: ResourceStatus doc explains the proto-enum offset. - CRF-35: readInstructionFile doc flags the sanitization gap delegated to the chatd follow-up.
306 lines
8.2 KiB
Go
306 lines
8.2 KiB
Go
package agentcontext_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/agent/agentcontext"
|
|
"github.com/coder/coder/v2/testutil"
|
|
"github.com/coder/quartz"
|
|
)
|
|
|
|
// fakePusher records every push and lets the test control the
|
|
// returned response and error.
|
|
type fakePusher struct {
|
|
mu sync.Mutex
|
|
requests []*agentcontext.PushRequest
|
|
resp *agentcontext.PushResponse
|
|
err error
|
|
// errOnce is non-nil to simulate a single transient
|
|
// failure followed by success.
|
|
errOnce error
|
|
signal chan struct{}
|
|
}
|
|
|
|
func newFakePusher() *fakePusher {
|
|
return &fakePusher{
|
|
resp: &agentcontext.PushResponse{Accepted: true},
|
|
signal: make(chan struct{}, 16),
|
|
}
|
|
}
|
|
|
|
func (p *fakePusher) PushContextState(_ context.Context, req *agentcontext.PushRequest) (*agentcontext.PushResponse, error) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
p.requests = append(p.requests, req)
|
|
if p.errOnce != nil {
|
|
err := p.errOnce
|
|
p.errOnce = nil
|
|
return nil, err
|
|
}
|
|
select {
|
|
case p.signal <- struct{}{}:
|
|
default:
|
|
}
|
|
return p.resp, p.err
|
|
}
|
|
|
|
func (p *fakePusher) snapshot() []*agentcontext.PushRequest {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
out := make([]*agentcontext.PushRequest, len(p.requests))
|
|
copy(out, p.requests)
|
|
return out
|
|
}
|
|
|
|
func TestRunPush_FirstPushIsInitial(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v1"), 0o600))
|
|
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return dir },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
ctx, cancel := context.WithCancel(testutil.Context(t, testutil.WaitShort))
|
|
defer cancel()
|
|
|
|
pushDone := make(chan error, 1)
|
|
go func() {
|
|
pushDone <- m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
}()
|
|
|
|
// Wait for the first push.
|
|
select {
|
|
case <-p.signal:
|
|
case <-time.After(testutil.WaitShort):
|
|
t.Fatalf("expected initial push")
|
|
}
|
|
|
|
requests := p.snapshot()
|
|
require.Len(t, requests, 1)
|
|
require.True(t, requests[0].Initial, "first push must be initial")
|
|
require.Equal(t, uint64(1), requests[0].Version)
|
|
|
|
cancel()
|
|
require.ErrorIs(t, <-pushDone, context.Canceled)
|
|
}
|
|
|
|
func TestRunPush_SubsequentPushOnChange(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v1"), 0o600))
|
|
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return dir },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
ctx, cancel := context.WithCancel(testutil.Context(t, testutil.WaitShort))
|
|
defer cancel()
|
|
|
|
pushDone := make(chan error, 1)
|
|
go func() {
|
|
pushDone <- m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
}()
|
|
|
|
// Initial push.
|
|
<-p.signal
|
|
|
|
// Trigger a resync via Resync.
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v2"), 0o600))
|
|
_, err := m.Resync(ctx)
|
|
require.NoError(t, err)
|
|
|
|
// Second push.
|
|
select {
|
|
case <-p.signal:
|
|
case <-time.After(testutil.WaitShort):
|
|
t.Fatalf("expected second push after resync")
|
|
}
|
|
|
|
requests := p.snapshot()
|
|
require.GreaterOrEqual(t, len(requests), 2)
|
|
require.False(t, requests[1].Initial, "subsequent pushes must not be Initial")
|
|
require.NotEqual(t, requests[0].AggregateHash, requests[1].AggregateHash,
|
|
"second push must reflect the v2 content, not a duplicate of the first snapshot")
|
|
require.Greater(t, requests[1].Version, requests[0].Version,
|
|
"version must advance between snapshots")
|
|
|
|
cancel()
|
|
require.ErrorIs(t, <-pushDone, context.Canceled)
|
|
}
|
|
|
|
func TestRunPush_StopsOnUnimplemented(t *testing.T) {
|
|
t.Parallel()
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return t.TempDir() },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
p.err = agentcontext.ErrPushUnimplemented
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
err := m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
require.NoError(t, err, "Unimplemented must stop the loop cleanly")
|
|
}
|
|
|
|
func TestRunPush_RetriesTransientError(t *testing.T) {
|
|
t.Parallel()
|
|
mClock := quartz.NewMock(t)
|
|
trap := mClock.Trap().NewTimer()
|
|
defer trap.Close()
|
|
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return t.TempDir() },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
p.errOnce = xerrors.New("transient")
|
|
|
|
ctx, cancel := context.WithCancel(testutil.Context(t, testutil.WaitShort))
|
|
defer cancel()
|
|
pushDone := make(chan error, 1)
|
|
go func() {
|
|
pushDone <- m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
InitialBackoff: time.Second,
|
|
Clock: mClock,
|
|
})
|
|
}()
|
|
|
|
// First push hits transient and arms the retry timer. Wait for
|
|
// the timer creation, then advance the clock past the backoff.
|
|
call := trap.MustWait(ctx)
|
|
call.MustRelease(ctx)
|
|
mClock.Advance(time.Second).MustWait(ctx)
|
|
|
|
select {
|
|
case <-p.signal:
|
|
case <-time.After(testutil.WaitShort):
|
|
t.Fatalf("expected push after transient error")
|
|
}
|
|
require.GreaterOrEqual(t, len(p.snapshot()), 2)
|
|
|
|
cancel()
|
|
<-pushDone
|
|
}
|
|
|
|
// TestRunPush_ClosesOnManagerClose verifies that calling
|
|
// Manager.Close terminates an in-flight RunPush even when the
|
|
// caller's context is still live. Without this guarantee the
|
|
// agent shutdown would leak a push goroutine until the
|
|
// surrounding ctx expired.
|
|
func TestRunPush_ClosesOnManagerClose(t *testing.T) {
|
|
t.Parallel()
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return t.TempDir() },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
}()
|
|
|
|
// Wait for the initial push so the loop is parked on the
|
|
// change channel, then close the Manager and assert that
|
|
// RunPush returns promptly with a nil error.
|
|
select {
|
|
case <-p.signal:
|
|
case <-ctx.Done():
|
|
t.Fatalf("initial push never landed: %v", ctx.Err())
|
|
}
|
|
require.NoError(t, m.Close())
|
|
|
|
select {
|
|
case err := <-done:
|
|
require.NoError(t, err)
|
|
case <-ctx.Done():
|
|
t.Fatalf("RunPush did not return after Manager.Close: %v", ctx.Err())
|
|
}
|
|
}
|
|
|
|
// TestRunPush_RejectedResponseProceeds verifies the contract
|
|
// that an Accepted=false response is not retried: pushWithRetry
|
|
// returns success and RunPush parks on the next change instead
|
|
// of re-sending the same snapshot. A regression that added
|
|
// retry-on-reject logic would loop here and fail the test.
|
|
func TestRunPush_RejectedResponseProceeds(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v1"), 0o600))
|
|
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return dir },
|
|
})
|
|
|
|
p := newFakePusher()
|
|
p.resp = &agentcontext.PushResponse{Accepted: false}
|
|
|
|
ctx, cancel := context.WithCancel(testutil.Context(t, testutil.WaitShort))
|
|
defer cancel()
|
|
pushDone := make(chan error, 1)
|
|
go func() {
|
|
pushDone <- m.RunPush(ctx, p, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
}()
|
|
|
|
// Initial push delivered and accepted=false; loop must park
|
|
// on changes, not retry the same payload.
|
|
select {
|
|
case <-p.signal:
|
|
case <-ctx.Done():
|
|
t.Fatalf("initial push never landed: %v", ctx.Err())
|
|
}
|
|
|
|
// Trigger a content change so a second push lands. Without
|
|
// the change, the loop should remain parked.
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v2"), 0o600))
|
|
_, err := m.Resync(ctx)
|
|
require.NoError(t, err)
|
|
|
|
select {
|
|
case <-p.signal:
|
|
case <-ctx.Done():
|
|
t.Fatalf("second push never landed after change: %v", ctx.Err())
|
|
}
|
|
|
|
requests := p.snapshot()
|
|
require.GreaterOrEqual(t, len(requests), 2,
|
|
"exactly one push per snapshot; rejection must not double-fire")
|
|
require.NotEqual(t, requests[0].AggregateHash, requests[1].AggregateHash)
|
|
|
|
cancel()
|
|
require.ErrorIs(t, <-pushDone, context.Canceled)
|
|
}
|
|
|
|
func TestRunPush_NilPusherErrors(t *testing.T) {
|
|
t.Parallel()
|
|
m := newTestManager(t, agentcontext.ManagerOptions{
|
|
WorkingDir: func() string { return t.TempDir() },
|
|
})
|
|
err := m.RunPush(context.Background(), nil, agentcontext.PushOptions{
|
|
Logger: testutil.Logger(t).Named("push"),
|
|
})
|
|
require.Error(t, err)
|
|
}
|