Files
agent-bot ee1e53b510
CI / go-vet (pull_request) Failing after 10s
CI / backup-restore-drill (pull_request) Failing after 25s
CI / web (pull_request) Successful in 33s
fix(tests): per-package test databases + legitimate approval fixtures (issue #2 close)
Root causes of the remaining 5 CI PG failures (and the hidden flaky set):
1. All PG-integration packages shared ONE database under parallel package
   execution - migrations/seeds/truncates raced (nondeterministic failure
   sets, locally and in CI).
   Fix: RequireTestDatabaseURL/SkipIfNoTestDatabaseURL now derive a
   per-package database (<base>_<pkgHash>, advisory-lock-guarded
   CREATE); 9 raw-env straggler tests funnelled through the helpers;
   db-package tests call it unqualified (no import cycle).
2. Approval-chain fixtures were not production-shaped: freezeReviewSnapshot
   only honors head_snapshot_id, fake content hashes never froze a
   snapshot, so ApproverApprove rejected with 'approved requires frozen
   review_snapshot_id'. Errors were swallowed (_, _ =) hiding this.
   Fix (ownership-respecting, arch-enforced):
   - docrevision.SeedContentSnapshotForTest seeds the ledger tables it owns
   - docgov GovernanceEngine.FreezeHeadSnapshotForTest freezes head/review
     pointers on document_records (docgov-owned) and clears engine cache
   - all four integration fixtures use the pair; swallowed errors checked
3. Fixed-name fixtures collide once a package shares one DB (projects
   uq_projects_name_global): uniquified in projectinstance + server tests.
4. CAS/ctxmgr tests never bootstrapped migrations (relied on other packages
   having migrated the shared DB): explicit RunMigrationsOnceForTest.

Verified: full parallel short suite = 75 packages, 0 failures against an
isolated throwaway PG. CI skip list removed - short tests fully required.
2026-09-29 09:54:43 +00:00

124 lines
4.0 KiB
Go

package ctxmgr
import (
"os"
"strings"
"sync"
)
const (
EnvSemanticSummaryShadow = "SEMANTIC_SUMMARY_SHADOW"
// REPLACE was sealed by Wave 5 X-2 (2026-09-18). Unsealed 2026-09-26 under an
// explicit user authorization for the chat-semantic-replace closeout task:
// wiring is fail-closed and gray-released via a session allowlist. Enabling
// the master flag alone replaces nothing while the allowlist is empty.
// See .doc/ops/short-term/2026-09-26-chat-semantic-replace.md and
// .doc/ops/short-term/2026-09-18-wave5-deadcode-decisions.md#x-2 (history).
EnvSemanticSummaryReplace = "SEMANTIC_SUMMARY_REPLACE"
// EnvSemanticSummaryReplaceSessions is the fail-closed gray-release
// allowlist: comma-separated chat session IDs eligible for narrative
// replacement. Empty allowlist (default) replaces no session. "*" widens
// eligibility to every session (present and future), mirroring
// INTENT_PLAN_PROJECT_ALLOWLIST=*; the master flag still gates globally.
EnvSemanticSummaryReplaceSessions = "SEMANTIC_SUMMARY_REPLACE_SESSIONS"
)
// SummaryFeatureFlags controls semantic summary rollout. REPLACE swaps the
// served chat Narrative only for allowlisted sessions (fail-closed gray).
type SummaryFeatureFlags struct {
Shadow bool
Replace bool
// ReplaceAllSessions is the "*" wildcard for ReplaceSessions: every
// session (present and future) is eligible while the master Replace flag
// stays on. An empty allowlist still replaces nothing.
ReplaceAllSessions bool
ReplaceSessions map[string]struct{}
}
var (
summaryFlagsMu sync.RWMutex
activeSummaryFlags = loadSummaryFlagsFromEnv()
)
func loadSummaryFlagsFromEnv() SummaryFeatureFlags {
sessions, all := parseReplaceSessions(os.Getenv(EnvSemanticSummaryReplaceSessions))
return SummaryFeatureFlags{
Shadow: envBoolDefault(EnvSemanticSummaryShadow, false),
Replace: envBoolDefault(EnvSemanticSummaryReplace, false),
ReplaceAllSessions: all,
ReplaceSessions: sessions,
}
}
// parseReplaceSessions splits the comma-separated allowlist. Any "*" token
// widens to all sessions; empty or ID-only values keep the explicit set.
func parseReplaceSessions(raw string) (map[string]struct{}, bool) {
out := make(map[string]struct{})
for _, part := range strings.Split(raw, ",") {
id := strings.TrimSpace(part)
if id == "*" {
return nil, true
}
if id != "" {
out[id] = struct{}{}
}
}
return out, false
}
func envBoolDefault(key string, def bool) bool {
v := strings.TrimSpace(os.Getenv(key))
if v == "" {
return def
}
return v == "1" || strings.EqualFold(v, "true") || strings.EqualFold(v, "yes")
}
// CurrentSummaryFlags returns the active summary feature flags.
func CurrentSummaryFlags() SummaryFeatureFlags {
summaryFlagsMu.RLock()
defer summaryFlagsMu.RUnlock()
return activeSummaryFlags
}
// SemanticSummaryShadowEnabled reports whether shadow semantic summary is on.
func SemanticSummaryShadowEnabled() bool {
return CurrentSummaryFlags().Shadow
}
// SemanticSummaryReplaceEnabled reports the REPLACE master env bit.
// Narrative replacement additionally requires the session allowlist
// (SemanticReplaceEnabledFor); the master bit alone replaces nothing.
func SemanticSummaryReplaceEnabled() bool {
return CurrentSummaryFlags().Replace
}
// SemanticReplaceEnabledFor reports fail-closed gray eligibility: the master
// flag must be on AND the session must be in the allowlist (or the allowlist
// is the "*" wildcard). Empty allowlist replaces no session.
func SemanticReplaceEnabledFor(sessionID string) bool {
f := CurrentSummaryFlags()
if !f.Replace || sessionID == "" {
return false
}
if f.ReplaceAllSessions {
return true
}
_, ok := f.ReplaceSessions[sessionID]
return ok
}
// SetSummaryFlagsForTest overrides flags in tests.
func SetSummaryFlagsForTest(f SummaryFeatureFlags) {
summaryFlagsMu.Lock()
activeSummaryFlags = f
summaryFlagsMu.Unlock()
}
// ResetSummaryFlagsForTest restores flags from the process environment.
func ResetSummaryFlagsForTest() {
summaryFlagsMu.Lock()
activeSummaryFlags = loadSummaryFlagsFromEnv()
summaryFlagsMu.Unlock()
}