diff --git a/internal/container/kit.go b/internal/container/kit.go index e78dad5d3..602dbce27 100644 --- a/internal/container/kit.go +++ b/internal/container/kit.go @@ -91,6 +91,11 @@ func StartKit(ctx context.Context, cli *client.Client, s KitSpec) (string, error return fail(fmt.Errorf("write kit file %s: %w", f.Path, err)) } } + for _, f := range s.Launch.Context { + if err := writeKitFile(ctx, cli, created.ID, "0", f.Path, f.Content, f.Mode, nil); err != nil { + return fail(fmt.Errorf("write agent context %s: %w", f.Path, err)) + } + } for i, h := range s.Launch.Startup { if err := runHook(ctx, cli, created.ID, h); err != nil { return fail(fmt.Errorf("startup hook %d: %w", i+1, err)) diff --git a/internal/infrastructure/kit/launch.go b/internal/infrastructure/kit/launch.go index 7f0e210e0..168d88069 100644 --- a/internal/infrastructure/kit/launch.go +++ b/internal/infrastructure/kit/launch.go @@ -3,6 +3,8 @@ package kit import ( "crypto/sha256" "encoding/hex" + "fmt" + "path" "regexp" "slices" "strings" @@ -17,8 +19,11 @@ const ( // DefaultHarnessUser is the Kit agent user: the harness runs as it when // the image names no user, or names root. DefaultHarnessUser = "1000" - // WorkspaceDir is where a Kit container mounts the task worktree. - WorkspaceDir = "/workspace" + // WorkspaceDir is where a Kit container mounts the task worktree. Its + // parent holds the agent-context profile: beside the worktree, not in + // it, and below the filesystem root, which some CLIs stop short of when + // they look upward for instruction files. + WorkspaceDir = "/archie/workspace" installHookUser = "0" ) @@ -68,6 +73,9 @@ type Launch struct { Install []Hook Startup []Hook Files []spec.File + // Context is the agent-context profile and the inline bodies it points + // at, written as root so a run can read but not change them. + Context []spec.File Volumes []Volume } @@ -123,9 +131,40 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { for _, v := range volumes { l.Volumes = append(l.Volumes, Volume{Name: volumeName(params.Execution, v.Path), Path: v.Path}) } + if l.Context, err = contextFiles(p); err != nil { + return Launch{}, err + } return l, nil } +// contextFiles materializes agent-context@1: the workload's profile file +// beside the workspace, never in it, carrying an index of each Kit's body. +// A staged body is pointed at where the image holds it; an inline one is +// written to its own file beside the profile. The profile never inlines a +// Kit's text, so stacking Kits does not grow what the agent always loads. +func contextFiles(p *Plan) ([]spec.File, error) { + ac, err := spec.AgentContextOf(p.Capabilities) + if err != nil || ac == nil || ac.Filename == "" { + return nil, err + } + root := path.Dir(WorkspaceDir) + var files []spec.File + var index strings.Builder + index.WriteString("# Archie\n\nArchie runs this environment. Each task's rules are in its prompt.\n") + for i, src := range p.ContextSources { + if i == 0 { + index.WriteString("\n## Kits\n\nRead a Kit's guidance when its tools are relevant.\n\n") + } + at := src.Path + if at == "" { + at = path.Join(root, "archie-kit-context", fmt.Sprintf("%d.md", i+1)) + files = append(files, spec.File{Path: at, Content: src.Content, Mode: "0644"}) + } + fmt.Fprintf(&index, "- `%s`: %s\n", src.Reference, at) + } + return append(files, spec.File{Path: path.Join(root, path.Base(ac.Filename)), Content: index.String(), Mode: "0644"}), nil +} + func harnessUser(imageUser string) string { name, _, _ := strings.Cut(imageUser, ":") if name == "" || name == "root" || name == "0" { diff --git a/internal/infrastructure/kit/launch_test.go b/internal/infrastructure/kit/launch_test.go index b6fc47ee4..6d11c4273 100644 --- a/internal/infrastructure/kit/launch_test.go +++ b/internal/infrastructure/kit/launch_test.go @@ -71,7 +71,7 @@ func TestAssembleHarness(t *testing.T) { } for _, want := range []string{ "PATH=/usr/local/bin:/usr/bin:/bin", "IMAGE_ONLY=kept", - "WORKSPACE_DIR=/workspace", + "WORKSPACE_DIR=" + WorkspaceDir, "HTTPS_PROXY=http://archie:tok@archie-egress:3128", "SSL_CERT_FILE=/etc/archie/ca.pem", "EXAMPLE_KEY=archie-proxy-managed", @@ -111,7 +111,7 @@ func TestAssembleHooks(t *testing.T) { if install.User != "0" { t.Errorf("install hook user %q, want root by default", install.User) } - if !slices.Contains(install.Env, "WORKSPACE_DIR=/workspace") || !slices.Contains(install.Env, "HTTPS_PROXY=http://archie:tok@archie-egress:3128") { + if !slices.Contains(install.Env, "WORKSPACE_DIR="+WorkspaceDir) || !slices.Contains(install.Env, "HTTPS_PROXY=http://archie:tok@archie-egress:3128") { t.Errorf("install env %v, want its declared variable and the egress baseline", install.Env) } warm := l.Startup[0] @@ -155,3 +155,66 @@ func TestAssembleVolumesArePerExecution(t *testing.T) { } } } + +func TestAssembleAgentContextProfile(t *testing.T) { + assemble := func(t *testing.T, contextYAML string) Launch { + t.Helper() + p, err := admit(t, spec.Contribution{Reference: "registry/team-set@sha256:ab", Descriptor: descriptor(t, spec.KindWorkload, sessions+contextYAML)}) + if err != nil { + t.Fatalf("admit: %v", err) + } + l, err := Assemble(p, image, LaunchParams{Execution: "exec-1"}) + if err != nil { + t.Fatalf("Assemble: %v", err) + } + return l + } + byPath := func(files []spec.File) map[string]string { + out := map[string]string{} + for _, f := range files { + out[f.Path] = f.Content + } + return out + } + + t.Run("a staged body is indexed where the image holds it", func(t *testing.T) { + files := byPath(assemble(t, ` + - type: com.docker.sandbox/agent-context@1 + config: {filename: CLAUDE.md, contentFile: /usr/share/sandbox/kit/team/context.md}`).Context) + profile, ok := files["/archie/CLAUDE.md"] + if !ok || len(files) != 1 { + t.Fatalf("context files = %v, want only the profile beside the workspace", files) + } + if !strings.Contains(profile, "registry/team-set@sha256:ab") || !strings.Contains(profile, "/usr/share/sandbox/kit/team/context.md") { + t.Fatalf("profile does not index the Kit's staged body:\n%s", profile) + } + }) + + t.Run("an inline body gets its own file the profile points at", func(t *testing.T) { + files := byPath(assemble(t, ` + - type: com.docker.sandbox/agent-context@1 + config: {filename: AGENTS.md, content: "Use motd."}`).Context) + profile := files["/archie/AGENTS.md"] + var body string + for path, content := range files { + if path != "/archie/AGENTS.md" { + body = path + if content != "Use motd." { + t.Fatalf("%s = %q, want the inline body", path, content) + } + } + } + if body == "" || strings.HasPrefix(body, WorkspaceDir+"/") || !strings.Contains(profile, body) { + t.Fatalf("files %v: want the inline body outside the workspace and indexed by the profile", files) + } + if strings.Contains(profile, "Use motd.") { + t.Fatal("the profile inlines a Kit's body; it must only point at it") + } + }) + + t.Run("no profile filename materializes nothing", func(t *testing.T) { + if files := assemble(t, "").Context; len(files) != 0 { + t.Fatalf("context files = %v, want none", files) + } + }) +}