Skip to content

Commit fdbd77b

Browse files
oldsjclaude
andcommitted
Agent dotfiles: lay a Session's bundle out read-only before turns
CreateSessionRequest.dotfiles names a bundle of owner-level agent files (AGENTS.md, CLAUDE.md, skills, subagents, commands) and its digest. Create requires the url's origin to carry one of the Session's credentials, which authorizes the download, and the dotfiles join the request identity. TaskStoreService.GetWorkspace returns the url, digest and that credential's header name. The Claude and Codex adapters wrap their runner with a dotfiles layer. It downloads the bundle with a placeholder header the gateway replaces, verifies the digest and limits, keeps the verified copy under /data/.kagent/dotfiles, and lays the files out read-only in the CLI's user configuration directory: again on every process start, and before any turn that finds an entry missing. Dotfiles are context, not a precondition: a failure is logged and the turn runs without them. A fork re-lays its source's bundle from its restored data. Codex's skills reconcile now removes read-only trees. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1aaa678 commit fdbd77b

36 files changed

Lines changed: 1895 additions & 168 deletions

File tree

‎.github/workflows/mainloop-fork-ci.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
- name: Build everything
7575
run: go build ./...
7676
- name: Test runtime and policy fixtures
77-
run: go test -count=1 ./adk/pkg/taskstore ./api/workspace ./core/internal/egress ./core/internal/substrate ./core/internal/service/taskstore
77+
run: go test -count=1 ./adk/pkg/taskstore ./api/workspace ./api/dotfiles ./core/internal/egress ./core/internal/substrate ./core/internal/service/taskstore
7878
# TestSandboxTemplateCatalog and TestEgressPolicyCatalog in grpcserver
7979
# start envtest, which needs these binaries.
8080
- name: Install envtest binaries
@@ -95,7 +95,7 @@ jobs:
9595
- name: Test fake native consumers
9696
run: >-
9797
go test -count=1 -skip '^TestPinnedCodexAcceptsGeneratedConfiguration$'
98-
./harness/claude/... ./harness/codex/... ./harness/runtime/...
98+
./harness/claude/... ./harness/codex/... ./harness/runtime/... ./harness/internal/utils
9999
100100
helm-unit-tests:
101101
name: Helm chart unit tests

‎docs/architecture/credential-injection.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ to that exact origin and header. Header names are case-insensitive. Duplicate
413413
origin/header bindings and collisions with revision credentials are rejected.
414414
References persist in `Session.credentials`; retrying a request ID with changed
415415
references conflicts. Checkpoint forks inherit no Session credentials.
416+
A Session's agent dotfiles bundle is downloaded with the Session credential bound
417+
to its url's origin; see [Agent dotfiles](runtime-and-lifecycle.md#agent-dotfiles).
416418

417419
The controller applies them where it applies every Actor's egress policy: the
418420
policy for a Session's Actor is the revision's effective access (pinned inputs

‎docs/architecture/runtime-and-lifecycle.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,51 @@ names the cause (ref not found, authentication, unreachable host, timeout, inval
632632
name), and the next turn retries. The bootstrapper also enforces the Harness origins, so
633633
a Session row that bypassed create-time validation still cannot reach another host.
634634

635+
## Agent dotfiles
636+
637+
`CreateSessionRequest.dotfiles` (`url`, `digest`, and `repository`, `commit`, `path`
638+
for provenance) names a bundle of owner-level agent files to lay out in the CLI's
639+
user configuration directory: `AGENTS.md`, `CLAUDE.md`, `skills/<name>/...`,
640+
`agents/<name>.md` and `commands/<name>.md`. The bundle is an uncompressed tar of
641+
regular files. Create rejects a `url` that is not plain HTTP(S) without user info,
642+
query or fragment, or whose origin carries none of the Session's credentials: that
643+
credential authorizes the download, so no new grant or egress origin is needed. The
644+
dotfiles are part of the request identity, so retrying a request ID with another
645+
bundle conflicts. Checkpoint forks get none of their own (see below).
646+
647+
`TaskStoreService.GetWorkspace` returns `dotfiles { url, digest, header }`, where
648+
`header` is the matching credential's header name. Before a turn, the Codex and
649+
Claude adapters' dotfiles layer GETs the url with that header set to a placeholder,
650+
which the gateway replaces; redirects are not followed. It verifies the digest and
651+
the limits (32 MiB bundle, 4000 files, 2 MiB per file, 16 MiB in all), refuses any
652+
entry that is not a regular file at one of the paths above, and executes nothing.
653+
The verified bundle and a manifest of what was written are kept in
654+
`/data/.kagent/dotfiles`.
655+
656+
| Bundle path | Claude (`/data/claude`) | Codex (`/data/codex`) |
657+
| ------------------- | --------------------------------------------------------- | --------------------- |
658+
| `AGENTS.md` | `AGENTS.md`, and `CLAUDE.md` with `@AGENTS.md` if absent | `AGENTS.md` |
659+
| `CLAUDE.md` | `CLAUDE.md` | not mapped |
660+
| `skills/<name>/...` | `skills/<name>/...` | `skills/<name>/...` |
661+
| `agents/<name>.md` | `agents/<name>.md` | not mapped |
662+
| `commands/<name>.md`| `commands/<name>.md` | not mapped |
663+
664+
Files are 0444, or 0555 when the tar marks them executable, and directories inside
665+
an entry 0555; the configuration directory and the shared `skills`, `agents` and
666+
`commands` directories stay writable. The agent runs as the same user, so the
667+
permissions are not the guarantee: each runtime process removes the manifest's
668+
entries and lays the bundle out again before its first turn, so edits never persist,
669+
and again before any turn that finds an entry missing (Codex setup re-materializes
670+
its skills root). A Codex skill the compiler selected wins a name clash. A symlink
671+
in place of a shared directory is replaced, never written through.
672+
673+
Dotfiles are context, not a precondition. A failed read, download or verification
674+
is logged, the turn runs without dotfiles, and the next turn tries again. A
675+
checkpoint fork has no delivery of its own: its restored data holds the source's
676+
verified bundle and manifest, so it lays out the source's files again. The Claude
677+
SDK runner's user profile reads its own home, not `/data/claude`, so it does not
678+
see dotfiles.
679+
635680
## Workspace service
636681

637682
`workspace.service_command` optionally declares a workspace service: an argv for a

‎go/adk/pkg/taskstore/store.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ func (s *Store) CompleteWorkspacePreparation(ctx context.Context, request *runti
352352
})
353353
}
354354

