|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + "github.com/shieldnet-360/secure-vibe/internal/proposal" |
| 12 | +) |
| 13 | + |
| 14 | +// upstreamRepo is where a reviewed knowledge proposal is contributed. |
| 15 | +const upstreamRepo = "ShieldNet-360/secure-vibe" |
| 16 | + |
| 17 | +// contributeSkillCmd is the review half of the knowledge LEARN loop. An |
| 18 | +// agent records proposals via the propose_skill_update MCP tool (an |
| 19 | +// inert append to .secure-vibe/skill-proposals.jsonl); a maintainer uses |
| 20 | +// this command to read them, and — when one holds up — to turn it into a |
| 21 | +// paste-ready upstream contribution. |
| 22 | +// |
| 23 | +// The command deliberately does NOT edit any SKILL.md, sign anything, or |
| 24 | +// open a PR. Applying a proposal is a human act: edit skills/<id>/SKILL.md |
| 25 | +// by hand, then re-run the signing pipeline (validate → regenerate → |
| 26 | +// manifest compute → sign). That human review + re-sign is the trust gate |
| 27 | +// that keeps a model — steerable by prompt injection — from mutating |
| 28 | +// signed knowledge. |
| 29 | +func contributeSkillCmd() *cobra.Command { |
| 30 | + var dir, show, upstream string |
| 31 | + c := &cobra.Command{ |
| 32 | + Use: "skill", |
| 33 | + Short: "Review agent-recorded skill-update proposals (knowledge LEARN loop)", |
| 34 | + Long: `Review the skill-knowledge proposals an agent recorded via the |
| 35 | +propose_skill_update MCP tool. Proposals are inert suggestions in |
| 36 | +.secure-vibe/skill-proposals.jsonl — this command reads them; it never edits |
| 37 | +or signs a skill. |
| 38 | +
|
| 39 | + secure-vibe contribute skill # list pending proposals |
| 40 | + secure-vibe contribute skill --show <id> # full detail for one proposal |
| 41 | + secure-vibe contribute skill --upstream <id> # a paste-ready upstream contribution |
| 42 | +
|
| 43 | +To adopt a proposal: edit skills/<skill-id>/SKILL.md by hand, then re-run the |
| 44 | +signing pipeline. To share it upstream, use --upstream and open the printed URL.`, |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + path, err := proposal.PathFor(dir) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + w := cmd.OutOrStdout() |
| 51 | + switch { |
| 52 | + case strings.TrimSpace(show) != "": |
| 53 | + p, err := proposal.Get(path, show) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if p == nil { |
| 58 | + return fmt.Errorf("no proposal matching %q in %s", show, path) |
| 59 | + } |
| 60 | + printProposalDetail(w, p) |
| 61 | + return nil |
| 62 | + case strings.TrimSpace(upstream) != "": |
| 63 | + p, err := proposal.Get(path, upstream) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + if p == nil { |
| 68 | + return fmt.Errorf("no proposal matching %q in %s", upstream, path) |
| 69 | + } |
| 70 | + printUpstreamBundle(w, p) |
| 71 | + return nil |
| 72 | + default: |
| 73 | + return listProposals(w, path) |
| 74 | + } |
| 75 | + }, |
| 76 | + } |
| 77 | + c.Flags().StringVar(&dir, "dir", "", "project directory holding .secure-vibe/skill-proposals.jsonl (default: cwd)") |
| 78 | + c.Flags().StringVar(&show, "show", "", "print full detail for one proposal id") |
| 79 | + c.Flags().StringVar(&upstream, "upstream", "", "print a paste-ready upstream contribution for one proposal id") |
| 80 | + return c |
| 81 | +} |
| 82 | + |
| 83 | +func listProposals(w io.Writer, path string) error { |
| 84 | + all, err := proposal.Load(path) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + if len(all) == 0 { |
| 89 | + fmt.Fprintf(w, "No skill proposals yet (%s).\nAgents record them via the propose_skill_update MCP tool.\n", path) |
| 90 | + return nil |
| 91 | + } |
| 92 | + fmt.Fprintf(w, "%d skill proposal(s) in %s:\n", len(all), path) |
| 93 | + for _, p := range all { |
| 94 | + fmt.Fprintf(w, " %s [%s] %s\n %s\n", p.ID, p.Kind, p.SkillID, oneLine(p.Claim, 100)) |
| 95 | + } |
| 96 | + fmt.Fprintf(w, "\nInspect one: secure-vibe contribute skill --show <id>\n") |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func printProposalDetail(w io.Writer, p *proposal.Proposal) { |
| 101 | + fmt.Fprintf(w, "id: %s\n", p.ID) |
| 102 | + fmt.Fprintf(w, "skill: %s\n", p.SkillID) |
| 103 | + fmt.Fprintf(w, "kind: %s\n", p.Kind) |
| 104 | + fmt.Fprintf(w, "recorded: %s (by %s)\n", p.Recorded, p.Source) |
| 105 | + fmt.Fprintf(w, "\nclaim:\n %s\n", p.Claim) |
| 106 | + fmt.Fprintf(w, "\nevidence:\n %s\n", p.Evidence) |
| 107 | + if strings.TrimSpace(p.SuggestedText) != "" { |
| 108 | + fmt.Fprintf(w, "\nsuggested text:\n %s\n", p.SuggestedText) |
| 109 | + } |
| 110 | + fmt.Fprintf(w, "\nTo adopt: edit skills/%s/SKILL.md, then re-run the signing pipeline.\n", p.SkillID) |
| 111 | + fmt.Fprintf(w, "To share: secure-vibe contribute skill --upstream %s\n", p.ID) |
| 112 | +} |
| 113 | + |
| 114 | +// printUpstreamBundle emits a Markdown block a maintainer can paste into |
| 115 | +// an upstream issue/PR, plus a prefilled "new issue" URL. It never opens |
| 116 | +// anything — publishing is the human's explicit act. |
| 117 | +func printUpstreamBundle(w io.Writer, p *proposal.Proposal) { |
| 118 | + title := fmt.Sprintf("skill(%s): %s knowledge — %s", p.SkillID, p.Kind, oneLine(p.Claim, 60)) |
| 119 | + var b strings.Builder |
| 120 | + fmt.Fprintf(&b, "## Proposed skill update: `%s`\n\n", p.SkillID) |
| 121 | + fmt.Fprintf(&b, "**Kind:** %s\n\n", p.Kind) |
| 122 | + fmt.Fprintf(&b, "**Claim**\n\n%s\n\n", p.Claim) |
| 123 | + fmt.Fprintf(&b, "**Evidence**\n\n%s\n\n", p.Evidence) |
| 124 | + if strings.TrimSpace(p.SuggestedText) != "" { |
| 125 | + fmt.Fprintf(&b, "**Suggested wording for `skills/%s/SKILL.md`**\n\n%s\n\n", p.SkillID, p.SuggestedText) |
| 126 | + } |
| 127 | + fmt.Fprintf(&b, "_Recorded locally by %s on %s (proposal %s). A maintainer should verify the evidence, edit `skills/%s/SKILL.md`, and re-sign._\n", |
| 128 | + p.Source, p.Recorded, p.ID, p.SkillID) |
| 129 | + body := b.String() |
| 130 | + |
| 131 | + issueURL := fmt.Sprintf("https://github.com/%s/issues/new?title=%s&body=%s&labels=%s", |
| 132 | + upstreamRepo, url.QueryEscape(title), url.QueryEscape(body), url.QueryEscape("skill-knowledge")) |
| 133 | + |
| 134 | + fmt.Fprintf(w, "%s\n", body) |
| 135 | + fmt.Fprintf(w, "----\nPaste the block above into a PR, or open a prefilled issue:\n\n%s\n", issueURL) |
| 136 | +} |
| 137 | + |
| 138 | +// oneLine collapses whitespace and truncates s to n runes for compact |
| 139 | +// listings and titles. |
| 140 | +func oneLine(s string, n int) string { |
| 141 | + s = strings.Join(strings.Fields(s), " ") |
| 142 | + if r := []rune(s); len(r) > n { |
| 143 | + return string(r[:n-1]) + "…" |
| 144 | + } |
| 145 | + return s |
| 146 | +} |
0 commit comments