diff --git a/README.md b/README.md index b0c2204..153b169 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ The format is intentionally minimal: no schema registry, no central authority, n - **Provenance (§5.1)** - the `sources` family with per-source credibility signals (`author`, `usage_count`, `last_modified`) and `usage_window`; footnote labels are validated as join keys into `sources[].id` for per-claim attribution. - **Trust (§5.2–5.3)** - `generated` and `verified` are parsed (a bare `verified` mapping is treated as a one-element list, as the spec requires) and every concept gets a derived trust tier: `unverified`, `machine-confirmed`, or `human-reviewed`. -- **Lifecycle (§5.4–5.5)** - `status` (draft/stable/deprecated, default stable) and `stale_after`; `validate` flags stale concepts and invalid values, and `show`/`list` expose the effective state. +- **Lifecycle (§5.4–5.5)** - `status` (draft/stable/deprecated, default stable) and `stale_after` (an RFC 3339 instant such as `2026-09-23T00:00:00Z`, or a `YYYY-MM-DD` date meaning midnight UTC); `validate` flags stale concepts and invalid values, and `show`/`list` expose the effective state. - **Attested Computations (§10)** - the full contract (`runtime`, `parameters`, `computation`, `executor`, `attester`) is parsed and validated: `runtime` is required, the computation must exist inline or as a file, and contract paths that point at missing files are flagged. - **Actor convention (§7)** - `generated.by` and `verified[].by` are checked against `human:`, `process:`, and `/` forms. - **Versioning (§12)** - `okf init` declares `okf_version: "0.2"` in the root `index.md`; `okf index` preserves an existing declaration when regenerating. diff --git a/cmd/okf/main_test.go b/cmd/okf/main_test.go index bcc3922..a758778 100644 --- a/cmd/okf/main_test.go +++ b/cmd/okf/main_test.go @@ -218,3 +218,40 @@ func TestE2E_IndexRefusesNonConceptMarkdown(t *testing.T) { t.Error("index.md was written even though the bundle failed to load") } } + +// --- issue #34: validate accepts the spec's datetime form of stale_after --- + +func TestE2E_StaleAfterDatetimeIsValid(t *testing.T) { + dir := t.TempDir() + // A past instant, in the exact form OKF §5.5 shows, so the concept is stale. + writeFile(t, filepath.Join(dir, "a.md"), + "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\nstale_after: 2026-09-23T00:00:00Z\n---\n\nbody") + + out, code := runOKF(t, "validate", dir) + if code != 0 { + t.Fatalf("validate exit %d, want 0; out=%s", code, out) + } + if strings.Contains(out, "stale-after-invalid") { + t.Errorf("validate reported stale-after-invalid for an RFC 3339 instant: %s", out) + } + + out, code = runOKF(t, "show", dir, "a") + if code != 0 { + t.Fatalf("show exit %d, want 0; out=%s", code, out) + } + var got struct { + Concept struct { + Stale bool `json:"stale"` + StaleAfter string `json:"stale_after"` + } `json:"concept"` + } + if err := json.Unmarshal([]byte(out), &got); err != nil { + t.Fatalf("bad json %q", out) + } + if !got.Concept.Stale { + t.Errorf("show stale = false, want true for a past stale_after instant") + } + if got.Concept.StaleAfter != "2026-09-23T00:00:00Z" { + t.Errorf("show stale_after = %q, want verbatim %q", got.Concept.StaleAfter, "2026-09-23T00:00:00Z") + } +} diff --git a/internal/concept/v02.go b/internal/concept/v02.go index 1c17ea7..a82686c 100644 --- a/internal/concept/v02.go +++ b/internal/concept/v02.go @@ -157,18 +157,30 @@ func (f *Frontmatter) GeneratedAt() (at time.Time, ok bool) { return time.Time{}, false } -// IsStale reports whether the concept is stale as of today: stale when -// today >= stale_after (OKF §5.5). Absent or unparseable stale_after is never +// IsStale reports whether the concept is stale as of now: stale when +// now >= stale_after (OKF §5.5). Absent or unparseable stale_after is never // stale; validation flags the unparseable case separately. -func (f *Frontmatter) IsStale(today time.Time) bool { +func (f *Frontmatter) IsStale(now time.Time) bool { if strings.TrimSpace(f.StaleAfter) == "" { return false } - d, err := time.Parse("2006-01-02", f.StaleAfter) + t, err := ParseStaleAfter(f.StaleAfter) if err != nil { return false } - return !today.Before(d) + return !now.Before(t) +} + +// ParseStaleAfter parses a stale_after value. OKF §5.5 defines it as an +// absolute instant, so an RFC 3339 datetime with a zone (the spec's own +// example form) is accepted. A bare YYYY-MM-DD date is also accepted and +// means midnight UTC on that day. +func ParseStaleAfter(s string) (time.Time, error) { + s = strings.TrimSpace(s) + if d, err := time.Parse(time.DateOnly, s); err == nil { + return d, nil + } + return ParseDatetime(s) } // ActorKind classifies an actor string per the convention of §7: diff --git a/internal/concept/v02_test.go b/internal/concept/v02_test.go index e19ced6..36fbe88 100644 --- a/internal/concept/v02_test.go +++ b/internal/concept/v02_test.go @@ -165,6 +165,81 @@ func TestIsStale_AbsentNeverStale(t *testing.T) { } } +// --- issue #34: stale_after is an absolute instant, not only a date --- + +func TestParseStaleAfter(t *testing.T) { + utc := func(y int, mo time.Month, d, h, mi, sec, ns int) time.Time { + return time.Date(y, mo, d, h, mi, sec, ns, time.UTC) + } + cases := []struct { + in string + want time.Time + }{ + // Date-only remains accepted and means midnight UTC. + {"2026-10-03", utc(2026, 10, 3, 0, 0, 0, 0)}, + // The form OKF §5.5 itself uses as its example. + {"2026-09-23T00:00:00Z", utc(2026, 9, 23, 0, 0, 0, 0)}, + {"2026-10-03T00:00:00+00:00", utc(2026, 10, 3, 0, 0, 0, 0)}, + {"2026-10-03T12:00:00+02:00", utc(2026, 10, 3, 10, 0, 0, 0)}, + {"2026-10-03T00:00:00.5Z", utc(2026, 10, 3, 0, 0, 0, 500000000)}, + {" 2026-10-03T00:00:00Z ", utc(2026, 10, 3, 0, 0, 0, 0)}, + } + for _, tc := range cases { + got, err := ParseStaleAfter(tc.in) + if err != nil { + t.Errorf("ParseStaleAfter(%q) error: %v", tc.in, err) + continue + } + if !got.Equal(tc.want) { + t.Errorf("ParseStaleAfter(%q) = %v, want %v", tc.in, got, tc.want) + } + } +} + +func TestParseStaleAfter_Invalid(t *testing.T) { + for _, in := range []string{ + "", + "soon", + "2026-13-01", + "2026-10-03T00:00:00", // no zone: not an absolute instant + "2026/10/03", + "03-10-2026", + } { + if _, err := ParseStaleAfter(in); err == nil { + t.Errorf("ParseStaleAfter(%q) = nil error, want error", in) + } + } +} + +func TestIsStale_Datetime(t *testing.T) { + raw := []byte("---\ntype: T\nstale_after: 2026-10-03T12:00:00+02:00\n---\nbody") + c, err := ParseBytes(raw, "a.md", "/tmp/a.md") + if err != nil { + t.Fatalf("ParseBytes: %v", err) + } + instant := time.Date(2026, 10, 3, 10, 0, 0, 0, time.UTC) + if c.Frontmatter.IsStale(instant.Add(-time.Second)) { + t.Error("IsStale(one second before) = true, want false") + } + if !c.Frontmatter.IsStale(instant) { + t.Error("IsStale(at instant) = false, want true (stale when now >= stale_after)") + } + if !c.Frontmatter.IsStale(instant.Add(time.Hour)) { + t.Error("IsStale(after) = false, want true") + } +} + +func TestIsStale_QuotedDatetime(t *testing.T) { + raw := []byte("---\ntype: T\nstale_after: \"2026-10-03T00:00:00Z\"\n---\nbody") + c, err := ParseBytes(raw, "a.md", "/tmp/a.md") + if err != nil { + t.Fatalf("ParseBytes: %v", err) + } + if !c.Frontmatter.IsStale(time.Date(2026, 10, 3, 0, 0, 0, 0, time.UTC)) { + t.Error("IsStale(quoted datetime, at instant) = false, want true") + } +} + // --- §13.1 generated.at with legacy timestamp fallback --- func TestGeneratedAt_FallbackToLegacyTimestamp(t *testing.T) { diff --git a/internal/validate/rules.go b/internal/validate/rules.go index 7c81794..4c7e543 100644 --- a/internal/validate/rules.go +++ b/internal/validate/rules.go @@ -71,8 +71,8 @@ var ruleDescriptions = map[string]string{ RuleLinkBroken: "cross-link does not resolve to a concept in the bundle; consumers must tolerate this (OKF §6.1, §11)", RuleStatusInvalid: "'status' must be draft, stable, or deprecated (OKF §5.4)", - RuleStaleAfterInvalid: "'stale_after' must be an absolute YYYY-MM-DD date (OKF §5.5)", - RuleStale: "concept is past its stale_after date (OKF §5.5)", + RuleStaleAfterInvalid: "'stale_after' must be an absolute instant: an RFC 3339 datetime with a zone, or YYYY-MM-DD (OKF §5.5)", + RuleStale: "concept is at or past its stale_after instant (OKF §5.5)", RuleGeneratedByRequired: "'generated' requires 'by' (OKF §5.2)", RuleGeneratedAtInvalid: "'generated.at' is not an ISO 8601 datetime (OKF §5.2)", diff --git a/internal/validate/v02.go b/internal/validate/v02.go index 9f0d688..43d13e1 100644 --- a/internal/validate/v02.go +++ b/internal/validate/v02.go @@ -36,14 +36,15 @@ func validateV02(r *Report, b *bundle.Bundle, c *concept.Concept) { "frontmatter: 'status' must be draft, stable, or deprecated, got %q (OKF §5.4)", fm.Status)) } - // §5.5 stale_after is an absolute YYYY-MM-DD date. + // §5.5 stale_after is an absolute instant: an RFC 3339 datetime with a + // zone, or a YYYY-MM-DD date (midnight UTC). if s := strings.TrimSpace(fm.StaleAfter); s != "" { - if _, err := time.Parse("2006-01-02", s); err != nil { + if _, err := concept.ParseStaleAfter(s); err != nil { r.add(c.ID, RuleStaleAfterInvalid, SeverityError, fmt.Sprintf( - "frontmatter: 'stale_after' must be an absolute YYYY-MM-DD date, got %q (OKF §5.5)", fm.StaleAfter)) + "frontmatter: 'stale_after' must be an absolute instant (RFC 3339 datetime with a zone, or YYYY-MM-DD), got %q (OKF §5.5)", fm.StaleAfter)) } else if fm.IsStale(now()) { r.add(c.ID, RuleStale, SeverityWarning, fmt.Sprintf( - "concept is stale: today >= stale_after (%s) (OKF §5.5)", fm.StaleAfter)) + "concept is stale: now >= stale_after (%s) (OKF §5.5)", fm.StaleAfter)) } } diff --git a/internal/validate/v02_test.go b/internal/validate/v02_test.go index d4b90bf..896c16c 100644 --- a/internal/validate/v02_test.go +++ b/internal/validate/v02_test.go @@ -82,6 +82,58 @@ func TestValidate_FreshConceptNoStaleWarning(t *testing.T) { mustNotFinding(t, Validate(b), "stale") } +// --- issue #34: OKF §5.5 calls stale_after an absolute instant --- + +func TestValidate_StaleAfterDatetimeAccepted(t *testing.T) { + defer func(orig func() time.Time) { now = orig }(now) + now = func() time.Time { return time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) } + for _, v := range []string{ + "2026-09-23T00:00:00Z", // the spec's own §5.5 example + "2026-10-03T00:00:00+00:00", + `"2026-10-03T00:00:00Z"`, + "2026-10-03T08:30:00-05:00", + "2026-10-03", + } { + b := testBundle(t, map[string]string{ + "a.md": "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\nstale_after: " + v + "\n---\n\nbody", + }) + r := Validate(b) + for _, f := range r.Findings { + if f.RuleID == RuleStaleAfterInvalid { + t.Errorf("stale_after %s: unexpected %s finding: %s", v, f.RuleID, f.Message) + } + } + if r.HasErrors() { + t.Errorf("stale_after %s: report has errors: %+v", v, r.Findings) + } + } +} + +func TestValidate_StaleDatetimeConceptWarns(t *testing.T) { + defer func(orig func() time.Time) { now = orig }(now) + now = func() time.Time { return time.Date(2026, 10, 3, 12, 0, 0, 0, time.UTC) } + b := testBundle(t, map[string]string{ + "a.md": "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\nstale_after: 2026-10-03T12:00:00Z\n---\n\nbody", + }) + mustFinding(t, Validate(b), SeverityWarning, "concept is stale") +} + +func TestValidate_FreshDatetimeConceptNoStaleWarning(t *testing.T) { + defer func(orig func() time.Time) { now = orig }(now) + now = func() time.Time { return time.Date(2026, 10, 3, 11, 59, 59, 0, time.UTC) } + b := testBundle(t, map[string]string{ + "a.md": "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\nstale_after: 2026-10-03T12:00:00Z\n---\n\nbody", + }) + mustNotFinding(t, Validate(b), "concept is stale") +} + +func TestValidate_StaleAfterWithoutZoneRejected(t *testing.T) { + b := testBundle(t, map[string]string{ + "a.md": "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\nstale_after: \"2026-10-03T00:00:00\"\n---\n\nbody", + }) + mustFinding(t, Validate(b), SeverityError, "RFC 3339") +} + // --- §5.2 generated / verified --- func TestValidate_GeneratedWithoutBy(t *testing.T) {