feat(kit): materialize the agent-context profile beside the workspace

agent-context@1 was listed as supported, but a Kit's contributed guidance
never reached the agent. At container create archie now writes the
workload's profile file (CLAUDE.md, AGENTS.md, ...) beside the workspace
with a Kits index: a staged body is pointed at where the image holds it,
an inline one gets its own file the index points at. The profile never
inlines a Kit's text. The files are written as root, so a run can read
but not change them, and are rewritten on every create.

The workspace moves to /archie/workspace so the profile sits in a real
parent directory rather than /, which some CLIs stop short of when they
look upward for instruction files.

Part of archie-core-egkf.6.
This commit is contained in:
sam committed 2026-09-28 02:12:29 +10:00
1 parent 70f9b1b87c
commit 525dbb14a7
3 files changed
+111 -4

No files matched your search

+5
View File
@@ -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))
+41 -2
View File
@@ -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" {
+65 -2
View File
@@ -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)
}
})
}