feat(brain): audit-log every tools/call; server-owned write provenance (#147)
Closes #144 (audit line per tools/call) and #145 (origin/written_by stamped on every model-reachable write, surfaced in brain_query/brain_get). Passed pr-gatekeeper after four rounds of adversarial review, verified by execution: #147 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mathias <mthbqv@gmail.com>
This commit was merged in pull request #147.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Only the leading frontmatter is provenance; a body line that looks like a
|
||||
// key is prose (the doc comment's claim, now observed).
|
||||
func TestExtractProvenanceIgnoresBodyLines(t *testing.T) {
|
||||
o, w := extractProvenance("---\ntitle: t\n---\norigin: static\nwritten_by: local-cli\n")
|
||||
assert.Equal(t, "", o)
|
||||
assert.Equal(t, "", w)
|
||||
o, w = extractProvenance("# no frontmatter\norigin: static\n")
|
||||
assert.Equal(t, "", o)
|
||||
assert.Equal(t, "", w)
|
||||
o, w = extractProvenance("---\norigin: jwt\nwritten_by: attacker\n---\norigin: static\n")
|
||||
assert.Equal(t, "jwt", o)
|
||||
assert.Equal(t, "attacker", w)
|
||||
}
|
||||
|
||||
// Round 3 (B1): an indented "---" inside a block scalar is content, not the
|
||||
// closing fence — the same rule as frontmatter.Split.
|
||||
func TestExtractProvenanceUsesTheSharedFenceRule(t *testing.T) {
|
||||
o, w := extractProvenance("---\nk: |\n ---\norigin: jwt\nwritten_by: attacker\n---\nbody\n")
|
||||
assert.Equal(t, "jwt", o)
|
||||
assert.Equal(t, "attacker", w)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
sharedfm "github.com/mathiasbq/hyperguild/ingestion/internal/frontmatter"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -48,6 +49,11 @@ type Result struct {
|
||||
// (infra#72). Read from frontmatter when present, otherwise
|
||||
// inferred from the parent directory.
|
||||
Tier string `json:"tier,omitempty"`
|
||||
// Origin/WrittenBy surface write provenance (hyperguild#145) so a
|
||||
// reader can discount an entry a connector session wrote. Omitted for
|
||||
// notes written before provenance existed — never fabricated.
|
||||
Origin string `json:"origin,omitempty"`
|
||||
WrittenBy string `json:"written_by,omitempty"`
|
||||
}
|
||||
|
||||
// tierWeight maps the DIKW tier to a score multiplier applied right
|
||||
@@ -146,6 +152,7 @@ func QueryContext(ctx context.Context, brainDir string, opts QueryOptions) ([]Re
|
||||
rel = filepath.ToSlash(rel)
|
||||
wing, hall := extractWingHall(string(content), rel)
|
||||
tier := extractTier(string(content), rel)
|
||||
origin, writtenBy := extractProvenance(string(content))
|
||||
results = append(results, Result{
|
||||
Path: rel,
|
||||
Title: extractTitle(string(content), d.Name()),
|
||||
@@ -154,6 +161,9 @@ func QueryContext(ctx context.Context, brainDir string, opts QueryOptions) ([]Re
|
||||
Wing: wing,
|
||||
Hall: hall,
|
||||
Tier: tier,
|
||||
|
||||
Origin: origin,
|
||||
WrittenBy: writtenBy,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
@@ -272,6 +282,7 @@ func hydrate(brainDir, relPath string) (Result, error) {
|
||||
}
|
||||
wing, hall := extractWingHall(string(content), relPath)
|
||||
tier := extractTier(string(content), relPath)
|
||||
origin, writtenBy := extractProvenance(string(content))
|
||||
return Result{
|
||||
Path: relPath,
|
||||
Title: extractTitle(string(content), filepath.Base(relPath)),
|
||||
@@ -279,9 +290,44 @@ func hydrate(brainDir, relPath string) (Result, error) {
|
||||
Wing: wing,
|
||||
Hall: hall,
|
||||
Tier: tier,
|
||||
|
||||
Origin: origin,
|
||||
WrittenBy: writtenBy,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// extractProvenance reads origin/written_by from the leading frontmatter
|
||||
// block only, located with the shared fence rule (frontmatter.Split: a "---"
|
||||
// at column 0) so an indented "---" inside a block scalar is content, not a
|
||||
// closing fence (PR #147 review round 3). A body line is never provenance.
|
||||
func extractProvenance(content string) (origin, writtenBy string) {
|
||||
block, _, ok := sharedfm.Split([]byte(content))
|
||||
if !ok {
|
||||
return "", ""
|
||||
}
|
||||
for _, line := range strings.Split(string(block), "\n") {
|
||||
if line == "" || line[0] == ' ' || line[0] == '\t' {
|
||||
continue
|
||||
}
|
||||
key, val, found := strings.Cut(line, ":")
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
v := strings.Trim(strings.TrimSpace(val), `"'`)
|
||||
switch strings.TrimSpace(key) {
|
||||
case "origin":
|
||||
if origin == "" {
|
||||
origin = v
|
||||
}
|
||||
case "written_by":
|
||||
if writtenBy == "" {
|
||||
writtenBy = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return origin, writtenBy
|
||||
}
|
||||
|
||||
// resolveRoots returns the directories to walk for the given wing/hall
|
||||
// filters. Validates hall against the closed vocabulary when set.
|
||||
func resolveRoots(brainDir, wing, hall string) ([]string, error) {
|
||||
|
||||
Reference in New Issue
Block a user