WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s

This commit is contained in:
yawan
2026-09-22 16:19:06 +08:00
commit f7fc992f57
3929 changed files with 1006607 additions and 0 deletions
+237
View File
@@ -0,0 +1,237 @@
package agent
import (
"strings"
"testing"
"text/template"
)
func TestWikiGranularityGuidance_RoutesByKey(t *testing.T) {
cases := map[string]string{
"focused": WikiGranularityGuidanceFocused,
"standard": WikiGranularityGuidanceStandard,
"exhaustive": WikiGranularityGuidanceExhaustive,
}
for key, want := range cases {
if got := WikiGranularityGuidance(key); got != want {
t.Errorf("WikiGranularityGuidance(%q) returned unexpected block", key)
_ = got
}
}
}
func TestWikiGranularityGuidance_UnknownDefaultsToStandard(t *testing.T) {
unknowns := []string{"", "FOCUSED", "detailed", "minimal", "full", "unknown"}
for _, k := range unknowns {
if WikiGranularityGuidance(k) != WikiGranularityGuidanceStandard {
t.Errorf("WikiGranularityGuidance(%q) should fall back to STANDARD block", k)
}
}
}
// Sanity check that the three guidance blocks are meaningfully different.
// A regression (e.g. two constants accidentally pointing at the same string)
// would silently disable the user-facing level control.
func TestWikiGranularityGuidance_BlocksAreDistinct(t *testing.T) {
blocks := []string{
WikiGranularityGuidanceFocused,
WikiGranularityGuidanceStandard,
WikiGranularityGuidanceExhaustive,
}
seen := make(map[string]bool, len(blocks))
for _, b := range blocks {
if b == "" {
t.Error("granularity guidance block must not be empty")
continue
}
if seen[b] {
t.Error("granularity guidance blocks must be distinct")
}
seen[b] = true
}
// Each block should name its mode, so the LLM can't silently get the
// wrong guidance without us noticing in review.
if !strings.Contains(WikiGranularityGuidanceFocused, "FOCUSED") {
t.Error("focused block should self-identify")
}
if !strings.Contains(WikiGranularityGuidanceStandard, "STANDARD") {
t.Error("standard block should self-identify")
}
if !strings.Contains(WikiGranularityGuidanceExhaustive, "EXHAUSTIVE") {
t.Error("exhaustive block should self-identify")
}
}
func renderWikiChunkCitation(t *testing.T, candidateSlugs, chunksXML, lang string) string {
t.Helper()
tmpl, err := template.New("cite").Parse(WikiChunkCitationPrompt)
if err != nil {
t.Fatalf("parse template: %v", err)
}
var b strings.Builder
if err := tmpl.Execute(&b, map[string]string{
"CandidateSlugs": candidateSlugs,
"ChunksXML": chunksXML,
"Language": lang,
}); err != nil {
t.Fatalf("execute template: %v", err)
}
return b.String()
}
// TestWikiChunkCitationPrompt_StablePrefixAcrossBatches verifies the property
// that enables provider prefix caching (issue #1687): within one document the
// candidate slugs and the static rules are constant and only the per-batch
// <chunks> block changes, so everything up to <chunks> must be byte-identical
// across batches. If the static rules trailed <chunks> they would be re-billed
// on every batch and this prefix would diverge.
func TestWikiChunkCitationPrompt_StablePrefixAcrossBatches(t *testing.T) {
const slugs = "entity/acme = Acme Corp\nconcept/rag = Retrieval-Augmented Generation"
a := renderWikiChunkCitation(t, slugs, `<c id="c001">first batch text</c>`, "English")
b := renderWikiChunkCitation(t, slugs, `<c id="c099">a completely different second batch</c>`, "English")
// Match the standalone <chunks> tag line (the per-batch data block), not the
// inline "<chunks> block" references inside the instructions prose.
const marker = "\n<chunks>\n"
ia := strings.Index(a, marker)
ib := strings.Index(b, marker)
if ia < 0 || ib < 0 {
t.Fatalf("rendered prompt missing %q block", marker)
}
if a[:ia] != b[:ib] {
t.Errorf("prompt prefix before <chunks> differs across batches — provider prefix cache will miss.\nA-prefix:\n%s\n---\nB-prefix:\n%s", a[:ia], b[:ib])
}
// The static rules and per-document candidate-slug block must live inside
// that shared prefix, not after the varying chunks.
for _, must := range []string{"### Primary task", "### JSON Formatting Rules", "\n<candidate_slugs>\n"} {
if idx := strings.Index(a, must); idx < 0 || idx > ia {
t.Errorf("%q must appear before <chunks> to be part of the cached prefix (idx=%d, chunks=%d)", must, idx, ia)
}
}
}
// TestWikiChunkCitationPrompt_PreservesPlaceholders guards against accidental
// loss of a template field during future reorders.
func TestWikiChunkCitationPrompt_PreservesPlaceholders(t *testing.T) {
for _, field := range []string{"{{.Language}}", "{{.CandidateSlugs}}", "{{.ChunksXML}}"} {
if !strings.Contains(WikiChunkCitationPrompt, field) {
t.Errorf("WikiChunkCitationPrompt lost template field %q", field)
}
}
}
func TestWikiPageModifyUserPrompt_HidesInternalChunkHandles(t *testing.T) {
combined := WikiPageModifySystemPrompt + "\n" + WikiPageModifyUserPrompt
for _, guidance := range []string{
"NEVER output them in the page body or summary",
"Source associations are stored separately by the system",
"clean Markdown without inline chunk IDs",
} {
if !strings.Contains(combined, guidance) {
t.Errorf("WikiPageModifyUserPrompt missing chunk-handle guidance %q", guidance)
}
}
for _, obsolete := range []string{"Preserve Citations", "followed by an inline citation"} {
if strings.Contains(combined, obsolete) {
t.Errorf("WikiPageModifyUserPrompt still contains obsolete inline-citation rule %q", obsolete)
}
}
}
func renderWikiPageModify(t *testing.T, sourceContexts, title string) string {
t.Helper()
tmpl, err := template.New("modify").Parse(WikiPageModifyUserPrompt)
if err != nil {
t.Fatalf("parse template: %v", err)
}
var b strings.Builder
if err := tmpl.Execute(&b, map[string]string{
"HasAdditions": "1",
"SharedSourceContexts": sourceContexts,
"PageSlug": "concept/" + strings.ToLower(title),
"PageTitle": title,
"PageType": "concept",
"ExistingContent": "(New page)",
"NewContent": "page-specific chunks for " + title,
"Language": "English",
}); err != nil {
t.Fatalf("execute template: %v", err)
}
return b.String()
}
func TestWikiPageModifyUserPrompt_SharedSourceContextPrecedesPageVariables(t *testing.T) {
const shared = "<document><title>Same Source</title><context>long shared summary</context></document>"
a := renderWikiPageModify(t, shared, "Alpha")
b := renderWikiPageModify(t, shared, "Beta")
marker := "\n<page_metadata>\n"
ia, ib := strings.Index(a, marker), strings.Index(b, marker)
if ia < 0 || ib < 0 {
t.Fatalf("rendered prompt missing page metadata marker")
}
if a[:ia] != b[:ib] {
t.Fatalf("shared source prefix differs across pages\nA: %s\nB: %s", a[:ia], b[:ib])
}
if !strings.Contains(a[:ia], shared) {
t.Fatalf("shared source context is not part of the cacheable prefix: %s", a[:ia])
}
}
func TestWikiPromptsPreserveSourceFaithfulnessAndSeparateMetadata(t *testing.T) {
const source = "Alpha and Beta are distinct devices. Alpha is also called First. For group A, operation is prohibited; group B must use caution. Report One gives ?-20 units; Report Two disputes that range."
data := map[string]string{
"Content": source, "Language": "English", "Granularity": "standard",
"GranularityGuidance": WikiGranularityGuidanceStandard,
"CandidateSlugs": `<candidate id="s000" name="Alpha" description="navigation only"/>`,
"ChunksXML": `<c id="c000">` + source + `</c>`,
"HasAdditions": "1", "PageSlug": "entity/alpha", "PageTitle": "Alpha", "PageType": "entity",
"NewContent": "<content>" + source + "</content>",
}
for _, test := range []struct {
name, prompt, metadata string
}{
{"candidate", WikiCandidateSlugPrompt, "never evidence for page synthesis"},
{"citation", WikiChunkCitationPrompt, "routing hints only, not evidence"},
{"page", WikiPageModifySystemPrompt + "\n" + WikiPageModifyUserPrompt, "routing metadata, not evidence"},
} {
t.Run(test.name, func(t *testing.T) {
tmpl, err := template.New(test.name).Parse(test.prompt)
if err != nil {
t.Fatal(err)
}
var b strings.Builder
if err := tmpl.Execute(&b, data); err != nil {
t.Fatal(err)
}
rendered := b.String()
for _, required := range []string{
source, test.metadata, "Never complete damaged OCR", "missing digits", "unreadable range endpoints",
"conditions, exceptions, negation and restriction strength", "Never weaken a prohibition into caution",
"Preserve conflicting claims, attribution and uncertainty", "Source ordering alone does not resolve disagreement",
"Do not assert a same-identity relationship unless it is explicitly supported by the supplied source",
"Never collapse subjects that the source distinguishes",
} {
if !strings.Contains(rendered, required) {
t.Errorf("rendered %s prompt lost source or grounding rule %q", test.name, required)
}
}
for _, obsolete := range []string{"fallback summary", "ONLY used when chunk-level citation fails", "well-known alternate names", "well-known synonyms used interchangeably", "prefer the newer information", "supersede or contradict existing content"} {
if strings.Contains(rendered, obsolete) {
t.Errorf("rendered %s prompt retains unsupported fallback/alias permission %q", test.name, obsolete)
}
}
if test.name != "page" {
for _, required := range []string{"wiki-source-identity-v2", "Entity names must be nonempty exact text", "ONE LF or CRLF", "Never join paragraphs", "Concept headings may descriptively summarize", `Every newly generated entity and concept must have "aliases": []`, "exactly equal outer-trimmed title"} {
if !strings.Contains(rendered, required) {
t.Errorf("rendered %s prompt lost identity rule %q", test.name, required)
}
}
}
})
}
}