Files
multica/server/internal/daemon/helpers_test.go
Bohan Jiang 8f10741a4d feat(daemon/gc): tighten GC defaults + flex duration suffix (#1559)
* feat(daemon/gc): tighten GC defaults + flex duration suffix

Driven by user feedback in #1539 (40 GB VPS filling within 24h of heavy
AI-coding usage): the existing TTLs were sized for desktop/laptop
deployments and are too lenient for small-disk, long-running daemons.

- GCTTL: 5d → 24h. Done/canceled issues almost never need a multi-day
  grace period in AI-coding workflows.
- GCOrphanTTL: 30d → 72h. Covers crash-leftover and pre-GC directories
  without a month-long wait.
- Issue-deleted orphans (API returns 404) are now cleaned on the next GC
  cycle regardless of mtime. The issue row is gone; there is nothing
  left to protect.
- parseFlexDuration: accept a `d` (day) suffix in addition to the stdlib
  time.ParseDuration syntax. MULTICA_GC_TTL=5d now works; previously only
  120h was accepted.

* fix(daemon/gc): address review — 404 safety + decimal/overflow in duration parser

Two issues flagged in PR review:

1. 404-immediate-clean is unsafe. The /gc-check endpoint returns 404 for
   both "issue deleted" AND "daemon token has no access to the workspace"
   (anti-enumeration, see requireDaemonWorkspaceAccess). Clean-on-404
   would let a scoped-down daemon token wipe taskDirs whose issues are
   still live. Restore the mtime gate against GCOrphanTTL. With the new
   72h default we still shrink the original 30d window dramatically
   without the cross-workspace hazard. Lock the behavior in with a new
   test that asserts a recent 404 is skipped.

2. parseFlexDuration mishandled decimals and swallowed Atoi errors:
   "0.5d" → 7m12s (regex matched only the "5d"), "1.5d" → 1h7m12s,
   and 20+ digit day values Atoi-errored silently to 0. Match the full
   decimal number with `\d*\.\d+|\d+` and parse with ParseFloat so
   fractional days and oversized inputs both go through
   time.ParseDuration correctly — fractions as sub-hour durations,
   overflow as a returned error.
2026-04-23 17:40:09 +08:00

53 lines
1.1 KiB
Go

package daemon
import (
"testing"
"time"
)
func TestParseFlexDuration(t *testing.T) {
t.Parallel()
cases := []struct {
in string
want time.Duration
}{
{"5d", 5 * 24 * time.Hour},
{"1d", 24 * time.Hour},
{"1d12h", 36 * time.Hour},
{"2d30m", 2*24*time.Hour + 30*time.Minute},
{"0.5d", 12 * time.Hour},
{"1.5d", 36 * time.Hour},
{".5d", 12 * time.Hour},
{"120h", 120 * time.Hour},
{"24h", 24 * time.Hour},
{"30m", 30 * time.Minute},
}
for _, tc := range cases {
got, err := parseFlexDuration(tc.in)
if err != nil {
t.Errorf("parseFlexDuration(%q) unexpected error: %v", tc.in, err)
continue
}
if got != tc.want {
t.Errorf("parseFlexDuration(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestParseFlexDuration_Invalid(t *testing.T) {
t.Parallel()
for _, in := range []string{
"",
"xyz",
"5days",
"abc5d",
// Overflow: 30 digits is well past int64/float64 safe range; must error
// rather than silently produce 0h.
"999999999999999999999999999999d",
} {
if _, err := parseFlexDuration(in); err == nil {
t.Errorf("parseFlexDuration(%q) expected error, got nil", in)
}
}
}