Compare commits

..
4 changed files with 212 additions and 229 deletions
+10 -86
View File
@@ -17,7 +17,6 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
@@ -102,66 +101,6 @@ func normalizeHeaders(headers map[string]string) (map[string]string, error) {
return out, nil
}
// originKey returns the scheme, host and effective port of u, in a form two
// URLs can be compared with. Empty means u is not an absolute http(s) URL with
// a host.
func originKey(u *url.URL) string {
if u == nil {
return ""
}
scheme := strings.ToLower(u.Scheme)
var port string
switch scheme {
case "http":
port = "80"
case "https":
port = "443"
default:
return ""
}
host := strings.ToLower(u.Hostname())
if host == "" {
return ""
}
if p := u.Port(); p != "" {
port = p
}
return scheme + "://" + net.JoinHostPort(host, port)
}
// sameOrigin reports whether u has the same scheme, host and effective port as
// base.
func sameOrigin(base, u *url.URL) bool {
key := originKey(base)
return key != "" && key == originKey(u)
}
// redirectPolicy refuses a redirect that leaves the origin of the configured
// endpoint, so a remote MCP server cannot retarget authenticated requests.
func redirectPolicy(base *url.URL) func(*http.Request, []*http.Request) error {
return func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("mcp: stopped after 10 redirects")
}
if !sameOrigin(base, req.URL) {
return fmt.Errorf("mcp: refusing a redirect to another origin: %s", req.URL.Redacted())
}
return nil
}
}
// parseMCPURL parses and validates a configured MCP endpoint URL.
func parseMCPURL(raw string) (*url.URL, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, fmt.Errorf("mcp url: %w", err)
}
if originKey(u) == "" {
return nil, fmt.Errorf("mcp url must be an absolute http or https URL with a host: %q", raw)
}
return u, nil
}
// New connects to a remote MCP endpoint and performs the initialize handshake.
// headers are sent on every request (Authorization, custom API keys, …).
// When insecure is true, TLS certificate verification is skipped so servers that
@@ -178,11 +117,7 @@ func New(ctx context.Context, server, url string, headers map[string]string, ins
if timeoutSeconds > 0 {
timeout = time.Duration(timeoutSeconds) * time.Second
}
base, err := parseMCPURL(url)
if err != nil {
return nil, err
}
hc := &http.Client{Timeout: timeout, CheckRedirect: redirectPolicy(base)}
hc := &http.Client{Timeout: timeout}
if insecure {
hc.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
@@ -207,11 +142,7 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
if err != nil {
return nil, err
}
base, err := parseMCPURL(sseURL)
if err != nil {
return nil, err
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)} // the SSE stream is intentionally long-lived.
hc := &http.Client{} // the SSE stream is intentionally long-lived.
if insecure {
hc.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
@@ -236,7 +167,7 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
return nil, fmt.Errorf("mcp sse http %d: %s", resp.StatusCode, readSnippet(resp.Body))
}
reader := bufio.NewReader(resp.Body)
endpoint, err := readSSEEndpoint(reader, base)
endpoint, err := readSSEEndpoint(reader, sseURL)
if err != nil {
resp.Body.Close()
cancel()
@@ -264,11 +195,7 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
return c, nil
}
// readSSEEndpoint reads the message endpoint announced by the SSE stream. The
// endpoint must stay on the origin of the configured SSE URL: the POSTs that
// follow carry the operator's headers, and a remote server must not be able to
// retarget them.
func readSSEEndpoint(r *bufio.Reader, base *url.URL) (string, error) {
func readSSEEndpoint(r *bufio.Reader, base string) (string, error) {
for {
line, err := r.ReadString('\n')
if err != nil {
@@ -286,17 +213,14 @@ func readSSEEndpoint(r *bufio.Reader, base *url.URL) (string, error) {
if err != nil {
return "", fmt.Errorf("mcp sse endpoint URL: %w", err)
}
if u.User != nil {
return "", errors.New("mcp sse endpoint must not carry userinfo")
}
abs := u
if !u.IsAbs() {
abs = base.ResolveReference(u)
b, err := url.Parse(base)
if err != nil {
return "", err
}
candidate = b.ResolveReference(u).String()
}
if !sameOrigin(base, abs) {
return "", fmt.Errorf("mcp sse endpoint %q is not on the configured origin", abs.Redacted())
}
return abs.String(), nil
return candidate, nil
}
}
-133
View File
@@ -1,13 +1,11 @@
package mcphttp
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
@@ -107,134 +105,3 @@ func TestLegacySSEClientDiscoverAndCall(t *testing.T) {
t.Fatalf("Call() = %q, %v", got, err)
}
}
func TestReadSSEEndpointSameOrigin(t *testing.T) {
base, err := url.Parse("https://mcp.example.com:8443/sse")
if err != nil {
t.Fatal(err)
}
read := func(line string) (string, error) {
stream := "event: endpoint\ndata: " + line + "\n\n"
return readSSEEndpoint(bufio.NewReader(strings.NewReader(stream)), base)
}
for _, line := range []string{
"/message?sessionId=1",
"https://mcp.example.com:8443/message",
"HTTPS://MCP.EXAMPLE.COM:8443/message",
"//mcp.example.com:8443/message",
} {
got, err := read(line)
if err != nil {
t.Errorf("%q: %v", line, err)
continue
}
u, err := url.Parse(got)
if err != nil || !sameOrigin(base, u) {
t.Errorf("%q: resolved to %q, not the configured origin", line, got)
}
}
for _, line := range []string{
"https://evil.example/message",
"http://mcp.example.com:8443/message",
"https://mcp.example.com:444/message",
"//evil.example/message",
"http://user:pass@mcp.example.com:8443/message",
"ftp://mcp.example.com/message",
} {
if got, err := read(line); err == nil {
t.Errorf("%q: accepted as %q, want a refusal", line, got)
}
}
}
func TestReadSSEEndpointDefaultPort(t *testing.T) {
base, err := url.Parse("https://mcp.example.com/sse")
if err != nil {
t.Fatal(err)
}
stream := "data: https://mcp.example.com:443/message\n\n"
if _, err := readSSEEndpoint(bufio.NewReader(strings.NewReader(stream)), base); err != nil {
t.Fatalf("explicit default port refused: %v", err)
}
}
func TestParseMCPURL(t *testing.T) {
for _, raw := range []string{"ftp://x/y", "not-a-url", "http:///nohost", ""} {
if _, err := parseMCPURL(raw); err == nil {
t.Errorf("%q accepted, want a refusal", raw)
}
}
for _, raw := range []string{"http://x/y", "https://x:8443/y"} {
if _, err := parseMCPURL(raw); err != nil {
t.Errorf("%q refused: %v", raw, err)
}
}
}
func TestLegacySSERefusesCrossOriginEndpoint(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprint(w, "event: endpoint\ndata: http://evil.invalid/message\n\n")
w.(http.Flusher).Flush()
<-r.Context().Done()
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := NewSSE(ctx, "test", srv.URL+"/sse", nil, false); err == nil {
t.Fatal("cross-origin SSE endpoint accepted")
}
}
func TestMCPClientRefusesCrossOriginRedirect(t *testing.T) {
var hits int
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
}))
defer target.Close()
src := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target.URL+"/steal", http.StatusFound)
}))
defer src.Close()
base, err := url.Parse(src.URL + "/sse")
if err != nil {
t.Fatal(err)
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)}
resp, err := hc.Get(src.URL + "/sse")
if err == nil {
resp.Body.Close()
t.Fatal("cross-origin redirect accepted")
}
if hits != 0 {
t.Fatalf("redirect target was reached %d times", hits)
}
}
func TestMCPClientAllowsSameOriginRedirect(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/sse2", http.StatusFound)
})
mux.HandleFunc("/sse2", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
srv := httptest.NewServer(mux)
defer srv.Close()
base, err := url.Parse(srv.URL + "/sse")
if err != nil {
t.Fatal(err)
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)}
resp, err := hc.Get(srv.URL + "/sse")
if err != nil {
t.Fatalf("same-origin redirect refused: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("status %d", resp.StatusCode)
}
}
+23 -10
View File
@@ -1182,6 +1182,27 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
return e.runIntent(ctx, t, name, worker, intent, token, "", "")
}
// intentWorkContext resolves the cached per-task exec context and returns a
// per-intent child context with the rotated LLM profile pin attached (#85,
// #376). Attaching the pin after execContextFor ensures a cached e.execCtx
// does not drop it; attaching it after the paused/deleting check ensures a
// rolled-back claim never advances the rotation counter.
func (e *Engine) intentWorkContext(ctx context.Context, t *Task) (ectx, workCtx context.Context, workCancel context.CancelCauseFunc, ok bool) {
ectx = e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
return ectx, nil, nil, false
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel = context.WithCancelCause(ectx)
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, pinned := t.nextRotatedProfile(); pinned {
workCtx = withLLMProfilePin(workCtx, head)
}
return ectx, workCtx, workCancel, true
}
// runIntent executes and fully settles one already-claimed (state=running) intent.
// Both the pool worker loop (via runWorkerStep) and the human-message handler (via
// runDetachedIntent, a dedicated goroutine outside the worker pool) call it, so the
@@ -1194,25 +1215,17 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
// while the worker is busy, and every intent state write below is fenced on it,
// so a worker whose claim was taken over cannot overwrite the new claimer.
func (e *Engine) runIntent(ctx context.Context, t *Task, name string, worker *agent.Worker, intent *db.Node, token, requestID, message string) bool {
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, ok := t.nextRotatedProfile(); ok {
ctx = withLLMProfilePin(ctx, head)
}
hasChatMessage := message != ""
e.stampFirstRun(t) // first real execution → stamp first_run_at + compute the deadline (only for tasks with a timeout)
e.touch(t.ID)
emit := func(r db.Activity) { e.emitActivity(t, r) }
ectx := e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
ectx, workCtx, workCancel, ok := e.intentWorkContext(ctx, t)
if !ok {
if err := t.Store.SettleIntentClaim(intent.ID, token, "open"); err != nil {
log.Printf("[worker %s] task %s intent #%d claim rollback failed: %v", name, t.ID, intent.ID, err)
}
return true
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel := context.WithCancelCause(ectx)
e.registerWork(intent.ID, workCancel)
stopHeartbeat := startClaimHeartbeat(t.Store, db.IntentClaimHeartbeatInterval, name, intent.ID, token, workCancel)
// wrap the guard hooks so steer_work can inject a mid-run course-correction
+179
View File
@@ -3,12 +3,14 @@ package server
import (
"context"
"fmt"
"iter"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/norma/llm"
)
@@ -211,3 +213,180 @@ func TestNextRotatedProfileRoundRobin(t *testing.T) {
t.Fatal("disabled rotation must not assign a pin")
}
}
type pinCapture struct {
id int64
ok bool
}
type pinCaptureProvider struct {
mu sync.Mutex
seen []pinCapture
}
func (p *pinCaptureProvider) record(ctx context.Context) {
id, ok := llmProfilePinFrom(ctx)
p.mu.Lock()
p.seen = append(p.seen, pinCapture{id: id, ok: ok})
p.mu.Unlock()
}
func (p *pinCaptureProvider) Stream(ctx context.Context, _ llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
p.record(ctx)
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SETextDelta, Text: "done"}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "end_turn"}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
func (p *pinCaptureProvider) Complete(ctx context.Context, _ llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
p.record(ctx)
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{llm.TextBlock("done")}}, "end_turn", llm.Usage{}, nil
}
// Regression test for #376 (DB-free): warming e.execContextFor first (as the
// planner does) must not drop the per-intent LLM profile pin on subsequent
// worker intents, and a paused/deleting rollback must not advance rotation.
func TestIntentWorkContextRotatesWithCachedExecCtx(t *testing.T) {
const (
taskID = "77"
profileA = int64(101)
profileB = int64(202)
)
e := NewEngine(nil)
tk := &Task{ID: taskID, LLMProfileIDs: []int64{profileA, profileB}, LLMRotate: true}
// Warm the per-task cached execCtx, simulating the planner running first.
warm := e.execContextFor(context.Background(), taskID)
if warm == nil || warm.Err() != nil {
t.Fatalf("warm execContextFor returned invalid ctx: %v", warm)
}
// A paused task must roll back without advancing the rotation counter.
e.Pause(taskID, agent.AbortPausedByUser)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("paused task unexpectedly yielded a work context")
}
e.Resume(tk)
// Re-warm cached execCtx after resume (again simulating planner before worker).
_ = e.execContextFor(context.Background(), taskID)
// A deleting task must also roll back without advancing the rotation counter.
e.BeginDelete(taskID)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("deleting task unexpectedly yielded a work context")
}
e.AbortDelete(taskID, false)
// Two consecutive claimed intents must carry distinct rotated pins (A then B).
_, workA, cancelA, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("first intentWorkContext failed")
}
defer cancelA(nil)
gotA, okA := llmProfilePinFrom(workA)
if !okA || gotA != profileA {
t.Fatalf("first workCtx pin = (%d, %v), want (%d, true)", gotA, okA, profileA)
}
_, workB, cancelB, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("second intentWorkContext failed")
}
defer cancelB(nil)
gotB, okB := llmProfilePinFrom(workB)
if !okB || gotB != profileB {
t.Fatalf("second workCtx pin = (%d, %v), want (%d, true)", gotB, okB, profileB)
}
if gotA == gotB {
t.Fatalf("expected distinct pinned profile IDs, both were %d", gotA)
}
}
// Regression test for #376 (end-to-end through runIntent): warm execContextFor
// first (simulating the planner running before workers), then run two intents
// through runIntent and assert the two work contexts carry distinct pinned
// profile IDs (A then B).
func TestRunIntentRotatesPinnedProfileWithCachedExecCtx(t *testing.T) {
m, err := NewManager(t.TempDir(), "")
if err != nil {
t.Skipf("postgres unavailable: %v", err)
}
defer m.Close()
ids := make([]int64, 0, 2)
for i := 0; i < 2; i++ {
id, err := m.pg.SaveProfile(&db.LLMProfile{
Name: fmt.Sprintf("runintent-rotate-%d-%d", time.Now().UnixNano(), i),
Format: "openai",
BaseURL: "http://127.0.0.1:1/v1",
Model: "test",
APIKey: "test-key",
})
if err != nil {
t.Fatal(err)
}
ids = append(ids, id)
defer m.pg.DeleteProfile(id)
}
on := true
task, err := m.CreateTaskWithOptions("runIntent rotation", "verify pin survives cached execCtx", db.TaskCreateOptions{
LLMProfileIDs: ids,
LLMRotate: &on,
})
if err != nil {
t.Fatal(err)
}
defer m.DeleteTask(task.ID, DeleteTaskOptions{})
e := NewEngine(m)
// Warm the per-task cached execCtx before any worker intent runs.
_ = e.execContextFor(context.Background(), task.ID)
// Verify paused rollback through runIntent does not advance rotation.
if _, err := task.Store.AddIntent(map[string]any{"summary": "rolled back while paused"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
pausedIntent, pausedToken := e.claimNext(task, "work#0")
if pausedIntent == nil {
t.Fatal("failed to claim intent for paused rollback check")
}
e.Pause(task.ID, agent.AbortPausedByUser)
probe := &pinCaptureProvider{}
worker := agent.NewWorker(probe, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, "work#0", worker, pausedIntent, pausedToken, "", "")
if len(probe.seen) != 0 {
t.Fatalf("paused runIntent called provider %d times, want 0", len(probe.seen))
}
e.Resume(task)
// Re-warm cached execCtx after resume (simulating planner before workers).
_ = e.execContextFor(context.Background(), task.ID)
// Run two intents through runIntent and assert distinct pins: ids[0] then ids[1].
if _, err := task.Store.AddIntent(map[string]any{"summary": "rotate intent 2"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
for i, wantID := range ids {
name := fmt.Sprintf("work#%d", i+1)
intent, token := e.claimNext(task, name)
if intent == nil {
t.Fatalf("intent %d not claimed", i+1)
}
p := &pinCaptureProvider{}
w := agent.NewWorker(p, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, name, w, intent, token, "", "")
if len(p.seen) == 0 {
t.Fatalf("intent %d: provider was not called", i+1)
}
got := p.seen[0]
if !got.ok || got.id != wantID {
t.Fatalf("intent %d: workCtx pin = (%d, %v), want (%d, true)", i+1, got.id, got.ok, wantID)
}
}
}