fix(oplog): keep selected global config logs in the state dir (#609)

SKILLSHARE_CONFIG can select a global config in a directory named
skillshare or .skillshare. Treating it as a project config puts logs and
a .gitignore beside the shared config and labels global logs as project logs.

Exclude selected and read-only global config paths from project routing.
Share the check with the log command so readers and writers agree.
Run the legacy config regression explicitly in Windows CI so it is exercised
without enabling unrelated tests that still need Windows fixes.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Rusty Shackleford
2026-10-25 22:55:36 +08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 3cb1fa4984
commit 61eedffe1e
7 changed files with 167 additions and 16 deletions
+5
View File
@@ -137,6 +137,11 @@ jobs:
./internal/validate \
./internal/version
# Run this Windows-only regression; the rest of oplog still has failures tracked in #344.
- name: Windows legacy config log regression
shell: bash
run: go test -v -count=1 ./internal/oplog -run "^TestLogDir_WindowsLegacySelectedGlobal$"
docker-sandbox-offline:
if: *not-release-duplicate
runs-on: ubuntu-latest
+2 -14
View File
@@ -11,9 +11,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/oplog"
"skillshare/internal/projectdir"
"skillshare/internal/ui"
"skillshare/internal/utils"
)
func cmdLog(args []string) error {
@@ -93,7 +91,7 @@ func runLog(args []string, configPath string) error {
}
modeLabel := "global"
if isProjectLogConfig(configPath) {
if oplog.IsProjectConfig(configPath) {
modeLabel = "project"
}
@@ -158,7 +156,7 @@ func printLogSection(configPath, filename, label string, limit int, f oplog.Filt
logPath = abs
}
mode := "global"
if isProjectLogConfig(configPath) {
if oplog.IsProjectConfig(configPath) {
mode = "project"
}
@@ -263,16 +261,6 @@ func runLogStats(configPath string, auditOnly bool, f oplog.Filter) error {
return nil
}
func isProjectLogConfig(configPath string) bool {
configDir := filepath.Dir(configPath)
if !projectdir.IsName(filepath.Base(configDir)) {
return false
}
// The visible project directory shares its name with the global config
// directory, so exclude the global location by full path.
return !utils.PathsEqual(configDir, config.BaseDir())
}
// runLogTUIDispatch launches the interactive TUI with async log loading.
func runLogTUIDispatch(configPath string, auditOnly bool, limit int, filter oplog.Filter, modeLabel string) error {
if auditOnly {
+10 -2
View File
@@ -44,18 +44,26 @@ func LogDir(configPath string) string {
//
// The visible project directory shares its name with the global config
// directory (<config-home>/skillshare), so a name match alone is ambiguous and
// the global location is excluded by full path.
// the canonical and selected global locations are excluded by full path.
func projectConfigDir(configPath string) (string, bool) {
configDir := filepath.Dir(configPath)
if !projectdir.IsName(filepath.Base(configDir)) {
return "", false
}
if utils.PathsEqual(configDir, config.BaseDir()) {
if utils.PathsEqual(configDir, config.BaseDir()) ||
utils.PathsEqual(configPath, config.ReadOnlyConfigPath()) {
return "", false
}
return configDir, true
}
// IsProjectConfig reports whether a config routes to project-local logs.
// Both readers and writers must use the same global/selected-config exclusions.
func IsProjectConfig(configPath string) bool {
_, ok := projectConfigDir(configPath)
return ok
}
// Write appends a single JSONL entry to the named log file.
func Write(configPath, filename string, e Entry) error {
dir := LogDir(configPath)
+54
View File
@@ -7,6 +7,8 @@ import (
"strings"
"testing"
"time"
"skillshare/internal/config"
)
func tempConfigPath(t *testing.T) string {
@@ -593,3 +595,55 @@ func TestWriteWithLimit_HysteresisAvoidsTruncation(t *testing.T) {
t.Errorf("got %d entries, want 11 (hysteresis should prevent truncation)", len(entries))
}
}
func TestLogDir_SelectedGlobalConfigRoundTrip(t *testing.T) {
for _, dir := range []string{"shared/skillshare", "shared/.skillshare", ".config/skillshare"} {
t.Run(dir, func(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg"))
t.Setenv("XDG_STATE_HOME", filepath.Join(home, "state"))
selected := filepath.Join(home, filepath.FromSlash(dir), "config.yaml")
t.Setenv("SKILLSHARE_CONFIG", selected)
canonical := filepath.Join(config.BaseDir(), "config.yaml")
want := filepath.Join(config.StateDir(), "logs")
if got := LogDir(selected); got != want {
t.Errorf("selected global logs=%q want %q", got, want)
}
if err := Write(canonical, OpsFile, Entry{Command: "project", Status: "ok"}); err != nil {
t.Fatal(err)
}
entries, err := Read(selected, OpsFile, 0)
if err != nil || len(entries) != 1 {
t.Errorf("global reader lost canonical write: %v %v", entries, err)
}
if err := Write(selected, AuditFile, Entry{Command: "audit", Status: "ok"}); err != nil {
t.Fatal(err)
}
for _, name := range []string{"logs", ".gitignore"} {
if _, err := os.Stat(filepath.Join(filepath.Dir(selected), name)); !os.IsNotExist(err) {
t.Fatalf("global config treated as a project: %s exists (%v)", name, err)
}
}
project := filepath.Join(home, "app", "skillshare", "config.yaml")
if err := Write(project, OpsFile, Entry{Command: "sync", Status: "ok"}); err != nil {
t.Fatal(err)
}
if got := LogDir(project); got != filepath.Join(filepath.Dir(project), "logs") {
t.Fatalf("real project moved: %s", got)
}
if err := Clear(selected, OpsFile); err != nil {
t.Fatal(err)
}
entries, err = Read(canonical, OpsFile, 0)
if err != nil || len(entries) != 0 {
t.Errorf("global clear missed canonical write: %v %v", entries, err)
}
entries, err = Read(project, OpsFile, 0)
if err != nil || len(entries) != 1 {
t.Errorf("global clear changed real project: %v %v", entries, err)
}
})
}
}
+34
View File
@@ -0,0 +1,34 @@
package oplog
import (
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
)
func TestLogDir_WindowsLegacySelectedGlobal(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg"))
t.Setenv("XDG_STATE_HOME", filepath.Join(home, "state"))
t.Setenv("SKILLSHARE_CONFIG", "")
legacy := filepath.Join(home, ".config", "skillshare", "config.yaml")
if err := os.MkdirAll(filepath.Dir(legacy), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(legacy, []byte("source: skills\n"), 0644); err != nil {
t.Fatal(err)
}
if got := config.ReadOnlyConfigPath(); got != legacy {
t.Fatalf("legacy selection=%q want %q", got, legacy)
}
if got, want := LogDir(legacy), filepath.Join(config.StateDir(), "logs"); got != want {
t.Fatalf("legacy-selected global logs=%q want %q", got, want)
}
if IsProjectConfig(legacy) {
t.Fatal("legacy-selected global classified as project")
}
}
+34
View File
@@ -6,6 +6,9 @@ import (
"net/http/httptest"
"strings"
"testing"
"skillshare/internal/config"
"skillshare/internal/oplog"
)
func TestHandleListLog_Empty(t *testing.T) {
@@ -82,3 +85,34 @@ func TestHandleClearLog(t *testing.T) {
t.Error("expected success true")
}
}
func TestHandleLog_ListStatsAndClear(t *testing.T) {
s, _ := newTestServer(t)
cfgPath := config.ConfigPath()
if err := oplog.Write(cfgPath, oplog.OpsFile, oplog.Entry{Command: "sync", Status: "ok"}); err != nil {
t.Fatal(err)
}
for _, url := range []string{"/api/log", "/api/log/stats"} {
rr := projectRequest(t, s, http.MethodGet, url, "")
var payload struct {
Total int `json:"total"`
}
if rr.Code != http.StatusOK {
t.Fatalf("%s: %d %s", url, rr.Code, rr.Body)
}
if err := json.Unmarshal(rr.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if payload.Total != 1 {
t.Errorf("%s missed global log entry: %s", url, rr.Body)
}
}
rr := projectRequest(t, s, http.MethodDelete, "/api/log", "")
if rr.Code != http.StatusOK {
t.Fatalf("clear: %d %s", rr.Code, rr.Body)
}
entries, err := oplog.Read(cfgPath, oplog.OpsFile, 0)
if err != nil || len(entries) != 0 {
t.Fatalf("clear missed canonical log: %v %v", entries, err)
}
}
@@ -0,0 +1,28 @@
//go:build !online
package integration
import (
"path/filepath"
"testing"
"skillshare/internal/testutil"
)
// Global commands share the selected config path and label its log as global.
func TestLog_GlobalConfigOverrideUsesStateDir(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("test-skill", map[string]string{"SKILL.md": "# Test\n\nTest."})
targetPath := sb.CreateTarget("claude")
sb.ConfigPath = filepath.Join(sb.Home, "shared", "skillshare", "config.yaml")
sb.WriteConfig("source: " + sb.SourcePath + "\nmode: merge\ntargets:\n claude:\n path: " + targetPath + "\n")
sb.RunCLI("sync", "-g").AssertSuccess(t)
list := sb.RunCLI("log", "-g", "--no-tui")
list.AssertSuccess(t)
list.AssertOutputContains(t, "sync")
list.AssertOutputNotContains(t, "· project")
}