Repository navigation
feat(#6681): inject harness skills into agent frontmatter for always-on activation #6859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ddeaeca
feat(#6681): inject harness skills into agent frontmatter for always-…
fullsend-ai-coder[bot] 89e6c6f
fix: address review feedback on PR #6859
fullsend-ai-coder[bot] 7c0e284
fix: address review feedback on PR #6859
fullsend-ai-coder[bot] 1b5de86
fix: block scalar false match, multi-line flow corruption, skill name…
ascerra 4b7c78a
fix: improve error messages in frontmatter skill injection
fullsend-ai-coder[bot] dbffe21
fix: use uppercase BOM hex escapes to match pi_agent.go convention
fullsend-ai-coder[bot] a1f19db
fix: flow-style misclassification, shared isFence, CRLF handling, tem…
fullsend-ai-coder[bot] b95bc4d
fix: flow-comment bracket scan and YAML anchor duplication
ascerra addafdf
fix: handle blank lines in flow-style skills
ascerra 7419758
fix: validate existing frontmatter skills
ascerra 80ffc8a
fix: parse frontmatter skills with yaml nodes
ascerra 06d59a5
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra 2900ff1
fix: validate rewritten frontmatter
ascerra 676f087
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra acdc7f8
fix: preserve merged frontmatter skills
ascerra de0dbbd
fix: harden frontmatter skill injection
ascerra 7aee05d
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra 1600566
fix: handle null frontmatter documents
ascerra 73608be
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra e7bd3c2
docs: qualify skill activation claims
ascerra b2616c4
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra 4700af5
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra 1552ae4
fix: restore upload workaround and add round-trip safety check
ascerra e7f5ece
fix: make safety assertion fail closed on unmarshal error
ascerra cab47ce
fix: stop validating existing skill names in frontmatter
ascerra d36c1bf
Merge remote-tracking branch 'origin/main' into agent/6681-inject-fro…
ascerra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // frontmatterSkills is the subset of agent YAML frontmatter used for | ||
| // skill injection. We unmarshal only the `skills` field to avoid | ||
| // disturbing any other frontmatter keys. | ||
| type frontmatterSkills struct { | ||
| Skills []string `yaml:"skills,omitempty"` | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // isFrontmatterFence reports whether line is a YAML frontmatter fence | ||
| // ("---", possibly followed by trailing whitespace or CRLF). Shared by | ||
| // parsePiAgent and injectFrontmatterSkills. | ||
| func isFrontmatterFence(line []byte) bool { | ||
| return strings.TrimRight(string(line), " \t\r\n") == "---" | ||
| } | ||
|
|
||
| // isValidSkillName reports whether name contains only characters safe for | ||
| // use as a bare YAML scalar: alphanumeric, hyphens, underscores, dots. | ||
| func isValidSkillName(name string) bool { | ||
| if name == "" { | ||
| return false | ||
| } | ||
| for _, c := range name { | ||
| if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.') { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // rewriteFrontmatterSkills updates the skills sequence in a parsed YAML | ||
| // document and marshals the complete frontmatter back to bytes. Using the | ||
| // YAML node tree keeps flow-style continuations, quoted keys, and spacing | ||
| // variants out of the reconstruction logic. | ||
| func rewriteFrontmatterSkills(frontBytes []byte, existing, added []string, eol string) ([]byte, error) { | ||
| var doc yaml.Node | ||
| if err := yaml.Unmarshal(frontBytes, &doc); err != nil { | ||
| return nil, fmt.Errorf("parsing frontmatter: %w", err) | ||
| } | ||
| if len(doc.Content) != 1 || doc.Content[0].Kind != yaml.MappingNode { | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| return nil, fmt.Errorf("frontmatter must be a YAML mapping") | ||
| } | ||
|
|
||
| mapping := doc.Content[0] | ||
| skillsIdx := -1 | ||
| for i := 0; i+1 < len(mapping.Content); i += 2 { | ||
| if mapping.Content[i].Value == "skills" { | ||
| // Keep the last occurrence to match YAML's last-key-wins behavior. | ||
| skillsIdx = i | ||
| } | ||
| } | ||
|
|
||
| var sequence *yaml.Node | ||
| if skillsIdx >= 0 { | ||
| value := mapping.Content[skillsIdx+1] | ||
| if value.Kind == yaml.ScalarNode && value.Tag == "!!null" { | ||
| sequence = &yaml.Node{ | ||
| Kind: yaml.SequenceNode, | ||
| Tag: "!!seq", | ||
| HeadComment: value.HeadComment, | ||
| LineComment: value.LineComment, | ||
| } | ||
| mapping.Content[skillsIdx+1] = sequence | ||
| } else if value.Kind != yaml.SequenceNode { | ||
| return nil, fmt.Errorf("skills field must be a YAML list") | ||
| } else { | ||
| sequence = value | ||
| } | ||
| sequence.Style = 0 // Always emit the injected result in block form. | ||
| } else { | ||
| sequence = &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"} | ||
| for _, name := range existing { | ||
| sequence.Content = append(sequence.Content, &yaml.Node{ | ||
| Kind: yaml.ScalarNode, | ||
| Tag: "!!str", | ||
| Value: name, | ||
| }) | ||
| } | ||
| mapping.Content = append(mapping.Content, | ||
| &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "skills"}, | ||
| sequence, | ||
| ) | ||
| } | ||
|
|
||
| for _, name := range added { | ||
| sequence.Content = append(sequence.Content, &yaml.Node{ | ||
| Kind: yaml.ScalarNode, | ||
| Tag: "!!str", | ||
| Value: name, | ||
| }) | ||
| } | ||
|
|
||
| var marshaledBuffer bytes.Buffer | ||
| encoder := yaml.NewEncoder(&marshaledBuffer) | ||
| encoder.SetIndent(2) | ||
| err := encoder.Encode(&doc) | ||
| closeErr := encoder.Close() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshaling frontmatter: %w", err) | ||
| } | ||
| if closeErr != nil { | ||
| return nil, fmt.Errorf("closing frontmatter encoder: %w", closeErr) | ||
| } | ||
| marshaled := marshaledBuffer.Bytes() | ||
| if eol == "\r\n" { | ||
| marshaled = bytes.ReplaceAll(marshaled, []byte("\n"), []byte("\r\n")) | ||
| } | ||
| var validated yaml.Node | ||
| if err := yaml.Unmarshal(marshaled, &validated); err != nil { | ||
| return nil, fmt.Errorf("validating rewritten frontmatter: %w", err) | ||
| } | ||
| return marshaled, nil | ||
| } | ||
|
|
||
| // injectFrontmatterSkills adds skill names derived from skillDirs into the | ||
| // agent definition's YAML frontmatter `skills:` section. Existing entries | ||
| // are preserved; new names are appended with deduplication by basename. | ||
| // If the agent has no frontmatter, one is created. If skillDirs is empty, | ||
| // the data is returned unchanged. | ||
| // | ||
| // This ensures harness-listed skills reliably activate: Claude Code loads | ||
| // skills listed in the agent frontmatter without requiring an explicit | ||
| // Skill tool call in the prompt body. | ||
| func injectFrontmatterSkills(data []byte, skillDirs []string) ([]byte, error) { | ||
| if len(skillDirs) == 0 { | ||
| return data, nil | ||
| } | ||
|
|
||
| // Collect basenames from skill directories — these are the names | ||
| // the runtime uses to identify skills in the sandbox. | ||
| seen := make(map[string]bool, len(skillDirs)) | ||
| newNames := make([]string, 0, len(skillDirs)) | ||
| for _, d := range skillDirs { | ||
| if d == "" { | ||
| continue | ||
| } | ||
| name := filepath.Base(d) | ||
| if !isValidSkillName(name) { | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| return nil, fmt.Errorf("invalid skill name %q from %q: must match [a-zA-Z0-9._-]+", name, d) | ||
| } | ||
| if seen[name] { | ||
| continue | ||
| } | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| seen[name] = true | ||
| newNames = append(newNames, name) | ||
| } | ||
| if len(newNames) == 0 { | ||
| return data, nil | ||
| } | ||
| sort.Strings(newNames) | ||
|
|
||
| content := bytes.TrimPrefix(data, []byte("\xEF\xBB\xBF")) | ||
|
|
||
| // Detect line ending style so injected lines match existing content. | ||
| eol := "\n" | ||
| if bytes.Contains(content, []byte("\r\n")) { | ||
| eol = "\r\n" | ||
| } | ||
|
|
||
| // Check for existing frontmatter. | ||
| lines := bytes.SplitAfter(content, []byte("\n")) | ||
| if bytes.HasPrefix(content, []byte("---")) && !isFrontmatterFence(lines[0]) { | ||
| return nil, fmt.Errorf("agent definition: first line starts with --- but is not a frontmatter fence: %q", strings.TrimRight(string(lines[0]), "\r\n")) | ||
| } | ||
|
|
||
| hasFrontmatter := len(lines) > 0 && isFrontmatterFence(lines[0]) | ||
|
|
||
| if !hasFrontmatter { | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| // No frontmatter — create one with just the skills list. | ||
| var buf bytes.Buffer | ||
| fmt.Fprintf(&buf, "---%s", eol) | ||
| fmt.Fprintf(&buf, "skills:%s", eol) | ||
| for _, name := range newNames { | ||
| fmt.Fprintf(&buf, " - %s%s", name, eol) | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| } | ||
| fmt.Fprintf(&buf, "---%s", eol) | ||
| buf.Write(content) | ||
| return buf.Bytes(), nil | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // Find the closing fence. | ||
| var frontBytes []byte | ||
| closingIdx := -1 | ||
| for i := 1; i < len(lines); i++ { | ||
| if isFrontmatterFence(lines[i]) { | ||
| frontBytes = bytes.Join(lines[1:i], nil) | ||
| closingIdx = i | ||
| break | ||
| } | ||
| } | ||
| if closingIdx < 0 { | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| return nil, fmt.Errorf("unterminated frontmatter") | ||
| } | ||
|
|
||
| // Parse existing skills from frontmatter. | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| var fm frontmatterSkills | ||
| if err := yaml.Unmarshal(frontBytes, &fm); err != nil { | ||
| // When the skills field has an unexpected type (e.g., scalar | ||
| // string instead of a list), produce a more specific message. | ||
| var probe struct { | ||
| Skills interface{} `yaml:"skills"` | ||
| } | ||
| if yaml.Unmarshal(frontBytes, &probe) == nil && probe.Skills != nil { | ||
| if _, isList := probe.Skills.([]interface{}); !isList { | ||
| return nil, fmt.Errorf("skills field must be a YAML list: %w", err) | ||
| } | ||
| } | ||
| return nil, fmt.Errorf("parsing frontmatter: %w", err) | ||
| } | ||
|
|
||
| // Deduplicate: build a set of existing skill names. | ||
| existing := make(map[string]bool, len(fm.Skills)) | ||
| for _, s := range fm.Skills { | ||
| if !isValidSkillName(s) { | ||
| return nil, fmt.Errorf("invalid existing skill name %q: must match [a-zA-Z0-9._-]+", s) | ||
| } | ||
| existing[s] = true | ||
| } | ||
|
|
||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| // Append only new names that are not already present. | ||
| added := make([]string, 0, len(newNames)) | ||
| for _, name := range newNames { | ||
| if !existing[name] { | ||
| existing[name] = true | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| added = append(added, name) | ||
| } | ||
| } | ||
|
|
||
| if len(added) == 0 { | ||
|
ascerra marked this conversation as resolved.
Resolved
|
||
| // All skills already present — return BOM-stripped content for consistency | ||
| // with the injection path (which always operates on BOM-stripped data). | ||
| return content, nil | ||
| } | ||
|
|
||
| updatedFrontmatter, err := rewriteFrontmatterSkills(frontBytes, fm.Skills, added, eol) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var result bytes.Buffer | ||
| result.Write(lines[0]) | ||
| result.Write(updatedFrontmatter) | ||
|
|
||
| // Write the closing fence and everything after it. | ||
| for i := closingIdx; i < len(lines); i++ { | ||
| result.Write(lines[i]) | ||
| } | ||
|
|
||
| return result.Bytes(), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.