Compare commits
25 Commits
deploy/pha
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6900101506 | ||
|
|
396eeb175e | ||
|
|
36d8e514d4 | ||
| 2ceb0c147c | |||
| cb45c462be | |||
| 6ac0e1ff47 | |||
| a76882de67 | |||
|
|
59580b6e45 | ||
| d793ab0871 | |||
|
|
286aa12bb0 | ||
|
|
f39f560c7f | ||
|
|
df864cc29e | ||
|
|
5c0622c1b1 | ||
| 9fae1c7dd8 | |||
| f9dc574172 | |||
| 24590cc351 | |||
| c95e5361e3 | |||
| 2906683f44 | |||
| c2b4e33897 | |||
| 84fc585c91 | |||
| fd9974d07e | |||
| ea2c722816 | |||
| 72edfec59c | |||
|
|
00fa15f98f | ||
| 0eb151117e |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,4 +1,9 @@
|
||||
# python
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# build output
|
||||
/bin/
|
||||
/dist/
|
||||
/current/
|
||||
*.test
|
||||
|
||||
106
Makefile
Normal file
106
Makefile
Normal file
@@ -0,0 +1,106 @@
|
||||
# Ai Resume - build entry points.
|
||||
#
|
||||
# `make validate` is the content gate: it is what CI and deploy.sh call.
|
||||
# It exits 0 only on a clean content tree.
|
||||
#
|
||||
# `make build` is the site gate: it generates the templ components, compiles the
|
||||
# two small binaries and writes the pre-rendered site into dist/. One binary
|
||||
# (bin/resume-server) serves the whole site; there is no bundler and no Node.
|
||||
|
||||
PYTHON ?= python3
|
||||
VALIDATOR := $(PYTHON) tools/validate_content.py
|
||||
GO ?= go
|
||||
TEMPL ?= templ
|
||||
|
||||
# The runtime image is distroless/static (deploy/Dockerfile): no libc, no
|
||||
# dynamic loader. Everything the server needs is pure Go (modernc.org/sqlite),
|
||||
# so a static binary is buildable and is what the container can actually exec.
|
||||
# Without this the build host's cgo links the nix glibc loader and the container
|
||||
# fails with "exec /usr/bin/resume-server: no such file or directory".
|
||||
CGO_ENABLED ?= 0
|
||||
|
||||
BIN := bin
|
||||
DIST := dist
|
||||
|
||||
.PHONY: build dist generate dev test validate validate-self-test fmt vet clean help \
|
||||
corpus corpus-sync corpus-rebuild corpus-audit corpus-verify corpus-demo corpus-test
|
||||
|
||||
help:
|
||||
@echo "make build templ generate, compile bin/, no dist"
|
||||
@echo "make dist build, then pre-render content into dist/"
|
||||
@echo "make dev pre-render into dist/ and serve it on 127.0.0.1:8096"
|
||||
@echo "make generate run templ generate (generated files are not committed)"
|
||||
@echo "make test go test ./... plus the validator self-test"
|
||||
@echo "make validate validate content/** front-matter against the frozen schema"
|
||||
@echo "make fmt vet gofmt / go vet over cmd and internal"
|
||||
@echo "make clean remove bin/ and dist/"
|
||||
@echo ""
|
||||
@echo "corpus (Phase 4, python3 -m corpus.cli):"
|
||||
@echo "make corpus rebuild the corpus from content/ (drops the collection)"
|
||||
@echo "make corpus-sync incremental update; only changed documents are re-embedded"
|
||||
@echo "make corpus-audit freshness + drift audit (exit 1 on findings)"
|
||||
@echo "make corpus-verify check the index against content/ and Qdrant"
|
||||
@echo "make corpus-demo same query with and without an area filter"
|
||||
@echo "make corpus-test corpus unit tests (no container, no network)"
|
||||
|
||||
build: generate
|
||||
@mkdir -p $(BIN)
|
||||
CGO_ENABLED=$(CGO_ENABLED) $(GO) build -trimpath -buildvcs=false -o $(BIN)/resume-build ./cmd/build
|
||||
CGO_ENABLED=$(CGO_ENABLED) $(GO) build -trimpath -buildvcs=false -o $(BIN)/resume-server ./cmd/server
|
||||
|
||||
dist: build
|
||||
@$(BIN)/resume-build --dist $(DIST)
|
||||
|
||||
dev: dist
|
||||
@RESUME_DIST=$(DIST) $(BIN)/resume-server
|
||||
|
||||
generate:
|
||||
@command -v $(TEMPL) >/dev/null || { echo "templ is not on PATH; run this inside 'nix develop'"; exit 1; }
|
||||
@$(TEMPL) generate
|
||||
|
||||
test: generate validate-self-test
|
||||
@$(GO) test ./...
|
||||
|
||||
validate:
|
||||
@$(VALIDATOR)
|
||||
|
||||
validate-self-test:
|
||||
@$(VALIDATOR) --self-test
|
||||
|
||||
fmt:
|
||||
@gofmt -l -w cmd internal
|
||||
|
||||
vet:
|
||||
@$(GO) vet ./...
|
||||
|
||||
# -- corpus (Phase 4, docs/phase-4-corpus.md) --------------------------------
|
||||
#
|
||||
# The corpus is data, not a build artefact: it lives in data/corpus/ (gitignored)
|
||||
# and in Qdrant. RESUME_QDRANT_URL defaults to http://127.0.0.1:6333 and
|
||||
# RESUME_EMBED_PROVIDER defaults to local-hashing; production sets both in the
|
||||
# environment, never in this file.
|
||||
|
||||
CORPUS := $(PYTHON) -m corpus.cli
|
||||
|
||||
corpus: corpus-rebuild
|
||||
|
||||
corpus-rebuild:
|
||||
@$(CORPUS) rebuild
|
||||
|
||||
corpus-sync:
|
||||
@$(CORPUS) sync
|
||||
|
||||
corpus-audit:
|
||||
@$(CORPUS) audit --fail-on-findings
|
||||
|
||||
corpus-verify:
|
||||
@$(CORPUS) verify
|
||||
|
||||
corpus-demo:
|
||||
@$(CORPUS) demo-filter --query "distributed audio endpoints" --area edge
|
||||
|
||||
corpus-test:
|
||||
@$(PYTHON) -m unittest discover -s corpus/tests -t .
|
||||
|
||||
clean:
|
||||
@rm -rf $(BIN) $(DIST)
|
||||
42
README.md
42
README.md
@@ -37,6 +37,42 @@ This folder **is** the git working copy of `sam/ai-resume`. One repo, one produc
|
||||
|
||||
Previously held an abandoned Vite/React scaffold. It was replaced by this build spec; the history is retained because it honestly records the false start, and it is referenced in the site's "alternatives considered" content.
|
||||
|
||||
## Build and run
|
||||
|
||||
The site is one Go binary serving pre-rendered HTML. There is no Node, no bundler and no
|
||||
`node_modules`; `go.sum` is the only dependency lock.
|
||||
|
||||
```sh
|
||||
nix develop # go, templ, sqlite3, make, curl
|
||||
make validate # content gate: front-matter against the frozen schema
|
||||
make test # go test ./... plus the validator self-test
|
||||
make dist # templ generate → bin/ → pre-rendered dist/
|
||||
make dev # build, then serve on http://127.0.0.1:8096
|
||||
```
|
||||
|
||||
`bin/resume-build` reads `content/` and writes `dist/` (pages, stage fragments, the fragment
|
||||
manifest, the SQLite FTS5 index, assets). `bin/resume-server` serves `dist/` and never reads
|
||||
`content/`, never calls a model, and never opens a socket to another service — that is what
|
||||
keeps acceptance test A1 true.
|
||||
|
||||
The two things that are not files are `/search` (FTS5 over page titles and body text) and
|
||||
`/api/stage/stream` (the presentation panel driven by an event stream instead of a route).
|
||||
|
||||
The retrieval corpus (Phase 4) is built separately, because it writes to a service rather
|
||||
than to `dist/`:
|
||||
|
||||
```sh
|
||||
docker compose -f deploy/qdrant.yml up -d # on the runtime host, loopback only
|
||||
make corpus # content/ -> chunks -> Qdrant + SQLite FTS5
|
||||
make corpus-verify # does the index agree with content/ and with Qdrant?
|
||||
make corpus-audit # stale documents, index drift, contract drift
|
||||
make corpus-test # 76 unit tests, no container and no network
|
||||
```
|
||||
|
||||
The site never calls Qdrant, so acceptance test A1 is unaffected: stopping the corpus
|
||||
services leaves every page working. Operator manual and measurements:
|
||||
`docs/phase-4-notes.md`.
|
||||
|
||||
## Ground rules
|
||||
|
||||
- **Do not touch other systems.** No stopping containers, no editing other projects, no `nixos-rebuild`, no Caddy changes except the single block specified in Phase 3 — and that one Sam applies.
|
||||
@@ -48,10 +84,10 @@ Previously held an abandoned Vite/React scaffold. It was replaced by this build
|
||||
## Current state
|
||||
|
||||
- [x] Repo prepared, scaffold retired, build spec written
|
||||
- [ ] Phase 1 — content source of truth
|
||||
- [ ] Phase 2 — static site
|
||||
- [x] Phase 1 — content source of truth (40 documents across six areas, `make validate`)
|
||||
- [x] Phase 2 — static site (`make dist`, 45 pages, 86 stage fragments, FTS5 search)
|
||||
- [ ] Phase 3 — public deployment
|
||||
- [ ] Phase 4 — corpus
|
||||
- [x] Phase 4 — corpus and retrieval (51 chunks, FTS5 + Qdrant, freshness audit)
|
||||
- [ ] Phase 5 — AI overlay
|
||||
- [ ] Phase 6 — guardrails
|
||||
- [ ] Phase 7 — polish
|
||||
|
||||
58
cmd/build/main.go
Normal file
58
cmd/build/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Command build pre-renders the whole site into a directory.
|
||||
//
|
||||
// It reads content/ and writes dist/: every page as HTML, one file per stage
|
||||
// fragment, the fragment manifest, the SQLite FTS5 index, the assets and the
|
||||
// static files. No network call is made, and the output is what the server
|
||||
// serves — Mode A never renders a page per request.
|
||||
//
|
||||
// Configuration is from the environment, so the same binary is used by
|
||||
// `make build`, by CI and by the container image:
|
||||
//
|
||||
// RESUME_ROOT repository root (default ".")
|
||||
// RESUME_CONTENT content directory (default $RESUME_ROOT/content)
|
||||
// RESUME_DIST output directory (default $RESUME_ROOT/dist)
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gitea.lab.audasmedia.com.au/sam/ai-resume/internal/site"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "usage: build [--root DIR] [--content DIR] [--dist DIR]\n")
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "environment: RESUME_ROOT, RESUME_CONTENT, RESUME_DIST\n")
|
||||
}
|
||||
root := flag.String("root", env("RESUME_ROOT", "."), "repository root")
|
||||
contentDir := flag.String("content", env("RESUME_CONTENT", ""), "content directory")
|
||||
distDir := flag.String("dist", env("RESUME_DIST", ""), "output directory")
|
||||
flag.Parse()
|
||||
|
||||
cfg := site.Config{Root: *root, ContentDir: *contentDir, OutDir: *distDir}
|
||||
s, err := site.New(cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "build: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := s.Build(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "build: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
info, err := site.LoadBuildInfo(s.Config.OutDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "build: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("built %d pages, %d stage fragments, %d indexed documents into %s\n",
|
||||
info.Pages, info.Fragments, info.Indexed, s.Config.OutDir)
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
155
cmd/server/main.go
Normal file
155
cmd/server/main.go
Normal file
@@ -0,0 +1,155 @@
|
||||
// Command server serves the site the build wrote.
|
||||
//
|
||||
// It reads a directory produced by cmd/build and serves it on loopback. It does
|
||||
// not read content/, does not need Qdrant, OmniRoute or any other service, and
|
||||
// makes no outbound connection, so every page it serves works with the AI stack
|
||||
// stopped (acceptance test A1).
|
||||
//
|
||||
// Configuration comes from the environment so the same binary works under
|
||||
// `make dev`, systemd and compose:
|
||||
//
|
||||
// RESUME_ADDR listen address (default 127.0.0.1:8096)
|
||||
// RESUME_DIST build directory (default "dist")
|
||||
// RESUME_STREAM_DELAY pacing for the streamed panel, e.g. 350ms
|
||||
//
|
||||
// Flags with the same names override the environment for local work.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.lab.audasmedia.com.au/sam/ai-resume/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", env("RESUME_ADDR", server.DefaultAddr), "listen address")
|
||||
dist := flag.String("dist", env("RESUME_DIST", server.DefaultDist), "build directory")
|
||||
streamDelay := flag.Duration("stream-delay", envDuration("RESUME_STREAM_DELAY", server.DefaultStreamDelay), "delay between streamed panel frames")
|
||||
healthCheck := flag.Bool("health-check", false, "ask a running server for /healthz and exit 0 only if static is ready")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "usage: server [--addr HOST:PORT] [--dist DIR] [--stream-delay DURATION] [--health-check]\n")
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "environment: RESUME_ADDR, RESUME_DIST, RESUME_STREAM_DELAY\n")
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
// The container image has no shell and no curl, so the health probe is this
|
||||
// binary talking to itself (deploy/compose.yml healthcheck).
|
||||
if *healthCheck {
|
||||
if err := probe(*addr); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "health check failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("static ready")
|
||||
return
|
||||
}
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
|
||||
site, err := server.New(server.Config{
|
||||
Addr: *addr,
|
||||
DistDir: *dist,
|
||||
StreamDelay: *streamDelay,
|
||||
Logger: log,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("start failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer site.Close()
|
||||
|
||||
// No WriteTimeout on purpose: the stage stream keeps the response open for
|
||||
// as long as it is producing frames. Reads are bounded.
|
||||
srv := &http.Server{
|
||||
Addr: *addr,
|
||||
Handler: site.Handler(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
MaxHeaderBytes: 64 << 10,
|
||||
ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelWarn),
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
log.Info("shutting down")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Warn("shutdown", "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Info("listening", "addr", *addr, "dist", *dist)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Error("serve failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// probe asks a running server whether the static site is ready. Only the static
|
||||
// field decides the exit code: Mode A must report ready with every AI service
|
||||
// stopped (acceptance test A1).
|
||||
func probe(addr string) error {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if host == "" || host == "0.0.0.0" || host == "::" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
res, err := client.Get("http://" + net.JoinHostPort(host, port) + "/healthz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("/healthz returned %s", res.Status)
|
||||
}
|
||||
var body struct {
|
||||
Static struct {
|
||||
Ready bool `json:"ready"`
|
||||
} `json:"static"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(res.Body, 64<<10)).Decode(&body); err != nil {
|
||||
return fmt.Errorf("decode /healthz: %w", err)
|
||||
}
|
||||
if !body.Static.Ready {
|
||||
return errors.New("static is not ready")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v, ok := os.LookupEnv(key); ok && v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func envDuration(key string, fallback time.Duration) time.Duration {
|
||||
v, ok := os.LookupEnv(key)
|
||||
if !ok || v == "" {
|
||||
return fallback
|
||||
}
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s=%q is not a duration: %v\n", key, v, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
return d
|
||||
}
|
||||
62
content/about/about-narrative.md
Normal file
62
content/about/about-narrative.md
Normal file
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: about-narrative
|
||||
area: about
|
||||
level: 2
|
||||
title: "The narrative — including the years that need explaining"
|
||||
claim: "A self-taught career across six technology eras — interrupted on purpose by a non-IT working life and five years overseas — with 2020 to 2026 spent raising two children while building a business."
|
||||
confidence: running
|
||||
period: "1999–"
|
||||
tech: [NixOS, Neovim, Python, JavaScript, React, Go, HTMX]
|
||||
metrics: [{label: "years shipping software", value: 25}, {label: "years spent overseas", value: 5}, {label: "non-IT roles worked", value: 6}]
|
||||
artefacts: [{type: doc, label: "Non-IT working history", url: "content/about/l3/about-non-it-work.md"}, {type: doc, label: "Education", url: "content/about/l3/about-education.md"}, {type: doc, label: "Interests — one honest line each", url: "content/about/l3/about-interests.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [about, narrative, 2020-2026]
|
||||
---
|
||||
|
||||
# The narrative — including the years that need explaining
|
||||
|
||||
**Capability statement.** Someone who learned by doing, in public, on deadlines, and who has kept learning after the deadlines stopped being someone else's. The range is real and the gaps are stated.
|
||||
|
||||
## The beginning
|
||||
|
||||
Programming started as a childhood interest in the 1980s, self-taught alongside art, music, philosophy and sport. The career proper started in the late 1990s building websites for an Australian car marketplace. When interactive front ends arrived — a creative layer that needed a technical spine — there was no course to take, so it was learned directly and immediately put into commercial work. That combination, creative front end with engineering underneath, has been the shape of the work ever since.
|
||||
|
||||
## The agency years
|
||||
|
||||
Freelance in Melbourne, then full-time roles in the United Kingdom during the emergence of online gaming and interactive advertising, then back to freelance where the work was with the largest agencies and their clients — banks, broadcasters, drinks brands, publishers, a global energy company, a design software vendor. Most of it was delivered under deadlines that could not move, and a meaningful share of it was rescue work: rewriting someone else's stalled project and shipping it on their date.
|
||||
|
||||
## The other working life
|
||||
|
||||
Not all of it was software, and that is worth stating rather than hiding. Six roles outside technology — a ski chalet kitchen in the French Alps, wine service, guided horse tours in the Victorian high country, building a stable complex, state government administration, and thoroughbred stable work. They are the reason the engineering reads as practical: a kitchen at service and a production incident have the same shape.
|
||||
|
||||
## The education
|
||||
|
||||
A fine arts degree with philosophy, a master's in international relations and contemporary political theory, and study in music composition and economics. None of it is computer science, and all of it is used: systems thinking, argument, aesthetics, and the habit of asking what a thing is actually for.
|
||||
|
||||
## 2020 to 2026 — stated plainly
|
||||
|
||||
This period is on the page because a résumé that hides it invites the wrong question. It was spent as a full-time parent to two children while building in parallel:
|
||||
|
||||
- A digital signage business taken from idea to customers — the product, the deployments, the support.
|
||||
- A site builder and CMS that carried the marketing for it.
|
||||
- A lost-property tag system, deployed.
|
||||
- A homelab that became the platform for everything else: declarative configuration, containers, backups, networking, public ingress.
|
||||
- An AI estate — pipelines, routing, retrieval, voice, guardrails — built and operated on the same hardware.
|
||||
- A deliberate move from PHP and front-end scripting into Go, systems work and platform engineering, and from hand-installed servers into reproducible configuration.
|
||||
|
||||
The honest framing is in the evidence notes and is repeated here: *full-time parent, building in parallel*. The output of those years is on the rest of this site and can be inspected.
|
||||
|
||||
## What next
|
||||
|
||||
The same three areas at a larger scale: [AI and agentic systems](../ai/ai-systems.md), the [platform engineering underneath them](../platform/platform-estate.md), and the [constrained devices at the edge](../edge/edge-fleet.md). Melbourne based and flexible about location and format — remote, hybrid, office, or travel.
|
||||
|
||||
## Contact policy
|
||||
|
||||
This site publishes no address, no phone number and no family details. Contact is by email, with the address supplied directly rather than scraped from a page. If a phone number belongs on the site, that is Sam's decision to make and it is not made here.
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **No computer science degree.** The trade is a longer, broader practical education; the pages on this site are the argument, not the certificate.
|
||||
- **A period of self-employment is described as such.** It is not dressed up as a sabbatical or as consulting.
|
||||
- **Interests are one honest sentence each.** The previous version of this site invented venues and results. That has been deleted rather than corrected in place.
|
||||
16
content/about/about-overview.md
Normal file
16
content/about/about-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: about-overview
|
||||
area: about
|
||||
level: 1
|
||||
title: "About"
|
||||
claim: "Sam has shipped software for twenty-five years — self-taught, through a non-IT working life and five years overseas — and now builds AI systems, the platforms under them and the devices at the edge."
|
||||
confidence: running
|
||||
period: "1999–"
|
||||
tech: [NixOS, Neovim, Python, JavaScript, React, Go, HTMX]
|
||||
metrics: [{label: "years shipping software", value: 25}, {label: "years spent overseas", value: 5}, {label: "non-IT roles worked", value: 6}]
|
||||
artefacts: [{type: doc, label: "Non-IT working history", url: "content/about/l3/about-non-it-work.md"}, {type: doc, label: "Education", url: "content/about/l3/about-education.md"}, {type: doc, label: "Interests — one honest line each", url: "content/about/l3/about-interests.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [about, narrative, background]
|
||||
---
|
||||
The other five areas prove what can be built; this one explains where the judgement came from. The narrative, including 2020 to 2026, is on the [narrative page](about-narrative.md).
|
||||
35
content/about/l3/about-education.md
Normal file
35
content/about/l3/about-education.md
Normal file
@@ -0,0 +1,35 @@
|
||||
---
|
||||
id: about-education
|
||||
area: about
|
||||
level: 3
|
||||
title: "Artefact — Education"
|
||||
claim: "A fine arts degree with philosophy · a master's in international relations and contemporary political theory · and study in music composition and economics."
|
||||
confidence: historic
|
||||
period: "1997–"
|
||||
tech: []
|
||||
metrics: [{label: "tertiary qualifications completed", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [about, education, artefact]
|
||||
---
|
||||
|
||||
Formal presentation lands in Phase 7. No certificates, transcripts or student identifiers are published.
|
||||
|
||||
## The record
|
||||
|
||||
- **Master of International Relations and Contemporary Political Theory** — University of Westminster, Centre for the Study of Democracy.
|
||||
- **Bachelor of Visual Communication and Fine Arts** — RMIT (Melbourne) and the University of South Australia, 1997–1999.
|
||||
- **Music composition and economics** — studied at La Trobe University alongside the composition work described under interests.
|
||||
|
||||
## What it proves
|
||||
|
||||
Two things the technical record cannot: formal training in argument and political economy, and formal training in visual judgement. Domain-switching is a habit rather than a claim — the degrees are in different fields from each other and from the career.
|
||||
|
||||
## Why no computer science degree
|
||||
|
||||
Because there is not one, and saying so is cheaper than having it discovered. The self-taught path is documented across this site: the History pages show the eras, the Platform and Edge pages show what came of them.
|
||||
|
||||
## Coverage limit
|
||||
|
||||
Qualification names, awarding institutions and the years the résumé states are published. Grades, student numbers and the master's completion year are not in the sources and are therefore not stated.
|
||||
70
content/about/l3/about-interests.md
Normal file
70
content/about/l3/about-interests.md
Normal file
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: about-interests
|
||||
area: about
|
||||
level: 3
|
||||
title: "Artefact — Interests · one honest line each"
|
||||
claim: "Fourteen pursuits with one true sentence each — and the invented venues · results and trips from the previous version of this site deleted rather than restated."
|
||||
confidence: running
|
||||
period: "1980–"
|
||||
tech: []
|
||||
metrics: [{label: "pursuits listed", value: 15}, {label: "bands played in", value: 2}, {label: "bee hives kept", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [about, interests, artefact]
|
||||
---
|
||||
|
||||
|
||||
|
||||
## Music
|
||||
Blues and jazz piano and blues harp; played in two bands. Studied music composition at university and kept the piano as the thing that resets the week.
|
||||
|
||||
## Fine art
|
||||
A fine arts degree and a continuing practice. Drawing and painting taught patience with iteration, which is most of debugging.
|
||||
|
||||
## Philosophy
|
||||
A degree's worth of it, and it is where the habit of asking what a thing is actually for came from.
|
||||
|
||||
## Skiing
|
||||
Italy, France, Japan and Australia. Six months based in Meribel in the French Alps as a chalet chef, including the Vallée Blanche glacier run.
|
||||
|
||||
## Camping
|
||||
Hiking, camping and canoeing, mostly in Victoria. Most recent: a two-day hike through Wilsons Promontory with a group of teenagers in tow.
|
||||
|
||||
## Canoeing
|
||||
The Yarra, the Otways and the Murray.
|
||||
|
||||
## Horse riding
|
||||
Grew up riding in the Otways; broke horses from track work back to dressage; guided tours in the Victorian high country; competed in dressage for two years.
|
||||
|
||||
## Surfing
|
||||
Grew up between Lorne and the Otways, on waves and horses.
|
||||
|
||||
## Hiking
|
||||
The walking version of the camping answer: long days, small pack, no phone signal.
|
||||
|
||||
## Global politics
|
||||
The master's was in it. Reading it is a habit, not a credential to display.
|
||||
|
||||
## Reading
|
||||
History, philosophy, politics, economics — the same four subjects as the degrees, which is either consistency or a rut.
|
||||
|
||||
## Cooking
|
||||
Worked in many restaurants and as chef in a five-star chalet in Meribel, France.
|
||||
|
||||
## Gardening
|
||||
Permaculture and a wood-chip, low-input approach to self-sufficiency, with sensors and automation doing the boring parts.
|
||||
|
||||
## Food production
|
||||
Bees, salami and cheese. Two hives, and salami made annually from four pigs with Italian friends in Benalla.
|
||||
|
||||
## Italy and travel
|
||||
Five years spent overseas, travelling extensively, with Italy as the standing preference.
|
||||
|
||||
## What was deleted, and why
|
||||
|
||||
The previous site claimed venues, competition results, swell sizes, distances and named restaurants. None of it could be traced. It has been cut rather than corrected: a single true sentence is worth more than ten specific ones that cannot be substantiated, and an interviewer who asks a follow-up question is entitled to an answer that survives it.
|
||||
|
||||
## Coverage limit
|
||||
|
||||
Everything above traces to Sam's own notes. Anything not in those notes is absent, not paraphrased.
|
||||
40
content/about/l3/about-non-it-work.md
Normal file
40
content/about/l3/about-non-it-work.md
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
id: about-non-it-work
|
||||
area: about
|
||||
level: 3
|
||||
title: "Artefact — Non-IT working history"
|
||||
claim: "Six roles outside technology — a chalet kitchen in the French Alps · wine service · guided horse tours · a stable build · government administration · thoroughbred stable work."
|
||||
confidence: historic
|
||||
period: "1990–2010"
|
||||
tech: []
|
||||
metrics: [{label: "non-IT roles worked", value: 6}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [about, history, work, artefact]
|
||||
---
|
||||
|
||||
The roles are final; the presentation lands in Phase 7.
|
||||
|
||||
## The roles
|
||||
|
||||
| Role | Where | What it involved |
|
||||
|---|---|---|
|
||||
| Chalet chef | Merribel, France | Meals for chalet guests, kitchen operations, menu planning, dietary requirements, in a high-pressure alpine season |
|
||||
| Wine waiter | South Australia and Victoria | Premium wine regions, varietals, food pairing |
|
||||
| Horseback tour guide | Victorian high country | Guided tours, rider safety, horse welfare, trail navigation |
|
||||
| Stable construction | Mount Beauty, Victoria | A 24-bay stable build — project management, budgeting, hands-on construction |
|
||||
| Civil servant | Victorian State Government, Melbourne | Administrative and operational support to state-level programs |
|
||||
| Horse trainer and stable hand | Sunbury, Victoria | Care and training of thoroughbreds, exercise, grooming, race-day preparation |
|
||||
|
||||
## What it proves
|
||||
|
||||
That the engineering sits on a wider working life. Kitchen service and production incidents share a shape; guiding strangers on horses in the high country is risk management with consequences; a stable build is a budget and a schedule held by hand. These are the reasons the technical pages read as practical rather than academic.
|
||||
|
||||
## Why this is on a technical site
|
||||
|
||||
Because leaving it out would make the career look narrower than it is, and because the years it occupies are otherwise unexplained.
|
||||
|
||||
## Coverage limit
|
||||
|
||||
Descriptions are limited to what the résumé and the evidence notes corroborate. Roles, employers' towns and the type of work are stated; no employer is named beyond the region, and no claim is made about scale, revenue or headcount that the sources do not carry.
|
||||
49
content/about/l3/about-sources.md
Normal file
49
content/about/l3/about-sources.md
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
id: about-sources
|
||||
area: about
|
||||
level: 3
|
||||
title: "What the assistant reads"
|
||||
claim: "Four sources, one pool, and a published rule for every one of them."
|
||||
confidence: running
|
||||
period: "2026–"
|
||||
tech: [Gitea, Obsidian, Graphify, Qdrant, SQLite]
|
||||
metrics: []
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, sources, corpus, retrieval]
|
||||
---
|
||||
|
||||
# What the assistant reads
|
||||
|
||||
The assistant answers from a pool built out of four sources, not from a single
|
||||
folder of copy.
|
||||
|
||||
| Source | Tier | What it contributes |
|
||||
|---|---|---|
|
||||
| This site's content | 1 | The curated claims: what Sam says is true about the work |
|
||||
| The Gitea estate | 2 | README files, file trees, selected source files, commit messages and language statistics |
|
||||
| The notes vault | 2 | Project and area notes, in Sam's own words |
|
||||
| The knowledge graph | 2 | Nodes and edges over the vault, each joined back to the note it came from |
|
||||
|
||||
Tier 1 wins when the two disagree. A harvested README can be out of date; the
|
||||
curated claim is the one that is maintained.
|
||||
|
||||
Every chunk records the platform it came from, the URL or repository path it can
|
||||
be opened at, the area it feeds, its tier, its honest confidence and when it last
|
||||
changed. A chunk without an origin is refused, not defaulted.
|
||||
|
||||
The rules are not hidden in code. Each platform has a guide that says what it is,
|
||||
where it lives, what is included, what is excluded and whether it is enabled.
|
||||
Those guides are shown at [the sources page](/platforms/) and are the same files
|
||||
the ingestion pipeline reads.
|
||||
|
||||
## What is excluded
|
||||
|
||||
`node_modules`, virtualenvs, git internals, trash, editor state, the SilverBullet
|
||||
auth file, credential-shaped files, binaries and media. Media is recorded as an
|
||||
image reference and never turned into text. Personal and family notes are not
|
||||
read.
|
||||
|
||||
The finished pool is scanned for credential-shaped content before a run can
|
||||
succeed, so the exclusion list is checked rather than trusted.
|
||||
16
content/ai/ai-overview.md
Normal file
16
content/ai/ai-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: ai-overview
|
||||
area: ai
|
||||
level: 1
|
||||
title: "AI & Agentic Systems"
|
||||
claim: "Sam builds and operates sixteen or more AI pipelines: agent harnesses, model routing and cost control, retrieval over a tagged corpus, and a voice front-end that runs a house."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [LangGraph, Prefect, n8n, Qdrant, OmniRoute, Whisper, Piper, MiniMax]
|
||||
metrics: [{label: "pipelines maintained", value: 16}, {label: "model providers behind one gateway", value: 6}, {label: "audio endpoints for spoken output", value: 8}]
|
||||
artefacts: [{type: doc, label: "House AI hub — service topology", url: "content/ai/l3/ai-hub-architecture.md"}, {type: doc, label: "Model routing and cost control", url: "content/ai/l3/ai-omniroute-routing.md"}, {type: doc, label: "Voice pipeline — speech in and speech out", url: "content/ai/l3/ai-voice-pipeline.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, agents, routing, retrieval, voice]
|
||||
---
|
||||
The pipelines run against real obligations — school portals, calendars, email, household audio — and they are operated rather than demonstrated. What actually runs, and what it cost to get there, is on the [capability page](ai-systems.md).
|
||||
56
content/ai/ai-systems.md
Normal file
56
content/ai/ai-systems.md
Normal file
@@ -0,0 +1,56 @@
|
||||
---
|
||||
id: ai-systems
|
||||
area: ai
|
||||
level: 2
|
||||
title: "Agent systems that are operated rather than demonstrated"
|
||||
claim: "Maintains sixteen or more production AI pipelines — agent harnesses · prompt-analysis routing across six providers · retrieval filtered by metadata before it searches · and a guarded voice front-end."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [LangGraph, Prefect, n8n, Paperclip, Qdrant, OmniRoute, Whisper, Piper, MiniMax, TUIOS]
|
||||
metrics: [{label: "pipelines maintained", value: 16}, {label: "model providers behind one gateway", value: 6}, {label: "audio endpoints for spoken output", value: 8}, {label: "hosts under standardised agent operations", value: 4}]
|
||||
artefacts: [{type: doc, label: "House AI hub — service topology", url: "content/ai/l3/ai-hub-architecture.md"}, {type: doc, label: "Model routing and cost control", url: "content/ai/l3/ai-omniroute-routing.md"}, {type: doc, label: "Paperclip and n8n pipelines in production", url: "content/ai/l3/ai-paperclip-pipelines.md"}, {type: doc, label: "Retrieval corpus and query graph", url: "content/ai/l3/ai-rag-corpus.md"}, {type: doc, label: "Voice pipeline — speech in and speech out", url: "content/ai/l3/ai-voice-pipeline.md"}, {type: doc, label: "Guardrails — hooks in the model path", url: "content/ai/l3/ai-guardrails.md"}, {type: doc, label: "Memory in two layers", url: "content/ai/l3/ai-memory-layers.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, agents, routing, retrieval, voice, guardrails]
|
||||
---
|
||||
|
||||
**Capability statement.** This is a working estate, not a prototype: more than sixteen pipelines in production, six providers behind a single gateway, retrieval that filters on metadata before it searches, and a voice front-end used daily by people who did not build it.
|
||||
|
||||
## What is actually run
|
||||
|
||||
**Routing.** Every model call goes through one gateway — OmniRoute. Six providers sit behind it: DeepSeek, MiniMax, OpenRouter, OpenCode-Go, Gemini and OpenAI. Requests are classified before they are sent, so the cheapest model that can do the job gets the job. No project in the estate calls a provider directly; that is a rule, not a preference.
|
||||
|
||||
**Pipelines.** Ten or more are maintained on n8n, Prefect and LangGraph — email scanners, school-portal scanners, calendar scanners, project agents — plus six Paperclip pipelines that run work through a bench of narrow specialist agents behind human approval gates. The estate is registered in a dashboard built to keep track of it.
|
||||
|
||||
**Harnesses.** JEV is a custom harness with its own routing, tool set and memory rather than a wrapper around a vendor SDK. Work is executed as sequences of narrow agents instead of one large prompt; capability is loaded per task through a skill-selection step, so the agent gets what this job needs and not an enormous system prompt.
|
||||
|
||||
**Memory.** Two deliberate layers. Engram keeps what should persist — curated summaries, decisions, prompts and session handoffs in local SQLite, integrated with JEV for fast compaction. Headroom keeps what should not persist out of the window, stripping and compressing prompt context, tool output and logs before they reach the model. Storage and token control are separate problems and are solved separately.
|
||||
|
||||
**Retrieval.** ChromaDB first, then replaced with Qdrant for payload-filtered retrieval and a smaller footprint. LangGraph query graphs on top; Prefect and n8n for ingestion. The corpus is metadata-tagged at ingest, which means retrieval filters before it searches — the difference between a device answer and a work-history answer is a filter, not a better prompt.
|
||||
|
||||
**Voice.** Local Whisper for speech to text with a fallback, Piper for text to speech, MiniMax for a cloned voice with emotion and non-verbal cues. A custom wake word on an ESP32 with LED confirmation. Voice is a system front-end: it controls lighting, music, calendars, shopping, email, SMS and alerts, and it answers through eight Snapcast endpoints with music ducked underneath.
|
||||
|
||||
**Guardrails.** Custom hooks live in the model path and block injection and constrain tool use, built because the house voice channel is shared and unauthenticated and there was no vendor control to borrow. Consequential actions sit behind approval gates, and no model sits in the path of a fast or dangerous command. The public site applies the same reasoning from the other end: the assistant it exposes has no tools at all, so the attack surface is a conversation rather than a shell.
|
||||
|
||||
**Agent operations.** Agent sessions are standardised on TUIOS across four hosts — sessions survive a disconnect, agents report their own state, and approvals land in one inbox. TUIOS is a third-party tool: the contribution is operations and packaging, not authorship. It is pinned at 0.10.1 in a hand-written Nix derivation because nixpkgs lagged at 0.7.0, and its daemon is declared as a systemd user service so the fleet is reproducible rather than hand-installed.
|
||||
|
||||
## Evidence
|
||||
|
||||
| Claim | Evidence | Artefact |
|
||||
|---|---|---|
|
||||
| Sixteen or more pipelines in production | Ten or more on n8n, Prefect and LangGraph plus six on Paperclip | [Paperclip and n8n pipelines](l3/ai-paperclip-pipelines.md) |
|
||||
| Six providers behind one gateway | DeepSeek, MiniMax, OpenRouter, OpenCode-Go, Gemini, OpenAI | [Model routing](l3/ai-omniroute-routing.md) |
|
||||
| Retrieval filters before it searches | Corpus tagged at ingest; Qdrant payload filters; LangGraph query nodes | [Corpus and query graph](l3/ai-rag-corpus.md) |
|
||||
| Spoken output reaches eight endpoints | Eight Snapcast clients with music ducking | [Voice pipeline](l3/ai-voice-pipeline.md) |
|
||||
| Guardrails are code in the path | Injection-blocking hooks and constrained tool use | [Guardrails](l3/ai-guardrails.md) |
|
||||
| Memory is two layers by design | Engram for persistence, Headroom for context control | [Memory layers](l3/ai-memory-layers.md) |
|
||||
| Agent operations are standardised | TUIOS across four hosts with one approval inbox | [House AI hub](l3/ai-hub-architecture.md) |
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **Local inference was measured, then demoted.** The estate started local on Ollama. Hardware constraints made local inference both slower and more expensive than partitioned remote calls, so heavy flows moved remote while sensitive and low-latency work stayed local. That is a documented comparison, not a taste in hardware.
|
||||
- **The retired stack is in the record.** Airflow DAGs were abandoned for RAM, ChromaDB was replaced by Qdrant, and a visual-builder layer was dropped. Each replacement is stated rather than hidden, because the reasoning is the transferable part.
|
||||
- **Not everything is automated.** Parts of the house are automated end to end and parts still need a person to press the button. The estate is described as it is run, not as it was imagined.
|
||||
- **Cost is bounded by routing, not by hope.** Caps and a refusal path exist so a runaway session cannot become a bill; the routing layer keeps the median request on a cheap model.
|
||||
|
||||
The period starts at the homelab year in the evidence notes: the AI estate and the homelab have the same origin.
|
||||
29
content/ai/l3/ai-guardrails.md
Normal file
29
content/ai/l3/ai-guardrails.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-guardrails
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Guardrails · hooks in the model path"
|
||||
claim: "Injection blocking and constrained tool use are code in the model path — built because the house voice channel is shared and unauthenticated."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [OmniRoute, Python]
|
||||
metrics: [{label: "tools exposed by the public assistant", value: 0}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, guardrails, security, artefact]
|
||||
---
|
||||
|
||||
A redacted injection-test result lands in Phase 6, when the guardrail library is wired into this site's own assistant.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The hooks that run before a request reaches a provider and before a response reaches an actuator: injection patterns are blocked, tool use is constrained to a declared set, and consequential actions stop at an approval gate. Behind them, one rule that outranks every prompt — no model sits in the path of a fast or dangerous command.
|
||||
|
||||
## What it proves
|
||||
|
||||
Safety enforced by mechanism. A prompt instruction is a suggestion; a hook is a control.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
The test result in Phase 6, using the same library the house runs. The policy that the public assistant has no tools at all is a Phase 5 and 6 decision and is not negotiable at runtime.
|
||||
29
content/ai/l3/ai-hub-architecture.md
Normal file
29
content/ai/l3/ai-hub-architecture.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-hub-architecture
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — House AI hub service topology"
|
||||
claim: "One hub fronts the AI estate — intake · routing · pipelines · retrieval · voice output · and a single set of network tools namespaced per household member."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [n8n, Prefect, LangGraph, OmniRoute, Qdrant, MQTT, Snapcast]
|
||||
metrics: [{label: "provider gateway entry points", value: 1}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, architecture, artefact]
|
||||
---
|
||||
|
||||
The diagram and the generated topology dump land in Phase 7 (polish). This stub exists so the level 2 page has no dangling reference.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A one-page diagram of the hub: request intake (voice · chat · scheduled job), the gateway hop, the pipeline runners, the retrieval path, and the output paths (house speakers, phone alerts, dashboards). Every box is a service that runs; nothing on the diagram is aspirational.
|
||||
|
||||
## What it proves
|
||||
|
||||
That the estate is a designed system with a single gateway and a single set of namespaced tools — not a collection of scripts that happen to share a machine.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. The source facts are in the evidence notes (sections 2 and 4). Capture must be from the live estate, so no diagram is drawn from memory in Phase 1.
|
||||
29
content/ai/l3/ai-memory-layers.md
Normal file
29
content/ai/l3/ai-memory-layers.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-memory-layers
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Memory in two layers · Engram and Headroom"
|
||||
claim: "Memory is split by purpose — Engram stores what should persist and Headroom keeps what should not persist out of the context window."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Engram, Headroom, SQLite, JEV]
|
||||
metrics: [{label: "memory layers", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, memory, context, artefact]
|
||||
---
|
||||
|
||||
A redacted session handoff and a before-and-after context measurement land in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
Engram is local-first storage for curated summaries, decisions, prompts and session handoffs in SQLite, so a conversation can be resumed in any project; it is integrated with JEV for fast compaction. Headroom is token control: it strips, cleanses and analyses code and logs before they reach the model, compressing rather than truncating.
|
||||
|
||||
## What it proves
|
||||
|
||||
Two distinct problems recognised as two — persistence and window economy — instead of one tool asked to do both.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Both tools are part of Sam's own workflow; they are deliberately not part of the public assistant, which is stateless by decision (see `reference/decisions.md` D11).
|
||||
29
content/ai/l3/ai-omniroute-routing.md
Normal file
29
content/ai/l3/ai-omniroute-routing.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-omniroute-routing
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Model routing and cost control"
|
||||
claim: "Every model call is classified first and sent to the cheapest capable provider — six providers behind one gateway with no direct SDK calls anywhere in the estate."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [OmniRoute, JEV, DeepSeek, MiniMax, OpenRouter, Gemini, OpenAI]
|
||||
metrics: [{label: "providers in rotation", value: 6}, {label: "gateways", value: 1}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, routing, cost, artefact]
|
||||
---
|
||||
|
||||
The routing decision path and a redacted cost table land in Phase 7. No keys or account identifiers are ever published.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The decision path: request arrives, a prompt-analysis step classifies it, the gateway selects a provider and model, and the response returns with its cost recorded. Plus the comparison that demoted local inference for heavy flows — local Ollama measured slower and more expensive under the available hardware than partitioned remote calls.
|
||||
|
||||
## What it proves
|
||||
|
||||
Cost control implemented as routing and measurement rather than as a monthly promise, and a single chokepoint where provider access can be audited.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. The evidence for the provider list and the local-versus-remote decision is in the evidence notes, section 2 and section 4.
|
||||
29
content/ai/l3/ai-paperclip-pipelines.md
Normal file
29
content/ai/l3/ai-paperclip-pipelines.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-paperclip-pipelines
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Paperclip and n8n pipelines in production"
|
||||
claim: "Sixteen or more pipelines run real work — scanners · schedulers · project agents — with specialist agents dispatched by task and human approval gates on anything consequential."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Paperclip, n8n, Prefect, LangGraph]
|
||||
metrics: [{label: "pipelines on n8n and Prefect and LangGraph", value: 10}, {label: "Paperclip pipelines", value: 6}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, pipelines, agents, artefact]
|
||||
---
|
||||
|
||||
A redacted run log of one pipeline lands in Phase 7, alongside the pipeline inventory.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
One pipeline shown end to end: trigger, steps, the agent or model each step uses, the approval gate, and the run log with identifiers removed. Alongside it, the inventory of what the sixteen or more pipelines do — email scanners, school-portal scanners, calendar scanners, project oversight, and the household automations.
|
||||
|
||||
## What it proves
|
||||
|
||||
Volume with operations behind it: these are scheduled, logged and monitored, not run once for a demonstration.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Counts come from the evidence notes, section 3.
|
||||
35
content/ai/l3/ai-rag-corpus.md
Normal file
35
content/ai/l3/ai-rag-corpus.md
Normal file
@@ -0,0 +1,35 @@
|
||||
---
|
||||
id: ai-rag-corpus
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Retrieval corpus and query graph"
|
||||
claim: "Retrieval is filtered by metadata before it searches — a tagged corpus · a four-node LangGraph query graph · and a deliberate replacement of ChromaDB with Qdrant."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Qdrant, LangGraph, Prefect, n8n, ChromaDB]
|
||||
metrics: [{label: "retrieval stores used over time", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, retrieval, rag, artefact]
|
||||
---
|
||||
|
||||
Ingest and index are live; the diagram below shows both sides, and the query path is labelled as the design it is until Phase 5 ships.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The ingest and query path: documents are chunked and tagged with metadata at ingest, stored in Qdrant, and filtered on that metadata before a similarity search runs. The LangGraph graph has four nodes; the fourth decides whether the answer can be given from what was retrieved.
|
||||
|
||||
## What it proves
|
||||
|
||||
Retrieval designed around payload filtering and a hard stop, plus the reasoning for moving off ChromaDB — heavier footprint and weaker filtering — recorded as a decision rather than a migration.
|
||||
|
||||
## The pipeline
|
||||
|
||||

|
||||
|
||||
The separating line is the design decision: metadata is written at ingest, so the query can filter by area, level and source before it pays for a similarity search. The fourth node is the hard stop — an answer that cannot be supported by what was retrieved is refused rather than improvised.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Ingest is delivered and measured (this site's own corpus, Phase 4). The query path is delivered in Phase 5; the streaming console is the visitor-facing half of it.
|
||||
29
content/ai/l3/ai-voice-pipeline.md
Normal file
29
content/ai/l3/ai-voice-pipeline.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: ai-voice-pipeline
|
||||
area: ai
|
||||
level: 3
|
||||
title: "Artefact — Voice pipeline · speech in and speech out"
|
||||
claim: "Speech in on a custom wake word · local transcription · a cloned voice out · and music ducked under every announcement across eight endpoints."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Whisper, Piper, MiniMax, ESP32, Snapcast, Mopidy, Home Assistant]
|
||||
metrics: [{label: "audio endpoints", value: 8}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [ai, voice, audio, artefact]
|
||||
---
|
||||
|
||||
A short recording of music ducking under a spoken announcement lands in the optional voice phase. Text describes the chain until then.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The chain: an ESP32 device with a microphone detects the wake word and confirms with an LED, audio is transcribed locally with Whisper, the request goes through the gateway, and the reply is spoken through Piper or the MiniMax cloned voice over all eight Snapcast endpoints, with music lowered for the announcement and restored afterwards.
|
||||
|
||||
## What it proves
|
||||
|
||||
Voice treated as a system front-end with latency and reliability requirements — not a party trick bolted onto a chatbot.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Recording in the optional voice phase (Phase 7). The endpoint count and the ducking behaviour are from the evidence notes, sections 1 and 2.
|
||||
0
content/assets/.gitkeep
Normal file
0
content/assets/.gitkeep
Normal file
36
content/assets/code/01-actionscript-class.as
Normal file
36
content/assets/code/01-actionscript-class.as
Normal file
@@ -0,0 +1,36 @@
|
||||
package thebeer {
|
||||
import utils.Bg;
|
||||
|
||||
import com.greensock.TweenMax;
|
||||
|
||||
import flash.display.MovieClip;
|
||||
import flash.events.Event;
|
||||
|
||||
/**
|
||||
* @author samrolfe
|
||||
*/
|
||||
public class Siren extends MovieClip {
|
||||
private var targ : Bg;
|
||||
private var delays : Number;
|
||||
private var wide: Number;
|
||||
private var high: Number;
|
||||
|
||||
public function Siren(col:Number,t:Bg,d:Number) {
|
||||
delays=d;
|
||||
targ=t;
|
||||
x=targ.x;
|
||||
y=targ.y;
|
||||
wide=this.width;
|
||||
high=this.high;
|
||||
TweenMax.to(this,0,{autoAlpha:0,colorMatrixFilter:{colorize:col}});
|
||||
}
|
||||
|
||||
private function setPos(e:Event) : void {
|
||||
x=parent[targ].x;
|
||||
y=parent[targ].y;
|
||||
}
|
||||
|
||||
public function flash() : void {
|
||||
TweenMax.to(this,0,{autoAlpha:1,width:wide,height:high});
|
||||
TweenMax.to(this,.1,{width:0,height:0,repeat:5,repeatDelay:delays,yoyo:true,onComplete:finish});
|
||||
}
|
||||
53
content/assets/code/02-js-easing-core.js
Normal file
53
content/assets/code/02-js-easing-core.js
Normal file
@@ -0,0 +1,53 @@
|
||||
// Excerpt from js/utils/text_effects.js (2,156 lines) in the WebsiteBuilder
|
||||
// source. Two regions are reproduced verbatim: the timeline loop that derives
|
||||
// each letter's delay from an easing function (lines 1111-1147), and the
|
||||
// hand-written easing functions themselves (lines 1340-1349).
|
||||
$(mySplitTextTarg).each(function(i){
|
||||
|
||||
tweenData.letter=$(this);
|
||||
tweenData.text_static.letter=$(this);
|
||||
tweenData.text_text_to.letter=$(this);
|
||||
tweenData.text_text_from.letter=$(this);
|
||||
|
||||
if(text_effect_type_animation=="standard"){
|
||||
if(tweenData.ignoreItems=="on" && Math.random()<tweenData.ignore_amount)return;
|
||||
|
||||
if(!keepHide(tweenData,i,$(this).text()))return;
|
||||
|
||||
var transforms=getTransforms(tweenData);
|
||||
|
||||
|
||||
//currenttime,start,change,duration
|
||||
if(tweenData.staggerTimeRnd=="random")stagger=min_max_random(0,tweenData.staggerTime);
|
||||
// stagger=positionsCubic(tweenData.alternatecount, 0, tweenData.duration, tweenData.total)
|
||||
var ease=tweenData.easeFunction;
|
||||
// var ease="easeInCubic";
|
||||
stime=tweenData.duration;
|
||||
if(tweenData.direc=="in"){
|
||||
stagger=jQuery.easing[ease](null,tweenData.alternatecount, 0, tweenData.duration, tweenData.total)+tweenData.startWait
|
||||
transforms.delay=stagger;
|
||||
TweenMax.from($(this), tweenData.duration, transforms);
|
||||
tt=tweenData.duration+stagger;
|
||||
}
|
||||
if(tweenData.direc=="out"){
|
||||
stagger=jQuery.easing[ease](null,tweenData.alternatecount, 0, tweenData.duration, tweenData.total)
|
||||
transforms.delay=stagger;
|
||||
transforms.onComplete=tweenRemove($(this),stagger+stime+tweenData.removeWait)
|
||||
TweenMax.to($(this), tweenData.duration, transforms);
|
||||
tt=tweenData.duration+stagger+tweenData.removeWait+1;
|
||||
}
|
||||
|
||||
setCssTweens(stime,text_effect_type_animation,tweenData,tweenData.text_static,tweenData.text_text_to,tweenData.text_text_from,$(this),stagger);
|
||||
|
||||
|
||||
// --- easing functions, lines 1340-1349 ---
|
||||
//t,b,c,d
|
||||
//currenttime,start,change,duration
|
||||
//t and do can be time or frames
|
||||
function positionsCubic(t, b, c, d){
|
||||
t /= d;
|
||||
return c*t*t*t + b;
|
||||
}
|
||||
function positionSine(t, b, c, d) {
|
||||
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
|
||||
}
|
||||
54
content/assets/code/03-php-encode-op.php
Normal file
54
content/assets/code/03-php-encode-op.php
Normal file
@@ -0,0 +1,54 @@
|
||||
// Excerpt from php/setMediaEncoder.php (3,402 lines) in the WebsiteBuilder
|
||||
// source: the create_gif encode operation, lines 614-665, reproduced verbatim.
|
||||
if($output_options->encode_media=="create_gif") {
|
||||
$output_gif= ".." . $settings->directory . $output_file .$output_options->format .".gif";
|
||||
|
||||
$docmime= checkFinfo($input_file);
|
||||
$doctype=explode("/",$docmime)[0];
|
||||
if ($doctype!="video"){
|
||||
$output_options->error_message= "We only create gifs from a video - it must be the first video in the media list.";
|
||||
$returnFiles = returnFiles ( $output_options, $output_file .$output_options->format . '.mp4', "video/mp4", '', null );
|
||||
return $returnFiles;
|
||||
}
|
||||
|
||||
$cmd = "ffmpeg -i ".$input_file." -y -vf fps=".$settings->settings->movie_frame_rate.",scale=".$settings->settings->video_width.":".$settings->settings->video_height.",palettegen palette.png 2>&1";
|
||||
// $cmd = "ffmpeg -i ".$input_file." -y -vf fps=10,scale=".$settings->settings->video_width.":".$settings->settings->video_height.",palettegen palette.png 2>&1";
|
||||
$result=shell_exec($cmd);
|
||||
$first_media=json_decode($settings->add_media)[0];
|
||||
|
||||
// $cmd = "ffmpeg -ss ".$first_media->add_media_start." -t ".$first_media->add_media_duration." -i ".$input_file." -i palette.png -y -filter_complex \"fps=10[x];[x][1:v]paletteuse\" ".$output_gif." ";
|
||||
$cmd = "ffmpeg -ss ".$first_media->add_media_start." -t ".$first_media->add_media_duration." -i ".$input_file." -i palette.png -y -filter_complex \"fps=".$settings->settings->movie_frame_rate.",scale=".$settings->settings->video_width.":".$settings->settings->video_height.":flags=lanczos[x];[x][1:v]paletteuse\" ".$output_gif." ";
|
||||
ffmpegMsg("ffmpeg","Creating Gif ", 0,$cmd);
|
||||
|
||||
// $cmd_2='mogrify -delay '.$_POST['compress_gif_delay'].' -alpha remove '.$matte.' -depth '.$_POST['compress_gif_depth'].' '.$deconstruct.' '.$dither.' -colors '.$_POST['compress_gif_colors'].' -fuzz '.$_POST['compress_gif_fuzz'].'% '.$loop.' '.$optimize.' '.$path.' 2>&1';
|
||||
|
||||
$_POST['loop_gif']="true";
|
||||
$_POST['compress_gif_matte']="false";
|
||||
$_POST['compress_gif_deconstruct']="false";
|
||||
$_POST['compress_gif_dither']="true";
|
||||
$_POST['compress_gif_optimize']="true";
|
||||
|
||||
$loop=($_POST['loop_gif']=="false")?"-loop 1":"";
|
||||
$matte=($_POST['compress_gif_matte']=="true")?"-matte":"";
|
||||
$deconstruct=($_POST['compress_gif_deconstruct']=="true")?"-deconstruct":"";
|
||||
$dither=($_POST['compress_gif_dither']=="true")?"-dither none ":"";
|
||||
$optimize=($_POST['compress_gif_optimize']=="true")?"-layers optimize ":"";
|
||||
// $optimize=($_POST['compress_gif_optimize']=="true")?" -coalesce -layers RemoveDups ":"";
|
||||
|
||||
$_POST['compress_gif_depth']=16;
|
||||
$_POST['compress_gif_delay']=0;
|
||||
$_POST['compress_gif_fuzz']="4";
|
||||
$_POST['compress_gif_colors']="16";
|
||||
|
||||
$cmd_2='mogrify -delay '.$_POST['compress_gif_delay'].' -alpha remove '.$matte.' -depth '.$_POST['compress_gif_depth'].' '.$deconstruct.' '.$dither.' -colors '.$_POST['compress_gif_colors'].' -fuzz '.$_POST['compress_gif_fuzz'].'% '.$loop.' '.$optimize.' '.$output_gif.' 2>&1';
|
||||
$result=shell_exec($cmd_2);
|
||||
|
||||
|
||||
$output_options->edited_url= $output_options->options->directory.$output_file .$output_options->format . '.gif';
|
||||
$output_options->edited_name= $output_file .$output_options->format . '.gif';
|
||||
$output_options->edited_type = "image/gif";
|
||||
$output_options->edited_filetype = explode ( "/", "image/gif");
|
||||
insert_document($output_options->edited_url,"image/gif",$output_options->edited_name,"image","edited");
|
||||
$returnFiles = returnFiles ( $output_options, $output_file .$output_options->format . '.mp4', "video/mp4", '', null );
|
||||
return $returnFiles;
|
||||
}
|
||||
38
content/assets/code/04-typescript-service-layer.ts
Normal file
38
content/assets/code/04-typescript-service-layer.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// In file: src/server/api/routers/commandTemplate.ts
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
|
||||
|
||||
export const commandTemplateRouter = createTRPCRouter({
|
||||
// Procedure to get all templates
|
||||
getAll: publicProcedure.query(({ ctx }) => {
|
||||
return ctx.db.commandTemplate.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}),
|
||||
|
||||
// Procedure to create a new template
|
||||
create: publicProcedure
|
||||
.input(z.object({
|
||||
name: z.string().min(1),
|
||||
type: z.string().min(1),
|
||||
payload: z.record(z.unknown()), // Accepts any JSON object
|
||||
}))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.db.commandTemplate.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
type: input.type,
|
||||
payload: input.payload,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
// Procedure to delete a template
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.number() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.db.commandTemplate.delete({
|
||||
where: { id: input.id },
|
||||
});
|
||||
}),
|
||||
});
|
||||
31
content/assets/code/05-embedded-cpp-interface.hpp
Normal file
31
content/assets/code/05-embedded-cpp-interface.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Two files from firmware/src/hw, reproduced verbatim: WiFiMgr.hpp (the
|
||||
// interface) and WiFiMgr.cpp (the implementation). The boundary is marked.
|
||||
// WiFiMgr.hpp — the interface the rest of the firmware depends on
|
||||
// WiFiMgr.hpp
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace WiFiMgr {
|
||||
bool connectHome(const char* ssid, const char* psk, uint32_t timeoutMs = 15000);
|
||||
void disconnect();
|
||||
}
|
||||
// WiFiMgr.cpp — the only translation unit that includes <WiFi.h>
|
||||
// WiFiMgr.cpp
|
||||
#include "WiFiMgr.hpp"
|
||||
#include <WiFi.h> // ESP32 WiFi
|
||||
|
||||
namespace WiFiMgr {
|
||||
bool connectHome(const char* ssid, const char* psk, uint32_t timeoutMs) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, psk);
|
||||
uint32_t t0 = millis();
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
|
||||
delay(200);
|
||||
}
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
void disconnect() {
|
||||
WiFi.disconnect(true, true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
}
|
||||
}
|
||||
33
content/assets/code/06-nix-derivation.nix
Normal file
33
content/assets/code/06-nix-derivation.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{ lib, stdenv, fetchurl }:
|
||||
|
||||
# tuios — terminal window manager (https://github.com/Gaurav-Gosain/tuios)
|
||||
# OVERRIDE: nixpkgs ships tuios 0.7.0; the multi-machine `hosts` UX needs 0.9+.
|
||||
# Official pure-Go prebuilt release binary (no runtime deps, runs bare on NixOS).
|
||||
# To return to nixpkgs packaging: use plain `tuios` in home.packages again and
|
||||
# delete this file (do this once nixpkgs ships tuios >= 0.10).
|
||||
stdenv.mkDerivation {
|
||||
pname = "tuios";
|
||||
version = "0.10.1";
|
||||
|
||||
src = fetchurl {
|
||||
url = "https://github.com/Gaurav-Gosain/tuios/releases/download/v0.10.1/tuios_0.10.1_Linux_x86_64.tar.gz";
|
||||
sha256 = "85a4bf339a9a6793e0b3d15abdccd9a96ca8d2ff76072007ec99be31f1a16a5f";
|
||||
};
|
||||
|
||||
# Tarball has no wrapping directory (tuios / LICENSE / README.md at root)
|
||||
sourceRoot = ".";
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
install -Dm755 tuios $out/bin/tuios
|
||||
runHook postInstall
|
||||
'';
|
||||
|
||||
meta = with lib; {
|
||||
description = "Terminal window manager — tiling panes, sessions that survive restarts (multi-machine daemon)";
|
||||
homepage = "https://github.com/Gaurav-Gosain/tuios";
|
||||
license = licenses.mit;
|
||||
mainProgram = "tuios";
|
||||
platforms = [ "x86_64-linux" ];
|
||||
};
|
||||
}
|
||||
109
content/assets/diagrams/homelab-topology.svg
Normal file
109
content/assets/diagrams/homelab-topology.svg
Normal file
@@ -0,0 +1,109 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="1110" height="600" viewBox="0 0 1110 600" role="img" aria-labelledby="topoTitle topoDesc" font-family="ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif">
|
||||
<title id="topoTitle">Homelab topology: build host, runtime host and public edge</title>
|
||||
<desc id="topoDesc">Three hosts. The build host produces a release; the runtime host runs the site, the vector store, a workflow worker and the model gateway, all bound to loopback; the public edge terminates TLS and is the only host reachable from the internet. All three join one private mesh, and backups run in three tiers.</desc>
|
||||
|
||||
<style>
|
||||
/* On paper the diagram is ink on white, not a dark panel. Presentation
|
||||
attributes are the weakest source, so these rules win without editing
|
||||
the drawing. */
|
||||
@media print {
|
||||
rect { fill: #fff; }
|
||||
text { fill: #111; }
|
||||
text[fill="#6ee7c6"] { fill: #0b5c48; }
|
||||
text[fill="#8b98a8"] { fill: #444; }
|
||||
line, path { stroke: #555; }
|
||||
marker path { fill: #555; }
|
||||
}
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0 0 10 5 0 10z" fill="#6ee7c6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
|
||||
<g fill="#12161b" stroke="#33404d" stroke-width="1.5">
|
||||
<rect x="30" y="104" width="310" height="268" rx="10"/>
|
||||
<rect x="400" y="104" width="310" height="268" rx="10"/>
|
||||
<rect x="770" y="104" width="310" height="268" rx="10"/>
|
||||
</g>
|
||||
|
||||
<!-- internet -->
|
||||
<g>
|
||||
<rect x="810" y="24" width="230" height="46" rx="23" fill="#171d24" stroke="#33404d" stroke-width="1.5"/>
|
||||
<text x="925" y="52" text-anchor="middle" font-size="13" fill="#b3bcc8">public internet · https :443</text>
|
||||
<line x1="925" y1="70" x2="925" y2="100" stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
</g>
|
||||
|
||||
<!-- host headers -->
|
||||
<g fill="#e6edf3" font-size="15" font-weight="600">
|
||||
<text x="54" y="136">Build and development</text>
|
||||
<text x="424" y="136">Runtime host</text>
|
||||
<text x="794" y="136">Public edge</text>
|
||||
</g>
|
||||
<g fill="#8b98a8" font-size="12" font-family="ui-monospace, SFMono-Regular, Menlo, monospace">
|
||||
<text x="54" y="156">.27 · 4-screen desktop</text>
|
||||
<text x="424" y="156">.13 · 15 GiB · memory-bound</text>
|
||||
<text x="794" y="156">.35 · Ubuntu · 2 vCPU</text>
|
||||
</g>
|
||||
|
||||
<!-- build host items -->
|
||||
<g fill="#b3bcc8" font-size="12.5">
|
||||
<text x="54" y="192">· repository working copy</text>
|
||||
<text x="54" y="219">· agent fleet, panes and approvals</text>
|
||||
<text x="54" y="246">· content, diagrams and exhibits</text>
|
||||
<text x="54" y="273">· release bundles and hashes</text>
|
||||
</g>
|
||||
|
||||
<!-- runtime: loopback trust boundary -->
|
||||
<rect x="416" y="172" width="278" height="150" rx="8" fill="none" stroke="#6ee7c6" stroke-width="1.2" stroke-dasharray="5 4"/>
|
||||
<text x="430" y="192" font-size="11" fill="#6ee7c6" font-family="ui-monospace, SFMono-Regular, Menlo, monospace">bound to 127.0.0.1</text>
|
||||
<g fill="#b3bcc8" font-size="12.5">
|
||||
<text x="430" y="216">· site server :8096</text>
|
||||
<text x="430" y="241">· Qdrant :6333 / :6334</text>
|
||||
<text x="430" y="266">· Prefect worker</text>
|
||||
<text x="430" y="291">· model gateway (shared)</text>
|
||||
</g>
|
||||
<text x="424" y="345" font-size="11.5" fill="#8b98a8">Caps equal swap; nothing listens publicly.</text>
|
||||
|
||||
<!-- edge host items -->
|
||||
<g fill="#b3bcc8" font-size="12.5">
|
||||
<text x="794" y="192">· Caddy terminates TLS</text>
|
||||
<text x="794" y="219">· proxies to the runtime host</text>
|
||||
<text x="794" y="246">· Gitea · Immich · Nextcloud</text>
|
||||
<text x="794" y="273">· one public listener, many sites</text>
|
||||
</g>
|
||||
|
||||
<!-- hand-off arrows -->
|
||||
<line x1="340" y1="230" x2="396" y2="230" stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
<text x="368" y="218" text-anchor="middle" font-size="10.5" fill="#8b98a8">deploy</text>
|
||||
<line x1="710" y1="230" x2="766" y2="230" stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
<text x="738" y="218" text-anchor="middle" font-size="10.5" fill="#8b98a8">proxy</text>
|
||||
|
||||
<!-- mesh band -->
|
||||
<rect x="30" y="410" width="1050" height="56" rx="10" fill="#12161b" stroke="#33404d" stroke-width="1.2" stroke-dasharray="6 5"/>
|
||||
<text x="555" y="436" text-anchor="middle" font-size="13" fill="#b3bcc8">private mesh · SSH on every host · remote access without an exposed port</text>
|
||||
<text x="555" y="454" text-anchor="middle" font-size="11" fill="#8b98a8">no service is reachable because it happened to bind to every interface</text>
|
||||
<g stroke="#33404d" stroke-width="1" stroke-dasharray="3 4">
|
||||
<line x1="185" y1="372" x2="185" y2="410"/>
|
||||
<line x1="555" y1="372" x2="555" y2="410"/>
|
||||
<line x1="925" y1="372" x2="925" y2="410"/>
|
||||
</g>
|
||||
|
||||
<!-- backup tiers -->
|
||||
<text x="30" y="506" font-size="13" font-weight="600" fill="#e6edf3">Three backup tiers — each covers a different failure</text>
|
||||
<g fill="#12161b" stroke="#33404d" stroke-width="1.2">
|
||||
<rect x="30" y="522" width="330" height="52" rx="8"/>
|
||||
<rect x="390" y="522" width="330" height="52" rx="8"/>
|
||||
<rect x="750" y="522" width="330" height="52" rx="8"/>
|
||||
</g>
|
||||
<g fill="#b3bcc8" font-size="12.5">
|
||||
<text x="50" y="545">local snapshots, on the machine</text>
|
||||
<text x="410" y="545">a copy on another machine</text>
|
||||
<text x="770" y="545">encrypted object storage, offsite</text>
|
||||
</g>
|
||||
<g fill="#8b98a8" font-size="11">
|
||||
<text x="50" y="562">restore tested, runbook beside it</text>
|
||||
<text x="410" y="562">over the private mesh</text>
|
||||
<text x="770" y="562">retention stated in the job</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.6 KiB |
119
content/assets/diagrams/rag-pipeline.svg
Normal file
119
content/assets/diagrams/rag-pipeline.svg
Normal file
@@ -0,0 +1,119 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="1120" height="580" viewBox="0 0 1120 580" role="img" aria-labelledby="ragTitle ragDesc" font-family="ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif">
|
||||
<title id="ragTitle">Retrieval pipeline: ingest on top, four-node query path below</title>
|
||||
<desc id="ragDesc">Ingest reads content and estate notes, excludes secrets, chunks and tags each block, embeds through the model gateway and upserts into two Qdrant collections on loopback. The query path classifies the question, routes it to an area, retrieves with a payload filter, composes an answer from the retrieved context only, and verifies that every citation resolves to a page.</desc>
|
||||
|
||||
<style>
|
||||
/* On paper the diagram is ink on white, not a dark panel. Presentation
|
||||
attributes are the weakest source, so these rules win without editing
|
||||
the drawing. */
|
||||
@media print {
|
||||
rect { fill: #fff; }
|
||||
text { fill: #111; }
|
||||
text[fill="#6ee7c6"] { fill: #0b5c48; }
|
||||
text[fill="#8b98a8"] { fill: #444; }
|
||||
line, path { stroke: #555; }
|
||||
marker path { fill: #555; }
|
||||
}
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0 0 10 5 0 10z" fill="#6ee7c6"/>
|
||||
</marker>
|
||||
<marker id="arrowDim" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0 0 10 5 0 10z" fill="#8b98a8"/>
|
||||
</marker>
|
||||
</defs>
|
||||
|
||||
<!-- ingest lane -->
|
||||
<text x="30" y="42" font-size="14" font-weight="600" fill="#e6edf3">Ingest — one source of truth, one index</text>
|
||||
|
||||
<g fill="#12161b" stroke="#33404d" stroke-width="1.4">
|
||||
<rect x="30" y="60" width="244" height="92" rx="9"/>
|
||||
<rect x="302" y="60" width="244" height="92" rx="9"/>
|
||||
<rect x="574" y="60" width="244" height="92" rx="9"/>
|
||||
<rect x="846" y="60" width="244" height="92" rx="9"/>
|
||||
</g>
|
||||
<g fill="#e6edf3" font-size="13.5" font-weight="600">
|
||||
<text x="48" y="86">Sources</text>
|
||||
<text x="320" y="86">Normalise</text>
|
||||
<text x="592" y="86">Chunk and tag</text>
|
||||
<text x="864" y="86">Embed and upsert</text>
|
||||
</g>
|
||||
<g fill="#b3bcc8" font-size="12">
|
||||
<text x="48" y="110">content/ Markdown</text>
|
||||
<text x="48" y="129">estate notes</text>
|
||||
<text x="48" y="148">platform guides</text>
|
||||
<text x="320" y="110">front-matter parsed</text>
|
||||
<text x="320" y="129">secrets and drafts</text>
|
||||
<text x="320" y="148">excluded by rule</text>
|
||||
<text x="592" y="110">blocks, not lines</text>
|
||||
<text x="592" y="129">area · level · source</text>
|
||||
<text x="592" y="148">title · tags</text>
|
||||
<text x="864" y="110">model gateway only</text>
|
||||
<text x="864" y="129">deterministic ids</text>
|
||||
<text x="864" y="148">batched, resumable</text>
|
||||
</g>
|
||||
<g stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)">
|
||||
<line x1="277" y1="106" x2="298" y2="106"/>
|
||||
<line x1="549" y1="106" x2="570" y2="106"/>
|
||||
<line x1="821" y1="106" x2="842" y2="106"/>
|
||||
</g>
|
||||
|
||||
<line x1="968" y1="152" x2="968" y2="184" stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
|
||||
<rect x="30" y="186" width="1060" height="62" rx="9" fill="#12161b" stroke="#6ee7c6" stroke-width="1.2" stroke-dasharray="5 4"/>
|
||||
<text x="50" y="210" font-size="13" font-weight="600" fill="#e6edf3">Qdrant on 127.0.0.1</text>
|
||||
<text x="50" y="231" font-size="12" fill="#b3bcc8">resume_corpus · one point per chunk resume_graph · one point per node and edge HNSW and payloads on disk · within a 400 MiB cap</text>
|
||||
<text x="50" y="272" font-size="11.5" fill="#8b98a8">A sync run re-embeds only the documents whose content, model or chunker version changed.</text>
|
||||
|
||||
<!-- query lane -->
|
||||
<text x="30" y="316" font-size="14" font-weight="600" fill="#e6edf3">Query</text>
|
||||
<text x="108" y="316" font-size="12" fill="#8b98a8">— four graph nodes. It grows past four only with a written reason.</text>
|
||||
|
||||
<line x1="776" y1="248" x2="776" y2="352" stroke="#8b98a8" stroke-width="1.3" stroke-dasharray="4 4" marker-end="url(#arrowDim)"/>
|
||||
<text x="788" y="330" font-size="11" fill="#8b98a8">payload-filtered search</text>
|
||||
|
||||
<g fill="#12161b" stroke="#33404d" stroke-width="1.4">
|
||||
<rect x="30" y="360" width="196" height="112" rx="9"/>
|
||||
<rect x="246" y="360" width="196" height="112" rx="9"/>
|
||||
<rect x="462" y="360" width="196" height="112" rx="9"/>
|
||||
<rect x="678" y="360" width="196" height="112" rx="9"/>
|
||||
<rect x="894" y="360" width="196" height="112" rx="9"/>
|
||||
</g>
|
||||
<g font-size="10.5" fill="#6ee7c6" font-family="ui-monospace, SFMono-Regular, Menlo, monospace">
|
||||
<text x="46" y="382">CLASSIFY</text>
|
||||
<text x="262" y="382">ROUTE</text>
|
||||
<text x="478" y="382">RETRIEVE</text>
|
||||
<text x="694" y="382">COMPOSE</text>
|
||||
<text x="910" y="382">VERIFY</text>
|
||||
</g>
|
||||
<g fill="#e6edf3" font-size="13" font-weight="600">
|
||||
<text x="46" y="406">Read the question</text>
|
||||
<text x="262" y="406">Pick the areas</text>
|
||||
<text x="478" y="406">Find the evidence</text>
|
||||
<text x="694" y="406">Answer from it</text>
|
||||
<text x="910" y="406">Check citations</text>
|
||||
</g>
|
||||
<g fill="#b3bcc8" font-size="11.5">
|
||||
<text x="46" y="430">intent · injection</text>
|
||||
<text x="46" y="448">off-topic · PII</text>
|
||||
<text x="262" y="430">which pages can</text>
|
||||
<text x="262" y="448">answer this</text>
|
||||
<text x="478" y="430">vector + keyword</text>
|
||||
<text x="478" y="448">ranked, top five</text>
|
||||
<text x="694" y="430">context only</text>
|
||||
<text x="694" y="448">no outside claims</text>
|
||||
<text x="910" y="430">every claim maps</text>
|
||||
<text x="910" y="448">to a real page</text>
|
||||
</g>
|
||||
<g stroke="#6ee7c6" stroke-width="1.5" marker-end="url(#arrow)">
|
||||
<line x1="227" y1="416" x2="242" y2="416"/>
|
||||
<line x1="443" y1="416" x2="458" y2="416"/>
|
||||
<line x1="659" y1="416" x2="674" y2="416"/>
|
||||
<line x1="875" y1="416" x2="890" y2="416"/>
|
||||
</g>
|
||||
|
||||
<text x="30" y="500" font-size="12" fill="#b3bcc8">A refusal is a real outcome: an off-topic question, an injection attempt or a request over the cost cap stops at the first node with zero model spend.</text>
|
||||
<text x="30" y="526" font-size="11.5" fill="#8b98a8">Ingest runs today. The query path is the Phase 5 design; until it ships, every page is pre-rendered and needs no model.</text>
|
||||
<text x="30" y="550" font-size="11.5" fill="#8b98a8">The public AI has no tools, no filesystem, no network fetch and no memory between visitors — architectural, not a prompt instruction.</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.5 KiB |
BIN
content/assets/images/sim7080g-module.jpg
Normal file
BIN
content/assets/images/sim7080g-module.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
55
content/edge/edge-fleet.md
Normal file
55
content/edge/edge-fleet.md
Normal file
@@ -0,0 +1,55 @@
|
||||
---
|
||||
id: edge-fleet
|
||||
area: edge
|
||||
level: 2
|
||||
title: "Constrained devices · custom radio · and a record of what broke"
|
||||
claim: "Twenty or more edge nodes across nine zones and twenty automations — solar power · deep sleep · LoRa and hand-built Zigbee — reporting into one automation layer with voice control on top."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, LoRa, Zigbee, MQTT, Home Assistant, Snapcast, Pi, PIR, mmWave]
|
||||
metrics: [{label: "edge nodes", value: 20}, {label: "lighting zones", value: 9}, {label: "automations", value: 20}, {label: "hand-built Zigbee routers", value: 2}]
|
||||
artefacts: [{type: doc, label: "Edge architecture — power · radio · deep sleep", url: "content/edge/l3/edge-architecture.md"}, {type: doc, label: "LoRa irrigation under solar power", url: "content/edge/l3/edge-lora-sprinkler.md"}, {type: doc, label: "Hand-built Zigbee routers and lighting", url: "content/edge/l3/edge-zigbee.md"}, {type: doc, label: "Distributed audio — eight endpoints with ducking", url: "content/edge/l3/edge-snapcast.md"}, {type: doc, label: "Front door — vision · voice · and a rebuild", url: "content/edge/l3/edge-front-door.md"}, {type: doc, label: "Sensors and actuation", url: "content/edge/l3/edge-sensors.md"}, {type: doc, label: "The failures, kept in the record", url: "content/edge/l3/edge-failures.md"}, {type: repo, label: "Embedded C++ — the Wi-Fi manager interface and implementation", url: "content/assets/code/05-embedded-cpp-interface.hpp"}]
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [edge, iot, esp32, lora, zigbee, audio]
|
||||
---
|
||||
|
||||
**Capability statement.** Devices that must run for months on a small battery, talk over radio that was chosen for range rather than convenience, and fail in ways that are visible. Twenty or more nodes, nine zones, twenty automations, and a voice layer on top of all of it.
|
||||
|
||||
## What is actually run
|
||||
|
||||
**Edge architecture.** Each node is designed around its power budget: a LiFePO battery with a solar controller where there is sun, deep sleep between wakes, and a wake source chosen per job. The sprinkler and the garden lights wake on radio rather than on a timer, so a command can arrive while the device is asleep.
|
||||
|
||||
**Radio.** Three links, each for a reason. LoRa for range and penetration on outdoor nodes — long enough to cover the garden and the shed without a repeater. Zigbee for the indoor lighting mesh, including two routers built by hand where the commercial mesh did not reach. Wi-Fi where a device is mains-powered and needs bandwidth, such as the audio endpoints and the camera path.
|
||||
|
||||
**Sensors and actuation.** PIR for movement, radar and mmWave for presence that a PIR cannot see — including a presence sensor built for a bathroom and a child's bedroom because the off-the-shelf options were wrong for the space. Relays and step-down converters for 240V loads, servos and a 16-channel PWM driver for mechanical jobs, solenoids for water, load cells for weight.
|
||||
|
||||
**Audio distribution.** Eight Snapcast endpoints on Pi hardware, fed by a Snapserver, with Spotify and a music server as sources. Every spoken announcement — automation notices, assistant replies, doorbell messages — plays across all eight endpoints with the music ducked underneath and restored afterwards.
|
||||
|
||||
**The automation layer.** Home Assistant holds the state and the routines; MQTT carries the messages. Nine lighting zones and twenty automations, with voice control on top, so a request spoken in one room is answered through the speakers in whichever room a person is standing.
|
||||
|
||||
**Cameras and vision.** The front door takes an image, logs it, has the image described, and speaks the result into the house before deciding what to do next. RFID tags on boxes and on trees resolve to records — inventory with prices for the boxes, care notes for the trees — written by an automation rather than typed by hand.
|
||||
|
||||
## Why this exhibit is good code
|
||||
|
||||
The header declares the interface and includes only `Arduino.h`, so a caller compiles without pulling the ESP32 Wi-Fi stack into its translation unit. Only the implementation file knows about station mode, polling and millisecond timeouts, and it returns a boolean so the caller decides what a failed join means. The two files are shown together because the point is the split, not either file alone.
|
||||
|
||||
## Evidence
|
||||
|
||||
| Claim | Evidence | Artefact |
|
||||
|---|---|---|
|
||||
| Twenty or more nodes | Ten ESP32 devices · two hand-built Zigbee routers · eight Pi audio endpoints | [Edge architecture](l3/edge-architecture.md) |
|
||||
| Radio chosen per job | LoRa outdoors with radio wake from deep sleep; Zigbee indoors with hand-built routers | [Irrigation](l3/edge-lora-sprinkler.md) · [Zigbee](l3/edge-zigbee.md) |
|
||||
| Nine zones and twenty automations | Lighting zones and routines in the automation layer | [Zigbee and lighting](l3/edge-zigbee.md) |
|
||||
| Eight endpoints with ducking | Snapcast clients on Pi hardware with music lowered under speech | [Distributed audio](l3/edge-snapcast.md) |
|
||||
| Vision and voice at the door | Image capture · description · spoken response · two-way audio | [Front door](l3/edge-front-door.md) |
|
||||
| Failures are stated | Lizard feeder destroyed · garden cameras postponed · doorbell being rebuilt | [Failures](l3/edge-failures.md) |
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **Power decides the design.** Deep sleep means a device is unreachable for most of its life, so every routine has to tolerate a node that is asleep. That constraint is why radio wake exists at all.
|
||||
- **Range over bandwidth.** LoRa is slow and payload-limited. It is chosen where a hundred bytes a day is enough and the alternative is a Wi-Fi access point in a garden bed.
|
||||
- **Some things are still manual.** The garden automation is partly manual — some watering and lighting is scheduled, some is a person turning something on. The page says which is which instead of implying full coverage.
|
||||
- **The failures cost money and are kept.** A lizard destroyed a feeder built for it. The garden cameras were postponed with the parts already bought. The doorbell is being rebuilt rather than declared finished. Those are the most honest evidence of production experience on this site.
|
||||
|
||||
Individual nodes carry their own label, and the ones being rebuilt or postponed are labelled that way on their pages.
|
||||
16
content/edge/edge-overview.md
Normal file
16
content/edge/edge-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: edge-overview
|
||||
area: edge
|
||||
level: 1
|
||||
title: "Edge & Devices"
|
||||
claim: "Sam builds and maintains a twenty-node edge fleet — constrained devices on solar power and deep sleep, with custom radio and sensors — that reports into a single automation layer."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, LoRa, Zigbee, MQTT, Home Assistant, Snapcast, Raspberry Pi]
|
||||
metrics: [{label: "edge nodes", value: 20}, {label: "lighting zones", value: 9}, {label: "automations", value: 20}]
|
||||
artefacts: [{type: doc, label: "Edge architecture — power · radio · deep sleep", url: "content/edge/l3/edge-architecture.md"}, {type: doc, label: "The failures, kept in the record", url: "content/edge/l3/edge-failures.md"}, {type: doc, label: "Distributed audio — eight endpoints with ducking", url: "content/edge/l3/edge-snapcast.md"}, {type: doc, label: "Vehicle tracker on LTE Cat-M", url: "content/edge/l3/edge-car-tracker.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, iot, esp32, lora, zigbee]
|
||||
---
|
||||
The devices are outdoors on solar and asleep most of their lives, and they have to keep working when nobody is looking; what failed is written down. The architecture, the radio and the failures are on the [fleet page](edge-fleet.md).
|
||||
29
content/edge/l3/edge-architecture.md
Normal file
29
content/edge/l3/edge-architecture.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: edge-architecture
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Edge architecture · power · radio · deep sleep"
|
||||
claim: "Each node is designed around its power budget — battery and solar · deep sleep between wakes · and a deliberate choice of wake source per job."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, LoRa, Zigbee, LiFePO, MQTT, Home Assistant]
|
||||
metrics: [{label: "edge nodes", value: 20}, {label: "ESP32 devices", value: 10}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, architecture, power, artefact]
|
||||
---
|
||||
|
||||
One annotated diagram of a representative node and the radio path lands in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The per-node pattern: 240V step-down or solar with a LiFePO battery and charge controller, an ESP32 in deep sleep, a wake source (timer, radio, or a physical input), a relay or driver for the load, and an MQTT message into the automation layer. Around it, the reason each node uses LoRa, Zigbee or Wi-Fi.
|
||||
|
||||
## What it proves
|
||||
|
||||
Power budgeting and remote wake as design decisions, not hobby wiring — and an architecture that survived being repeated across twenty nodes.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: evidence notes section 1.
|
||||
51
content/edge/l3/edge-car-tracker.md
Normal file
51
content/edge/l3/edge-car-tracker.md
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
id: edge-car-tracker
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Vehicle tracker on LTE Cat-M"
|
||||
claim: "A tracker that sleeps between reports · wakes on a schedule or when the car moves · and reports when it leaves a boundary set from the web interface."
|
||||
confidence: in-progress
|
||||
period: "2024–"
|
||||
tech: [ESP32, LTE Cat-M, NB-IoT, SIM7080G, GPS, LiFePO4]
|
||||
metrics: [{label: "battery packs", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [edge, tracker, lte, low-power, artefact]
|
||||
---
|
||||
|
||||
The build is on the bench: two LiFePO batteries, a buck converter, an ESP32 with GPS, and a
|
||||
cellular module for the uplink. The car keeps the packs charged while it is running.
|
||||
|
||||

|
||||
|
||||
## What it does
|
||||
|
||||
Two behaviours, chosen by the settings rather than hard-coded. When the car is parked the
|
||||
ESP32 sits in deep sleep, wakes on a timer, checks its own configuration, and reports only
|
||||
what the settings ask for. When the car is moving it stays awake and reports on a shorter
|
||||
interval. Either way the interval and the boundary can be changed from the web interface,
|
||||
and a change can force an immediate update rather than waiting for the next wake.
|
||||
|
||||
The geofence is the part that matters. A boundary is drawn from the web interface, and the
|
||||
device is responsible for deciding whether it has left it — which is why the settings have
|
||||
to travel to the device rather than live only in the server.
|
||||
|
||||
## Why LTE Cat-M rather than WiFi
|
||||
|
||||
The tracker lives in a car. WiFi coverage is a driveway, not a road. Cat-M and NB-IoT were
|
||||
chosen because they are designed for exactly this shape of traffic — small payloads, long
|
||||
sleeps, coverage beyond what 4G handsets need — and because the modules are cheap enough to
|
||||
leave installed.
|
||||
|
||||
## What it proves
|
||||
|
||||
A complete path rather than a demo: constrained hardware, a power budget that dictates the
|
||||
schedule, a device that holds enough state to act on its own, and a web application that
|
||||
configures it and shows where it has been.
|
||||
|
||||
## Still open
|
||||
|
||||
Field testing across the full battery cycle. The sleep interval that is right for a parked
|
||||
car and the interval that is right for a moving one are guesses until the discharge curve
|
||||
has been measured over a week rather than a day.
|
||||
37
content/edge/l3/edge-failures.md
Normal file
37
content/edge/l3/edge-failures.md
Normal file
@@ -0,0 +1,37 @@
|
||||
---
|
||||
id: edge-failures
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — The failures · kept in the record"
|
||||
claim: "Three things did not go to plan — a feeder the animal destroyed · cameras postponed with the parts already bought · and a doorbell being rebuilt instead of declared finished."
|
||||
confidence: in-progress
|
||||
period: "2020–"
|
||||
tech: [ESP32, Servo, PWM, Camera]
|
||||
metrics: [{label: "crickets released per day by the feeder", value: 10}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, failures, artefact]
|
||||
---
|
||||
|
||||
Two items are being rebuilt, one is postponed. The page is not finished because the work is not finished.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A short, unpolished register of what did not work, in the order it broke.
|
||||
|
||||
**The lizard feeder.** Servo motors on a 16-channel PWM driver, rubber-band spring-loaded boxes, an ESP32 — one box opened per day to release ten crickets for the bearded dragon it was built for. The lizard attacked the boxes and destroyed the mechanism. Version one is abandoned; it is being rebuilt with plastic containers.
|
||||
|
||||
**The garden cameras.** Postponed. The parts have arrived and are sitting in a box. No photographs and no claim until they are installed.
|
||||
|
||||
**The front door.** Built, then found to greet people who were not at the door. It is being rebuilt with a time-of-flight ranging sensor rather than being described as complete.
|
||||
|
||||
**The garden automation.** Partly manual. Some of it runs on schedule and some of it is a person. Both are true and the page says so.
|
||||
|
||||
## What it proves
|
||||
|
||||
That this is a working estate and not a rendering: things break, budgets get spent on parts that are not installed yet, and hardware has to be designed around an animal's behaviour. Production experience is measured in rebuilds as much as in launches.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7, with photographs of the rebuilt feeder and the installed cameras if they exist by then. If they do not, the entry stays as written — labelled rather than deleted.
|
||||
33
content/edge/l3/edge-front-door.md
Normal file
33
content/edge/l3/edge-front-door.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: edge-front-door
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Front door · vision · voice · and a rebuild"
|
||||
claim: "A doorbell that sees a person · describes the image · speaks to the house and to the visitor · and is currently being rebuilt with a time-of-flight sensor."
|
||||
confidence: in-progress
|
||||
period: "2020–"
|
||||
tech: [ESP32, ESP32-CAM, PIR, mmWave, VL53L0X, MiniMax, Piper, Whisper, Apprise]
|
||||
metrics: [{label: "voice engines in use", value: 2}, {label: "presence sensing inputs", value: "PIR · mmWave · time-of-flight"}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, doorbell, vision, voice, artefact]
|
||||
---
|
||||
|
||||
The device was built and is being rebuilt. The page states this rather than describing it as finished.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A door unit with microphone, speaker, camera, button, PIR and mmWave presence. It detects a person, captures an image, logs it, has the image described, and announces to the house who is at the door before offering a spoken greeting; the visitor's reply is captured and played over the house speakers, with an alert pushed to a phone. Voice output ran on one provider and now runs on a cloned voice with a fallback to a local synthesiser.
|
||||
|
||||
## What it proves
|
||||
|
||||
Vision, voice and actuation in one device under real constraints — latency that a person at a door will tolerate, and a failure mode that must be polite.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
The measurement it is being rebuilt around: presence detection with a time-of-flight ranging sensor, to stop the unit greeting people who are walking past rather than standing at the door. Until the rebuild is finished the honest label is `in-progress`.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7 for photos and the sensor change. Source: evidence notes section 1.
|
||||
33
content/edge/l3/edge-lora-sprinkler.md
Normal file
33
content/edge/l3/edge-lora-sprinkler.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: edge-lora-sprinkler
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — LoRa irrigation under solar power"
|
||||
claim: "A solar-powered controller that sleeps · wakes on radio · and drives a solenoid — with the first version polling over a message queue instead."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, LoRa, LiFePO, Solenoid, Home Assistant, RabbitMQ]
|
||||
metrics: [{label: "major revisions", value: 2}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, lora, solar, irrigation, artefact]
|
||||
---
|
||||
|
||||
The bill of materials and the wake-on-radio change land in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The build: solar panel, charge controller, LiFePO battery, ESP32, LoRa module, relay and solenoid valve. Version one woke on a schedule and polled through a message queue to learn whether it should run. Version two is better: a LoRa packet can wake the ESP32 from deep sleep and switch the valve directly, so on-time is short and the battery is not spent asking whether there is work.
|
||||
|
||||
## What it proves
|
||||
|
||||
Iteration on a real constraint. The first version worked; the second version was designed around the power cost of asking, which is the kind of decision you only make if you are watching the battery.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
Watering is partly scheduled and partly a person. The routine lives in the automation layer and the voice assistant can trigger it, but the garden is not fully automatic and the page does not pretend otherwise.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: evidence notes section 1.
|
||||
29
content/edge/l3/edge-sensors.md
Normal file
29
content/edge/l3/edge-sensors.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: edge-sensors
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Sensors and actuation"
|
||||
claim: "Movement · presence · weight · temperature · water and mechanical actuation — chosen per job and integrated into one automation layer."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, PIR, Radar, mmWave, Load cell, Servo, Solenoid, MQTT]
|
||||
metrics: [{label: "automations", value: 20}, {label: "PWM channels on the feeder driver", value: 16}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, sensors, actuation, artefact]
|
||||
---
|
||||
|
||||
A sensor and actuator table lands in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The parts list of the fleet in the way an engineer would want it: what is sensed, which device senses it, what is actuated, and how the decision reaches a physical action — a relay, a solenoid, a servo, a step-down converter switching 240V, a wheel that flicks food into a tank, an auger that dispenses it, a load cell that decides when to top up water.
|
||||
|
||||
## What it proves
|
||||
|
||||
Range across the awkward middle of embedded work: not just reading a sensor but choosing an actuator, sizing its driver, and making the whole loop recoverable when the network is absent.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: evidence notes section 1.
|
||||
29
content/edge/l3/edge-snapcast.md
Normal file
29
content/edge/l3/edge-snapcast.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: edge-snapcast
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Distributed audio · eight endpoints with ducking"
|
||||
claim: "Eight synchronised endpoints with music ducked under every spoken announcement and restored afterwards — driven by Home Assistant and the voice assistant."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Snapcast, Mopidy, Spotify, Raspberry Pi, Home Assistant]
|
||||
metrics: [{label: "audio endpoints", value: 8}, {label: "client hardware", value: "Pi 2W"}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, audio, snapcast, artefact]
|
||||
---
|
||||
|
||||
The topology diagram and a short recording of ducking land in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A Snapserver with eight Pi clients, Spotify and a local music server as sources, and one behaviour that matters more than the rest: when the house needs to speak, the music drops and the announcement plays everywhere, then the music returns to its previous level. Requests arrive by voice, so the same system that plays music is also the output for every automation and every answer.
|
||||
|
||||
## What it proves
|
||||
|
||||
Multi-room audio treated as infrastructure with a predictable interruption behaviour — and the difference between "the speakers work" and "the speakers can be interrupted safely". Mains-powered nodes, bandwidth-heavy work, and a state machine for volume: the opposite design constraints to the battery nodes.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: evidence notes section 1.
|
||||
29
content/edge/l3/edge-zigbee.md
Normal file
29
content/edge/l3/edge-zigbee.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: edge-zigbee
|
||||
area: edge
|
||||
level: 3
|
||||
title: "Artefact — Hand-built Zigbee routers and lighting"
|
||||
claim: "Two routers built by hand to extend an indoor mesh · feeding nine lighting zones with presence sensing and voice control."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [ESP32, Zigbee, Relay, PIR, Radar, Home Assistant]
|
||||
metrics: [{label: "hand-built Zigbee routers", value: 2}, {label: "lighting zones", value: 9}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [edge, zigbee, lighting, artefact]
|
||||
---
|
||||
|
||||
The router build and a zone map land in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
Two Zigbee routers on ESP32 hardware, built because the commercial mesh did not cover the house and a repeater would have been a black box. Behind them, nine lighting zones: custom presence sensors with PIR and radar for the bathroom and a child's bedroom, a central light controller with a readout and a potentiometer, and a three-button remote with a dial — click on, double click off, dial for level.
|
||||
|
||||
## What it proves
|
||||
|
||||
Mesh networking understood at the level where you can add a node instead of buying one, and lighting built for how a house is used rather than for a product category.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: evidence notes section 1.
|
||||
53
content/history/history-engagements.md
Normal file
53
content/history/history-engagements.md
Normal file
@@ -0,0 +1,53 @@
|
||||
---
|
||||
id: history-engagements
|
||||
area: history
|
||||
level: 2
|
||||
title: "Fifteen years of delivery for agencies and their clients"
|
||||
claim: "Twenty-two agencies and twenty-three brands · national campaigns · bank and broadcaster platforms · betting front-ends · and a habit of being called in to rescue work someone else could not finish."
|
||||
confidence: historic
|
||||
period: "1999–2014"
|
||||
tech: [ActionScript, PHP, JavaScript, MySQL, XML, SOAP, React, Python]
|
||||
metrics: [{label: "agencies on the client wall", value: 22}, {label: "brands delivered for", value: 23}, {label: "code exhibits kept", value: 6}]
|
||||
artefacts: [{type: doc, label: "The client wall", url: "content/history/l3/history-client-wall.md"}, {type: doc, label: "Six code exhibits across five eras", url: "content/history/l3/history-code-exhibits.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [history, agencies, brands, exhibits]
|
||||
---
|
||||
|
||||
**Capability statement.** Range under deadline. The work in this period ran from car classifieds and online betting to a bank's pitch piece, a broadcaster's on-demand banners and a publisher's education platform — usually with a date attached that could not move.
|
||||
|
||||
## The eras, briefly
|
||||
|
||||
**1999–2002 — the start.** Web design and development for a high-volume car marketplace, then freelance work building sites, applications, interfaces and database back ends for small agencies.
|
||||
|
||||
**2002–2005 — London, full time.** Online gaming: lead front-end developer on a synchronous multi-game platform handling a thousand simultaneous clients, integrating against Java back ends and XML sockets. Then the emerging interactive advertising industry, where a creative front end with a technical spine was scarce.
|
||||
|
||||
**2006–2009 — the agency run.** The bulk of the client wall: bank pitch work delivered in weeks, broadcaster promotions, a global oil company's re-brand site, premium drinks campaigns, a news organisation's media presentation layer, a publisher's education platform, and a global computer-aided design vendor's localised site. Several of these were rescues — brought in on a short deadline to rewrite work another team could not land.
|
||||
|
||||
**2009–2012 — product and campaign.** A standalone encrypted presentation builder with its own media library and remote update path. A national beer campaign with video submissions, cross-brand promotion, in-store mechanics and authorisation over SOAP. Interactive launches with 3D content in the browser.
|
||||
|
||||
**2012–2014 — small products for small business.** Online book building, a reactive design system, and art applications for a motion-sensing controller, transitioned toward mobile.
|
||||
|
||||
**2014–2020 — the workshop years.** The site builder and the signage product (see [Products](../products/products-business.md)), which is where the work moved from delivering campaigns for other people's brands to owning software and supporting customers.
|
||||
|
||||
## Languages across the eras
|
||||
|
||||
The point of the exhibit set is that the tooling changed repeatedly and the delivery discipline did not: ActionScript 3 with class-based architecture, hand-written JavaScript engines, PHP media pipelines, TypeScript with typed service layers and a relational schema, embedded C++ for firmware, and now declarative infrastructure as its own kind of code.
|
||||
|
||||
## Evidence
|
||||
|
||||
| Claim | Evidence | Artefact |
|
||||
|---|---|---|
|
||||
| Twenty-two agencies and twenty-three brands | The client wall, carried from the résumé and the recovered site content | [Client wall](l3/history-client-wall.md) |
|
||||
| Work across six technology eras | Six exhibits from OO ActionScript to a hand-written Nix package | [Code exhibits](l3/history-code-exhibits.md) |
|
||||
| Rescue work under immovable deadlines | Multiple engagements recorded as rewrites of stalled projects | [Client wall](l3/history-client-wall.md) |
|
||||
| Sponsorship of the later product era | 2014–2020 product work described in the résumé | [Products](../products/products-business.md) |
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **Historic work is labelled historic.** Nothing in this section is presented as current capability; where a technique is still used today, the page links to where it is used.
|
||||
- **One engagement has no screenshot.** The national archive work was an internal tool. It is described and not illustrated, because there is nothing legitimately publishable to show.
|
||||
- **No client has been named beyond a brand.** Brands are named because they were public campaigns. Individuals, internal contacts and unpublished commercial detail are excluded.
|
||||
- **The exhibits are excerpts.** Around forty lines each, chosen to show judgement — structure, error handling, an exit plan — not to demonstrate volume.
|
||||
|
||||
This work is finished, and it is presented in the past tense throughout.
|
||||
16
content/history/history-overview.md
Normal file
16
content/history/history-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: history-overview
|
||||
area: history
|
||||
level: 1
|
||||
title: "History"
|
||||
claim: "Sam delivered interactive work for twenty-two agencies and twenty-three brands between 1999 and 2014 — the commercial apprenticeship that produced the range."
|
||||
confidence: historic
|
||||
period: "1999–2014"
|
||||
tech: [ActionScript, PHP, JavaScript, MySQL, XML, React, Python, Go, HTMX]
|
||||
metrics: [{label: "agencies on the client wall", value: 22}, {label: "brands delivered for", value: 23}, {label: "code exhibits kept", value: 6}]
|
||||
artefacts: [{type: doc, label: "The client wall", url: "content/history/l3/history-client-wall.md"}, {type: doc, label: "Six code exhibits across five eras", url: "content/history/l3/history-code-exhibits.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [history, agencies, brands, career]
|
||||
---
|
||||
The languages changed six times across the period — ActionScript, PHP, JavaScript, React, Python, and now Go with HTMX — and the exhibits are chosen to show that rather than to show a decade. The era-by-era record is on the [engagement record](history-engagements.md).
|
||||
90
content/history/l3/history-client-wall.md
Normal file
90
content/history/l3/history-client-wall.md
Normal file
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: history-client-wall
|
||||
area: history
|
||||
level: 3
|
||||
title: "Artefact — The client wall"
|
||||
claim: "Twenty-two agencies and twenty-three brands — the delivery record carried from the résumé rather than reconstructed from memory."
|
||||
confidence: historic
|
||||
period: "1999–2014"
|
||||
tech: [ActionScript, PHP, JavaScript, MySQL, XML]
|
||||
metrics: [{label: "agencies on the client wall", value: 22}, {label: "brands on the client wall", value: 23}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [history, clients, artefact]
|
||||
---
|
||||
|
||||
The names are final. The treatment is deliberate: one typeface, one size, one grid, and no logo collage — see *Why there are no logos here* below.
|
||||
|
||||
## The agencies
|
||||
|
||||
<div class="client-wall" role="list" aria-label="Agencies">
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Clemenger</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Agency Republic</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Brand Union</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Jackpot Joy</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Bryce Ford</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">RMG Connect</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Sporting Index</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Adventis Digital</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Trinity Street</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Inferno</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">SAS Design</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Ogilvy</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Wunderman</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Illumina</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Barkers</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Mook</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Pearsons</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Penguin</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Draft London</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Behan Consulting</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">MCM Entertainment</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Carsales</span></div>
|
||||
</div>
|
||||
|
||||
## The brands
|
||||
|
||||
<div class="client-wall" role="list" aria-label="Brands">
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Carlton Draught</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Diageo</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">KPMG</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">McDonald's</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Accenture</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">BBC National Archive</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Bank of America</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">War of the Worlds</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Madonna (Hard Candy)</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">PlayStation</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">HSBC</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">DHL</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Reuters</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Volvic</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">AutoCAD</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Channel 4</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Pearsons</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Shell</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Stella Artois</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Baileys Irish Cream</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Parkgate Homes</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Carlton Dry</span></div>
|
||||
<div class="client-tile" role="listitem"><span class="client-name">Cottees Cordial</span></div>
|
||||
</div>
|
||||
|
||||
## Why there are no logos here
|
||||
|
||||
A wall of mixed marks — some full colour, some monochrome, some missing — reads as broken, and the eight marks that exist for these names are not the eight the work is best remembered by. One typeface, one size and one grid says the same thing more clearly, and it sits beside the technical pages without competing with them. A mark is added only when it is the client's official mark and Sam has approved its use; none has been invented.
|
||||
|
||||
## Corrections carried into this list
|
||||
|
||||
- **Diageo**, not the "Diajio" spelling that appeared in the recovered site content.
|
||||
- **Baileys Irish Cream**, stated in full and spelled correctly.
|
||||
- The **BBC National Archive** engagement was an internal tool. It is described, and it is not illustrated with a screenshot, because no legitimately publishable image exists.
|
||||
|
||||
## Count discrepancy — recorded, not smoothed over
|
||||
|
||||
The summary material said twenty-three agencies. The recovered, verified list holds **twenty-two** names, and the count published above is the count of the list. If a name is missing, the number moves to twenty-three and this note is deleted; until then the smaller, checkable number is the one on the page.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Delivered as a body of names and descriptions, and presented as a typographic wall from Phase 7. Names and one-line descriptions are approved as recovered and corrected.
|
||||
46
content/history/l3/history-code-exhibits.md
Normal file
46
content/history/l3/history-code-exhibits.md
Normal file
@@ -0,0 +1,46 @@
|
||||
---
|
||||
id: history-code-exhibits
|
||||
area: history
|
||||
level: 3
|
||||
title: "Artefact — Six code exhibits across five eras"
|
||||
claim: "Six short excerpts spanning object-oriented ActionScript · a hand-written JavaScript animation engine · a PHP media pipeline · typed TypeScript · embedded C++ · and declarative Nix."
|
||||
confidence: historic
|
||||
period: "2006–2026"
|
||||
tech: [ActionScript, JavaScript, PHP, TypeScript, C++, Nix]
|
||||
metrics: [{label: "code exhibits", value: 6}, {label: "technology eras spanned", value: 5}]
|
||||
artefacts: [{type: repo, label: "ActionScript 3 — the Siren class", url: "content/assets/code/01-actionscript-class.as"}]
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [history, code, exhibits, artefact]
|
||||
---
|
||||
|
||||
**Status: published.** This page shows the ActionScript class and indexes the other five, each of which is exhibited on the page where it belongs.
|
||||
|
||||
## The six
|
||||
|
||||
| # | Era | Exhibit | Where it is shown |
|
||||
|---|---|---|---|
|
||||
| 1 | Object-oriented ActionScript | `01-actionscript-class.as` — class, imports, encapsulation | this page |
|
||||
| 2 | Hand-written JavaScript engine | `02-js-easing-core.js` — easing functions and the timeline loop | [the site builder](../../products/l3/products-websitebuilder.md) |
|
||||
| 3 | PHP media pipeline | `03-php-encode-op.php` — one GIF encode operation and its FFmpeg commands | [the site builder](../../products/l3/products-websitebuilder.md) |
|
||||
| 4 | TypeScript | `04-typescript-service-layer.ts` — a typed tRPC boundary | [the estate](../../platform/platform-estate.md) |
|
||||
| 5 | Embedded C++ | `05-embedded-cpp-interface.hpp` — interface split from implementation | [the edge fleet](../../edge/edge-fleet.md) |
|
||||
| 6 | Declarative infrastructure | `06-nix-derivation.nix` — pinned version, verified hash, exit condition | [the Nix package](../../platform/l3/platform-tuios-package.md) |
|
||||
|
||||
Total is 243 lines across twenty years, which is the point: each excerpt is chosen to show judgement rather than volume.
|
||||
|
||||
## Why this exhibit is good code
|
||||
|
||||
Every field is private and explicitly typed, and the public surface is two methods, so a caller cannot push the object into a state it does not understand. The constructor takes what it needs as parameters instead of reaching for it, which is why the same class can drive every siren on the screen without knowing where any of them are.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
The excerpt is verbatim, including a latent typo (`this.high` rather than `this.height`). It is left as found: the excerpt is evidence, not a portfolio piece.
|
||||
|
||||
## What it proves
|
||||
|
||||
That the stack changed six times and the discipline did not. Exhibit 6 is the least common of the six and the most current — a short, correct, self-documenting derivation with an exit plan is a stronger infrastructure signal than a long configuration file.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Published. The six files are in this repository under `content/assets/code/`; the archive path is withheld. The other five are linked above from the pages that carry their claims.
|
||||
33
content/platform/l3/platform-backups.md
Normal file
33
content/platform/l3/platform-backups.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: platform-backups
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — Backups and restore runbook"
|
||||
claim: "Three tiers — local snapshots · a copy on another machine · an offsite copy — with retention stated in the job and a restore runbook kept beside them."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Borg, Kopia, Backrest, AWS S3, systemd, NFS]
|
||||
metrics: [{label: "backup tiers", value: 3}, {label: "retention windows in the primary job", value: 3}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, backups, restore, artefact]
|
||||
---
|
||||
|
||||
Redacted job definitions and the restore runbook land in Phase 7. Passphrase locations and remote paths are removed before publication.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The backup design: Borg jobs on the two desktop hosts to local storage with a network copy, Kopia and Backrest on the edge host to network storage, an offsite copy in object storage, and retention expressed in the job (seven daily, four weekly, three monthly, plus annual on the edge). Beside it, the restore runbook and the list of what is deliberately not covered.
|
||||
|
||||
## What it proves
|
||||
|
||||
That failure was designed for: three tiers cover disk loss, machine loss and site loss, and the restore path is written down rather than recovered from memory at the worst possible moment.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
Not everything is covered, and the page says so — one storage host is itself a single point of failure for some repositories, and the file server has no backup of its own. Those are known gaps with owners, not oversights to be hidden.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source: the infrastructure reference and the backup system notes, read 2026-10-26.
|
||||
29
content/platform/l3/platform-containers.md
Normal file
29
content/platform/l3/platform-containers.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: platform-containers
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — Container inventory"
|
||||
claim: "Fifty or more containers across the estate — each with its own port · its own folder · and a memory cap equal to its swap cap."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Docker, Compose, Caddy, Proxmox]
|
||||
metrics: [{label: "containers in the estate", value: 50}, {label: "memory cap rule", value: "mem_limit equals memswap_limit"}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, docker, artefact]
|
||||
---
|
||||
|
||||
A digest of the inventory — grouped by purpose, with addresses removed — lands in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The inventory generated from the hosts, grouped by purpose: media and file services, home automation, the AI services, the public edge, and the build tooling. For each service the same three facts are recorded — port, configuration folder, memory cap.
|
||||
|
||||
## What it proves
|
||||
|
||||
That the estate is inventoried rather than accumulated, and that the resource rules are applied per service instead of being a policy on a wiki page.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7, generated from the live hosts. Counts follow the approved metric set; the exact number is regenerated at capture time.
|
||||
35
content/platform/l3/platform-network.md
Normal file
35
content/platform/l3/platform-network.md
Normal file
@@ -0,0 +1,35 @@
|
||||
---
|
||||
id: platform-network
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — Network layout and remote access"
|
||||
claim: "A private mesh and SSH everywhere · services bound to loopback · and one public listener at the edge terminating TLS for the sites that are allowed to be seen."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [Tailscale, SSH, Caddy, DNS, Termux]
|
||||
metrics: [{label: "public listeners per service", value: 1}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, networking, security, artefact]
|
||||
---
|
||||
|
||||
The diagram below shows three hosts and one trust boundary; internal addresses, hostnames and public DNS names are generalised, not published.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The layout: hosts on a private mesh for remote access, SSH on every machine, a phone used as a terminal, and an edge host that terminates TLS and proxies to loopback-bound services. New services bind to loopback by default; making something public is an explicit edge change, not a side effect of starting a container.
|
||||
|
||||
## What it proves
|
||||
|
||||
Security posture chosen rather than inherited: no service is reachable because it happened to bind to all interfaces, and there is exactly one place where the outside world meets the estate.
|
||||
|
||||
## The layout
|
||||
|
||||

|
||||
|
||||
Read it left to right: work is built on one machine, runs on a memory-bound machine with every service bound to loopback, and is published by a single edge that terminates TLS. The dashed boundary is the point of the whole arrangement — crossing it is a deliberate change to the edge configuration, not a side effect of starting a container. Below both, the mesh carries remote access and the second backup tier, so neither depends on a port being open.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Delivered. Internal addressing, hostnames and public DNS names are excluded from publication by the redaction pass.
|
||||
33
content/platform/l3/platform-nixos.md
Normal file
33
content/platform/l3/platform-nixos.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: platform-nixos
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — Declarative multi-host configuration"
|
||||
claim: "Machines are described rather than assembled — a shared NixOS base · a replicated desktop and terminal · and Proxmox underneath the virtualised side."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [NixOS, Niri, Proxmox, Neovim, Zsh, Ghostty]
|
||||
metrics: [{label: "hosts on the declarative base", value: 4}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, nixos, artefact]
|
||||
---
|
||||
|
||||
A redacted excerpt of the configuration tree lands in Phase 7. No hostnames, addresses or keys are published.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The shape of the configuration: one shared base, per-host modules, and the parts that make the desktop and the terminal identical everywhere — window manager, editor, shell and the plugins that carry the work. Proxmox hosts the virtualised side, including the file server and the home automation appliance.
|
||||
|
||||
## What it proves
|
||||
|
||||
That a machine can be rebuilt from a description, and that the same environment follows the person between machines instead of being re-created by memory.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
The terminal-first setup is deliberate: a heavier desktop was downgraded to the terminal on one machine for memory headroom. A phone-as-server experiment is on hold, not shipped, and is described as on hold.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Source facts: evidence notes section 3.
|
||||
29
content/platform/l3/platform-signage.md
Normal file
29
content/platform/l3/platform-signage.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
id: platform-signage
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — Signage infrastructure · screens as a managed fleet"
|
||||
claim: "HDMI-CEC control of power · rotation · mirroring and multi-screen layout — so a display fleet is driven by the platform rather than by a remote control."
|
||||
confidence: delivered
|
||||
period: "2014–"
|
||||
tech: [HDMI-CEC, Raspberry Pi, Python, JavaScript, FFmpeg]
|
||||
metrics: [{label: "commercial deployments evidenced", value: "salon · financial services business"}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, signage, hdmi-cec, artefact]
|
||||
---
|
||||
|
||||
The CEC command set and a layout diagram land in Phase 7.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The platform side of the signage product: the codes and control path that switch screens on and off, rotate and mirror output, and lay out multi-screen displays, including synchronised playback across screens in one site.
|
||||
|
||||
## What it proves
|
||||
|
||||
Device-level control combined with fleet management. This is the same skill the edge pages show on small devices, applied to physical displays that other people depend on.
|
||||
|
||||
## Confidence
|
||||
|
||||
`delivered` — this ran in commercial deployments. The product's business status is stated separately on the Products pages, where the honest position is that the software is being rebuilt and three businesses still run it.
|
||||
33
content/platform/l3/platform-tuios-package.md
Normal file
33
content/platform/l3/platform-tuios-package.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: platform-tuios-package
|
||||
area: platform
|
||||
level: 3
|
||||
title: "Artefact — A hand-written Nix package with an exit plan"
|
||||
claim: "Twenty-five lines of Nix that pin a tool at 0.10.1 over nixpkgs' 0.7.0 — with a verified hash · a written reason and a written condition for deleting the file."
|
||||
confidence: running
|
||||
period: "2026–"
|
||||
tech: [Nix, NixOS]
|
||||
metrics: [{label: "pinned version", value: "0.10.1"}, {label: "nixpkgs version it overrides", value: "0.7.0"}]
|
||||
artefacts: [{type: repo, label: "Nix derivation — tuios 0.10.1 override with a removal condition", url: "content/assets/code/06-nix-derivation.nix"}]
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [platform, nix, packaging, artefact]
|
||||
---
|
||||
|
||||
**Status: published.** The whole file is shown verbatim.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A package definition for the agent operations console: `fetchurl` against a released artifact with a verified hash, a documented reason for overriding nixpkgs, an `installPhase` that stages the binary, and a comment stating the condition under which the file should be deleted.
|
||||
|
||||
## What it proves
|
||||
|
||||
More than knowing Nix exists. The signals are that someone noticed a version gap, fixed it locally instead of waiting, pinned it so the fleet is reproducible, and wrote down when to remove their own patch. Knowing when to delete your own override is the senior part.
|
||||
|
||||
## Why this exhibit is good code
|
||||
|
||||
The override records why it exists and the exact condition under which it should be deleted, which is the part of a local patch that usually goes missing. Pinning a released binary by `fetchurl` with a verified hash keeps the result reproducible without waiting for nixpkgs.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Published. The file is in this repository at `content/assets/code/06-nix-derivation.nix`; the infrastructure repository remains the source. Source fact: content spine section 4.2.
|
||||
55
content/platform/platform-estate.md
Normal file
55
content/platform/platform-estate.md
Normal file
@@ -0,0 +1,55 @@
|
||||
---
|
||||
id: platform-estate
|
||||
area: platform
|
||||
level: 2
|
||||
title: "A small estate run to a documented standard"
|
||||
claim: "Fifty or more containers on a four-host estate, all on declarative configuration — with packaging discipline · encrypted backups and a restore runbook · loopback-bound services · and one public listener."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [NixOS, Proxmox, Docker, Caddy, Tailscale, Borg, Kopia, AWS S3, HDMI-CEC]
|
||||
metrics: [{label: "containers in the estate", value: 50}, {label: "hosts", value: 4}, {label: "backup tiers", value: "Borg · Kopia · AWS"}, {label: "public listeners", value: 1}]
|
||||
artefacts: [{type: doc, label: "Declarative multi-host configuration", url: "content/platform/l3/platform-nixos.md"}, {type: doc, label: "A hand-written Nix package with an exit plan", url: "content/platform/l3/platform-tuios-package.md"}, {type: doc, label: "Container inventory", url: "content/platform/l3/platform-containers.md"}, {type: doc, label: "Backups and restore runbook", url: "content/platform/l3/platform-backups.md"}, {type: doc, label: "Network layout and remote access", url: "content/platform/l3/platform-network.md"}, {type: doc, label: "Signage infrastructure — screens as a managed fleet", url: "content/platform/l3/platform-signage.md"}, {type: repo, label: "TypeScript — a typed tRPC service boundary", url: "content/assets/code/04-typescript-service-layer.ts"}]
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [platform, devops, nixos, docker, backups, networking]
|
||||
---
|
||||
|
||||
**Capability statement.** Four hosts, fifty or more containers, configuration in version control instead of in a shell history, and one place where traffic enters. The estate is deliberately small because it is run rather than demonstrated.
|
||||
|
||||
## What is actually run
|
||||
|
||||
**Declarative configuration.** NixOS is the base on the machines that matter, with the desktop and the terminal replicated across them so an environment can be rebuilt from a description. Proxmox runs the virtualised side — Ubuntu file server and a Home Assistant appliance alongside the desktop hosts. The point of the arrangement is repeatability: a machine is a configuration, not a pet.
|
||||
|
||||
**Packaging.** When a tool is missing or stale in nixpkgs, it gets a derivation rather than a manual install. The TUIOS package pins 0.10.1 over nixpkgs' 0.7.0 with `fetchurl` and a verified hash, documents why the override exists, and states the condition for deleting it. A patch with an exit plan is maintenance; a patch without one is debt.
|
||||
|
||||
**Container estate.** Fifty or more containers run across the hosts — the file server and media services, the automation layer, the AI services, traffic for public ingress, and the tools used to build all of it. Each new service is given its own port, its own folder and a memory cap equal to its swap cap, so it cannot take the host's swap.
|
||||
|
||||
**Networking.** Remote access is a private mesh rather than an exposed SSH port: Tailscale across the machines, SSH on every host, and a phone used as a terminal. Services bind to loopback; Caddy is the only public listener, terminating TLS at the edge host and proxying the sites that are allowed to be seen.
|
||||
|
||||
**Storage and backups.** Three tiers, each covering a different failure: local snapshots on the machine, a copy on another machine over the network, and an offsite copy in object storage. Retention is stated in the job (daily, weekly, monthly, annual) and a restore runbook sits next to the jobs, because a backup nobody has restored from is an assumption.
|
||||
|
||||
**Observability and operations.** Resource use is watched at the host level, because the runtime host is memory-bound and a new service can destabilise the ones people rely on. The agent fleet is observed as a fleet through TUIOS — sessions, state and approvals in one console — rather than by tailing logs on four machines.
|
||||
|
||||
**Signage infrastructure.** The display work is treated as a fleet to be managed: HDMI-CEC control for power, rotation, mirroring and multi-screen layout, so screens are driven by the platform rather than by a person with a remote control.
|
||||
|
||||
## Why this exhibit is good code
|
||||
|
||||
The Prisma schema defines the stored types, and the tRPC router validates input with Zod at the boundary, so the data layer receives a shape it can trust rather than one it hopes for. Each procedure is a thin call on the database context, which keeps the router a description of the API rather than a second copy of the queries.
|
||||
|
||||
## Evidence
|
||||
|
||||
| Claim | Evidence | Artefact |
|
||||
|---|---|---|
|
||||
| Configuration is declarative and replicated | NixOS hosts with shared configuration; Proxmox for the virtualised side | [Declarative configuration](l3/platform-nixos.md) |
|
||||
| Tooling is packaged rather than hand-installed | `pkgs/tuios.nix` pinning 0.10.1 with a documented removal condition | [Nix package](l3/platform-tuios-package.md) |
|
||||
| Fifty or more containers are operated | Inventory by host and by purpose | [Container inventory](l3/platform-containers.md) |
|
||||
| Backups are three-tier with a restore runbook | Borg on two hosts, Kopia and Backrest on the edge host, offsite object storage | [Backups](l3/platform-backups.md) |
|
||||
| One public listener | Loopback-bound services behind Caddy at the edge | [Network](l3/platform-network.md) |
|
||||
| Screens are managed as a fleet | HDMI-CEC control of power, rotation and layout | [Signage infrastructure](l3/platform-signage.md) |
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **Memory over hardware.** The runtime host runs 15 GiB with little free and swap in use. The response was caps, fewer and smaller services, and moving heavy work to the gateway rather than buying another machine. Every new service is sized before it is deployed, and the ceiling for this site is one gigabyte and four containers.
|
||||
- **Self-hosting is a choice with a cost.** Monitoring, backups, ingress and the AI gateway are all operated here, which means the failure modes are also operated here. The mitigation is documentation and measurement, not optimism.
|
||||
- **Rejected on purpose.** A long list of tools was evaluated and refused — Airflow for RAM, self-hosted Langfuse for six containers it could not afford, LiteLLM because it would duplicate the existing gateway, and a pile of documentation platforms. "Alternatives considered" is kept because the reasoning is the senior artifact.
|
||||
- **Not everything is automated.** Some operations are still a documented command run by a person. The runbook exists precisely because the automations do not cover everything.
|
||||
16
content/platform/platform-overview.md
Normal file
16
content/platform/platform-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: platform-overview
|
||||
area: platform
|
||||
level: 1
|
||||
title: "Platform & Infrastructure"
|
||||
claim: "Sam runs a multi-machine NixOS and Proxmox estate sized to the hardware rather than the budget: fifty-plus containers, declarative configuration, encrypted backups with a written restore runbook, and public ingress."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [NixOS, Proxmox, Docker, Caddy, Tailscale, Borg, Kopia, AWS S3]
|
||||
metrics: [{label: "containers in the estate", value: 50}, {label: "hosts", value: 4}, {label: "backup tiers", value: "Borg · Kopia · AWS"}]
|
||||
artefacts: [{type: doc, label: "Declarative multi-host configuration", url: "content/platform/l3/platform-nixos.md"}, {type: doc, label: "Backups and restore runbook", url: "content/platform/l3/platform-backups.md"}, {type: doc, label: "A hand-written Nix package with an exit plan", url: "content/platform/l3/platform-tuios-package.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [platform, devops, nixos, docker, backups]
|
||||
---
|
||||
The runtime host is memory-bound rather than CPU-bound, so every service carries a memory cap and is bound to loopback. What runs, where, and what was rejected is on the [estate page](platform-estate.md).
|
||||
33
content/products/l3/products-signage.md
Normal file
33
content/products/l3/products-signage.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: products-signage
|
||||
area: products
|
||||
level: 3
|
||||
title: "Artefact — Commercial signage on commodity hardware"
|
||||
claim: "A digital signage platform built as market disruption — any HDMI screen · a small player · fleet control of power · rotation · layout and playback · sold to twelve businesses over time."
|
||||
confidence: delivered
|
||||
period: "2014–"
|
||||
tech: [Raspberry Pi, ESP32, HDMI-CEC, JavaScript, three.js, FFmpeg, Python]
|
||||
metrics: [{label: "businesses currently running the product", value: 3}, {label: "adoptions over time", value: 12}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [products, signage, artefact]
|
||||
---
|
||||
|
||||
The product shipped and customers still run it. The rebuild on the current stack is not finished and is labelled as unfinished everywhere it is mentioned.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The product in use: a player on commodity hardware — a small Raspberry Pi or a microcontroller class device — driving any HDMI screen, with fleet control from the platform: switch screens on and off, rotate, mirror, lay out multiple screens, coordinate boards, and synchronise playback across a site. Content is built by the customer from complex animation down to a simple image rotation, and the system adapts to the board it finds rather than demanding a specification.
|
||||
|
||||
## What it proves
|
||||
|
||||
The commercial read: a market was overpriced, the substitute was built on commodity hardware, and it was sold and operated for years. Twelve adoptions over time, three businesses still running. That is a product, not a project.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
Twelve is cumulative adoption and three is the current count — the distinction is kept because inflating it would be the easiest lie on this site. The rebuild is in progress. No revenue figures or customer names beyond type of business are published.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7 for screenshots and a demonstration recording, captured from the running product. The one-liners for reference customers are approved for the salon and the financial services business only.
|
||||
39
content/products/l3/products-websitebuilder.md
Normal file
39
content/products/l3/products-websitebuilder.md
Normal file
@@ -0,0 +1,39 @@
|
||||
---
|
||||
id: products-websitebuilder
|
||||
area: products
|
||||
level: 3
|
||||
title: "Artefact — The site builder behind the signage"
|
||||
claim: "A templated site builder and CMS with media editors — drawing · image · video · text animation · 3D import — that also produced the marketing sites and the social output."
|
||||
confidence: delivered
|
||||
period: "2014–"
|
||||
tech: [PHP, JavaScript, three.js, FFmpeg, ImageMagick, MySQL]
|
||||
metrics: [{label: "media editors in the product", value: "drawing · image · video · text animation · transitions · 3D import"}]
|
||||
artefacts: [{type: repo, label: "Text-effects engine — easing functions and the timeline loop", url: "content/assets/code/02-js-easing-core.js"}, {type: repo, label: "Media encoder — one GIF encode operation and its FFmpeg commands", url: "content/assets/code/03-php-encode-op.php"}]
|
||||
public: true
|
||||
last_verified: 2026-10-28
|
||||
tags: [products, cms, media, artefact]
|
||||
---
|
||||
|
||||
**Status: delivered, retained.** Retired as a general-purpose product; kept as the content engine for the signage.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
The templated builder: pick a template, build the site, publish it — with editors for drawing, images, video and text animation, video effects and transitions, 3D object import and text extrusion, plus document and office-format handling. Image animation is composed in JavaScript and HTML and rendered on Raspberry Pi hardware through Python. Around the site itself sat the commercial scaffolding a small business actually needs: shop, invoicing, room and event management, presentations, document management, client records and bulk email and SMS.
|
||||
|
||||
## What it proves
|
||||
|
||||
Two things at once — depth in media pipelines (FFmpeg, ImageMagick, PDF and office formats, three.js) and the discipline of shipping a general product that strangers could operate without help.
|
||||
|
||||
## Why these exhibits are good code
|
||||
|
||||
The animation engine evaluates a named easing function to give each letter its delay, so the spacing of an animation follows the same curve as its motion instead of a guessed linear stagger. Both directions build from one transform object, which is what stops the two halves of the same effect drifting apart over years of edits.
|
||||
|
||||
The media pipeline checks the real MIME type before it builds a command, so a wrong input returns a message instead of a half-written file. The GIF encode takes two passes — a palette from the clip, then frames mapped onto it — which is the difference between a GIF that holds its colour and one that does not.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
The PHP is 2014-era procedural code: global state, interpolated shell commands, and long functions. The exhibit is here for the operational judgement, not as a model of structure. The surface was too large for one person to support indefinitely; the plan recorded here is to strip it back to signage plus the content engine and stop selling the rest, which is a decision about focus rather than a claim of failure.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
Phase 7. Both code exhibits are published at `content/assets/code/`; a screen recording of the editors is still to come. Source: résumé (2014–2020 freelance) and evidence notes section 3.
|
||||
33
content/products/l3/products-where-woof.md
Normal file
33
content/products/l3/products-where-woof.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: products-where-woof
|
||||
area: products
|
||||
level: 3
|
||||
title: "Artefact — Where-Woof · deployed and pre-market"
|
||||
claim: "A QR and RFID tag system for lost property that is deployed and in use — and has not gone to market."
|
||||
confidence: running
|
||||
period: "2020–"
|
||||
tech: [QR, RFID, PHP, JavaScript]
|
||||
metrics: [{label: "market launch", value: 0}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [products, where-woof, artefact]
|
||||
---
|
||||
|
||||
Deployed is true, commercial is not — that is the exact wording the site uses, deliberately.
|
||||
|
||||
## What the artefact is
|
||||
|
||||
A tag system for lost property. A tag carries a QR code and an RFID identity; whoever finds the item resolves the tag and the owner is contacted. The system is running and being deployed now.
|
||||
|
||||
## What it proves
|
||||
|
||||
That a product can be taken into the field before it is monetised, and that the honest label for that state exists. The distinction matters on a CV because the alternative — calling a pre-launch product commercial — is the kind of claim a technical interviewer unpicks in one question.
|
||||
|
||||
## Limits stated on the page
|
||||
|
||||
No customers, no revenue and no launch date are claimed. The metric recorded here is a zero on purpose.
|
||||
|
||||
## When it is delivered
|
||||
|
||||
The tag and resolve-flow photographs land in Phase 7 if Sam chooses to publish them. Product name and description are approved; commercial framing is not.
|
||||
58
content/products/products-business.md
Normal file
58
content/products/products-business.md
Normal file
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: products-business
|
||||
area: products
|
||||
level: 2
|
||||
title: "Software sold and operated — with the status stated"
|
||||
claim: "Three product lines from one workshop — signage on commodity hardware · the site builder that produced the marketing · and a lost-property tag system — with three businesses still running the signage today."
|
||||
confidence: running
|
||||
period: "2014–"
|
||||
tech: [PHP, JavaScript, three.js, FFmpeg, ImageMagick, HDMI-CEC, Raspberry Pi, ESP32, QR, RFID]
|
||||
metrics: [{label: "businesses currently running the product", value: 3}, {label: "adoptions over time", value: 12}, {label: "product lines", value: 3}]
|
||||
artefacts: [{type: doc, label: "Commercial signage on commodity hardware", url: "content/products/l3/products-signage.md"}, {type: doc, label: "The site builder behind it", url: "content/products/l3/products-websitebuilder.md"}, {type: doc, label: "Where-Woof — deployed and pre-market", url: "content/products/l3/products-where-woof.md"}, {type: doc, label: "Signage infrastructure — screens as a managed fleet", url: "content/platform/l3/platform-signage.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [products, business, signage, where-woof, positioning]
|
||||
---
|
||||
|
||||
**Capability statement.** Building software for a client and building software as a product are different jobs. This page is the second one: a market that was overpriced, a substitute built on hardware that costs a fraction of the incumbent systems, customers who adopted it, and the operational cost of keeping it running.
|
||||
|
||||
## The commercial story
|
||||
|
||||
The digital signage market sells walls: thousands of dollars of hardware with fans, cooling and glass, locked to monopoly software. The product was built as a deliberate inversion of that — a system that runs on a small Raspberry Pi or a microcontroller, drives any HDMI screen including an ordinary monitor, and adapts to the board rather than requiring the board to adapt to it.
|
||||
|
||||
It manages screens as a fleet: power on and off, rotation, mirroring, multi-screen layout, coordinated boards, and synchronised playback across screens in one site or across sites. Users build complex animations or simple image rotations, and the system presents whatever suits the display it finds.
|
||||
|
||||
Demonstrations were rolled out in a hair salon and a financial services business. Around twelve businesses adopted it over time and three still run it today. That number is deliberately specific: twelve is cumulative adoption, three is current, and the difference is the honest measure of a product that was sold but not scaled.
|
||||
|
||||
The same workshop produced the CMS behind the marketing — a templated site builder with editors for drawing, image, video and text animation, video effects and transitions, 3D object import and text extrusion — built on JavaScript and three.js with FFmpeg, ImageMagick and PHP tools for documents, and rendering image animation on Raspberry Pi hardware.
|
||||
|
||||
The third product is different in kind: Where-Woof is a QR and RFID tag system for lost property. It is deployed and being deployed now. It has **not** gone to market, and this page will not call it commercial until it has.
|
||||
|
||||
## Evidence
|
||||
|
||||
| Claim | Evidence | Artefact |
|
||||
|---|---|---|
|
||||
| Signage replaced thousand-dollar wall systems with commodity hardware | Any HDMI screen · Raspberry Pi or ESP32 class player · fleet control | [Signage product](l3/products-signage.md) |
|
||||
| Twelve adoptions over time, three businesses running now | Adoption history and current customers | [Signage product](l3/products-signage.md) |
|
||||
| A templated builder produced the marketing sites | Editors for drawing · video · image and animation; FFmpeg and ImageMagick pipeline | [WebsiteBuilder](l3/products-websitebuilder.md) |
|
||||
| Where-Woof is deployed and pre-market | Device and tag system in use; no market launch | [Where-Woof](l3/products-where-woof.md) |
|
||||
| Screens are managed as a fleet | HDMI-CEC control of power · rotation · layout | [Signage infrastructure](../platform/l3/platform-signage.md) |
|
||||
|
||||
## Product status — stated plainly
|
||||
|
||||
| Product | What it is | Status | Next |
|
||||
|---|---|---|---|
|
||||
| Display screen media (Audas Media) | Digital signage on commodity hardware with multi-screen fleet control | Delivered · three businesses running · being rebuilt with AI | Strip back to signage only and rebuild on the current stack |
|
||||
| WebsiteBuilder | Templated site builder and CMS with media editors and social output | Delivered · retained | Retired as a general product; kept as the content engine |
|
||||
| Where-Woof | QR and RFID tags for lost property | Deployed · pre-market | Take to market — not done yet |
|
||||
|
||||
## Trade-offs and limits
|
||||
|
||||
- **Scope discipline came late.** The builder carried a shop, invoicing, room management, calendars, presentations, documents, bulk messaging and client management. Product surface grew faster than a one-person support model could carry, which is why the plan now is to keep the signage and the content engine and retire the rest.
|
||||
- **The rebuild is unfinished.** "Rebuilding with AI" is a statement of work in progress, not a launch. It is labelled as such on every page that mentions it.
|
||||
- **One product is pre-revenue by design.** Where-Woof is deployed before it is sold. Describing it as commercial would be a lie that a careful reader would find in a minute.
|
||||
- **Support is the hidden cost.** Customers running the product for years is the strongest evidence of value and the strongest argument for keeping the surface small.
|
||||
|
||||
Signage infrastructure is also platform work: [screens as a managed fleet](../platform/l3/platform-signage.md).
|
||||
|
||||
Each product carries its own status, and the two caveats — a rebuild in progress and a launch not yet made — are in the table above rather than in a footnote.
|
||||
16
content/products/products-overview.md
Normal file
16
content/products/products-overview.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
id: products-overview
|
||||
area: products
|
||||
level: 1
|
||||
title: "Products"
|
||||
claim: "Sam identified an overpriced market and shipped a cheaper substitute on commodity hardware: twelve adoptions over time and three businesses still running it."
|
||||
confidence: running
|
||||
period: "2014–"
|
||||
tech: [PHP, JavaScript, three.js, FFmpeg, ImageMagick, HDMI-CEC, Raspberry Pi, ESP32]
|
||||
metrics: [{label: "businesses currently running the product", value: 3}, {label: "adoptions over time", value: 12}, {label: "product lines", value: 3}]
|
||||
artefacts: [{type: doc, label: "Commercial signage on commodity hardware", url: "content/products/l3/products-signage.md"}, {type: doc, label: "The site builder behind it", url: "content/products/l3/products-websitebuilder.md"}, {type: doc, label: "Where-Woof — deployed and pre-market", url: "content/products/l3/products-where-woof.md"}]
|
||||
public: true
|
||||
last_verified: 2026-10-26
|
||||
tags: [products, signage, business, where-woof]
|
||||
---
|
||||
Building, selling, deploying and still supporting software for other businesses is a different discipline from writing it, and the caveats on these three products are stated rather than hidden. The commercial story is on the [products page](products-business.md).
|
||||
22
corpus/__init__.py
Normal file
22
corpus/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""Phase 4 — the retrievable corpus.
|
||||
|
||||
`content/` is the source of truth. This package turns it into the two indexes
|
||||
the AI console reads:
|
||||
|
||||
* **vectors** in Qdrant, one point per chunk, with the payload that lets the
|
||||
query path filter *before* searching (`area`, `confidence`);
|
||||
* **keywords** in SQLite FTS5, one row per chunk, so exact tokens (repo names,
|
||||
`LoRaWAN`, `MQTT`, dates) are not lost the way they are with vectors alone
|
||||
(decision D10, hybrid retrieval).
|
||||
|
||||
Everything here is driven by `python3 -m corpus.cli`; `docs/phase-4-notes.md`
|
||||
is the operator manual and records what was measured. The pipeline is
|
||||
Provider-agnostic by design: `corpus/embeddings.py` is the only place that knows
|
||||
how a vector is made, and the provider + model + dimension are recorded per
|
||||
collection so a provider change is a governed migration, not silent corruption.
|
||||
|
||||
Standard library only, plus the repository's own validator (`tools/`). No new
|
||||
dependency, no Node, no framework (AGENTS.md section 4.1).
|
||||
"""
|
||||
|
||||
__all__ = ["config", "loader", "chunker", "payloads", "embeddings", "qdrant", "store", "pipeline", "freshness"]
|
||||
8
corpus/__main__.py
Normal file
8
corpus/__main__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""`python3 -m corpus` is the same as `python3 -m corpus.cli`."""
|
||||
|
||||
import sys
|
||||
|
||||
from .cli import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
287
corpus/chunker.py
Normal file
287
corpus/chunker.py
Normal file
@@ -0,0 +1,287 @@
|
||||
"""Code-aware chunking.
|
||||
|
||||
The rules come from `docs/phase-4-corpus.md` §4.1:
|
||||
|
||||
* 400–800 tokens per chunk, about 12% overlap;
|
||||
* **never split inside a fenced block, a table or a front-matter block.**
|
||||
|
||||
The last rule is structural here, not a post-hoc check: the body is parsed into
|
||||
*blocks* first and chunks are packed out of whole blocks, so a boundary can only
|
||||
ever fall between blocks. Fenced code, tables, headings, list items and
|
||||
paragraphs are all blocks; the large ones cannot be subdivided.
|
||||
|
||||
Front-matter is removed by `corpus.loader` before this module sees the body, and
|
||||
parsing a body that still has front-matter is an error, so it cannot be split
|
||||
either.
|
||||
|
||||
`tokens` are estimated with a word-and-punctuation regex. A real BPE tokenizer
|
||||
counts 15–30% more for prose and more again for code, so a chunk that measures
|
||||
800 here is a conservative upper bound.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
# Packing targets (docs/phase-4-corpus.md §4.1).
|
||||
TOKEN_TARGET = 600
|
||||
TOKEN_MAX = 800
|
||||
TOKEN_MIN = 400
|
||||
OVERLAP_RATIO = 0.12
|
||||
|
||||
FENCE_RE = re.compile(r"^ {0,3}(`{3,}|~{3,})")
|
||||
HEADING_RE = re.compile(r"^ {0,3}#{1,6}\s")
|
||||
LIST_RE = re.compile(r"^(\s*)([-*+]|\d{1,3}[.)])\s+")
|
||||
RULE_RE = re.compile(r"^ {0,3}([-*_])(\s*\1){2,}\s*$")
|
||||
TOKEN_RE = re.compile(r"\w+|[^\w\s]", re.UNICODE)
|
||||
|
||||
PROTECTED_KINDS = ("fence", "table")
|
||||
|
||||
|
||||
class ChunkError(RuntimeError):
|
||||
"""The body could not be chunked without breaking the rules."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Block:
|
||||
"""An indivisible unit of Markdown. Never split, only packed."""
|
||||
|
||||
kind: str # fence | table | heading | list | quote | para | rule
|
||||
text: str
|
||||
tokens: int
|
||||
line_start: int # 1-based, relative to the body this block came from
|
||||
line_end: int
|
||||
|
||||
@property
|
||||
def protected(self) -> bool:
|
||||
return self.kind in PROTECTED_KINDS
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Chunk:
|
||||
index: int
|
||||
text: str
|
||||
tokens: int
|
||||
line_start: int
|
||||
line_end: int
|
||||
blocks: tuple
|
||||
oversize: bool = False
|
||||
|
||||
@property
|
||||
def kinds(self) -> tuple:
|
||||
return tuple(block.kind for block in self.blocks)
|
||||
|
||||
|
||||
def estimate_tokens(text: str) -> int:
|
||||
"""Word-plus-punctuation token estimate. Deterministic and cheap."""
|
||||
return len(TOKEN_RE.findall(text))
|
||||
|
||||
|
||||
def parse_blocks(body: str) -> list:
|
||||
"""Split a Markdown body into atomic blocks.
|
||||
|
||||
Raises ChunkError if the body still carries a front-matter block: that is a
|
||||
loader bug, and silently chunking it would split something that must not be
|
||||
split.
|
||||
"""
|
||||
if body.lstrip().startswith("---"):
|
||||
raise ChunkError(
|
||||
"body still starts with a front-matter block; strip front-matter "
|
||||
"before chunking (corpus.loader does this)"
|
||||
)
|
||||
|
||||
lines = body.split("\n")
|
||||
blocks = []
|
||||
i = 0
|
||||
total = len(lines)
|
||||
while i < total:
|
||||
raw = lines[i]
|
||||
stripped = raw.strip()
|
||||
if not stripped:
|
||||
i += 1
|
||||
continue
|
||||
start = i
|
||||
fence = FENCE_RE.match(raw)
|
||||
if fence:
|
||||
i = _read_fence(lines, i, fence.group(1))
|
||||
kind = "fence"
|
||||
elif stripped.startswith("|"):
|
||||
while i < total and lines[i].strip().startswith("|"):
|
||||
i += 1
|
||||
kind = "table"
|
||||
elif HEADING_RE.match(raw):
|
||||
i += 1
|
||||
kind = "heading"
|
||||
elif RULE_RE.match(raw):
|
||||
i += 1
|
||||
kind = "rule"
|
||||
elif LIST_RE.match(raw):
|
||||
i = _read_list_item(lines, i)
|
||||
kind = "list"
|
||||
elif stripped.startswith(">"):
|
||||
while i < total and lines[i].strip().startswith(">"):
|
||||
i += 1
|
||||
kind = "quote"
|
||||
else:
|
||||
i = _read_paragraph(lines, i)
|
||||
kind = "para"
|
||||
text = "\n".join(lines[start:i])
|
||||
blocks.append(
|
||||
Block(kind=kind, text=text, tokens=estimate_tokens(text), line_start=start + 1, line_end=i)
|
||||
)
|
||||
return blocks
|
||||
|
||||
|
||||
def _read_fence(lines: list, i: int, marker: str) -> int:
|
||||
"""Return the index just past a fenced block. Unterminated runs to EOF."""
|
||||
char = marker[0]
|
||||
width = len(marker)
|
||||
i += 1
|
||||
while i < len(lines):
|
||||
candidate = FENCE_RE.match(lines[i])
|
||||
if candidate and candidate.group(1)[0] == char and len(candidate.group(1)) >= width:
|
||||
return i + 1
|
||||
i += 1
|
||||
return len(lines)
|
||||
|
||||
|
||||
def _read_list_item(lines: list, i: int) -> int:
|
||||
"""Return the index just past one top-level list item, continuations included."""
|
||||
base_indent = len(LIST_RE.match(lines[i]).group(1))
|
||||
i += 1
|
||||
while i < len(lines):
|
||||
raw = lines[i]
|
||||
if not raw.strip():
|
||||
# A blank line continues the item only if an indented line follows.
|
||||
if i + 1 < len(lines) and lines[i + 1].startswith(" " * (base_indent + 1)):
|
||||
i += 1
|
||||
continue
|
||||
return i
|
||||
indent = len(raw) - len(raw.lstrip())
|
||||
if indent > base_indent or LIST_RE.match(raw) and len(LIST_RE.match(raw).group(1)) > base_indent:
|
||||
i += 1
|
||||
continue
|
||||
if LIST_RE.match(raw):
|
||||
return i
|
||||
if indent <= base_indent:
|
||||
return i
|
||||
i += 1
|
||||
return i
|
||||
|
||||
|
||||
def _read_paragraph(lines: list, i: int) -> int:
|
||||
i += 1
|
||||
while i < len(lines):
|
||||
raw = lines[i]
|
||||
stripped = raw.strip()
|
||||
if not stripped:
|
||||
return i
|
||||
if FENCE_RE.match(raw) or HEADING_RE.match(raw) or RULE_RE.match(raw):
|
||||
return i
|
||||
if stripped.startswith("|") or stripped.startswith(">") or LIST_RE.match(raw):
|
||||
return i
|
||||
i += 1
|
||||
return i
|
||||
|
||||
|
||||
def chunk_blocks(blocks: list) -> list:
|
||||
"""Pack blocks into windows of at most TOKEN_MAX, with ~12% overlap."""
|
||||
chunks = []
|
||||
total = len(blocks)
|
||||
if total == 0:
|
||||
return chunks
|
||||
i = 0
|
||||
while i < total:
|
||||
start = i
|
||||
tokens = 0
|
||||
end = i
|
||||
while end < total:
|
||||
block = blocks[end]
|
||||
if end > start:
|
||||
if tokens + block.tokens > TOKEN_MAX:
|
||||
break
|
||||
# Prefer breaking in front of a heading.
|
||||
if block.kind == "heading" and tokens >= int(TOKEN_TARGET * 0.75):
|
||||
break
|
||||
tokens += block.tokens
|
||||
end += 1
|
||||
if tokens >= TOKEN_TARGET and end < total and blocks[end].kind == "heading":
|
||||
break
|
||||
window = blocks[start:end]
|
||||
chunks.append(_make_chunk(len(chunks), window))
|
||||
if end >= total:
|
||||
break
|
||||
# Back up over trailing blocks until roughly OVERLAP_RATIO of the window
|
||||
# repeats, leaving at least one block behind so the walk always advances.
|
||||
budget = max(1, int(round(TOKEN_TARGET * OVERLAP_RATIO)))
|
||||
carried = 0
|
||||
back = end
|
||||
while back - 1 > start and carried + blocks[back - 1].tokens <= budget * 2:
|
||||
carried += blocks[back - 1].tokens
|
||||
back -= 1
|
||||
if carried >= budget:
|
||||
break
|
||||
i = max(back, start + 1)
|
||||
return chunks
|
||||
|
||||
|
||||
def _make_chunk(index: int, window: list) -> Chunk:
|
||||
text = "\n\n".join(block.text for block in window)
|
||||
return Chunk(
|
||||
index=index,
|
||||
text=text,
|
||||
tokens=sum(block.tokens for block in window),
|
||||
line_start=window[0].line_start,
|
||||
line_end=window[-1].line_end,
|
||||
blocks=tuple(window),
|
||||
oversize=len(window) == 1 and window[0].tokens > TOKEN_MAX,
|
||||
)
|
||||
|
||||
|
||||
def chunk_document(body: str) -> list:
|
||||
"""Chunk one document body. The pipeline calls this and nothing else."""
|
||||
chunks = chunk_blocks(parse_blocks(body))
|
||||
verify_no_split(chunks, protected_regions(body))
|
||||
return chunks
|
||||
|
||||
|
||||
def protected_regions(body: str) -> list:
|
||||
"""Line ranges (1-based, inclusive) that a chunk boundary must not cross.
|
||||
|
||||
Recomputed from the body text alone, independently of the packing code, so
|
||||
the check is not the implementation grading its own homework.
|
||||
"""
|
||||
regions = []
|
||||
lines = body.split("\n")
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
raw = lines[i]
|
||||
fence = FENCE_RE.match(raw)
|
||||
if fence:
|
||||
end = _read_fence(lines, i, fence.group(1))
|
||||
regions.append((i + 1, end))
|
||||
i = end
|
||||
continue
|
||||
if raw.strip().startswith("|"):
|
||||
start = i
|
||||
while i < len(lines) and lines[i].strip().startswith("|"):
|
||||
i += 1
|
||||
regions.append((start + 1, i))
|
||||
continue
|
||||
i += 1
|
||||
return regions
|
||||
|
||||
|
||||
def verify_no_split(chunks: list, regions: list) -> None:
|
||||
"""Raise ChunkError if any chunk boundary cuts a protected region."""
|
||||
for chunk in chunks:
|
||||
for first, last in regions:
|
||||
spans_start = chunk.line_start <= first <= chunk.line_end
|
||||
spans_end = chunk.line_start <= last <= chunk.line_end
|
||||
covers = chunk.line_start <= first and chunk.line_end >= last
|
||||
if (spans_start or spans_end) and not covers:
|
||||
raise ChunkError(
|
||||
f"chunk {chunk.index} splits protected lines {first}-{last} "
|
||||
f"(chunk covers {chunk.line_start}-{chunk.line_end})"
|
||||
)
|
||||
257
corpus/cli.py
Normal file
257
corpus/cli.py
Normal file
@@ -0,0 +1,257 @@
|
||||
"""Command line entry point: `python3 -m corpus.cli <command>`.
|
||||
|
||||
Commands
|
||||
|
||||
sync incremental update of the corpus (the normal command)
|
||||
rebuild drop the collection and rebuild it from content/ (recovery)
|
||||
audit freshness audit; reports stale documents, index drift
|
||||
verify does the index agree with content/ and with Qdrant?
|
||||
demo-filter the same query with and without an `area` filter
|
||||
hybrid vectors + FTS5 fused with reciprocal rank fusion
|
||||
measure Qdrant memory at 500 / 5,000 / 20,000 chunks
|
||||
info what this build thinks it is configured to do
|
||||
|
||||
Exit codes: 0 success, 1 findings or verification problems, 2 configuration or
|
||||
usage error. Every command accepts `--json` where a machine-readable result is
|
||||
useful (Prefect reads JSON, a human reads the table).
|
||||
|
||||
Configuration is environment-first (`corpus/config.py`); the flags exist so a
|
||||
one-off run can point at a scratch Qdrant without exporting variables.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from . import config as config_module
|
||||
from . import freshness, pipeline, store as store_module
|
||||
from .config import Config, ConfigError
|
||||
from .qdrant import Qdrant, QdrantError
|
||||
|
||||
|
||||
def main(argv=None) -> int:
|
||||
parser = _build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
if args.command is None:
|
||||
parser.print_help()
|
||||
return 2
|
||||
|
||||
overrides = {
|
||||
"root": args.root,
|
||||
"qdrant_url": args.qdrant_url,
|
||||
"collection": args.collection,
|
||||
"provider": args.provider,
|
||||
"model": args.model,
|
||||
"dim": args.dim,
|
||||
"db_path": args.db,
|
||||
"data_dir": args.data,
|
||||
}
|
||||
try:
|
||||
cfg = Config.from_env(**overrides)
|
||||
except ConfigError as exc:
|
||||
print(f"configuration error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
log = (lambda _msg: None) if args.quiet else (lambda msg: print(msg, file=sys.stderr))
|
||||
|
||||
try:
|
||||
handler = {
|
||||
"sync": _cmd_sync,
|
||||
"rebuild": _cmd_rebuild,
|
||||
"audit": _cmd_audit,
|
||||
"verify": _cmd_verify,
|
||||
"demo-filter": _cmd_demo_filter,
|
||||
"hybrid": _cmd_hybrid,
|
||||
"measure": _cmd_measure,
|
||||
"info": _cmd_info,
|
||||
}[args.command]
|
||||
return handler(cfg, args, log)
|
||||
except (pipeline.PipelineError, ConfigError) as exc:
|
||||
print(f"error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
except QdrantError as exc:
|
||||
print(f"error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
|
||||
def _build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="python3 -m corpus.cli",
|
||||
description="Build and audit the Ai Resume retrieval corpus (docs/phase-4-corpus.md).",
|
||||
)
|
||||
parser.add_argument("--root", help="repository root (default: parent of corpus/)")
|
||||
parser.add_argument("--qdrant-url", help="Qdrant REST URL (default http://127.0.0.1:6333)")
|
||||
parser.add_argument("--collection", help="Qdrant collection name (default resume_corpus)")
|
||||
parser.add_argument("--provider", choices=sorted(config_module.PROVIDERS), help="embedding provider")
|
||||
parser.add_argument("--model", help="embedding model name")
|
||||
parser.add_argument("--dim", type=int, help="embedding dimension")
|
||||
parser.add_argument("--db", help="SQLite corpus database path")
|
||||
parser.add_argument("--data", help="corpus data directory")
|
||||
parser.add_argument("--quiet", action="store_true", help="progress on stderr only becomes silent")
|
||||
|
||||
sub = parser.add_subparsers(dest="command")
|
||||
|
||||
sync = sub.add_parser("sync", help="incremental update")
|
||||
sync.add_argument("--json", action="store_true")
|
||||
|
||||
rebuild = sub.add_parser("rebuild", help="drop the collection and rebuild from content/")
|
||||
rebuild.add_argument("--json", action="store_true")
|
||||
|
||||
audit = sub.add_parser("audit", help="freshness and drift audit")
|
||||
audit.add_argument("--json", action="store_true")
|
||||
audit.add_argument("--today", help="audit as of this date (YYYY-MM-DD), for tests")
|
||||
audit.add_argument("--fail-on-findings", action="store_true", help="exit 1 when anything is found")
|
||||
audit.add_argument("--alert", action="store_true", help="POST the report to RESUME_APPRISE_URL")
|
||||
|
||||
verify = sub.add_parser("verify", help="check the index against content/ and Qdrant")
|
||||
verify.add_argument("--json", action="store_true")
|
||||
|
||||
demo = sub.add_parser("demo-filter", help="same query with and without an area filter")
|
||||
demo.add_argument("--query", required=True)
|
||||
demo.add_argument("--area", help="area to filter on (ai, platform, edge, products, history, about)")
|
||||
demo.add_argument("--limit", type=int, default=5)
|
||||
demo.add_argument("--json", action="store_true")
|
||||
|
||||
hybrid = sub.add_parser("hybrid", help="vectors + FTS5 fused with RRF")
|
||||
hybrid.add_argument("--query", required=True)
|
||||
hybrid.add_argument("--area")
|
||||
hybrid.add_argument("--limit", type=int, default=5)
|
||||
hybrid.add_argument("--json", action="store_true")
|
||||
|
||||
measure = sub.add_parser("measure", help="Qdrant memory at three corpus sizes")
|
||||
measure.add_argument("--sizes", default="500,5000,20000")
|
||||
measure.add_argument("--container", default="", help="Qdrant container name for docker stats")
|
||||
measure.add_argument("--keep", action="store_true", help="keep the measurement collection")
|
||||
measure.add_argument("--json", action="store_true")
|
||||
|
||||
sub.add_parser("info", help="print the resolved configuration")
|
||||
return parser
|
||||
|
||||
|
||||
# -- commands --------------------------------------------------------------
|
||||
|
||||
|
||||
def _cmd_sync(cfg, args, log) -> int:
|
||||
stats = pipeline.run(cfg, mode="sync", log=log)
|
||||
if args.json:
|
||||
print(json.dumps(stats.to_dict(), indent=2, sort_keys=True))
|
||||
else:
|
||||
print(stats.summary())
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_rebuild(cfg, args, log) -> int:
|
||||
stats = pipeline.run(cfg, mode="rebuild", log=log)
|
||||
if args.json:
|
||||
print(json.dumps(stats.to_dict(), indent=2, sort_keys=True))
|
||||
else:
|
||||
print(stats.summary())
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_audit(cfg, args, log) -> int:
|
||||
today = datetime.date.fromisoformat(args.today) if args.today else None
|
||||
report = freshness.audit(cfg, today=today, log=log)
|
||||
if args.json:
|
||||
print(json.dumps(report, indent=2, sort_keys=True))
|
||||
else:
|
||||
print(freshness.render_text(report))
|
||||
if args.alert:
|
||||
try:
|
||||
sent = freshness.alert(report, cfg.apprise_url)
|
||||
if not sent:
|
||||
log("alert: RESUME_APPRISE_URL is not set; nothing sent")
|
||||
except Exception as exc: # noqa: BLE001 - an alert failure must not hide the audit
|
||||
print(f"warning: could not send the Apprise alert: {exc}", file=sys.stderr)
|
||||
if args.fail_on_findings and not report["ok"]:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_verify(cfg, args, log) -> int:
|
||||
problems = pipeline.verify(cfg, log=log)
|
||||
if args.json:
|
||||
print(json.dumps({"ok": not problems, "problems": problems}, indent=2))
|
||||
else:
|
||||
if problems:
|
||||
print(f"FAIL: {len(problems)} problem(s)")
|
||||
for problem in problems:
|
||||
print(f" - {problem}")
|
||||
else:
|
||||
print("OK: the index agrees with content/ and Qdrant")
|
||||
return 1 if problems else 0
|
||||
|
||||
|
||||
def _cmd_demo_filter(cfg, args, log) -> int:
|
||||
result = pipeline.demo_area_filter(cfg, args.query, area=args.area, limit=args.limit)
|
||||
if args.json:
|
||||
print(json.dumps(result, indent=2, sort_keys=True))
|
||||
return 0
|
||||
print(f"query: {result['query']!r}")
|
||||
print("")
|
||||
print(f"without a filter ({len(result['unfiltered'])} result(s)):")
|
||||
for row in result["unfiltered"]:
|
||||
print(f" {row['score']:.3f} [{row['area']}/{row['confidence']}] {row['title']} {row['url']}")
|
||||
print("")
|
||||
if not args.area:
|
||||
print("no --area given, so the filtered set is empty; re-run with e.g. --area edge")
|
||||
return 0
|
||||
print(f"with area={args.area!r} ({len(result['filtered'])} result(s)):")
|
||||
for row in result["filtered"]:
|
||||
print(f" {row['score']:.3f} [{row['area']}/{row['confidence']}] {row['title']} {row['url']}")
|
||||
wrong = [row for row in result["filtered"] if row["area"] != args.area]
|
||||
if wrong:
|
||||
print(f"FAIL: {len(wrong)} filtered result(s) are not in area {args.area!r}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_hybrid(cfg, args, log) -> int:
|
||||
rows = pipeline.hybrid_search(cfg, args.query, area=args.area, limit=args.limit)
|
||||
if args.json:
|
||||
print(json.dumps(rows, indent=2, sort_keys=True, default=str))
|
||||
return 0
|
||||
print(f"hybrid (RRF) query={args.query!r} area={args.area!r}: {len(rows)} result(s)")
|
||||
for row in rows:
|
||||
print(f" {row['score']:.4f} {row.get('title')} {row.get('url')} [{row['id'][:8]}]")
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_measure(cfg, args, log) -> int:
|
||||
from . import measure as measure_module
|
||||
|
||||
sizes = [int(part) for part in args.sizes.split(",") if part.strip()]
|
||||
report = measure_module.measure(
|
||||
cfg, sizes=sizes, container=args.container, keep=args.keep, log=log
|
||||
)
|
||||
if args.json:
|
||||
print(json.dumps(report, indent=2, sort_keys=True))
|
||||
return 0
|
||||
print(measure_module.render_text(report))
|
||||
return 0
|
||||
|
||||
|
||||
def _cmd_info(cfg, args, log) -> int:
|
||||
print(cfg.describe())
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
print("")
|
||||
print(f"qdrant: {'answering, version ' + qdrant.version() if qdrant.health() else 'NOT answering'}")
|
||||
if qdrant.collection_exists(cfg.collection):
|
||||
print(f"collection {cfg.collection}: {qdrant.point_count(cfg.collection)} point(s)")
|
||||
else:
|
||||
print(f"collection {cfg.collection}: does not exist")
|
||||
if os.path.exists(cfg.db_path):
|
||||
with store_module.Store(cfg.db_path) as store:
|
||||
print(f"store: {store.chunk_count()} chunk(s) across {store.source_count()} source(s)")
|
||||
print(f"contract: {json.dumps(store.get_meta('collection'), sort_keys=True)}")
|
||||
else:
|
||||
print(f"store: {cfg.db_path} does not exist")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
160
corpus/config.py
Normal file
160
corpus/config.py
Normal file
@@ -0,0 +1,160 @@
|
||||
"""Configuration for the corpus pipeline. Environment only.
|
||||
|
||||
Nothing in this file holds a credential. The OmniRoute key is read from the
|
||||
process environment, which systemd populates from
|
||||
`~/.config/environment.d/10-secrets.conf` (AGENTS.md section 4.4). `describe()`
|
||||
is the only way the pipeline prints its configuration, and it redacts the key.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
# Bumped whenever chunk boundaries or payload shape change. A stored corpus
|
||||
# carrying a different version is stale by definition and gets rebuilt.
|
||||
CHUNKER_VERSION = "blocks-v1"
|
||||
|
||||
# Provider defaults. `local-hashing` is a deterministic, offline provider used
|
||||
# by the tests, by `make corpus-test`, and by any rebuild on a host with no
|
||||
# model gateway. It is NOT a semantic embedding model: it exists so the whole
|
||||
# pipeline can be exercised without a provider decision, and so a rebuild never
|
||||
# depends on the network. The production provider is `omniroute`.
|
||||
PROVIDERS = {
|
||||
"local-hashing": ("local-hashing-v1", 256),
|
||||
"omniroute": ("text-embedding-3-small", 1536),
|
||||
}
|
||||
|
||||
DEFAULT_QDRANT_URL = "http://127.0.0.1:6333"
|
||||
DEFAULT_COLLECTION = "resume_corpus"
|
||||
DEFAULT_PROVIDER = "local-hashing"
|
||||
DEFAULT_OMNIROUTE_URL = "https://omniroute.lab.audasmedia.com.au"
|
||||
|
||||
# Freshness horizon: a document whose `last_verified` is older than this is
|
||||
# reported stale by the nightly audit. docs/phase-4-notes.md section 5.
|
||||
DEFAULT_MAX_AGE_DAYS = 180
|
||||
DEFAULT_WARN_DAYS = 30
|
||||
|
||||
|
||||
class ConfigError(RuntimeError):
|
||||
"""A configuration value is missing or unusable."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Config:
|
||||
"""Everything the pipeline needs, resolved once."""
|
||||
|
||||
root: str
|
||||
content_dir: str
|
||||
data_dir: str
|
||||
db_path: str
|
||||
qdrant_url: str
|
||||
collection: str
|
||||
provider: str
|
||||
model: str
|
||||
dim: int
|
||||
api_url: str
|
||||
api_key: str
|
||||
batch_size: int
|
||||
timeout: float
|
||||
max_age_days: int
|
||||
warn_days: int
|
||||
apprise_url: str
|
||||
vectors_on_disk: bool = False
|
||||
|
||||
# -- construction -----------------------------------------------------
|
||||
|
||||
@classmethod
|
||||
def from_env(cls, root: str | None = None, **overrides) -> "Config":
|
||||
"""Build a Config from the environment, then apply explicit overrides.
|
||||
|
||||
Overrides exist so the CLI can accept flags without a second code path;
|
||||
they are applied last so a flag always wins over the environment.
|
||||
"""
|
||||
root = os.path.abspath(root or os.environ.get("RESUME_ROOT") or _default_root())
|
||||
content_dir = os.environ.get("RESUME_CONTENT") or os.path.join(root, "content")
|
||||
data_dir = os.environ.get("RESUME_CORPUS_DATA") or os.path.join(root, "data", "corpus")
|
||||
provider = os.environ.get("RESUME_EMBED_PROVIDER") or DEFAULT_PROVIDER
|
||||
if provider not in PROVIDERS:
|
||||
raise ConfigError(
|
||||
f"RESUME_EMBED_PROVIDER={provider!r} is not one of {sorted(PROVIDERS)}"
|
||||
)
|
||||
default_model, default_dim = PROVIDERS[provider]
|
||||
|
||||
values = dict(
|
||||
root=root,
|
||||
content_dir=os.path.abspath(content_dir),
|
||||
data_dir=os.path.abspath(data_dir),
|
||||
db_path=os.path.abspath(
|
||||
os.environ.get("RESUME_CORPUS_DB") or os.path.join(data_dir, "corpus.db")
|
||||
),
|
||||
qdrant_url=(os.environ.get("RESUME_QDRANT_URL") or DEFAULT_QDRANT_URL).rstrip("/"),
|
||||
collection=os.environ.get("RESUME_COLLECTION") or DEFAULT_COLLECTION,
|
||||
provider=provider,
|
||||
model=os.environ.get("RESUME_EMBED_MODEL") or default_model,
|
||||
dim=int(os.environ.get("RESUME_EMBED_DIM") or default_dim),
|
||||
api_url=(os.environ.get("OMNIROUTE_URL") or DEFAULT_OMNIROUTE_URL).rstrip("/"),
|
||||
api_key=os.environ.get("OMNIROUTE_API_KEY") or "",
|
||||
batch_size=int(os.environ.get("RESUME_EMBED_BATCH") or 32),
|
||||
timeout=float(os.environ.get("RESUME_QDRANT_TIMEOUT") or 30.0),
|
||||
max_age_days=int(
|
||||
os.environ.get("RESUME_MAX_AGE_DAYS") or DEFAULT_MAX_AGE_DAYS
|
||||
),
|
||||
warn_days=int(os.environ.get("RESUME_WARN_DAYS") or DEFAULT_WARN_DAYS),
|
||||
apprise_url=os.environ.get("RESUME_APPRISE_URL") or "",
|
||||
vectors_on_disk=(os.environ.get("RESUME_QDRANT_VECTORS_ON_DISK") or "").lower()
|
||||
in ("1", "true", "yes"),
|
||||
)
|
||||
values.update({k: v for k, v in overrides.items() if v not in (None, "")})
|
||||
cfg = cls(**values)
|
||||
cfg.check()
|
||||
return cfg
|
||||
|
||||
# -- validation -------------------------------------------------------
|
||||
|
||||
def check(self) -> None:
|
||||
if self.dim <= 0:
|
||||
raise ConfigError(f"embedding dimension must be positive, got {self.dim}")
|
||||
if self.batch_size <= 0:
|
||||
raise ConfigError(f"batch size must be positive, got {self.batch_size}")
|
||||
if self.provider == "omniroute" and not self.api_key:
|
||||
raise ConfigError(
|
||||
"provider 'omniroute' needs OMNIROUTE_API_KEY in the environment "
|
||||
"(~/.config/environment.d/10-secrets.conf). It is never read from this repo."
|
||||
)
|
||||
if not os.path.isdir(self.content_dir):
|
||||
raise ConfigError(f"content directory not found: {self.content_dir}")
|
||||
|
||||
# -- presentation -----------------------------------------------------
|
||||
|
||||
def describe(self) -> str:
|
||||
"""Human-readable configuration. Never includes the API key value."""
|
||||
key = "set" if self.api_key else "unset"
|
||||
return (
|
||||
f"root={self.root}\n"
|
||||
f"content={self.content_dir}\n"
|
||||
f"data={self.data_dir}\n"
|
||||
f"db={self.db_path}\n"
|
||||
f"qdrant={self.qdrant_url} collection={self.collection}\n"
|
||||
f"provider={self.provider} model={self.model} dim={self.dim} "
|
||||
f"batch={self.batch_size}\n"
|
||||
f"api_url={self.api_url} api_key={key}\n"
|
||||
f"freshness=max_age_days:{self.max_age_days} warn_days:{self.warn_days}\n"
|
||||
f"qdrant vectors_on_disk={self.vectors_on_disk}\n"
|
||||
f"chunker={CHUNKER_VERSION}"
|
||||
)
|
||||
|
||||
# -- derived paths ----------------------------------------------------
|
||||
|
||||
@property
|
||||
def chunker_version(self) -> str:
|
||||
return CHUNKER_VERSION
|
||||
|
||||
@property
|
||||
def meta_path(self) -> str:
|
||||
return os.path.join(self.data_dir, "collection.json")
|
||||
|
||||
|
||||
def _default_root() -> str:
|
||||
"""The repository root: the parent of this package."""
|
||||
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
241
corpus/embeddings.py
Normal file
241
corpus/embeddings.py
Normal file
@@ -0,0 +1,241 @@
|
||||
"""The one place a vector is made.
|
||||
|
||||
`docs/phase-4-corpus.md` §4.3: *"Record the embedding model and dimension per
|
||||
collection. A provider change is a governed migration, not a silent corruption.
|
||||
Provider decision pending Sam — build provider-agnostic behind one interface."*
|
||||
|
||||
So: one interface (`Provider`), two implementations, and the provider name,
|
||||
model and dimension are written to the collection metadata by the pipeline. A
|
||||
later run with a different model refuses to mix vectors into the same
|
||||
collection; it reports a governed migration instead.
|
||||
|
||||
**local-hashing** is deterministic, offline, and *not semantic*. It hashes word
|
||||
tokens into a fixed-width vector. It exists so that the pipeline, its tests and
|
||||
a from-scratch rebuild never depend on a network or on an unmade decision. It is
|
||||
the default for `make corpus-test` and for the Qdrant memory measurement.
|
||||
|
||||
**omniroute** is the production path: an OpenAI-compatible `/v1/embeddings`
|
||||
call through OmniRoute, which is the only permitted model gateway
|
||||
(AGENTS.md §4.5). No provider SDK, no direct vendor call, key from the
|
||||
environment only.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
class EmbeddingError(RuntimeError):
|
||||
"""Embedding failed. The pipeline aborts rather than writing partial vectors."""
|
||||
|
||||
|
||||
class Provider:
|
||||
"""Interface every embedding provider implements."""
|
||||
|
||||
name = "provider"
|
||||
model = "model"
|
||||
dim = 0
|
||||
|
||||
def embed(self, texts: list) -> list:
|
||||
raise NotImplementedError
|
||||
|
||||
def stats(self) -> dict:
|
||||
"""Call counters. The DoD proves incremental mode with these numbers."""
|
||||
return {"calls": 0, "texts": 0}
|
||||
|
||||
def reset_stats(self) -> None:
|
||||
pass
|
||||
|
||||
def describe(self) -> dict:
|
||||
return {"provider": self.name, "model": self.model, "dim": self.dim}
|
||||
|
||||
|
||||
class LocalHashingProvider(Provider):
|
||||
"""Deterministic feature hashing. Offline, fast, not semantic.
|
||||
|
||||
Every token is hashed with sha256 (never Python's salted `hash()`), so the
|
||||
same text produces the same vector on every host and every run. Vectors are
|
||||
L2-normalised, which suits cosine distance.
|
||||
"""
|
||||
|
||||
name = "local-hashing"
|
||||
|
||||
def __init__(self, model: str = "local-hashing-v1", dim: int = 256) -> None:
|
||||
if dim <= 0:
|
||||
raise EmbeddingError(f"local-hashing needs a positive dimension, got {dim}")
|
||||
self.model = model
|
||||
self.dim = dim
|
||||
self._calls = 0
|
||||
self._texts = 0
|
||||
|
||||
def embed(self, texts: list) -> list:
|
||||
self._calls += 1
|
||||
self._texts += len(texts)
|
||||
return [self._vector(text) for text in texts]
|
||||
|
||||
def _vector(self, text: str) -> list:
|
||||
vector = [0.0] * self.dim
|
||||
for token in _tokens(text):
|
||||
digest = hashlib.sha256(token.encode("utf-8")).digest()
|
||||
index = int.from_bytes(digest[:8], "big") % self.dim
|
||||
sign = 1.0 if digest[8] & 1 else -1.0
|
||||
vector[index] += sign
|
||||
norm = math.sqrt(sum(value * value for value in vector))
|
||||
if norm == 0.0:
|
||||
# An empty text still needs a unit vector of the right width.
|
||||
vector[0] = 1.0
|
||||
return vector
|
||||
return [value / norm for value in vector]
|
||||
|
||||
def stats(self) -> dict:
|
||||
return {"calls": self._calls, "texts": self._texts}
|
||||
|
||||
def reset_stats(self) -> None:
|
||||
self._calls = 0
|
||||
self._texts = 0
|
||||
|
||||
|
||||
class OmniRouteProvider(Provider):
|
||||
"""OpenAI-compatible `/v1/embeddings` through OmniRoute.
|
||||
|
||||
Retries on 429 and 5xx with exponential backoff (the risk table in
|
||||
`docs/phase-4-corpus.md` calls this out for bulk ingest). The key is never
|
||||
logged, never put in an error message, and never read from this repository.
|
||||
"""
|
||||
|
||||
name = "omniroute"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_url: str,
|
||||
api_key: str,
|
||||
model: str,
|
||||
dim: int,
|
||||
batch_size: int = 32,
|
||||
timeout: float = 30.0,
|
||||
attempts: int = 4,
|
||||
backoff: float = 1.5,
|
||||
) -> None:
|
||||
if not api_key:
|
||||
raise EmbeddingError(
|
||||
"OmniRoute needs OMNIROUTE_API_KEY in the environment "
|
||||
"(~/.config/environment.d/10-secrets.conf)"
|
||||
)
|
||||
self.api_url = api_url.rstrip("/")
|
||||
self._api_key = api_key
|
||||
self.model = model
|
||||
self.dim = dim
|
||||
self.batch_size = max(1, batch_size)
|
||||
self.timeout = timeout
|
||||
self.attempts = max(1, attempts)
|
||||
self.backoff = backoff
|
||||
self._calls = 0
|
||||
self._texts = 0
|
||||
|
||||
def embed(self, texts: list) -> list:
|
||||
out = []
|
||||
for start in range(0, len(texts), self.batch_size):
|
||||
batch = texts[start : start + self.batch_size]
|
||||
out.extend(self._embed_batch(batch))
|
||||
return out
|
||||
|
||||
def _embed_batch(self, batch: list) -> list:
|
||||
body = json.dumps({"model": self.model, "input": batch}).encode("utf-8")
|
||||
last_error = None
|
||||
for attempt in range(self.attempts):
|
||||
request = urllib.request.Request(
|
||||
f"{self.api_url}/v1/embeddings",
|
||||
data=body,
|
||||
method="POST",
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {self._api_key}",
|
||||
},
|
||||
)
|
||||
self._calls += 1
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=self.timeout) as response:
|
||||
payload = json.loads(response.read().decode("utf-8"))
|
||||
vectors = _vectors_from_response(payload, self.model, self.dim)
|
||||
self._texts += len(batch)
|
||||
return vectors
|
||||
except urllib.error.HTTPError as exc:
|
||||
last_error = f"HTTP {exc.code} from {self.api_url}/v1/embeddings"
|
||||
exc.close()
|
||||
# 4xx other than 429 is our fault: do not hammer the gateway.
|
||||
if exc.code != 429 and exc.code < 500:
|
||||
raise EmbeddingError(f"{last_error} (not retryable)") from None
|
||||
except (urllib.error.URLError, TimeoutError) as exc:
|
||||
last_error = f"cannot reach {self.api_url}: {exc.reason}"
|
||||
except EmbeddingError:
|
||||
raise
|
||||
if attempt + 1 < self.attempts:
|
||||
time.sleep(self.backoff ** attempt)
|
||||
raise EmbeddingError(f"{last_error} after {self.attempts} attempts")
|
||||
|
||||
def stats(self) -> dict:
|
||||
return {"calls": self._calls, "texts": self._texts}
|
||||
|
||||
def reset_stats(self) -> None:
|
||||
self._calls = 0
|
||||
self._texts = 0
|
||||
|
||||
|
||||
def _vectors_from_response(payload: dict, model: str, dim: int) -> list:
|
||||
data = payload.get("data")
|
||||
if not isinstance(data, list) or not data:
|
||||
raise EmbeddingError("embedding response has no 'data' array")
|
||||
ordered = sorted(data, key=lambda item: item.get("index", 0))
|
||||
vectors = []
|
||||
for item in ordered:
|
||||
vector = item.get("embedding")
|
||||
if not isinstance(vector, list) or not vector:
|
||||
raise EmbeddingError("embedding response item has no 'embedding' array")
|
||||
if dim and len(vector) != dim:
|
||||
raise EmbeddingError(
|
||||
f"provider returned dimension {len(vector)} but the collection is "
|
||||
f"configured for {dim}; refusing to mix dimensions"
|
||||
)
|
||||
vectors.append([float(value) for value in vector])
|
||||
returned_model = payload.get("model")
|
||||
if returned_model and model and returned_model != model:
|
||||
raise EmbeddingError(
|
||||
f"provider answered as model {returned_model!r}, not {model!r}; "
|
||||
"a model change must be a governed migration"
|
||||
)
|
||||
return vectors
|
||||
|
||||
|
||||
def build_provider(cfg) -> Provider:
|
||||
"""Construct the provider named by the configuration."""
|
||||
if cfg.provider == "local-hashing":
|
||||
return LocalHashingProvider(model=cfg.model, dim=cfg.dim)
|
||||
if cfg.provider == "omniroute":
|
||||
return OmniRouteProvider(
|
||||
api_url=cfg.api_url,
|
||||
api_key=cfg.api_key,
|
||||
model=cfg.model,
|
||||
dim=cfg.dim,
|
||||
batch_size=cfg.batch_size,
|
||||
timeout=cfg.timeout,
|
||||
)
|
||||
raise EmbeddingError(f"unknown embedding provider {cfg.provider!r}")
|
||||
|
||||
|
||||
def _tokens(text: str) -> list:
|
||||
out = []
|
||||
current = []
|
||||
for char in text.lower():
|
||||
if char.isalnum():
|
||||
current.append(char)
|
||||
elif current:
|
||||
out.append("".join(current))
|
||||
current = []
|
||||
if current:
|
||||
out.append("".join(current))
|
||||
return out
|
||||
197
corpus/freshness.py
Normal file
197
corpus/freshness.py
Normal file
@@ -0,0 +1,197 @@
|
||||
"""Freshness audit — the thing that stops the corpus rotting silently.
|
||||
|
||||
The risk table in `docs/phase-4-corpus.md` names it: *"Silent staleness… a
|
||||
stale index is otherwise an invisible quality failure."* So the audit is a first
|
||||
class command, it runs nightly from Prefect, and it alerts through the existing
|
||||
Apprise service when it finds something.
|
||||
|
||||
It reports four kinds of finding, and they are different problems:
|
||||
|
||||
* **stale** — a document whose `last_verified` is older than the horizon. The
|
||||
claim on the page may no longer be true.
|
||||
* **due soon** — inside the warning window. Re-verify before it becomes stale.
|
||||
* **index drift** — the source changed (or disappeared, or was never indexed)
|
||||
since it was embedded. Retrieval is answering from an older world.
|
||||
* **contract drift** — the collection was built with a different model,
|
||||
dimension or chunker than the running configuration.
|
||||
|
||||
Exit code is 0 for a clean audit and 1 when findings exist and `--fail-on-findings`
|
||||
was passed, so the Prefect deployment can alert on the result without parsing
|
||||
text.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
from . import loader, store as store_module
|
||||
from .config import CHUNKER_VERSION
|
||||
from .pipeline import _contract
|
||||
from .qdrant import Qdrant
|
||||
|
||||
|
||||
def audit(cfg, today: datetime.date = None, log=None) -> dict:
|
||||
"""Return the audit report as a plain dict (JSON-serialisable)."""
|
||||
log = log or (lambda _msg: None)
|
||||
today = today or datetime.date.today()
|
||||
documents = loader.load_documents(cfg)
|
||||
|
||||
findings = {"stale": [], "due_soon": [], "index_drift": [], "contract_drift": []}
|
||||
|
||||
for doc in documents:
|
||||
try:
|
||||
verified = datetime.date.fromisoformat(doc.last_verified)
|
||||
except ValueError:
|
||||
findings["index_drift"].append(
|
||||
{"source_path": doc.path, "problem": f"invalid last_verified {doc.last_verified!r}"}
|
||||
)
|
||||
continue
|
||||
age = (today - verified).days
|
||||
entry = {
|
||||
"source_path": doc.path,
|
||||
"title": doc.title,
|
||||
"area": doc.area,
|
||||
"confidence": doc.confidence,
|
||||
"last_verified": doc.last_verified,
|
||||
"age_days": age,
|
||||
}
|
||||
if age > cfg.max_age_days:
|
||||
findings["stale"].append(entry)
|
||||
elif age > cfg.max_age_days - cfg.warn_days:
|
||||
findings["due_soon"].append(entry)
|
||||
|
||||
with store_module.Store(cfg.db_path) as store:
|
||||
manifest = store.manifest()
|
||||
on_disk = {doc.path: doc for doc in documents}
|
||||
|
||||
for doc in documents:
|
||||
row = manifest.get(doc.path)
|
||||
if row is None:
|
||||
findings["index_drift"].append(
|
||||
{"source_path": doc.path, "problem": "not indexed (run sync)"}
|
||||
)
|
||||
continue
|
||||
if row["content_hash"] != doc.digest:
|
||||
findings["index_drift"].append(
|
||||
{"source_path": doc.path, "problem": "source changed since it was embedded"}
|
||||
)
|
||||
if row["chunker_version"] != CHUNKER_VERSION:
|
||||
findings["index_drift"].append(
|
||||
{
|
||||
"source_path": doc.path,
|
||||
"problem": f"chunked with {row['chunker_version']}, current {CHUNKER_VERSION}",
|
||||
}
|
||||
)
|
||||
|
||||
for source in sorted(set(manifest) - set(on_disk)):
|
||||
findings["index_drift"].append(
|
||||
{"source_path": source, "problem": "indexed but no longer in content/"}
|
||||
)
|
||||
|
||||
stored_contract = store.get_meta("collection")
|
||||
if stored_contract != _contract(cfg):
|
||||
findings["contract_drift"].append(
|
||||
{"problem": "collection contract does not match this configuration",
|
||||
"stored": stored_contract, "configured": _contract(cfg)}
|
||||
)
|
||||
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
reachable = qdrant.health()
|
||||
points = None
|
||||
if reachable and qdrant.collection_exists(cfg.collection):
|
||||
points = qdrant.point_count(cfg.collection)
|
||||
elif not reachable:
|
||||
findings["contract_drift"].append(
|
||||
{"problem": f"Qdrant is not answering at {cfg.qdrant_url}"}
|
||||
)
|
||||
|
||||
report = {
|
||||
"generated_at": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"today": today.isoformat(),
|
||||
"horizon": {
|
||||
"max_age_days": cfg.max_age_days,
|
||||
"warn_days": cfg.warn_days,
|
||||
},
|
||||
"corpus": {
|
||||
"documents": len(documents),
|
||||
"chunks": store.chunk_count(),
|
||||
"sources": store.source_count(),
|
||||
"collection": cfg.collection,
|
||||
"collection_points": points,
|
||||
"provider": cfg.provider,
|
||||
"model": cfg.model,
|
||||
"dim": cfg.dim,
|
||||
"chunker_version": CHUNKER_VERSION,
|
||||
},
|
||||
"findings": findings,
|
||||
"summary": {
|
||||
"stale": len(findings["stale"]),
|
||||
"due_soon": len(findings["due_soon"]),
|
||||
"index_drift": len(findings["index_drift"]),
|
||||
"contract_drift": len(findings["contract_drift"]),
|
||||
},
|
||||
# `due_soon` is a warning, not a failure: the document is still
|
||||
# inside its verification horizon. Stale, drift and contract
|
||||
# problems are failures.
|
||||
"ok": not (
|
||||
findings["stale"] or findings["index_drift"] or findings["contract_drift"]
|
||||
),
|
||||
}
|
||||
log(f"audit: {json.dumps(report['summary'], sort_keys=True)}")
|
||||
return report
|
||||
|
||||
|
||||
def render_text(report: dict) -> str:
|
||||
"""Human-readable audit, for a terminal or an Apprise body."""
|
||||
lines = []
|
||||
corpus = report["corpus"]
|
||||
lines.append(
|
||||
f"corpus: {corpus['documents']} document(s), {corpus['chunks']} chunk(s), "
|
||||
f"{corpus['collection_points']} vector(s) in {corpus['collection']}"
|
||||
)
|
||||
lines.append(
|
||||
f"horizon: last_verified within {report['horizon']['max_age_days']} days "
|
||||
f"(warning at {report['horizon']['max_age_days'] - report['horizon']['warn_days']} days)"
|
||||
)
|
||||
findings = report["findings"]
|
||||
for kind, label in (
|
||||
("stale", "STALE — past the verification horizon"),
|
||||
("due_soon", "DUE SOON — verify before it goes stale"),
|
||||
("index_drift", "INDEX DRIFT — the index does not match content/"),
|
||||
("contract_drift", "CONTRACT DRIFT — rebuild required"),
|
||||
):
|
||||
rows = findings[kind]
|
||||
if not rows:
|
||||
continue
|
||||
lines.append("")
|
||||
lines.append(f"{label} ({len(rows)})")
|
||||
for row in rows:
|
||||
if "problem" in row:
|
||||
lines.append(f" - {row.get('source_path', '<collection>')}: {row['problem']}")
|
||||
else:
|
||||
lines.append(
|
||||
f" - {row['source_path']} ({row['area']}, {row['confidence']}): "
|
||||
f"last_verified {row['last_verified']} ({row['age_days']} days ago)"
|
||||
)
|
||||
if report["ok"]:
|
||||
lines.append("")
|
||||
lines.append("OK: nothing stale, no drift." + (" (see warnings above)" if findings["due_soon"] else ""))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def alert(report: dict, apprise_url: str, timeout: float = 10.0) -> bool:
|
||||
"""POST the audit to the existing Apprise service. No-op without a URL."""
|
||||
if not apprise_url:
|
||||
return False
|
||||
title = "ai-resume corpus audit: findings" if not report["ok"] else "ai-resume corpus audit: OK"
|
||||
body = json.dumps({"title": title, "body": render_text(report), "type": "warning" if not report["ok"] else "info"})
|
||||
request = urllib.request.Request(
|
||||
apprise_url,
|
||||
data=body.encode("utf-8"),
|
||||
method="POST",
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=timeout):
|
||||
return True
|
||||
162
corpus/loader.py
Normal file
162
corpus/loader.py
Normal file
@@ -0,0 +1,162 @@
|
||||
"""Load `content/` into documents, reusing the Phase 1 validator's parser.
|
||||
|
||||
There is exactly one front-matter dialect in this repository and exactly one
|
||||
parser for it: `tools/validate_content.py`. This module imports that parser
|
||||
rather than reimplementing it, so the corpus can never disagree with the gate
|
||||
about what a document says.
|
||||
|
||||
The gate itself is `validate_tree` from the same file. The pipeline runs it
|
||||
before it chunks anything: an invalid content tree stops the run, it never
|
||||
reaches the vector store.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import os
|
||||
import sys
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
VALIDATOR_NAME = "validate_content"
|
||||
|
||||
|
||||
class ContentError(RuntimeError):
|
||||
"""The content tree is unreadable or invalid; the run must stop."""
|
||||
|
||||
|
||||
def load_validator(root: str):
|
||||
"""Import tools/validate_content.py as a module (cached per root)."""
|
||||
cached = sys.modules.get(VALIDATOR_NAME)
|
||||
if cached is not None and getattr(cached, "__validator_root__", None) == root:
|
||||
return cached
|
||||
path = os.path.join(root, "tools", "validate_content.py")
|
||||
if not os.path.exists(path):
|
||||
raise ContentError(f"validator not found at {path}")
|
||||
spec = importlib.util.spec_from_file_location(VALIDATOR_NAME, path)
|
||||
if spec is None or spec.loader is None:
|
||||
raise ContentError(f"cannot load validator at {path}")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
module.__validator_root__ = root # type: ignore[attr-defined]
|
||||
sys.modules[VALIDATOR_NAME] = module
|
||||
return module
|
||||
|
||||
|
||||
def to_python(node):
|
||||
"""Convert a validator front-matter Node into plain Python values."""
|
||||
if node is None or node.kind == "null":
|
||||
return None
|
||||
if node.kind == "scalar":
|
||||
return node.value
|
||||
if node.kind == "seq":
|
||||
return [to_python(item) for item in node.value]
|
||||
if node.kind == "map":
|
||||
return {key: to_python(value) for key, value in node.value.items()}
|
||||
raise ContentError(f"unexpected front-matter node kind {node.kind!r}")
|
||||
|
||||
|
||||
@dataclass
|
||||
class Document:
|
||||
"""One Markdown file plus the front-matter the schema guarantees."""
|
||||
|
||||
path: str # repository-relative, posix, e.g. "content/edge/l3/edge-zigbee.md"
|
||||
source_id: str
|
||||
area: str
|
||||
level: int
|
||||
title: str
|
||||
claim: str
|
||||
confidence: str
|
||||
period: str
|
||||
tech: list
|
||||
metrics: list
|
||||
artefacts: list
|
||||
public: bool
|
||||
last_verified: str
|
||||
tags: list
|
||||
body: str
|
||||
digest: str # sha256 of the file bytes: the incremental-mode key
|
||||
url: str # canonical page URL, matching internal/content.PageURL
|
||||
|
||||
# Filled in by the pipeline as it goes; not part of the schema.
|
||||
chunk_count: int = field(default=0)
|
||||
|
||||
@property
|
||||
def artefact_urls(self) -> list:
|
||||
return [str(a.get("url", "")) for a in self.artefacts if a.get("url")]
|
||||
|
||||
|
||||
def page_url(area: str, doc_id: str, level: int) -> str:
|
||||
"""Canonical URL for a document. Mirrors internal/content.PageURL exactly."""
|
||||
if level == 1:
|
||||
return f"/{area}/"
|
||||
if level == 2:
|
||||
return f"/{area}/{doc_id}/"
|
||||
return f"/{area}/a/{doc_id}/"
|
||||
|
||||
|
||||
def digest_of(path: str) -> str:
|
||||
"""sha256 of the raw file bytes — the same digest for any platform."""
|
||||
with open(path, "rb") as handle:
|
||||
return hashlib.sha256(handle.read()).hexdigest()
|
||||
|
||||
|
||||
def load_documents(cfg) -> list:
|
||||
"""Load every document under content/, sorted by path.
|
||||
|
||||
Raises ContentError when the gate fails, so a broken content tree never
|
||||
produces a half-correct index.
|
||||
"""
|
||||
validator = load_validator(cfg.root)
|
||||
violations, count = validator.validate_tree(cfg.root, cfg.content_dir, quiet=True)
|
||||
if violations:
|
||||
detail = "\n ".join(violations[:20])
|
||||
more = "" if len(violations) <= 20 else f"\n … and {len(violations) - 20} more"
|
||||
raise ContentError(
|
||||
f"content gate failed with {len(violations)} violation(s) "
|
||||
f"across {count} document(s). Run `make validate`:\n {detail}{more}"
|
||||
)
|
||||
|
||||
documents = []
|
||||
for path in validator.iter_documents(cfg.content_dir):
|
||||
documents.append(_load_one(cfg, validator, path))
|
||||
documents.sort(key=lambda d: d.path)
|
||||
if not documents:
|
||||
raise ContentError(f"no documents found under {cfg.content_dir}")
|
||||
return documents
|
||||
|
||||
|
||||
def _load_one(cfg, validator, path: str) -> Document:
|
||||
rel = os.path.relpath(path, cfg.root).replace(os.sep, "/")
|
||||
fm_lines, body, problem = validator.split_front_matter(path)
|
||||
if problem:
|
||||
raise ContentError(f"{rel}: {problem}")
|
||||
try:
|
||||
root_node = validator.parse_front_matter(fm_lines)
|
||||
except validator.YamlError as exc:
|
||||
raise ContentError(f"{rel}:{exc.line}: invalid YAML: {exc.msg}") from exc
|
||||
fields = to_python(root_node)
|
||||
if not isinstance(fields, dict):
|
||||
raise ContentError(f"{rel}: front-matter must be a mapping")
|
||||
|
||||
level = int(fields["level"])
|
||||
doc = Document(
|
||||
path=rel,
|
||||
source_id=str(fields["id"]),
|
||||
area=str(fields["area"]),
|
||||
level=level,
|
||||
title=str(fields["title"]),
|
||||
claim=str(fields["claim"]),
|
||||
confidence=str(fields["confidence"]),
|
||||
period=str(fields["period"]),
|
||||
tech=list(fields.get("tech") or []),
|
||||
metrics=list(fields.get("metrics") or []),
|
||||
artefacts=list(fields.get("artefacts") or []),
|
||||
public=bool(fields.get("public")),
|
||||
last_verified=str(fields["last_verified"]),
|
||||
tags=list(fields.get("tags") or []),
|
||||
body=body,
|
||||
digest=digest_of(path),
|
||||
url=page_url(str(fields["area"]), str(fields["id"]), level),
|
||||
)
|
||||
return doc
|
||||
242
corpus/measure.py
Normal file
242
corpus/measure.py
Normal file
@@ -0,0 +1,242 @@
|
||||
"""Qdrant memory at 500 / 5,000 / 20,000 chunks.
|
||||
|
||||
`docs/phase-4-corpus.md` requires the measurement and `reference/stack.md` is
|
||||
where the number is recorded. The corpus itself is small; the point of the
|
||||
measurement is to show the *shape* of the growth so the 400 m cap is a known
|
||||
quantity rather than a hope.
|
||||
|
||||
Method:
|
||||
|
||||
1. Create an empty collection and sample memory — the baseline.
|
||||
2. For each size: recreate the collection, upsert that many chunks with payloads
|
||||
shaped exactly like production payloads (same fields, same text lengths),
|
||||
wait for the write to settle, then sample.
|
||||
|
||||
Memory is read from Qdrant's own Prometheus `app_process_resident_memory_bytes`,
|
||||
which is the process that would be capped, not the container sandbox. When
|
||||
`--container` is also given the container's counter is recorded next to it.
|
||||
|
||||
The measurement collection is dropped afterwards unless `--keep` is passed, and
|
||||
it is never the production collection name.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import statistics
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from . import chunker, embeddings, loader, payloads
|
||||
from .qdrant import Qdrant
|
||||
|
||||
# Qdrant 1.20 exposes memory_resident_bytes; older builds used the Prometheus
|
||||
# default app_process_resident_memory_bytes. Accept either.
|
||||
RSS_METRIC = re.compile(
|
||||
r"^(?:memory_resident_bytes|app_process_resident_memory_bytes)\s+([0-9.eE+]+)",
|
||||
re.MULTILINE,
|
||||
)
|
||||
INGEST_BATCH = 250
|
||||
INGEST_SETTLE = 0.4
|
||||
MEMUSAGE = re.compile(r"([0-9.]+)\s*([KMG]i?B)", re.IGNORECASE)
|
||||
CAP_MIB = 400 # reference/stack.md memory budget for Qdrant
|
||||
|
||||
|
||||
def measure(cfg, sizes=(500, 5000, 20000), container: str = "", keep: bool = False, log=None) -> dict:
|
||||
log = log or (lambda _msg: None)
|
||||
collection = f"{cfg.collection}_measure"
|
||||
provider = embeddings.build_provider(cfg)
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=max(cfg.timeout, 60.0))
|
||||
if not qdrant.health():
|
||||
raise RuntimeError(f"Qdrant is not answering at {cfg.qdrant_url}")
|
||||
|
||||
templates = _templates(cfg, provider)
|
||||
log(f"measure: {len(templates)} payload template(s), dim={cfg.dim}, collection={collection}")
|
||||
|
||||
qdrant.drop_collection(collection)
|
||||
_create(qdrant, cfg, collection)
|
||||
time.sleep(1.0)
|
||||
baseline = _sample(qdrant, container)
|
||||
|
||||
samples = []
|
||||
for size in sorted(int(value) for value in sizes):
|
||||
qdrant.drop_collection(collection)
|
||||
_create(qdrant, cfg, collection)
|
||||
points = _points(templates, size, provider)
|
||||
started = time.time()
|
||||
# Throttled ingest: the peak during a bulk write is what trips the
|
||||
# cgroup limit, not the settled size. One batch at a time, sampling as we
|
||||
# go, so the measurement reports the peak and not just the end state.
|
||||
sent = 0
|
||||
peak = baseline["rss_mib"]
|
||||
for start in range(0, len(points), INGEST_BATCH):
|
||||
sent += qdrant.upsert(collection, points[start : start + INGEST_BATCH], batch=INGEST_BATCH)
|
||||
time.sleep(INGEST_SETTLE)
|
||||
reading = _rss_mib(qdrant)
|
||||
if reading is not None:
|
||||
peak = max(peak, reading)
|
||||
elapsed = round(time.time() - started, 2)
|
||||
points_now = qdrant.point_count(collection)
|
||||
time.sleep(2.0)
|
||||
reading = _sample(qdrant, container)
|
||||
growth = reading["rss_mib"] - baseline["rss_mib"]
|
||||
samples.append(
|
||||
{
|
||||
"chunks_requested": size,
|
||||
"points": points_now,
|
||||
"sent": sent,
|
||||
"upsert_seconds": elapsed,
|
||||
"rss_mib": reading["rss_mib"],
|
||||
"peak_rss_mib": round(peak, 1),
|
||||
"container_mib": reading["container_mib"],
|
||||
"growth_mib": round(growth, 1),
|
||||
"bytes_per_chunk": int(growth * 1024 * 1024 / size) if size else None,
|
||||
}
|
||||
)
|
||||
log(
|
||||
f"measure: {size} chunk(s) -> {points_now} point(s), rss {reading['rss_mib']:.1f} MiB "
|
||||
f"(+{growth:.1f} MiB), upsert {elapsed}s"
|
||||
)
|
||||
|
||||
if not keep:
|
||||
qdrant.drop_collection(collection)
|
||||
|
||||
report = {
|
||||
"collection": collection,
|
||||
"dim": cfg.dim,
|
||||
"distance": "Cosine",
|
||||
"provider": cfg.provider,
|
||||
"model": cfg.model,
|
||||
"qdrant_version": qdrant.version(),
|
||||
"cap_mib": CAP_MIB,
|
||||
"baseline": baseline,
|
||||
"samples": samples,
|
||||
"within_cap": all(
|
||||
max(sample["rss_mib"], sample["peak_rss_mib"]) <= CAP_MIB for sample in samples
|
||||
),
|
||||
"measured_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
"kept": keep,
|
||||
}
|
||||
return report
|
||||
|
||||
|
||||
def _create(qdrant: Qdrant, cfg, collection: str) -> None:
|
||||
"""Same collection settings as production, so the memory reading means something."""
|
||||
qdrant.create_collection(
|
||||
collection, cfg.dim, vectors_on_disk=cfg.vectors_on_disk
|
||||
)
|
||||
|
||||
|
||||
def _templates(cfg, provider) -> list:
|
||||
"""Real payloads from content/, so field count and text length are realistic."""
|
||||
documents = loader.load_documents(cfg)
|
||||
out = []
|
||||
for doc in documents:
|
||||
for chunk in chunker.chunk_document(doc.body):
|
||||
payload = payloads.build_payload(doc, chunk, cfg)
|
||||
payload["id"] = "template"
|
||||
out.append(payload)
|
||||
if not out:
|
||||
raise RuntimeError("no content to build a measurement from")
|
||||
return out
|
||||
|
||||
|
||||
def _points(templates: list, size: int, provider) -> list:
|
||||
"""`size` points with unique ids, unique source paths and real payload shape."""
|
||||
points = []
|
||||
texts = []
|
||||
for index in range(size):
|
||||
template = templates[index % len(templates)]
|
||||
payload = dict(template)
|
||||
payload["id"] = str(uuid.UUID(hex=hashlib.sha256(f"measure:{index}".encode()).hexdigest()[:32]))
|
||||
payload["source_path"] = f"measure/{template['area']}/{index}.md"
|
||||
payload["chunk_index"] = index
|
||||
points.append({"id": payload["id"], "payload": payload})
|
||||
texts.append(f"{index} {payload['text']}")
|
||||
vectors = provider.embed(texts)
|
||||
for point, vector in zip(points, vectors):
|
||||
point["vector"] = vector
|
||||
return points
|
||||
|
||||
|
||||
def _sample(qdrant: Qdrant, container: str) -> dict:
|
||||
"""Three readings two seconds apart; report the median so a GC pause is not the number."""
|
||||
readings = []
|
||||
for attempt in range(3):
|
||||
readings.append(_rss_mib(qdrant))
|
||||
if attempt < 2:
|
||||
time.sleep(2.0)
|
||||
readings = [value for value in readings if value is not None]
|
||||
rss = round(statistics.median(readings), 1) if readings else 0.0
|
||||
return {"rss_mib": rss, "container_mib": _container_mib(container) if container else None}
|
||||
|
||||
|
||||
def _rss_mib(qdrant: Qdrant):
|
||||
text = qdrant.metrics()
|
||||
if not text:
|
||||
return None
|
||||
match = RSS_METRIC.search(text)
|
||||
if not match:
|
||||
return None
|
||||
return float(match.group(1)) / (1024 * 1024)
|
||||
|
||||
|
||||
def _container_mib(container: str):
|
||||
"""Container memory from `docker stats`. None when docker or the container is absent."""
|
||||
try:
|
||||
completed = subprocess.run(
|
||||
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=20,
|
||||
check=False,
|
||||
)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return None
|
||||
if completed.returncode != 0:
|
||||
return None
|
||||
match = MEMUSAGE.search(completed.stdout)
|
||||
if not match:
|
||||
return None
|
||||
value = float(match.group(1))
|
||||
unit = match.group(2).upper()
|
||||
if unit.startswith("G"):
|
||||
value *= 1024
|
||||
elif unit.startswith("K"):
|
||||
value /= 1024
|
||||
return round(value, 1)
|
||||
|
||||
|
||||
def render_text(report: dict) -> str:
|
||||
lines = [
|
||||
f"Qdrant {report['qdrant_version']} · collection {report['collection']} · "
|
||||
f"dim {report['dim']} cosine · provider {report['provider']}/{report['model']}",
|
||||
f"baseline (empty collection): {report['baseline']['rss_mib']:.1f} MiB",
|
||||
"",
|
||||
f"{'chunks':>8} {'points':>8} {'rss MiB':>8} {'peak':>7} {'growth':>7} {'B/chunk':>8} {'upsert s':>8}",
|
||||
]
|
||||
for sample in report["samples"]:
|
||||
per_chunk = sample["bytes_per_chunk"] or 0
|
||||
lines.append(
|
||||
f"{sample['chunks_requested']:>8} {sample['points']:>8} {sample['rss_mib']:>8.1f} "
|
||||
f"{sample['peak_rss_mib']:>7.1f} {sample['growth_mib']:>+7.1f} {per_chunk:>8} "
|
||||
f"{sample['upsert_seconds']:>8}"
|
||||
)
|
||||
lines.append("")
|
||||
verdict = "within" if report["within_cap"] else "OVER"
|
||||
lines.append(f"cap {report['cap_mib']} MiB: {verdict} cap at every measured size")
|
||||
if report["kept"]:
|
||||
lines.append(f"collection {report['collection']} was kept for inspection")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
from .config import Config
|
||||
|
||||
cfg = Config.from_env(dim=int(sys.argv[1]) if len(sys.argv) > 1 else None)
|
||||
print(render_text(measure(cfg, container="", log=print)))
|
||||
155
corpus/payloads.py
Normal file
155
corpus/payloads.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""Chunk payloads: the thing the retrieval filter runs against.
|
||||
|
||||
`docs/phase-4-corpus.md` §4.2 fixes the payload. `area` and `confidence` are the
|
||||
two that matter: they let the query path narrow *before* it searches, and they
|
||||
let an answer qualify unfinished work instead of presenting it as delivered.
|
||||
|
||||
**A missing or invalid field fails the run.** Nothing in this module supplies a
|
||||
default value: `validate_payload` raises on the first field that is absent,
|
||||
empty or out of enum, and the pipeline reports which document and chunk it was.
|
||||
That is the DoD item "a missing field fails the run rather than defaulting".
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
|
||||
AREAS = ("ai", "platform", "edge", "products", "history", "about")
|
||||
LEVELS = (1, 2, 3)
|
||||
SOURCE_TYPES = ("doc", "code", "adr", "metric")
|
||||
CONFIDENCE = ("delivered", "running", "in-progress", "abandoned", "historic")
|
||||
|
||||
# Fields every payload must carry (docs/phase-4-corpus.md §4.2), plus the ones
|
||||
# the index needs to answer "where did this come from?" without a second lookup.
|
||||
REQUIRED_FIELDS = (
|
||||
"area",
|
||||
"level",
|
||||
"source_type",
|
||||
"confidence",
|
||||
"period",
|
||||
"artefacts",
|
||||
"last_verified",
|
||||
"text",
|
||||
)
|
||||
PROVENANCE_FIELDS = (
|
||||
"id",
|
||||
"source_path",
|
||||
"source_id",
|
||||
"chunk_index",
|
||||
"title",
|
||||
"claim",
|
||||
"url",
|
||||
"content_hash",
|
||||
"embedding_model",
|
||||
"embedding_dim",
|
||||
"chunker_version",
|
||||
)
|
||||
|
||||
|
||||
class PayloadError(RuntimeError):
|
||||
"""A payload is incomplete or out of enum. The run must stop."""
|
||||
|
||||
|
||||
def source_type_for(doc) -> str:
|
||||
"""Classify a document. Everything in content/ is a doc unless it says so.
|
||||
|
||||
The schema has no `source_type` field and the schema is frozen, so the
|
||||
classification is derived from tags the schema does allow.
|
||||
"""
|
||||
tags = {str(tag).lower() for tag in doc.tags}
|
||||
if "adr" in tags:
|
||||
return "adr"
|
||||
if "metric" in tags:
|
||||
return "metric"
|
||||
if "code" in tags:
|
||||
return "code"
|
||||
return "doc"
|
||||
|
||||
|
||||
def build_payload(doc, chunk, cfg) -> dict:
|
||||
"""Build one chunk payload from a document and a chunk.
|
||||
|
||||
Values are copied, never guessed: if the document does not carry a field,
|
||||
the payload does not gain one.
|
||||
"""
|
||||
payload = {
|
||||
# -- the retrieval filter (docs/phase-4-corpus.md §4.2) ------------
|
||||
"area": doc.area,
|
||||
"level": doc.level,
|
||||
"source_type": source_type_for(doc),
|
||||
"confidence": doc.confidence,
|
||||
"period": doc.period,
|
||||
"artefacts": [str(a.get("url", "")) for a in doc.artefacts],
|
||||
"last_verified": doc.last_verified,
|
||||
"text": chunk.text,
|
||||
# -- provenance ----------------------------------------------------
|
||||
"id": None, # filled by the pipeline once the id is computed
|
||||
"source_path": doc.path,
|
||||
"source_id": doc.source_id,
|
||||
"chunk_index": chunk.index,
|
||||
"title": doc.title,
|
||||
"claim": doc.claim,
|
||||
"url": doc.url,
|
||||
"content_hash": doc.digest,
|
||||
"embedding_model": cfg.model,
|
||||
"embedding_dim": cfg.dim,
|
||||
"chunker_version": cfg.chunker_version,
|
||||
# -- supporting evidence, used by the answer to cite ---------------
|
||||
"public": bool(doc.public),
|
||||
"tech": list(doc.tech),
|
||||
"tags": list(doc.tags),
|
||||
"chunk_tokens": chunk.tokens,
|
||||
}
|
||||
return payload
|
||||
|
||||
|
||||
def validate_payload(payload: dict, where: str = "chunk") -> None:
|
||||
"""Raise PayloadError if a required field is missing, empty or out of enum."""
|
||||
for field in REQUIRED_FIELDS + PROVENANCE_FIELDS:
|
||||
if field == "id" and payload.get("id") is None:
|
||||
raise PayloadError(f"{where}: payload field 'id' was not set")
|
||||
if field not in payload:
|
||||
raise PayloadError(f"{where}: payload is missing required field {field!r}")
|
||||
|
||||
area = payload["area"]
|
||||
if area not in AREAS:
|
||||
raise PayloadError(f"{where}: area {area!r} is not one of {list(AREAS)}")
|
||||
if payload["level"] not in LEVELS:
|
||||
raise PayloadError(f"{where}: level {payload['level']!r} is not one of {list(LEVELS)}")
|
||||
if payload["source_type"] not in SOURCE_TYPES:
|
||||
raise PayloadError(
|
||||
f"{where}: source_type {payload['source_type']!r} is not one of {list(SOURCE_TYPES)}"
|
||||
)
|
||||
if payload["confidence"] not in CONFIDENCE:
|
||||
raise PayloadError(
|
||||
f"{where}: confidence {payload['confidence']!r} is not one of {list(CONFIDENCE)}"
|
||||
)
|
||||
|
||||
for field in ("period", "title", "claim", "url", "source_path", "source_id", "embedding_model"):
|
||||
value = payload[field]
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise PayloadError(f"{where}: field {field!r} must be a non-empty string")
|
||||
|
||||
text = payload["text"]
|
||||
if not isinstance(text, str) or not text.strip():
|
||||
raise PayloadError(f"{where}: field 'text' must be non-empty")
|
||||
|
||||
if not isinstance(payload["artefacts"], list):
|
||||
raise PayloadError(f"{where}: field 'artefacts' must be a list (empty is allowed)")
|
||||
|
||||
_check_date(payload["last_verified"], where)
|
||||
if not isinstance(payload["embedding_dim"], int) or payload["embedding_dim"] <= 0:
|
||||
raise PayloadError(f"{where}: field 'embedding_dim' must be a positive integer")
|
||||
if not isinstance(payload["chunk_index"], int) or payload["chunk_index"] < 0:
|
||||
raise PayloadError(f"{where}: field 'chunk_index' must be a non-negative integer")
|
||||
if not isinstance(payload["content_hash"], str) or len(payload["content_hash"]) != 64:
|
||||
raise PayloadError(f"{where}: field 'content_hash' must be a sha256 hex digest")
|
||||
|
||||
|
||||
def _check_date(value: str, where: str) -> None:
|
||||
try:
|
||||
datetime.date.fromisoformat(str(value))
|
||||
except ValueError as exc:
|
||||
raise PayloadError(
|
||||
f"{where}: field 'last_verified' must be an ISO date (YYYY-MM-DD), got {value!r}"
|
||||
) from exc
|
||||
426
corpus/pipeline.py
Normal file
426
corpus/pipeline.py
Normal file
@@ -0,0 +1,426 @@
|
||||
"""The pipeline: content/ -> validate -> chunk -> enrich -> embed -> upsert.
|
||||
|
||||
One entry point, `run`, with two modes:
|
||||
|
||||
* **rebuild** — drop the collection, empty the store, embed everything. This is
|
||||
the "one command rebuilds the corpus from an empty volume" path.
|
||||
* **sync** — the default. Only documents whose content hash, embedding model,
|
||||
embedding dimension or chunker version changed are re-chunked and
|
||||
re-embedded. Everything else is left alone.
|
||||
|
||||
The order matters. Payloads are built and validated for *every* changed document
|
||||
before a single vector is written, so an invalid payload stops the run with an
|
||||
empty diff rather than leaving half an index behind.
|
||||
|
||||
Deterministic ids (`sha256(source_path + chunk_index + content_hash)`) mean
|
||||
re-running updates points instead of duplicating them, and the id is a valid
|
||||
UUID so Qdrant accepts it as a point id without a mapping table.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
from dataclasses import asdict, dataclass, field
|
||||
|
||||
from . import chunker, embeddings, loader, payloads, store as store_module
|
||||
from .config import CHUNKER_VERSION
|
||||
from .qdrant import Qdrant, QdrantError
|
||||
|
||||
|
||||
class PipelineError(RuntimeError):
|
||||
"""The run cannot continue without corrupting the index."""
|
||||
|
||||
|
||||
def chunk_id(source_path: str, chunk_index: int, content_hash: str) -> str:
|
||||
"""Deterministic point id: sha256(source_path + chunk_index + content_hash).
|
||||
|
||||
Rendered as a UUID because Qdrant only accepts unsigned integers or UUIDs as
|
||||
point ids; the first 128 bits of the digest keep collisions improbable.
|
||||
"""
|
||||
digest = hashlib.sha256(
|
||||
f"{source_path}\x00{chunk_index}\x00{content_hash}".encode("utf-8")
|
||||
).hexdigest()
|
||||
return str(uuid.UUID(hex=digest[:32]))
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunStats:
|
||||
mode: str = "sync"
|
||||
documents: int = 0
|
||||
added: int = 0
|
||||
updated: int = 0
|
||||
unchanged: int = 0
|
||||
removed: int = 0
|
||||
chunks_written: int = 0
|
||||
chunks_deleted: int = 0
|
||||
embed_calls: int = 0
|
||||
texts_embedded: int = 0
|
||||
points_sent: int = 0
|
||||
collection_points: int = 0
|
||||
store_chunks: int = 0
|
||||
duration_s: float = 0.0
|
||||
collection: str = ""
|
||||
provider: str = ""
|
||||
model: str = ""
|
||||
dim: int = 0
|
||||
chunker_version: str = CHUNKER_VERSION
|
||||
notes: list = field(default_factory=list)
|
||||
|
||||
def summary(self) -> str:
|
||||
rows = [
|
||||
f"mode={self.mode} documents={self.documents} chunks={self.store_chunks}",
|
||||
f"documents added={self.added} updated={self.updated} unchanged={self.unchanged} removed={self.removed}",
|
||||
f"chunks written={self.chunks_written} deleted={self.chunks_deleted}",
|
||||
f"embedding calls={self.embed_calls} texts={self.texts_embedded}",
|
||||
f"points sent={self.points_sent} collection points={self.collection_points}",
|
||||
f"provider={self.provider} model={self.model} dim={self.dim} chunker={self.chunker_version}",
|
||||
f"collection={self.collection} duration={self.duration_s:.2f}s",
|
||||
]
|
||||
rows.extend(self.notes)
|
||||
return "\n".join(rows)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
def run(cfg, mode: str = "sync", log=None) -> RunStats:
|
||||
"""Build or refresh the corpus. Returns what happened; raises on any error."""
|
||||
if mode not in ("sync", "rebuild"):
|
||||
raise PipelineError(f"unknown mode {mode!r}; expected 'sync' or 'rebuild'")
|
||||
started = time.time()
|
||||
log = log or (lambda _msg: None)
|
||||
stats = RunStats(
|
||||
mode=mode,
|
||||
collection=cfg.collection,
|
||||
provider=cfg.provider,
|
||||
model=cfg.model,
|
||||
dim=cfg.dim,
|
||||
chunker_version=cfg.chunker_version,
|
||||
)
|
||||
|
||||
documents = loader.load_documents(cfg)
|
||||
stats.documents = len(documents)
|
||||
log(f"validated {len(documents)} document(s)")
|
||||
|
||||
provider = embeddings.build_provider(cfg)
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
if not qdrant.health():
|
||||
raise PipelineError(
|
||||
f"Qdrant is not answering at {cfg.qdrant_url}. Start it "
|
||||
"(deploy/qdrant.yml) or point RESUME_QDRANT_URL at a running instance."
|
||||
)
|
||||
|
||||
with store_module.Store(cfg.db_path) as store:
|
||||
contract = _contract(cfg)
|
||||
mode = _check_contract(store, qdrant, cfg, contract, mode)
|
||||
|
||||
_ensure_collection(qdrant, cfg)
|
||||
|
||||
manifest = store.manifest()
|
||||
changed, removed = _classify(documents, manifest, cfg, mode)
|
||||
stats.unchanged = len(documents) - len(changed)
|
||||
stats.removed = len(removed)
|
||||
log(
|
||||
f"incremental plan: {len(changed)} to embed, "
|
||||
f"{stats.unchanged} unchanged, {len(removed)} removed"
|
||||
)
|
||||
|
||||
for path in removed:
|
||||
stats.chunks_deleted += store.delete_document(path)
|
||||
qdrant.delete_by_source(cfg.collection, path)
|
||||
log(f"removed {path}")
|
||||
|
||||
for doc in changed:
|
||||
payloads_for_doc = _prepare(doc, cfg, manifest)
|
||||
stats.chunks_written += len(payloads_for_doc)
|
||||
|
||||
vectors = provider.embed([p["text"] for p in payloads_for_doc])
|
||||
if len(vectors) != len(payloads_for_doc):
|
||||
raise PipelineError(
|
||||
f"{doc.path}: provider returned {len(vectors)} vectors for "
|
||||
f"{len(payloads_for_doc)} chunks"
|
||||
)
|
||||
|
||||
points = []
|
||||
for payload, vector in zip(payloads_for_doc, vectors):
|
||||
if len(vector) != cfg.dim:
|
||||
raise PipelineError(
|
||||
f"{doc.path}: vector dimension {len(vector)} != configured {cfg.dim}"
|
||||
)
|
||||
points.append({"id": payload["id"], "vector": vector, "payload": payload})
|
||||
|
||||
# Order: remove what this document used to have (chunk counts change),
|
||||
# then write the new rows and points. A crash between the two leaves an
|
||||
# index that reports as incomplete, never one that claims to be whole.
|
||||
stats.chunks_deleted += store.delete_document(doc.path)
|
||||
qdrant.delete_by_source(cfg.collection, doc.path)
|
||||
stats.points_sent += qdrant.upsert(cfg.collection, points)
|
||||
store.write_document(doc, payloads_for_doc)
|
||||
|
||||
if doc.path in manifest:
|
||||
stats.updated += 1
|
||||
log(f"updated {doc.path} ({len(points)} chunk(s))")
|
||||
else:
|
||||
stats.added += 1
|
||||
log(f"added {doc.path} ({len(points)} chunk(s))")
|
||||
|
||||
provider_stats = provider.stats()
|
||||
stats.embed_calls = provider_stats["calls"]
|
||||
stats.texts_embedded = provider_stats["texts"]
|
||||
stats.store_chunks = store.chunk_count()
|
||||
stats.collection_points = qdrant.point_count(cfg.collection)
|
||||
|
||||
if stats.collection_points != stats.store_chunks:
|
||||
raise PipelineError(
|
||||
"index is inconsistent after the run: "
|
||||
f"{stats.collection_points} point(s) in Qdrant but "
|
||||
f"{stats.store_chunks} chunk(s) in SQLite. Run `python3 -m corpus.cli verify`."
|
||||
)
|
||||
|
||||
store.set_meta("collection", contract)
|
||||
store.set_meta("last_run", {**stats.to_dict(), "finished_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())})
|
||||
_write_collection_file(cfg, store, stats)
|
||||
|
||||
stats.duration_s = round(time.time() - started, 3)
|
||||
return stats
|
||||
|
||||
|
||||
def _contract(cfg) -> dict:
|
||||
"""What this collection *is*. Recorded so a change is a migration, not drift."""
|
||||
return {
|
||||
"provider": cfg.provider,
|
||||
"model": cfg.model,
|
||||
"dim": cfg.dim,
|
||||
"chunker_version": cfg.chunker_version,
|
||||
"distance": "Cosine",
|
||||
}
|
||||
|
||||
|
||||
def _check_contract(store, qdrant, cfg, contract: dict, mode: str) -> str:
|
||||
stored = store.get_meta("collection")
|
||||
if mode == "rebuild":
|
||||
qdrant.drop_collection(cfg.collection)
|
||||
store.reset()
|
||||
return mode
|
||||
if stored is None:
|
||||
# No contract yet: either a fresh store, or one whose database was lost.
|
||||
if qdrant.collection_exists(cfg.collection) and qdrant.point_count(cfg.collection) > 0:
|
||||
raise PipelineError(
|
||||
f"collection {cfg.collection!r} holds points but this store has no "
|
||||
"collection contract (was data/corpus/corpus.db deleted?). "
|
||||
"Confirm you want to overwrite it: python3 -m corpus.cli rebuild"
|
||||
)
|
||||
return mode
|
||||
if stored != contract:
|
||||
raise PipelineError(
|
||||
"the collection contract does not match this configuration — refusing to "
|
||||
"mix vectors in one collection (docs/phase-4-corpus.md §4.3).\n"
|
||||
f" stored: {json.dumps(stored, sort_keys=True)}\n"
|
||||
f" config: {json.dumps(contract, sort_keys=True)}\n"
|
||||
"A provider or chunker change is a governed migration: run "
|
||||
"`python3 -m corpus.cli rebuild` once Sam has approved the change."
|
||||
)
|
||||
return mode
|
||||
|
||||
|
||||
def _ensure_collection(qdrant: Qdrant, cfg) -> None:
|
||||
if not qdrant.collection_exists(cfg.collection):
|
||||
qdrant.create_collection(
|
||||
cfg.collection, cfg.dim, vectors_on_disk=cfg.vectors_on_disk
|
||||
)
|
||||
return
|
||||
info = qdrant.collection_info(cfg.collection)
|
||||
vectors = ((info.get("config") or {}).get("params") or {}).get("vectors")
|
||||
size = None
|
||||
if isinstance(vectors, dict):
|
||||
size = vectors.get("size")
|
||||
if size is not None and int(size) != int(cfg.dim):
|
||||
raise PipelineError(
|
||||
f"collection {cfg.collection!r} has vectors of size {size} but this run "
|
||||
f"is configured for {cfg.dim}. That is a governed migration: rebuild."
|
||||
)
|
||||
|
||||
|
||||
def _classify(documents: list, manifest: dict, cfg, mode: str):
|
||||
"""Which documents need work, and which are gone."""
|
||||
changed = []
|
||||
on_disk = set()
|
||||
for doc in documents:
|
||||
on_disk.add(doc.path)
|
||||
if mode == "rebuild":
|
||||
changed.append(doc)
|
||||
continue
|
||||
entry = manifest.get(doc.path)
|
||||
if entry is None:
|
||||
changed.append(doc)
|
||||
continue
|
||||
if (
|
||||
entry["content_hash"] != doc.digest
|
||||
or entry["embedding_model"] != cfg.model
|
||||
or int(entry["embedding_dim"]) != int(cfg.dim)
|
||||
or entry["chunker_version"] != cfg.chunker_version
|
||||
):
|
||||
changed.append(doc)
|
||||
removed = sorted(path for path in manifest if path not in on_disk)
|
||||
return changed, removed
|
||||
|
||||
|
||||
def _prepare(doc, cfg, manifest: dict) -> list:
|
||||
"""Chunk one document and build validated payloads. No network, no writes."""
|
||||
try:
|
||||
chunks = chunker.chunk_document(doc.body)
|
||||
except chunker.ChunkError as exc:
|
||||
raise PipelineError(f"{doc.path}: {exc}") from exc
|
||||
if not chunks:
|
||||
raise PipelineError(f"{doc.path}: produced no chunks (empty body?)")
|
||||
doc.chunk_count = len(chunks)
|
||||
|
||||
out = []
|
||||
for chunk in chunks:
|
||||
payload = payloads.build_payload(doc, chunk, cfg)
|
||||
payload["id"] = chunk_id(doc.path, chunk.index, doc.digest)
|
||||
try:
|
||||
payloads.validate_payload(payload, where=f"{doc.path}#chunk-{chunk.index}")
|
||||
except payloads.PayloadError as exc:
|
||||
raise PipelineError(str(exc)) from exc
|
||||
out.append(payload)
|
||||
return out
|
||||
|
||||
|
||||
def _write_collection_file(cfg, store, stats: RunStats) -> None:
|
||||
"""Mirror the contract to data/corpus/collection.json for the operator."""
|
||||
body = {
|
||||
"collection": cfg.collection,
|
||||
"contract": _contract(cfg),
|
||||
"qdrant_url": cfg.qdrant_url,
|
||||
"chunks": store.chunk_count(),
|
||||
"sources": store.source_count(),
|
||||
"last_run": store.get_meta("last_run"),
|
||||
}
|
||||
with open(cfg.meta_path, "w", encoding="utf-8") as handle:
|
||||
json.dump(body, handle, indent=2, sort_keys=True)
|
||||
handle.write("\n")
|
||||
|
||||
|
||||
# -- verification and demonstration ---------------------------------------
|
||||
|
||||
|
||||
def verify(cfg, log=None) -> list:
|
||||
"""Return a list of problems. Empty means the index agrees with content/."""
|
||||
log = log or (lambda _msg: None)
|
||||
problems = []
|
||||
documents = loader.load_documents(cfg)
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
if not qdrant.health():
|
||||
return [f"Qdrant is not answering at {cfg.qdrant_url}"]
|
||||
|
||||
contract = _contract(cfg)
|
||||
with store_module.Store(cfg.db_path) as store:
|
||||
stored = store.get_meta("collection")
|
||||
if stored != contract:
|
||||
problems.append(f"collection contract mismatch: stored={stored} config={contract}")
|
||||
|
||||
if not qdrant.collection_exists(cfg.collection):
|
||||
problems.append(f"collection {cfg.collection!r} does not exist")
|
||||
return problems
|
||||
|
||||
info = qdrant.collection_info(cfg.collection)
|
||||
size = (((info.get("config") or {}).get("params") or {}).get("vectors") or {}).get("size")
|
||||
if size is not None and int(size) != cfg.dim:
|
||||
problems.append(f"collection vector size {size} != configured {cfg.dim}")
|
||||
|
||||
points_by_source = qdrant.scroll_sources(cfg.collection)
|
||||
rows = store.chunk_payloads()
|
||||
by_source = {}
|
||||
for row in rows:
|
||||
by_source.setdefault(row["source_path"], []).append(row)
|
||||
try:
|
||||
payloads.validate_payload(row, where=f"{row['source_path']}#chunk-{row['chunk_index']}")
|
||||
except payloads.PayloadError as exc:
|
||||
problems.append(str(exc))
|
||||
|
||||
for doc in documents:
|
||||
payload_rows = by_source.get(doc.path, [])
|
||||
if not payload_rows:
|
||||
problems.append(f"{doc.path}: no chunks in the index (run sync)")
|
||||
continue
|
||||
if payload_rows[0]["content_hash"] != doc.digest:
|
||||
problems.append(f"{doc.path}: source changed since it was embedded")
|
||||
expected = len(payload_rows)
|
||||
actual = points_by_source.get(doc.path, 0)
|
||||
if actual != expected:
|
||||
problems.append(
|
||||
f"{doc.path}: {expected} chunk row(s) but {actual} vector(s) in Qdrant"
|
||||
)
|
||||
|
||||
for source in sorted(set(by_source) - {doc.path for doc in documents}):
|
||||
problems.append(f"{source}: indexed but no longer in content/ (run sync)")
|
||||
|
||||
total_points = sum(points_by_source.values())
|
||||
if total_points != store.chunk_count():
|
||||
problems.append(
|
||||
f"{total_points} vector(s) in Qdrant but {store.chunk_count()} chunk(s) in SQLite"
|
||||
)
|
||||
log(
|
||||
f"verify: {store.chunk_count()} chunk(s) across {store.source_count()} source(s), "
|
||||
f"{total_points} vector(s) in Qdrant"
|
||||
)
|
||||
return problems
|
||||
|
||||
|
||||
def demo_area_filter(cfg, query: str, area: str = None, limit: int = 5) -> dict:
|
||||
"""Same query with and without an `area` filter — the DoD's filter evidence."""
|
||||
provider = embeddings.build_provider(cfg)
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
vector = provider.embed([query])[0]
|
||||
unfiltered = qdrant.search(cfg.collection, vector, limit=limit)
|
||||
filtered = qdrant.search(cfg.collection, vector, limit=limit, area=area) if area else []
|
||||
return {
|
||||
"query": query,
|
||||
"area": area,
|
||||
"unfiltered": [_hit(row) for row in unfiltered],
|
||||
"filtered": [_hit(row) for row in filtered],
|
||||
}
|
||||
|
||||
|
||||
def _hit(row: dict) -> dict:
|
||||
payload = row.get("payload") or {}
|
||||
return {
|
||||
"score": round(row["score"], 4),
|
||||
"area": payload.get("area"),
|
||||
"confidence": payload.get("confidence"),
|
||||
"title": payload.get("title"),
|
||||
"url": payload.get("url"),
|
||||
"source_path": payload.get("source_path"),
|
||||
"chunk_index": payload.get("chunk_index"),
|
||||
"preview": (payload.get("text") or "").replace("\n", " ")[:120],
|
||||
}
|
||||
|
||||
|
||||
def hybrid_search(cfg, query: str, area: str = None, limit: int = 5) -> list:
|
||||
"""Vectors + FTS5 fused with reciprocal rank fusion (decision D10).
|
||||
|
||||
Phase 5 owns the query graph; this is here because Phase 4 is the phase that
|
||||
has to prove the two halves line up — same ids, same metadata, one ranking.
|
||||
"""
|
||||
provider = embeddings.build_provider(cfg)
|
||||
qdrant = Qdrant(cfg.qdrant_url, timeout=cfg.timeout)
|
||||
vector = provider.embed([query])[0]
|
||||
ranked = {}
|
||||
for rank, hit in enumerate(qdrant.search(cfg.collection, vector, limit=limit * 4, area=area)):
|
||||
entry = ranked.setdefault(hit["id"], {"id": hit["id"], "payload": hit["payload"], "score": 0.0})
|
||||
entry["score"] += 1.0 / (60 + rank)
|
||||
with store_module.Store(cfg.db_path) as store:
|
||||
for rank, hit in enumerate(store.keyword_search(query, area=area, limit=limit * 4)):
|
||||
entry = ranked.setdefault(hit["id"], {"id": hit["id"], "payload": {}, "score": 0.0})
|
||||
entry.setdefault("keyword", hit["snippet"])
|
||||
entry["score"] += 1.0 / (60 + rank)
|
||||
ordered = sorted(ranked.values(), key=lambda row: row["score"], reverse=True)[:limit]
|
||||
for row in ordered:
|
||||
payload = row.get("payload") or {}
|
||||
row["title"] = payload.get("title")
|
||||
row["url"] = payload.get("url")
|
||||
return ordered
|
||||
269
corpus/qdrant.py
Normal file
269
corpus/qdrant.py
Normal file
@@ -0,0 +1,269 @@
|
||||
"""Qdrant REST client — standard library only.
|
||||
|
||||
No `qdrant-client` dependency: the four calls this pipeline needs are HTTP
|
||||
requests, and AGENTS.md §4.1 freezes the stack. `_request` is the single
|
||||
transport hook, which is what the tests replace to run the whole pipeline
|
||||
without a container.
|
||||
|
||||
Qdrant is bound to `127.0.0.1:6333` on the runtime host (`.13`) and is never
|
||||
publicly reachable; Caddy is the only public listener.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import http.client
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
DISTANCE_COSINE = "Cosine"
|
||||
|
||||
# The runtime host (.13) is memory-bound: 2.3 GiB free and 8 GiB of swap already
|
||||
# in use, so Qdrant gets a 400 m cap and must live inside it
|
||||
# (reference/stack.md). Measured on 2026-10-26: the *default* HNSW and payload
|
||||
# settings are OOM-killed at the cap between 5,000 and 20,000 chunks of 1024
|
||||
# dimensions. Keeping the HNSW graph and the payloads on disk, and using a
|
||||
# smaller graph (m=8, ef_construct=64), fits the cap because the kernel can
|
||||
# reclaim those page-cache pages instead of killing the process. Vectors stay in
|
||||
# RAM because they are the part search reads on every query.
|
||||
#
|
||||
# The trade is build time and a little search latency; the corpus is ~50 chunks
|
||||
# today and the public site has no query burst. See docs/phase-4-notes.md §7.
|
||||
MEMORY_FRUGAL_DEFAULTS = {
|
||||
"on_disk_payload": True,
|
||||
"hnsw_m": 8,
|
||||
"hnsw_ef_construct": 64,
|
||||
"vectors_on_disk": False,
|
||||
}
|
||||
|
||||
# Segment behaviour under a cap. `memmap_threshold` makes Qdrant mmap segments
|
||||
# this size and larger (page cache the kernel can reclaim instead of OOM), and
|
||||
# `indexing_threshold` is raised so a bulk ingest does not build the HNSW graph
|
||||
# while it is still writing segments. Measured: 20,000 chunks at 1024
|
||||
# dimensions peaked at 296 MiB during ingest and settled to 96 MiB.
|
||||
MEMORY_FRUGAL_OPTIMIZERS = {
|
||||
"default_segment_number": 2,
|
||||
"memmap_threshold": 5000,
|
||||
"indexing_threshold": 20000,
|
||||
}
|
||||
|
||||
|
||||
class QdrantError(RuntimeError):
|
||||
"""A Qdrant call failed, or Qdrant is not reachable."""
|
||||
|
||||
|
||||
class Qdrant:
|
||||
def __init__(self, url: str = "http://127.0.0.1:6333", timeout: float = 30.0, attempts: int = 3) -> None:
|
||||
self.url = url.rstrip("/")
|
||||
self.timeout = timeout
|
||||
self.attempts = max(1, attempts)
|
||||
|
||||
# -- transport --------------------------------------------------------
|
||||
|
||||
def _request(self, method: str, path: str, body=None) -> dict:
|
||||
"""One HTTP call with retry/backoff. The only place that touches the network."""
|
||||
data = None if body is None else json.dumps(body).encode("utf-8")
|
||||
last_error = None
|
||||
for attempt in range(self.attempts):
|
||||
request = urllib.request.Request(
|
||||
f"{self.url}{path}",
|
||||
data=data,
|
||||
method=method,
|
||||
headers={"Content-Type": "application/json"} if data else {},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=self.timeout) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
return json.loads(raw) if raw.strip() else {}
|
||||
except urllib.error.HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8", "replace")[:400]
|
||||
exc.close()
|
||||
if exc.code == 404:
|
||||
raise QdrantNotFound(f"{method} {path}: {detail}") from None
|
||||
if exc.code != 429 and exc.code < 500:
|
||||
raise QdrantError(f"{method} {path} failed: HTTP {exc.code} {detail}") from None
|
||||
last_error = f"HTTP {exc.code} {detail}"
|
||||
except (urllib.error.URLError, TimeoutError, http.client.HTTPException, ConnectionError) as exc:
|
||||
# RemoteDisconnected and friends arrive here when the server dies
|
||||
# mid-request (an OOM kill looks exactly like this).
|
||||
last_error = f"cannot reach {self.url}: {getattr(exc, 'reason', exc)}"
|
||||
if attempt + 1 < self.attempts:
|
||||
time.sleep(0.5 * (2 ** attempt))
|
||||
raise QdrantError(f"{method} {path} failed after {self.attempts} attempts: {last_error}")
|
||||
|
||||
# -- cluster ----------------------------------------------------------
|
||||
|
||||
def health(self) -> bool:
|
||||
"""True when Qdrant answers. Never raises."""
|
||||
try:
|
||||
self._request("GET", "/")
|
||||
return True
|
||||
except (QdrantError, QdrantNotFound):
|
||||
return False
|
||||
|
||||
def version(self) -> str:
|
||||
response = self._request("GET", "/")
|
||||
return str(response.get("version", "unknown"))
|
||||
|
||||
# -- collections ------------------------------------------------------
|
||||
|
||||
def collection_exists(self, name: str) -> bool:
|
||||
try:
|
||||
self._request("GET", f"/collections/{name}")
|
||||
return True
|
||||
except QdrantNotFound:
|
||||
return False
|
||||
|
||||
def create_collection(
|
||||
self,
|
||||
name: str,
|
||||
dim: int,
|
||||
distance: str = DISTANCE_COSINE,
|
||||
on_disk_payload: bool = None,
|
||||
hnsw_m: int = None,
|
||||
hnsw_ef_construct: int = None,
|
||||
vectors_on_disk: bool = None,
|
||||
) -> None:
|
||||
"""Create a collection sized for a memory-bound host (see MEMORY_FRUGAL_DEFAULTS)."""
|
||||
defaults = MEMORY_FRUGAL_DEFAULTS
|
||||
body = {
|
||||
"vectors": {
|
||||
"size": int(dim),
|
||||
"distance": distance,
|
||||
"on_disk": bool(
|
||||
defaults["vectors_on_disk"] if vectors_on_disk is None else vectors_on_disk
|
||||
),
|
||||
},
|
||||
"hnsw_config": {
|
||||
"m": int(defaults["hnsw_m"] if hnsw_m is None else hnsw_m),
|
||||
"ef_construct": int(
|
||||
defaults["hnsw_ef_construct"] if hnsw_ef_construct is None else hnsw_ef_construct
|
||||
),
|
||||
"on_disk": True,
|
||||
},
|
||||
"on_disk_payload": bool(
|
||||
defaults["on_disk_payload"] if on_disk_payload is None else on_disk_payload
|
||||
),
|
||||
"optimizers_config": dict(MEMORY_FRUGAL_OPTIMIZERS),
|
||||
}
|
||||
self._request("PUT", f"/collections/{name}", body)
|
||||
|
||||
def drop_collection(self, name: str) -> None:
|
||||
try:
|
||||
self._request("DELETE", f"/collections/{name}")
|
||||
except QdrantNotFound:
|
||||
pass
|
||||
|
||||
def collection_info(self, name: str) -> dict:
|
||||
response = self._request("GET", f"/collections/{name}")
|
||||
return dict(response.get("result") or {})
|
||||
|
||||
def point_count(self, name: str) -> int:
|
||||
response = self._request("POST", f"/collections/{name}/points/count", {"exact": True})
|
||||
return int((response.get("result") or {}).get("count", 0))
|
||||
|
||||
# -- points -----------------------------------------------------------
|
||||
|
||||
def upsert(self, name: str, points: list, batch: int = 256) -> int:
|
||||
"""Upsert points in batches. Returns the number of points sent."""
|
||||
sent = 0
|
||||
for start in range(0, len(points), max(1, batch)):
|
||||
chunk = points[start : start + max(1, batch)]
|
||||
self._request(
|
||||
"PUT",
|
||||
f"/collections/{name}/points?wait=true",
|
||||
{"points": chunk},
|
||||
)
|
||||
sent += len(chunk)
|
||||
return sent
|
||||
|
||||
def delete_by_source(self, name: str, source_path: str) -> None:
|
||||
"""Delete every point that came from one document.
|
||||
|
||||
Re-chunking a document can produce fewer chunks than before, so the old
|
||||
points are removed rather than overwritten. Deterministic ids make
|
||||
duplicates impossible; this makes orphans impossible.
|
||||
"""
|
||||
self._request(
|
||||
"POST",
|
||||
f"/collections/{name}/points/delete?wait=true",
|
||||
{"filter": {"must": [{"key": "source_path", "match": {"value": source_path}}]}},
|
||||
)
|
||||
|
||||
def search(
|
||||
self,
|
||||
name: str,
|
||||
vector: list,
|
||||
limit: int = 5,
|
||||
area: str = None,
|
||||
confidence: str = None,
|
||||
with_payload: bool = True,
|
||||
) -> list:
|
||||
"""Nearest neighbours, optionally filtered *before* the search."""
|
||||
body = {
|
||||
"query": [float(value) for value in vector],
|
||||
"limit": int(limit),
|
||||
"with_payload": bool(with_payload),
|
||||
}
|
||||
filters = _filter_conditions(area, confidence)
|
||||
if filters:
|
||||
body["filter"] = {"must": filters}
|
||||
try:
|
||||
response = self._request("POST", f"/collections/{name}/points/query", body)
|
||||
points = ((response.get("result") or {}).get("points")) or []
|
||||
except QdrantNotFound:
|
||||
# Older Qdrant exposes the same operation as /points/search.
|
||||
response = self._request("POST", f"/collections/{name}/points/search", body)
|
||||
points = response.get("result") or []
|
||||
return [
|
||||
{
|
||||
"id": point.get("id"),
|
||||
"score": float(point.get("score") or 0.0),
|
||||
"payload": point.get("payload") or {},
|
||||
}
|
||||
for point in points
|
||||
]
|
||||
|
||||
def scroll_sources(self, name: str, limit: int = 1000) -> dict:
|
||||
"""Map source_path -> number of vectors, for consistency checks."""
|
||||
counts = {}
|
||||
offset = None
|
||||
while True:
|
||||
body = {"limit": limit, "with_payload": ["source_path"], "with_vector": False}
|
||||
if offset is not None:
|
||||
body["offset"] = offset
|
||||
response = self._request("POST", f"/collections/{name}/points/scroll", body)
|
||||
result = response.get("result") or {}
|
||||
points = result.get("points") or []
|
||||
for point in points:
|
||||
source = (point.get("payload") or {}).get("source_path", "<missing>")
|
||||
counts[source] = counts.get(source, 0) + 1
|
||||
offset = result.get("next_page_offset")
|
||||
if not points or offset is None:
|
||||
return counts
|
||||
|
||||
def metrics(self) -> str:
|
||||
"""Prometheus text from Qdrant's /metrics route (used by the memory measurement).
|
||||
|
||||
Returns "" when the route is not exposed, so callers can fall back to
|
||||
reading the container's own memory counters.
|
||||
"""
|
||||
try:
|
||||
with urllib.request.urlopen(f"{self.url}/metrics", timeout=self.timeout) as response:
|
||||
return response.read().decode("utf-8", "replace")
|
||||
except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError):
|
||||
return ""
|
||||
|
||||
|
||||
class QdrantNotFound(QdrantError):
|
||||
"""A 404 from Qdrant: the collection or route does not exist."""
|
||||
|
||||
|
||||
def _filter_conditions(area: str = None, confidence: str = None) -> list:
|
||||
conditions = []
|
||||
if area:
|
||||
conditions.append({"key": "area", "match": {"value": area}})
|
||||
if confidence:
|
||||
conditions.append({"key": "confidence", "match": {"value": confidence}})
|
||||
return conditions
|
||||
295
corpus/store.py
Normal file
295
corpus/store.py
Normal file
@@ -0,0 +1,295 @@
|
||||
"""SQLite: the keyword index and the incremental state.
|
||||
|
||||
Two jobs, one file (`RESUME_CORPUS_DB`, default `data/corpus/corpus.db`):
|
||||
|
||||
* **`chunk_fts`** — an FTS5 index over chunk text with the same filter columns
|
||||
the vectors carry. This is the keyword half of hybrid retrieval (decision
|
||||
D10). Phase 2 indexes *pages* for the site's own search box; this indexes
|
||||
*chunks*, because retrieval hands the model a passage, not a page.
|
||||
* **`manifest`** — one row per source document: its content hash, its chunk
|
||||
count and the embedding model/dimension it was embedded with. Incremental mode
|
||||
is a hash comparison against this table, and nothing else.
|
||||
|
||||
`meta` records the collection contract (provider, model, dimension, chunker
|
||||
version) so a provider change is detected instead of silently corrupting the
|
||||
index.
|
||||
|
||||
Standard library `sqlite3` only. The same file is opened read-only by the Phase
|
||||
5 retrieval service.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sqlite3
|
||||
import time
|
||||
import unicodedata
|
||||
|
||||
SCHEMA_VERSION = "1"
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS chunks (
|
||||
id TEXT PRIMARY KEY,
|
||||
source_path TEXT NOT NULL,
|
||||
source_id TEXT NOT NULL,
|
||||
chunk_index INTEGER NOT NULL,
|
||||
area TEXT NOT NULL,
|
||||
level INTEGER NOT NULL,
|
||||
source_type TEXT NOT NULL,
|
||||
confidence TEXT NOT NULL,
|
||||
period TEXT NOT NULL,
|
||||
last_verified TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
claim TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
content_hash TEXT NOT NULL,
|
||||
embedding_model TEXT NOT NULL,
|
||||
embedding_dim INTEGER NOT NULL,
|
||||
chunker_version TEXT NOT NULL,
|
||||
chunk_tokens INTEGER NOT NULL,
|
||||
public INTEGER NOT NULL,
|
||||
artefacts TEXT NOT NULL,
|
||||
tech TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
text TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS chunks_source ON chunks(source_path);
|
||||
CREATE INDEX IF NOT EXISTS chunks_area ON chunks(area);
|
||||
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS chunk_fts USING fts5(
|
||||
id UNINDEXED,
|
||||
source_path UNINDEXED,
|
||||
title,
|
||||
terms,
|
||||
text,
|
||||
area UNINDEXED,
|
||||
confidence UNINDEXED,
|
||||
tokenize = 'porter unicode61 remove_diacritics 2'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS manifest (
|
||||
source_path TEXT PRIMARY KEY,
|
||||
content_hash TEXT NOT NULL,
|
||||
chunk_count INTEGER NOT NULL,
|
||||
embedding_model TEXT NOT NULL,
|
||||
embedding_dim INTEGER NOT NULL,
|
||||
chunker_version TEXT NOT NULL,
|
||||
last_verified TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS meta (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
# Column order used by every INSERT in this module.
|
||||
_CHUNK_COLUMNS = (
|
||||
"id", "source_path", "source_id", "chunk_index", "area", "level", "source_type",
|
||||
"confidence", "period", "last_verified", "title", "claim", "url", "content_hash",
|
||||
"embedding_model", "embedding_dim", "chunker_version", "chunk_tokens",
|
||||
"public", "artefacts", "tech", "tags", "text",
|
||||
)
|
||||
|
||||
_JSON_FIELDS = ("artefacts", "tech", "tags")
|
||||
|
||||
|
||||
class StoreError(RuntimeError):
|
||||
"""The corpus database is unreadable or inconsistent."""
|
||||
|
||||
|
||||
class Store:
|
||||
def __init__(self, path: str) -> None:
|
||||
self.path = path
|
||||
os.makedirs(os.path.dirname(os.path.abspath(path)) or ".", exist_ok=True)
|
||||
self.db = sqlite3.connect(path)
|
||||
self.db.row_factory = sqlite3.Row
|
||||
self.db.execute("PRAGMA journal_mode = WAL")
|
||||
self.db.execute("PRAGMA synchronous = NORMAL")
|
||||
self.db.executescript(SCHEMA)
|
||||
self.set_meta("schema_version", SCHEMA_VERSION)
|
||||
|
||||
# -- lifecycle --------------------------------------------------------
|
||||
|
||||
def close(self) -> None:
|
||||
self.db.close()
|
||||
|
||||
def __enter__(self) -> "Store":
|
||||
return self
|
||||
|
||||
def __exit__(self, *_exc) -> None:
|
||||
self.close()
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Empty every table. Used by a from-scratch rebuild."""
|
||||
for table in ("chunk_fts", "chunks", "manifest", "meta"):
|
||||
self.db.execute(f"DELETE FROM {table}")
|
||||
self.db.commit()
|
||||
|
||||
# -- writing ----------------------------------------------------------
|
||||
|
||||
def write_document(self, doc, payloads: list) -> None:
|
||||
"""Replace all rows for one document: chunks, FTS rows and manifest."""
|
||||
self.delete_document(doc.path, keep_manifest=True)
|
||||
for payload in payloads:
|
||||
values = []
|
||||
for column in _CHUNK_COLUMNS:
|
||||
value = payload[column]
|
||||
if column in _JSON_FIELDS:
|
||||
value = json.dumps(value, sort_keys=True)
|
||||
values.append(value)
|
||||
cursor = self.db.execute(
|
||||
f"INSERT INTO chunks ({', '.join(_CHUNK_COLUMNS)}) "
|
||||
f"VALUES ({', '.join('?' for _ in _CHUNK_COLUMNS)})",
|
||||
values,
|
||||
)
|
||||
# `terms` is the keyword surface: the words a visitor or a recruiter
|
||||
# would type that live in front-matter rather than the body — tech
|
||||
# names, tags, the one-line claim. Without it, a search for "zigbee"
|
||||
# misses a page whose body says "mesh". It is indexed, never retrieved.
|
||||
terms = " ".join(
|
||||
str(value)
|
||||
for value in (
|
||||
payload["claim"],
|
||||
payload["title"],
|
||||
" ".join(payload["tech"]),
|
||||
" ".join(payload["tags"]),
|
||||
)
|
||||
if value
|
||||
)
|
||||
self.db.execute(
|
||||
"INSERT INTO chunk_fts (rowid, id, source_path, title, terms, text, area, confidence) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
cursor.lastrowid,
|
||||
payload["id"],
|
||||
payload["source_path"],
|
||||
payload["title"],
|
||||
terms,
|
||||
payload["text"],
|
||||
payload["area"],
|
||||
payload["confidence"],
|
||||
),
|
||||
)
|
||||
self.db.execute(
|
||||
"INSERT OR REPLACE INTO manifest "
|
||||
"(source_path, content_hash, chunk_count, embedding_model, embedding_dim, "
|
||||
" chunker_version, last_verified, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
doc.path,
|
||||
doc.digest,
|
||||
len(payloads),
|
||||
payloads[0]["embedding_model"] if payloads else "",
|
||||
payloads[0]["embedding_dim"] if payloads else 0,
|
||||
payloads[0]["chunker_version"] if payloads else "",
|
||||
doc.last_verified,
|
||||
time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
),
|
||||
)
|
||||
self.db.commit()
|
||||
|
||||
def delete_document(self, source_path: str, keep_manifest: bool = False) -> int:
|
||||
rowids = [
|
||||
row["rowid"]
|
||||
for row in self.db.execute("SELECT rowid FROM chunks WHERE source_path = ?", (source_path,))
|
||||
]
|
||||
for rowid in rowids:
|
||||
self.db.execute("DELETE FROM chunk_fts WHERE rowid = ?", (rowid,))
|
||||
self.db.execute("DELETE FROM chunks WHERE source_path = ?", (source_path,))
|
||||
if not keep_manifest:
|
||||
self.db.execute("DELETE FROM manifest WHERE source_path = ?", (source_path,))
|
||||
self.db.commit()
|
||||
return len(rowids)
|
||||
|
||||
# -- reading ----------------------------------------------------------
|
||||
|
||||
def manifest(self) -> dict:
|
||||
return {
|
||||
row["source_path"]: dict(row)
|
||||
for row in self.db.execute("SELECT * FROM manifest")
|
||||
}
|
||||
|
||||
def chunk_count(self) -> int:
|
||||
return int(self.db.execute("SELECT count(*) FROM chunks").fetchone()[0])
|
||||
|
||||
def source_count(self) -> int:
|
||||
return int(self.db.execute("SELECT count(DISTINCT source_path) FROM chunks").fetchone()[0])
|
||||
|
||||
def chunk_ids(self) -> list:
|
||||
return [row["id"] for row in self.db.execute("SELECT id FROM chunks ORDER BY id")]
|
||||
|
||||
def chunk_payloads(self, source_path: str = None) -> list:
|
||||
"""Every stored payload, optionally for one document. Used by verify()."""
|
||||
sql = "SELECT * FROM chunks"
|
||||
args = ()
|
||||
if source_path:
|
||||
sql += " WHERE source_path = ?"
|
||||
args = (source_path,)
|
||||
sql += " ORDER BY source_path, chunk_index"
|
||||
out = []
|
||||
for row in self.db.execute(sql, args):
|
||||
payload = dict(row)
|
||||
for field in _JSON_FIELDS:
|
||||
payload[field] = json.loads(payload[field])
|
||||
out.append(payload)
|
||||
return out
|
||||
|
||||
def keyword_search(self, query: str, area: str = None, confidence: str = None, limit: int = 10) -> list:
|
||||
"""BM25 over chunk text, optionally filtered before ranking."""
|
||||
match = match_expression(query)
|
||||
if not match:
|
||||
return []
|
||||
sql = [
|
||||
"SELECT c.id, c.url, c.title, c.area, c.confidence, c.source_path,",
|
||||
" c.chunk_index, snippet(chunk_fts, 4, '<mark>', '</mark>', '…', 18) AS snippet,",
|
||||
" bm25(chunk_fts) AS score",
|
||||
"FROM chunk_fts JOIN chunks c ON c.rowid = chunk_fts.rowid",
|
||||
"WHERE chunk_fts MATCH ?",
|
||||
]
|
||||
args = [match]
|
||||
if area:
|
||||
sql.append("AND c.area = ?")
|
||||
args.append(area)
|
||||
if confidence:
|
||||
sql.append("AND c.confidence = ?")
|
||||
args.append(confidence)
|
||||
sql.append("ORDER BY bm25(chunk_fts) LIMIT ?")
|
||||
args.append(int(limit))
|
||||
rows = self.db.execute(" ".join(sql), args)
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
# -- meta -------------------------------------------------------------
|
||||
|
||||
def set_meta(self, key: str, value) -> None:
|
||||
self.db.execute(
|
||||
"INSERT OR REPLACE INTO meta (key, value) VALUES (?, ?)",
|
||||
(key, json.dumps(value, sort_keys=True)),
|
||||
)
|
||||
self.db.commit()
|
||||
|
||||
def get_meta(self, key: str, default=None):
|
||||
row = self.db.execute("SELECT value FROM meta WHERE key = ?", (key,)).fetchone()
|
||||
if row is None:
|
||||
return default
|
||||
return json.loads(row["value"])
|
||||
|
||||
def meta(self) -> dict:
|
||||
return {row["key"]: json.loads(row["value"]) for row in self.db.execute("SELECT key, value FROM meta")}
|
||||
|
||||
|
||||
def match_expression(query: str) -> str:
|
||||
"""Free text -> a safe FTS5 expression: bare words, each a prefix, all required.
|
||||
|
||||
Mirrors `MatchExpression` in internal/search/search.go so the site's search
|
||||
box and the retrieval path treat a visitor's words identically. FTS5
|
||||
operators in the input are never honoured.
|
||||
"""
|
||||
terms = []
|
||||
for raw in re.split(r"[^0-9a-zA-Z\-]+", unicodedata.normalize("NFKC", query or "").lower()):
|
||||
term = raw.strip("-")
|
||||
if term:
|
||||
terms.append('"' + term.replace('"', "") + '"*')
|
||||
return " AND ".join(terms)
|
||||
0
corpus/tests/__init__.py
Normal file
0
corpus/tests/__init__.py
Normal file
112
corpus/tests/fake_qdrant.py
Normal file
112
corpus/tests/fake_qdrant.py
Normal file
@@ -0,0 +1,112 @@
|
||||
"""An in-memory stand-in for Qdrant, used by the pipeline tests.
|
||||
|
||||
It implements the routes the pipeline actually calls and nothing else, and it is
|
||||
deliberately dumb: brute-force cosine over a dict. If the pipeline passes
|
||||
against this, the pipeline's own logic is right; the real server is exercised
|
||||
separately in `docs/phase-4-notes.md` (rebuild against a live container).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from corpus.qdrant import Qdrant, QdrantError, QdrantNotFound
|
||||
|
||||
|
||||
class FakeQdrant(Qdrant):
|
||||
def __init__(self, dim: int):
|
||||
super().__init__(url="fake://qdrant", timeout=1.0, attempts=1)
|
||||
self.dim = dim
|
||||
self.collections = {} # name -> {id: {"vector": [...], "payload": {...}}}
|
||||
self.calls = []
|
||||
|
||||
# -- transport --------------------------------------------------------
|
||||
|
||||
def _request(self, method: str, path: str, body=None) -> dict:
|
||||
self.calls.append((method, path, body))
|
||||
query = ""
|
||||
if "?" in path:
|
||||
path, query = path.split("?", 1)
|
||||
parts = [part for part in path.split("/") if part]
|
||||
|
||||
if not parts:
|
||||
return {"version": "fake-1.0"}
|
||||
|
||||
if parts[0] != "collections":
|
||||
raise QdrantNotFound(f"no route {path}")
|
||||
if len(parts) == 2:
|
||||
name = parts[1]
|
||||
if method == "PUT":
|
||||
self.collections.setdefault(name, {})
|
||||
return {"result": True}
|
||||
if method == "DELETE":
|
||||
if name not in self.collections:
|
||||
raise QdrantNotFound("collection not found")
|
||||
del self.collections[name]
|
||||
return {"result": True}
|
||||
if name not in self.collections:
|
||||
raise QdrantNotFound("collection not found")
|
||||
return {
|
||||
"result": {
|
||||
"points_count": len(self.collections[name]),
|
||||
"config": {"params": {"vectors": {"size": self.dim, "distance": "Cosine"}}},
|
||||
}
|
||||
}
|
||||
name = parts[1]
|
||||
if name not in self.collections:
|
||||
raise QdrantNotFound("collection not found")
|
||||
points = self.collections[name]
|
||||
|
||||
if parts[2:4] == ["points", "count"]:
|
||||
return {"result": {"count": len(points)}}
|
||||
if len(parts) == 3 and parts[2] == "points" and method == "PUT":
|
||||
for point in (body or {}).get("points", []):
|
||||
if len(point["vector"]) != self.dim:
|
||||
raise QdrantError("wrong vector dimension")
|
||||
points[point["id"]] = {"vector": point["vector"], "payload": point["payload"]}
|
||||
return {"result": {"status": "completed"}}
|
||||
if parts[2:4] == ["points", "delete"]:
|
||||
must = (((body or {}).get("filter") or {}).get("must")) or []
|
||||
for point_id in list(points):
|
||||
if _matches(points[point_id]["payload"], must):
|
||||
del points[point_id]
|
||||
return {"result": {"status": "completed"}}
|
||||
if parts[2:4] == ["points", "query"]:
|
||||
vector = body["query"]
|
||||
must = (((body or {}).get("filter") or {}).get("must")) or []
|
||||
scored = []
|
||||
for point_id, point in points.items():
|
||||
if not _matches(point["payload"], must):
|
||||
continue
|
||||
scored.append(
|
||||
{
|
||||
"id": point_id,
|
||||
"score": _cosine(vector, point["vector"]),
|
||||
"payload": point["payload"],
|
||||
}
|
||||
)
|
||||
scored.sort(key=lambda row: row["score"], reverse=True)
|
||||
return {"result": {"points": scored[: int(body.get("limit", 10))]}}
|
||||
if parts[2:4] == ["points", "scroll"]:
|
||||
limit = int((body or {}).get("limit", 100))
|
||||
rows = [
|
||||
{"id": point_id, "payload": {"source_path": point["payload"].get("source_path")}}
|
||||
for point_id, point in list(points.items())[:limit]
|
||||
]
|
||||
return {"result": {"points": rows, "next_page_offset": None}}
|
||||
raise QdrantNotFound(f"no route {path}")
|
||||
|
||||
|
||||
def _matches(payload: dict, conditions: list) -> bool:
|
||||
for condition in conditions:
|
||||
key = condition.get("key")
|
||||
expected = (condition.get("match") or {}).get("value")
|
||||
if payload.get(key) != expected:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _cosine(left: list, right: list) -> float:
|
||||
dot = sum(a * b for a, b in zip(left, right))
|
||||
norm = math.sqrt(sum(a * a for a in left)) * math.sqrt(sum(b * b for b in right))
|
||||
return dot / norm if norm else 0.0
|
||||
186
corpus/tests/support.py
Normal file
186
corpus/tests/support.py
Normal file
@@ -0,0 +1,186 @@
|
||||
"""Shared fixtures for the corpus tests: a throwaway content tree and a fake Qdrant."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
from corpus.config import Config
|
||||
from corpus.tests.fake_qdrant import FakeQdrant
|
||||
|
||||
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
DOC = """---
|
||||
id: {id}
|
||||
area: {area}
|
||||
level: {level}
|
||||
title: "{title}"
|
||||
claim: "{claim}"
|
||||
confidence: {confidence}
|
||||
period: "2023–"
|
||||
tech: [Go, SQLite]
|
||||
metrics: [{{label: endpoints, value: 8}}]
|
||||
artefacts: []
|
||||
public: true
|
||||
last_verified: {last_verified}
|
||||
tags: [{tags}]
|
||||
---
|
||||
|
||||
# {title}
|
||||
|
||||
{body}
|
||||
"""
|
||||
|
||||
FENCE = """A paragraph before the code block explains the constraint.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
func main() {{
|
||||
println("chunk boundary must never land inside this fence")
|
||||
}}
|
||||
```
|
||||
|
||||
A paragraph after the code block.
|
||||
"""
|
||||
|
||||
TABLE = """Measured results, one row per endpoint:
|
||||
|
||||
| endpoint | latency | loss |
|
||||
|---|---|---|
|
||||
| alpha | 12ms | 0.0% |
|
||||
| beta | 14ms | 0.1% |
|
||||
| gamma | 9ms | 0.0% |
|
||||
|
||||
And a closing paragraph.
|
||||
"""
|
||||
|
||||
PROSE = "\n\n".join(
|
||||
f"Paragraph {index} describes a delivery constraint in corporate language, "
|
||||
"with enough words that the chunker has to think about where to break, and "
|
||||
"never inside a fenced block, a table or a front-matter block."
|
||||
for index in range(1, 40)
|
||||
)
|
||||
|
||||
DEFAULT_DOCS = {
|
||||
"ai/ai-overview.md": dict(
|
||||
id="ai-overview", area="ai", level=1, title="Agent Systems",
|
||||
claim="Harnesses, memory and routing.", confidence="running",
|
||||
last_verified="2026-10-01", tags="agents, llm", body=PROSE,
|
||||
),
|
||||
"edge/l3/edge-zigbee.md": dict(
|
||||
id="edge-zigbee", area="edge", level=3, title="Low Power Mesh Sensing",
|
||||
claim="A constrained mesh that survives power loss.", confidence="delivered",
|
||||
last_verified="2026-09-15", tags="zigbee, mesh", body=FENCE,
|
||||
),
|
||||
"platform/l3/platform-containers.md": dict(
|
||||
id="platform-containers", area="platform", level=3, title="Container Estate",
|
||||
claim="Fifty containers with measured resource limits.", confidence="in-progress",
|
||||
last_verified="2026-08-01", tags="docker", body=TABLE,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def temp_repo(docs: dict = None) -> str:
|
||||
"""Create a throwaway repository root with tools/ and content/."""
|
||||
root = tempfile.mkdtemp(prefix="corpus-test-")
|
||||
os.makedirs(os.path.join(root, "tools"), exist_ok=True)
|
||||
shutil.copy(
|
||||
os.path.join(REPO_ROOT, "tools", "validate_content.py"),
|
||||
os.path.join(root, "tools", "validate_content.py"),
|
||||
)
|
||||
write_docs(root, docs or DEFAULT_DOCS)
|
||||
return root
|
||||
|
||||
|
||||
def write_docs(root: str, docs: dict) -> None:
|
||||
for rel, fields in docs.items():
|
||||
path = os.path.join(root, "content", rel)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as handle:
|
||||
handle.write(DOC.format(**fields))
|
||||
|
||||
|
||||
def write_doc(root: str, rel: str, **fields) -> None:
|
||||
path = os.path.join(root, "content", rel)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as handle:
|
||||
handle.write(DOC.format(**fields))
|
||||
# A fresh interpreter's import cache is fine; the loader re-reads the file.
|
||||
|
||||
|
||||
def make_config(root: str, **overrides) -> Config:
|
||||
values = dict(
|
||||
provider="local-hashing",
|
||||
model="local-hashing-v1",
|
||||
dim=32,
|
||||
qdrant_url="fake://qdrant",
|
||||
collection="test_corpus",
|
||||
)
|
||||
values.update(overrides)
|
||||
return Config.from_env(root=root, **values)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def fake_qdrant(cfg, fake: FakeQdrant = None):
|
||||
"""Point every module that builds a Qdrant client at one in-memory fake."""
|
||||
fake = fake or FakeQdrant(cfg.dim)
|
||||
with mock.patch("corpus.pipeline.Qdrant", return_value=fake), mock.patch(
|
||||
"corpus.freshness.Qdrant", return_value=fake
|
||||
):
|
||||
yield fake
|
||||
|
||||
|
||||
class StubServer:
|
||||
"""A throwaway HTTP server on a loopback ephemeral port, for provider tests.
|
||||
|
||||
`responder(method, path, body)` returns `(status, payload)`; every request is
|
||||
recorded with its parsed body so a test can assert the wire shape without a
|
||||
real provider or a real Qdrant.
|
||||
"""
|
||||
|
||||
def __init__(self, responder):
|
||||
import json
|
||||
import threading
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
self.requests = []
|
||||
self._responder = responder
|
||||
outer = self
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def _handle(self):
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
raw = self.rfile.read(length).decode("utf-8") if length else ""
|
||||
body = json.loads(raw) if raw.strip() else None
|
||||
outer.requests.append(
|
||||
{"method": self.command, "path": self.path, "body": body,
|
||||
"headers": dict(self.headers)}
|
||||
)
|
||||
status, payload = outer._responder(self.command, self.path, body)
|
||||
data = payload.encode("utf-8") if isinstance(payload, str) else json.dumps(payload).encode("utf-8")
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(data)))
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
do_GET = do_POST = do_PUT = do_DELETE = _handle
|
||||
|
||||
def log_message(self, *_args):
|
||||
pass
|
||||
|
||||
self.server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
|
||||
self._thread = threading.Thread(target=self.server.serve_forever, daemon=True)
|
||||
|
||||
def __enter__(self):
|
||||
self._thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *_exc):
|
||||
self.server.shutdown()
|
||||
self.server.server_close()
|
||||
145
corpus/tests/test_chunker.py
Normal file
145
corpus/tests/test_chunker.py
Normal file
@@ -0,0 +1,145 @@
|
||||
"""Chunking rules: size, overlap, determinism, and the hard one — no split inside
|
||||
a fence, a table or front-matter (docs/phase-4-corpus.md DoD item 3)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from corpus import chunker, loader
|
||||
from corpus.config import Config
|
||||
from corpus.tests import support
|
||||
|
||||
REPO_ROOT = support.REPO_ROOT
|
||||
|
||||
|
||||
def content_documents():
|
||||
cfg = Config.from_env(root=REPO_ROOT, provider="local-hashing", dim=32)
|
||||
return loader.load_documents(cfg)
|
||||
|
||||
|
||||
class BlockParsingTest(unittest.TestCase):
|
||||
def test_fence_is_one_block(self):
|
||||
blocks = chunker.parse_blocks(support.FENCE)
|
||||
kinds = [block.kind for block in blocks]
|
||||
self.assertEqual(kinds, ["para", "fence", "para"])
|
||||
self.assertIn("chunk boundary must never land", blocks[1].text)
|
||||
|
||||
def test_table_is_one_block(self):
|
||||
blocks = chunker.parse_blocks(support.TABLE)
|
||||
table = [block for block in blocks if block.kind == "table"]
|
||||
self.assertEqual(len(table), 1)
|
||||
self.assertEqual(len(table[0].text.splitlines()), 5) # header, rule, 3 rows
|
||||
|
||||
def test_front_matter_in_body_is_refused(self):
|
||||
with self.assertRaises(chunker.ChunkError):
|
||||
chunker.parse_blocks("---\nid: x\n---\n\n# Heading\n")
|
||||
|
||||
def test_unterminated_fence_runs_to_end(self):
|
||||
blocks = chunker.parse_blocks("intro\n\n```go\nfunc main() {}\n")
|
||||
self.assertEqual(blocks[-1].kind, "fence")
|
||||
self.assertTrue(blocks[-1].text.rstrip().endswith("}"))
|
||||
|
||||
def test_headings_start_blocks(self):
|
||||
blocks = chunker.parse_blocks("# One\n\ntext\n\n## Two\n\nmore\n")
|
||||
self.assertEqual([block.kind for block in blocks], ["heading", "para", "heading", "para"])
|
||||
|
||||
def test_list_items_are_separate_blocks(self):
|
||||
blocks = chunker.parse_blocks("- one\n- two\n continued\n- three\n")
|
||||
self.assertEqual([block.kind for block in blocks], ["list", "list", "list"])
|
||||
self.assertIn("continued", blocks[1].text)
|
||||
|
||||
|
||||
class ChunkingTest(unittest.TestCase):
|
||||
def test_small_body_is_one_chunk(self):
|
||||
chunks = chunker.chunk_document("# Title\n\nA short body.\n")
|
||||
self.assertEqual(len(chunks), 1)
|
||||
self.assertEqual(chunks[0].index, 0)
|
||||
self.assertFalse(chunks[0].oversize)
|
||||
|
||||
def test_long_body_respects_maximum_and_overlaps(self):
|
||||
chunks = chunker.chunk_document(support.PROSE)
|
||||
self.assertGreater(len(chunks), 1)
|
||||
for chunk in chunks:
|
||||
self.assertLessEqual(chunk.tokens, chunker.TOKEN_MAX, f"chunk {chunk.index} too large")
|
||||
# Overlap: consecutive chunks share at least one block.
|
||||
for previous, current in zip(chunks, chunks[1:]):
|
||||
self.assertTrue(
|
||||
set(id(block) for block in previous.blocks) & set(id(block) for block in current.blocks),
|
||||
f"chunks {previous.index} and {current.index} do not overlap",
|
||||
)
|
||||
|
||||
def test_chunk_indices_are_sequential(self):
|
||||
chunks = chunker.chunk_document(support.PROSE)
|
||||
self.assertEqual([chunk.index for chunk in chunks], list(range(len(chunks))))
|
||||
|
||||
def test_deterministic(self):
|
||||
first = chunker.chunk_document(support.PROSE)
|
||||
second = chunker.chunk_document(support.PROSE)
|
||||
self.assertEqual([chunk.text for chunk in first], [chunk.text for chunk in second])
|
||||
|
||||
def test_code_fence_is_never_split(self):
|
||||
body = support.PROSE + "\n\n" + support.FENCE + "\n\n" + support.PROSE
|
||||
chunks = chunker.chunk_document(body)
|
||||
fences = [block for block in chunker.parse_blocks(body) if block.kind == "fence"]
|
||||
self.assertTrue(fences)
|
||||
for fence in fences:
|
||||
holders = [chunk for chunk in chunks if fence in chunk.blocks]
|
||||
self.assertTrue(holders, "the fenced block disappeared from every chunk")
|
||||
for holder in holders:
|
||||
self.assertIn(fence.text, holder.text, "a chunk holds part of a fenced block")
|
||||
|
||||
def test_oversize_single_block_is_flagged_not_split(self):
|
||||
huge = "```go\n" + "\n".join(f"line{i} := {i}" for i in range(1200)) + "\n```\n"
|
||||
chunks = chunker.chunk_document(huge)
|
||||
self.assertEqual(len(chunks), 1)
|
||||
self.assertTrue(chunks[0].oversize)
|
||||
self.assertGreater(chunks[0].tokens, chunker.TOKEN_MAX)
|
||||
|
||||
|
||||
class RealContentTest(unittest.TestCase):
|
||||
"""The same rules, over every document in content/."""
|
||||
|
||||
def test_no_boundary_splits_a_protected_region(self):
|
||||
for doc in content_documents():
|
||||
regions = chunker.protected_regions(doc.body)
|
||||
chunks = chunker.chunk_document(doc.body)
|
||||
chunker.verify_no_split(chunks, regions) # raises on failure
|
||||
|
||||
def test_every_chunk_is_whole_blocks(self):
|
||||
for doc in content_documents():
|
||||
expected = [block.text for block in chunker.parse_blocks(doc.body)]
|
||||
seen = []
|
||||
for chunk in chunker.chunk_document(doc.body):
|
||||
for block in chunk.blocks:
|
||||
self.assertIn(block.text, chunk.text)
|
||||
if not seen or seen[-1] != block.text:
|
||||
seen.append(block.text)
|
||||
# Every block appears, in order, exactly as parsed (overlap repeats a
|
||||
# block at the start of the next chunk, which is expected).
|
||||
position = 0
|
||||
for text in expected:
|
||||
while position < len(seen) and seen[position] != text:
|
||||
position += 1
|
||||
self.assertLess(position, len(seen), f"{doc.path}: block was dropped: {text[:40]!r}")
|
||||
position += 1
|
||||
|
||||
def test_chunks_are_within_the_size_band(self):
|
||||
for doc in content_documents():
|
||||
for chunk in chunker.chunk_document(doc.body):
|
||||
if chunk.oversize:
|
||||
self.assertEqual(len(chunk.blocks), 1)
|
||||
continue
|
||||
self.assertLessEqual(chunk.tokens, chunker.TOKEN_MAX, f"{doc.path} chunk {chunk.index}")
|
||||
|
||||
def test_content_tree_is_chunked_completely(self):
|
||||
total = 0
|
||||
for doc in content_documents():
|
||||
chunks = chunker.chunk_document(doc.body)
|
||||
self.assertTrue(chunks, f"{doc.path}: no chunks")
|
||||
total += len(chunks)
|
||||
self.assertGreater(total, 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
125
corpus/tests/test_embeddings.py
Normal file
125
corpus/tests/test_embeddings.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""Providers: determinism offline, and an OpenAI-compatible wire shape through OmniRoute.
|
||||
|
||||
There is no direct vendor SDK call anywhere (AGENTS.md §4.5), so the provider
|
||||
test asserts the HTTP request shape, not a client library's behaviour.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import unittest
|
||||
|
||||
from corpus import embeddings
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
class LocalHashingTest(unittest.TestCase):
|
||||
def test_deterministic_and_unit_length(self):
|
||||
provider = embeddings.LocalHashingProvider(dim=64)
|
||||
first = provider.embed(["zigbee mesh latency"])[0]
|
||||
second = embeddings.LocalHashingProvider(dim=64).embed(["zigbee mesh latency"])[0]
|
||||
self.assertEqual(first, second)
|
||||
self.assertEqual(len(first), 64)
|
||||
self.assertAlmostEqual(math.sqrt(sum(value * value for value in first)), 1.0, places=9)
|
||||
|
||||
def test_different_text_differs(self):
|
||||
provider = embeddings.LocalHashingProvider(dim=64)
|
||||
a, b = provider.embed(["zigbee", "lora"])
|
||||
self.assertNotEqual(a, b)
|
||||
|
||||
def test_empty_text_still_returns_a_unit_vector(self):
|
||||
provider = embeddings.LocalHashingProvider(dim=16)
|
||||
vector = provider.embed([""])[0]
|
||||
self.assertEqual(len(vector), 16)
|
||||
self.assertAlmostEqual(math.sqrt(sum(value * value for value in vector)), 1.0, places=9)
|
||||
|
||||
def test_stats_count_calls_and_texts(self):
|
||||
provider = embeddings.LocalHashingProvider(dim=16)
|
||||
provider.embed(["a", "b", "c"])
|
||||
provider.embed(["d"])
|
||||
self.assertEqual(provider.stats(), {"calls": 2, "texts": 4})
|
||||
provider.reset_stats()
|
||||
self.assertEqual(provider.stats(), {"calls": 0, "texts": 0})
|
||||
|
||||
def test_rejects_a_zero_dimension(self):
|
||||
with self.assertRaises(embeddings.EmbeddingError):
|
||||
embeddings.LocalHashingProvider(dim=0)
|
||||
|
||||
|
||||
class OmniRouteTest(unittest.TestCase):
|
||||
def responder_factory(self, dim=8, status_plan=None):
|
||||
plan = list(status_plan or [])
|
||||
state = {"n": 0}
|
||||
|
||||
def responder(method, path, body):
|
||||
state["n"] += 1
|
||||
if plan:
|
||||
status = plan.pop(0)
|
||||
if status != 200:
|
||||
return status, {"error": "planned failure"}
|
||||
vectors = [
|
||||
{"index": index, "embedding": [0.1] * dim}
|
||||
for index, _text in enumerate(body.get("input") or [])
|
||||
]
|
||||
return 200, {"model": body.get("model"), "data": vectors}
|
||||
|
||||
return responder
|
||||
|
||||
def test_posts_to_v1_embeddings_with_the_configured_model(self):
|
||||
with support.StubServer(self.responder_factory()) as server:
|
||||
provider = embeddings.OmniRouteProvider(
|
||||
api_url=server.url, api_key="test-key", model="text-embedding-3-small", dim=8
|
||||
)
|
||||
vectors = provider.embed(["one", "two"])
|
||||
self.assertEqual(len(vectors), 2)
|
||||
request = server.requests[0]
|
||||
self.assertEqual(request["method"], "POST")
|
||||
self.assertEqual(request["path"], "/v1/embeddings")
|
||||
self.assertEqual(request["body"]["model"], "text-embedding-3-small")
|
||||
self.assertEqual(request["body"]["input"], ["one", "two"])
|
||||
self.assertEqual(request["headers"]["Authorization"], "Bearer test-key")
|
||||
self.assertEqual(provider.stats(), {"calls": 1, "texts": 2})
|
||||
|
||||
def test_batches_requests(self):
|
||||
with support.StubServer(self.responder_factory()) as server:
|
||||
provider = embeddings.OmniRouteProvider(
|
||||
api_url=server.url, api_key="k", model="m", dim=8, batch_size=2
|
||||
)
|
||||
provider.embed(["a", "b", "c", "d", "e"])
|
||||
self.assertEqual(len(server.requests), 3)
|
||||
self.assertEqual(provider.stats(), {"calls": 3, "texts": 5})
|
||||
|
||||
def test_dimension_mismatch_is_refused(self):
|
||||
with support.StubServer(self.responder_factory(dim=4)) as server:
|
||||
provider = embeddings.OmniRouteProvider(
|
||||
api_url=server.url, api_key="k", model="m", dim=8
|
||||
)
|
||||
with self.assertRaises(embeddings.EmbeddingError) as caught:
|
||||
provider.embed(["one"])
|
||||
self.assertIn("dimension", str(caught.exception))
|
||||
|
||||
def test_retries_then_succeeds(self):
|
||||
with support.StubServer(self.responder_factory(status_plan=[429, 200])) as server:
|
||||
provider = embeddings.OmniRouteProvider(
|
||||
api_url=server.url, api_key="k", model="m", dim=8, backoff=0.01
|
||||
)
|
||||
vectors = provider.embed(["one"])
|
||||
self.assertEqual(len(vectors), 1)
|
||||
self.assertEqual(len(server.requests), 2)
|
||||
|
||||
def test_client_error_is_not_retried(self):
|
||||
with support.StubServer(self.responder_factory(status_plan=[400])) as server:
|
||||
provider = embeddings.OmniRouteProvider(
|
||||
api_url=server.url, api_key="k", model="m", dim=8, backoff=0.01
|
||||
)
|
||||
with self.assertRaises(embeddings.EmbeddingError):
|
||||
provider.embed(["one"])
|
||||
self.assertEqual(len(server.requests), 1)
|
||||
|
||||
def test_missing_key_is_refused_before_any_call(self):
|
||||
with self.assertRaises(embeddings.EmbeddingError):
|
||||
embeddings.OmniRouteProvider(api_url="http://127.0.0.1:1", api_key="", model="m", dim=8)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
110
corpus/tests/test_freshness.py
Normal file
110
corpus/tests/test_freshness.py
Normal file
@@ -0,0 +1,110 @@
|
||||
"""Freshness audit: stale documents, index drift and contract drift."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import unittest
|
||||
|
||||
from corpus import freshness, pipeline, store as store_module
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
class AuditTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = support.temp_repo()
|
||||
self.cfg = support.make_config(self.root)
|
||||
self.addCleanup(_cleanup, self.root)
|
||||
with support.fake_qdrant(self.cfg):
|
||||
pipeline.run(self.cfg, mode="rebuild")
|
||||
|
||||
def audit(self, today=None, cfg=None):
|
||||
with support.fake_qdrant(cfg or self.cfg):
|
||||
return freshness.audit(cfg or self.cfg, today=today)
|
||||
|
||||
def test_clean_soon_after_rebuild(self):
|
||||
report = self.audit(today=datetime.date(2026, 10, 5))
|
||||
self.assertTrue(report["ok"], report["findings"])
|
||||
self.assertEqual(report["corpus"]["documents"], 3)
|
||||
|
||||
def test_reports_stale_documents(self):
|
||||
report = self.audit(today=datetime.date(2027, 10, 5))
|
||||
stale = {row["source_path"] for row in report["findings"]["stale"]}
|
||||
self.assertEqual(
|
||||
stale,
|
||||
{
|
||||
"content/ai/ai-overview.md",
|
||||
"content/edge/l3/edge-zigbee.md",
|
||||
"content/platform/l3/platform-containers.md",
|
||||
},
|
||||
)
|
||||
self.assertFalse(report["ok"])
|
||||
text = freshness.render_text(report)
|
||||
self.assertIn("STALE", text)
|
||||
self.assertIn("platform-containers", text)
|
||||
|
||||
def test_warns_before_a_document_goes_stale(self):
|
||||
# platform-containers was verified 2026-08-01. Horizon 180 days, warning
|
||||
# 30, so on 2027-01-20 it is 172 days old: due soon, not yet stale.
|
||||
report = self.audit(today=datetime.date(2027, 1, 20))
|
||||
due = {row["source_path"] for row in report["findings"]["due_soon"]}
|
||||
self.assertEqual(due, {"content/platform/l3/platform-containers.md"})
|
||||
self.assertEqual(report["findings"]["stale"], [])
|
||||
self.assertTrue(report["ok"])
|
||||
|
||||
def test_reports_index_drift_when_a_source_changes(self):
|
||||
support.write_doc(
|
||||
self.root,
|
||||
"ai/ai-overview.md",
|
||||
id="ai-overview", area="ai", level=1, title="Agent Systems",
|
||||
claim="Edited but not re-indexed.", confidence="running",
|
||||
last_verified="2026-10-05", tags="agents", body="New words for the corpus.",
|
||||
)
|
||||
report = self.audit(today=datetime.date(2026, 10, 6))
|
||||
problems = [row["problem"] for row in report["findings"]["index_drift"]]
|
||||
self.assertIn("source changed since it was embedded", problems)
|
||||
self.assertFalse(report["ok"])
|
||||
|
||||
def test_reports_contract_drift_when_the_model_changes(self):
|
||||
other = support.make_config(self.root, model="different-model")
|
||||
report = self.audit(today=datetime.date(2026, 10, 5), cfg=other)
|
||||
self.assertTrue(report["findings"]["contract_drift"])
|
||||
|
||||
def test_report_is_json_serialisable(self):
|
||||
import json
|
||||
|
||||
report = self.audit(today=datetime.date(2026, 10, 5))
|
||||
json.dumps(report)
|
||||
|
||||
|
||||
class AlertTest(unittest.TestCase):
|
||||
def test_no_url_means_no_alert(self):
|
||||
report = {"ok": True, "findings": {"stale": []}, "corpus": {}, "horizon": {}, "summary": {}}
|
||||
self.assertFalse(freshness.alert(report, ""))
|
||||
|
||||
def test_posts_to_apprise(self):
|
||||
report = {
|
||||
"generated_at": "2026-10-26T00:00:00Z",
|
||||
"today": "2026-10-26",
|
||||
"horizon": {"max_age_days": 180, "warn_days": 30},
|
||||
"corpus": {
|
||||
"documents": 1, "chunks": 1, "sources": 1, "collection": "c",
|
||||
"collection_points": 1, "provider": "p", "model": "m", "dim": 8,
|
||||
"chunker_version": "v",
|
||||
},
|
||||
"findings": {"stale": [], "due_soon": [], "index_drift": [], "contract_drift": []},
|
||||
"summary": {"stale": 0},
|
||||
"ok": True,
|
||||
}
|
||||
with support.StubServer(lambda method, path, body: (200, "ok")) as server:
|
||||
self.assertTrue(freshness.alert(report, server.url))
|
||||
self.assertIn("title", server.requests[0]["body"])
|
||||
|
||||
|
||||
def _cleanup(root):
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(root, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
100
corpus/tests/test_payloads.py
Normal file
100
corpus/tests/test_payloads.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""Payload rules: every field present, enums enforced, missing field fails the run."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from corpus import chunker, loader, payloads
|
||||
from corpus.config import Config
|
||||
from corpus.pipeline import chunk_id
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
def first_payload():
|
||||
cfg = Config.from_env(root=support.REPO_ROOT, provider="local-hashing", dim=32)
|
||||
doc = loader.load_documents(cfg)[0]
|
||||
chunk = chunker.chunk_document(doc.body)[0]
|
||||
payload = payloads.build_payload(doc, chunk, cfg)
|
||||
payload["id"] = chunk_id(doc.path, chunk.index, doc.digest)
|
||||
return cfg, doc, payload
|
||||
|
||||
|
||||
class BuildTest(unittest.TestCase):
|
||||
def test_payload_has_every_required_field(self):
|
||||
cfg, doc, payload = first_payload()
|
||||
payloads.validate_payload(payload)
|
||||
for field in payloads.REQUIRED_FIELDS:
|
||||
self.assertIn(field, payload)
|
||||
self.assertEqual(payload["area"], doc.area)
|
||||
self.assertEqual(payload["confidence"], doc.confidence)
|
||||
self.assertEqual(payload["last_verified"], doc.last_verified)
|
||||
self.assertEqual(payload["embedding_model"], cfg.model)
|
||||
self.assertEqual(payload["embedding_dim"], cfg.dim)
|
||||
|
||||
def test_every_real_document_produces_valid_payloads(self):
|
||||
cfg = Config.from_env(root=support.REPO_ROOT, provider="local-hashing", dim=32)
|
||||
for doc in loader.load_documents(cfg):
|
||||
for chunk in chunker.chunk_document(doc.body):
|
||||
payload = payloads.build_payload(doc, chunk, cfg)
|
||||
payload["id"] = chunk_id(doc.path, chunk.index, doc.digest)
|
||||
payloads.validate_payload(payload, where=doc.path)
|
||||
|
||||
def test_source_type_is_doc_by_default(self):
|
||||
_cfg, doc, payload = first_payload()
|
||||
self.assertEqual(payload["source_type"], "doc")
|
||||
|
||||
def test_source_type_from_tags(self):
|
||||
doc = loader.Document(
|
||||
path="content/ai/adr-x.md", source_id="adr-x", area="ai", level=3, title="ADR",
|
||||
claim="c", confidence="delivered", period="2024", tech=[], metrics=[], artefacts=[],
|
||||
public=True, last_verified="2026-01-01", tags=["adr"], body="", digest="0" * 64,
|
||||
url="/ai/a/adr-x/",
|
||||
)
|
||||
self.assertEqual(payloads.source_type_for(doc), "adr")
|
||||
|
||||
|
||||
class ValidationTest(unittest.TestCase):
|
||||
def test_missing_field_raises(self):
|
||||
_cfg, _doc, payload = first_payload()
|
||||
for field in ("area", "level", "source_type", "confidence", "period", "artefacts", "last_verified", "text"):
|
||||
broken = dict(payload)
|
||||
del broken[field]
|
||||
with self.assertRaises(payloads.PayloadError, msg=f"missing {field} passed validation"):
|
||||
payloads.validate_payload(broken)
|
||||
|
||||
def test_missing_provenance_field_raises(self):
|
||||
_cfg, _doc, payload = first_payload()
|
||||
for field in payloads.PROVENANCE_FIELDS:
|
||||
broken = dict(payload)
|
||||
del broken[field]
|
||||
with self.assertRaises(payloads.PayloadError, msg=f"missing {field} passed validation"):
|
||||
payloads.validate_payload(broken)
|
||||
|
||||
def test_bad_enums_raise(self):
|
||||
_cfg, _doc, payload = first_payload()
|
||||
for field, value in (
|
||||
("area", "sales"),
|
||||
("level", 4),
|
||||
("source_type", "blog"),
|
||||
("confidence", "vibes"),
|
||||
):
|
||||
broken = dict(payload)
|
||||
broken[field] = value
|
||||
with self.assertRaises(payloads.PayloadError, msg=f"{field}={value!r} passed validation"):
|
||||
payloads.validate_payload(broken)
|
||||
|
||||
def test_empty_text_raises(self):
|
||||
_cfg, _doc, payload = first_payload()
|
||||
payload["text"] = " "
|
||||
with self.assertRaises(payloads.PayloadError):
|
||||
payloads.validate_payload(payload)
|
||||
|
||||
def test_bad_date_raises(self):
|
||||
_cfg, _doc, payload = first_payload()
|
||||
payload["last_verified"] = "last october"
|
||||
with self.assertRaises(payloads.PayloadError):
|
||||
payloads.validate_payload(payload)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
179
corpus/tests/test_pipeline.py
Normal file
179
corpus/tests/test_pipeline.py
Normal file
@@ -0,0 +1,179 @@
|
||||
"""The pipeline: deterministic ids, no duplicate vectors on re-run, incremental mode,
|
||||
incomplete payloads never reach the store (docs/phase-4-corpus.md DoD 1-6)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from corpus import chunker, loader, pipeline, store as store_module
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
class RebuildTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = support.temp_repo()
|
||||
self.cfg = support.make_config(self.root)
|
||||
self.addCleanup(_cleanup, self.root)
|
||||
# One fake for the whole test: a second run must see the first run's points.
|
||||
self.fake = support.FakeQdrant(self.cfg.dim)
|
||||
|
||||
def run_pipeline(self, mode="sync", cfg=None):
|
||||
cfg = cfg or self.cfg
|
||||
with support.fake_qdrant(cfg, self.fake):
|
||||
return pipeline.run(cfg, mode=mode)
|
||||
|
||||
def test_rebuild_from_empty_volume(self):
|
||||
stats = self.run_pipeline("rebuild")
|
||||
self.assertEqual(stats.documents, 3)
|
||||
self.assertEqual(stats.added, 3)
|
||||
self.assertGreater(stats.store_chunks, 0)
|
||||
self.assertEqual(stats.store_chunks, stats.collection_points)
|
||||
self.assertEqual(stats.collection_points, len(self.fake.collections[self.cfg.collection]))
|
||||
self.assertGreater(stats.embed_calls, 0)
|
||||
|
||||
def test_running_twice_does_not_duplicate_vectors(self):
|
||||
first = self.run_pipeline("rebuild")
|
||||
points_after_first = len(self.fake.collections[self.cfg.collection])
|
||||
second = self.run_pipeline("sync")
|
||||
points_after_second = len(self.fake.collections[self.cfg.collection])
|
||||
self.assertEqual(points_after_first, points_after_second)
|
||||
self.assertEqual(first.store_chunks, second.store_chunks)
|
||||
self.assertEqual(second.added, 0)
|
||||
self.assertEqual(second.updated, 0)
|
||||
self.assertEqual(second.chunks_written, 0)
|
||||
|
||||
def test_incremental_reembeds_only_changed_documents(self):
|
||||
self.run_pipeline("rebuild")
|
||||
|
||||
# Nothing changed: no embedding calls at all.
|
||||
unchanged = self.run_pipeline("sync")
|
||||
self.assertEqual(unchanged.embed_calls, 0)
|
||||
self.assertEqual(unchanged.texts_embedded, 0)
|
||||
|
||||
# One document changes: exactly one embedding call, for that document.
|
||||
support.write_doc(
|
||||
self.root,
|
||||
"edge/l3/edge-zigbee.md",
|
||||
id="edge-zigbee", area="edge", level=3, title="Low Power Mesh Sensing",
|
||||
claim="A constrained mesh that survives power loss.", confidence="delivered",
|
||||
last_verified="2026-09-15", tags="zigbee, mesh",
|
||||
body="The mesh now also reports battery voltage on every node.",
|
||||
)
|
||||
changed = self.run_pipeline("sync")
|
||||
self.assertEqual(changed.added + changed.updated, 1)
|
||||
self.assertEqual(changed.embed_calls, 1)
|
||||
expected_chunks = len(
|
||||
chunker.chunk_document(
|
||||
loader.load_documents(support.make_config(self.root))[1].body
|
||||
)
|
||||
)
|
||||
self.assertEqual(changed.texts_embedded, expected_chunks)
|
||||
self.assertEqual(changed.unchanged, 2)
|
||||
|
||||
def test_removed_document_is_removed_from_both_indexes(self):
|
||||
self.run_pipeline("rebuild")
|
||||
before = self.run_pipeline("sync").store_chunks
|
||||
os.remove(os.path.join(self.root, "content", "platform", "l3", "platform-containers.md"))
|
||||
after = self.run_pipeline("sync")
|
||||
self.assertEqual(after.removed, 1)
|
||||
self.assertLess(after.store_chunks, before)
|
||||
self.assertEqual(after.store_chunks, after.collection_points)
|
||||
with store_module.Store(self.cfg.db_path) as store:
|
||||
self.assertEqual(store.keyword_search("endpoint latency loss"), [])
|
||||
|
||||
def test_ids_are_deterministic(self):
|
||||
first = pipeline.chunk_id("content/ai/ai-overview.md", 0, "a" * 64)
|
||||
second = pipeline.chunk_id("content/ai/ai-overview.md", 0, "a" * 64)
|
||||
other = pipeline.chunk_id("content/ai/ai-overview.md", 1, "a" * 64)
|
||||
self.assertEqual(first, second)
|
||||
self.assertNotEqual(first, other)
|
||||
self.assertEqual(len(first), 36)
|
||||
|
||||
def test_invalid_payload_aborts_before_any_write(self):
|
||||
real_build = pipeline.payloads.build_payload
|
||||
|
||||
def broken(doc, chunk, cfg):
|
||||
payload = real_build(doc, chunk, cfg)
|
||||
payload.pop("confidence")
|
||||
return payload
|
||||
|
||||
with mock.patch("corpus.payloads.build_payload", side_effect=broken):
|
||||
with self.assertRaises(pipeline.PipelineError):
|
||||
self.run_pipeline("rebuild")
|
||||
self.assertEqual(self.fake.collections.get(self.cfg.collection, {}), {})
|
||||
|
||||
def test_provider_change_is_refused_without_rebuild(self):
|
||||
self.run_pipeline("rebuild")
|
||||
other = support.make_config(self.root, model="local-hashing-v2")
|
||||
with self.assertRaises(pipeline.PipelineError) as caught:
|
||||
self.run_pipeline("sync", cfg=other)
|
||||
self.assertIn("governed migration", str(caught.exception))
|
||||
|
||||
def test_contract_is_recorded(self):
|
||||
self.run_pipeline("rebuild")
|
||||
with store_module.Store(self.cfg.db_path) as store:
|
||||
contract = store.get_meta("collection")
|
||||
self.assertEqual(contract["model"], self.cfg.model)
|
||||
self.assertEqual(contract["dim"], self.cfg.dim)
|
||||
self.assertEqual(contract["chunker_version"], self.cfg.chunker_version)
|
||||
self.assertTrue(os.path.exists(self.cfg.meta_path))
|
||||
|
||||
|
||||
class VerifyTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = support.temp_repo()
|
||||
self.cfg = support.make_config(self.root)
|
||||
self.addCleanup(_cleanup, self.root)
|
||||
|
||||
def test_clean_after_rebuild(self):
|
||||
with support.fake_qdrant(self.cfg):
|
||||
pipeline.run(self.cfg, mode="rebuild")
|
||||
self.assertEqual(pipeline.verify(self.cfg), [])
|
||||
|
||||
def test_detects_source_changed_without_sync(self):
|
||||
with support.fake_qdrant(self.cfg):
|
||||
pipeline.run(self.cfg, mode="rebuild")
|
||||
support.write_doc(
|
||||
self.root,
|
||||
"ai/ai-overview.md",
|
||||
id="ai-overview", area="ai", level=1, title="Agent Systems",
|
||||
claim="Changed.", confidence="running", last_verified="2026-10-02",
|
||||
tags="agents", body="A different body entirely.",
|
||||
)
|
||||
problems = pipeline.verify(self.cfg)
|
||||
self.assertTrue(any("changed since it was embedded" in problem for problem in problems), problems)
|
||||
|
||||
def test_detects_missing_document(self):
|
||||
with support.fake_qdrant(self.cfg):
|
||||
pipeline.run(self.cfg, mode="rebuild")
|
||||
os.remove(os.path.join(self.root, "content", "ai", "ai-overview.md"))
|
||||
problems = pipeline.verify(self.cfg)
|
||||
self.assertTrue(any("no longer in content/" in problem for problem in problems), problems)
|
||||
|
||||
|
||||
class AreaFilterTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = support.temp_repo()
|
||||
self.cfg = support.make_config(self.root)
|
||||
self.addCleanup(_cleanup, self.root)
|
||||
|
||||
def test_filter_narrows_the_result_set(self):
|
||||
with support.fake_qdrant(self.cfg):
|
||||
pipeline.run(self.cfg, mode="rebuild")
|
||||
result = pipeline.demo_area_filter(self.cfg, "container estate limits", area="edge")
|
||||
self.assertTrue(result["unfiltered"])
|
||||
self.assertTrue(result["filtered"])
|
||||
self.assertEqual({row["area"] for row in result["filtered"]}, {"edge"})
|
||||
self.assertGreater(len({row["area"] for row in result["unfiltered"]}), 1)
|
||||
|
||||
|
||||
def _cleanup(root):
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(root, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
104
corpus/tests/test_qdrant_client.py
Normal file
104
corpus/tests/test_qdrant_client.py
Normal file
@@ -0,0 +1,104 @@
|
||||
"""Qdrant client: the wire shape the pipeline depends on, and the fallback route."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from corpus import qdrant as qdrant_module
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
class ClientTest(unittest.TestCase):
|
||||
def test_create_collection_sends_size_and_distance(self):
|
||||
with support.StubServer(lambda method, path, body: (200, {"result": True})) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
client.create_collection("resume_corpus", 1536)
|
||||
request = server.requests[0]
|
||||
self.assertEqual(request["method"], "PUT")
|
||||
self.assertEqual(request["path"], "/collections/resume_corpus")
|
||||
body = request["body"]
|
||||
self.assertEqual(body["vectors"]["size"], 1536)
|
||||
self.assertEqual(body["vectors"]["distance"], "Cosine")
|
||||
# A memory-bound host: payloads and the HNSW graph live on disk.
|
||||
self.assertTrue(body["on_disk_payload"])
|
||||
self.assertTrue(body["hnsw_config"]["on_disk"])
|
||||
self.assertFalse(body["vectors"]["on_disk"])
|
||||
self.assertGreater(body["optimizers_config"]["memmap_threshold"], 0)
|
||||
|
||||
def test_upsert_waits_and_batches(self):
|
||||
with support.StubServer(lambda method, path, body: (200, {"result": {}})) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
points = [{"id": f"id-{index}", "vector": [0.0], "payload": {}} for index in range(5)]
|
||||
sent = client.upsert("c", points, batch=2)
|
||||
self.assertEqual(sent, 5)
|
||||
self.assertEqual(len(server.requests), 3)
|
||||
self.assertTrue(all("wait=true" in request["path"] for request in server.requests))
|
||||
self.assertEqual(sum(len(r["body"]["points"]) for r in server.requests), 5)
|
||||
|
||||
def test_delete_by_source_sends_a_source_filter(self):
|
||||
with support.StubServer(lambda method, path, body: (200, {"result": {}})) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
client.delete_by_source("c", "content/edge/l3/edge-zigbee.md")
|
||||
request = server.requests[0]
|
||||
self.assertEqual(request["method"], "POST")
|
||||
self.assertIn("/collections/c/points/delete", request["path"])
|
||||
self.assertEqual(
|
||||
request["body"],
|
||||
{"filter": {"must": [{"key": "source_path", "match": {"value": "content/edge/l3/edge-zigbee.md"}}]}},
|
||||
)
|
||||
|
||||
def test_search_filters_before_searching(self):
|
||||
responder = lambda method, path, body: (200, {"result": {"points": []}}) # noqa: E731
|
||||
with support.StubServer(responder) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
client.search("c", [0.1, 0.2], limit=3, area="edge", confidence="delivered")
|
||||
body = server.requests[0]["body"]
|
||||
self.assertEqual(server.requests[0]["path"], "/collections/c/points/query")
|
||||
self.assertEqual(body["limit"], 3)
|
||||
self.assertEqual(
|
||||
body["filter"],
|
||||
{
|
||||
"must": [
|
||||
{"key": "area", "match": {"value": "edge"}},
|
||||
{"key": "confidence", "match": {"value": "delivered"}},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
def test_search_falls_back_to_the_legacy_route(self):
|
||||
def responder(method, path, body):
|
||||
if path.endswith("/points/query"):
|
||||
return 404, {"status": "not found"}
|
||||
return 200, {"result": [{"id": "p1", "score": 0.9, "payload": {"area": "ai"}}]}
|
||||
|
||||
with support.StubServer(responder) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
hits = client.search("c", [0.1])
|
||||
self.assertEqual([hit["id"] for hit in hits], ["p1"])
|
||||
self.assertEqual([r["path"] for r in server.requests][-1], "/collections/c/points/search")
|
||||
|
||||
def test_point_count_is_exact(self):
|
||||
responder = lambda method, path, body: (200, {"result": {"count": 42}}) # noqa: E731
|
||||
with support.StubServer(responder) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=1)
|
||||
self.assertEqual(client.point_count("c"), 42)
|
||||
self.assertEqual(server.requests[0]["body"], {"exact": True})
|
||||
|
||||
def test_health_is_false_when_unreachable(self):
|
||||
client = qdrant_module.Qdrant("http://127.0.0.1:1", timeout=0.2, attempts=1)
|
||||
self.assertFalse(client.health())
|
||||
|
||||
def test_upsert_retries_a_server_error(self):
|
||||
plan = [500, 200]
|
||||
|
||||
def responder(method, path, body):
|
||||
return plan.pop(0), {"result": {}}
|
||||
|
||||
with support.StubServer(responder) as server:
|
||||
client = qdrant_module.Qdrant(server.url, attempts=2)
|
||||
client.upsert("c", [{"id": "x", "vector": [0.0], "payload": {}}])
|
||||
self.assertEqual(len(server.requests), 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
106
corpus/tests/test_store.py
Normal file
106
corpus/tests/test_store.py
Normal file
@@ -0,0 +1,106 @@
|
||||
"""FTS5 chunk index, manifest and meta: the keyword half of hybrid retrieval."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from corpus import chunker, loader, payloads, store as store_module
|
||||
from corpus.config import Config
|
||||
from corpus.pipeline import chunk_id
|
||||
from corpus.tests import support
|
||||
|
||||
|
||||
def build_store(path, cfg, documents):
|
||||
store = store_module.Store(path)
|
||||
for doc in documents:
|
||||
payload_list = []
|
||||
for chunk in chunker.chunk_document(doc.body):
|
||||
payload = payloads.build_payload(doc, chunk, cfg)
|
||||
payload["id"] = chunk_id(doc.path, chunk.index, doc.digest)
|
||||
payload_list.append(payload)
|
||||
store.write_document(doc, payload_list)
|
||||
return store
|
||||
|
||||
|
||||
class StoreTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = support.temp_repo()
|
||||
self.cfg = support.make_config(self.root)
|
||||
self.addCleanup(_cleanup, self.root)
|
||||
self.documents = loader.load_documents(self.cfg)
|
||||
self.store = build_store(self.cfg.db_path, self.cfg, self.documents)
|
||||
self.addCleanup(self.store.close)
|
||||
|
||||
def test_counts_match_the_documents(self):
|
||||
self.assertEqual(self.store.source_count(), len(self.documents))
|
||||
expected = sum(len(chunker.chunk_document(doc.body)) for doc in self.documents)
|
||||
self.assertEqual(self.store.chunk_count(), expected)
|
||||
|
||||
def test_keyword_search_finds_exact_tokens(self):
|
||||
hits = self.store.keyword_search("zigbee")
|
||||
self.assertTrue(hits, "no hit for a token that is literally in the document")
|
||||
self.assertEqual(hits[0]["area"], "edge")
|
||||
|
||||
def test_keyword_search_respects_area_filter(self):
|
||||
all_hits = self.store.keyword_search("latency")
|
||||
edge_hits = self.store.keyword_search("latency", area="edge")
|
||||
self.assertTrue(all_hits)
|
||||
for hit in edge_hits:
|
||||
self.assertEqual(hit["area"], "edge")
|
||||
|
||||
def test_keyword_search_returns_snippets_and_scores(self):
|
||||
hits = self.store.keyword_search("fence")
|
||||
self.assertTrue(hits)
|
||||
self.assertIn("score", hits[0])
|
||||
self.assertIn("snippet", hits[0])
|
||||
|
||||
def test_operator_injection_is_not_honoured(self):
|
||||
# FTS5 operators must be treated as words, never as query syntax.
|
||||
hits = self.store.keyword_search('edge OR "NEAR"(a b)')
|
||||
self.assertIsInstance(hits, list)
|
||||
|
||||
def test_manifest_records_the_content_hash(self):
|
||||
manifest = self.store.manifest()
|
||||
self.assertEqual(set(manifest), {doc.path for doc in self.documents})
|
||||
for doc in self.documents:
|
||||
self.assertEqual(manifest[doc.path]["content_hash"], doc.digest)
|
||||
self.assertGreater(manifest[doc.path]["chunk_count"], 0)
|
||||
|
||||
def test_delete_removes_rows_from_both_tables(self):
|
||||
target = self.documents[1]
|
||||
self.store.delete_document(target.path)
|
||||
self.assertNotIn(target.path, self.store.manifest())
|
||||
for row in self.store.chunk_payloads():
|
||||
self.assertNotEqual(row["source_path"], target.path)
|
||||
|
||||
def test_meta_roundtrip(self):
|
||||
self.store.set_meta("collection", {"model": "x", "dim": 8})
|
||||
self.assertEqual(self.store.get_meta("collection"), {"model": "x", "dim": 8})
|
||||
self.assertIn("collection", self.store.meta())
|
||||
|
||||
def test_payloads_survive_the_roundtrip(self):
|
||||
rows = self.store.chunk_payloads()
|
||||
self.assertTrue(rows)
|
||||
for row in rows:
|
||||
payloads.validate_payload(row)
|
||||
|
||||
|
||||
class MatchExpressionTest(unittest.TestCase):
|
||||
def test_terms_become_prefix_queries(self):
|
||||
self.assertEqual(store_module.match_expression("Zigbee mesh"), '"zigbee"* AND "mesh"*')
|
||||
|
||||
def test_empty_query_is_empty(self):
|
||||
self.assertEqual(store_module.match_expression(" "), "")
|
||||
|
||||
def test_punctuation_is_dropped(self):
|
||||
self.assertEqual(store_module.match_expression("-edge- LoRaWAN, 2023"), '"edge"* AND "lorawan"* AND "2023"*')
|
||||
|
||||
|
||||
def _cleanup(root):
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(root, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,28 +1,29 @@
|
||||
# resume — runtime image (Phase 3 §3.2).
|
||||
# Runtime image for the Mode A site (docs/phase-2-static-site.md §2.1).
|
||||
#
|
||||
# Built by deploy/deploy.sh from a release directory, with the prebuilt static
|
||||
# Go binary already in place. No Go toolchain, no network and no package install
|
||||
# at build time: the image is `alpine` + the release payload, nothing else.
|
||||
# The image contains no toolchain: `make build dist` on the build host produces
|
||||
# bin/resume-server and dist/, and the image just carries them. That keeps the
|
||||
# runtime offline and small, and it means the deployed bytes are the bytes that
|
||||
# were built and tested, not a second compile.
|
||||
#
|
||||
# The binary must be statically linked (deploy.sh's fallback build sets
|
||||
# CGO_ENABLED=0). If the app ever needs cgo, change the base image to a glibc
|
||||
# one (e.g. golang:1.25-bookworm) in git — do not patch it on the host.
|
||||
# Build context is the repository root:
|
||||
#
|
||||
# make build dist
|
||||
# docker build -f deploy/Dockerfile -t ai-resume:local .
|
||||
#
|
||||
# Phase 3 (§3.1) owns the release directory, the compose invocation and the
|
||||
# rollback; this file only describes the process that runs.
|
||||
|
||||
FROM alpine:3.21
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
|
||||
ARG APP_VERSION=dev
|
||||
LABEL org.opencontainers.image.title="resume" \
|
||||
org.opencontainers.image.version="${APP_VERSION}" \
|
||||
org.opencontainers.image.description="Sam Rolfe — portfolio site (Mode A static)"
|
||||
COPY bin/resume-server /usr/bin/resume-server
|
||||
COPY dist/ /srv/site
|
||||
|
||||
WORKDIR /srv
|
||||
|
||||
# bin/server plus whatever the build emitted (dist/ for rendered pages/assets).
|
||||
COPY . /srv/
|
||||
|
||||
ENV PORT=8096 \
|
||||
APP_VERSION=${APP_VERSION}
|
||||
# 0.0.0.0 is inside the container's own network namespace only. The host
|
||||
# publishes the port on 127.0.0.1 (see deploy/compose.yml), which is what keeps
|
||||
# Caddy the only public listener.
|
||||
ENV RESUME_ADDR=0.0.0.0:8096 \
|
||||
RESUME_DIST=/srv/site
|
||||
|
||||
EXPOSE 8096
|
||||
|
||||
ENTRYPOINT ["/srv/bin/server"]
|
||||
USER nonroot:nonroot
|
||||
ENTRYPOINT ["/usr/bin/resume-server"]
|
||||
|
||||
111
deploy/README.md
111
deploy/README.md
@@ -1,111 +0,0 @@
|
||||
# Deployment (Phase 3)
|
||||
|
||||
One script, one release directory, one atomic symlink. Deploy is a script, not a
|
||||
model decision (`AGENTS.md` §4.6), and nobody hand-edits files on the runtime
|
||||
host: change these files in git and deploy again.
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `deploy.sh` | build + test → release → flip → `up -d` → health gate → rollback on failure |
|
||||
| `deploy.sh --rollback` | restore the previous release in one command |
|
||||
| `docker-compose.yml` | runtime container: published on `127.0.0.1:8096` **and** `192.168.20.13:8096` (never `0.0.0.0`), `mem_limit == memswap_limit == 120m`, capped logs |
|
||||
| `Dockerfile` | runtime image only — alpine + the prebuilt release payload, no toolchain, no network |
|
||||
| `rehearse.sh` | repeatable proof that deploy, fail-fast, health-gate rollback and `--rollback` all work |
|
||||
|
||||
## Layout on `.13`
|
||||
|
||||
```
|
||||
/home/sam/deployment/resume/
|
||||
├── releases/<UTC timestamp>/ immutable: bin/server, dist/, compose, Dockerfile, manifest.txt, .env
|
||||
├── current -> releases/<ts> live release; swapped with ln + mv -T (atomic rename)
|
||||
├── previous -> releases/<ts> the release that was live before the last flip
|
||||
└── deploy.log append-only record of every attempt, including failures
|
||||
```
|
||||
|
||||
## What `deploy.sh` expects from the app
|
||||
|
||||
1. `make build` if a `Makefile` with a `build` target exists, otherwise
|
||||
`go build ./cmd/server`. Either way the result must be one server binary at
|
||||
`bin/server` or `dist/server`; anything else the binary serves from disk goes
|
||||
in `dist/` and is copied into the release.
|
||||
2. `make test` (or `go test ./...`) — runs **before** anything is copied or
|
||||
flipped, so a broken build never reaches the release directory.
|
||||
3. `GET /healthz` → `200`. If the body is JSON with a `static` field it must be
|
||||
`true`. AI readiness is deliberately **not** part of the gate: Mode A must
|
||||
work with every AI service stopped (acceptance test A1).
|
||||
4. Bind `$PORT` (default `8096`) on `0.0.0.0` **inside the container**. The
|
||||
container's own loopback is not reachable through a published port; exposure
|
||||
is controlled by the host side of `ports:` in `docker-compose.yml`.
|
||||
|
||||
## Deploying
|
||||
|
||||
```bash
|
||||
cd /home/sam/paseo/projects/resume_ai_system_paperclip_build
|
||||
./deploy/deploy.sh # build, test, release, flip, health gate
|
||||
./deploy/deploy.sh --rollback # one command back to the previous release
|
||||
tail -40 /home/sam/deployment/resume/deploy.log
|
||||
```
|
||||
|
||||
Exit code is the answer: `0` means the new release answered `/healthz`.
|
||||
Non-zero means either (a) build/test failed and **nothing changed**, or (b) the
|
||||
health gate failed, `current` was flipped back to `previous`, the previous
|
||||
release was restarted, and the log says which.
|
||||
|
||||
Flags worth knowing: `--source DIR` (build from another checkout — this is how
|
||||
`rehearse.sh` drives the script), `--root DIR`, `--port N`,
|
||||
`--health-timeout N`.
|
||||
|
||||
The Go toolchain is auto-detected, including `/nix/store/*-go-1.*/bin/go`, so
|
||||
the script works on a host where `go` is not on `PATH`.
|
||||
|
||||
## Rehearsing the rollback
|
||||
|
||||
```bash
|
||||
./deploy/rehearse.sh # 21 assertions, exit 0 when every one holds
|
||||
```
|
||||
|
||||
It builds three throwaway services in a scratch directory, runs the real script
|
||||
against the real release root and the real port, and asserts:
|
||||
|
||||
1. a good release deploys, answers `/healthz` over loopback and the LAN
|
||||
address, publishes on exactly those two addresses with no wildcard bind,
|
||||
and has `memswap_limit == mem_limit`;
|
||||
2. a build failure exits non-zero, leaves `current` untouched and keeps serving;
|
||||
3. a release whose `/healthz` is `500` is rolled back by the health gate, exits
|
||||
non-zero, and the previous release serves again;
|
||||
4. `deploy.sh --rollback` flips back to the previous release and exits 0;
|
||||
5. `docker compose down && up` (acceptance test N3) restores the service.
|
||||
|
||||
It removes the release root, container and images it created. `KEEP_REHEARSAL=1`
|
||||
keeps them for inspection.
|
||||
|
||||
## Container limits — not negotiable
|
||||
|
||||
`mem_limit` and `memswap_limit` are both `120m`, so the container cannot eat
|
||||
host swap on a host that is already swapping. Measured RSS for a Go static
|
||||
server is single-digit MiB; `deploy.sh` prints the real figure after every
|
||||
deploy so drift shows up in the log.
|
||||
|
||||
## Bind addresses — decided 2026-10-26
|
||||
|
||||
The container publishes on two explicit addresses and nothing else:
|
||||
|
||||
| Address | Why |
|
||||
|---|---|
|
||||
| `127.0.0.1:8096` | `deploy.sh`'s health gate and any check made on `.13` itself |
|
||||
| `192.168.20.13:8096` | the LAN address Caddy on `.35` proxies to (`docs/phase-3-deploy.md` §3.3) |
|
||||
|
||||
A wildcard bind (`0.0.0.0`) is still forbidden: Caddy on `.35` stays the only
|
||||
public listener. The phase document asked for loopback only, but a loopback-only
|
||||
listener is unreachable from `.35`; Sam resolved the contradiction on AUD-133
|
||||
with "A - dual bind: 127.0.0.1:8096 and 192.168.20.13:8096". `APP_LAN_IP`
|
||||
overrides the LAN address (default `192.168.20.13`); `deploy.sh --lan-ip ADDR`
|
||||
sets it for one run. Both `deploy.sh` and `rehearse.sh` assert that the LAN
|
||||
address answers, not just loopback.
|
||||
|
||||
## Caddy
|
||||
|
||||
`docs/phase-3-deploy.md` §3.3 holds the block for `.35`. It is additive, applied
|
||||
with `caddy reload` (never a restart), and **Sam applies it**. The block proxies
|
||||
to `192.168.20.13:8096`, which is why the container publishes on that address as
|
||||
well as loopback (see *Bind addresses* above).
|
||||
71
deploy/compose.yml
Normal file
71
deploy/compose.yml
Normal file
@@ -0,0 +1,71 @@
|
||||
# Container shape for the Mode A site (docs/phase-2-static-site.md §2.1).
|
||||
#
|
||||
# Values are fixed by the specification and must not drift:
|
||||
# mem_limit == memswap_limit == 120m so the app cannot eat the host's swap
|
||||
# ports published on 127.0.0.1 only Caddy on .35 is the only public listener
|
||||
# logging capped one bad day cannot fill the disk
|
||||
#
|
||||
# Run it (build the artefacts first — Phase 3's deploy.sh does both):
|
||||
#
|
||||
# make build dist
|
||||
# docker compose -f deploy/compose.yml up -d
|
||||
#
|
||||
# `curl http://127.0.0.1:8096/healthz` must answer with static ready.
|
||||
|
||||
# The compose project name is deliberately not `ai-resume`: /home/sam/deployment/
|
||||
# ai-resume on .13 already runs a compose project by that name (backend + db on
|
||||
# the ai-mesh network). Sharing the name would make this site's containers show
|
||||
# up as orphans in that project and vice versa, so anything running `compose
|
||||
# down --remove-orphans` under either project could stop the other. The image
|
||||
# tag stays ai-resume:local (Phase 3 fixes it); only the project namespace is
|
||||
# distinct.
|
||||
name: ai-resume-site
|
||||
|
||||
services:
|
||||
resume:
|
||||
image: ai-resume:local
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: deploy/Dockerfile
|
||||
restart: unless-stopped
|
||||
mem_limit: 120m
|
||||
memswap_limit: 120m
|
||||
# Published on this host's LAN address, not loopback. Caddy lives on .35 and
|
||||
# proxies to 192.168.20.13:8096, exactly as it does for offsite (:8095),
|
||||
# photo-filter (:8092), garage-ui (:3909) and prefect (:4200). The port is
|
||||
# reachable inside the house only; Caddy on .35 remains the sole public
|
||||
# listener, which is the intent of AGENTS.md section 4.
|
||||
ports:
|
||||
- "192.168.20.13:8096:8096"
|
||||
read_only: true
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: 10m
|
||||
max-file: "3"
|
||||
healthcheck:
|
||||
test: ["CMD", "/usr/bin/resume-server", "--health-check"]
|
||||
interval: 30s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
|
||||
# The project network gets an explicit, small subnet. The host's docker daemon
|
||||
# has every default address pool allocated by other projects ("all predefined
|
||||
# address pools have been fully subnetted"), so an implicit compose network
|
||||
# cannot be created. Pinning /28 of RFC1918 space that no other bridge or route
|
||||
# on .13 uses keeps this deploy independent of the host's pool exhaustion and
|
||||
# does not touch any other container. Single-service stack: two addresses is
|
||||
# plenty.
|
||||
networks:
|
||||
default:
|
||||
ipam:
|
||||
config:
|
||||
- subnet: 10.89.0.0/28
|
||||
gateway: 10.89.0.1
|
||||
|
||||
# Phase 4/5 add at most three more containers (Qdrant, Prefect worker, query
|
||||
# service) inside the 4-container and 1.0 GiB budget in AGENTS.md §4.3. Mode A
|
||||
# needs none of them and must keep working when they are stopped (test A1).
|
||||
635
deploy/deploy.sh
635
deploy/deploy.sh
@@ -1,321 +1,400 @@
|
||||
#!/usr/bin/env bash
|
||||
# resume — release script (Phase 3).
|
||||
# deploy.sh — release, deploy and roll back the Mode A site (docs/phase-3-deploy.md §3.1).
|
||||
#
|
||||
# Deploy is a script, not a model decision (AGENTS.md §4.6).
|
||||
# No one hand-edits files on the production host: change this script in git.
|
||||
# ./deploy/deploy.sh build, test, stage a release, flip `current`,
|
||||
# start the container, gate on /healthz
|
||||
# ./deploy/deploy.sh --rollback put the previous release back, in one command
|
||||
# ./deploy/deploy.sh --status what is live, what is previous, is it healthy
|
||||
# ./deploy/deploy.sh --release-dir D deploy an already-built release directory
|
||||
# (copied into releases/ first; used by the
|
||||
# rollback rehearsal below)
|
||||
#
|
||||
# ./deploy/deploy.sh build, test, release, flip, health-gate
|
||||
# ./deploy/deploy.sh --rollback restore the previous release, one command
|
||||
#
|
||||
# Layout on the runtime host (.13):
|
||||
# Layout on the runtime host — the release directory, not the image tag, is the
|
||||
# unit that runs, so a rollback cannot publish bytes that were never tested:
|
||||
#
|
||||
# /home/sam/deployment/resume/
|
||||
# ├── releases/<UTC timestamp>/ immutable release: bin/, dist/, compose, manifest
|
||||
# ├── current -> releases/<ts> the live release (atomic symlink flip)
|
||||
# ├── previous -> releases/<ts> the release that was live before the last flip
|
||||
# └── deploy.log append-only record of every deploy attempt
|
||||
# releases/<utc-timestamp>-<git-sha>/{bin/resume-server,dist/,release.json}
|
||||
# current -> releases/<…> what the container is running
|
||||
# previous -> releases/<…> what --rollback restores
|
||||
#
|
||||
# A release is only "current" after /healthz answers 200 on 127.0.0.1:$PORT.
|
||||
# If the health gate fails, `current` is flipped back to `previous`, the previous
|
||||
# release is restarted, and this script exits non-zero.
|
||||
# A deploy is: build → test → stage → flip `current` → build the image from that
|
||||
# release directory → `compose up -d` → `curl /healthz` until ready. Any failure
|
||||
# after the flip puts `current` and the container back on the previous release
|
||||
# and exits non-zero. No step is a model decision (AGENTS.md §4.6); this script
|
||||
# is the only thing that publishes.
|
||||
#
|
||||
# Rollback rehearsal (Definition of Done: "a deliberate bad build proves rollback"):
|
||||
#
|
||||
# good=$(readlink ~/deployment/resume/current)
|
||||
# bad=$(mktemp -d) && mkdir -p "$bad/bin" "$bad/dist"
|
||||
# printf '#!/bin/sh\nexit 1\n' >"$bad/bin/resume-server" # binary that cannot start
|
||||
# chmod +x "$bad/bin/resume-server"
|
||||
# cp -a ~/deployment/resume/"$good"/dist/. "$bad/dist/" # valid pages
|
||||
# ./deploy/deploy.sh --release-dir "$bad" # expect exit 1
|
||||
# readlink ~/deployment/resume/current # still $good
|
||||
#
|
||||
# Run it on the runtime host (.13) from a checkout of this repository. It never
|
||||
# reads content/ and never calls a model.
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
APP_NAME="resume"
|
||||
DEFAULT_PORT=8096
|
||||
DEFAULT_ROOT="/home/sam/deployment/resume"
|
||||
APP=ai-resume # image tag (fixed by Phase 3 §3.2)
|
||||
PROJECT=ai-resume-site # compose project; distinct from the older
|
||||
# /home/sam/deployment/ai-resume project on .13
|
||||
PORT=8096
|
||||
HEALTH_PATH=/healthz
|
||||
|
||||
SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
ROOT="${DEPLOY_ROOT:-$DEFAULT_ROOT}"
|
||||
PORT="${APP_PORT:-$DEFAULT_PORT}"
|
||||
# Caddy on .35 proxies to this host's LAN address (phase doc §3.3), so the
|
||||
# container publishes on loopback *and* on that address. Sam's decision on
|
||||
# AUD-133, 2026-10-26: "A - dual bind: 127.0.0.1:8096 and 192.168.20.13:8096".
|
||||
LAN_IP="${APP_LAN_IP:-192.168.20.13}"
|
||||
DO_ROLLBACK=0
|
||||
HEALTH_TIMEOUT="${HEALTH_TIMEOUT:-40}"
|
||||
# The host address the port is published on. Caddy runs on .35 and proxies to
|
||||
# this address, so a loopback publish is unreachable from the edge (Sam's
|
||||
# decision, 2026-10-26, interaction 54aeb1a2). Never 0.0.0.0: the port must not
|
||||
# be reachable off the LAN. The health gate below follows this address — once the
|
||||
# port is published on the LAN address the host no longer listens on
|
||||
# 127.0.0.1:8096, so a loopback probe would refuse and every deploy would roll
|
||||
# back.
|
||||
PUBLISH_IP=${RESUME_PUBLISH_IP:-192.168.20.13}
|
||||
|
||||
ROOT=${RESUME_DEPLOY_ROOT:-/home/sam/deployment/resume}
|
||||
RELEASES=$ROOT/releases
|
||||
CURRENT=$ROOT/current
|
||||
PREVIOUS=$ROOT/previous
|
||||
|
||||
KEEP=${RESUME_KEEP_RELEASES:-5} # release directories kept on disk
|
||||
HEALTH_TRIES=${RESUME_HEALTH_TRIES:-40} # seconds to wait for /healthz
|
||||
|
||||
REPO=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
COMPOSE=$REPO/deploy/compose.yml
|
||||
DOCKERFILE=$REPO/deploy/Dockerfile
|
||||
|
||||
MODE=deploy
|
||||
RELEASE_DIR_OVERRIDE=
|
||||
|
||||
log() { printf '%s %s
|
||||
' "$(date -u +%H:%M:%SZ)" "$*" >&2; }
|
||||
warn() { printf '%s WARN %s
|
||||
' "$(date -u +%H:%M:%SZ)" "$*" >&2; }
|
||||
die() { printf '%s FAIL %s
|
||||
' "$(date -u +%H:%M:%SZ)" "$*" >&2; exit 1; }
|
||||
|
||||
on_err() {
|
||||
local rc=$? line=$1
|
||||
printf '%s FAIL line %s exited %s
|
||||
' "$(date -u +%H:%M:%SZ)" "$line" "$rc" >&2
|
||||
exit "$rc"
|
||||
}
|
||||
trap 'on_err $LINENO' ERR
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: deploy.sh [options]
|
||||
|
||||
--rollback restore the previous release and exit
|
||||
--source DIR build from DIR instead of the repo this script lives in
|
||||
--root DIR release root (default /home/sam/deployment/resume)
|
||||
--port N app port (default 8096)
|
||||
--lan-ip ADDR LAN address the container also publishes on (default 192.168.20.13)
|
||||
--health-timeout N seconds to wait for /healthz (default 40)
|
||||
-h, --help this text
|
||||
|
||||
Environment:
|
||||
GO path to the go toolchain (auto-detected, incl. /nix/store)
|
||||
DEPLOY_ROOT same as --root
|
||||
APP_PORT same as --port
|
||||
APP_LAN_IP same as --lan-ip
|
||||
EOF
|
||||
sed -n '2,13p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
|
||||
exit "${1:-0}"
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--rollback) DO_ROLLBACK=1; shift ;;
|
||||
--source) SRC_DIR="$(cd "$2" && pwd)"; shift 2 ;;
|
||||
--root) ROOT="$2"; shift 2 ;;
|
||||
--port) PORT="$2"; shift 2 ;;
|
||||
--lan-ip) LAN_IP="$2"; shift 2 ;;
|
||||
--health-timeout) HEALTH_TIMEOUT="$2"; shift 2 ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) echo "unknown argument: $1" >&2; usage >&2; exit 2 ;;
|
||||
esac
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--rollback) MODE=rollback ;;
|
||||
--status) MODE=status ;;
|
||||
--release-dir) shift; RELEASE_DIR_OVERRIDE=${1:-} ;;
|
||||
--keep) shift; KEEP=${1:-5} ;;
|
||||
-h|--help) usage 0 ;;
|
||||
*) printf 'unknown argument: %s
|
||||
' "$1" >&2; usage 1 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
RELEASES="$ROOT/releases"
|
||||
CURRENT="$ROOT/current"
|
||||
PREVIOUS="$ROOT/previous"
|
||||
LOG="$ROOT/deploy.log"
|
||||
# ---------------------------------------------------------------- helpers ---
|
||||
|
||||
mkdir -p "$RELEASES"
|
||||
compose() { docker compose -f "$COMPOSE" -p "$PROJECT" "$@"; }
|
||||
|
||||
log() { printf '%s %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" | tee -a "$LOG" >&2; }
|
||||
die() { log "FAIL: $*"; exit 1; }
|
||||
current_target() { readlink -- "$CURRENT" 2>/dev/null || true; }
|
||||
previous_target() { readlink -- "$PREVIOUS" 2>/dev/null || true; }
|
||||
|
||||
# ---------------------------------------------------------------- toolchain
|
||||
find_go() {
|
||||
if [[ -n "${GO:-}" && -x "${GO}" ]]; then echo "$GO"; return; fi
|
||||
if command -v go >/dev/null 2>&1; then command -v go; return; fi
|
||||
# NixOS: the toolchain lives in the store even when it is not on PATH.
|
||||
local cand
|
||||
cand=$(ls -d /nix/store/*-go-1.*/bin/go 2>/dev/null | grep -v '\.drv' | sort -V | tail -1)
|
||||
if [[ -n "$cand" && -x "$cand" ]]; then echo "$cand"; return; fi
|
||||
return 1
|
||||
# flip <relative-path-under-ROOT> — replace the symlink atomically. A reader sees
|
||||
# either the old release or the new one, never a missing link.
|
||||
flip() {
|
||||
ln -sfn "$1" "$CURRENT.tmp"
|
||||
mv -Tf "$CURRENT.tmp" "$CURRENT"
|
||||
}
|
||||
|
||||
compose() {
|
||||
# $1 = release directory that holds docker-compose.yml + .env
|
||||
local dir="$1"; shift
|
||||
docker compose -p "$APP_NAME" --project-directory "$dir" -f "$dir/docker-compose.yml" "$@"
|
||||
release_name_of() { basename "$1"; }
|
||||
|
||||
# The tag deploy/compose.yml runs. Releases are distinguished by content, not by
|
||||
# tag, so compose.yml keeps its fixed image name.
|
||||
image_name() { printf '%s:local' "$APP"; }
|
||||
|
||||
# The port must be ours, or free. 8095 belongs to offsite.lab.
|
||||
check_port() {
|
||||
local listening
|
||||
listening=$(ss -ltnH "sport = :$PORT" 2>/dev/null || true)
|
||||
if [ -n "$listening" ]; then
|
||||
if [ -z "$(compose ps -q resume 2>/dev/null)" ]; then
|
||||
printf '%s
|
||||
' "$listening" >&2
|
||||
die "port $PORT is held by something that is not $APP; pick the next free port and record it in reference/stack.md"
|
||||
fi
|
||||
log "port $PORT is held by our own container (expected on a redeploy)"
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- health gate
|
||||
check_health() {
|
||||
# 200 on /healthz, and if the body is JSON with a "static" field it must be true.
|
||||
# AI readiness is deliberately NOT part of this gate: Mode A must work with
|
||||
# every AI service stopped (acceptance test A1).
|
||||
local url="http://127.0.0.1:${PORT}/healthz" body code
|
||||
body=$(curl -sS --max-time 3 -w '\n%{http_code}' "$url" 2>/dev/null || true)
|
||||
code=$(printf '%s' "$body" | tail -1)
|
||||
body=$(printf '%s' "$body" | sed '$d')
|
||||
if [[ "$code" != "200" ]]; then
|
||||
log "health: $url -> HTTP ${code:-none}"
|
||||
return 1
|
||||
fi
|
||||
if printf '%s' "$body" | head -c 1 | grep -q '{' && command -v jq >/dev/null 2>&1; then
|
||||
local static_ok
|
||||
static_ok=$(printf '%s' "$body" | jq -r 'if has("static") then (.static|tostring) else "absent" end' 2>/dev/null || echo absent)
|
||||
if [[ "$static_ok" == "false" ]]; then
|
||||
log "health: $url -> 200 but static readiness is false: $body"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
log "health: $url -> 200"
|
||||
check_tools() {
|
||||
command -v docker >/dev/null || die "docker is not on PATH"
|
||||
docker compose version >/dev/null 2>&1 || die "docker compose v2 is not available"
|
||||
command -v curl >/dev/null || die "curl is not on PATH"
|
||||
command -v ss >/dev/null || die "ss (iproute2) is not on PATH"
|
||||
mkdir -p "$RELEASES"
|
||||
}
|
||||
|
||||
# The container also publishes on the LAN address because that is what Caddy
|
||||
# on .35 proxies to. A loopback-only success would be a site nobody can reach.
|
||||
local lan_url lan_code
|
||||
lan_url="http://${LAN_IP}:${PORT}/healthz"
|
||||
lan_code=$(curl -s --max-time 3 -o /dev/null -w '%{http_code}' "$lan_url" 2>/dev/null || true)
|
||||
if [[ "$lan_code" != "200" ]]; then
|
||||
log "health: $lan_url -> HTTP ${lan_code:-none} (LAN bind missing?)"
|
||||
return 1
|
||||
fi
|
||||
log "health: $lan_url -> 200"
|
||||
return 0
|
||||
memory_report() {
|
||||
local avail
|
||||
avail=$(awk '/^MemAvailable:/ {print int($2/1024)}' /proc/meminfo)
|
||||
log "host memory available: ${avail} MiB — the container cap is 120 MiB; .13 is memory-tight by design"
|
||||
if [ "${avail:-0}" -lt 512 ]; then
|
||||
warn "under 512 MiB available; the Go build may push this host into swap"
|
||||
fi
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------- build ------
|
||||
|
||||
build_and_test() {
|
||||
log "build + test in $REPO"
|
||||
(
|
||||
cd "$REPO"
|
||||
rm -rf dist
|
||||
if command -v nix >/dev/null 2>&1; then
|
||||
nix develop . -c bash -c 'make validate test dist'
|
||||
elif command -v templ >/dev/null 2>&1 && command -v go >/dev/null 2>&1; then
|
||||
make validate test dist
|
||||
else
|
||||
echo "no toolchain: neither 'nix develop' nor templ+go is available" >&2
|
||||
exit 1
|
||||
fi
|
||||
)
|
||||
[ -f "$REPO/bin/resume-server" ] || die "build produced no bin/resume-server"
|
||||
[ -f "$REPO/dist/index.html" ] || die "build produced no dist/index.html"
|
||||
log "build ok: $(jq -r '"\(.pages) pages, \(.documents) documents, \(.fragments) fragments"' "$REPO/dist/build.json" 2>/dev/null || echo 'dist/build.json unreadable')"
|
||||
}
|
||||
|
||||
# -------------------------------------------------------------- release -----
|
||||
|
||||
write_manifest() {
|
||||
local dir=$1 sha dirty
|
||||
sha=$(git -C "$REPO" rev-parse HEAD 2>/dev/null || echo unknown)
|
||||
dirty=false
|
||||
git -C "$REPO" diff --quiet 2>/dev/null || dirty=true
|
||||
cat >"$dir/release.json" <<JSON
|
||||
{
|
||||
"release": "$(basename "$dir")",
|
||||
"image": "$(image_name)",
|
||||
"git_sha": "$sha",
|
||||
"git_dirty": $dirty,
|
||||
"built_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
|
||||
"host": "$(hostname)",
|
||||
"pages": $(jq -r '.pages // 0' "$REPO/dist/build.json" 2>/dev/null || echo 0),
|
||||
"documents": $(jq -r '.documents // 0' "$REPO/dist/build.json" 2>/dev/null || echo 0)
|
||||
}
|
||||
JSON
|
||||
}
|
||||
|
||||
stage_release() {
|
||||
local name dest sha
|
||||
sha=$(git -C "$REPO" rev-parse HEAD 2>/dev/null || echo unknown)
|
||||
name=$(date -u +%Y%m%dT%H%M%SZ)-${sha:0:7}
|
||||
dest=$RELEASES/$name
|
||||
|
||||
mkdir -p "$dest/bin" "$dest/dist"
|
||||
install -m 0755 "$REPO/bin/resume-server" "$dest/bin/resume-server"
|
||||
cp -a "$REPO/dist/." "$dest/dist/"
|
||||
write_manifest "$dest"
|
||||
printf '%s
|
||||
' "$dest"
|
||||
}
|
||||
|
||||
# Copy an externally built release into releases/ so that every deployable
|
||||
# directory lives under exactly one root and `current` never dangles.
|
||||
adopt_release() {
|
||||
local src dest
|
||||
src=$(cd "$1" && pwd)
|
||||
[ -f "$src/bin/resume-server" ] || die "$src has no bin/resume-server"
|
||||
[ -f "$src/dist/index.html" ] || die "$src has no dist/index.html"
|
||||
dest=$RELEASES/$(date -u +%Y%m%dT%H%M%SZ)-manual
|
||||
mkdir -p "$dest"
|
||||
cp -a "$src/." "$dest/"
|
||||
write_manifest "$dest"
|
||||
printf '%s
|
||||
' "$dest"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- image -----
|
||||
|
||||
build_image() {
|
||||
log "docker build from $1"
|
||||
docker build --file "$DOCKERFILE" --tag "$(image_name)" "$1" 1>&2
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ run -----
|
||||
|
||||
start() {
|
||||
log "docker compose up -d ($1)"
|
||||
compose up -d --no-build --force-recreate 1>&2
|
||||
}
|
||||
|
||||
wait_healthy() {
|
||||
local i
|
||||
for ((i = 1; i <= HEALTH_TIMEOUT; i++)); do
|
||||
if check_health; then return 0; fi
|
||||
sleep 1
|
||||
done
|
||||
return 1
|
||||
local i body
|
||||
for i in $(seq 1 "$HEALTH_TRIES"); do
|
||||
if body=$(curl -fsS --max-time 3 "http://$PUBLISH_IP:$PORT$HEALTH_PATH" 2>/dev/null); then
|
||||
if printf '%s' "$body" | grep -Eq '"status"[[:space:]]*:[[:space:]]*"ok"'; then
|
||||
log "health gate passed after ${i}s"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
warn "health gate failed after ${HEALTH_TRIES}s; last response:"
|
||||
curl -sS --max-time 3 "http://$PUBLISH_IP:$PORT$HEALTH_PATH" >&2 || true
|
||||
compose logs --tail 40 resume >&2 || true
|
||||
return 1
|
||||
}
|
||||
|
||||
container_state() {
|
||||
docker inspect -f 'name={{.Name}} status={{.State.Status}} mem={{.HostConfig.Memory}} memswap={{.HostConfig.MemorySwap}} ports={{json .HostConfig.PortBindings}}' \
|
||||
"${APP_NAME}-app" 2>/dev/null || echo "container ${APP_NAME}-app: absent"
|
||||
# Definition-of-Done evidence, checked on every deploy instead of by hand:
|
||||
# mem_limit == memswap_limit == the value declared in compose.yml, and the port
|
||||
# published on $PUBLISH_IP only (the LAN address Caddy proxies to — never
|
||||
# 0.0.0.0 and no longer loopback).
|
||||
assert_shape() {
|
||||
local cid mem swap ip declared
|
||||
declared=$(awk '/^[[:space:]]*mem_limit:/ {gsub(/[^0-9]/, "", $2); print $2}' "$COMPOSE")
|
||||
cid=$(compose ps -q resume 2>/dev/null || true)
|
||||
[ -n "$cid" ] || die "no container for service 'resume'"
|
||||
mem=$(docker inspect -f '{{.HostConfig.Memory}}' "$cid")
|
||||
swap=$(docker inspect -f '{{.HostConfig.MemorySwap}}' "$cid")
|
||||
[ "$mem" = "$swap" ] || die "mem_limit ($mem) != memswap_limit ($swap)"
|
||||
[ "$mem" = "$((declared * 1024 * 1024))" ] || die "mem_limit is $mem, expected ${declared} MiB"
|
||||
ip=$(docker inspect -f '{{range $p, $b := .HostConfig.PortBindings}}{{range $b}}{{.HostIp}} {{end}}{{end}}' "$cid" | xargs)
|
||||
[ "$ip" = "$PUBLISH_IP" ] || die "published on '${ip}', expected $PUBLISH_IP only"
|
||||
log "shape ok: mem_limit == memswap_limit == ${declared} MiB, published on $PUBLISH_IP:$PORT"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- build + test
|
||||
discover_server() {
|
||||
# Contract (deploy/README.md): the app build produces one server binary at
|
||||
# bin/server or dist/server. `make build` may also emit dist/ for static assets.
|
||||
local d
|
||||
for d in "$1/bin/server" "$1/dist/server" "$1/server"; do
|
||||
if [[ -f "$d" && -x "$d" ]]; then echo "$d"; return 0; fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
deploy_release() {
|
||||
local dir old name
|
||||
dir=$1
|
||||
name=$(release_name_of "$dir")
|
||||
[ -d "$dir" ] || die "release directory does not exist: $dir"
|
||||
[ -f "$dir/bin/resume-server" ] || die "$dir has no bin/resume-server"
|
||||
[ -f "$dir/dist/index.html" ] || die "$dir has no dist/index.html — a build that cannot serve a landing page is not a release"
|
||||
|
||||
build_and_test() {
|
||||
local src="$1" out="$2"
|
||||
local gobin=""
|
||||
log "build: source $src"
|
||||
mkdir -p "$out/bin"
|
||||
cd "$src"
|
||||
build_image "$dir"
|
||||
old=$(current_target)
|
||||
|
||||
gobin=$(find_go || true)
|
||||
if [[ -z "$gobin" ]]; then
|
||||
log "build: no go toolchain found (set GO=/path/to/go)"
|
||||
else
|
||||
export PATH="$(dirname "$gobin"):$PATH"
|
||||
log "build: go $("$gobin" version)"
|
||||
fi
|
||||
# The flip happens before the container change, so `current` always describes
|
||||
# what is being started. The release it replaced becomes `previous`.
|
||||
flip "releases/$name"
|
||||
if [ -n "$old" ] && [ "$old" != "releases/$name" ]; then
|
||||
ln -sfn "$old" "$PREVIOUS"
|
||||
log "previous -> $old"
|
||||
fi
|
||||
|
||||
if [[ -f Makefile ]] && grep -qE '^build:' Makefile; then
|
||||
log "build: make build"
|
||||
make build
|
||||
else
|
||||
[[ -n "$gobin" ]] || die "no Makefile 'build' target and no go toolchain"
|
||||
log "build: go build ./cmd/server"
|
||||
CGO_ENABLED=0 "$gobin" build -trimpath -buildvcs=false -o "$out/bin/server" ./cmd/server
|
||||
fi
|
||||
if start "$name" && wait_healthy && assert_shape; then
|
||||
printf '%s
|
||||
' "$(date -u +%Y-%m-%dT%H:%M:%SZ) deploy releases/$name ok" >>"$ROOT/deploy.log"
|
||||
log "deployed $name"
|
||||
prune
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Tests fail fast, before anything is copied or flipped.
|
||||
if [[ -f Makefile ]] && grep -qE '^test:' Makefile; then
|
||||
log "test: make test"
|
||||
make test
|
||||
elif [[ -n "$gobin" ]]; then
|
||||
log "test: go test ./..."
|
||||
"$gobin" test ./...
|
||||
fi
|
||||
if [ -z "$old" ] || [ "$old" = "releases/$name" ]; then
|
||||
warn "first deploy failed and there is no previous release; leaving the container down"
|
||||
compose down >&2 || true
|
||||
rm -f "$CURRENT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Collect the binary, wherever the app's build put it.
|
||||
local bin
|
||||
bin=$(discover_server "$src" || true)
|
||||
if [[ -z "$bin" ]]; then
|
||||
bin=$(discover_server "$out" || true)
|
||||
fi
|
||||
[[ -n "$bin" ]] || die "no server binary found (looked for bin/server, dist/server, server)"
|
||||
if [[ "$(readlink -f "$bin")" != "$(readlink -f "$out/bin/server")" ]]; then
|
||||
cp -f "$bin" "$out/bin/server"
|
||||
fi
|
||||
chmod +x "$out/bin/server"
|
||||
log "build: binary $(stat -c %s "$out/bin/server") bytes, sha256 $(sha256sum "$out/bin/server" | cut -c1-16)"
|
||||
|
||||
# Static assets, if the build emits them.
|
||||
if [[ -d "$src/dist" ]]; then
|
||||
cp -a "$src/dist/." "$out/dist/" 2>/dev/null || { mkdir -p "$out/dist"; cp -a "$src/dist/." "$out/dist/"; }
|
||||
log "build: copied dist/ ($(find "$out/dist" -type f | wc -l) files)"
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- deploy
|
||||
do_deploy() {
|
||||
local src="$1"
|
||||
local ts sha branch release prev_target
|
||||
ts=$(date -u +%Y%m%dT%H%M%SZ)
|
||||
sha=$(git -C "$src" rev-parse --short HEAD 2>/dev/null || echo unknown)
|
||||
branch=$(git -C "$src" rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)
|
||||
release="$RELEASES/$ts"
|
||||
|
||||
log "deploy: $APP_NAME $ts (branch $branch, $sha) -> $release"
|
||||
mkdir -p "$release"
|
||||
|
||||
# 1. Build + test. Nothing is touched on failure.
|
||||
build_and_test "$src" "$release"
|
||||
|
||||
# 2. Release payload: binary, compose file, runtime image, manifest.
|
||||
cp -f "$src/deploy/docker-compose.yml" "$release/docker-compose.yml"
|
||||
cp -f "$src/deploy/Dockerfile" "$release/Dockerfile"
|
||||
{
|
||||
echo "app=$APP_NAME"
|
||||
echo "release=$ts"
|
||||
echo "git_sha=$(git -C "$src" rev-parse HEAD 2>/dev/null || echo unknown)"
|
||||
echo "git_branch=$branch"
|
||||
echo "built_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
echo "built_on=$(hostname)"
|
||||
echo "binary_sha256=$(sha256sum "$release/bin/server" | cut -d' ' -f1)"
|
||||
} > "$release/manifest.txt"
|
||||
printf 'APP_VERSION=%s\nAPP_PORT=%s\nAPP_LAN_IP=%s\n' "$ts" "$PORT" "$LAN_IP" > "$release/.env"
|
||||
|
||||
# 3. Runtime image, tagged with this release.
|
||||
log "deploy: docker build resume/app:$ts"
|
||||
docker build --quiet -t "resume/app:$ts" \
|
||||
--build-arg "APP_VERSION=$ts" -f "$release/Dockerfile" "$release" >>"$LOG" 2>&1 \
|
||||
|| die "image build failed"
|
||||
|
||||
# 4. Remember what is live now, then flip atomically.
|
||||
prev_target=""
|
||||
if [[ -L "$CURRENT" ]]; then prev_target=$(readlink -f "$CURRENT"); fi
|
||||
ln -sfn "$release" "$CURRENT.tmp" && mv -T "$CURRENT.tmp" "$CURRENT" || die "symlink flip failed"
|
||||
if [[ -n "$prev_target" && "$prev_target" != "$(readlink -f "$release")" ]]; then
|
||||
ln -sfn "$prev_target" "$PREVIOUS.tmp" && mv -T "$PREVIOUS.tmp" "$PREVIOUS"
|
||||
log "deploy: previous -> $prev_target"
|
||||
fi
|
||||
log "deploy: current -> $(readlink -f "$CURRENT")"
|
||||
|
||||
# 5. Bring up, then gate on health. Anything less than 200 means roll back.
|
||||
compose "$release" up -d --remove-orphans >>"$LOG" 2>&1 || log "deploy: compose up returned non-zero"
|
||||
if ! wait_healthy; then
|
||||
log "deploy: HEALTH GATE FAILED for $ts"
|
||||
rollback_internal "$ts"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "deploy: OK $ts"
|
||||
container_state | tee -a "$LOG" >&2
|
||||
docker stats --no-stream --format 'rss={{.MemUsage}} pct={{.MemPerc}}' "${APP_NAME}-app" 2>/dev/null | tee -a "$LOG" >&2 || true
|
||||
}
|
||||
|
||||
rollback_internal() {
|
||||
# Used by a failed deploy: restore the previous release, restart it, verify.
|
||||
local failed="$1" target
|
||||
if [[ ! -L "$PREVIOUS" ]]; then
|
||||
log "rollback: no previous release to restore; stopping ${APP_NAME}-app"
|
||||
compose "$CURRENT" down >>"$LOG" 2>&1 || true
|
||||
return 1
|
||||
fi
|
||||
target=$(readlink -f "$PREVIOUS")
|
||||
log "rollback: $failed failed -> restoring $target"
|
||||
ln -sfn "$target" "$CURRENT.tmp" && mv -T "$CURRENT.tmp" "$CURRENT"
|
||||
compose "$target" up -d --remove-orphans >>"$LOG" 2>&1 || true
|
||||
if wait_healthy; then
|
||||
log "rollback: previous release healthy ($(readlink -f "$CURRENT"))"
|
||||
else
|
||||
log "rollback: previous release ALSO unhealthy — manual attention required"
|
||||
return 1
|
||||
fi
|
||||
warn "deploy of $name failed; restoring $old"
|
||||
flip "$old"
|
||||
build_image "$ROOT/$old" || die "could not rebuild the previous image; container state unknown"
|
||||
if start "$old" && wait_healthy && assert_shape; then
|
||||
printf '%s
|
||||
' "$(date -u +%Y-%m-%dT%H:%M:%SZ) rolled back to $old after failed deploy of releases/$name" >>"$ROOT/deploy.log"
|
||||
die "deploy of releases/$name failed; rolled back to $old (the failed release is kept for inspection)"
|
||||
fi
|
||||
die "deploy of releases/$name failed AND rollback to $old failed — container state is unknown; run '$0 --status' and read $ROOT/deploy.log"
|
||||
}
|
||||
|
||||
do_rollback() {
|
||||
local target
|
||||
if [[ ! -L "$PREVIOUS" ]]; then die "no previous release at $PREVIOUS — nothing to roll back to"; fi
|
||||
target=$(readlink -f "$PREVIOUS")
|
||||
[[ -d "$target" ]] || die "previous release target missing: $target"
|
||||
log "rollback: current=$( [[ -L "$CURRENT" ]] && readlink -f "$CURRENT" || echo none) -> $target"
|
||||
local cur prev
|
||||
cur=$(current_target)
|
||||
prev=$(previous_target)
|
||||
[ -n "$prev" ] || die "no previous release recorded; nothing to roll back to"
|
||||
[ "$prev" != "$cur" ] || die "previous and current are the same release ($cur); nothing to roll back"
|
||||
[ -d "$ROOT/$prev" ] || die "previous release directory is missing: $ROOT/$prev"
|
||||
|
||||
# Keep the release we are leaving, so the rollback is itself reversible.
|
||||
local leaving=""
|
||||
if [[ -L "$CURRENT" ]]; then leaving=$(readlink -f "$CURRENT"); fi
|
||||
ln -sfn "$target" "$CURRENT.tmp" && mv -T "$CURRENT.tmp" "$CURRENT" || die "symlink flip failed"
|
||||
if [[ -n "$leaving" && "$leaving" != "$target" ]]; then
|
||||
ln -sfn "$leaving" "$PREVIOUS.tmp" && mv -T "$PREVIOUS.tmp" "$PREVIOUS"
|
||||
fi
|
||||
log "rolling back: $cur -> $prev"
|
||||
build_image "$ROOT/$prev"
|
||||
flip "$prev"
|
||||
if start "$prev" && wait_healthy && assert_shape; then
|
||||
[ -n "$cur" ] && ln -sfn "$cur" "$PREVIOUS"
|
||||
printf '%s
|
||||
' "$(date -u +%Y-%m-%dT%H:%M:%SZ) rollback $cur -> $prev" >>"$ROOT/deploy.log"
|
||||
log "rolled back to $prev; $cur is now previous"
|
||||
return 0
|
||||
fi
|
||||
|
||||
compose "$target" up -d --remove-orphans >>"$LOG" 2>&1 || log "rollback: compose up returned non-zero"
|
||||
if wait_healthy; then
|
||||
log "rollback: OK, current -> $target"
|
||||
container_state | tee -a "$LOG" >&2
|
||||
exit 0
|
||||
fi
|
||||
log "rollback: FAILED, $target is not healthy"
|
||||
exit 1
|
||||
warn "rollback to $prev failed; returning to $cur"
|
||||
flip "$cur"
|
||||
build_image "$ROOT/$cur" || die "could not rebuild $cur; container state unknown"
|
||||
start "$cur" && wait_healthy || die "return to $cur failed; container state unknown"
|
||||
die "rollback to $prev failed; $cur was restored"
|
||||
}
|
||||
|
||||
mkdir -p "$ROOT"
|
||||
log "=== ${APP_NAME} deploy.sh start (pid $$) ==="
|
||||
if (( DO_ROLLBACK == 1 )); then
|
||||
do_rollback
|
||||
else
|
||||
do_deploy "$SRC_DIR"
|
||||
fi
|
||||
# Keep the newest KEEP releases plus current and previous, nothing else.
|
||||
prune() {
|
||||
local cur prev name dir
|
||||
cur=$(current_target); prev=$(previous_target)
|
||||
for dir in $(ls -1dt "$RELEASES"/*/ 2>/dev/null | tail -n +"$((KEEP + 1))"); do
|
||||
name="releases/$(basename "$dir")"
|
||||
if [ "$name" = "$cur" ] || [ "$name" = "$prev" ]; then continue; fi
|
||||
rm -rf -- "$dir"
|
||||
log "pruned $name"
|
||||
done
|
||||
}
|
||||
|
||||
do_status() {
|
||||
local cur prev body
|
||||
cur=$(current_target); prev=$(previous_target)
|
||||
body=$(curl -fsS --max-time 3 "http://$PUBLISH_IP:$PORT$HEALTH_PATH" 2>/dev/null || true)
|
||||
printf 'root %s
|
||||
' "$ROOT"
|
||||
printf 'current %s
|
||||
' "${cur:-<none>}"
|
||||
printf 'previous %s
|
||||
' "${prev:-<none>}"
|
||||
if [ -n "$body" ]; then
|
||||
printf 'health %s
|
||||
' "$(printf '%s' "$body" | jq -c '{status, pages: .static.pages, search: .search.ready}' 2>/dev/null || printf '%s' "$body")"
|
||||
else
|
||||
printf 'health unreachable
|
||||
'
|
||||
fi
|
||||
compose ps || true
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ main ----
|
||||
|
||||
case $MODE in
|
||||
status)
|
||||
do_status
|
||||
;;
|
||||
rollback)
|
||||
check_tools
|
||||
do_rollback
|
||||
;;
|
||||
deploy)
|
||||
check_tools
|
||||
check_port
|
||||
memory_report
|
||||
if [ -n "$RELEASE_DIR_OVERRIDE" ]; then
|
||||
deploy_release "$(adopt_release "$RELEASE_DIR_OVERRIDE")"
|
||||
else
|
||||
build_and_test
|
||||
deploy_release "$(stage_release)"
|
||||
fi
|
||||
log "live: $(current_target)"
|
||||
;;
|
||||
esac
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
# resume — runtime container (Phase 3 §3.2).
|
||||
#
|
||||
# Copied into each release directory by deploy/deploy.sh; the release's .env
|
||||
# supplies APP_VERSION (the image tag). Never edited on the host — change it
|
||||
# here, in git, and deploy again.
|
||||
#
|
||||
# Limits that are not negotiable (AGENTS.md §4.3):
|
||||
# mem_limit == memswap_limit == 120m -> the container cannot touch host swap
|
||||
# ports never 0.0.0.0 -> Caddy on .35 is the only public listener
|
||||
#
|
||||
# Published on two explicit addresses and nothing else (Sam, 2026-10-26, on
|
||||
# AUD-133: "A - dual bind: 127.0.0.1:8096 and 192.168.20.13:8096"):
|
||||
# 127.0.0.1 -> deploy.sh health gate and local checks on .13
|
||||
# 192.168.20.13 -> the LAN address Caddy on .35 proxies to (phase doc §3.3)
|
||||
# A wildcard bind is still forbidden; if APP_LAN_IP is unset the LAN address
|
||||
# defaults to this host's own 192.168.20.13.
|
||||
#
|
||||
# .13 has every default Docker address pool subnetted out (many projects), so a
|
||||
# single-container service uses the host bridge and publishes its port rather
|
||||
# than creating a project network. Same pattern as compass-teams-nhs.
|
||||
|
||||
name: resume
|
||||
|
||||
services:
|
||||
app:
|
||||
image: resume/app:${APP_VERSION}
|
||||
container_name: resume-app
|
||||
restart: unless-stopped
|
||||
network_mode: bridge
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PORT:-8096}:8096"
|
||||
- "${APP_LAN_IP:-192.168.20.13}:${APP_PORT:-8096}:8096"
|
||||
environment:
|
||||
PORT: "8096"
|
||||
APP_VERSION: "${APP_VERSION}"
|
||||
# Mode B values arrive in Phase 5 from ~/.config/environment.d/ — never here.
|
||||
mem_limit: 120m
|
||||
memswap_limit: 120m
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8096/healthz"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
55
deploy/qdrant.yml
Normal file
55
deploy/qdrant.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
# Qdrant for the corpus (docs/phase-4-corpus.md, reference/stack.md).
|
||||
#
|
||||
# mem_limit == memswap_limit == 400m the cap in the memory budget, and the
|
||||
# container cannot reach host swap
|
||||
# ports on 127.0.0.1 only Caddy on .35 is the only public listener
|
||||
# its own compose project stopping or recreating it cannot disturb
|
||||
# the Mode A site (test A1 still passes:
|
||||
# the site never calls Qdrant)
|
||||
#
|
||||
# Run on the runtime host (.13):
|
||||
#
|
||||
# docker compose -f deploy/qdrant.yml up -d
|
||||
# curl -s http://127.0.0.1:6333/ # must answer with a version
|
||||
#
|
||||
# Then, in the corpus environment (RESUME_QDRANT_URL is already the default):
|
||||
#
|
||||
# python3 -m corpus.cli rebuild
|
||||
#
|
||||
# Deleting the volume is always recoverable: `rebuild` re-derives every vector
|
||||
# from content/.
|
||||
|
||||
name: ai-resume-index
|
||||
|
||||
services:
|
||||
qdrant:
|
||||
image: qdrant/qdrant:v1.20.0
|
||||
container_name: ai-resume-qdrant
|
||||
restart: unless-stopped
|
||||
mem_limit: 400m
|
||||
memswap_limit: 400m
|
||||
ports:
|
||||
- "127.0.0.1:6333:6333" # REST — the pipeline and the query service use this
|
||||
- "127.0.0.1:6334:6334" # gRPC — reserved for the Phase 5 query service
|
||||
volumes:
|
||||
- qdrant_storage:/qdrant/storage
|
||||
environment:
|
||||
# The corpus is ~1k chunks today and the measurement in
|
||||
# reference/stack.md records the ceiling. Keep the HNSW defaults; do not
|
||||
# tune for speed on a memory-bound host.
|
||||
QDRANT__TELEMETRY_DISABLED: "true"
|
||||
QDRANT__LOG_LEVEL: "WARN"
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: 10m
|
||||
max-file: "3"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "bash -c '</dev/tcp/127.0.0.1/6333' || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
volumes:
|
||||
qdrant_storage:
|
||||
@@ -1,198 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# resume — rollback rehearsal (Phase 3 DoD: "Rollback rehearsed once and documented").
|
||||
#
|
||||
# Builds three throwaway Go services in a scratch directory and runs the real
|
||||
# deploy mechanism against the real release root and the real port:
|
||||
#
|
||||
# 1. good -> deploys, health-gated, serving on loopback and the LAN address
|
||||
# 2. broken -> does not compile: deploy must fail fast, current must not move
|
||||
# 3. unhealthy -> builds and runs, but /healthz is 500: the health gate must
|
||||
# roll back to the previous release and exit non-zero
|
||||
# 4. good again -> then `deploy.sh --rollback` must restore the earlier release
|
||||
#
|
||||
# It leaves nothing running and removes the release root it created, unless
|
||||
# KEEP_REHEARSAL=1. Nothing outside $DEPLOY_ROOT, the scratch dir and this
|
||||
# service's own container/image is touched.
|
||||
#
|
||||
# ./deploy/rehearse.sh
|
||||
#
|
||||
# Exit 0 means every assertion held; the transcript is the evidence.
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
DEPLOY="$HERE/deploy.sh"
|
||||
ROOT="${DEPLOY_ROOT:-/home/sam/deployment/resume}"
|
||||
PORT="${APP_PORT:-8096}"
|
||||
LAN_IP="${APP_LAN_IP:-192.168.20.13}"
|
||||
SCRATCH="${REHEARSAL_DIR:-$(mktemp -d -t resume-rehearsal-XXXXXX)}"
|
||||
KEEP="${KEEP_REHEARSAL:-0}"
|
||||
|
||||
pass=0; fail=0
|
||||
ok() { printf ' PASS %s\n' "$*"; pass=$((pass + 1)); }
|
||||
no() { printf ' FAIL %s\n' "$*"; fail=$((fail + 1)); }
|
||||
hdr() { printf '\n== %s\n' "$*"; }
|
||||
|
||||
cleanup() {
|
||||
if [[ "$KEEP" == "1" ]]; then
|
||||
printf '\nkept for inspection: container resume-app, images resume/app:*, release root %s, scratch %s\n' "$ROOT" "$SCRATCH"
|
||||
return 0
|
||||
fi
|
||||
if docker ps -a --format '{{.Names}}' | grep -qx 'resume-app'; then
|
||||
docker rm -f resume-app >/dev/null 2>&1 || true
|
||||
fi
|
||||
docker images --format '{{.Repository}}:{{.Tag}}' | grep -q '^resume/app:' \
|
||||
&& docker images --format '{{.Repository}}:{{.Tag}}' | grep '^resume/app:' \
|
||||
| xargs -r docker rmi -f >/dev/null 2>&1 || true
|
||||
rm -rf "$ROOT"
|
||||
[[ "$SCRATCH" == /tmp/resume-rehearsal-* || -n "${REHEARSAL_DIR:-}" ]] && rm -rf "$SCRATCH"
|
||||
printf '\ncleaned: container, images, release root and rehearsal sources removed\n'
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# --------------------------------------------------------------- fixtures
|
||||
make_app() { # $1 dir, $2 mode: good|broken|unhealthy
|
||||
local dir="$1" mode="$2"
|
||||
mkdir -p "$dir/cmd/server" "$dir/deploy"
|
||||
cp -f "$HERE/docker-compose.yml" "$HERE/Dockerfile" "$dir/deploy/"
|
||||
printf 'module rehearsal\n\ngo 1.25\n' > "$dir/go.mod"
|
||||
cat > "$dir/cmd/server/main.go" <<'GO'
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8096"
|
||||
}
|
||||
bind := os.Getenv("BIND")
|
||||
if bind == "" {
|
||||
bind = "0.0.0.0"
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
BROKEN
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, `{"status":"ok","static":true,"ai":"disabled","version":%q,"time":%q}`,
|
||||
os.Getenv("APP_VERSION"), time.Now().UTC().Format(time.RFC3339))
|
||||
})
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprintf(w, "<!doctype html><title>rehearsal</title><p>version %s</p>", os.Getenv("APP_VERSION"))
|
||||
})
|
||||
srv := &http.Server{Addr: bind + ":" + port, Handler: mux, ReadHeaderTimeout: 5 * time.Second}
|
||||
log.Printf("listening on %s:%s", bind, port)
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
GO
|
||||
local patch=''
|
||||
case "$mode" in
|
||||
good) patch='' ;;
|
||||
unhealthy) patch='w.WriteHeader(http.StatusInternalServerError)' ;;
|
||||
broken) patch='' ;;
|
||||
esac
|
||||
python3 - "$dir/cmd/server/main.go" "$patch" <<'PY'
|
||||
import sys
|
||||
p, patch = sys.argv[1], sys.argv[2]
|
||||
s = open(p).read().replace("BROKEN", patch, 1)
|
||||
open(p, "w").write(s)
|
||||
PY
|
||||
if [[ "$mode" == "broken" ]]; then
|
||||
sed -i 's/^func main() {/func main( {/' "$dir/cmd/server/main.go"
|
||||
fi
|
||||
}
|
||||
|
||||
health() { curl -s -m 3 -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/healthz" || true; }
|
||||
health_lan() { curl -s -m 3 -o /dev/null -w '%{http_code}' "http://$LAN_IP:$PORT/healthz" || true; }
|
||||
listeners() { ss -ltn 2>/dev/null | awk -v p=":${PORT}\$" '$4 ~ p {print $4}' | sort -u; }
|
||||
serving_version() { curl -s -m 3 "http://127.0.0.1:$PORT/healthz" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p'; }
|
||||
current_target() { [[ -L "$ROOT/current" ]] && readlink -f "$ROOT/current" || echo none; }
|
||||
|
||||
# --------------------------------------------------------------- 0. preconditions
|
||||
hdr "0. preconditions"
|
||||
if ss -ltn 2>/dev/null | awk '{print $4}' | grep -q ":$PORT\$"; then
|
||||
no "port $PORT already in use — nothing started"
|
||||
exit 1
|
||||
fi
|
||||
ok "port $PORT free"
|
||||
rm -rf "$ROOT"
|
||||
make_app "$SCRATCH/good" good
|
||||
make_app "$SCRATCH/broken" broken
|
||||
make_app "$SCRATCH/unhealthy" unhealthy
|
||||
ok "three throwaway sources built in $SCRATCH"
|
||||
|
||||
# --------------------------------------------------------------- 1. good deploy
|
||||
hdr "1. good release deploys and health-gates"
|
||||
if "$DEPLOY" --source "$SCRATCH/good" > "$SCRATCH/1-deploy.log" 2>&1; then
|
||||
ok "deploy.sh exit 0"
|
||||
else
|
||||
no "deploy.sh exit non-zero"; tail -20 "$SCRATCH/1-deploy.log"; exit 1
|
||||
fi
|
||||
[[ "$(health)" == "200" ]] && ok "/healthz 200 on 127.0.0.1:$PORT" || no "/healthz not 200"
|
||||
v1=$(serving_version); [[ -n "$v1" ]] && ok "serving release $v1" || no "no version reported"
|
||||
ss -ltn | grep -q "127.0.0.1:$PORT" && ok "container publishes on 127.0.0.1:$PORT (loopback, used by the deploy.sh health gate)" || no "no loopback bind"
|
||||
ss -ltn | grep -q "$LAN_IP:$PORT" && ok "container publishes on $LAN_IP:$PORT (the address Caddy on .35 proxies to)" || no "no LAN bind on $LAN_IP:$PORT"
|
||||
if listeners | grep -qE '^(0\.0\.0\.0|\*|\[::\]|::)'; then
|
||||
no "wildcard bind on $PORT: $(listeners | tr '\n' ' ')"
|
||||
else
|
||||
ok "no wildcard bind on $PORT — only: $(listeners | tr '\n' ' ')"
|
||||
fi
|
||||
[[ "$(health_lan)" == "200" ]] && ok "/healthz 200 over the LAN address http://$LAN_IP:$PORT" || no "LAN address not answering"
|
||||
mem=$(docker inspect -f '{{.HostConfig.Memory}}' resume-app); swp=$(docker inspect -f '{{.HostConfig.MemorySwap}}' resume-app)
|
||||
[[ "$mem" == "$swp" && "$mem" != "0" ]] && ok "memswap_limit == mem_limit ($mem bytes)" || no "mem limits differ ($mem vs $swp)"
|
||||
|
||||
# --------------------------------------------------------------- 2. broken build
|
||||
hdr "2. build failure fails fast, current does not move"
|
||||
before=$(current_target)
|
||||
set +e; "$DEPLOY" --source "$SCRATCH/broken" > "$SCRATCH/2-broken.log" 2>&1; rc=$?; set -e
|
||||
[[ $rc -ne 0 ]] && ok "deploy.sh exit $rc (non-zero, as required)" || no "broken build reported success"
|
||||
[[ "$(current_target)" == "$before" ]] && ok "current unchanged ($before)" || no "current moved after a failed build"
|
||||
[[ "$(health)" == "200" ]] && ok "previous release still serving" || no "site down after failed build"
|
||||
|
||||
# --------------------------------------------------------------- 3. health gate
|
||||
hdr "3. unhealthy release is rolled back by the health gate"
|
||||
before=$(current_target)
|
||||
set +e; HEALTH_TIMEOUT=10 "$DEPLOY" --source "$SCRATCH/unhealthy" > "$SCRATCH/3-unhealthy.log" 2>&1; rc=$?; set -e
|
||||
[[ $rc -ne 0 ]] && ok "deploy.sh exit $rc (non-zero, as required)" || no "unhealthy release reported success"
|
||||
[[ "$(current_target)" == "$before" ]] && ok "current restored to $before" || no "current left on $(current_target)"
|
||||
[[ "$(health)" == "200" ]] && ok "rolled-back release serving again" || no "no healthy release after rollback"
|
||||
grep -q 'HEALTH GATE FAILED' "$SCRATCH/3-unhealthy.log" && ok "log records the health-gate failure" || no "no health-gate failure recorded"
|
||||
|
||||
# --------------------------------------------------------------- 4. --rollback
|
||||
hdr "4. deploy.sh --rollback restores the previous release in one command"
|
||||
"$DEPLOY" --source "$SCRATCH/good" > "$SCRATCH/4-deploy.log" 2>&1 && ok "second good release deployed" || no "second deploy failed"
|
||||
v2=$(serving_version)
|
||||
prev=$(readlink -f "$ROOT/previous")
|
||||
set +e; "$DEPLOY" --rollback > "$SCRATCH/4-rollback.log" 2>&1; rc=$?; set -e
|
||||
[[ $rc -eq 0 ]] && ok "rollback exit 0" || no "rollback exit $rc"
|
||||
[[ "$(readlink -f "$ROOT/current")" == "$prev" ]] && ok "current flipped back to $prev" || no "current not restored"
|
||||
[[ "$(health)" == "200" ]] && ok "rolled-back release healthy" || no "rolled-back release unhealthy"
|
||||
v3=$(serving_version)
|
||||
[[ -n "$v3" && "$v3" != "$v2" ]] && ok "serving $v3 again (was $v2)" || no "version did not change on rollback"
|
||||
|
||||
# --------------------------------------------------------------- 5. restart
|
||||
hdr "5. N3: docker compose down && up restores the service"
|
||||
rel=$(readlink -f "$ROOT/current")
|
||||
docker compose -p resume --project-directory "$rel" -f "$rel/docker-compose.yml" down >/dev/null 2>&1
|
||||
[[ "$(health)" != "200" ]] && ok "service down after compose down" || no "still serving after compose down"
|
||||
docker compose -p resume --project-directory "$rel" -f "$rel/docker-compose.yml" up -d >/dev/null 2>&1
|
||||
restored=0
|
||||
for _ in $(seq 1 20); do
|
||||
if [[ "$(health)" == "200" ]]; then restored=1; break; fi
|
||||
sleep 1
|
||||
done
|
||||
[[ $restored == 1 ]] && ok "service restored after compose up" || no "service did not come back within 20s"
|
||||
|
||||
# --------------------------------------------------------------- verdict
|
||||
hdr "verdict"
|
||||
printf 'passed %d, failed %d\n' "$pass" "$fail"
|
||||
[[ $fail -eq 0 ]] || exit 1
|
||||
64
docs/content-redaction.md
Normal file
64
docs/content-redaction.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# Content redaction pass — what was excluded and why
|
||||
|
||||
**Scope:** every document under `content/`, authored in Phase 1 (issue AUD-131).
|
||||
**Date of pass:** 2026-10-26.
|
||||
**Sources read for this pass:** `../resume_ai_system/plan/EVIDENCE-NOTES.md`, `../resume_ai_system/plan/draft-content.md`, `../resume_ai_system/plan/20-content-spine.md`, `../resume_ai_system/old_projects.md`, `resume_2025.docx`, `reference/content-map.md`, `reference/decisions.md`, `reference/stack.md`, and the Obsidian backup notes.
|
||||
|
||||
Acceptance test C6 covers this: *no family, address, credential or third-party personal data in pages or corpus.* The list below is what was removed to satisfy it, and the reason for each removal.
|
||||
|
||||
## Excluded — personal identifiers
|
||||
|
||||
| Item | Where it appeared | Why excluded |
|
||||
|---|---|---|
|
||||
| Date of birth | `resume_2025.docx`, Personal Details | Not needed to assess capability. Age is a discriminator and an identity-theft input. |
|
||||
| Phone number | `resume_2025.docx`, Personal Details | Publishing a mobile exposes it to scrapers. Sam has not confirmed a phone number belongs on the site (`reference/stack.md` open items). |
|
||||
| Street address / suburb | `resume_2025.docx` ("live in Ivanhoe"), `draft-content.md` | Address-level detail is a safety issue and is irrelevant to the work. "Melbourne based" is the published granularity. |
|
||||
| Email address | `resume_2025.docx` | Kept off the pages in Phase 1. Contact details are an open decision for Sam; the narrative states the policy rather than the address. |
|
||||
| Passport and visa status | `resume_2025.docx` | Immigration status is not published. Working rights are a conversation, not a web page. |
|
||||
| Family names and details | `EVIDENCE-NOTES.md` (school portals, children, household) | Never published. The 2020–2026 period is described as "full-time parent to two children" with no names, ages, schools or locations. |
|
||||
|
||||
## Excluded — third-party personal data
|
||||
|
||||
| Item | Where it appeared | Why excluded |
|
||||
|---|---|---|
|
||||
| Names of individual client contacts, agency colleagues and referees | résumé prose | No third party has consented to being named. Clients are represented by brand only, which was public campaign work. |
|
||||
| Details identifying minors in a personal anecdote | `EVIDENCE-NOTES.md` interests ("a 2 day hike … with 7 13 year old boys") | Rewritten as "a group of teenagers in tow". A count of identifiable children is not needed to make the point. |
|
||||
| Names of household members appearing in automation examples | `EVIDENCE-NOTES.md` | Household automation is described by function (lighting, audio, doors) and never by person. |
|
||||
|
||||
## Excluded — credentials and infrastructure detail
|
||||
|
||||
| Item | Where it appeared | Why excluded |
|
||||
|---|---|---|
|
||||
| API keys, tokens, passphrases, secret file names | n/a — never copied | Nothing from `~/.config/environment.d/` was read or quoted. No key material enters the repo, per `AGENTS.md` §4.4. |
|
||||
| Backup passphrase file path and repository passphrase | Obsidian backup note | Publishing the location of a passphrase file is a hint worth withholding. Backups are described by tier, tool and retention only. |
|
||||
| Hostnames, internal IP addresses, ports-by-host, public DNS names | `reference/stack.md`, Obsidian notes, live inspection | Internal addressing is not needed to judge the platform work. Hosts are described by role ("the runtime host", "the edge host"). |
|
||||
| Internal service URLs and dashboards | `EVIDENCE-NOTES.md` (the pipeline dashboard) | Described as "a dashboard built to keep track of it". A private URL on a public page is an invitation. |
|
||||
| Third-party account identifiers, billing detail | n/a | Not in the sources and not wanted. |
|
||||
|
||||
## Excluded — unverifiable or invented content
|
||||
|
||||
| Item | Where it appeared | Why excluded |
|
||||
|---|---|---|
|
||||
| "42 devices" | recovered site content | Explicitly withdrawn in `draft-content.md`. Not used. |
|
||||
| "~800ms end-to-end" latency | recovered site content | Explicitly withdrawn. Not used. |
|
||||
| Invented venues, competition results, swell sizes, distances and restaurants | recovered pursuits content | Marked ❌ SUSPECT and confirmed as generated. Replaced with one honest line per pursuit. |
|
||||
| "Diajio" | recovered client list | Spelling error for **Diageo**. Corrected, not carried. |
|
||||
| Screenshot of the BBC National Archive engagement | recovered site content | The work was an internal tool. Described only; no image. |
|
||||
|
||||
## Metric discipline
|
||||
|
||||
Every numeric `metric` in `content/` is traced to a source line, and the trace is recorded per metric in the completion comment on AUD-131. Two figures were deliberately **not** promoted:
|
||||
|
||||
- **Businesses: 3 active, 12 cumulative.** The site says three are running now and twelve adopted it over time. Conflating them would be the easiest inflation available on this site.
|
||||
- **Where-Woof: deployed, pre-market.** The word "commercial" is not used about it anywhere.
|
||||
|
||||
## Two numbers reported, not smoothed
|
||||
|
||||
1. **Agencies: 22 published, 23 in the summary material.** The recovered, verified list holds twenty-two agency names. The published count is the count of the list. If a name is missing, the number rises to twenty-three and the note in `content/history/l3/history-client-wall.md` is deleted.
|
||||
2. **Period start dates.** Where the sources give no start date, the period is anchored on the homelab year (2020) and the page says so. Exact start dates are Sam's to correct.
|
||||
|
||||
## Open questions for Sam
|
||||
|
||||
- Contact details: email only, or also a phone number? (`about-narrative.md` states the current policy: email, supplied directly.)
|
||||
- Is a Melbourne suburb to be published? Currently excluded.
|
||||
- The missing twenty-third agency, if there is one.
|
||||
184
docs/content-schema.md
Normal file
184
docs/content-schema.md
Normal file
@@ -0,0 +1,184 @@
|
||||
# Content schema (frozen)
|
||||
|
||||
This document states the front-matter schema that every file in `content/` must carry, and the rules the validator enforces. The schema is the one in [`AGENTS.md` §7](../AGENTS.md#7-content-conventions). Read that section first; this page is the exact, testable restatement of it.
|
||||
|
||||
**The schema is frozen.** Any change to the fields, their types, their enum values or the artefact rules is a **breaking change**: it invalidates every existing file in `content/` and both renderers. A change requires **Sam's written approval** before it is made. Do not widen or narrow the schema to make a document validate — fix the document.
|
||||
|
||||
---
|
||||
|
||||
## 1. How to validate
|
||||
|
||||
```
|
||||
make validate # the gate: exits 0 only on a clean tree
|
||||
make test # validator self-test, 20 fixture cases
|
||||
```
|
||||
|
||||
`make validate` is the command CI and `deploy.sh` call in later phases. Run it before every content commit.
|
||||
|
||||
Both targets invoke:
|
||||
|
||||
```
|
||||
python3 tools/validate_content.py [--root DIR] [--content DIR] [--quiet]
|
||||
```
|
||||
|
||||
Exit codes: `0` clean (also when `content/` does not exist yet), `1` at least one violation, `2` usage or internal error.
|
||||
|
||||
---
|
||||
|
||||
## 2. Document shape
|
||||
|
||||
* Every `.md` file under `content/` is a **document** and is validated. Other file types under `content/` (images, SVG, PDF) are **assets** and are not validated — they exist to be referenced.
|
||||
* A document starts with a front-matter block on line 1:
|
||||
|
||||
```
|
||||
---
|
||||
<fields>
|
||||
---
|
||||
```
|
||||
|
||||
* The closing delimiter is `---` (or `...`). Everything after it is the Markdown body and is not validated.
|
||||
* One document per file. One front-matter block per file.
|
||||
* `content/` is organised by area:
|
||||
|
||||
```
|
||||
content/
|
||||
ai/ platform/ edge/ products/ history/ about/ # documents, plus l3/ below each
|
||||
assets/ # diagrams, screenshots, logos
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Fields
|
||||
|
||||
All thirteen fields are **required on every document**, including L3 stubs (which use empty lists). Order does not matter.
|
||||
|
||||
| Field | Type | Rule |
|
||||
|---|---|---|
|
||||
| `id` | string | Lowercase kebab-case: `a-z`, `0-9`, single hyphens between parts. **Must equal the file name** without `.md`. **Unique** across all of `content/`. |
|
||||
| `area` | enum | One of `ai`, `platform`, `edge`, `products`, `history`, `about`. |
|
||||
| `level` | integer | `1`, `2` or `3` (depth level: L1 landing, L2 capability, L3 artefact). |
|
||||
| `title` | string | Non-empty. Corporate capability framing (see `AGENTS.md` §7); product names are evidence, not headings. |
|
||||
| `claim` | string | Non-empty. One sentence a hiring manager can verify. |
|
||||
| `confidence` | enum | One of `delivered`, `running`, `in-progress`, `abandoned`, `historic`. **Displayed on the page.** Never promote a value to make a page read better. |
|
||||
| `period` | string | Non-empty. Free text, e.g. `"2023–"`, `"2019–2024"`, `"2000–2014"`. |
|
||||
| `tech` | list of strings | Non-empty strings. May be `[]`. Product and technology names belong here, not in `title`. |
|
||||
| `metrics` | list of maps | May be `[]`. Each item is exactly `{label, value}`. Both non-empty. `value` may be a number (`8`) or a string (`"50+"`). Every value must trace to the evidence notes — the validator checks shape, not truth. |
|
||||
| `artefacts` | list of maps | May be `[]`. Each item is exactly `{type, label, url}`. See §4. |
|
||||
| `public` | boolean | `true` or `false`. `false` keeps the document out of the published output. |
|
||||
| `last_verified` | date | `YYYY-MM-DD` and a real calendar date. |
|
||||
| `tags` | list of strings | Non-empty strings. May be `[]`. |
|
||||
|
||||
**Unknown fields are rejected — at the top level and inside `metrics[]` and `artefacts[]`.** There is no extension point. A field that an author thinks is useful is a schema change (§6).
|
||||
|
||||
The one exception to "all required": nothing. `tech: []`, `metrics: []`, `artefacts: []` and `tags: []` are the correct way to say "this document has none". An omitted field is a violation; an empty list is not.
|
||||
|
||||
---
|
||||
|
||||
## 4. Artefact links — dangling references are fatal
|
||||
|
||||
Every `artefacts[].url` must resolve to a file that exists in this repository. This is the rule that keeps every claim one step away from its proof.
|
||||
|
||||
| `url` form | Resolves against | Example |
|
||||
|---|---|---|
|
||||
| `content/...` or any path without a leading `./` or `../` | the repository root | `content/edge/l3/audio-architecture.md` |
|
||||
| `./...` or `../...` | the directory of the document | `./l3/audio-architecture.md` |
|
||||
|
||||
Rules:
|
||||
|
||||
* The target must be an existing **file** — an L3 stub document (`.md`) or an asset (`.svg`, `.png`, `.pdf`, …). A directory is a violation.
|
||||
* **External URLs (`https://…`) are not allowed.** If the artefact lives outside the repository (a Gitea repo, a video), create an in-repo L3 stub document that describes and links it, and point `url` at that stub. This keeps every drill-down verifiable offline and keeps Mode A free of third-party dependencies.
|
||||
* Root-absolute paths (`/content/...`) are not allowed — they do not survive a release directory move.
|
||||
* A path that escapes the repository is a violation.
|
||||
|
||||
`type` is one of `repo`, `image`, `video`, `doc` and describes the artefact behind the link, not the file extension of the stub.
|
||||
|
||||
---
|
||||
|
||||
## 5. Cross-document rules
|
||||
|
||||
* **`id` is unique** across `content/`. Two files with the same `id` are a violation on the second file.
|
||||
* **`id` matches the file name.** `content/edge/snapcast.md` must have `id: snapcast`; an L3 stub `content/edge/l3/snapcast-code.md` must have `id: snapcast-code`.
|
||||
|
||||
---
|
||||
|
||||
## 6. Changing the schema
|
||||
|
||||
Breaking (requires Sam's written approval, then a migration of every file):
|
||||
|
||||
* adding, removing or renaming a field
|
||||
* changing a field's type
|
||||
* adding or removing an enum value (`area`, `level`, `confidence`, artefact `type`)
|
||||
* relaxing or tightening a rule (e.g. allowing external artefact URLs)
|
||||
|
||||
Not a schema change: adding documents, filling empty lists, adding tags or metrics within the existing shape.
|
||||
|
||||
A change that supersedes this document must update, in one commit: `AGENTS.md` §7, this file, `tools/validate_content.py`, and the fixtures under `test/fixtures/`.
|
||||
|
||||
---
|
||||
|
||||
## 7. Error output
|
||||
|
||||
One line per violation, on stdout:
|
||||
|
||||
```
|
||||
<path>:<line>: <field>: <rule broken>
|
||||
```
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
content/edge/audio.md:14: artefacts[0].url: dangling reference: 'content/edge/l3/not-written-yet.md' does not exist (create the L3 stub or asset)
|
||||
content/edge/audio.md:16: author: unknown field (the schema is frozen; see docs/content-schema.md)
|
||||
content/edge/audio.md:3: area: must be one of ai, platform, edge, products, history, about
|
||||
content/edge/audio.md:1: claim: missing required field
|
||||
```
|
||||
|
||||
A missing field is reported on line 1, because there is no line that carries it. A malformed front-matter block is reported as `<front-matter>: invalid YAML: …` and stops validation of that document only.
|
||||
|
||||
The final line is either `FAIL: N violation(s) in M document(s)` or `OK: M document(s) validated, 0 violations`.
|
||||
|
||||
---
|
||||
|
||||
## 8. Front-matter dialect the parser accepts
|
||||
|
||||
The validator contains its own small YAML-subset parser so the repository needs no third-party dependency (`AGENTS.md` §4: the stack is frozen, and the `.27` build host has no Go toolchain yet). It accepts:
|
||||
|
||||
* block mappings, block sequences, and flow collections: `tech: [a, b]`, `metrics: [{label: "endpoints", value: 8}]`
|
||||
* single- and double-quoted scalars, plain scalars
|
||||
* literal (`|`) and folded (`>`) block scalars, including `|-` / `>-`
|
||||
* `#` comments outside quoted strings
|
||||
* blank lines
|
||||
|
||||
It does **not** accept: anchors/aliases (`&`, `*`), explicit tags (`!!str`), multi-document files, complex keys, tab indentation. Use two or four spaces, consistently within a block.
|
||||
|
||||
Two consequences worth knowing:
|
||||
|
||||
* A quoted string must be closed. `claim: "text # not a comment` is an error, not a comment.
|
||||
* `#` after a space ends a plain scalar, as in YAML. Quote values that contain ` #`.
|
||||
|
||||
---
|
||||
|
||||
## 9. What the validator does not check
|
||||
|
||||
Enforced elsewhere in Phase 1, and reviewed by Sam:
|
||||
|
||||
* that a `metric` value appears in the evidence notes (no invented numbers)
|
||||
* that `confidence` matches reality — the validator cannot know; it only forces an honest label to be present
|
||||
* the redaction pass (no family names, address, phone, credentials)
|
||||
* that every L1 claim has a metric and at least one L3 artefact
|
||||
* corporate framing of headings
|
||||
|
||||
---
|
||||
|
||||
## 10. Implementation and tests
|
||||
|
||||
| Path | Purpose |
|
||||
|---|---|
|
||||
| `tools/validate_content.py` | The validator. One file, Python 3 standard library only, no framework. |
|
||||
| `Makefile` | `validate` and `test` targets. |
|
||||
| `test/fixtures/<case>/content/` | Fixture trees. `ok-*` cases must pass, `fail-*` cases must fail. |
|
||||
| `test/fixtures/<case>/expect.txt` | Substrings that `fail-*` output must contain, one per line. |
|
||||
|
||||
Fixture cases cover: missing field, `area` enum, `level` enum, `confidence` enum, `last_verified` format and calendar validity, empty `metrics[].label`, artefact `type` enum, artefact missing `url`, dangling artefact, external artefact URL, duplicate `id`, `id`/filename mismatch, unknown top-level field, unknown nested artefact field, missing front-matter, malformed YAML. `ok-empty` proves an empty `content/` exits 0; `ok-block-scalars` proves the block-scalar and block-sequence dialects.
|
||||
|
||||
Fixtures live under `test/`, never under `content/`.
|
||||
80
docs/image-brief.md
Normal file
80
docs/image-brief.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# Image brief
|
||||
|
||||
Every image on this site gets the same treatment. Consistency is what makes a set look
|
||||
designed; six images treated six ways look worse than six treated identically.
|
||||
|
||||
The palette is fixed by `internal/web/static/site.css`:
|
||||
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--bg` | `#0b0d10` | canvas and fade colour. Never pure black |
|
||||
| `--surface` | `#12161b` | screenshot backgrounds |
|
||||
| `--line` | `#242c35` | screenshot borders |
|
||||
| `--line-strong` | `#33404d` | diagram strokes |
|
||||
| `--text-dim` | `#b3bcc8` | diagram labels |
|
||||
| `--link` | `#8ecbff` | rim glow |
|
||||
| `--accent` | `#6ee7c6` | sparing emphasis only |
|
||||
|
||||
## Why white-background product shots fail here
|
||||
|
||||
`#FFFFFF` on a `#0b0d10` page is roughly twenty times the brightness of everything around
|
||||
it. The brightest thing in the frame becomes the backdrop rather than the object, and the
|
||||
eye lands on the backdrop first. One reads as an accident; six read as a scrapbook.
|
||||
|
||||
## Photographs
|
||||
|
||||
- **Canvas** 1600×1000 for hero images, 1200×1200 for detail. Landscape unless the object demands otherwise.
|
||||
- **Background** `#0b0d10`, the page colour. Not black — pure black always looks like a void.
|
||||
- **Subject** cut out, placed right of centre, occupying 55–70% of the canvas height. Off-centre because the fade comes from the left.
|
||||
- **Fade** a `#0b0d10` gradient, 100% opacity at the left edge to 0% at about 55% across. The object emerges from the page and dissolves to the right. This is the move that stops an image looking pasted on.
|
||||
- **Rim glow** `#8ecbff` at 25–40% opacity, 8–14px blur, derived from the cut-out's own alpha channel so it follows real edges. Enough to lift the object off the background. Not enough to look like a gaming mouse.
|
||||
- **Shadow lift** 5–8% on the subject itself. Cut-outs on dark backgrounds lose their dark detail and read as flat stickers otherwise.
|
||||
- **Format** WebP, quality 82, plus a JPEG fallback. Target under 120 KB for a hero.
|
||||
|
||||
## Screenshots
|
||||
|
||||
Never cut out. Frame them like the site's own panels:
|
||||
|
||||
- corner radius `10px` (`--radius`)
|
||||
- 1px border in `--line`
|
||||
- edges darkened 8%
|
||||
- soft drop shadow
|
||||
- crop out browser chrome where possible
|
||||
|
||||
## Diagrams
|
||||
|
||||
Stay SVG and stay in the palette: `--line-strong` strokes, `--text-dim` labels, `--accent`
|
||||
and `--link` for emphasis. No white fills, no default Mermaid blues.
|
||||
|
||||
## Logos
|
||||
|
||||
One colour, inheriting `currentColor` from the surrounding text. Forty-six client logos in
|
||||
full colour would shriek. If one deserves emphasis, give it `--accent`; the rest stay muted.
|
||||
|
||||
## Prohibited
|
||||
|
||||
White boxes · stock photography · lens flares · 3D renders of objects not owned ·
|
||||
glow strong enough to be the point · any image containing a family member, an address,
|
||||
a credential, or a screen showing personal data.
|
||||
|
||||
## Capture guidance
|
||||
|
||||
Treatment rescues a product shot; it cannot rescue a photograph of a white wall in daylight.
|
||||
|
||||
- dark surface, one light source from one side
|
||||
- shoot slightly above the object
|
||||
- keep the object off-centre in frame
|
||||
- no overhead lighting, no visible background clutter
|
||||
- a phone is fine
|
||||
|
||||
Rack and cabinet shots are the exception: keep them whole and wide, framed like
|
||||
screenshots rather than cut out.
|
||||
|
||||
## Output contract
|
||||
|
||||
Every image lands in `content/assets/images/`, named lowercase and hyphenated with no
|
||||
spaces or dates. Every image used on a page carries a caption naming the object; a product
|
||||
photograph additionally states that it is a product image and where it came from. The
|
||||
original, if it is Sam's own photograph or a sourced product image, is also filed in the
|
||||
repository of the project it belongs to, under `docs/images/`, with provenance recorded in
|
||||
that folder's `README.md`.
|
||||
@@ -30,7 +30,7 @@ deploy.sh
|
||||
├─ copy to releases/<timestamp>/
|
||||
├─ flip `current` symlink (atomic)
|
||||
├─ docker compose up -d
|
||||
├─ curl http://127.0.0.1:8096/healthz → must be ready
|
||||
├─ curl http://192.168.20.13:8096/healthz → must be ready
|
||||
└─ on failure: flip back to `previous`, exit non-zero
|
||||
```
|
||||
|
||||
@@ -40,7 +40,12 @@ deploy.sh
|
||||
|
||||
### 3.2 Container
|
||||
- `mem_limit` = `memswap_limit` = `120m`.
|
||||
- `ports: ["127.0.0.1:8096:8096"]` — never `0.0.0.0`.
|
||||
- `ports: ["192.168.20.13:8096:8096"]` — **never** `0.0.0.0`.
|
||||
Caddy runs on `.35`, so the port has to be reachable from the LAN; a
|
||||
`127.0.0.1` publish answers only on `.13` and the public URL returns 502.
|
||||
LAN-only publish, Caddy on `.35` still the sole public listener. Decision:
|
||||
Sam, 2026-10-26, interaction `54aeb1a2`. The health gate and `assert_shape()`
|
||||
in `deploy.sh` follow the same address (`RESUME_PUBLISH_IP`).
|
||||
- `logging` capped (`max-size: 10m`, `max-file: 3`).
|
||||
- `restart: unless-stopped`.
|
||||
|
||||
@@ -75,7 +80,7 @@ Repo stays private through this phase. Sam decides on public visibility after th
|
||||
|
||||
- [ ] `deploy.sh` and `deploy.sh --rollback` both work; a deliberate bad build proves rollback.
|
||||
- [ ] `https://resume.lab.audasmedia.com.au` serves the site.
|
||||
- [ ] Container bound to `127.0.0.1` only; confirmed with `ss -ltn`.
|
||||
- [ ] Container published on `192.168.20.13:8096` only, reachable from `.35`; confirmed with `ss -ltn`.
|
||||
- [ ] `memswap_limit == mem_limit`; confirmed with `docker inspect`.
|
||||
- [ ] Security headers present; CSP does not break the page.
|
||||
- [ ] A1 still passes over the public URL with the AI layer stopped.
|
||||
|
||||
@@ -1,264 +0,0 @@
|
||||
# Phase 3 evidence — public deployment
|
||||
|
||||
**Status: mechanism complete and rehearsed; bind decided by Sam; public URL deferred.**
|
||||
The deploy machinery exists, is rehearsed against the real release root and the
|
||||
real port on `.13`, and leaves nothing running. Sam answered the bind question on
|
||||
2026-10-26 (**dual bind**, §5), and that answer is now in the committed compose
|
||||
file and proven reachable from `.35`. The site itself is still not deployed
|
||||
because Phase 2 has not produced a binary yet.
|
||||
|
||||
Recorded 2026-10-26 on `.13` (`nixos-desktop`), runs `630e2ed9` and `a61a5b22`.
|
||||
|
||||
---
|
||||
|
||||
## Definition of Done
|
||||
|
||||
| # | Item | Verdict | Evidence |
|
||||
|---|---|---|---|
|
||||
| 1 | `deploy.sh` and `deploy.sh --rollback` work; a deliberate bad build proves rollback | **met** | §1, `deploy/rehearse.sh` 24/24 |
|
||||
| 2 | `https://resume.lab.audasmedia.com.au` serves the site | **deferred** | Phase 2 has not produced a binary; Caddy block not yet applied (§4, §5) |
|
||||
| 3 | Container bound to `127.0.0.1` only, `ss -ltn` | **met as amended by Sam**: loopback **and** `192.168.20.13`, no wildcard — see §5 | §2 |
|
||||
| 4 | `memswap_limit == mem_limit`, `docker inspect` | **met** | §2 |
|
||||
| 5 | Security headers present, CSP does not break the page | **deferred** | headers belong to the `.35` Caddy block (§4) |
|
||||
| 6 | A1 passes over the public URL with the AI layer stopped | **deferred** | needs a live URL |
|
||||
| 7 | Sam applied the Caddy block and confirmed other lab hostnames | **deferred — Sam's action** | block and verification steps in §4 |
|
||||
| 8 | Rollback rehearsed once and documented | **met** | §1, `deploy/rehearse.sh` |
|
||||
|
||||
Nothing in the deferred column is asserted. Each depends on the Phase 2 binary
|
||||
(items 2, 5, 6) or on Sam (items 2, 5, 7).
|
||||
|
||||
---
|
||||
|
||||
## 1. What was rehearsed, and the result
|
||||
|
||||
`deploy/rehearse.sh` builds throwaway services and drives the real script. Full
|
||||
transcript of the run after the dual-bind change (24 assertions):
|
||||
|
||||
```
|
||||
== 0. preconditions
|
||||
PASS port 8096 free
|
||||
PASS three throwaway sources built in /tmp/.../rehearsal4
|
||||
|
||||
== 1. good release deploys and health-gates
|
||||
PASS deploy.sh exit 0
|
||||
PASS /healthz 200 on 127.0.0.1:8096
|
||||
PASS serving release 20261026T064248Z
|
||||
PASS container publishes on 127.0.0.1:8096 (loopback, used by the deploy.sh health gate)
|
||||
PASS container publishes on 192.168.20.13:8096 (the address Caddy on .35 proxies to)
|
||||
PASS no wildcard bind on 8096 — only: 127.0.0.1:8096 192.168.20.13:8096
|
||||
PASS /healthz 200 over the LAN address http://192.168.20.13:8096
|
||||
PASS memswap_limit == mem_limit (125829120 bytes)
|
||||
|
||||
== 2. build failure fails fast, current does not move
|
||||
PASS deploy.sh exit 1 (non-zero, as required)
|
||||
PASS current unchanged (/home/sam/deployment/resume/releases/20261026T064248Z)
|
||||
PASS previous release still serving
|
||||
|
||||
== 3. unhealthy release is rolled back by the health gate
|
||||
PASS deploy.sh exit 1 (non-zero, as required)
|
||||
PASS current restored to /home/sam/deployment/resume/releases/20261026T064248Z
|
||||
PASS rolled-back release serving again
|
||||
PASS log records the health-gate failure
|
||||
|
||||
== 4. deploy.sh --rollback restores the previous release in one command
|
||||
PASS second good release deployed
|
||||
PASS rollback exit 0
|
||||
PASS current flipped back to /home/sam/deployment/resume/releases/20261026T064248Z
|
||||
PASS rolled-back release healthy
|
||||
PASS serving 20261026T064248Z again (was 20261026T064307Z)
|
||||
|
||||
== 5. N3: docker compose down && up restores the service
|
||||
PASS service down after compose down
|
||||
PASS service restored after compose up
|
||||
|
||||
== verdict
|
||||
passed 24, failed 0
|
||||
```
|
||||
|
||||
Reachability from the Caddy host itself (`.35`), run during that rehearsal:
|
||||
|
||||
```
|
||||
$ ssh sam@192.168.20.35 'curl -sS -m3 -w "%{http_code}" http://192.168.20.13:8096/healthz'
|
||||
200
|
||||
```
|
||||
|
||||
That is the exact hop §3.3's `reverse_proxy` will take, proven before the Caddy
|
||||
block is applied.
|
||||
|
||||
The health-gate rollback in the deploy log, verbatim:
|
||||
|
||||
```
|
||||
06:28:46Z deploy: previous -> /home/sam/deployment/resume/releases/20261026T062828Z
|
||||
06:28:46Z deploy: current -> /home/sam/deployment/resume/releases/20261026T062842Z
|
||||
06:29:02Z deploy: HEALTH GATE FAILED for 20261026T062842Z
|
||||
06:29:02Z rollback: 20261026T062842Z failed -> restoring .../releases/20261026T062828Z
|
||||
06:29:04Z health: http://127.0.0.1:8096/healthz -> 200
|
||||
06:29:04Z rollback: previous release healthy (.../releases/20261026T062828Z)
|
||||
```
|
||||
|
||||
The deliberate bad build, verbatim:
|
||||
|
||||
```
|
||||
$ ./deploy/deploy.sh --source .../app-broken
|
||||
cmd/server/main.go:11:12: syntax error: unexpected {, expected )
|
||||
exit=1
|
||||
current before: .../releases/20261026T062828Z
|
||||
current after: .../releases/20261026T062828Z # unchanged
|
||||
health after broken build: 200 # old release still serving
|
||||
```
|
||||
|
||||
Re-run it any time with `./deploy/rehearse.sh` (exit 0 = every assertion held).
|
||||
|
||||
---
|
||||
|
||||
## 2. Container limits and bind scope
|
||||
|
||||
```
|
||||
$ ss -ltn | grep 8096
|
||||
LISTEN 0 4096 127.0.0.1:8096 0.0.0.0:*
|
||||
LISTEN 0 4096 192.168.20.13:8096 0.0.0.0:*
|
||||
|
||||
$ docker inspect resume-app
|
||||
mem_limit=125829120 memswap_limit=125829120
|
||||
port_bindings={"8096/tcp":[{"HostIp":"127.0.0.1","HostPort":"8096"},{"HostIp":"192.168.20.13","HostPort":"8096"}]}
|
||||
restart=unless-stopped
|
||||
log_config={"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}
|
||||
image=resume/app:20261026T064248Z
|
||||
|
||||
$ curl -sS http://127.0.0.1:8096/healthz
|
||||
{"status":"ok","static":true,"ai":"disabled","version":"20261026T064248Z",...}
|
||||
$ curl -sS http://192.168.20.13:8096/healthz
|
||||
{"status":"ok","static":true,"ai":"disabled","version":"20261026T064248Z",...}
|
||||
```
|
||||
|
||||
Two explicit addresses, no wildcard listener: `0.0.0.0` never appears, so
|
||||
nothing but Caddy on `.35` can be reached from outside the LAN. The reasoning is
|
||||
in §5.
|
||||
|
||||
Measured memory (idle Go static server, placeholder build):
|
||||
|
||||
```
|
||||
$ docker stats --no-stream --format 'rss={{.MemUsage}} pct={{.MemPerc}}' resume-app
|
||||
rss=1.73MiB / 120MiB pct=1.44%
|
||||
```
|
||||
|
||||
Host pressure at the time of the rehearsal — this is why the cap is 120m:
|
||||
|
||||
```
|
||||
$ free -m
|
||||
total used free available
|
||||
Mem: 15918 13943 223 1975
|
||||
```
|
||||
|
||||
No residue was left behind: after the rehearsal there is no `resume-app`
|
||||
container, no `resume/app:*` image, nothing listening on 8096, and
|
||||
`/home/sam/deployment/resume/` does not exist. `deploy.sh` creates it on the
|
||||
first real deploy. (The pre-existing `ai-resume-*` containers from
|
||||
`/home/sam/deployment/ai-resume/` were not touched.)
|
||||
|
||||
---
|
||||
|
||||
## 3. Facts established on the host
|
||||
|
||||
| Fact | Value |
|
||||
|---|---|
|
||||
| Port 8096 | free (`ss -ltn`; 8090, 8091, 8092, 8095 are taken) |
|
||||
| Release root | `/home/sam/deployment/resume/` — created by `deploy.sh` |
|
||||
| Go toolchain | not on `PATH`; `/nix/store/…-go-1.26.5/bin/go`, auto-detected by the script |
|
||||
| Container runtime | Docker 29.6.2, Compose 5.3.1 |
|
||||
| `.13` runs its own Caddy | `caddy run --config /etc/static/caddy/caddy_config`, listening `*:8000`, 8 legacy site blocks, all `reverse_proxy localhost:<port>` |
|
||||
| Services added since then | bind `0.0.0.0` and are proxied by `.35` directly on their own LAN port (hub 8788, compass 8792, email-agent 8790, webrestrict 8791) |
|
||||
|
||||
---
|
||||
|
||||
## 4. Caddy block for Sam to apply (`.35`, additive, `caddy reload` only)
|
||||
|
||||
```caddy
|
||||
resume.lab.audasmedia.com.au {
|
||||
encode zstd gzip
|
||||
reverse_proxy 192.168.20.13:8096
|
||||
header {
|
||||
Strict-Transport-Security "max-age=31536000; includeSubDomains"
|
||||
X-Content-Type-Options "nosniff"
|
||||
Referrer-Policy "strict-origin-when-cross-origin"
|
||||
X-Frame-Options "SAMEORIGIN"
|
||||
Content-Security-Policy "default-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'"
|
||||
}
|
||||
@static path *.css *.js *.svg *.png *.jpg *.webp *.woff2
|
||||
header @static Cache-Control "public, max-age=31536000, immutable"
|
||||
}
|
||||
```
|
||||
|
||||
Apply, in this order:
|
||||
|
||||
```bash
|
||||
# 1. add the block above to the Caddyfile on .35, then validate:
|
||||
caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
|
||||
# 2. reload — not restart, so other sites are not interrupted:
|
||||
caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile
|
||||
# 3. confirm the other hostnames first, from anywhere on the LAN:
|
||||
for h in gitea.lab.audasmedia.com.au immich.lab.audasmedia.com.au \
|
||||
nextcloud.lab.audasmedia.com.au penpot.lab.audasmedia.com.au \
|
||||
wiki.lab.audasmedia.com.au chat.lab.audasmedia.com.au; do
|
||||
printf '%-45s %s\n' "$h" "$(curl -s -o /dev/null -w '%{http_code}' "https://$h/")"
|
||||
done
|
||||
# 4. then the new one (404/502 is expected until Phase 2's binary is deployed):
|
||||
curl -sSI https://resume.lab.audasmedia.com.au/ | sed -n '1,12p'
|
||||
```
|
||||
|
||||
Header check after that (`item 5` of the DoD): the response above must include
|
||||
`Strict-Transport-Security`, `X-Content-Type-Options`, `Referrer-Policy`,
|
||||
`X-Frame-Options` and `Content-Security-Policy`. If the page renders but the CSP
|
||||
breaks it, the CSP is wrong — do not weaken it to `unsafe-inline` for scripts.
|
||||
|
||||
---
|
||||
|
||||
## 5. Bind address — decided by Sam, 2026-10-26 (dual bind)
|
||||
|
||||
The phase document said two things that could not both be true:
|
||||
|
||||
- §3.2 and DoD item 3: the container publishes on **`127.0.0.1:8096` only**.
|
||||
- §3.3: Caddy **on `.35`** proxies to **`192.168.20.13:8096`**.
|
||||
|
||||
A loopback-only listener on `.13` is not reachable from `.35`. Measured at the
|
||||
time of the question:
|
||||
|
||||
```
|
||||
$ curl -sS -m3 http://192.168.20.13:8096/healthz
|
||||
curl: (7) Failed to connect to 192.168.20.13:8096
|
||||
```
|
||||
|
||||
`.13`'s own Caddy (`*:8000`, `/etc/static/caddy/caddy_config`) only serves eight
|
||||
legacy hostnames; every service added since (hub, compass, email-agent,
|
||||
webrestrict) is proxied by `.35` straight to its LAN port. §3.3's block assumes
|
||||
that pattern.
|
||||
|
||||
**Answer (AUD-133, 2026-10-26):** "A - dual bind: 127.0.0.1:8096 and
|
||||
192.168.20.13:8096". That is now the committed behaviour:
|
||||
|
||||
| Change | Where |
|
||||
|---|---|
|
||||
| both addresses in `ports:` | `deploy/docker-compose.yml` (`${APP_LAN_IP:-192.168.20.13}`) |
|
||||
| LAN address carried into the release `.env`; `--lan-ip ADDR` / `APP_LAN_IP` override | `deploy/deploy.sh` |
|
||||
| health gate now requires `/healthz` 200 on **loopback and** the LAN address | `deploy/deploy.sh` `check_health()` |
|
||||
| assertions for both binds, no wildcard, and LAN reachability | `deploy/rehearse.sh` |
|
||||
|
||||
Consequence for DoD item 3, stated plainly: the container is **not** bound to
|
||||
`127.0.0.1` only. It is bound to `127.0.0.1` and `192.168.20.13`, never
|
||||
`0.0.0.0`, so it is reachable on the `.13` LAN exactly like hub/compass today and
|
||||
not from anywhere else. That is a written change to §3.2 / N1 made by Sam, not a
|
||||
judgement call by the agent; §2 and §4 of this document carry the output.
|
||||
|
||||
No NixOS change and no `.13` Caddy change is needed under this option. §3.3's
|
||||
block is applied verbatim.
|
||||
|
||||
---
|
||||
|
||||
## 6. Deferred, with owners
|
||||
|
||||
| Item | Owner | Unblocked by |
|
||||
|---|---|---|
|
||||
| Deploy the real site (DoD 2) | this issue, woken by [AUD-132](/AUD/issues/AUD-132) | Phase 2 producing `bin/server` + `dist/` from `make build` |
|
||||
| Header + CSP proof (DoD 5) | this issue, after the Caddy block is applied | Sam applies §4 |
|
||||
| A1 over the public URL (DoD 6) | this issue, after 2 and 5 | — |
|
||||
| Caddy block applied and other hostnames confirmed (DoD 7) | **Sam** | §4 |
|
||||
@@ -1,81 +1,157 @@
|
||||
# Phase 4 — Corpus and retrieval
|
||||
# Phase 4 — Ingestion from sources
|
||||
|
||||
**Goal:** `content/` becomes a retrievable corpus with filters that work before the vector search, and a freshness audit that cannot silently rot.
|
||||
**Goal:** a fireable tool that reads the estate — Gitea, the notes vault, the knowledge graph — and keeps a searchable pool current. The pool is not built from `content/` alone; `content/` is the top tier of it.
|
||||
|
||||
**Depends on:** Phase 1 (content), Phase 2 (FTS5 index exists).
|
||||
**Depends on:** Phase 1 (content and schema), Phase 2 (FTS5 index).
|
||||
|
||||
**Why this changed.** An earlier version of this document said the corpus would be built from `content/` only. That was wrong. A hand-curated set cannot answer "how did the photo pipeline deduplicate" or "what did the garden controller do before the rewrite". The estate can. Curated claims answer questions about Sam; the estate answers questions about the work.
|
||||
|
||||
---
|
||||
|
||||
## Work
|
||||
## 1. Sources
|
||||
|
||||
### 4.1 Pipeline
|
||||
| Source | Reached by | What to take |
|
||||
|---|---|---|
|
||||
| **Gitea**, all repositories | API over HTTPS | `README.md`, file tree (paths only), selected source files, recent commit messages, language statistics, repo description and topics |
|
||||
| **`sam/obsidian-vault`** | Same Gitea API — the vault is a repo | Markdown body, front-matter title and tags, folder path |
|
||||
| **Graphify output** | `graph.json` committed to the vault repo | Nodes (label, source_file, community), edges (relation, confidence) |
|
||||
| **`content/`** | Local repo | The curated claims. **Tier 1** — outranks everything above on conflict |
|
||||
|
||||
```
|
||||
content/ ──► validate ──► chunk ──► enrich ──► embed ──► upsert Qdrant
|
||||
│
|
||||
SQLite FTS5 (from Phase 2) ┘
|
||||
Gitea is the only network dependency. The vault and the graph arrive through it, so there is no cross-machine copy and no SSH from `.13` to `.27`.
|
||||
|
||||
### What not to read
|
||||
|
||||
Media never becomes text. Images, video and audio are recorded as `image` node references, tagged with their path, and nothing more.
|
||||
|
||||
---
|
||||
|
||||
## 2. Platform guides
|
||||
|
||||
One file per platform in `platforms/<name>.yml`. They are configuration **and** public copy — the site shows them so a visitor can see what the assistant reads.
|
||||
|
||||
```yaml
|
||||
name: gitea
|
||||
enabled: true
|
||||
what: "Self-hosted Git. Source for every current project."
|
||||
fits: [ai, platform, edge]
|
||||
base: https://gitea.lab.audasmedia.com.au
|
||||
auth: GITEA_TOKEN
|
||||
include:
|
||||
repos: all
|
||||
exclude:
|
||||
paths: ["**/node_modules/**", "**/.venv/**", "**/.git/**", "**/*.env*",
|
||||
"**/.silverbullet.auth.json", "**/*.pem", "**/*.key"]
|
||||
repos: []
|
||||
read: [readme, tree, selected_source, commits, languages]
|
||||
limits: { max_documents: 4000, max_file_kb: 200 }
|
||||
```
|
||||
|
||||
- **Chunking:** 400–800 tokens, ~12% overlap, **code-aware** — never split inside a fenced block, table or front-matter.
|
||||
- **Deterministic IDs:** `sha256(source_path + chunk_index + content_hash)`. Re-running must update, not duplicate.
|
||||
- **Incremental:** hash the source set; only changed documents are re-chunked and re-embedded.
|
||||
- **One command** rebuilds from scratch. Deleting the Qdrant volume is always recoverable.
|
||||
```yaml
|
||||
name: obsidian
|
||||
enabled: true
|
||||
what: "Personal knowledge base. High-level notes on every system, in Sam's words."
|
||||
fits: [about, ai, platform, edge]
|
||||
base: sam/obsidian-vault
|
||||
include:
|
||||
paths: ["200 projects/**", "300 areas/**", "400 Personal Family/**",
|
||||
"100 Table of Contents/**", "010 inbox/**"]
|
||||
exclude:
|
||||
paths: [".obsidian/**", ".trash/**", ".venv/**", "templates/**",
|
||||
"**/*.excalidraw", "**/*.png", "**/*.jpg"]
|
||||
limits: { max_documents: 4000, max_file_kb: 200 }
|
||||
```
|
||||
|
||||
### 4.2 Payload schema (the retrieval filter)
|
||||
```yaml
|
||||
name: graphify
|
||||
enabled: true
|
||||
what: "Knowledge graph over the vault. Answers 'what connects X to Y' rather than 'what mentions X'."
|
||||
fits: [ai]
|
||||
base: sam/obsidian-vault
|
||||
read: [graph_json]
|
||||
```
|
||||
|
||||
`fits` drives the payload `area`, which is what lets the query filter before it searches.
|
||||
|
||||
---
|
||||
|
||||
## 3. Metadata on every chunk
|
||||
|
||||
```json
|
||||
{
|
||||
"area": "ai|platform|edge|products|history|about",
|
||||
"level": 1,
|
||||
"source_type": "doc|code|adr|metric",
|
||||
"confidence": "delivered|running|in-progress|abandoned|historic",
|
||||
"period": "2023–",
|
||||
"artefacts": ["..."],
|
||||
"last_verified": "2026-10-20",
|
||||
"platform": "gitea | obsidian | graphify | curated",
|
||||
"tier": 1,
|
||||
"source_type": "doc | code | commit | graph_node | claim",
|
||||
"area": "ai | platform | edge | products | history | about",
|
||||
"origin_url": "https://gitea.../sam/photo-pipeline/src/x.py",
|
||||
"path": "src/x.py",
|
||||
"title": "photo-pipeline / dedup",
|
||||
"confidence": "delivered | running | in-progress | abandoned | historic",
|
||||
"last_modified": "2026-10-14T05:00:00Z",
|
||||
"content_hash": "sha256:...",
|
||||
"public": true,
|
||||
"text": "..."
|
||||
}
|
||||
```
|
||||
|
||||
`area` and `confidence` are the two that matter: they let the query path filter *before* searching, and they let the answer qualify unfinished work.
|
||||
`tier: 1` is curated `content/`. Everything harvested is `tier: 2`. The query layer uses this to prefer the curated claim when the two disagree.
|
||||
|
||||
### 4.3 Embeddings
|
||||
- Batch, low volume, incremental.
|
||||
- **Record the embedding model and dimension per collection.** A provider change is a governed migration, not a silent corruption.
|
||||
- Provider decision pending Sam — see `reference/stack.md`. Build provider-agnostic behind one interface.
|
||||
|
||||
### 4.4 Scheduling
|
||||
- Prefect deployment on the existing Prefect server; **worker only**, do not deploy a second server.
|
||||
- Trigger: git push to `content/`, plus a nightly audit.
|
||||
- Nightly audit reports documents past their `last_verified` horizon and alerts via the existing Apprise service.
|
||||
`origin_url` is mandatory for harvested chunks. A chunk with no origin is not admitted — an answer that cannot be traced is worse than no answer.
|
||||
|
||||
---
|
||||
|
||||
## Definition of Done
|
||||
## 4. Pipeline
|
||||
|
||||
- [ ] One command rebuilds the corpus from an empty volume.
|
||||
- [ ] Running it twice produces no duplicate vectors (count is stable).
|
||||
- [ ] No chunk boundary lands inside a code fence, table or front-matter block.
|
||||
- [ ] Every chunk carries all payload fields; a missing field fails the run rather than defaulting.
|
||||
- [ ] Payload filtering demonstrably narrows results — demonstrate the same query with and without an `area` filter.
|
||||
- [ ] Incremental mode re-embeds only changed documents; prove it with the provider call count.
|
||||
- [ ] Freshness audit runs and reports stale documents.
|
||||
- [ ] Qdrant memory measured at 500 / 5,000 / 20,000 chunks and recorded in `reference/stack.md`, within its cap.
|
||||
```
|
||||
guides → harvest (per platform) → normalise to text → chunk → enrich → embed → upsert
|
||||
│
|
||||
└─ reject: excluded path, no origin, binary, oversize
|
||||
```
|
||||
|
||||
- **Chunking** is code-aware: 400–800 tokens, ~12% overlap, never split inside a fenced block, table, front-matter block or function. Use the language grammar where available; fall back to paragraph splitting.
|
||||
- **Deterministic IDs:** `sha256(origin_url + chunk_index + content_hash)`. Re-running updates, never duplicates.
|
||||
- **Incremental:** compare `content_hash` against what the pool already holds; embed only what changed. A full rebuild must always be possible and is one command.
|
||||
- **Graph** is not chunked. Nodes and edges go into their own collection, joined to note chunks by `path`, so a graph answer can name the note it came from.
|
||||
|
||||
---
|
||||
|
||||
## Out of scope
|
||||
## 5. Scheduling
|
||||
|
||||
- Answering questions. Phase 4 ends at "retrieval returns the right chunks".
|
||||
- Query graphs, prompts, guardrails — Phase 5 and 6.
|
||||
- Ingesting anything outside `content/`. Four sources maximum, and this phase uses one: the repo.
|
||||
- On demand: one command.
|
||||
- Scheduled: a Prefect deployment on the existing server. Worker only; do not deploy a second Prefect server.
|
||||
- Reports what changed each run: documents added, updated, removed, and any excluded-path rejections.
|
||||
- A source that has not changed in its expected window raises an alert through the existing Apprise service. Silent staleness is the failure mode that matters.
|
||||
|
||||
---
|
||||
|
||||
## Risks
|
||||
## 6. Definition of Done
|
||||
|
||||
- [ ] One command rebuilds the pool from empty.
|
||||
- [ ] A second run produces no duplicate vectors.
|
||||
- [ ] A probe document placed in an excluded path never appears in the pool, and the run reports the rejection.
|
||||
- [ ] A document with no resolvable origin is refused, not defaulted.
|
||||
- [ ] Every chunk carries all fields in section 3.
|
||||
- [ ] Incremental mode re-embeds only changed documents, proven by embedding call count.
|
||||
- [ ] Graph nodes and edges are queryable and each resolves to a source note.
|
||||
- [ ] The scheduled run reports deltas and alerts on staleness.
|
||||
- [ ] Qdrant stays inside its `mem_limit`; footprint recorded at 1k, 10k and 40k chunks.
|
||||
- [ ] No credential, `.env`, key file or family-personal binary is present in the pool — verified by search, not by assertion.
|
||||
|
||||
---
|
||||
|
||||
## 7. Out of scope
|
||||
|
||||
- Answering questions. This phase ends at "retrieval returns the right chunks with their origins".
|
||||
- The query graph, prompts and guardrails — Phase 5.
|
||||
- Building the Graphify graph. It already exists and is rebuilt on `.27` with its own skill. This phase consumes its output.
|
||||
|
||||
---
|
||||
|
||||
## 8. Risks
|
||||
|
||||
| Risk | Mitigation |
|
||||
|---|---|
|
||||
| Corpus grows beyond RAM | Incremental by default; measure at three corpus sizes; cap Qdrant with `mem_limit` |
|
||||
| Duplicate vectors on re-run | Deterministic IDs are mandatory, not optional |
|
||||
| Silent staleness | Nightly audit + Apprise alert; a stale index is otherwise an invisible quality failure |
|
||||
| Provider rate limits during bulk embed | Batch with retry/backoff; ingest is low-volume and incremental |
|
||||
| Volume — 50 repos plus 280 notes exceeds the memory cap | Cap per platform, skip large files, measure at three corpus sizes |
|
||||
| Scraped text contradicts a curated claim | `tier` field; the query prefers tier 1 and says so |
|
||||
| A secret is harvested | Exclusion list plus a post-run scan of the pool for credential patterns, failing the run if found |
|
||||
| Staleness is invisible | Scheduled run plus alert |
|
||||
| The estate is unavailable when a recruiter asks | The pool is local; only ingestion needs Gitea. Serving never does |
|
||||
|
||||
304
docs/phase-4-notes.md
Normal file
304
docs/phase-4-notes.md
Normal file
@@ -0,0 +1,304 @@
|
||||
# Phase 4 notes — corpus and retrieval
|
||||
|
||||
Operator manual for the corpus pipeline, and the evidence for the Phase 4
|
||||
Definition of Done in `docs/phase-4-corpus.md`. Every number below was measured
|
||||
on 2026-10-26 against a live Qdrant 1.20.0 container with a 400 MiB cap.
|
||||
|
||||
---
|
||||
|
||||
## 1. What exists now
|
||||
|
||||
| Path | What it is |
|
||||
|---|---|
|
||||
| `corpus/` | The pipeline: one Python package, standard library only |
|
||||
| `corpus/cli.py` | `python3 -m corpus.cli {rebuild,sync,audit,verify,demo-filter,hybrid,measure,info}` |
|
||||
| `corpus/chunker.py` | Code-aware chunking (fenced block, table and front-matter are atomic) |
|
||||
| `corpus/embeddings.py` | The only place a vector is made; two providers behind one interface |
|
||||
| `corpus/qdrant.py` | Qdrant REST client (no `qdrant-client` dependency) |
|
||||
| `corpus/store.py` | SQLite: FTS5 over chunks, the incremental manifest, the collection contract |
|
||||
| `corpus/pipeline.py` | validate → chunk → enrich → embed → upsert, in that order |
|
||||
| `corpus/freshness.py` | Nightly audit: stale documents, index drift, contract drift |
|
||||
| `corpus/measure.py` | Memory measurement at 500 / 5,000 / 20,000 chunks |
|
||||
| `corpus/tests/` | 76 unit tests, no container, no network |
|
||||
| `flows/corpus.py` | Prefect flows: `corpus-sync`, `corpus-rebuild`, `corpus-audit` |
|
||||
| `deploy/qdrant.yml` | Qdrant container: `mem_limit` = `memswap_limit` = 400 m, loopback only |
|
||||
|
||||
Two indexes are written from one pass over `content/`, so they cannot drift:
|
||||
|
||||
* **vectors** in Qdrant (`resume_corpus`), one point per chunk;
|
||||
* **keywords** in SQLite (`data/corpus/corpus.db`), one FTS5 row per chunk.
|
||||
|
||||
Retrieval uses both (`corpus.cli hybrid`, reciprocal rank fusion). Vectors miss
|
||||
exact tokens such as repo names, `LoRaWAN`, `MQTT` and dates; FTS5 alone misses
|
||||
paraphrase. Decision D10 requires the pair.
|
||||
|
||||
---
|
||||
|
||||
## 2. Commands
|
||||
|
||||
```sh
|
||||
make corpus # rebuild from content/ (drops the collection first)
|
||||
make corpus-sync # incremental: only changed documents are re-embedded
|
||||
make corpus-audit # freshness + drift audit; exit 1 on findings
|
||||
make corpus-verify # index vs content/ vs Qdrant, consistency report
|
||||
make corpus-demo # the same query with and without an area filter
|
||||
make corpus-test # 76 unit tests
|
||||
```
|
||||
|
||||
Direct form, when you need flags:
|
||||
|
||||
```sh
|
||||
python3 -m corpus.cli rebuild --qdrant-url http://127.0.0.1:6333 --dim 1024
|
||||
python3 -m corpus.cli audit --today 2027-01-20 --json --fail-on-findings
|
||||
python3 -m corpus.cli measure --sizes 500,5000,20000 --container ai-resume-qdrant
|
||||
```
|
||||
|
||||
Configuration is environment-first. Nothing is read from a file in this repo.
|
||||
|
||||
| Variable | Default | Meaning |
|
||||
|---|---|---|
|
||||
| `RESUME_QDRANT_URL` | `http://127.0.0.1:6333` | Qdrant REST endpoint |
|
||||
| `RESUME_COLLECTION` | `resume_corpus` | collection name |
|
||||
| `RESUME_EMBED_PROVIDER` | `local-hashing` | `local-hashing` or `omniroute` |
|
||||
| `RESUME_EMBED_MODEL` | provider default | recorded per collection |
|
||||
| `RESUME_EMBED_DIM` | provider default | recorded per collection |
|
||||
| `RESUME_CORPUS_DB` | `data/corpus/corpus.db` | FTS5 + manifest |
|
||||
| `RESUME_MAX_AGE_DAYS` | `180` | freshness horizon |
|
||||
| `RESUME_WARN_DAYS` | `30` | how early "due soon" starts |
|
||||
| `RESUME_APPRISE_URL` | unset | existing Apprise endpoint for audit alerts |
|
||||
| `OMNIROUTE_API_KEY` | unset | only for the `omniroute` provider; never in the repo |
|
||||
|
||||
---
|
||||
|
||||
## 3. Pipeline
|
||||
|
||||
```
|
||||
content/ ─► make validate ─► chunk ─► enrich ─► embed ─► upsert Qdrant
|
||||
│
|
||||
└─► SQLite FTS5 + manifest
|
||||
```
|
||||
|
||||
* **Validate first.** The pipeline runs the Phase 1 validator
|
||||
(`tools/validate_content.py`). A broken content tree stops the run before a
|
||||
single vector is written.
|
||||
* **Chunking.** Target 600 tokens, hard ceiling 800, ~12 % overlap. The body is
|
||||
parsed into atomic blocks (fenced code, table, heading, list item, quote,
|
||||
paragraph, rule) and chunks are packed from whole blocks, so a boundary
|
||||
cannot fall inside a fence, a table or front-matter. A list item or a table
|
||||
that exceeds 800 tokens becomes one oversize chunk and is flagged, never cut.
|
||||
Tokens are counted as words plus punctuation, which is a conservative
|
||||
under-estimate of BPE tokens.
|
||||
* **Payload before vectors.** Every chunk payload is built and validated for
|
||||
every changed document *before* one point is written. A missing or invalid
|
||||
field aborts the run with the document and chunk named.
|
||||
* **Deterministic ids.** `sha256(source_path + chunk_index + content_hash)`,
|
||||
rendered as a UUID because Qdrant accepts only integers or UUIDs. Re-running
|
||||
updates points; a document whose chunk count shrinks has its old points
|
||||
deleted by `source_path` filter, so orphans cannot accumulate.
|
||||
* **Incremental.** One hash comparison against the manifest per document. The
|
||||
document is re-embedded when its bytes changed, or when the embedding model,
|
||||
dimension or chunker version changed.
|
||||
* **Consistency check.** At the end of every run the point count in Qdrant must
|
||||
equal the chunk count in SQLite, or the run fails.
|
||||
|
||||
Corpus today: **40 documents → 51 chunks**, token range 100–707 (median 225),
|
||||
8 protected regions (fenced blocks and tables), 0 boundary violations.
|
||||
|
||||
---
|
||||
|
||||
## 4. Payload
|
||||
|
||||
`docs/phase-4-corpus.md` §4.2 fields, plus provenance so an answer can cite:
|
||||
|
||||
```json
|
||||
{
|
||||
"area": "edge", "level": 3, "source_type": "doc", "confidence": "running",
|
||||
"period": "2020–", "artefacts": [], "last_verified": "2026-10-26",
|
||||
"text": "# Artefact — Distributed audio …",
|
||||
"id": "014d792f-28d9-e0a0-610e-7ffc04eea3ad",
|
||||
"source_path": "content/edge/l3/edge-snapcast.md", "source_id": "edge-snapcast",
|
||||
"chunk_index": 0, "title": "…", "claim": "…", "url": "/edge/a/edge-snapcast/",
|
||||
"content_hash": "12fc4fa9…", "embedding_model": "local-hashing-v1",
|
||||
"embedding_dim": 1024, "chunker_version": "blocks-v1", "chunk_tokens": 191,
|
||||
"public": true, "tech": ["Snapcast", "Mopidy", "…"], "tags": ["edge", "audio"]
|
||||
}
|
||||
```
|
||||
|
||||
`area` and `confidence` are the two that matter. The query path filters on them
|
||||
*before* it searches, and an answer can qualify unfinished work instead of
|
||||
presenting it as delivered.
|
||||
|
||||
---
|
||||
|
||||
## 5. Freshness audit
|
||||
|
||||
```sh
|
||||
make corpus-audit # human table, exit 1 on findings
|
||||
python3 -m corpus.cli audit --json # machine-readable, for Prefect
|
||||
```
|
||||
|
||||
Findings, in order of seriousness:
|
||||
|
||||
| Finding | Meaning |
|
||||
|---|---|
|
||||
| `stale` | `last_verified` older than `RESUME_MAX_AGE_DAYS` (180). The claim may no longer be true |
|
||||
| `due_soon` | inside the warning window (30 days by default). A warning, not a failure |
|
||||
| `index_drift` | the source changed, disappeared or was never indexed since it was embedded |
|
||||
| `contract_drift` | the collection was built with a different model, dimension or chunker |
|
||||
|
||||
`ok` is false when there is a `stale`, `index_drift` or `contract_drift`
|
||||
finding. `due_soon` alone keeps the audit green.
|
||||
|
||||
`--alert` posts the report to `RESUME_APPRISE_URL` when the audit is not ok.
|
||||
The Prefect flow does this by default.
|
||||
|
||||
---
|
||||
|
||||
## 6. Scheduling (Prefect, existing server, worker only)
|
||||
|
||||
`flows/corpus.py` defines three flows. It imports Prefect when present and runs
|
||||
as plain Python when it is not, so the CLI never depends on the orchestrator.
|
||||
|
||||
On `.13`, which already runs the Prefect server — **do not start a second one**:
|
||||
|
||||
```sh
|
||||
# once, per deployment
|
||||
export RESUME_QDRANT_URL=http://127.0.0.1:6333
|
||||
export RESUME_EMBED_PROVIDER=omniroute RESUME_EMBED_MODEL=<approved model> RESUME_EMBED_DIM=<dim>
|
||||
prefect deploy flows/corpus.py:sync_corpus --name corpus-sync
|
||||
prefect deploy flows/corpus.py:freshness_audit --name corpus-audit --cron "0 4 * * *"
|
||||
```
|
||||
|
||||
Triggers required by `docs/phase-4-corpus.md` §4.4:
|
||||
|
||||
* **on push touching `content/`** — a deployment trigger on the repository, or
|
||||
`make corpus-sync` from the same post-receive hook that runs `deploy.sh`;
|
||||
* **nightly audit** — cron `0 4 * * *`, which alerts through Apprise.
|
||||
|
||||
This deployment is **not applied yet**: it needs the runtime host and the
|
||||
provider decision in §8. Both are Sam's. The flows themselves are ready.
|
||||
|
||||
---
|
||||
|
||||
## 7. Qdrant memory at three corpus sizes
|
||||
|
||||
Measured with `python3 -m corpus.cli measure`, Qdrant 1.20.0 in a container
|
||||
capped at 400 MiB (`mem_limit` = `memswap_limit`), 1024-dimensional vectors,
|
||||
real payloads from `content/`, throttled ingest in batches of 250.
|
||||
|
||||
| Chunks | Points | Settled RSS | Peak during ingest | Growth | Bytes/chunk | Ingest time |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 0 | 0 | 88.6 MiB | — | — | — | — |
|
||||
| 500 | 500 | 128.3 MiB | 148.5 MiB | +39.7 MiB | 83 KiB | 1.1 s |
|
||||
| 5,000 | 5,000 | 137.7 MiB | 305.0 MiB | +49.1 MiB | 10 KiB | 10.9 s |
|
||||
| 20,000 | 20,000 | 150.9 MiB | 292.3 MiB | +62.3 MiB | 3 KiB | 43.7 s |
|
||||
|
||||
All three sizes are **within the 400 MiB cap**. The peak is the number that
|
||||
matters: the cgroup limit is tripped during a bulk write, not after it.
|
||||
|
||||
### The finding that changed the design
|
||||
|
||||
With Qdrant's **default** settings the container was OOM-killed at exactly the
|
||||
cap between 5,000 and 20,000 chunks (`anon-rss: 404204kB`, `exitCode 137`,
|
||||
`constraint=CONSTRAINT_MEMCG`). The default keeps the HNSW graph and the payload
|
||||
in RAM and holds several segments live during ingest.
|
||||
|
||||
The collection is now created memory-frugal, and the pipeline is the only code
|
||||
that creates it (`corpus/qdrant.py`):
|
||||
|
||||
```json
|
||||
{
|
||||
"vectors": {"size": 1024, "distance": "Cosine", "on_disk": false},
|
||||
"hnsw_config": {"m": 8, "ef_construct": 64, "on_disk": true},
|
||||
"on_disk_payload": true,
|
||||
"optimizers_config": {"default_segment_number": 2, "memmap_threshold": 5000, "indexing_threshold": 20000}
|
||||
}
|
||||
```
|
||||
|
||||
Vectors stay in RAM because search reads them on every query. The HNSW graph and
|
||||
the payloads go to disk, where the kernel can reclaim those pages instead of
|
||||
killing the process. `memmap_threshold` 5,000 makes segments that size mmap;
|
||||
`indexing_threshold` 20,000 stops a bulk ingest from building the graph while it
|
||||
is still writing. The cost is build time and a little search latency, both
|
||||
irrelevant for a corpus of this size and a public site with no query burst.
|
||||
|
||||
Practical ceiling: at 1024 dimensions the cap holds 20,000 chunks. If the corpus
|
||||
grows past that, set `RESUME_QDRANT_VECTORS_ON_DISK=1`, or reduce the dimension,
|
||||
or raise the cap — in that order of preference. Vector memory scales linearly
|
||||
with dimension: 1,536-dimension vectors need 50 % more than recorded here.
|
||||
|
||||
---
|
||||
|
||||
## 8. Embedding provider — still Sam's decision
|
||||
|
||||
`reference/stack.md` lists the open item: Gemini free tier, OpenRouter, or
|
||||
local. Phase 4 does not wait for it:
|
||||
|
||||
* **`omniroute`** is implemented and tested. OpenAI-compatible
|
||||
`/v1/embeddings`, batched, retry with backoff on 429 and 5xx, key from the
|
||||
environment only, no provider SDK, no direct vendor call (AGENTS.md §4.5).
|
||||
* **`local-hashing`** is the default for tests, for offline rebuilds and for the
|
||||
memory measurement. It is deterministic feature hashing, **not a semantic
|
||||
model**: it is a placeholder that lets the whole pipeline run without a
|
||||
provider decision, and it must not be used for answers.
|
||||
|
||||
The provider, model, dimension and chunker version are stored in the collection
|
||||
contract (`data/corpus/collection.json` and the `meta` table). A run whose
|
||||
configuration differs is refused:
|
||||
|
||||
```
|
||||
the collection contract does not match this configuration — refusing to mix
|
||||
vectors in one collection (docs/phase-4-corpus.md §4.3).
|
||||
```
|
||||
|
||||
### Changing the provider (governed migration)
|
||||
|
||||
1. Get Sam's decision and record it in `reference/decisions.md`.
|
||||
2. Set `RESUME_EMBED_PROVIDER`, `RESUME_EMBED_MODEL`, `RESUME_EMBED_DIM`, and
|
||||
`OMNIROUTE_API_KEY` in `~/.config/environment.d/`.
|
||||
3. `make corpus` — a rebuild, because a provider change invalidates every
|
||||
vector. It is one command and takes seconds at this corpus size.
|
||||
4. `make corpus-verify` and `make corpus-audit`.
|
||||
|
||||
---
|
||||
|
||||
## 9. Recovery
|
||||
|
||||
Deleting the Qdrant volume or `data/corpus/` is **always recoverable**:
|
||||
|
||||
```sh
|
||||
docker compose -f deploy/qdrant.yml down -v
|
||||
docker compose -f deploy/qdrant.yml up -d
|
||||
make corpus # re-derives every vector and FTS row from content/
|
||||
make corpus-verify
|
||||
```
|
||||
|
||||
There is no state that exists only in the index. `content/` is the source of
|
||||
truth; the corpus is a derived artefact and lives in `data/`, which is
|
||||
gitignored.
|
||||
|
||||
---
|
||||
|
||||
## 10. Definition of Done — evidence
|
||||
|
||||
| DoD item | Evidence |
|
||||
|---|---|
|
||||
| One command rebuilds from an empty volume | `make corpus` on an empty Qdrant volume: 40 documents → 51 chunks, 51 points, 1.0 s |
|
||||
| Running it twice produces no duplicate vectors | rebuild ×2 → 51 points both times; `sync` with no changes → 0 embedding calls, 0 points sent |
|
||||
| No chunk boundary inside a fence, table or front-matter | `corpus/tests/test_chunker.py`; 51/51 stored chunks reproduce from `content/`, 8 protected regions, 0 violations |
|
||||
| Every chunk carries all payload fields; missing field fails the run | `corpus/tests/test_payloads.py` (each field removed in turn); 51/51 stored payloads validate; a broken payload aborts before any write (`test_invalid_payload_aborts_before_any_write`) |
|
||||
| Payload filtering demonstrably narrows results | `make corpus-demo` — query "distributed audio endpoints": unfiltered returns edge *and* ai documents; `--area edge` returns edge only, `--area ai` returns ai only |
|
||||
| Incremental re-embeds only changed documents | edit one of 40 documents → `updated=1 unchanged=39 embedding calls=1 texts=1`; re-run → `calls=0` |
|
||||
| Freshness audit runs and reports stale documents | `python3 -m corpus.cli audit` today: 0 findings. `--today 2027-10-05`: 40 stale documents, one row per document with age in days. `--today 2026-11-01`: 0 findings |
|
||||
| Qdrant memory at 500 / 5,000 / 20,000 chunks, within cap | §7 table; 128 / 138 / 151 MiB settled, 149 / 305 / 292 MiB peak, cap 400 MiB |
|
||||
|
||||
---
|
||||
|
||||
## 11. Not done here
|
||||
|
||||
* Qdrant is **not deployed to `.13`** and the Prefect deployment is not applied.
|
||||
`deploy/qdrant.yml` is ready; the runtime host is Phase 3's (`AUD-143`) and
|
||||
Sam's to apply.
|
||||
* No provider is chosen (§8).
|
||||
* Answering questions, the query graph, the streaming console and guardrails are
|
||||
Phase 5 and Phase 6. Phase 4 ends at "retrieval returns the right chunks".
|
||||
@@ -80,3 +80,43 @@ One hand-written TypeScript island, ES modules, no framework, no bundler.
|
||||
| Panel refactor needed | Phase 2 §2.4 contract; if it fails, fix the panel contract, do not fork the UI |
|
||||
| Streaming stalls behind Caddy | Explicit flush settings; p90 measurement, not a spot check |
|
||||
| Answers drift from the site | Citation verification is a node, not a hope |
|
||||
|
||||
---
|
||||
|
||||
## 5.6 Citations point at the original
|
||||
|
||||
An answer cites where it came from, not a curated page standing in for it.
|
||||
|
||||
| Source | Cited as |
|
||||
|---|---|
|
||||
| Gitea repo file | full URL to the file in Gitea |
|
||||
| Vault note | full URL to the note in `sam/obsidian-vault` |
|
||||
| Graph answer | the note title the node came from, plus its URL |
|
||||
| Curated claim | the site page |
|
||||
|
||||
A citation whose URL cannot be opened is dropped, not rendered. A claim with no
|
||||
traceable origin is not returned at all.
|
||||
|
||||
## 5.7 Curated outranks harvested
|
||||
|
||||
`content/` is tier 1; everything harvested is tier 2. When the two disagree, the
|
||||
tier 1 claim wins and the answer says so plainly: the curated description is
|
||||
current; the older text is background.
|
||||
|
||||
This is the rule that stops a stale README from quietly overwriting Sam's own
|
||||
description of his work.
|
||||
|
||||
## 5.8 The live source readout
|
||||
|
||||
While the answer streams, a panel shows what is being read, named as the visitor
|
||||
would recognise it:
|
||||
|
||||
```
|
||||
reading · gitea/photo-pipeline/README.md
|
||||
· obsidian/210 AI Resume/Graphify.md
|
||||
· graph · community 42 · vault infrastructure
|
||||
```
|
||||
|
||||
This is a feature, not a debug view. It answers the question every recruiter has
|
||||
and does not ask: is this making things up? Show the working — it is also the most
|
||||
convincing demonstration of the system on the page.
|
||||
|
||||
71
docs/phase-7-notes.md
Normal file
71
docs/phase-7-notes.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Phase 7 — notes
|
||||
|
||||
Running record for the visual layer (AUD-149). Each batch states what changed,
|
||||
what it measures, and what it still needs.
|
||||
|
||||
---
|
||||
|
||||
## Batch 1 — marks, area icons, client wall, two diagrams
|
||||
|
||||
### What is in the tree
|
||||
|
||||
| Piece | Where | Note |
|
||||
|---|---|---|
|
||||
| Technology marks | `internal/brand/` | 50 Simple Icons paths (CC0), inlined as 16 px `currentColor` SVG. No request, no font, no bundler. |
|
||||
| Regenerator | `tools/gen_brand_icons.sh` | Development-time only. The generated Go file is committed, so `make build` never needs the network. |
|
||||
| Area icons | `internal/web/templates/icons.templ` | Six marks drawn for this site, plus one for the evidence index. 24×24, 1.6 px stroke, `aria-hidden`. |
|
||||
| Client wall | `content/history/l3/history-client-wall.md` + `.client-wall` in `site.css` | 45 names, one typeface, one size, one grid of equal tiles. No client mark is used, because none has been approved. |
|
||||
| Diagrams | `content/assets/diagrams/` | Homelab topology and RAG pipeline. Both referenced from the page they explain. |
|
||||
| Image sizing | `internal/content/markdown.go` | Every content image gets intrinsic `width`/`height`, `loading="lazy"`, `decoding="async"`. |
|
||||
| Guard | `internal/site/site_test.go` | `TestImagesReserveTheirSpace` fails if an image ships without size or alt text. |
|
||||
| Topbar | `site.css` | Two rows at desktop. One row overlapped the search form once the marks were added; Lighthouse `target-size` caught it. |
|
||||
| Favicon | `internal/web/static/favicon.svg` | Stops the browser asking for a missing `/favicon.ico` (it was the only console error). |
|
||||
|
||||
### Adding a technology mark
|
||||
|
||||
1. Add the label → slug pair to `slugs` in `internal/brand/brand.go`.
|
||||
2. Add the slug to `slugs()` in `tools/gen_brand_icons.sh` and run it.
|
||||
3. `make dist`, then check the chip on the page.
|
||||
|
||||
A technology with no published mark renders as text alone, on purpose: a chip
|
||||
with a missing image would read as a broken page.
|
||||
|
||||
### Adding an image
|
||||
|
||||
- File goes under `content/assets/` (published at `/assets/...`).
|
||||
- SVG needs explicit `width` and `height` on the root element; PNG and JPEG are
|
||||
measured with `image.DecodeConfig`. Both give the page its reserved space.
|
||||
- Alt text describes the thing, not the file name.
|
||||
- The diagrams carry their own print style, so they print as ink on white.
|
||||
|
||||
### Measurements (2026-10-28)
|
||||
|
||||
Lighthouse 12.8.2, headless Chrome, against the local server on `127.0.0.1`.
|
||||
|
||||
| Page | Mode | Performance | Accessibility | Best practices | SEO | CLS |
|
||||
|---|---|---|---|---|---|---|
|
||||
| Landing | mobile | 100 | 100 | 100 | 100 | 0 |
|
||||
| Landing | desktop | 100 | 100 | 100 | 100 | 0 |
|
||||
| Platform network (diagram) | mobile | 100 | 100 | 100 | 100 | 0 |
|
||||
| AI retrieval (diagram) | desktop | 100 | 100 | 100 | 100 | 0 |
|
||||
| Client wall | mobile | 100 | 100 | 100 | 100 | — |
|
||||
| Edge audio artefact | mobile | 100 | 100 | 100 | 100 | — |
|
||||
|
||||
Total page weight: landing 153 KiB, diagram page 105 KiB. Diagram files are 5.7
|
||||
KiB and 6.6 KiB; the stylesheet is 24 KiB; htmx is 51 KiB and is the largest
|
||||
single asset.
|
||||
|
||||
Two defects were found by measurement rather than by eye and fixed in this
|
||||
batch: the desktop topbar overlap, and the missing favicon.
|
||||
|
||||
### Still open — needs Sam
|
||||
|
||||
| DoD line | State | What is needed |
|
||||
|---|---|---|
|
||||
| At least one real photograph in use | Not started | Which photographs may be used (Immich on `.35` is the likely source). Anything used is checked for people, address and screens in shot. |
|
||||
| Two screenshots in use | Not started | Approval to capture Home Assistant, Gitea, TUIOS with agent panes, or Graphify's `graph.html`. Screens must contain nothing personal. |
|
||||
| One recording, 30–60 s | Not started | Either Sam records it, or Sam approves a recording of the agent console. |
|
||||
| Client wall marks | Deliberately absent | Official marks only, with Sam's approval. Nothing invented. |
|
||||
|
||||
Nothing above is blocking the site: it ships, measures 100 across the board, and
|
||||
every page works with the AI services stopped.
|
||||
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1792773339,
|
||||
"narHash": "sha256-rZLrzrC6i+jnTAlvPZHogKDulY7WWDEv9hJeHieO7Zw=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3ec636362e1816bc2f0888fa0fa2c69f1bc9e504",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
27
flake.nix
Normal file
27
flake.nix
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
description = "Ai Resume — build environment (Go + templ + SQLite). Node, Vite and bundlers are deliberately absent.";
|
||||
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
systems = [ "x86_64-linux" "aarch64-linux" ];
|
||||
forAll = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
|
||||
in {
|
||||
devShells = forAll (pkgs: {
|
||||
default = pkgs.mkShell {
|
||||
packages = [
|
||||
pkgs.go
|
||||
pkgs.templ
|
||||
pkgs.sqlite # the sqlite3 CLI, for inspecting the FTS5 index
|
||||
pkgs.gnumake
|
||||
pkgs.curl
|
||||
];
|
||||
shellHook = ''
|
||||
echo "ai-resume dev shell — go $(go version | cut -d' ' -f3), templ $(templ version)"
|
||||
echo "targets: make generate build test dev"
|
||||
'';
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
89
flows/corpus.py
Normal file
89
flows/corpus.py
Normal file
@@ -0,0 +1,89 @@
|
||||
"""Prefect entry points for the corpus pipeline.
|
||||
|
||||
`docs/phase-4-corpus.md` §4.4: *"Prefect deployment on the existing Prefect
|
||||
server; worker only, do not deploy a second server. Trigger: git push to
|
||||
content/, plus a nightly audit."*
|
||||
|
||||
Prefect is optional at runtime. If it is not installed, these functions are
|
||||
plain callables and the same code runs straight from the CLI
|
||||
(`python3 -m corpus.cli sync`), which is what `make corpus` does. Nothing about
|
||||
the pipeline depends on the orchestrator, and the orchestrator depends on
|
||||
nothing but the pipeline.
|
||||
|
||||
The deployment commands (run on `.13`, which already has the Prefect server) are
|
||||
in `docs/phase-4-notes.md` §6. This file is only the work definition.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
try: # pragma: no cover - exercised only where Prefect is installed
|
||||
from prefect import flow as _flow
|
||||
from prefect import get_run_logger as _get_run_logger
|
||||
|
||||
PREFECT_AVAILABLE = True
|
||||
except ImportError: # pragma: no cover
|
||||
PREFECT_AVAILABLE = False
|
||||
|
||||
def _flow(*_args, **_kwargs):
|
||||
def decorator(func):
|
||||
return func
|
||||
|
||||
return decorator
|
||||
|
||||
def _get_run_logger():
|
||||
class _Logger:
|
||||
def info(self, message, *args, **kwargs):
|
||||
print(message, file=sys.stderr)
|
||||
|
||||
warning = error = info
|
||||
|
||||
return _Logger()
|
||||
|
||||
|
||||
from corpus import freshness, pipeline # noqa: E402 (after the Prefect shim)
|
||||
from corpus.config import Config # noqa: E402
|
||||
|
||||
|
||||
@_flow(name="corpus-sync", log_prints=True)
|
||||
def sync_corpus() -> dict:
|
||||
"""Incremental refresh. Runs on every push that touches content/."""
|
||||
logger = _get_run_logger()
|
||||
cfg = Config.from_env()
|
||||
stats = pipeline.run(cfg, mode="sync", log=logger.info)
|
||||
return stats.to_dict()
|
||||
|
||||
|
||||
@_flow(name="corpus-rebuild", log_prints=True)
|
||||
def rebuild_corpus() -> dict:
|
||||
"""From-scratch rebuild. The recovery path: deleting the volume is always safe."""
|
||||
logger = _get_run_logger()
|
||||
cfg = Config.from_env()
|
||||
stats = pipeline.run(cfg, mode="rebuild", log=logger.info)
|
||||
return stats.to_dict()
|
||||
|
||||
|
||||
@_flow(name="corpus-audit", log_prints=True)
|
||||
def freshness_audit(alert: bool = True) -> dict:
|
||||
"""Nightly audit. Reports stale documents and index drift, then alerts."""
|
||||
logger = _get_run_logger()
|
||||
cfg = Config.from_env()
|
||||
report = freshness.audit(cfg, log=logger.info)
|
||||
logger.info(freshness.render_text(report))
|
||||
if alert and not report["ok"]:
|
||||
try:
|
||||
freshness.alert(report, cfg.apprise_url)
|
||||
except Exception as exc: # noqa: BLE001 - the alert must not fail the audit
|
||||
logger.warning(f"Apprise alert failed: {exc}")
|
||||
return report
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover - convenience for a manual run
|
||||
command = sys.argv[1] if len(sys.argv) > 1 else "sync"
|
||||
if command == "audit":
|
||||
print(freshness.render_text(freshness_audit(alert=False)))
|
||||
elif command == "rebuild":
|
||||
print(rebuild_corpus())
|
||||
else:
|
||||
print(sync_corpus())
|
||||
25
go.mod
Normal file
25
go.mod
Normal file
@@ -0,0 +1,25 @@
|
||||
module gitea.lab.audasmedia.com.au/sam/ai-resume
|
||||
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/a-h/templ v0.3.1020
|
||||
github.com/alecthomas/chroma/v2 v2.27.0
|
||||
github.com/yuin/goldmark v1.8.6
|
||||
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2/v2 v2.2.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
modernc.org/libc v1.55.3 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.8.0 // indirect
|
||||
modernc.org/sqlite v1.34.5 // indirect
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user