From b164bcbb14615223f946f5c9023451e5eadac42a Mon Sep 17 00:00:00 2001 From: archie Date: Thu, 23 Jul 2026 23:47:23 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20agent=20context=20doubled=20up=20?= =?UTF-8?q?=E2=80=94=20conversation=20history=20duplicated=20in=20prompt?= =?UTF-8?q?=20(archie)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements #65 --- internal/agentexec/skill_test.go | 39 ++++++++++++++++++-------------- internal/agentexec/worker.go | 25 -------------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/internal/agentexec/skill_test.go b/internal/agentexec/skill_test.go index 9daf927f5..42a087976 100644 --- a/internal/agentexec/skill_test.go +++ b/internal/agentexec/skill_test.go @@ -26,14 +26,14 @@ func (c *captureRunner) Run(_ context.Context, _ string, req Request) (Result, e }, nil } -// ── regression: Agent doesn't load its own skills ─────────────────── +// ── regression: HandleMessage must not duplicate skill body ───────── -func TestHandleMessageLoadsSkillsFromWorktree(t *testing.T) { - // PRD section 1: archie-agent owns skill loading — reads - // .agents/skills/archie-wf-* from the worktree. - // - // Currently the daemon loads skills and injects the body into - // the Mission string. HandleMessage never touches .agents/skills/. +func TestHandleMessageDoesNotDuplicateSkillBody(t *testing.T) { + // Regression for issue #65: the daemon already prepends the skill + // body to every mission via missionWithSkill() before the Request + // reaches HandleMessage. HandleMessage must pass the mission + // through unchanged — a second prepend here would duplicate the + // skill body in the agent's context window. dir := t.TempDir() skillsDir := filepath.Join(dir, ".agents", "skills", "archie-wf-tdd") @@ -53,6 +53,12 @@ Analyse the bug. Write repro tests. Fix. t.Fatal(err) } + // Mission already includes the daemon-side prepend (as produced by + // missionWithSkill in internal/workflow/agent.go). + alreadyPrepended := "Follow these project-specific guidelines:\n\n" + + "Analyse the bug. Write repro tests. Fix.\n\n---\n\n" + + "Analyse this bug" + var capturedMission string captureFactory := func(providers map[string]Provider, log *slog.Logger) Runner { return &captureRunner{callback: func(req Request) { @@ -69,7 +75,7 @@ Analyse the bug. Write repro tests. Fix. Request: Request{ Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "analyse", Model: "test/model", - Mission: "Analyse this bug", + Mission: alreadyPrepended, Budget: Budget{MaxSteps: 1, MaxTokens: 10}, }, Providers: map[string]Provider{"test": {Class: "openai", APIKeyEnv: "FAKE"}}, @@ -81,14 +87,13 @@ Analyse the bug. Write repro tests. Fix. } _ = resp - // The agent must load the skill body from its workspace. If the - // agent loaded the skill, the Mission sent to the runner would - // include the skill body prepended. - if capturedMission == "Analyse this bug" { - t.Error("Gap: HandleMessage does not load skills from the worktree. " + - "The Mission sent to the runner is the bare mission string — " + - "no skill content was loaded from " + skillsDir + ". " + - "HandleMessage must load .agents/skills//SKILL.md " + - "from the workspace when processing a task. PRD section 1.") + // The mission must be passed through unchanged. Any modification + // would indicate a double-prepend bug (issue #65). + if capturedMission != alreadyPrepended { + t.Errorf("HandleMessage modified the mission:\n got: %q\n want: %q\n\n"+ + "The daemon already prepends the skill body via missionWithSkill(). "+ + "HandleMessage must not prepend it again — doing so duplicates content "+ + "in the agent's context window (issue #65).", + capturedMission, alreadyPrepended) } } diff --git a/internal/agentexec/worker.go b/internal/agentexec/worker.go index 45e816bd5..18446062f 100644 --- a/internal/agentexec/worker.go +++ b/internal/agentexec/worker.go @@ -8,8 +8,6 @@ import ( "io" "log/slog" "strings" - - "github.com/samcharles93/archie-core/internal/skill" ) const maxProtocolBytes = 8 << 20 @@ -70,18 +68,6 @@ func HandleMessage(ctx context.Context, msg AgentRequestMessage, log *slog.Logge channel = "response" } - // Load the skill body from the worktree — the agent owns skill - // loading per PRD section 1. Mirrors daemon-side loadSkillBody(). - if msg.Workflow != "" && msg.Workspace != "" { - body := skill.LoadBody(msg.Workspace, skillDirForWorkflow(msg.Workspace, msg.Workflow)) - if body != "" { - msg.Request.Mission = "Follow these project-specific guidelines:\n\n" + body + "\n\n---\n\n" + msg.Request.Mission - for i := range msg.Stages { - msg.Stages[i].Mission = "Follow these project-specific guidelines:\n\n" + body + "\n\n---\n\n" + msg.Stages[i].Mission - } - } - } - // Per-task mode: run all stages for this workflow as a batch. if msg.Workflow != "" && len(msg.Stages) > 0 { return runStages(ctx, runner, msg, channel, log) @@ -151,17 +137,6 @@ func runStages(ctx context.Context, runner Runner, msg AgentRequestMessage, chan }, nil } -// skillDirForWorkflow finds the skill directory for a workflow from the -// workspace catalog. Skills declare their workflow in metadata.archie.workflow. -func skillDirForWorkflow(workspace, workflow string) string { - catalog, _ := skill.Catalog(workspace) - entry := skill.SkillForWorkflow(catalog, workflow) - if entry == nil { - return "" - } - return entry.Dir -} - func decodeOne(r io.Reader, value any) error { data, err := io.ReadAll(io.LimitReader(r, maxProtocolBytes+1)) if err != nil {