355+
// GetDotfiles returns how to fetch this runtime's Session's agent dotfiles
356+
// bundle, or nil when the Session was created without one.
357+
func (s *Store) GetDotfiles(ctx context.Context) (*runtimev1.DotfilesDelivery, error) {
358+
response, err := s.GetWorkspacePreparation(ctx)
359+
if err != nil {
360+
return nil, err
361+
}
362+
return response.GetDotfiles(), nil
363+
}
364+
355365
// SessionID returns the Session this runtime serves, from its local identity.
356366
func (s *Store) SessionID() (string, error) { return s.sessionID() }
357367

‎go/api/dotfiles/dotfiles.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Package dotfiles holds the rules shared by the control plane and the Harness
2+
// runtimes for a Session's agent dotfiles bundle: which Session credential
3+
// authorizes its download, and the limits a bundle must stay within.
4+
package dotfiles
5+
6+
import (
7+
"errors"
8+
"net/url"
9+
"strings"
10+
11+
apiv1 "github.com/kagent-dev/kagent/go/api/gen/kagent/api/v1"
12+
)
13+
14+
// Limits a bundle must stay within. The producer enforces the same ones; the
15+
// runtime checks them again before laying anything out.
16+
const (
17+
MaxBundleBytes = 32 << 20
18+
MaxFiles = 4000
19+
MaxFileBytes = 2 << 20
20+
MaxTotalBytes = 16 << 20
21+
)
22+
23+
// ErrNoCredential means no Session credential is bound to the bundle url's origin.
24+
var ErrNoCredential = errors.New("dotfiles url origin carries none of the Session's credentials")
25+
26+
// Origin is rawURL's "scheme://host[:port]", lower-cased, as a SessionCredential
27+
// names it. Only http and https urls without user info, query or fragment qualify.
28+
func Origin(rawURL string) (string, error) {
29+
u, err := url.Parse(rawURL)
30+
if err != nil {
31+
return "", err
32+
}
33+
if (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil ||
34+
u.RawQuery != "" || u.Fragment != "" || u.Opaque != "" {
35+
return "", errors.New("dotfiles url must be an http or https url without credentials, query or fragment")
36+
}
37+
return strings.ToLower(u.Scheme + "://" + u.Host), nil
38+
}
39+
40+
// Credential returns the Session credential whose origin is the bundle url's.
41+
func Credential(d *apiv1.Dotfiles, credentials []*apiv1.SessionCredential) (*apiv1.SessionCredential, error) {
42+
origin, err := Origin(d.GetUrl())
43+
if err != nil {
44+
return nil, err
45+
}
46+
for _, c := range credentials {
47+
if strings.EqualFold(c.GetOrigin(), origin) {
48+
return c, nil
49+
}
50+
}
51+
return nil, ErrNoCredential
52+
}

‎go/api/dotfiles/dotfiles_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dotfiles
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
apiv1 "github.com/kagent-dev/kagent/go/api/gen/kagent/api/v1"
8+
)
9+
10+
func TestCredentialIsTheOneBoundToTheURLOrigin(t *testing.T) {
11+
credentials := []*apiv1.SessionCredential{
12+
{Origin: "https://api.example.test", Header: "authorization"},
13+
{Origin: "http://mcp.example.test:8000", Header: "x-mcp-token"},
14+
}
15+
c, err := Credential(&apiv1.Dotfiles{Url: "http://MCP.example.test:8000/dotfiles/x"}, credentials)
16+
if err != nil || c.GetHeader() != "x-mcp-token" {
17+
t.Fatalf("Credential = %v, %v", c, err)
18+
}
19+
for _, url := range []string{
20+
"http://mcp.example.test/dotfiles/x", // another port
21+
"https://mcp.example.test:8000/dotfiles/x", // another scheme
22+
"http://other.example.test:8000/dotfiles/x",
23+
} {
24+
if _, err := Credential(&apiv1.Dotfiles{Url: url}, credentials); !errors.Is(err, ErrNoCredential) {
25+
t.Errorf("%s: %v, want no credential", url, err)
26+
}
27+
}
28+
for _, url := range []string{"ftp://mcp.example.test/x", "http://u:p@mcp.example.test:8000/x", "http://mcp.example.test:8000/x?a=1", "http://mcp.example.test:8000/x#f", "/dotfiles/x", ""} {
29+
if _, err := Origin(url); err == nil {
30+
t.Errorf("%q: accepted", url)
31+
}
32+
}
33+
}

‎go/api/gen/kagent/api/v1/common.pb.go‎

Lines changed: 102 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)