mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
* feat(hooks): record Codex hook trust on sync with --trust Codex runs a user-level hook only after its trusted_hash is stored in that home's config.toml, keyed by the hook's position in hooks.json. Only the TUI's /hooks review writes it, so every hook Skillshare adds or moves starts untrusted and codex exec skips it without a word. `hooks sync --trust` (global mode, opt-in) asks each Codex home's `codex app-server` for the current hash of the hooks Skillshare owns and writes trusted_hash through config/batchWrite, the same calls /hooks makes, so the hash is always the one Codex computes. It changes nothing else in config.toml: disabled hooks stay disabled and are reported. Trust it wrote is remembered in the hooks state directory, so when a hook moves or is removed the next --trust sync clears the old position, unless that trust has changed since. Without codex on PATH the hook files are still written and the command fails saying trust was not recorded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(hooks): scope Codex trust to config owner Scope trust selection and saved hashes to the active config owner so a global config cannot trust or reconcile another config’s hooks. Recheck hooks.json after hooks/list to avoid trusting a handler from a stale position, and only report trusted or cleared actions after config/batchWrite succeeds. Keep v1 ownerless hashes cleanup-only so upgraded installs clear stale trust after hook removal without trusting foreign hooks. * fix(hooks): retain parked Codex trust receipts Locate hooks using all records for their canonical file before selecting the active config owner, so identical hooks remain identifiable across shared and symlinked homes. Keep cleanup receipts for parked registrations whose native group still matches, including when duplicate counts make locate ambiguous, while clearing receipts for removed hooks. Add regressions for identical hooks across config owners, symlink aliases, parked-home restoration, ambiguous duplicates, and stale receipt cleanup. * fix(hooks): coalesce symlinked Codex trust homes Canonicalize current-owner and legacy trust receipts so symlink spellings of one hooks.json are processed once. Remove the redundant parked-position branch because the hash-matching fallback below covers it. Add regression coverage for current and legacy receipts. * fix(hooks): coalesce removed Codex hook homes Resolve existing home directories when hook files have been removed so trust receipts for symlink aliases reconcile once. Correct canonical key expectations and exercise coalescing without unsupported ledger migration. * fix(hooks): read reconciled trust fixture into fresh state JSON decoding retains existing map entries, so reusing the seeded receipt map makes the alias appear to survive reconciliation. Decode the persisted state into a new value to verify its actual contents. * feat(hooks): confirm Codex trust before recording it and drop the receipts file `hooks sync --trust` recorded trust for every owned hook without showing what it was trusting, and tracked its own writes in codex-trust.json so it could clear them later. Codex's review exists so that a person sees a command before it runs, and the receipts file was the source of most of the edge cases found in review. Trust is now two steps. A preview asks Codex which owned hooks are new or changed and returns them with their commands and a revision; the write takes that revision and is refused when the hooks changed since. The CLI lists the hooks and asks once, `--yes` skips the question, and without a terminal nothing is trusted unless `--yes` is given. The dashboard gets the same two steps as API routes. Nothing is cleared any more: a leftover entry only matches a hook with the same event, matcher and handler at that position, which is the hook the user already approved. That removes codex-trust.json. The app-server is started without CODEX_HOME for the default home. Codex resolves the symlinks of CODEX_HOME but not of ~/.codex, so a symlinked default home got a trust key the user's own Codex never looks up. A home whose write fails no longer stops the other homes, and the error names which were recorded. A preview releases the hooks operation lock before it waits on Codex, so a slow Codex does not fail other hooks operations. Refs #828 * feat(ui): trust Codex hooks from the Hooks page The dashboard could sync Codex hooks but left them untrusted, so Codex skipped them until the user ran the CLI or approved each one in Codex. The Sync box now offers "Trust Codex hooks" in the global scope when a hook is bound to a Codex target. The dialog shows each hook that needs trust with its full command and records trust for exactly that list; a change since the list was shown is refused. The button is hidden while changes are pending, because trust covers the hooks on disk and the next sync would invalidate it. Refs #828 * test(ui): cover the Codex trust dialog's refused, empty and disabled states Only the happy path was tested, so a regression in the states where the dialog must not offer a write would have passed. The docs sentence on several Codex homes now says what is isolated: a failed write, not a home that cannot be listed. Refs #828 * refactor(hooks): split the trust prompt and output rendering out of the handlers hooks_handlers.go had grown past 500 lines and mixed orchestration with the confirmation prompt and every print function, which the CLI layering rule asks to keep apart. The code is moved unchanged: the prompt goes to hooks_prompt.go and the printing to hooks_render.go. * fix(hooks): leave out a Codex home that cannot be listed instead of stopping With several Codex homes, one home Codex could not list aborted the whole trust run, so a broken account home kept every other home untrusted. A home that fails to list is now reported and left out, and the other homes are previewed and trusted as usual. The CLI warns before the question and exits with an error that names the home; the dashboard dialog shows the same note. When no home can be listed the preview still fails. Codex answers a home it cannot load, such as one with an invalid config.toml, with a successful reply that has no hooks and the reason in `errors`. That was read as a home without hooks and passed silently. It is now a failed listing that carries Codex's own message. Refs #828 * fix(hooks): check the reviewed Codex hooks per home when recording trust The revision was one digest over every home's hooks. When a home was listed in the preview but failed to list again at the write, its hooks dropped out of the digest, so the write was refused as stale: the homes the user had just reviewed stayed untrusted and the real listing error was hidden behind "changed since they were listed". The revision now has one part per home. A home is refused only when its own hooks changed; a home that cannot be listed any more is reported and the reviewed homes are still trusted. Refs #828 * chore: release as 0.30.10 Keep the next release on the patch version the maintainer asked for, instead of the 0.31.0 that Release Please derives from the feat commit. Release-As: 0.30.10 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Willie <egg358358@gmail.com>
384 lines
13 KiB
Go
384 lines
13 KiB
Go
package hooks
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/gofrs/flock"
|
|
)
|
|
|
|
// Service is scoped to one Skillshare config; StateDir is shared across scopes so
|
|
// separate configs cannot silently take over each other's native registrations.
|
|
type Service struct {
|
|
ConfigPath string
|
|
ProjectRoot string
|
|
Home string
|
|
StateDir string
|
|
Platform string
|
|
// ConfigDirs contains explicitly resolved native Agent directory overrides.
|
|
ConfigDirs map[string]string
|
|
Accounts map[string]Account // global scope only
|
|
scopedAccount string
|
|
// OpenCodeVersion is an injectable read-only version probe.
|
|
OpenCodeVersion func(string) (string, error)
|
|
Git GitRunner
|
|
// GitGlobalConfig is the file override Git honors in GIT_CONFIG_GLOBAL.
|
|
GitGlobalConfig string
|
|
// CodexAppServer starts Codex's app-server for one Codex home; nil runs `codex app-server`.
|
|
// defaultHome is true when the user's Codex finds that home as ~/.codex, not CODEX_HOME.
|
|
CodexAppServer func(home string, defaultHome bool) (CodexAppServer, error)
|
|
}
|
|
|
|
// Mutation is shared by CLI and dashboard preview/save/sync flows. An empty
|
|
// mutation synchronizes the current source.
|
|
type Mutation struct {
|
|
// Project is a root under the global config's hooks.projects. Set alone, it creates
|
|
// an empty block; with Remove and no Name, it removes the block.
|
|
Project string `json:"project,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Entry *Entry `json:"entry,omitempty"`
|
|
Remove bool `json:"remove,omitempty"`
|
|
// Replace explicitly takes over this entry's conflicting native outputs: an
|
|
// identical unmanaged registration, an unmanaged file at an output path, or an
|
|
// owned output edited outside Skillshare. It never overrides another config.
|
|
Replace bool `json:"replace,omitempty"`
|
|
// Adopt saves an imported entry as the owner of the native registrations it was
|
|
// read from: identical unmanaged elements become its own without Replace, so the
|
|
// next sync adopts them in place. It never takes over edited outputs or another
|
|
// config's registrations.
|
|
Adopt bool `json:"adopt,omitempty"`
|
|
// Unmanage, with Remove and a Name, also forgets which native registrations the
|
|
// hook wrote, so sync leaves them in place as the user's own hooks.
|
|
Unmanage bool `json:"unmanage,omitempty"`
|
|
}
|
|
|
|
// Change deliberately carries no hook contents.
|
|
type Change struct {
|
|
Target string `json:"target"`
|
|
Path string `json:"path"`
|
|
Name string `json:"name"`
|
|
// Root is the hooks.projects root this change belongs to, empty for the config's own scope.
|
|
Root string `json:"root,omitempty"`
|
|
Action string `json:"action"` // add, update, remove, unchanged, adopt, release, conflict, inactive
|
|
Message string `json:"message,omitempty"`
|
|
// InactiveReason reports why generated Git output cannot run, independently of its file action.
|
|
InactiveReason string `json:"inactiveReason,omitempty"`
|
|
// Events names the native events this change writes into a shared hooks file.
|
|
Events *EventChanges `json:"events,omitempty"`
|
|
}
|
|
|
|
// EventChanges is the event-level detail of a change to a shared hooks file: events
|
|
// the entry newly occupies, events whose registrations change, and events it leaves.
|
|
type EventChanges struct {
|
|
Added []string `json:"added,omitempty"`
|
|
Updated []string `json:"updated,omitempty"`
|
|
Removed []string `json:"removed,omitempty"`
|
|
}
|
|
|
|
// Plan is an optimistic-concurrency-protected preview.
|
|
type Plan struct {
|
|
Revision string `json:"revision"`
|
|
// Fingerprint identifies only what the preview shows about hooks: the proposed
|
|
// entries and projects, replacements, native hook sections and files, ownership
|
|
// and the resulting changes. Unrelated config or native settings leave it unchanged.
|
|
Fingerprint string `json:"fingerprint"`
|
|
SourcePath string `json:"sourcePath"`
|
|
Blocked bool `json:"blocked"`
|
|
Changes []Change `json:"changes"`
|
|
// Warnings are advisory, such as event names an Agent does not document; they
|
|
// never block a sync.
|
|
Warnings []string `json:"warnings"`
|
|
|
|
source *Source
|
|
state ledger
|
|
stateBytes []byte
|
|
files []*filePlan
|
|
// adopted are the ledger keys an Adopt mutation claims, recorded even without sync.
|
|
adopted []string
|
|
gitGuards []*gitGuard
|
|
gitInactive map[string]string
|
|
}
|
|
|
|
// FileDiff is one native file's text before and after a planned sync.
|
|
type FileDiff struct {
|
|
Target string `json:"target"`
|
|
Path string `json:"path"`
|
|
Root string `json:"root,omitempty"`
|
|
// Before is the current text, "" when the file is missing; After is exactly what
|
|
// sync writes, "" when it removes the file.
|
|
Before string `json:"before"`
|
|
After string `json:"after"`
|
|
}
|
|
|
|
// Files returns each native file the plan would change, with its full text.
|
|
func (p *Plan) Files() []FileDiff {
|
|
out := []FileDiff{}
|
|
for _, f := range p.files {
|
|
if f.changed() && string(f.before) != string(f.after) {
|
|
out = append(out, FileDiff{Target: f.target, Path: f.path, Root: f.root, Before: string(f.before), After: string(f.after)})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Result reports applied files individually; a multi-file operation is not a
|
|
// filesystem transaction and may leave completed files when a later one fails.
|
|
type Result struct {
|
|
Plan *Plan `json:"plan,omitempty"`
|
|
Applied []string `json:"applied"`
|
|
BackupIDs []string `json:"backupIds"`
|
|
// CodexTrust is set by sync --trust.
|
|
CodexTrust []CodexTrust `json:"codexTrust,omitempty"`
|
|
}
|
|
|
|
// SourceInfo is the declaration shown by List.
|
|
type SourceInfo struct {
|
|
Path string `json:"path"`
|
|
ConfigPath string `json:"configPath"`
|
|
Entries map[string]Entry `json:"entries"`
|
|
// Projects are roots a global config publishes into, keyed by absolute path.
|
|
Projects map[string]Project `json:"projects,omitempty"`
|
|
}
|
|
|
|
// Backup is metadata for one native write.
|
|
type Backup struct {
|
|
Root string `json:"root,omitempty"`
|
|
ID string `json:"id"`
|
|
Target string `json:"target"`
|
|
Path string `json:"path"`
|
|
Time string `json:"time,omitempty"`
|
|
}
|
|
|
|
// Unmanaged names native hooks no Skillshare config owns, including additional
|
|
// sources Skillshare never edits.
|
|
type Unmanaged struct {
|
|
Project string `json:"project,omitempty"`
|
|
Target string `json:"target"`
|
|
Path string `json:"path"`
|
|
Names []string `json:"names"`
|
|
}
|
|
|
|
// Inventory is everything the list views need.
|
|
type Inventory struct {
|
|
Git *GitInfo `json:"git,omitempty"`
|
|
ProjectGit map[string]*GitInfo `json:"projectGit,omitempty"`
|
|
Source SourceInfo `json:"source"`
|
|
Targets []TargetDef `json:"targets"`
|
|
Paths map[string]string `json:"paths"`
|
|
Plan *Plan `json:"plan,omitempty"`
|
|
PreviewError string `json:"previewError,omitempty"`
|
|
Backups []Backup `json:"backups"`
|
|
Unmanaged []Unmanaged `json:"unmanaged"`
|
|
// ProjectConfigs are hooks.projects roots that have their own Skillshare config;
|
|
// the global config cannot manage them.
|
|
ProjectConfigs []string `json:"projectConfigs"`
|
|
// ProjectPaths are each hooks.projects root's native destinations.
|
|
ProjectPaths map[string]map[string]string `json:"projectPaths"`
|
|
}
|
|
|
|
// ImportRequest reads native hooks without executing them. Content, when set,
|
|
// replaces reading the Agent's file in the current scope.
|
|
type ImportRequest struct {
|
|
From string `json:"from"`
|
|
// Root reads a hooks.projects root's native files instead of the current scope's.
|
|
Root string `json:"root,omitempty"`
|
|
Content string `json:"content,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
}
|
|
|
|
// Candidate is one importable entry.
|
|
type Candidate struct {
|
|
Name string `json:"name"`
|
|
Entry Entry `json:"entry"`
|
|
Problems []string `json:"problems"`
|
|
Warnings []string `json:"warnings"`
|
|
// Editable marks unmanaged hooks a NativeEdit can change or remove in place.
|
|
Editable bool `json:"editable,omitempty"`
|
|
}
|
|
|
|
// ConfigDirsFromEnv reads the Agent directory overrides Agents themselves honor.
|
|
func ConfigDirsFromEnv() map[string]string {
|
|
dirs := map[string]string{}
|
|
for key, env := range configDirEnv {
|
|
value := strings.TrimSpace(os.Getenv(env))
|
|
if value == "" || !filepath.IsAbs(value) {
|
|
continue
|
|
}
|
|
dirs[key] = value
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
// configDirEnv maps ConfigDirs keys to the environment variables Agents document.
|
|
var configDirEnv = map[string]string{
|
|
"claude": "CLAUDE_CONFIG_DIR",
|
|
"codex": "CODEX_HOME",
|
|
"copilot": "COPILOT_HOME",
|
|
"opencode": "OPENCODE_CONFIG_DIR",
|
|
"pi": "PI_CODING_AGENT_DIR",
|
|
"omp": "PI_CODING_AGENT_DIR", // OMP is a Pi fork and reads the same variable.
|
|
"xdg": "XDG_CONFIG_HOME",
|
|
}
|
|
|
|
func (s *Service) home() (string, error) {
|
|
if s.Home != "" {
|
|
return s.Home, nil
|
|
}
|
|
return os.UserHomeDir()
|
|
}
|
|
|
|
// configDir is the Agent's own config directory in this scope. A project scope never
|
|
// falls back to a global path.
|
|
func (s *Service) configDir(target string) (string, error) {
|
|
if target == "git" {
|
|
d, err := s.gitDestination("")
|
|
return d.base, err
|
|
}
|
|
if s.ProjectRoot != "" {
|
|
dirs := map[string]string{"claude": ".claude", "codex": ".codex", "gemini": ".gemini", "qwen": ".qwen", "copilot": ".github", "cursor": ".cursor", "droid": ".factory", "antigravity": ".agents", "pi": ".pi", "omp": ".omp", "amp": ".amp", "opencode": ".opencode"}
|
|
dir, ok := dirs[target]
|
|
if !ok {
|
|
return "", fmt.Errorf("unsupported hooks Agent %q", target)
|
|
}
|
|
return filepath.Join(s.ProjectRoot, dir), nil
|
|
}
|
|
if s.scopedAccount != "" {
|
|
return s.Accounts[s.scopedAccount].Dir, nil
|
|
}
|
|
if dir := s.ConfigDirs[target]; dir != "" && s.envShadow(target) == "" {
|
|
if !filepath.IsAbs(dir) {
|
|
return "", fmt.Errorf("%s config directory must be absolute", target)
|
|
}
|
|
return filepath.Clean(dir), nil
|
|
}
|
|
return s.defaultConfigDir(target)
|
|
}
|
|
|
|
func (s *Service) defaultConfigDir(target string) (string, error) {
|
|
home, err := s.home()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
xdg := s.ConfigDirs["xdg"]
|
|
if xdg == "" {
|
|
xdg = filepath.Join(home, ".config")
|
|
}
|
|
switch target {
|
|
case "claude", "codex", "gemini", "qwen", "copilot", "cursor":
|
|
return filepath.Join(home, "."+target), nil
|
|
case "droid":
|
|
return filepath.Join(home, ".factory"), nil
|
|
case "antigravity":
|
|
return filepath.Join(home, ".gemini", "config"), nil
|
|
case "pi":
|
|
return filepath.Join(home, ".pi", "agent"), nil
|
|
case "omp":
|
|
return filepath.Join(home, ".omp", "agent"), nil
|
|
case "amp":
|
|
return filepath.Join(xdg, "amp"), nil
|
|
case "opencode":
|
|
return filepath.Join(xdg, "opencode"), nil
|
|
}
|
|
return "", fmt.Errorf("unsupported hooks Agent %q", target)
|
|
}
|
|
|
|
// nativePath is the shared hooks file of a command Agent, or the folder that holds
|
|
// Skillshare's files for Copilot and the code Agents.
|
|
func (s *Service) nativePath(target string) (string, error) {
|
|
if target == "git" {
|
|
d, err := s.gitDestination("")
|
|
return d.hooksFile, err
|
|
}
|
|
dir, err := s.configDir(target)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
switch target {
|
|
case "claude", "gemini", "qwen":
|
|
return filepath.Join(dir, "settings.json"), nil
|
|
case "codex", "cursor", "droid", "antigravity":
|
|
return filepath.Join(dir, "hooks.json"), nil
|
|
case "copilot":
|
|
return filepath.Join(dir, "hooks"), nil
|
|
case "pi", "omp":
|
|
return filepath.Join(dir, "extensions"), nil
|
|
case "amp", "opencode":
|
|
return filepath.Join(dir, "plugins"), nil
|
|
}
|
|
return "", fmt.Errorf("unsupported hooks Agent %q", target)
|
|
}
|
|
|
|
// codePath is the Skillshare-owned file of a code Agent or Copilot for one entry.
|
|
func (s *Service) codePath(target, entry string) (string, error) {
|
|
dir, err := s.nativePath(target)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if target == "copilot" {
|
|
return filepath.Join(dir, "skillshare-"+entry+".json"), nil
|
|
}
|
|
return filepath.Join(dir, "skillshare-"+entry+".ts"), nil
|
|
}
|
|
|
|
// scriptDir holds an entry's script files for a command Agent.
|
|
func (s *Service) scriptDir(target, entry string) (string, error) {
|
|
if target == "git" {
|
|
d, err := s.gitDestination("")
|
|
return filepath.Join(d.base, "skillshare", "files", entry), err
|
|
}
|
|
dir, err := s.configDir(target)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(dir, "hooks", "skillshare", entry), nil
|
|
}
|
|
|
|
// Paths exposes native destinations for the current scope.
|
|
func (s *Service) Paths() map[string]string {
|
|
out := map[string]string{}
|
|
for _, t := range Targets {
|
|
if path, err := s.nativePath(t.Name); err == nil {
|
|
out[t.Name] = path
|
|
}
|
|
}
|
|
for key := range s.accountAgents() {
|
|
sc, agent := s.forTarget(key)
|
|
if path, err := sc.nativePath(agent); err == nil {
|
|
out[key] = path
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Service) platform() string {
|
|
if s.Platform != "" {
|
|
return s.Platform
|
|
}
|
|
return runtime.GOOS
|
|
}
|
|
|
|
func (s *Service) stateDir() string { return filepath.Join(s.StateDir, "hooks") }
|
|
|
|
func (s *Service) statePath() string { return filepath.Join(s.stateDir(), "state.json") }
|
|
|
|
func (s *Service) lock() (*flock.Flock, error) {
|
|
if s.StateDir == "" {
|
|
return nil, fmt.Errorf("hooks state directory is required")
|
|
}
|
|
if err := os.MkdirAll(s.stateDir(), 0700); err != nil {
|
|
return nil, err
|
|
}
|
|
lock := flock.New(filepath.Join(s.stateDir(), "operation.lock"))
|
|
ok, err := lock.TryLock()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
return nil, fmt.Errorf("another hooks operation is running; retry when it completes")
|
|
}
|
|
return lock, nil
|
|
}
|