Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,21 @@ func writeMarker(path, ref, short string) error {
current = markerRE.ReplaceAllString(current, block)
next = []byte(current)
} else {
// Insert at the top of the body (after any YAML frontmatter).
// All other user content is preserved byte-for-byte below.
// Append at the bottom of the file so user persona sections
// (e.g. SOUL.md identity, AGENTS.md workspace instructions)
// stay prominent at the top. The Pilot directive is placed
// after all user content and after any YAML frontmatter.
frontmatter, body := splitFrontmatter(current)
body = strings.TrimRight(body, "\n")
sep := ""
if frontmatter != "" && !strings.HasSuffix(frontmatter, "\n") {
sep = "\n"
}
bodySep := ""
if body != "" && !strings.HasPrefix(body, "\n") {
bodySep = "\n"
if body != "" {
bodySep = "\n\n"
}
next = []byte(frontmatter + sep + block + bodySep + body)
next = []byte(frontmatter + sep + body + bodySep + block)
}
}

Expand Down
34 changes: 12 additions & 22 deletions zz_skillinject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ func TestMarker_PreservesUserContent(t *testing.T) {
}

got := mustRead(t, hb)
// Marker now lives at TOP of body (not at bottom) so it dominates
// any competing first-action instructions later in the file. User
// content is preserved BELOW the marker.
if !strings.HasPrefix(got, "<!-- pilot:begin v=1 hash=") {
t.Errorf("marker block not at top of file:\n%s", got)
// Marker lives at the BOTTOM of the file so user persona sections
// stay prominent at the top. User content is preserved ABOVE the marker.
if !strings.HasSuffix(strings.TrimSpace(got), "<!-- pilot:end -->") {
t.Errorf("marker block not at bottom of file:\n%s", got)
}
if !strings.Contains(got, pre) {
t.Errorf("user content lost from file:\n%s", got)
Expand Down Expand Up @@ -203,8 +202,8 @@ func TestMarker_PreservesUserContent(t *testing.T) {
}
}

// Marker is injected at the top of the body (after any frontmatter) and
// preserves all existing user content below.
// Marker is appended at the bottom of the file (after all user content and
// after any YAML frontmatter) preserving user persona sections at the top.
func TestMarker_PreservesUserContentBelow(t *testing.T) {
t.Parallel()
home := t.TempDir()
Expand Down Expand Up @@ -239,9 +238,9 @@ Some other content stays.
t.Errorf("user content lost (%q):\n%s", want, got)
}
}
// Marker is present and at the top.
if !strings.HasPrefix(got, "<!-- pilot:begin") {
t.Errorf("marker not at top of body:\n%s", got[:min(150, len(got))])
// Marker is present and at the bottom.
if !strings.HasSuffix(strings.TrimSpace(got), "<!-- pilot:end -->") {
t.Errorf("marker not at bottom of file:\n%s", got[len(got)-min(150,len(got)):])
}
}

Expand Down Expand Up @@ -271,16 +270,7 @@ func TestMarker_PreservesYAMLFrontmatter(t *testing.T) {
if !strings.Contains(got, "User body.") {
t.Errorf("body content lost")
}
// Marker should be AFTER frontmatter, BEFORE body content.
idxFrontmatterEnd := strings.Index(got, "---\n") + len("---\n")
// Walk past possible additional ---\n
for strings.HasPrefix(got[idxFrontmatterEnd:], "---\n") || strings.HasPrefix(got[idxFrontmatterEnd:], "\n") {
if strings.HasPrefix(got[idxFrontmatterEnd:], "---\n") {
idxFrontmatterEnd += len("---\n")
} else {
break
}
}
// Marker should be AFTER frontmatter AND AFTER body content.
idxMarker := strings.Index(got, "<!-- pilot:begin")
idxBody := strings.Index(got, "# PicoClaw")
if idxMarker < 0 {
Expand All @@ -289,8 +279,8 @@ func TestMarker_PreservesYAMLFrontmatter(t *testing.T) {
if idxBody < 0 {
t.Fatal("body header missing")
}
if idxMarker > idxBody {
t.Errorf("marker should be ABOVE body but is below (marker=%d body=%d)", idxMarker, idxBody)
if idxMarker < idxBody {
t.Errorf("marker should be BELOW body but is above (marker=%d body=%d)", idxMarker, idxBody)
}
}

Expand Down
Loading