Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db00644cab | ||
|
|
125c99bb46 | ||
|
|
3e537bd582 | ||
|
|
4e6fc7aeee | ||
|
|
9a232ad444 | ||
|
|
3da0eca9b4 | ||
|
|
7d1213451c | ||
|
|
7c9293f4ed | ||
|
|
6e0155a2ab | ||
|
|
1cea2c9f78 | ||
|
|
9dcd60931a | ||
|
|
1fac90ed2a | ||
|
|
cb9c2513a4 | ||
|
|
3617a6c386 | ||
|
|
1938170131 | ||
|
|
b34717e6b8 | ||
|
|
d8d7e9a307 |
@@ -78,6 +78,14 @@ These rules apply to every task across every project, regardless of harness.
|
|||||||
- Surface the brain entries and issue numbers in the closing summary so the
|
- Surface the brain entries and issue numbers in the closing summary so the
|
||||||
trail is auditable.
|
trail is auditable.
|
||||||
|
|
||||||
|
## CAD sprint patterns
|
||||||
|
|
||||||
|
Two CAD sprint archetypes (defs: brain `wiki/homelab/decisions/continuous-agentic-development-cad-concept-2026-06-16`):
|
||||||
|
- **Inception Sprint** — birth a new repo. Oath-gated close; tag only once every clause holds. Brain: `.../inception-sprint-and-oath`.
|
||||||
|
- **Sweep Sprint** — clear an existing backlog. Parallel read-only research per issue → sequential one-issue-per-commit execution. Brain: `.../sweep-sprint-and-oath`.
|
||||||
|
|
||||||
|
Shared: decisions that are yours, not the agent's, surface as `DECISION NEEDED` (question/options/recommendation/blocks) — ask live if you're present, else comment and wait, never act past it. Live-breaking regressions get `git revert`, never forward-fixed; host-network work skips even read-only research (solo, synchronous, you present). Rule 6's closing ritual only fires once every Oath clause genuinely holds — blocked = descoped-and-tracked, never marked done.
|
||||||
|
|
||||||
## Default stack
|
## Default stack
|
||||||
|
|
||||||
| Layer | Default | Fallback | Last resort |
|
| Layer | Default | Fallback | Last resort |
|
||||||
@@ -88,7 +96,7 @@ These rules apply to every task across every project, regardless of harness.
|
|||||||
| Containers | Docker Compose (dev), k3s (prod) | — | — |
|
| Containers | Docker Compose (dev), k3s (prod) | — | — |
|
||||||
| DB | PostgreSQL + sqlc | SQLite | — |
|
| DB | PostgreSQL + sqlc | SQLite | — |
|
||||||
| Search | pgvector (vector), BM25 | Qdrant (when >1M vectors or hybrid retrieval) | — |
|
| Search | pgvector (vector), BM25 | Qdrant (when >1M vectors or hybrid retrieval) | — |
|
||||||
| Logging | slog (structured) | — | — |
|
| Logging | slog (structured) | stdlib `logging` w/ structured `extra=` (or structlog) | — |
|
||||||
| Testing | Table-driven, testify | — | — |
|
| Testing | Table-driven, testify | — | — |
|
||||||
| Agents (Go) | google.golang.org/adk + pkg/litellm adapter | — | — |
|
| Agents (Go) | google.golang.org/adk + pkg/litellm adapter | — | — |
|
||||||
|
|
||||||
@@ -97,7 +105,12 @@ Exploratory: Rust, Zig — I'll tell you when I want these.
|
|||||||
## Code conventions
|
## Code conventions
|
||||||
|
|
||||||
- **Go style**: golines, gofumpt, golangci-lint
|
- **Go style**: golines, gofumpt, golangci-lint
|
||||||
- **Errors**: `fmt.Errorf("operation: %w", err)` — never naked, never log-and-return
|
- **Python style** (fallback language): ruff (format+lint, one tool), mypy --strict (non-negotiable,
|
||||||
|
matches Go's static typing discipline), pytest + pytest-cov (table-driven via
|
||||||
|
`@pytest.mark.parametrize`), uv (venv+deps+lock, one tool), pydantic-settings (typed env-var config
|
||||||
|
— same principle as Go's typed structs), src-layout + `pyproject.toml` only (no `setup.py`)
|
||||||
|
- **Errors**: `fmt.Errorf("operation: %w", err)` — never naked, never log-and-return.
|
||||||
|
Python: `raise X from e` (exception chaining, same principle) — never bare `except`, never silent `pass`
|
||||||
- **Naming**: stdlib conventions, no stuttering
|
- **Naming**: stdlib conventions, no stuttering
|
||||||
- **Architecture**: prefer stdlib over frameworks, constructor injection, env-var config parsed into typed structs
|
- **Architecture**: prefer stdlib over frameworks, constructor injection, env-var config parsed into typed structs
|
||||||
- **Git**: conventional commits (`feat:`, `fix:`, `chore:`), commit directly to main,
|
- **Git**: conventional commits (`feat:`, `fix:`, `chore:`), commit directly to main,
|
||||||
|
|||||||
+17
-3
@@ -48,9 +48,23 @@ jobs:
|
|||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.INFRA_DEPLOY_KEY }}" > ~/.ssh/infra_deploy_key
|
echo "${{ secrets.INFRA_DEPLOY_KEY }}" > ~/.ssh/infra_deploy_key
|
||||||
chmod 600 ~/.ssh/infra_deploy_key
|
chmod 600 ~/.ssh/infra_deploy_key
|
||||||
printf 'Host git.d-ma.be\n HostName 127.0.0.1\n Port 30022\n StrictHostKeyChecking no\n' >> ~/.ssh/config
|
# In-cluster DNS to gitea's SSH NodePort service, not 127.0.0.1:30022
|
||||||
|
# (that only worked when act_runner ran on koala's bare host network;
|
||||||
GIT_SSH_COMMAND="ssh -i ~/.ssh/infra_deploy_key -o IdentitiesOnly=yes" \
|
# from inside the containerized runner's own pod netns, loopback
|
||||||
|
# never reaches the host — "Connection refused", found 2026-07-27).
|
||||||
|
#
|
||||||
|
# Pass as -o overrides on the ssh invocation itself, NOT appended to
|
||||||
|
# ~/.ssh/config: $HOME (/data) is a PVC that persists across job
|
||||||
|
# runs on this runner (same "workspace not ephemeral" class as
|
||||||
|
# brain: act-runner-host-executor-tmp-persists), so an appended
|
||||||
|
# line here would pile up duplicate `Host git.d-ma.be` blocks
|
||||||
|
# across every run — ssh_config is first-match-wins, so a stale
|
||||||
|
# entry from an earlier failed run would silently shadow this
|
||||||
|
# fix forever (exactly what happened once already: this fix's
|
||||||
|
# own first attempt got appended AFTER an already-stale entry
|
||||||
|
# and lost). CLI -o options always win regardless of file state,
|
||||||
|
# so this step is safe to re-run any number of times.
|
||||||
|
GIT_SSH_COMMAND="ssh -i ~/.ssh/infra_deploy_key -o IdentitiesOnly=yes -o HostName=gitea-ssh-nodeport.gitea.svc.cluster.local -o Port=22 -o StrictHostKeyChecking=no" \
|
||||||
git clone "${INFRA_REPO}" /tmp/infra-update
|
git clone "${INFRA_REPO}" /tmp/infra-update
|
||||||
|
|
||||||
cd /tmp/infra-update
|
cd /tmp/infra-update
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# gitleaks config for the hyperguild repo (infra#39 — leak prevention pass,
|
||||||
|
# Phase 3 checklist item: "gitleaks pre-commit hook in infra AND hyperguild").
|
||||||
|
#
|
||||||
|
# Ported from mathias/infra's .gitleaks.toml (2026-08-04) — same homelab
|
||||||
|
# token-shape rules, minus the SOPS/searxng allowlists infra needed (this
|
||||||
|
# repo doesn't use SOPS).
|
||||||
|
|
||||||
|
title = "hyperguild gitleaks config"
|
||||||
|
|
||||||
|
[extend]
|
||||||
|
useDefault = true
|
||||||
|
|
||||||
|
# --- Homelab-specific rules -------------------------------------------------
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
id = "homelab-static-bearer"
|
||||||
|
description = "Homelab MCP/LLM static bearer or API key assigned a long literal value"
|
||||||
|
regex = '''(?i)\b(DMABE_[A-Z0-9_]+|[A-Z0-9_]*MCP_TOKEN|ROUTING_MCP_TOKEN|INFRA_MCP_TOKEN|BRAIN_MCP_TOKEN|GITEA_MCP_TOKEN|LITELLM_MASTER_KEY|LITELLM_SALT_KEY|DMABE_LLMAPI_KEY|BRAIN_PG_DSN)\s*[:=]\s*['"]?([A-Za-z0-9/_+.\-]{16,})['"]?'''
|
||||||
|
keywords = ["dmabe_", "mcp_token", "litellm_master_key", "litellm_salt_key", "llmapi_key", "brain_pg_dsn"]
|
||||||
|
[[rules.allowlists]]
|
||||||
|
description = "Env indirection is not a literal secret"
|
||||||
|
regexes = [
|
||||||
|
'''os\.environ''',
|
||||||
|
'''valueFrom''',
|
||||||
|
'''secretKeyRef''',
|
||||||
|
'''\$\{?[A-Za-z_][A-Za-z0-9_]*\}?''',
|
||||||
|
'''REDACTED''',
|
||||||
|
'''<[A-Z_]+>''',
|
||||||
|
]
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
id = "homelab-authorization-bearer"
|
||||||
|
description = "Hardcoded Authorization: Bearer header"
|
||||||
|
regex = '''(?i)authorization['"]?\s*[:=]\s*['"]?bearer\s+([A-Za-z0-9/_+.\-=]{16,})'''
|
||||||
|
keywords = ["authorization", "bearer"]
|
||||||
|
[[rules.allowlists]]
|
||||||
|
description = "Env indirection is not a literal secret"
|
||||||
|
regexes = [
|
||||||
|
'''\$\{?[A-Za-z_][A-Za-z0-9_]*\}?''',
|
||||||
|
'''os\.environ''',
|
||||||
|
'''REDACTED''',
|
||||||
|
'''<[A-Z_]+>''',
|
||||||
|
]
|
||||||
|
|
||||||
|
# --- Global allowlist: claudewatcher's own scrubber test fixtures ------------
|
||||||
|
# ingestion/internal/claudewatcher/{scrubber,watcher}_test.go deliberately
|
||||||
|
# contain fake secret-shaped literals to test that the scrubber detects and
|
||||||
|
# redacts them. Verified 2026-08-04: all 9 findings here are test fixtures
|
||||||
|
# (github-pat, jwt, generic-api-key, homelab-authorization-bearer rules) plus
|
||||||
|
# 1 doc finding that was gitleaks matching the literal placeholder word
|
||||||
|
# "REDACTED" in a plan doc — not a real secret in either case.
|
||||||
|
[[allowlists]]
|
||||||
|
description = "claudewatcher scrubber test fixtures — deliberately fake secrets"
|
||||||
|
paths = [
|
||||||
|
'''ingestion/internal/claudewatcher/scrubber_test\.go$''',
|
||||||
|
'''ingestion/internal/claudewatcher/watcher_test\.go$''',
|
||||||
|
]
|
||||||
|
|
||||||
|
[[allowlists]]
|
||||||
|
description = "Literal placeholder word REDACTED matched as if it were a token (verified 2026-08-04: extracted Secret == 'REDACTED' exactly, gitleaks' curl-auth-header rule matched the placeholder text itself, not a real credential)"
|
||||||
|
condition = "AND"
|
||||||
|
paths = ['''docs/superpowers/plans/2026-04-22-phase4-attempt-wiring\.md$''']
|
||||||
|
regexes = ['''REDACTED''']
|
||||||
@@ -73,6 +73,14 @@ These rules apply to every task across every project, regardless of harness.
|
|||||||
- Surface the brain entries and issue numbers in the closing summary so the
|
- Surface the brain entries and issue numbers in the closing summary so the
|
||||||
trail is auditable.
|
trail is auditable.
|
||||||
|
|
||||||
|
## CAD sprint patterns
|
||||||
|
|
||||||
|
Two CAD sprint archetypes (defs: brain `wiki/homelab/decisions/continuous-agentic-development-cad-concept-2026-06-16`):
|
||||||
|
- **Inception Sprint** — birth a new repo. Oath-gated close; tag only once every clause holds. Brain: `.../inception-sprint-and-oath`.
|
||||||
|
- **Sweep Sprint** — clear an existing backlog. Parallel read-only research per issue → sequential one-issue-per-commit execution. Brain: `.../sweep-sprint-and-oath`.
|
||||||
|
|
||||||
|
Shared: decisions that are yours, not the agent's, surface as `DECISION NEEDED` (question/options/recommendation/blocks) — ask live if you're present, else comment and wait, never act past it. Live-breaking regressions get `git revert`, never forward-fixed; host-network work skips even read-only research (solo, synchronous, you present). Rule 6's closing ritual only fires once every Oath clause genuinely holds — blocked = descoped-and-tracked, never marked done.
|
||||||
|
|
||||||
## Default stack
|
## Default stack
|
||||||
|
|
||||||
| Layer | Default | Fallback | Last resort |
|
| Layer | Default | Fallback | Last resort |
|
||||||
@@ -83,7 +91,7 @@ These rules apply to every task across every project, regardless of harness.
|
|||||||
| Containers | Docker Compose (dev), k3s (prod) | — | — |
|
| Containers | Docker Compose (dev), k3s (prod) | — | — |
|
||||||
| DB | PostgreSQL + sqlc | SQLite | — |
|
| DB | PostgreSQL + sqlc | SQLite | — |
|
||||||
| Search | pgvector (vector), BM25 | Qdrant (when >1M vectors or hybrid retrieval) | — |
|
| Search | pgvector (vector), BM25 | Qdrant (when >1M vectors or hybrid retrieval) | — |
|
||||||
| Logging | slog (structured) | — | — |
|
| Logging | slog (structured) | stdlib `logging` w/ structured `extra=` (or structlog) | — |
|
||||||
| Testing | Table-driven, testify | — | — |
|
| Testing | Table-driven, testify | — | — |
|
||||||
| Agents (Go) | google.golang.org/adk + pkg/litellm adapter | — | — |
|
| Agents (Go) | google.golang.org/adk + pkg/litellm adapter | — | — |
|
||||||
|
|
||||||
@@ -92,7 +100,12 @@ Exploratory: Rust, Zig — I'll tell you when I want these.
|
|||||||
## Code conventions
|
## Code conventions
|
||||||
|
|
||||||
- **Go style**: golines, gofumpt, golangci-lint
|
- **Go style**: golines, gofumpt, golangci-lint
|
||||||
- **Errors**: `fmt.Errorf("operation: %w", err)` — never naked, never log-and-return
|
- **Python style** (fallback language): ruff (format+lint, one tool), mypy --strict (non-negotiable,
|
||||||
|
matches Go's static typing discipline), pytest + pytest-cov (table-driven via
|
||||||
|
`@pytest.mark.parametrize`), uv (venv+deps+lock, one tool), pydantic-settings (typed env-var config
|
||||||
|
— same principle as Go's typed structs), src-layout + `pyproject.toml` only (no `setup.py`)
|
||||||
|
- **Errors**: `fmt.Errorf("operation: %w", err)` — never naked, never log-and-return.
|
||||||
|
Python: `raise X from e` (exception chaining, same principle) — never bare `except`, never silent `pass`
|
||||||
- **Naming**: stdlib conventions, no stuttering
|
- **Naming**: stdlib conventions, no stuttering
|
||||||
- **Architecture**: prefer stdlib over frameworks, constructor injection, env-var config parsed into typed structs
|
- **Architecture**: prefer stdlib over frameworks, constructor injection, env-var config parsed into typed structs
|
||||||
- **Git**: conventional commits (`feat:`, `fix:`, `chore:`), commit directly to main,
|
- **Git**: conventional commits (`feat:`, `fix:`, `chore:`), commit directly to main,
|
||||||
|
|||||||
@@ -101,12 +101,48 @@ tasks:
|
|||||||
- task: lint
|
- task: lint
|
||||||
- task: test
|
- task: test
|
||||||
- task: vet
|
- task: vet
|
||||||
|
- task: security:gitleaks
|
||||||
|
|
||||||
|
# ── Security ─────────────────────────────────────────────
|
||||||
|
security:gitleaks:
|
||||||
|
desc: Scan the working tree for secrets (gitleaks, fail-closed; skipped if gitleaks absent)
|
||||||
|
dir: '{{.ROOT_DIR}}'
|
||||||
|
cmds:
|
||||||
|
- |
|
||||||
|
GL="$(command -v gitleaks || true)"
|
||||||
|
[ -z "$GL" ] && [ -x "$(go env GOPATH 2>/dev/null)/bin/gitleaks" ] && GL="$(go env GOPATH)/bin/gitleaks"
|
||||||
|
if [ -z "$GL" ]; then
|
||||||
|
echo "⚠ gitleaks not installed — skipping secret scan (CI enforces it)."
|
||||||
|
echo " Install: go install github.com/zricethezav/gitleaks/v8@latest"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
"$GL" detect --no-git --redact --config .gitleaks.toml --source .
|
||||||
|
|
||||||
|
security:gitleaks:history:
|
||||||
|
desc: "One-time FULL-HISTORY secret audit (infra#39 rotation pass; not a per-push gate)"
|
||||||
|
dir: '{{.ROOT_DIR}}'
|
||||||
|
cmds:
|
||||||
|
- |
|
||||||
|
GL="$(command -v gitleaks || true)"
|
||||||
|
[ -z "$GL" ] && [ -x "$(go env GOPATH 2>/dev/null)/bin/gitleaks" ] && GL="$(go env GOPATH)/bin/gitleaks"
|
||||||
|
if [ -z "$GL" ]; then
|
||||||
|
echo "gitleaks not installed: go install github.com/zricethezav/gitleaks/v8@latest" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
echo "Scanning FULL git history (redacted). Known historical leaks are expected"
|
||||||
|
echo "until the infra#39 rotation pass completes — triage against the rotation list."
|
||||||
|
"$GL" detect --redact --config .gitleaks.toml
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
cmds:
|
cmds:
|
||||||
- golangci-lint run ./...
|
- golangci-lint run ./...
|
||||||
- cd ingestion && golangci-lint run ./...
|
- cd ingestion && golangci-lint run ./...
|
||||||
|
|
||||||
|
lint:frontmatter:
|
||||||
|
desc: Report brain notes whose YAML frontmatter does not parse (BRAIN=path, FIX=1 repairs in place)
|
||||||
|
cmds:
|
||||||
|
- cd ingestion && go run ./cmd/frontmatterlint {{if .FIX}}-fix{{end}} {{.BRAIN | default "../../brain"}}
|
||||||
|
|
||||||
test:
|
test:
|
||||||
cmds:
|
cmds:
|
||||||
- go test -race -count=1 ./...
|
- go test -race -count=1 ./...
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
// Command frontmatterlint reports markdown notes whose YAML frontmatter does
|
||||||
|
// not parse, and optionally repairs the ones that only need quoting.
|
||||||
|
//
|
||||||
|
// go run ./cmd/frontmatterlint ../../brain # report
|
||||||
|
// go run ./cmd/frontmatterlint -fix ../../brain # repair in place
|
||||||
|
//
|
||||||
|
// Exit codes: 0 clean (or everything repaired), 1 findings remain, 2 the scan
|
||||||
|
// itself failed.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/frontmatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fix := flag.Bool("fix", false, "repair files that quoting can rescue, in place")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
root := "."
|
||||||
|
if flag.NArg() > 0 {
|
||||||
|
root = flag.Arg(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
findings, err := frontmatter.Scan(root, *fix)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "frontmatterlint:", err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
var unresolved int
|
||||||
|
for _, f := range findings {
|
||||||
|
if f.Repaired {
|
||||||
|
fmt.Printf("repaired %s\n", f.Path)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
unresolved++
|
||||||
|
fmt.Printf("BROKEN %s: %v\n", f.Path, f.Err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if unresolved > 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "\n%d file(s) with unparseable frontmatter", unresolved)
|
||||||
|
if !*fix {
|
||||||
|
fmt.Fprint(os.Stderr, " — re-run with -fix")
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stderr)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Printf("✓ frontmatter: %d file(s) repaired, none broken\n", len(findings))
|
||||||
|
}
|
||||||
@@ -32,6 +32,7 @@ import (
|
|||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/pipeline"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/pipeline"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/reranker"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/reranker"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/startupretry"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/vectorstore"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/vectorstore"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/watcher"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/watcher"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/webhook"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/webhook"
|
||||||
@@ -314,6 +315,36 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Built before the watcher, the graph backfill and session ingestion, and
|
||||||
|
// retried rather than fatal (#83). Two separate reasons:
|
||||||
|
//
|
||||||
|
// - On a cold cluster boot this pod can beat dex to serving and gets a
|
||||||
|
// 502 from ingress. Nothing is misconfigured; it simply started first.
|
||||||
|
// Exiting there meant CrashLoopBackOff redoing the backfill on every
|
||||||
|
// attempt, with exponential backoff stretching the brain outage.
|
||||||
|
// - Constructing it first means a genuine config error (wrong issuer,
|
||||||
|
// bad audience) fails in seconds instead of after all that work.
|
||||||
|
//
|
||||||
|
// The budget stays bounded: retrying forever would turn a misconfiguration
|
||||||
|
// into a process that looks alive and never serves.
|
||||||
|
jwtValidator, err := startupretry.Do(ctx,
|
||||||
|
startupretry.Policy{Attempts: 8, Base: time.Second, Max: 30 * time.Second},
|
||||||
|
func(ctx context.Context) (*chassisauth.JWTValidator, error) {
|
||||||
|
v, err := chassisauth.NewJWTValidator(ctx, os.Getenv("DEX_ISSUER_URL"), os.Getenv("MCP_AUDIENCE"))
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("oidc discovery not ready, retrying", "err", err)
|
||||||
|
}
|
||||||
|
return v, err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("build jwt validator", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if jwtValidator != nil {
|
||||||
|
logger.Info("jwt auth enabled", "issuer", os.Getenv("DEX_ISSUER_URL"))
|
||||||
|
}
|
||||||
|
|
||||||
if watchInterval > 0 {
|
if watchInterval > 0 {
|
||||||
watcher.Start(ctx, watcher.Config{
|
watcher.Start(ctx, watcher.Config{
|
||||||
BrainDir: brainDir,
|
BrainDir: brainDir,
|
||||||
@@ -418,15 +449,6 @@ func main() {
|
|||||||
if webhookHandler != nil {
|
if webhookHandler != nil {
|
||||||
mux.Handle("POST /webhooks/brain-sync", webhookHandler)
|
mux.Handle("POST /webhooks/brain-sync", webhookHandler)
|
||||||
}
|
}
|
||||||
jwtValidator, err := chassisauth.NewJWTValidator(ctx, os.Getenv("DEX_ISSUER_URL"), os.Getenv("MCP_AUDIENCE"))
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("build jwt validator", "err", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if jwtValidator != nil {
|
|
||||||
logger.Info("jwt auth enabled", "issuer", os.Getenv("DEX_ISSUER_URL"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resource-metadata URL is only emitted on 401 when Dex OAuth is
|
// Resource-metadata URL is only emitted on 401 when Dex OAuth is
|
||||||
// configured. Static-Bearer-only deployments leave this empty so
|
// configured. Static-Bearer-only deployments leave this empty so
|
||||||
// clients never see an OAuth challenge.
|
// clients never see an OAuth challenge.
|
||||||
@@ -457,12 +479,8 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
auditSink := buildAuditSink(ctx, brainDir, logger)
|
auditSink := buildAuditSink(ctx, brainDir, logger)
|
||||||
// The Gitea client also satisfies SummaryWriter (#66): session
|
|
||||||
// summaries are written to mathias/ai-sessions over the same API
|
|
||||||
// token. nil only if a future tracker impl lacks file writes.
|
|
||||||
summaryWriter, _ := tracker.(capture.SummaryWriter)
|
|
||||||
captureSvc := capture.NewService(
|
captureSvc := capture.NewService(
|
||||||
mcpSrv.BrainStore(), tracker, summaryWriter, classCfg, auditSink)
|
mcpSrv.BrainStore(), tracker, classCfg, auditSink)
|
||||||
sovereign := splitList(os.Getenv("BRAIN_CAPTURE_SOVEREIGN_PRINCIPALS"))
|
sovereign := splitList(os.Getenv("BRAIN_CAPTURE_SOVEREIGN_PRINCIPALS"))
|
||||||
resolver := capturehttp.NewOriginResolver(sovereign)
|
resolver := capturehttp.NewOriginResolver(sovereign)
|
||||||
captureH := capturehttp.New(captureSvc, jwtValidator, mcpToken, "local-cli", resolver)
|
captureH := capturehttp.New(captureSvc, jwtValidator, mcpToken, "local-cli", resolver)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
// Package capture is the Clean-Architecture use-case for the uniform
|
// Package capture is the Clean-Architecture use-case for the uniform
|
||||||
// capture capability (issue #49/#51): persist a finished session's
|
// capture capability (issue #49/#51): persist a finished session's
|
||||||
// valuable output — insights → brain, action items → Gitea tickets,
|
// valuable output — insights → brain, action items → Gitea tickets —
|
||||||
// optional summary → ai-sessions — with one invocation, identical core
|
// with one invocation, identical core behaviour across every harness.
|
||||||
// behaviour across every harness.
|
|
||||||
//
|
//
|
||||||
// This package is pure orchestration. It depends only on ports
|
// This package is pure orchestration. It depends only on ports
|
||||||
// (interfaces) and plain entities — no HTTP, no live Gitea, no embedding
|
// (interfaces) and plain entities — no HTTP, no live Gitea, no embedding
|
||||||
@@ -83,19 +82,11 @@ type Ticket struct {
|
|||||||
Body string
|
Body string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Summary is an optional session summary bound for ai-sessions.
|
|
||||||
type Summary struct {
|
|
||||||
Title string
|
|
||||||
Body string
|
|
||||||
ReposTouched []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureInput is the whole capture request.
|
// CaptureInput is the whole capture request.
|
||||||
type CaptureInput struct {
|
type CaptureInput struct {
|
||||||
Context CaptureContext
|
Context CaptureContext
|
||||||
Insights []Insight
|
Insights []Insight
|
||||||
Tickets []Ticket
|
Tickets []Ticket
|
||||||
Summary *Summary
|
|
||||||
DryRun bool
|
DryRun bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,12 +108,6 @@ type TicketResult struct {
|
|||||||
OK bool `json:"ok"`
|
OK bool `json:"ok"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SummaryResult is the summary outcome in the receipt.
|
|
||||||
type SummaryResult struct {
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
OK bool `json:"ok"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ItemError pins a failure to a specific request item for the partial
|
// ItemError pins a failure to a specific request item for the partial
|
||||||
// receipt. Item is a stable locator like "insight[1]" or "ticket[0]".
|
// receipt. Item is a stable locator like "insight[1]" or "ticket[0]".
|
||||||
type ItemError struct {
|
type ItemError struct {
|
||||||
@@ -136,7 +121,6 @@ type ItemError struct {
|
|||||||
type CaptureReceipt struct {
|
type CaptureReceipt struct {
|
||||||
Insights []InsightResult `json:"insights"`
|
Insights []InsightResult `json:"insights"`
|
||||||
Tickets []TicketResult `json:"tickets"`
|
Tickets []TicketResult `json:"tickets"`
|
||||||
Summary *SummaryResult `json:"summary,omitempty"`
|
|
||||||
Errors []ItemError `json:"errors"`
|
Errors []ItemError `json:"errors"`
|
||||||
EffectiveClassification string `json:"effective_classification,omitempty"`
|
EffectiveClassification string `json:"effective_classification,omitempty"`
|
||||||
DryRun bool `json:"dry_run"`
|
DryRun bool `json:"dry_run"`
|
||||||
|
|||||||
@@ -69,11 +69,6 @@ type IssueTracker interface {
|
|||||||
CommentIssue(ctx context.Context, repo string, number int, body string) (IssueRef, error)
|
CommentIssue(ctx context.Context, repo string, number int, body string) (IssueRef, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SummaryWriter is the ai-sessions summary port.
|
|
||||||
type SummaryWriter interface {
|
|
||||||
WriteFile(ctx context.Context, repo, path, content string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClassificationPolicy derives a target's sensitivity (model C). The
|
// ClassificationPolicy derives a target's sensitivity (model C). The
|
||||||
// "stricter wins" combination of declared vs derived is use-case policy
|
// "stricter wins" combination of declared vs derived is use-case policy
|
||||||
// and lives in the service, so the port stays minimal. Satisfied by
|
// and lives in the service, so the port stays minimal. Satisfied by
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package capture
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -14,22 +12,19 @@ import (
|
|||||||
|
|
||||||
// Service is the CaptureSession use-case. It depends only on ports.
|
// Service is the CaptureSession use-case. It depends only on ports.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
brain BrainStore
|
brain BrainStore
|
||||||
issues IssueTracker
|
issues IssueTracker
|
||||||
summaries SummaryWriter
|
policy ClassificationPolicy
|
||||||
policy ClassificationPolicy
|
audit AuditSink
|
||||||
audit AuditSink
|
|
||||||
|
|
||||||
// now is the clock, injectable for deterministic summary paths and
|
// now is the clock, injectable for deterministic audit timestamps in
|
||||||
// audit timestamps in tests.
|
// tests.
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewService constructs a Service from its ports. summaries may be nil
|
// NewService constructs a Service from its ports.
|
||||||
// when no summary persistence is wired; a CaptureInput with a Summary
|
func NewService(b BrainStore, tr IssueTracker, p ClassificationPolicy, a AuditSink) *Service {
|
||||||
// then fails that item rather than panicking.
|
return &Service{brain: b, issues: tr, policy: p, audit: a, now: time.Now}
|
||||||
func NewService(b BrainStore, tr IssueTracker, sw SummaryWriter, p ClassificationPolicy, a AuditSink) *Service {
|
|
||||||
return &Service{brain: b, issues: tr, summaries: sw, policy: p, audit: a, now: time.Now}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var validActions = map[string]bool{"create": true, "close": true, "comment": true}
|
var validActions = map[string]bool{"create": true, "close": true, "comment": true}
|
||||||
@@ -88,7 +83,7 @@ func (s *Service) Capture(ctx context.Context, in CaptureInput) (CaptureReceipt,
|
|||||||
declared, _ = classification.ParseLevel(in.Context.Classification)
|
declared, _ = classification.ParseLevel(in.Context.Classification)
|
||||||
}
|
}
|
||||||
|
|
||||||
effective, securityEvents := s.resolveClassification(declared, in)
|
effective, securityEvents, escalators := s.resolveClassification(declared, in)
|
||||||
|
|
||||||
// Server-derived origin governs the I1 gate; a caller-asserted harness
|
// Server-derived origin governs the I1 gate; a caller-asserted harness
|
||||||
// label that names a different zone is descriptive-only and logged as a
|
// label that names a different zone is descriptive-only and logged as a
|
||||||
@@ -112,8 +107,8 @@ func (s *Service) Capture(ctx context.Context, in CaptureInput) (CaptureReceipt,
|
|||||||
Items: nil, // refused before any write
|
Items: nil, // refused before any write
|
||||||
SecurityEvents: append(securityEvents, "I1 refusal: confidential capture via us-nexus origin"),
|
SecurityEvents: append(securityEvents, "I1 refusal: confidential capture via us-nexus origin"),
|
||||||
}, AuditCentral)
|
}, AuditCentral)
|
||||||
return CaptureReceipt{}, fmt.Errorf("%w: effective classification confidential through %s origin",
|
return CaptureReceipt{}, fmt.Errorf("%w: effective classification confidential through %s origin (%s)",
|
||||||
ErrSovereigntyRefused, in.Context.Origin)
|
ErrSovereigntyRefused, in.Context.Origin, escalationReason(escalators))
|
||||||
}
|
}
|
||||||
|
|
||||||
receipt := CaptureReceipt{
|
receipt := CaptureReceipt{
|
||||||
@@ -131,9 +126,6 @@ func (s *Service) Capture(ctx context.Context, in CaptureInput) (CaptureReceipt,
|
|||||||
for _, tk := range in.Tickets {
|
for _, tk := range in.Tickets {
|
||||||
receipt.Tickets = append(receipt.Tickets, TicketResult{Repo: tk.Repo, Action: tk.Action, Number: tk.Number, OK: true})
|
receipt.Tickets = append(receipt.Tickets, TicketResult{Repo: tk.Repo, Action: tk.Action, Number: tk.Number, OK: true})
|
||||||
}
|
}
|
||||||
if in.Summary != nil {
|
|
||||||
receipt.Summary = &SummaryResult{Path: s.summaryPath(in.Context, in.Summary), OK: true}
|
|
||||||
}
|
|
||||||
return receipt, nil
|
return receipt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,16 +161,6 @@ func (s *Service) Capture(ctx context.Context, in CaptureInput) (CaptureReceipt,
|
|||||||
landed = append(landed, fmt.Sprintf("ticket:%s#%d", tk.Repo, res.Number))
|
landed = append(landed, fmt.Sprintf("ticket:%s#%d", tk.Repo, res.Number))
|
||||||
}
|
}
|
||||||
|
|
||||||
if in.Summary != nil {
|
|
||||||
res, err := s.persistSummary(ctx, in.Context, in.Summary)
|
|
||||||
receipt.Summary = &res
|
|
||||||
if err != nil {
|
|
||||||
receipt.Errors = append(receipt.Errors, ItemError{Item: "summary", Error: err.Error()})
|
|
||||||
} else {
|
|
||||||
landed = append(landed, "summary:"+res.Path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// I5: persist the request-level audit record of exactly what landed,
|
// I5: persist the request-level audit record of exactly what landed,
|
||||||
// using the outcome reserved before the writes. AuditBuffered surfaces
|
// using the outcome reserved before the writes. AuditBuffered surfaces
|
||||||
// the degraded (locally-buffered) state on the receipt.
|
// the degraded (locally-buffered) state on the receipt.
|
||||||
@@ -239,15 +221,28 @@ func (s *Service) validate(in CaptureInput) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveClassification computes the effective level (stricter of
|
// resolveClassification computes the effective level (stricter of declared
|
||||||
// declared and every target's derived level) and collects a security
|
// and every target's derived level), collects a security event whenever the
|
||||||
// event whenever the caller under-declared relative to a target floor.
|
// caller under-declared relative to a target floor, and names the targets
|
||||||
func (s *Service) resolveClassification(declared classification.Level, in CaptureInput) (classification.Level, []string) {
|
// that carry the strictest level.
|
||||||
|
//
|
||||||
|
// That third return is what makes an I1 refusal actionable. A caller sending
|
||||||
|
// twenty tickets gets one refusal; without the offending target it cannot
|
||||||
|
// tell which repo tripped the gate, and infra#265 shows what happens next —
|
||||||
|
// the caller concluded the tickets ARRAY was broken, built a fallback of
|
||||||
|
// twenty individual calls, and filed a bug against the wrong thing.
|
||||||
|
func (s *Service) resolveClassification(declared classification.Level, in CaptureInput) (classification.Level, []string, []string) {
|
||||||
effective := declared
|
effective := declared
|
||||||
var events []string
|
var events []string
|
||||||
|
derivedFor := map[string]classification.Level{}
|
||||||
|
var order []string
|
||||||
consider := func(kind classification.TargetKind, name string) {
|
consider := func(kind classification.TargetKind, name string) {
|
||||||
derived := s.policy.Derive(classification.Target{Kind: kind, Name: name})
|
derived := s.policy.Derive(classification.Target{Kind: kind, Name: name})
|
||||||
effective = classification.Stricter(effective, derived)
|
effective = classification.Stricter(effective, derived)
|
||||||
|
if _, seen := derivedFor[name]; !seen {
|
||||||
|
derivedFor[name] = derived
|
||||||
|
order = append(order, name)
|
||||||
|
}
|
||||||
if declared < derived {
|
if declared < derived {
|
||||||
events = append(events, fmt.Sprintf("classification under-declared: declared=%s target=%s(%s) derived=%s",
|
events = append(events, fmt.Sprintf("classification under-declared: declared=%s target=%s(%s) derived=%s",
|
||||||
declared, name, kindString(kind), derived))
|
declared, name, kindString(kind), derived))
|
||||||
@@ -259,12 +254,28 @@ func (s *Service) resolveClassification(declared classification.Level, in Captur
|
|||||||
for _, tk := range in.Tickets {
|
for _, tk := range in.Tickets {
|
||||||
consider(classification.RepoTarget, tk.Repo)
|
consider(classification.RepoTarget, tk.Repo)
|
||||||
}
|
}
|
||||||
if in.Summary != nil {
|
|
||||||
for _, repo := range in.Summary.ReposTouched {
|
// Only targets that actually reach the effective level are named: at
|
||||||
consider(classification.RepoTarget, repo)
|
// twenty tickets, listing the innocent ones is noise that hides the one
|
||||||
|
// that matters.
|
||||||
|
var escalators []string
|
||||||
|
for _, name := range order {
|
||||||
|
if derivedFor[name] == effective && effective > declared {
|
||||||
|
escalators = append(escalators, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return effective, events
|
return effective, events, escalators
|
||||||
|
}
|
||||||
|
|
||||||
|
// escalationReason renders why the effective classification landed where it
|
||||||
|
// did, for an operator reading a refusal. Naming the caller's own declaration
|
||||||
|
// matters as much as naming a target: "you asked for this" and "a repo you
|
||||||
|
// touched required it" call for different fixes.
|
||||||
|
func escalationReason(escalators []string) string {
|
||||||
|
if len(escalators) == 0 {
|
||||||
|
return "declared by the caller; no target required it"
|
||||||
|
}
|
||||||
|
return "required by: " + strings.Join(escalators, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) persistInsight(ctx context.Context, ins Insight) (InsightResult, string, error) {
|
func (s *Service) persistInsight(ctx context.Context, ins Insight) (InsightResult, string, error) {
|
||||||
@@ -309,60 +320,6 @@ func (s *Service) persistTicket(ctx context.Context, tk Ticket) (TicketResult, e
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) persistSummary(ctx context.Context, c CaptureContext, sum *Summary) (SummaryResult, error) {
|
|
||||||
if s.summaries == nil {
|
|
||||||
return SummaryResult{OK: false}, fmt.Errorf("no summary writer configured")
|
|
||||||
}
|
|
||||||
path := s.summaryPath(c, sum)
|
|
||||||
content := s.renderSummary(c, sum)
|
|
||||||
repo := "ai-sessions"
|
|
||||||
if err := s.summaries.WriteFile(ctx, repo, path, content); err != nil {
|
|
||||||
return SummaryResult{Path: path, OK: false}, err
|
|
||||||
}
|
|
||||||
return SummaryResult{Path: path, OK: true}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// summaryPath builds summaries/<harness>/<YYYY-MM>/<date>-<slug>-<ref8>.md.
|
|
||||||
// The ref8 disambiguator is derived from the session_ref (or the title
|
|
||||||
// when no ref is present) so distinct sessions never collide.
|
|
||||||
func (s *Service) summaryPath(c CaptureContext, sum *Summary) string {
|
|
||||||
t := s.now().UTC()
|
|
||||||
slug := brain.Sanitise(sum.Title)
|
|
||||||
if slug == "" {
|
|
||||||
slug = "summary"
|
|
||||||
}
|
|
||||||
seed := c.SessionRef
|
|
||||||
if seed == "" {
|
|
||||||
seed = sum.Title + sum.Body
|
|
||||||
}
|
|
||||||
sum8 := shortHash(seed)
|
|
||||||
return fmt.Sprintf("summaries/%s/%s/%s-%s-%s.md",
|
|
||||||
brain.Sanitise(c.Harness), t.Format("2006-01"), t.Format("2006-01-02"), slug, sum8)
|
|
||||||
}
|
|
||||||
|
|
||||||
// renderSummary stamps fidelity + session metadata into frontmatter so the
|
|
||||||
// richer-fidelity-supersedes-thinner collision rule has the data it needs.
|
|
||||||
func (s *Service) renderSummary(c CaptureContext, sum *Summary) string {
|
|
||||||
var b strings.Builder
|
|
||||||
b.WriteString("---\n")
|
|
||||||
fmt.Fprintf(&b, "title: %s\n", sum.Title)
|
|
||||||
fmt.Fprintf(&b, "harness: %s\n", c.Harness)
|
|
||||||
if c.SessionRef != "" {
|
|
||||||
fmt.Fprintf(&b, "session_ref: %s\n", c.SessionRef)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&b, "fidelity: %s\n", c.Fidelity)
|
|
||||||
fmt.Fprintf(&b, "captured_at: %s\n", s.now().UTC().Format(time.RFC3339))
|
|
||||||
if len(sum.ReposTouched) > 0 {
|
|
||||||
fmt.Fprintf(&b, "repos_touched: [%s]\n", strings.Join(sum.ReposTouched, ", "))
|
|
||||||
}
|
|
||||||
b.WriteString("---\n\n")
|
|
||||||
b.WriteString(sum.Body)
|
|
||||||
if !strings.HasSuffix(sum.Body, "\n") {
|
|
||||||
b.WriteByte('\n')
|
|
||||||
}
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func kindString(k classification.TargetKind) string {
|
func kindString(k classification.TargetKind) string {
|
||||||
if k == classification.RepoTarget {
|
if k == classification.RepoTarget {
|
||||||
return "repo"
|
return "repo"
|
||||||
@@ -381,8 +338,3 @@ func firstLine(s string) string {
|
|||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func shortHash(s string) string {
|
|
||||||
sum := sha256.Sum256([]byte(s))
|
|
||||||
return hex.EncodeToString(sum[:])[:8]
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package capture
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -59,12 +60,18 @@ type fakeTracker struct {
|
|||||||
closed []int
|
closed []int
|
||||||
comments []int
|
comments []int
|
||||||
err error
|
err error
|
||||||
|
// failOnTitle makes CreateIssue fail for one specific title, so a
|
||||||
|
// partial-failure receipt can be exercised without failing every item.
|
||||||
|
failOnTitle string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeTracker) CreateIssue(_ context.Context, repo, title, _ string) (IssueRef, error) {
|
func (f *fakeTracker) CreateIssue(_ context.Context, repo, title, _ string) (IssueRef, error) {
|
||||||
if f.err != nil {
|
if f.err != nil {
|
||||||
return IssueRef{}, f.err
|
return IssueRef{}, f.err
|
||||||
}
|
}
|
||||||
|
if f.failOnTitle != "" && title == f.failOnTitle {
|
||||||
|
return IssueRef{}, errors.New("tracker refused this one")
|
||||||
|
}
|
||||||
f.created = append(f.created, repo+":"+title)
|
f.created = append(f.created, repo+":"+title)
|
||||||
return IssueRef{Repo: repo, Number: 100 + len(f.created), URL: "https://git/" + repo + "/issues/x"}, nil
|
return IssueRef{Repo: repo, Number: 100 + len(f.created), URL: "https://git/" + repo + "/issues/x"}, nil
|
||||||
}
|
}
|
||||||
@@ -85,24 +92,11 @@ func (f *fakeTracker) CommentIssue(_ context.Context, repo string, number int, _
|
|||||||
return IssueRef{Repo: repo, Number: number}, nil
|
return IssueRef{Repo: repo, Number: number}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeSummary struct {
|
|
||||||
paths []string
|
|
||||||
content []string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeSummary) WriteFile(_ context.Context, _, path, content string) error {
|
|
||||||
if f.err != nil {
|
|
||||||
return f.err
|
|
||||||
}
|
|
||||||
f.paths = append(f.paths, path)
|
|
||||||
f.content = append(f.content, content)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fakePolicy derives from an explicit map; default Internal so tests pin
|
// fakePolicy derives from an explicit map; default Internal so tests pin
|
||||||
// behaviour without depending on the real defaulting.
|
// behaviour without depending on the real defaulting.
|
||||||
type fakePolicy struct{ tags map[string]classification.Level }
|
type fakePolicy struct {
|
||||||
|
tags map[string]classification.Level
|
||||||
|
}
|
||||||
|
|
||||||
func (p fakePolicy) Derive(t classification.Target) classification.Level {
|
func (p fakePolicy) Derive(t classification.Target) classification.Level {
|
||||||
if lvl, ok := p.tags[t.Name]; ok {
|
if lvl, ok := p.tags[t.Name]; ok {
|
||||||
@@ -135,8 +129,8 @@ func (f *fakeAudit) Record(_ context.Context, e AuditEntry, _ AuditOutcome) erro
|
|||||||
|
|
||||||
// --- helpers ---
|
// --- helpers ---
|
||||||
|
|
||||||
func newSvc(b BrainStore, tr IssueTracker, sw SummaryWriter, p ClassificationPolicy, a AuditSink) *Service {
|
func newSvc(b BrainStore, tr IssueTracker, p ClassificationPolicy, a AuditSink) *Service {
|
||||||
s := NewService(b, tr, sw, p, a)
|
s := NewService(b, tr, p, a)
|
||||||
s.now = func() time.Time { return time.Date(2026, 6, 22, 12, 0, 0, 0, time.UTC) }
|
s.now = func() time.Time { return time.Date(2026, 6, 22, 12, 0, 0, 0, time.UTC) }
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -151,7 +145,7 @@ func TestCaptureHappyPath(t *testing.T) {
|
|||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
svc := newSvc(b, tr, nil, fakePolicy{}, au)
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -180,7 +174,7 @@ func TestCaptureHappyPath(t *testing.T) {
|
|||||||
|
|
||||||
func TestCaptureSupersedeNotDuplicate(t *testing.T) {
|
func TestCaptureSupersedeNotDuplicate(t *testing.T) {
|
||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
svc := newSvc(b, &fakeTracker{}, nil, fakePolicy{}, &fakeAudit{})
|
svc := newSvc(b, &fakeTracker{}, fakePolicy{}, &fakeAudit{})
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -197,7 +191,7 @@ func TestCaptureValidationFailClosed(t *testing.T) {
|
|||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
svc := newSvc(b, tr, nil, fakePolicy{}, au)
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
_, err := svc.Capture(context.Background(), CaptureInput{
|
_, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -216,7 +210,7 @@ func TestCaptureValidationFailClosed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCaptureValidationRejectsBadTicket(t *testing.T) {
|
func TestCaptureValidationRejectsBadTicket(t *testing.T) {
|
||||||
svc := newSvc(&fakeBrain{}, &fakeTracker{}, nil, fakePolicy{}, &fakeAudit{})
|
svc := newSvc(&fakeBrain{}, &fakeTracker{}, fakePolicy{}, &fakeAudit{})
|
||||||
_, err := svc.Capture(context.Background(), CaptureInput{
|
_, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
Tickets: []Ticket{{Repo: "hyperguild", Action: "frobnicate"}}, // bad action
|
Tickets: []Ticket{{Repo: "hyperguild", Action: "frobnicate"}}, // bad action
|
||||||
@@ -239,7 +233,7 @@ func TestCapturePartialFailureBestEffort(t *testing.T) {
|
|||||||
}}
|
}}
|
||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
svc := newSvc(b, tr, nil, fakePolicy{}, au)
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -264,7 +258,7 @@ func TestCaptureDryRunWritesNothing(t *testing.T) {
|
|||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
svc := newSvc(b, tr, nil, fakePolicy{}, au)
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -287,7 +281,7 @@ func TestCaptureStricterClassificationWins(t *testing.T) {
|
|||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
||||||
svc := newSvc(b, &fakeTracker{}, nil, pol, au)
|
svc := newSvc(b, &fakeTracker{}, pol, au)
|
||||||
|
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Classification = "internal"
|
ctx.Classification = "internal"
|
||||||
@@ -306,7 +300,7 @@ func TestCaptureCallerRaisingSensitivityHonoured(t *testing.T) {
|
|||||||
// Caller declares confidential; target internal → effective confidential, NOT a security event.
|
// Caller declares confidential; target internal → effective confidential, NOT a security event.
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
pol := fakePolicy{tags: map[string]classification.Level{"hyperguild": classification.Internal}}
|
pol := fakePolicy{tags: map[string]classification.Level{"hyperguild": classification.Internal}}
|
||||||
svc := newSvc(&fakeBrain{}, &fakeTracker{}, nil, pol, au)
|
svc := newSvc(&fakeBrain{}, &fakeTracker{}, pol, au)
|
||||||
|
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Classification = "confidential"
|
ctx.Classification = "confidential"
|
||||||
@@ -319,26 +313,6 @@ func TestCaptureCallerRaisingSensitivityHonoured(t *testing.T) {
|
|||||||
assert.Empty(t, au.entries[0].SecurityEvents, "raising sensitivity is honoured, not flagged")
|
assert.Empty(t, au.entries[0].SecurityEvents, "raising sensitivity is honoured, not flagged")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCaptureSummaryPathAndFidelity(t *testing.T) {
|
|
||||||
sw := &fakeSummary{}
|
|
||||||
svc := newSvc(&fakeBrain{}, &fakeTracker{}, sw, fakePolicy{}, &fakeAudit{})
|
|
||||||
|
|
||||||
ctx := baseCtx()
|
|
||||||
ctx.Fidelity = "transcript-parse"
|
|
||||||
ctx.SessionRef = "abc123def456"
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
|
||||||
Context: ctx,
|
|
||||||
Summary: &Summary{Title: "Session Wrap", Body: "did stuff", ReposTouched: []string{"hyperguild"}},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rec.Summary)
|
|
||||||
assert.True(t, rec.Summary.OK)
|
|
||||||
require.Len(t, sw.paths, 1)
|
|
||||||
assert.True(t, strings.HasPrefix(sw.paths[0], "summaries/claude-code/2026-06/"), "path: %s", sw.paths[0])
|
|
||||||
assert.Contains(t, sw.paths[0], "session-wrap")
|
|
||||||
assert.Contains(t, sw.content[0], "fidelity: transcript-parse", "fidelity stamped in frontmatter")
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- I1 sovereignty gate (#53) ---
|
// --- I1 sovereignty gate (#53) ---
|
||||||
|
|
||||||
func TestCaptureRefusesConfidentialViaUSNexus(t *testing.T) {
|
func TestCaptureRefusesConfidentialViaUSNexus(t *testing.T) {
|
||||||
@@ -346,7 +320,7 @@ func TestCaptureRefusesConfidentialViaUSNexus(t *testing.T) {
|
|||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
||||||
svc := newSvc(b, tr, nil, pol, au)
|
svc := newSvc(b, tr, pol, au)
|
||||||
|
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Classification = "confidential"
|
ctx.Classification = "confidential"
|
||||||
@@ -368,7 +342,7 @@ func TestCaptureRefusesConfidentialViaUSNexus(t *testing.T) {
|
|||||||
func TestCaptureAllowsConfidentialViaSovereign(t *testing.T) {
|
func TestCaptureAllowsConfidentialViaSovereign(t *testing.T) {
|
||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
||||||
svc := newSvc(b, &fakeTracker{}, nil, pol, &fakeAudit{})
|
svc := newSvc(b, &fakeTracker{}, pol, &fakeAudit{})
|
||||||
|
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Classification = "confidential"
|
ctx.Classification = "confidential"
|
||||||
@@ -387,7 +361,7 @@ func TestCaptureAssertedLabelIgnoredAndLogged(t *testing.T) {
|
|||||||
// us-nexus; confidential ⇒ refused, and the discrepancy is a security event.
|
// us-nexus; confidential ⇒ refused, and the discrepancy is a security event.
|
||||||
au := &fakeAudit{}
|
au := &fakeAudit{}
|
||||||
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
pol := fakePolicy{tags: map[string]classification.Level{"client-seb": classification.Confidential}}
|
||||||
svc := newSvc(&fakeBrain{}, &fakeTracker{}, nil, pol, au)
|
svc := newSvc(&fakeBrain{}, &fakeTracker{}, pol, au)
|
||||||
|
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Harness = "sovereign-soil" // asserted
|
ctx.Harness = "sovereign-soil" // asserted
|
||||||
@@ -407,7 +381,7 @@ func TestCaptureAssertedLabelIgnoredAndLogged(t *testing.T) {
|
|||||||
func TestCaptureInternalViaUSNexusAllowed(t *testing.T) {
|
func TestCaptureInternalViaUSNexusAllowed(t *testing.T) {
|
||||||
// us-nexus origin is fine for non-confidential data.
|
// us-nexus origin is fine for non-confidential data.
|
||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
svc := newSvc(b, &fakeTracker{}, nil, fakePolicy{}, &fakeAudit{})
|
svc := newSvc(b, &fakeTracker{}, fakePolicy{}, &fakeAudit{})
|
||||||
ctx := baseCtx()
|
ctx := baseCtx()
|
||||||
ctx.Origin = ZoneUSNexus // internal classification, so gate doesn't fire
|
ctx.Origin = ZoneUSNexus // internal classification, so gate doesn't fire
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
@@ -426,7 +400,7 @@ func TestCaptureRefusesWhenAuditReserveFails(t *testing.T) {
|
|||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
tr := &fakeTracker{}
|
tr := &fakeTracker{}
|
||||||
au := &fakeAudit{reserveErr: errors.New("central sink unreachable")}
|
au := &fakeAudit{reserveErr: errors.New("central sink unreachable")}
|
||||||
svc := newSvc(b, tr, nil, fakePolicy{}, au)
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
_, err := svc.Capture(context.Background(), CaptureInput{
|
_, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -443,7 +417,7 @@ func TestCaptureFlagsLocallyBufferedAudit(t *testing.T) {
|
|||||||
// proceeds and the receipt flags the degraded audit state.
|
// proceeds and the receipt flags the degraded audit state.
|
||||||
b := &fakeBrain{}
|
b := &fakeBrain{}
|
||||||
au := &fakeAudit{reserveMode: AuditBuffered}
|
au := &fakeAudit{reserveMode: AuditBuffered}
|
||||||
svc := newSvc(b, &fakeTracker{}, nil, fakePolicy{}, au)
|
svc := newSvc(b, &fakeTracker{}, fakePolicy{}, au)
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -458,7 +432,7 @@ func TestCaptureFlagsLocallyBufferedAudit(t *testing.T) {
|
|||||||
func TestCaptureDryRunSkipsAuditGate(t *testing.T) {
|
func TestCaptureDryRunSkipsAuditGate(t *testing.T) {
|
||||||
// dry_run must not even probe the audit sink (writes nothing anywhere).
|
// dry_run must not even probe the audit sink (writes nothing anywhere).
|
||||||
au := &fakeAudit{reserveErr: errors.New("would refuse")}
|
au := &fakeAudit{reserveErr: errors.New("would refuse")}
|
||||||
svc := newSvc(&fakeBrain{}, &fakeTracker{}, nil, fakePolicy{}, au)
|
svc := newSvc(&fakeBrain{}, &fakeTracker{}, fakePolicy{}, au)
|
||||||
|
|
||||||
rec, err := svc.Capture(context.Background(), CaptureInput{
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
Context: baseCtx(),
|
Context: baseCtx(),
|
||||||
@@ -469,3 +443,98 @@ func TestCaptureDryRunSkipsAuditGate(t *testing.T) {
|
|||||||
assert.True(t, rec.DryRun)
|
assert.True(t, rec.DryRun)
|
||||||
assert.Empty(t, au.entries)
|
assert.Empty(t, au.entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- hyperguild#91 part 1 -------------------------------------------------
|
||||||
|
//
|
||||||
|
// The reported symptom was "capture with a tickets array fails" (infra#265:
|
||||||
|
// 20 tickets errored, 5 errored, caller fell back to 20 individual
|
||||||
|
// issue_create calls). The array was never the failing dimension — every
|
||||||
|
// ticket targeted a client repo, and the I1 gate refuses that through a
|
||||||
|
// us-nexus origin at ANY length. The refusal did not say which target
|
||||||
|
// escalated the classification, so it read as a malfunction.
|
||||||
|
|
||||||
|
func TestCapture_TicketsArrayWorksAtLengthOne(t *testing.T) {
|
||||||
|
b, tr, au := &fakeBrain{}, &fakeTracker{}, &fakeAudit{}
|
||||||
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
|
Context: baseCtx(),
|
||||||
|
Tickets: []Ticket{{Repo: "hyperguild", Action: "create", Title: "one", Body: "b"}},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, rec.Tickets, 1)
|
||||||
|
assert.True(t, rec.Tickets[0].OK)
|
||||||
|
assert.Len(t, tr.created, 1)
|
||||||
|
assert.Empty(t, rec.Errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCapture_TicketsArrayWorksAtLengthMany(t *testing.T) {
|
||||||
|
b, tr, au := &fakeBrain{}, &fakeTracker{}, &fakeAudit{}
|
||||||
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
|
in := CaptureInput{Context: baseCtx()}
|
||||||
|
for i := range 20 {
|
||||||
|
in.Tickets = append(in.Tickets, Ticket{
|
||||||
|
Repo: "hyperguild", Action: "create", Title: fmt.Sprintf("t%02d", i), Body: "b",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, err := svc.Capture(context.Background(), in)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, rec.Tickets, 20, "one receipt entry per ticket")
|
||||||
|
for i, r := range rec.Tickets {
|
||||||
|
assert.True(t, r.OK, "ticket %d failed: %+v", i, rec.Errors)
|
||||||
|
}
|
||||||
|
assert.Len(t, tr.created, 20)
|
||||||
|
assert.Empty(t, rec.Errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCapture_OneFailingTicketDoesNotSinkTheRest(t *testing.T) {
|
||||||
|
// infra#265 asked explicitly: "if ticket 3 of 5 fails, what happened to
|
||||||
|
// 1 and 2?" They land, and the receipt says so per item.
|
||||||
|
b, au := &fakeBrain{}, &fakeAudit{}
|
||||||
|
tr := &fakeTracker{failOnTitle: "boom"}
|
||||||
|
svc := newSvc(b, tr, fakePolicy{}, au)
|
||||||
|
|
||||||
|
rec, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
|
Context: baseCtx(),
|
||||||
|
Tickets: []Ticket{
|
||||||
|
{Repo: "hyperguild", Action: "create", Title: "ok-1", Body: "b"},
|
||||||
|
{Repo: "hyperguild", Action: "create", Title: "boom", Body: "b"},
|
||||||
|
{Repo: "hyperguild", Action: "create", Title: "ok-3", Body: "b"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "a per-item failure is a partial receipt, not a call error")
|
||||||
|
require.Len(t, rec.Tickets, 3)
|
||||||
|
assert.True(t, rec.Tickets[0].OK)
|
||||||
|
assert.False(t, rec.Tickets[1].OK)
|
||||||
|
assert.True(t, rec.Tickets[2].OK)
|
||||||
|
require.Len(t, rec.Errors, 1)
|
||||||
|
assert.Contains(t, rec.Errors[0].Item, "ticket[1]")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCapture_I1RefusalNamesWhatEscalatedTheClassification(t *testing.T) {
|
||||||
|
// The actual defect behind infra#265. Without the offending target in
|
||||||
|
// the message, a caller with 20 tickets cannot tell which repo tripped
|
||||||
|
// the gate — and reasonably concludes the array itself is broken.
|
||||||
|
b, tr, au := &fakeBrain{}, &fakeTracker{}, &fakeAudit{}
|
||||||
|
pol := fakePolicy{tags: map[string]classification.Level{"trucker-ecommerce": classification.Confidential}}
|
||||||
|
svc := newSvc(b, tr, pol, au)
|
||||||
|
|
||||||
|
ctx := baseCtx()
|
||||||
|
ctx.Origin = ZoneUSNexus
|
||||||
|
_, err := svc.Capture(context.Background(), CaptureInput{
|
||||||
|
Context: ctx,
|
||||||
|
Tickets: []Ticket{
|
||||||
|
{Repo: "hyperguild", Action: "create", Title: "fine", Body: "b"},
|
||||||
|
{Repo: "trucker-ecommerce", Action: "create", Title: "client work", Body: "b"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, ErrSovereigntyRefused)
|
||||||
|
assert.Contains(t, err.Error(), "trucker-ecommerce",
|
||||||
|
"the refusal must name the target that escalated the classification")
|
||||||
|
assert.NotContains(t, err.Error(), "hyperguild",
|
||||||
|
"only the offending target, or the message is noise at 20 tickets")
|
||||||
|
assert.Empty(t, tr.created, "refused before any write")
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ type request struct {
|
|||||||
Context contextBody `json:"context"`
|
Context contextBody `json:"context"`
|
||||||
Insights []insightBody `json:"insights"`
|
Insights []insightBody `json:"insights"`
|
||||||
Tickets []ticketBody `json:"tickets"`
|
Tickets []ticketBody `json:"tickets"`
|
||||||
Summary *summaryBody `json:"summary,omitempty"`
|
|
||||||
DryRun bool `json:"dry_run"`
|
DryRun bool `json:"dry_run"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,12 +81,6 @@ type ticketBody struct {
|
|||||||
Body string `json:"body,omitempty"`
|
Body string `json:"body,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type summaryBody struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
ReposTouched []string `json:"repos_touched,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP authenticates, derives origin, runs the use-case, and maps the
|
// ServeHTTP authenticates, derives origin, runs the use-case, and maps the
|
||||||
// result to an HTTP status.
|
// result to an HTTP status.
|
||||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -183,11 +176,6 @@ func (b request) toInput() capture.CaptureInput {
|
|||||||
Repo: t.Repo, Action: t.Action, Number: t.Number, Title: t.Title, Body: t.Body,
|
Repo: t.Repo, Action: t.Action, Number: t.Number, Title: t.Title, Body: t.Body,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if b.Summary != nil {
|
|
||||||
in.Summary = &capture.Summary{
|
|
||||||
Title: b.Summary.Title, Body: b.Summary.Body, ReposTouched: b.Summary.ReposTouched,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return in
|
return in
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,9 +192,6 @@ func statusFor(rec capture.CaptureReceipt) int {
|
|||||||
for _, t := range rec.Tickets {
|
for _, t := range rec.Tickets {
|
||||||
count(&ok, &fail, t.OK)
|
count(&ok, &fail, t.OK)
|
||||||
}
|
}
|
||||||
if rec.Summary != nil {
|
|
||||||
count(&ok, &fail, rec.Summary.OK)
|
|
||||||
}
|
|
||||||
switch {
|
switch {
|
||||||
case fail == 0:
|
case fail == 0:
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func newHandler(t *testing.T, v capturehttp.Validator, tr capture.IssueTracker,
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
cfg, err := classification.Load(t.TempDir())
|
cfg, err := classification.Load(t.TempDir())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
svc := capture.NewService(brainstore.New(t.TempDir()), tr, nil, cfg, audit.NewSlogSink(nil))
|
svc := capture.NewService(brainstore.New(t.TempDir()), tr, cfg, audit.NewSlogSink(nil))
|
||||||
return capturehttp.New(svc, v, staticTok, "local-cli", capturehttp.NewOriginResolver(sovereign))
|
return capturehttp.New(svc, v, staticTok, "local-cli", capturehttp.NewOriginResolver(sovereign))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ func (refusingAudit) Record(context.Context, capture.AuditEntry, capture.AuditOu
|
|||||||
func TestAuditUnavailableIs503(t *testing.T) {
|
func TestAuditUnavailableIs503(t *testing.T) {
|
||||||
cfg, err := classification.Load(t.TempDir())
|
cfg, err := classification.Load(t.TempDir())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
svc := capture.NewService(brainstore.New(t.TempDir()), fakeTracker{}, nil, cfg, refusingAudit{})
|
svc := capture.NewService(brainstore.New(t.TempDir()), fakeTracker{}, cfg, refusingAudit{})
|
||||||
h := capturehttp.New(svc, nil, staticTok, "local-cli", capturehttp.NewOriginResolver(nil))
|
h := capturehttp.New(svc, nil, staticTok, "local-cli", capturehttp.NewOriginResolver(nil))
|
||||||
|
|
||||||
rr := do(t, h, "Bearer "+staticTok, internalReq())
|
rr := do(t, h, "Bearer "+staticTok, internalReq())
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
// Package frontmatter lints and repairs the YAML frontmatter block of a
|
||||||
|
// markdown note.
|
||||||
|
//
|
||||||
|
// Notes written directly — hand-authored, or promoted from brain/raw/ — do not
|
||||||
|
// pass through pipeline.BuildPages, so nothing quotes their scalars. A title
|
||||||
|
// containing an unescaped ": " then produces a block that yaml.Unmarshal
|
||||||
|
// rejects with "mapping values are not allowed here", and every consumer that
|
||||||
|
// reads frontmatter silently skips the note.
|
||||||
|
//
|
||||||
|
// Fix is deliberately conservative: it only adds quoting, never reorders or
|
||||||
|
// rewrites, and it refuses to return a document that still does not parse.
|
||||||
|
package frontmatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const fence = "---"
|
||||||
|
|
||||||
|
// Lint reports whether doc's frontmatter block parses as YAML.
|
||||||
|
//
|
||||||
|
// A document with no frontmatter block, or with an unterminated one, is not an
|
||||||
|
// error: there is nothing claiming to be YAML.
|
||||||
|
func Lint(doc []byte) error {
|
||||||
|
b, ok := block(doc)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var v any
|
||||||
|
if err := yaml.Unmarshal(b, &v); err != nil {
|
||||||
|
return fmt.Errorf("frontmatter: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix quotes unquoted scalars whose value breaks YAML parsing and returns the
|
||||||
|
// repaired document.
|
||||||
|
//
|
||||||
|
// It returns (doc, false, nil) when the document already parses, and
|
||||||
|
// (doc, false, err) when quoting cannot rescue it — an unfixable document is
|
||||||
|
// always returned byte-identical, never half-repaired.
|
||||||
|
func Fix(doc []byte) ([]byte, bool, error) {
|
||||||
|
if err := Lint(doc); err == nil {
|
||||||
|
return doc, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b, ok := block(doc)
|
||||||
|
if !ok {
|
||||||
|
// Lint only fails when there is a block, so this is unreachable in
|
||||||
|
// practice; return the original rather than guess.
|
||||||
|
return doc, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fixed := quoteScalars(b)
|
||||||
|
if bytes.Equal(fixed, b) {
|
||||||
|
return doc, false, fmt.Errorf("frontmatter: not repairable by quoting: %w", Lint(doc))
|
||||||
|
}
|
||||||
|
|
||||||
|
var v any
|
||||||
|
if err := yaml.Unmarshal(fixed, &v); err != nil {
|
||||||
|
return doc, false, fmt.Errorf("frontmatter: quoting did not repair the block: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, rest, _ := split(doc)
|
||||||
|
out := []byte(fence + "\n")
|
||||||
|
out = append(out, fixed...)
|
||||||
|
out = append(out, []byte(fence)...)
|
||||||
|
out = append(out, rest...)
|
||||||
|
return out, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mappingLine matches "key: value" at any indentation, capturing the prefix so
|
||||||
|
// it can be re-emitted untouched.
|
||||||
|
//
|
||||||
|
// Sequence items ("- value") are deliberately not handled: "- foo: bar" is
|
||||||
|
// valid YAML, so a document containing one does not reach Fix in the first
|
||||||
|
// place. See the Lint table for why that gap is left open.
|
||||||
|
var mappingLine = regexp.MustCompile(`^(\s*[A-Za-z_][\w.-]*:[ \t]+)(\S.*)$`)
|
||||||
|
|
||||||
|
// quoteScalars single-quotes every unquoted scalar value in the block that
|
||||||
|
// contains a colon. Values that are already quoted, or that carry YAML syntax
|
||||||
|
// this package has no business rewriting, are left alone.
|
||||||
|
func quoteScalars(block []byte) []byte {
|
||||||
|
lines := strings.Split(string(block), "\n")
|
||||||
|
for i, line := range lines {
|
||||||
|
m := mappingLine.FindStringSubmatch(line)
|
||||||
|
if m == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
prefix, value := m[1], m[2]
|
||||||
|
if !needsQuoting(value) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lines[i] = prefix + "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
||||||
|
}
|
||||||
|
return []byte(strings.Join(lines, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// needsQuoting reports whether a scalar value has to be quoted to load as the
|
||||||
|
// string it plainly is.
|
||||||
|
//
|
||||||
|
// The test is YAML's own parser rather than a pattern: ask whether the value
|
||||||
|
// stands alone as the right-hand side of a mapping. Heuristics get this wrong
|
||||||
|
// in both directions — `title: "Foo" — bar` opens with a quote but is not
|
||||||
|
// quoted, and a value may contain a colon inside quotes and be perfectly fine.
|
||||||
|
//
|
||||||
|
// Anything that already parses is left alone, whatever it parses into — a flow
|
||||||
|
// collection, a number, a bool. It is not broken, and quoting would change its
|
||||||
|
// type.
|
||||||
|
func needsQuoting(value string) bool {
|
||||||
|
if strings.HasPrefix(value, "|") || strings.HasPrefix(value, ">") {
|
||||||
|
return false // block scalar; its content is not parsed as YAML
|
||||||
|
}
|
||||||
|
var probe map[string]any
|
||||||
|
return yaml.Unmarshal([]byte("v: "+value+"\n"), &probe) != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// block returns the raw YAML between the opening and closing fences.
|
||||||
|
func block(doc []byte) ([]byte, bool) {
|
||||||
|
b, _, ok := split(doc)
|
||||||
|
return b, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// split returns the frontmatter block and everything from the closing fence
|
||||||
|
// onward (the closing fence itself excluded, its trailing newline included).
|
||||||
|
func split(doc []byte) (block, rest []byte, ok bool) {
|
||||||
|
s := string(doc)
|
||||||
|
if !strings.HasPrefix(s, fence+"\n") {
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
body := s[len(fence)+1:]
|
||||||
|
|
||||||
|
idx := strings.Index(body, "\n"+fence)
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
return []byte(body[:idx+1]), []byte(body[idx+1+len(fence):]), true
|
||||||
|
}
|
||||||
@@ -0,0 +1,188 @@
|
|||||||
|
// ingestion/internal/frontmatter/frontmatter_test.go
|
||||||
|
package frontmatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLint(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
doc string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no frontmatter block is not an error",
|
||||||
|
doc: "# Just a heading\n\nSome body text.\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "quoted title parses",
|
||||||
|
doc: "---\ntitle: 'Headless dispatch runs: Bash is denied'\n---\n\nbody\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "plain title parses",
|
||||||
|
doc: "---\ntitle: A simple title\nwing: homelab\n---\n\nbody\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unquoted colon in title does not parse",
|
||||||
|
doc: "---\ntitle: Headless dispatch runs: Bash is denied — use gitea MCP\nwing: homelab\n---\n\nbody\n",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Known gap, deliberately not fixed here: this parses, but the
|
||||||
|
// item becomes map[string]any{"Headless dispatch": "denied"}
|
||||||
|
// rather than a string. It is a silent type corruption, not a
|
||||||
|
// parse failure, so repairing it would rewrite documents that
|
||||||
|
// currently load — out of scope for a lint about broken YAML.
|
||||||
|
name: "unquoted colon in a list item parses as a mapping, not an error",
|
||||||
|
doc: "---\ntitle: Fine\naliases:\n - Headless dispatch: denied\n---\n\nbody\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unterminated frontmatter is not treated as frontmatter",
|
||||||
|
doc: "---\ntitle: no closing fence\n\nbody\n",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := Lint([]byte(tt.doc))
|
||||||
|
if tt.wantErr {
|
||||||
|
require.Error(t, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFix(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
doc string
|
||||||
|
wantChanged bool
|
||||||
|
wantKey string
|
||||||
|
wantValue string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "quotes an unquoted title containing a colon",
|
||||||
|
doc: "---\ntitle: Headless dispatch runs: Bash is denied — use gitea MCP\nwing: homelab\n---\n\nbody\n",
|
||||||
|
wantChanged: true,
|
||||||
|
wantKey: "title",
|
||||||
|
wantValue: "Headless dispatch runs: Bash is denied — use gitea MCP",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Parses already (see the Lint table), so Fix must leave it alone
|
||||||
|
// rather than rewrite a document that loads.
|
||||||
|
name: "leaves a colon-bearing list item alone because it already parses",
|
||||||
|
doc: "---\ntitle: Fine\naliases:\n - Headless dispatch: denied\n---\n\nbody\n",
|
||||||
|
wantChanged: false,
|
||||||
|
wantKey: "title",
|
||||||
|
wantValue: "Fine",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "escapes an embedded single quote when quoting",
|
||||||
|
doc: "---\ntitle: Don't do this: it breaks\n---\n\nbody\n",
|
||||||
|
wantChanged: true,
|
||||||
|
wantKey: "title",
|
||||||
|
wantValue: "Don't do this: it breaks",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// The value opens with a double quote and then keeps going after
|
||||||
|
// the closing one, so YAML reads it as a quoted scalar followed by
|
||||||
|
// junk. Treating "starts with a quote" as "already quoted" leaves
|
||||||
|
// this broken forever.
|
||||||
|
name: "quotes a value that only looks quoted",
|
||||||
|
doc: "---\ntitle: \"Evaluation Driven Generation\" — a TDD-style scaffold\ntags: [a, b]\n---\n\nbody\n",
|
||||||
|
wantChanged: true,
|
||||||
|
wantKey: "title",
|
||||||
|
wantValue: "\"Evaluation Driven Generation\" — a TDD-style scaffold",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "leaves an already-valid document untouched",
|
||||||
|
doc: "---\ntitle: 'Already quoted: fine'\nwing: homelab\n---\n\nbody\n",
|
||||||
|
wantChanged: false,
|
||||||
|
wantKey: "title",
|
||||||
|
wantValue: "Already quoted: fine",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "leaves a document with no frontmatter untouched",
|
||||||
|
doc: "# Heading\n\nbody: with a colon\n",
|
||||||
|
wantChanged: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, changed, err := Fix([]byte(tt.doc))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.wantChanged, changed)
|
||||||
|
|
||||||
|
if !tt.wantChanged {
|
||||||
|
assert.Equal(t, tt.doc, string(got), "unchanged docs must be returned byte-identical")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Whatever came back must parse — that is the whole point.
|
||||||
|
require.NoError(t, Lint(got), "Fix must not return a document that still fails to parse")
|
||||||
|
|
||||||
|
if tt.wantKey == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
block, ok := block(got)
|
||||||
|
require.True(t, ok)
|
||||||
|
var m map[string]any
|
||||||
|
require.NoError(t, yaml.Unmarshal(block, &m))
|
||||||
|
assert.Equal(t, tt.wantValue, m[tt.wantKey], "the value must survive quoting unchanged")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFix_LeavesStructuredValuesStructured(t *testing.T) {
|
||||||
|
// Only the broken scalar may be touched. A flow sequence on a neighbouring
|
||||||
|
// line still has to load as a sequence afterwards, not as a string.
|
||||||
|
doc := "---\ntitle: Broken: here\ntags: [prompting, llm]\ncount: 3\n---\n\nbody\n"
|
||||||
|
|
||||||
|
got, changed, err := Fix([]byte(doc))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, changed)
|
||||||
|
|
||||||
|
b, ok := block(got)
|
||||||
|
require.True(t, ok)
|
||||||
|
var m map[string]any
|
||||||
|
require.NoError(t, yaml.Unmarshal(b, &m))
|
||||||
|
|
||||||
|
assert.Equal(t, "Broken: here", m["title"])
|
||||||
|
assert.Equal(t, []any{"prompting", "llm"}, m["tags"])
|
||||||
|
assert.Equal(t, 3, m["count"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFix_PreservesBodyExactly(t *testing.T) {
|
||||||
|
body := "\nThe body has: colons, 'quotes', and\n---\na horizontal rule.\n"
|
||||||
|
doc := "---\ntitle: Broken: title\n---" + body
|
||||||
|
|
||||||
|
got, changed, err := Fix([]byte(doc))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, changed)
|
||||||
|
assert.True(t, len(got) > 0)
|
||||||
|
|
||||||
|
_, rest, ok := split([]byte(doc))
|
||||||
|
require.True(t, ok)
|
||||||
|
_, gotRest, ok := split(got)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, string(rest), string(gotRest), "the body must not be touched")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFix_RefusesToReturnAnUnparseableResult(t *testing.T) {
|
||||||
|
// A frontmatter block that quoting cannot rescue: the structure itself is
|
||||||
|
// broken, not a scalar. Fix must return the original and an error rather
|
||||||
|
// than a half-repaired document.
|
||||||
|
doc := "---\ntitle: fine\n\tbad: tab-indented mapping\n - stray list item\n---\n\nbody\n"
|
||||||
|
|
||||||
|
got, changed, err := Fix([]byte(doc))
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.False(t, changed)
|
||||||
|
assert.Equal(t, doc, string(got), "an unfixable document must come back byte-identical")
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
// ingestion/internal/frontmatter/scan.go
|
||||||
|
package frontmatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Finding is one markdown file whose frontmatter does not parse.
|
||||||
|
type Finding struct {
|
||||||
|
Path string // relative to the scanned root
|
||||||
|
Err error // why it does not parse, or why the repair was refused
|
||||||
|
Repaired bool // true only when fix was requested and the file was rewritten
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan walks root for *.md files whose frontmatter fails to parse.
|
||||||
|
//
|
||||||
|
// With fix set, each finding that quoting can rescue is rewritten in place;
|
||||||
|
// anything quoting cannot rescue is reported and left byte-identical. A file
|
||||||
|
// that cannot be read or decoded is reported as a finding rather than aborting
|
||||||
|
// the walk — one poisoned note must not hide the other findings.
|
||||||
|
func Scan(root string, fix bool) ([]Finding, error) {
|
||||||
|
var found []Finding
|
||||||
|
|
||||||
|
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".md") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, relErr := filepath.Rel(root, path)
|
||||||
|
if relErr != nil {
|
||||||
|
rel = path
|
||||||
|
}
|
||||||
|
|
||||||
|
doc, readErr := os.ReadFile(path)
|
||||||
|
if readErr != nil {
|
||||||
|
found = append(found, Finding{Path: rel, Err: readErr})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !utf8.Valid(doc) {
|
||||||
|
found = append(found, Finding{Path: rel, Err: fmt.Errorf("not valid UTF-8")})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if lintErr := Lint(doc); lintErr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fix {
|
||||||
|
found = append(found, Finding{Path: rel, Err: Lint(doc)})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
repaired, changed, fixErr := Fix(doc)
|
||||||
|
if fixErr != nil || !changed {
|
||||||
|
found = append(found, Finding{Path: rel, Err: fixErr})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if writeErr := writeFilePreservingMode(path, repaired); writeErr != nil {
|
||||||
|
found = append(found, Finding{Path: rel, Err: writeErr})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
found = append(found, Finding{Path: rel, Repaired: true})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("scan %s: %w", root, err)
|
||||||
|
}
|
||||||
|
return found, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeFilePreservingMode(path string, content []byte) error {
|
||||||
|
mode := fs.FileMode(0o644)
|
||||||
|
if info, err := os.Stat(path); err == nil {
|
||||||
|
mode = info.Mode().Perm()
|
||||||
|
}
|
||||||
|
return os.WriteFile(path, content, mode)
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
// ingestion/internal/frontmatter/scan_test.go
|
||||||
|
package frontmatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeTree(t *testing.T, files map[string]string) string {
|
||||||
|
t.Helper()
|
||||||
|
root := t.TempDir()
|
||||||
|
for name, content := range files {
|
||||||
|
path := filepath.Join(root, name)
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
|
||||||
|
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
|
||||||
|
}
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
brokenDoc = "---\ntitle: Broken: title here\nwing: homelab\n---\n\nbody\n"
|
||||||
|
validDoc = "---\ntitle: 'Fine: quoted'\n---\n\nbody\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScan_ReportsOnlyBrokenFiles(t *testing.T) {
|
||||||
|
root := writeTree(t, map[string]string{
|
||||||
|
"wiki/a/broken.md": brokenDoc,
|
||||||
|
"wiki/a/valid.md": validDoc,
|
||||||
|
"wiki/b/plain.md": "# no frontmatter\n\nbody: with colon\n",
|
||||||
|
"wiki/a/notes.txt": brokenDoc, // not markdown — must be ignored
|
||||||
|
})
|
||||||
|
|
||||||
|
found, err := Scan(root, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Len(t, found, 1)
|
||||||
|
assert.Equal(t, "wiki/a/broken.md", found[0].Path)
|
||||||
|
assert.False(t, found[0].Repaired)
|
||||||
|
assert.Error(t, found[0].Err)
|
||||||
|
|
||||||
|
// Dry run must not touch the file.
|
||||||
|
got, err := os.ReadFile(filepath.Join(root, "wiki/a/broken.md"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, brokenDoc, string(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScan_FixWritesRepairedFilesAndLeavesTheRestAlone(t *testing.T) {
|
||||||
|
root := writeTree(t, map[string]string{
|
||||||
|
"wiki/a/broken.md": brokenDoc,
|
||||||
|
"wiki/a/valid.md": validDoc,
|
||||||
|
})
|
||||||
|
|
||||||
|
found, err := Scan(root, true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, found, 1)
|
||||||
|
assert.True(t, found[0].Repaired)
|
||||||
|
assert.NoError(t, found[0].Err)
|
||||||
|
|
||||||
|
repaired, err := os.ReadFile(filepath.Join(root, "wiki/a/broken.md"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, Lint(repaired), "the repaired file must parse")
|
||||||
|
assert.Contains(t, string(repaired), "title: 'Broken: title here'")
|
||||||
|
assert.Contains(t, string(repaired), "wing: homelab", "untouched keys must survive")
|
||||||
|
|
||||||
|
untouched, err := os.ReadFile(filepath.Join(root, "wiki/a/valid.md"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, validDoc, string(untouched))
|
||||||
|
|
||||||
|
// Second pass finds nothing — the repair is idempotent and complete.
|
||||||
|
again, err := Scan(root, true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, again)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScan_ReportsUnfixableWithoutWriting(t *testing.T) {
|
||||||
|
unfixable := "---\ntitle: fine\n\tbad: tab-indented\n - stray\n---\n\nbody\n"
|
||||||
|
root := writeTree(t, map[string]string{"wiki/x.md": unfixable})
|
||||||
|
|
||||||
|
found, err := Scan(root, true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, found, 1)
|
||||||
|
assert.False(t, found[0].Repaired)
|
||||||
|
assert.Error(t, found[0].Err)
|
||||||
|
|
||||||
|
got, err := os.ReadFile(filepath.Join(root, "wiki/x.md"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, unfixable, string(got), "an unfixable file must be left exactly as it was")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScan_ReportsUndecodableFilesInsteadOfFailingTheRun(t *testing.T) {
|
||||||
|
root := writeTree(t, map[string]string{"wiki/ok.md": validDoc})
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(root, "wiki/bad-bytes.md"),
|
||||||
|
[]byte{'-', '-', '-', '\n', 0xff, 0xfe, '\n', '-', '-', '-', '\n'}, 0o644))
|
||||||
|
|
||||||
|
found, err := Scan(root, false)
|
||||||
|
require.NoError(t, err, "one unreadable file must not abort the whole scan")
|
||||||
|
require.Len(t, found, 1)
|
||||||
|
assert.Equal(t, "wiki/bad-bytes.md", found[0].Path)
|
||||||
|
}
|
||||||
@@ -105,13 +105,6 @@ func (s *Server) tools() []map[string]any {
|
|||||||
"target": str("path of target note (must be in a different wing)"),
|
"target": str("path of target note (must be in a different wing)"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "brain_index",
|
|
||||||
"description": "Regenerate _index.md (Map of Content) for one or all wings under brain/wiki/. Auto-called after brain_write with wing+hall.",
|
|
||||||
"inputSchema": schema([]string{}, map[string]any{
|
|
||||||
"wing": str("optional wing to index; if absent, rebuilds every wing"),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "brain_ingest_raw",
|
"name": "brain_ingest_raw",
|
||||||
"description": "Ingest pre-structured pages into the brain wiki, bypassing the LLM extraction step.",
|
"description": "Ingest pre-structured pages into the brain wiki, bypassing the LLM extraction step.",
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package mcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
// pendingNudgeThreshold is how old a raw/ note has to be before brain_context
|
||||||
|
// mentions the queue. Two weeks: long enough that a note written this sprint
|
||||||
|
// does not nag, short enough that four items cannot sit unnoticed for three
|
||||||
|
// months again (#82).
|
||||||
|
const pendingNudgeThreshold = 14 * 24 * time.Hour
|
||||||
|
|
||||||
|
// PendingNudge is the in-session reminder that the promote/reject queue has
|
||||||
|
// items waiting. It is advisory: a count, an age, and how to look.
|
||||||
|
type PendingNudge struct {
|
||||||
|
Pending int `json:"pending"` // everything in the queue
|
||||||
|
Aging int `json:"aging"` // those past the threshold
|
||||||
|
Oldest string `json:"oldest"` // filename of the oldest pending note
|
||||||
|
OldestDays int `json:"oldest_days"` // its age, in whole days
|
||||||
|
Hint string `json:"hint"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// pendingNudge summarises the raw/ review queue, or returns nil when there is
|
||||||
|
// nothing worth saying.
|
||||||
|
//
|
||||||
|
// The trigger is the missing half of the queue (#82): brain_pending has always
|
||||||
|
// existed, and four real items still sat unreviewed from 2026-04-23 to
|
||||||
|
// 2026-07-15 because nothing ever prompted anyone to call it. The system
|
||||||
|
// should be what remembers the queue exists.
|
||||||
|
//
|
||||||
|
// Threshold-gated on purpose. A notice on every context call is one people
|
||||||
|
// learn to skip, which reproduces the original failure with extra steps.
|
||||||
|
//
|
||||||
|
// It never promotes, rejects, or reorders anything. Age is a reason to ask a
|
||||||
|
// human, not a verdict — an old note may be exactly right to promote once
|
||||||
|
// someone finally looks at it.
|
||||||
|
func pendingNudge(brainDir string, now time.Time, threshold time.Duration) *PendingNudge {
|
||||||
|
pending, err := api.ListPending(brainDir)
|
||||||
|
if err != nil || len(pending) == 0 {
|
||||||
|
// brain_context is a hot path; a broken queue degrades to silence
|
||||||
|
// rather than failing the call that asked for project context.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var aging int
|
||||||
|
var oldestName string
|
||||||
|
var oldestAge time.Duration
|
||||||
|
|
||||||
|
for _, p := range pending {
|
||||||
|
created, perr := time.Parse(time.RFC3339, p.CreatedAt)
|
||||||
|
if perr != nil {
|
||||||
|
continue // an unparseable timestamp is not evidence of age
|
||||||
|
}
|
||||||
|
age := now.Sub(created)
|
||||||
|
if age >= threshold {
|
||||||
|
aging++
|
||||||
|
}
|
||||||
|
if age > oldestAge {
|
||||||
|
oldestAge, oldestName = age, p.Filename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if aging == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &PendingNudge{
|
||||||
|
Pending: len(pending),
|
||||||
|
Aging: aging,
|
||||||
|
Oldest: oldestName,
|
||||||
|
OldestDays: int(oldestAge.Hours() / 24),
|
||||||
|
Hint: "call brain_pending to review; nothing is promoted or rejected automatically",
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package mcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// writePending drops a raw/ note whose created_at is age old.
|
||||||
|
func writePending(t *testing.T, brainDir, name string, age time.Duration, now time.Time) {
|
||||||
|
t.Helper()
|
||||||
|
raw := filepath.Join(brainDir, "raw")
|
||||||
|
require.NoError(t, os.MkdirAll(raw, 0o755))
|
||||||
|
body := "---\ncreated_at: " + now.Add(-age).UTC().Format(time.RFC3339) + "\n---\n\nnote body\n"
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(raw, name), []byte(body), 0o644))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingNudge_SilentWhenNothingIsPending(t *testing.T) {
|
||||||
|
// No queue, no nudge. A notice that fires every session is one people
|
||||||
|
// learn to skip, which is how the queue got ignored for three months in
|
||||||
|
// the first place (#82).
|
||||||
|
got := pendingNudge(t.TempDir(), time.Now(), 14*24*time.Hour)
|
||||||
|
assert.Nil(t, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingNudge_SilentWhileEverythingIsBelowTheThreshold(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
dir := t.TempDir()
|
||||||
|
writePending(t, dir, "fresh.md", 2*24*time.Hour, now)
|
||||||
|
|
||||||
|
got := pendingNudge(dir, now, 14*24*time.Hour)
|
||||||
|
assert.Nil(t, got, "a two-day-old note is not aging")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingNudge_ReportsCountAndOldestAgeOnceSomethingAges(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
dir := t.TempDir()
|
||||||
|
writePending(t, dir, "old.md", 100*24*time.Hour, now)
|
||||||
|
writePending(t, dir, "older.md", 120*24*time.Hour, now)
|
||||||
|
writePending(t, dir, "fresh.md", 1*24*time.Hour, now)
|
||||||
|
|
||||||
|
got := pendingNudge(dir, now, 14*24*time.Hour)
|
||||||
|
require.NotNil(t, got)
|
||||||
|
|
||||||
|
// All three are pending; the threshold decides whether to speak, not
|
||||||
|
// what to count. Someone opening the queue sees the whole queue.
|
||||||
|
assert.Equal(t, 3, got.Pending)
|
||||||
|
assert.Equal(t, 2, got.Aging)
|
||||||
|
assert.Equal(t, 120, got.OldestDays)
|
||||||
|
assert.Equal(t, "older.md", got.Oldest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingNudge_NeverDecidesAnything(t *testing.T) {
|
||||||
|
// Aging is a trigger to ask a human, never a verdict. The nudge must
|
||||||
|
// carry no action and must leave the queue exactly as it found it.
|
||||||
|
now := time.Now()
|
||||||
|
dir := t.TempDir()
|
||||||
|
writePending(t, dir, "ancient.md", 365*24*time.Hour, now)
|
||||||
|
|
||||||
|
before, err := os.ReadDir(filepath.Join(dir, "raw"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
got := pendingNudge(dir, now, 14*24*time.Hour)
|
||||||
|
require.NotNil(t, got)
|
||||||
|
|
||||||
|
after, err := os.ReadDir(filepath.Join(dir, "raw"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Len(t, after, len(before), "the nudge must not promote or reject anything")
|
||||||
|
assert.Contains(t, got.Hint, "brain_pending", "the nudge should say how to look, not act")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingNudge_SurvivesAnUnreadableQueue(t *testing.T) {
|
||||||
|
// brain_context is a hot path; a broken raw/ dir must degrade to silence
|
||||||
|
// rather than failing the whole context call.
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "raw"), []byte("not a directory"), 0o644))
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
assert.Nil(t, pendingNudge(dir, time.Now(), 14*24*time.Hour))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBrainContext_CarriesTheNudgeWithoutFailingWhenTheQueueIsEmpty(t *testing.T) {
|
||||||
|
// The wiring, not the summary: a nudge that never reaches a caller is the
|
||||||
|
// same as no nudge, and brain_context must still work with no queue.
|
||||||
|
dir := t.TempDir()
|
||||||
|
srv := NewServer(dir, nil, nil, nil)
|
||||||
|
|
||||||
|
raw, err := srv.brainContext(context.Background(),
|
||||||
|
json.RawMessage(`{"project_root":"/home/mathias/dev/AGENTS/hyperguild"}`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var empty map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(raw, &empty))
|
||||||
|
assert.NotContains(t, empty, "pending_review", "silence when nothing is aging")
|
||||||
|
|
||||||
|
writePending(t, dir, "ancient.md", 200*24*time.Hour, time.Now())
|
||||||
|
|
||||||
|
raw, err = srv.brainContext(context.Background(),
|
||||||
|
json.RawMessage(`{"project_root":"/home/mathias/dev/AGENTS/hyperguild"}`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var withQueue map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(raw, &withQueue))
|
||||||
|
nudge, ok := withQueue["pending_review"].(map[string]any)
|
||||||
|
require.True(t, ok, "the nudge must reach the caller: %v", withQueue)
|
||||||
|
assert.Equal(t, float64(1), nudge["aging"])
|
||||||
|
assert.Equal(t, "ancient.md", nudge["oldest"])
|
||||||
|
}
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
// Package mcp implements an MCP HTTP handler for the ingestion service.
|
// Package mcp implements an MCP HTTP handler for the ingestion service.
|
||||||
// Exposed tools: brain_query, brain_write, brain_update, brain_get,
|
// Exposed tools: brain_query, brain_write, brain_update, brain_get,
|
||||||
// brain_pending, brain_promote, brain_index, brain_tunnel, brain_ingest,
|
// brain_pending, brain_promote, brain_tunnel, brain_ingest,
|
||||||
// brain_ingest_raw, brain_answer, brain_classify, brain_graph,
|
// brain_ingest_raw, brain_answer, brain_classify, brain_graph,
|
||||||
// brain_context, session_log, and capture (the #55 relay tool, registered
|
// brain_context, session_log, and capture (the #55 relay tool, registered
|
||||||
// only when WithCapture is set).
|
// only when WithCapture is set).
|
||||||
|
//
|
||||||
|
// brain_index dispatches but is deliberately absent from tools/list: it is
|
||||||
|
// auto-called after every write, so surfacing it as a caller verb is
|
||||||
|
// cognitive load with no decision behind it (ADR-0012 rule 4, #78). It stays
|
||||||
|
// callable for maintenance — de-surfacing hides the verb, not the operation.
|
||||||
package mcp
|
package mcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -59,11 +59,35 @@ func TestServerToolsList(t *testing.T) {
|
|||||||
assert.ElementsMatch(t, []string{
|
assert.ElementsMatch(t, []string{
|
||||||
"brain_query", "brain_write", "brain_update", "brain_get",
|
"brain_query", "brain_write", "brain_update", "brain_get",
|
||||||
"brain_pending", "brain_promote",
|
"brain_pending", "brain_promote",
|
||||||
"brain_index", "brain_tunnel",
|
"brain_tunnel",
|
||||||
"brain_ingest_raw", "brain_ingest",
|
"brain_ingest_raw", "brain_ingest",
|
||||||
"brain_answer", "brain_classify", "brain_graph", "brain_context",
|
"brain_answer", "brain_classify", "brain_graph", "brain_context",
|
||||||
"session_log",
|
"session_log",
|
||||||
}, names)
|
}, names)
|
||||||
|
|
||||||
|
// brain_index is auto-called after every brain_write, so surfacing it as
|
||||||
|
// a caller verb is cognitive load with no decision behind it (ADR-0012
|
||||||
|
// rule 4, #78). The capability stays reachable — see the admin test below.
|
||||||
|
assert.NotContains(t, names, "brain_index")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBrainIndexStaysCallableForAdminUseAfterDeSurfacing(t *testing.T) {
|
||||||
|
// De-surfacing hides the verb from the model-facing list; it must not
|
||||||
|
// remove the operation. A maintenance caller still needs to rebuild a
|
||||||
|
// Map of Content by hand.
|
||||||
|
srv := mcp.NewServer(t.TempDir(), nil, nil, nil)
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/mcp", body(t, map[string]any{
|
||||||
|
"jsonrpc": "2.0", "id": 3, "method": "tools/call",
|
||||||
|
"params": map[string]any{"name": "brain_index", "arguments": map[string]any{}},
|
||||||
|
}))
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
srv.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusOK, rr.Code)
|
||||||
|
var resp map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp))
|
||||||
|
assert.NotContains(t, resp, "error", "brain_index must still dispatch: %v", resp["error"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServerNotificationGetsNoBody(t *testing.T) {
|
func TestServerNotificationGetsNoBody(t *testing.T) {
|
||||||
|
|||||||
@@ -58,17 +58,13 @@ func captureToolDescriptor() map[string]any {
|
|||||||
},
|
},
|
||||||
"insights": map[string]any{"type": "array", "items": insightItem},
|
"insights": map[string]any{"type": "array", "items": insightItem},
|
||||||
"tickets": map[string]any{"type": "array", "items": ticketItem},
|
"tickets": map[string]any{"type": "array", "items": ticketItem},
|
||||||
"summary": map[string]any{"type": "object", "properties": map[string]any{
|
|
||||||
"title": str("summary title"), "body": str("summary body"),
|
|
||||||
"repos_touched": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
|
|
||||||
}},
|
|
||||||
"dry_run": map[string]any{"type": "boolean", "description": "validate + return the would-be receipt, write nothing"},
|
"dry_run": map[string]any{"type": "boolean", "description": "validate + return the would-be receipt, write nothing"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(schema)
|
b, _ := json.Marshal(schema)
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"name": "capture",
|
"name": "capture",
|
||||||
"description": "Persist a session's value uniformly: insights → brain (write or supersede), action items → Gitea tickets, optional summary → ai-sessions. The relay door for MCP-native harnesses. Origin is server-derived from your authenticated identity; confidential captures through a us-nexus surface are refused (I1). Returns a partial-aware receipt.",
|
"description": "Persist a session's value uniformly: insights → brain (write or supersede), action items → Gitea tickets. The relay door for MCP-native harnesses. Origin is server-derived from your authenticated identity; confidential captures through a us-nexus surface are refused (I1). Returns a partial-aware receipt.",
|
||||||
"inputSchema": json.RawMessage(b),
|
"inputSchema": json.RawMessage(b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func captureServer(t *testing.T, validator capturehttp.Validator, sovereign []st
|
|||||||
brainDir := t.TempDir()
|
brainDir := t.TempDir()
|
||||||
cfg, err := classification.Load(brainDir)
|
cfg, err := classification.Load(brainDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
svc := capture.NewService(brainstore.New(brainDir), capFakeTracker{}, nil, cfg, audit.NewSlogSink(nil))
|
svc := capture.NewService(brainstore.New(brainDir), capFakeTracker{}, cfg, audit.NewSlogSink(nil))
|
||||||
srv := mcp.NewServer(brainDir, nil, nil, nil)
|
srv := mcp.NewServer(brainDir, nil, nil, nil)
|
||||||
srv.WithCapture(svc, validator, capStaticTok, "local-cli", capturehttp.NewOriginResolver(sovereign))
|
srv.WithCapture(svc, validator, capStaticTok, "local-cli", capturehttp.NewOriginResolver(sovereign))
|
||||||
return srv, brainDir
|
return srv, brainDir
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/textutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// brainContextArgs is the input shape of brain_context. project_root is
|
// brainContextArgs is the input shape of brain_context. project_root is
|
||||||
@@ -155,17 +157,21 @@ func (s *Server) brainContext(ctx context.Context, args json.RawMessage) (json.R
|
|||||||
entries = entries[:limit]
|
entries = entries[:limit]
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Marshal(map[string]any{"entries": entries})
|
out := map[string]any{"entries": entries}
|
||||||
|
// The queue nudge rides along with a call the session already makes, so
|
||||||
|
// noticing an aging note costs nobody a decision to go looking (#82).
|
||||||
|
// Advisory only, and absent entirely when there is nothing to say.
|
||||||
|
if n := pendingNudge(s.brainDir, time.Now(), pendingNudgeThreshold); n != nil {
|
||||||
|
out["pending_review"] = n
|
||||||
|
}
|
||||||
|
return json.Marshal(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncateExcerpt clamps an already-stripped excerpt to maxLen characters
|
// truncateExcerpt clamps an already-stripped excerpt to maxLen characters
|
||||||
// without re-running the frontmatter parser. The ellipsis suffix matches
|
// without re-running the frontmatter parser. The ellipsis suffix matches
|
||||||
// the convention used in search.excerpt.
|
// the convention used in search.excerpt.
|
||||||
func truncateExcerpt(s string, maxLen int) string {
|
func truncateExcerpt(s string, maxLen int) string {
|
||||||
if len(s) <= maxLen {
|
return textutil.Truncate(s, maxLen)
|
||||||
return s
|
|
||||||
}
|
|
||||||
return s[:maxLen] + "…"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// readExcerpt loads a doc relative to brainDir, strips its frontmatter,
|
// readExcerpt loads a doc relative to brainDir, strips its frontmatter,
|
||||||
@@ -185,10 +191,7 @@ func readExcerpt(brainDir, relPath string, maxLen int) string {
|
|||||||
if len(parts) == 3 {
|
if len(parts) == 3 {
|
||||||
body = strings.TrimSpace(parts[2])
|
body = strings.TrimSpace(parts[2])
|
||||||
}
|
}
|
||||||
if len(body) > maxLen {
|
return textutil.Truncate(body, maxLen)
|
||||||
return body[:maxLen] + "…"
|
|
||||||
}
|
|
||||||
return body
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// max1 returns the maximum of n and 1, used to guard against divide-by-zero
|
// max1 returns the maximum of n and 1, used to guard against divide-by-zero
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/brain"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/brain"
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/textutil"
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/vectorstore"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/vectorstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -427,8 +428,5 @@ func excerpt(content string, maxLen int) string {
|
|||||||
if len(parts) == 3 {
|
if len(parts) == 3 {
|
||||||
body = strings.TrimSpace(parts[2])
|
body = strings.TrimSpace(parts[2])
|
||||||
}
|
}
|
||||||
if len(body) > maxLen {
|
return textutil.Truncate(body, maxLen)
|
||||||
return body[:maxLen] + "…"
|
|
||||||
}
|
|
||||||
return body
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
// Package startupretry retries a startup dependency that may not be serving
|
||||||
|
// yet, instead of exiting on its first failure.
|
||||||
|
//
|
||||||
|
// It exists for the cold-boot ordering race (#83): on a cluster restart the
|
||||||
|
// ingestion pod can reach OIDC discovery before dex is answering, gets a 502
|
||||||
|
// from ingress, and dies — after having already connected postgres, backfilled
|
||||||
|
// the graph and ingested session batches, all of which the restart redoes.
|
||||||
|
// Nothing is misconfigured in that scenario; the pod simply started first.
|
||||||
|
//
|
||||||
|
// The budget is bounded on purpose. Retrying forever would turn a genuine
|
||||||
|
// misconfiguration (wrong issuer URL, bad audience) into a process that looks
|
||||||
|
// healthy and never serves, which is harder to diagnose than a crash loop.
|
||||||
|
package startupretry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Policy bounds the retry: at most Attempts calls, with the delay doubling
|
||||||
|
// from Base and capped at Max.
|
||||||
|
type Policy struct {
|
||||||
|
Attempts int // total calls, including the first; < 1 means 1
|
||||||
|
Base time.Duration // delay before the second attempt
|
||||||
|
Max time.Duration // ceiling for the doubling delay
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do calls fn until it succeeds, the attempt budget is spent, or ctx ends.
|
||||||
|
//
|
||||||
|
// The returned error wraps the last failure, so callers keep the diagnosis
|
||||||
|
// they would have had without the retry.
|
||||||
|
func Do[T any](ctx context.Context, p Policy, fn func(context.Context) (T, error)) (T, error) {
|
||||||
|
var zero T
|
||||||
|
|
||||||
|
attempts := p.Attempts
|
||||||
|
if attempts < 1 {
|
||||||
|
attempts = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
delay := p.Base
|
||||||
|
var lastErr error
|
||||||
|
|
||||||
|
for i := range attempts {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return zero, fmt.Errorf("startupretry: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := fn(ctx)
|
||||||
|
if err == nil {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
|
||||||
|
if i == attempts-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if delay <= 0 {
|
||||||
|
delay = time.Second
|
||||||
|
}
|
||||||
|
if p.Max > 0 && delay > p.Max {
|
||||||
|
delay = p.Max
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return zero, fmt.Errorf("startupretry: %w", ctx.Err())
|
||||||
|
case <-time.After(delay):
|
||||||
|
}
|
||||||
|
delay *= 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return zero, fmt.Errorf("startupretry: gave up after %d attempts: %w", attempts, lastErr)
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
// ingestion/internal/startupretry/startupretry_test.go
|
||||||
|
package startupretry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fastPolicy(attempts int) Policy {
|
||||||
|
return Policy{Attempts: attempts, Base: time.Millisecond, Max: 2 * time.Millisecond}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo_ReturnsImmediatelyOnFirstSuccess(t *testing.T) {
|
||||||
|
calls := 0
|
||||||
|
got, err := Do(context.Background(), fastPolicy(5), func(context.Context) (string, error) {
|
||||||
|
calls++
|
||||||
|
return "ready", nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Do: %v", err)
|
||||||
|
}
|
||||||
|
if got != "ready" {
|
||||||
|
t.Errorf("got %q, want %q", got, "ready")
|
||||||
|
}
|
||||||
|
if calls != 1 {
|
||||||
|
t.Errorf("calls = %d, want 1", calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo_SurvivesTransientFailuresThenSucceeds(t *testing.T) {
|
||||||
|
// The observed cold-boot race: dex is not serving yet, ingress answers
|
||||||
|
// 502 for the first few attempts, then discovery works.
|
||||||
|
calls := 0
|
||||||
|
got, err := Do(context.Background(), fastPolicy(5), func(context.Context) (string, error) {
|
||||||
|
calls++
|
||||||
|
if calls < 4 {
|
||||||
|
return "", errors.New("oidc discovery: status 502")
|
||||||
|
}
|
||||||
|
return "ready", nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Do returned %v — a transient 502 must not be fatal", err)
|
||||||
|
}
|
||||||
|
if got != "ready" {
|
||||||
|
t.Errorf("got %q, want %q", got, "ready")
|
||||||
|
}
|
||||||
|
if calls != 4 {
|
||||||
|
t.Errorf("calls = %d, want 4", calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo_GivesUpAfterTheAttemptBudgetAndReportsTheLastError(t *testing.T) {
|
||||||
|
// A genuinely broken issuer must still fail — retrying forever would turn
|
||||||
|
// a config error into a silent hang, which is worse than crash-looping.
|
||||||
|
last := errors.New("oidc discovery: status 404")
|
||||||
|
calls := 0
|
||||||
|
|
||||||
|
_, err := Do(context.Background(), fastPolicy(3), func(context.Context) (string, error) {
|
||||||
|
calls++
|
||||||
|
return "", last
|
||||||
|
})
|
||||||
|
|
||||||
|
if !errors.Is(err, last) {
|
||||||
|
t.Fatalf("err = %v, want it to wrap the last failure", err)
|
||||||
|
}
|
||||||
|
if calls != 3 {
|
||||||
|
t.Errorf("calls = %d, want 3 (the attempt budget)", calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo_StopsPromptlyWhenTheContextIsCancelled(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
calls := 0
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
var err error
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
_, err = Do(ctx, Policy{Attempts: 100, Base: 50 * time.Millisecond, Max: time.Second},
|
||||||
|
func(context.Context) (string, error) {
|
||||||
|
calls++
|
||||||
|
if calls == 1 {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
return "", errors.New("still down")
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("Do did not return after context cancellation")
|
||||||
|
}
|
||||||
|
if !errors.Is(err, context.Canceled) {
|
||||||
|
t.Errorf("err = %v, want context.Canceled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo_BacksOffAndIsCappedByMax(t *testing.T) {
|
||||||
|
var delays []time.Duration
|
||||||
|
p := Policy{Attempts: 5, Base: 10 * time.Millisecond, Max: 25 * time.Millisecond}
|
||||||
|
|
||||||
|
last := time.Now()
|
||||||
|
_, _ = Do(context.Background(), p, func(context.Context) (string, error) {
|
||||||
|
now := time.Now()
|
||||||
|
delays = append(delays, now.Sub(last))
|
||||||
|
last = now
|
||||||
|
return "", errors.New("down")
|
||||||
|
})
|
||||||
|
|
||||||
|
// delays[0] is the first call (no wait); the rest are the sleeps.
|
||||||
|
if len(delays) != 5 {
|
||||||
|
t.Fatalf("attempts = %d, want 5", len(delays))
|
||||||
|
}
|
||||||
|
if delays[1] >= delays[2] {
|
||||||
|
t.Errorf("backoff is not growing: %v then %v", delays[1], delays[2])
|
||||||
|
}
|
||||||
|
for i, d := range delays[1:] {
|
||||||
|
if d > 2*p.Max {
|
||||||
|
t.Errorf("delay %d = %v, exceeds the Max cap %v with slack", i+1, d, p.Max)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPolicy_ZeroValueStillMakesOneAttempt(t *testing.T) {
|
||||||
|
// A caller that forgets to configure the policy must not silently get a
|
||||||
|
// no-op that returns a nil validator and a nil error.
|
||||||
|
calls := 0
|
||||||
|
_, err := Do(context.Background(), Policy{}, func(context.Context) (string, error) {
|
||||||
|
calls++
|
||||||
|
return "", errors.New("down")
|
||||||
|
})
|
||||||
|
|
||||||
|
if calls != 1 {
|
||||||
|
t.Errorf("calls = %d, want 1", calls)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Error("err = nil, want the failure surfaced")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
// Package textutil holds small text helpers shared across the ingestion
|
||||||
|
// service.
|
||||||
|
package textutil
|
||||||
|
|
||||||
|
import "unicode/utf8"
|
||||||
|
|
||||||
|
// Ellipsis marks a value that was cut short.
|
||||||
|
const Ellipsis = "…"
|
||||||
|
|
||||||
|
// Truncate shortens s to at most limit CHARACTERS, appending an ellipsis when
|
||||||
|
// anything was dropped.
|
||||||
|
//
|
||||||
|
// It exists because four call sites did `s[:limit] + "…"` (#93). Both halves
|
||||||
|
// of that are byte operations: len() counts bytes and a slice cuts bytes, so
|
||||||
|
// on Swedish text — where å ä ö are two bytes and an em dash is three — the
|
||||||
|
// cut lands mid-character and leaves an orphaned lead byte. The result is
|
||||||
|
// invalid UTF-8.
|
||||||
|
//
|
||||||
|
// That failed loudly in exactly one place, wiki/index.md, which is written
|
||||||
|
// raw to disk and then rejected by every strict decoder. The other three fed
|
||||||
|
// MCP responses, where encoding/json silently substitutes U+FFFD — so those
|
||||||
|
// had been quietly eating the last character of truncated excerpts with no
|
||||||
|
// error anywhere.
|
||||||
|
//
|
||||||
|
// The marker is appended only when content was actually dropped: an ellipsis
|
||||||
|
// on complete text is a lie about the content.
|
||||||
|
func Truncate(s string, limit int) string {
|
||||||
|
if limit <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if utf8.RuneCountInString(s) <= limit {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk runes rather than converting to []rune: the input can be a whole
|
||||||
|
// note body, and this runs on the read path.
|
||||||
|
count := 0
|
||||||
|
for i := range s {
|
||||||
|
if count == limit {
|
||||||
|
return s[:i] + Ellipsis
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
return s + Ellipsis
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
// ingestion/internal/textutil/truncate_test.go
|
||||||
|
package textutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/textutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTruncate_ShortStringsAreUntouched(t *testing.T) {
|
||||||
|
for _, s := range []string{"", "kort", "räv", "a — b"} {
|
||||||
|
if got := textutil.Truncate(s, 100); got != s {
|
||||||
|
t.Errorf("Truncate(%q, 100) = %q, want it unchanged", s, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncate_NeverCutsInsideACharacter(t *testing.T) {
|
||||||
|
// The property, not an example: whatever the limit, the result must be
|
||||||
|
// valid UTF-8. Byte-slicing Swedish text (å ä ö) or an em dash produces
|
||||||
|
// an orphaned lead byte, which is what corrupted wiki/index.md (#93).
|
||||||
|
s := strings.Repeat("räv — ål — ökäd ", 20)
|
||||||
|
|
||||||
|
for limit := 0; limit <= len(s)+5; limit++ {
|
||||||
|
got := textutil.Truncate(s, limit)
|
||||||
|
if !utf8.ValidString(got) {
|
||||||
|
t.Fatalf("Truncate(s, %d) produced invalid UTF-8: %q", limit, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncate_CountsCharactersNotBytes(t *testing.T) {
|
||||||
|
// "räv" is 4 bytes, 3 runes. A byte-based limit of 3 would cut inside the
|
||||||
|
// "ä"; a rune-based one returns it whole.
|
||||||
|
if got := textutil.Truncate("räv", 3); got != "räv" {
|
||||||
|
t.Errorf("Truncate(\"räv\", 3) = %q, want the whole word", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := textutil.Truncate("räven springer", 5)
|
||||||
|
if utf8.RuneCountInString(got) != 6 { // 5 runes + the ellipsis
|
||||||
|
t.Errorf("Truncate(...) = %q, want 5 runes plus the marker", got)
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(got, "…") {
|
||||||
|
t.Errorf("Truncate(...) = %q, want it marked as truncated", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncate_MarksOnlyWhenSomethingWasDropped(t *testing.T) {
|
||||||
|
// An ellipsis on text that was not cut is a lie about the content.
|
||||||
|
if got := textutil.Truncate("räv", 10); strings.HasSuffix(got, "…") {
|
||||||
|
t.Errorf("Truncate(%q, 10) = %q, want no truncation marker", "räv", got)
|
||||||
|
}
|
||||||
|
if got := textutil.Truncate("räv", 3); strings.HasSuffix(got, "…") {
|
||||||
|
t.Errorf("exact-length input must not be marked: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncate_NonPositiveLimitYieldsNothingRatherThanPanicking(t *testing.T) {
|
||||||
|
for _, limit := range []int{0, -1, -100} {
|
||||||
|
if got := textutil.Truncate("räven", limit); got != "" {
|
||||||
|
t.Errorf("Truncate(_, %d) = %q, want empty", limit, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mathiasbq/hyperguild/ingestion/internal/textutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RebuildIndex writes brain/wiki/index.md from the current wiki contents.
|
// RebuildIndex writes brain/wiki/index.md from the current wiki contents.
|
||||||
@@ -62,10 +64,7 @@ func pageFirstSentence(brainDir string, e Entry) string {
|
|||||||
if line == "" || strings.HasPrefix(line, "#") {
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(line) > 100 {
|
return textutil.Truncate(line, 100)
|
||||||
return line[:100] + "…"
|
|
||||||
}
|
|
||||||
return line
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,34 +42,35 @@ These actions are **carried into the Phase 4 capture call** as `tickets[]` rathe
|
|||||||
|
|
||||||
## Phase 4 — Capture (one uniform call)
|
## Phase 4 — Capture (one uniform call)
|
||||||
|
|
||||||
Persist the session via a **single `capture` call** (the `brain:capture` MCP tool, live on the Claude.ai connector). Capture owns the writes server-side — insights → brain, action items → Gitea tickets, summary → ai-sessions — plus the I1 sovereignty gate, the I5 audit record, and the supersession/read-after-write discipline. The skill's job is to *assemble the payload*, not to write each store itself. Do NOT fall back to separate `gitea:file_write_branch` + `brain_write` steps unless `capture` is unreachable (see fallback below).
|
Persist the session via a **single `capture` call** (the `brain:capture` MCP tool, live on the Claude.ai connector). Capture owns the writes server-side — insights → brain, action items → Gitea tickets — plus the I1 sovereignty gate, the I5 audit record, and the supersession/read-after-write discipline. The skill's job is to *assemble the payload*, not to write each store itself. Do NOT fall back to separate `gitea:file_write_branch` + `brain_write` steps unless `capture` is unreachable (see fallback below).
|
||||||
|
|
||||||
|
**Note:** capture no longer writes a session summary anywhere (the `summary` → `ai-sessions` write path was removed — it produced a frontmatter shape `ai-sessions`' own pipeline couldn't parse, silently invisible to that repo's own audit tooling; see `ai-sessions#13`). If the session's decisions/artifacts are worth a durable narrative beyond the `insights[]` this skill writes to the brain, that's a separate, explicit call — not something this skill does implicitly.
|
||||||
|
|
||||||
**Assemble one payload:**
|
**Assemble one payload:**
|
||||||
|
|
||||||
- **`insights[]`** — the generalizable learnings from Phase 1 (decisions/failures worth re-reading). Each: `{text, wing, hall}`; add `supersede_slug` to revise a prior note in place instead of creating a duplicate. `hall` ∈ facts/decisions/failures/hypotheses/sources.
|
- **`insights[]`** — the generalizable learnings from Phase 1 (decisions/failures worth re-reading). Each: `{text, wing, hall}`; add `supersede_slug` to revise a prior note in place instead of creating a duplicate. `hall` ∈ facts/decisions/failures/hypotheses/sources.
|
||||||
- **`tickets[]`** — the issue actions from Phase 3: `{repo, action, ...}` where action ∈ create/close/comment. Owner is always `mathias` (server-forced).
|
- **`tickets[]`** — the issue actions from Phase 3: `{repo, action, ...}` where action ∈ create/close/comment. Owner is always `mathias` (server-forced).
|
||||||
- **`summary`** — `{title, body, repos_touched}`. Capture writes it to `ai-sessions` and stamps `fidelity` in frontmatter. Body stays reconstructable: one-paragraph summary, decisions, key artifacts, open threads.
|
|
||||||
- **`context`** — `{harness: "claudeai-chat", session_ref: <chatid8-or-slug>, fidelity: "live-capture", actor: "mathias", classification: <see gate below>}`.
|
- **`context`** — `{harness: "claudeai-chat", session_ref: <chatid8-or-slug>, fidelity: "live-capture", actor: "mathias", classification: <see gate below>}`.
|
||||||
|
|
||||||
**THE CLASSIFICATION GATE (read before calling — this is where capture refuses).**
|
**THE CLASSIFICATION GATE (read before calling — this is where capture refuses).**
|
||||||
Capture computes an **effective classification = the strictest across EVERY target it touches** (each insight's `wing`, each ticket's `repo`, and every entry in `summary.repos_touched`), then refuses if that effective level is `confidential` and the origin is us-nexus (claude.ai is us-nexus). Levels come from `classification.yaml` at the brain root (source of truth, #67), with the code defaults as the floor: `hyperguild`/`homelab` → internal; `client-*` → confidential; **anything untagged → confidential (fail-safe)**.
|
Capture computes an **effective classification = the strictest across EVERY target it touches** (each insight's `wing`, each ticket's `repo`), then refuses if that effective level is `confidential` and the origin is us-nexus (claude.ai is us-nexus). Levels come from `classification.yaml` at the brain root (source of truth, #67), with the code defaults as the floor: `hyperguild`/`homelab` → internal; `client-*` → confidential; **anything untagged → confidential (fail-safe)**.
|
||||||
- **Tagged `internal` today** (safe through claude.ai): wings `hyperguild`, `homelab`; repos `brain`, `ai-sessions`, `infra`, `hyperguild`, `homelab`, `tapir`, `agentsquad`, `jepa-fx-risk`, `swedsl`. Treat `classification.yaml` as authoritative — this list is a hint, not gospel.
|
- **Tagged `internal` today** (safe through claude.ai): wings `hyperguild`, `homelab`; repos `brain`, `ai-sessions`, `infra`, `hyperguild`, `homelab`, `tapir`, `agentsquad`, `jepa-fx-risk`, `swedsl`. Treat `classification.yaml` as authoritative — this list is a hint, not gospel.
|
||||||
- Declare `context.classification: "internal"` for normal homelab work.
|
- Declare `context.classification: "internal"` for normal homelab work.
|
||||||
- `summary.repos_touched`, insight `wing`s, and ticket `repo`s are classification INPUTS, not free-form metadata — every target must resolve `internal` or the whole capture escalates to `confidential` and the gate refuses via claude.ai. Listing the central homelab repos (incl. `brain`/`ai-sessions`) is now fine; they're tagged. The summary always lands in `ai-sessions` (internal), so the summary path itself never escalates.
|
- Insight `wing`s and ticket `repo`s are classification INPUTS, not free-form metadata — every target must resolve `internal` or the whole capture escalates to `confidential` and the gate refuses via claude.ai. Listing the central homelab repos (incl. `brain`/`ai-sessions`) is now fine; they're tagged.
|
||||||
- If a session genuinely touched **`client-*` or otherwise-untagged** material, it cannot be captured through claude.ai — note that in the verdict rather than trying to force it.
|
- If a session genuinely touched **`client-*` or otherwise-untagged** material, it cannot be captured through claude.ai — note that in the verdict rather than trying to force it.
|
||||||
|
|
||||||
**GATE — dry-run first, then execute.**
|
**GATE — dry-run first, then execute.**
|
||||||
1. Call `capture` with `dry_run: true`. It validates the whole payload and returns the would-be receipt + `effective_classification`, writing nothing.
|
1. Call `capture` with `dry_run: true`. It validates the whole payload and returns the would-be receipt + `effective_classification`, writing nothing.
|
||||||
2. **STOP. Show the dry-run receipt** (effective classification, the insights/tickets/summary that would land) and get explicit confirmation.
|
2. **STOP. Show the dry-run receipt** (effective classification, the insights/tickets that would land) and get explicit confirmation.
|
||||||
3. On confirmation, call `capture` again with `dry_run: false`. Read the returned receipt: it is partial-aware (`errors[]`, per-item `ok`). Report exactly what landed.
|
3. On confirmation, call `capture` again with `dry_run: false`. Read the returned receipt: it is partial-aware (`errors[]`, per-item `ok`). Report exactly what landed.
|
||||||
|
|
||||||
If `capture` is **unreachable** (tool not on the connector — e.g. a session that started before a deploy; a tool-list refresh usually fixes it): say so. Only then fall back to the legacy inline path (`gitea:file_write_branch` summary + `brain_write`/`brain_update` + `brain_get` confirm), and note in the verdict that the I5 audit record was NOT produced.
|
If `capture` is **unreachable** (tool not on the connector — e.g. a session that started before a deploy; a tool-list refresh usually fixes it): say so. Only then fall back to the legacy inline path (`brain_write`/`brain_update` + `brain_get` confirm), and note in the verdict that the I5 audit record was NOT produced.
|
||||||
|
|
||||||
## Phase 5 — Verdict
|
## Phase 5 — Verdict
|
||||||
|
|
||||||
Deliver a final "safe to archive" verdict in the chat. Either:
|
Deliver a final "safe to archive" verdict in the chat. Either:
|
||||||
|
|
||||||
- **SAFE TO ARCHIVE** — list what landed from the capture receipt (issues closed/filed with numbers, summary path, brain note ids/paths) so the trail is auditable. Then list anything still in the user's queue (e.g. a PR awaiting their merge, a decision owed next session).
|
- **SAFE TO ARCHIVE** — list what landed from the capture receipt (issues closed/filed with numbers, brain note ids/paths) so the trail is auditable. Then list anything still in the user's queue (e.g. a PR awaiting their merge, a decision owed next session).
|
||||||
- **NOT YET** — name the specific gate that wasn't passed, the capture refusal reason, or the per-item error from the receipt, and what to do about it.
|
- **NOT YET** — name the specific gate that wasn't passed, the capture refusal reason, or the per-item error from the receipt, and what to do about it.
|
||||||
|
|
||||||
Never claim safe-to-archive if the capture refused, any receipt item errored, or a gated confirmation was declined. The verdict is the skill's contract: if it says safe, the session can be lost without losing the work.
|
Never claim safe-to-archive if the capture refused, any receipt item errored, or a gated confirmation was declined. The verdict is the skill's contract: if it says safe, the session can be lost without losing the work.
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
**Status:** Decisions resolved 2026-06-22 (§4). Ready for implementation scoping. `capture` is a
|
**Status:** Decisions resolved 2026-06-22 (§4). Ready for implementation scoping. `capture` is a
|
||||||
privileged cross-harness write path touching brain + Gitea + ai-sessions.
|
privileged cross-harness write path touching brain + Gitea + ai-sessions.
|
||||||
|
**Superseded 2026-07-27:** the ai-sessions summary write path specified below was removed post-ship
|
||||||
|
(see `specs/capture-implementation-report.md`). `capture` now touches brain + Gitea only.
|
||||||
**Tracks:** hyperguild #49.
|
**Tracks:** hyperguild #49.
|
||||||
**Governed by:** `infra/docs/architecture/01-invariants.md` (I1–I5), the admissibility test in
|
**Governed by:** `infra/docs/architecture/01-invariants.md` (I1–I5), the admissibility test in
|
||||||
`00-synthesis-model.md`, and the distributed-consolidation shape mandated by
|
`00-synthesis-model.md`, and the distributed-consolidation shape mandated by
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
# Capture capability — implementation report (as-built)
|
# Capture capability — implementation report (as-built)
|
||||||
|
|
||||||
**Status:** Shipped 2026-06-23, tagged `v0.11.0`. Epic hyperguild #49 (sub-issues #50–#55) closed.
|
**Status:** Shipped 2026-06-23, tagged `v0.11.0`. Epic hyperguild #49 (sub-issues #50–#55) closed.
|
||||||
|
**Superseded 2026-07-27:** the `summary` → `ai-sessions` write path documented below was removed.
|
||||||
|
It wrote a frontmatter shape (`title`/`harness`/`fidelity`/`captured_at`/`repos_touched`) that
|
||||||
|
`ai-sessions`' own extract/audit pipeline couldn't parse — invisible to that repo's own audit
|
||||||
|
tooling and bypassing its redaction/completeness gates (`ai-sessions#13`). `capture` now persists
|
||||||
|
insights → brain and action items → Gitea tickets only. This document is kept as the historical
|
||||||
|
as-built record of what shipped in #49; treat every `summary`/ai-sessions reference below as
|
||||||
|
retired, not current behaviour.
|
||||||
**Spec:** `specs/capture-bdd-spec.md` (the design contract this implements).
|
**Spec:** `specs/capture-bdd-spec.md` (the design contract this implements).
|
||||||
**Governed by:** `infra/docs/architecture/01-invariants.md` (I1–I5) + the I2 acceptance ledger entry in `infra/docs/security-baseline.md`.
|
**Governed by:** `infra/docs/architecture/01-invariants.md` (I1–I5) + the I2 acceptance ledger entry in `infra/docs/security-baseline.md`.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user