The PR #147 review forged origin: static as a JWT principal through three tools the first version never touched (brain_edit, brain_ingest_raw, ingest_raw + brain_promote), and through YAML spellings a key-name match missed (quoted, escaped, tagged, flow-mapped, merge keys, lone CR). - api.StampProvenance removes every spelling of origin/written_by from the frontmatter and appends the real values. A line is dropped if EITHER yaml.v3 (decoding quotes, escapes, tags, flow maps) or the brain's line-oriented readers would read it as provenance; complex keys, orphan values and merge keys are dropped outright. Lone CRs in frontmatter become LF. Principal values that are not bare words are quoted. - Applied as the last step of writeHallNote, UpdateNote, PromoteNote (re-attributed to the promoter), brain_edit (re-attributed to the editor) and both ingests (every page written or merged). REST /write and /promote carry no principal and stamp unknown. - Ingest: newlines in scalar fields fold to spaces; a page type must be a plain lower-case name (../raw wrote outside wiki/, pre-existing). - Audit: outcome is ok|error, never the error text, which echoed argument values. Tests: every escape from the review is a table case asserting the invariant on the file (parses as YAML, yaml.v3 and brain_get agree, exactly the stamped lines). Plus REST /write, REST capture AuthPath, body-line provenance in search, and quoted principals; two of the review's surviving flips now go red. Three tests renamed so names claim only what they assert. Refs #145 #144 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
3.7 KiB
Go
132 lines
3.7 KiB
Go
// ingestion/internal/pipeline/build.go
|
|
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/mathiasbq/hyperguild/ingestion/internal/wiki"
|
|
)
|
|
|
|
// BuildPages converts RawPages from the LLM into wiki.Pages with computed slugs,
|
|
// paths, and YAML frontmatter. sourceSlug is the slug of the source being ingested
|
|
// (derived from the filename, not the LLM title). Pages whose title resolves to an
|
|
// empty slug are skipped and returned as warnings instead.
|
|
func BuildPages(rawPages []RawPage, sourceSlug, date string) ([]wiki.Page, []string) {
|
|
out := make([]wiki.Page, 0, len(rawPages))
|
|
var warnings []string
|
|
for _, rp := range rawPages {
|
|
// The type becomes a directory under wiki/; "../raw" wrote outside it
|
|
// (PR #147 review). Only a plain lower-case name is a page type.
|
|
if !validPageType.MatchString(rp.Type) {
|
|
warnings = append(warnings, fmt.Sprintf("skipped page with invalid type %q", rp.Type))
|
|
continue
|
|
}
|
|
slug := computeSlug(rp, sourceSlug)
|
|
if slug == "" {
|
|
warnings = append(warnings, fmt.Sprintf("skipped page with empty title (type: %s)", rp.Type))
|
|
continue
|
|
}
|
|
out = append(out, buildPage(rp, sourceSlug, date))
|
|
}
|
|
return out, warnings
|
|
}
|
|
|
|
var validPageType = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)
|
|
|
|
func computeSlug(rp RawPage, sourceSlug string) string {
|
|
if rp.Type == "source" {
|
|
return sourceSlug
|
|
}
|
|
return wiki.Slug(rp.Title)
|
|
}
|
|
|
|
func buildPage(rp RawPage, sourceSlug, date string) wiki.Page {
|
|
var slug, dir string
|
|
switch rp.Type {
|
|
case "source":
|
|
slug = sourceSlug
|
|
dir = "wiki/sources"
|
|
case "concept":
|
|
slug = wiki.Slug(rp.Title)
|
|
dir = "wiki/concepts"
|
|
case "entity":
|
|
slug = wiki.Slug(rp.Title)
|
|
dir = "wiki/entities"
|
|
default:
|
|
slug = wiki.Slug(rp.Title)
|
|
dir = "wiki/" + rp.Type
|
|
}
|
|
|
|
path := dir + "/" + slug + ".md"
|
|
fm := buildFrontmatter(rp, date)
|
|
|
|
return wiki.Page{
|
|
Path: path,
|
|
Content: fm + "\n" + rp.Content,
|
|
}
|
|
}
|
|
|
|
func buildFrontmatter(rp RawPage, date string) string {
|
|
var sb strings.Builder
|
|
sb.WriteString("---\n")
|
|
fmt.Fprintf(&sb, "title: %s\n", yamlScalar(rp.Title))
|
|
|
|
switch rp.Type {
|
|
case "source":
|
|
subtype := rp.Subtype
|
|
if subtype == "" {
|
|
subtype = "article"
|
|
}
|
|
fmt.Fprintf(&sb, "type: %s\n", yamlScalar(subtype))
|
|
if rp.Domain != "" {
|
|
fmt.Fprintf(&sb, "domain: %s\n", yamlScalar(rp.Domain))
|
|
}
|
|
fmt.Fprintf(&sb, "date_ingested: %s\n", date)
|
|
fmt.Fprintf(&sb, "last_updated: %s\n", date)
|
|
if rp.SourceHash != "" {
|
|
fmt.Fprintf(&sb, "source_hash: %s\n", rp.SourceHash)
|
|
}
|
|
if rp.Source != "" {
|
|
fmt.Fprintf(&sb, "source: %s\n", yamlScalar(rp.Source))
|
|
}
|
|
if rp.Author != "" {
|
|
fmt.Fprintf(&sb, "author: %s\n", yamlScalar(rp.Author))
|
|
}
|
|
if rp.Published != "" {
|
|
fmt.Fprintf(&sb, "published: %s\n", yamlScalar(rp.Published))
|
|
}
|
|
case "concept":
|
|
if rp.Domain != "" {
|
|
fmt.Fprintf(&sb, "domain: %s\n", yamlScalar(rp.Domain))
|
|
}
|
|
fmt.Fprintf(&sb, "last_updated: %s\n", date)
|
|
case "entity":
|
|
if rp.Subtype != "" {
|
|
fmt.Fprintf(&sb, "type: %s\n", yamlScalar(rp.Subtype))
|
|
}
|
|
if rp.Domain != "" {
|
|
fmt.Fprintf(&sb, "domain: %s\n", yamlScalar(rp.Domain))
|
|
}
|
|
fmt.Fprintf(&sb, "last_updated: %s\n", date)
|
|
default:
|
|
if rp.Domain != "" {
|
|
fmt.Fprintf(&sb, "domain: %s\n", yamlScalar(rp.Domain))
|
|
}
|
|
fmt.Fprintf(&sb, "last_updated: %s\n", date)
|
|
}
|
|
|
|
fmt.Fprintf(&sb, "aliases:\n - %s\n", yamlScalar(rp.Title))
|
|
sb.WriteString("---\n")
|
|
return sb.String()
|
|
}
|
|
|
|
func yamlScalar(s string) string {
|
|
// A raw newline or CR would end the scalar for every line-oriented
|
|
// frontmatter reader and start a new key (PR #147 review: a title of
|
|
// "x\norigin: static" forged provenance). Fold all line breaks to spaces.
|
|
s = strings.NewReplacer("\r\n", " ", "\r", " ", "\n", " ").Replace(s)
|
|
return "'" + strings.ReplaceAll(s, "'", "''") + "'"
|
|
}
|