feat(pipeline): feed the current source page into the extraction prompt
#103 made ingest merge into the page already on disk instead of clobbering it, but the extraction itself stayed page-blind: the model wrote every page as if fresh, and Merge reconciled the two texts afterwards by heading. It could restate a claim but never refine it, and had no way to know what already existed. BuildPrompt now accepts the source page's current on-disk content (when one exists) and asks the model to revise it explicitly: add what's missing, strengthen what the source now supports, leave the rest alone and reuse its wording verbatim so unchanged claims are recognized as unchanged. Scoped to the source page specifically — its path (wiki/sources/<sourceSlug>.md) is deterministic before extraction runs, unlike a concept/entity page's path, which only exists once the model has produced a title. The deterministic merge in wiki.Merge is unchanged and stays the backstop: an on-disk section the model doesn't touch survives untouched regardless of what the prompt asks for, so a model that ignores the instruction still cannot destroy human content. Closes hyperguild#106 (first half; heading normalization follows). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -277,6 +277,104 @@ func TestRun_ReExtractsWhenTheSourceBodyChanged(t *testing.T) {
|
||||
assert.Equal(t, 2, calls, "changed content must be re-extracted")
|
||||
}
|
||||
|
||||
// --- #106: page-aware extraction — the model sees the page it is revising --
|
||||
|
||||
func TestRun_ExistingSourcePageIsIncludedInTheExtractionPrompt(t *testing.T) {
|
||||
// #103 made the merge asymmetric (on-disk wins); #106 is the other half —
|
||||
// the extraction itself gets to see what's already there instead of
|
||||
// producing a page blind and letting the merge reconcile it afterwards.
|
||||
// This only proves the existing page reaches the prompt; the byte-identical
|
||||
// guarantee for an echoed-back revision is a separate test below.
|
||||
dir := t.TempDir()
|
||||
for _, sub := range []string{"wiki/concepts", "wiki/entities", "wiki/sources"} {
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(dir, sub), 0o755))
|
||||
}
|
||||
|
||||
seeded := "---\ntitle: 'Shape Up'\n---\n\n## Summary\n\nA book about shaping product work.\n\n## Key Claims\n\n- Appetite beats estimation.\n"
|
||||
page := filepath.Join(dir, "wiki", "sources", "shape-up.md")
|
||||
require.NoError(t, os.WriteFile(page, []byte(seeded), 0o644))
|
||||
|
||||
var gotPrompt string
|
||||
cfg := Config{Complete: func(_ context.Context, _, user string) (string, error) {
|
||||
gotPrompt = user
|
||||
return mustJSON([]RawPage{{
|
||||
Title: "Shape Up", Type: "source", Subtype: "book",
|
||||
Content: "## Summary\n\nA book about shaping product work.\n",
|
||||
}}), nil
|
||||
}}
|
||||
|
||||
// Deliberately does not mention "Appetite beats estimation." — that phrase
|
||||
// can only reach the prompt via the existing on-disk page, not the source
|
||||
// content, so its presence proves the page was threaded through.
|
||||
content := "A short editorial update to the book's listing. No new claims here."
|
||||
_, err := Run(context.Background(), cfg, dir, content, "shape-up", false)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Contains(t, gotPrompt, "Appetite beats estimation.",
|
||||
"the existing page's content must be visible to the extraction prompt")
|
||||
assert.Contains(t, gotPrompt, "revis", "the prompt must ask for a revision, not a fresh page")
|
||||
}
|
||||
|
||||
func TestRun_NewSourceHasNoExistingPageInThePrompt(t *testing.T) {
|
||||
// The other side of the same behavior: a source with no page on disk yet
|
||||
// must not get an "existing page" section injected into its prompt at all.
|
||||
dir := t.TempDir()
|
||||
for _, sub := range []string{"wiki/concepts", "wiki/entities", "wiki/sources"} {
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(dir, sub), 0o755))
|
||||
}
|
||||
|
||||
var gotPrompt string
|
||||
cfg := Config{Complete: func(_ context.Context, _, user string) (string, error) {
|
||||
gotPrompt = user
|
||||
return mustJSON([]RawPage{{
|
||||
Title: "New Thing", Type: "source", Subtype: "article",
|
||||
Content: "## Summary\n\nBrand new.\n",
|
||||
}}), nil
|
||||
}}
|
||||
|
||||
_, err := Run(context.Background(), cfg, dir, "Brand new content.", "new-thing", false)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NotContains(t, gotPrompt, "Current page",
|
||||
"a source with no page on disk yet must not get a revision section in its prompt")
|
||||
}
|
||||
|
||||
func TestRun_RevisionEchoingExistingPageStaysByteIdentical(t *testing.T) {
|
||||
// The issue's explicit risk: a model given the current page will sometimes
|
||||
// echo it back verbatim. That must not duplicate content or otherwise
|
||||
// perturb the page — the deterministic merge is still the backstop.
|
||||
dir := t.TempDir()
|
||||
for _, sub := range []string{"wiki/concepts", "wiki/entities", "wiki/sources"} {
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(dir, sub), 0o755))
|
||||
}
|
||||
|
||||
seeded := "---\ntitle: 'Shape Up'\n---\n\n## Summary\n\nA book about shaping product work.\n\n## Key Claims\n\n- Appetite beats estimation.\n"
|
||||
page := filepath.Join(dir, "wiki", "sources", "shape-up.md")
|
||||
require.NoError(t, os.WriteFile(page, []byte(seeded), 0o644))
|
||||
|
||||
cfg := Config{Complete: func(_ context.Context, _, _ string) (string, error) {
|
||||
return mustJSON([]RawPage{{
|
||||
Title: "Shape Up", Type: "source", Subtype: "book",
|
||||
Content: "## Summary\n\nA book about shaping product work.\n\n## Key Claims\n\n- Appetite beats estimation.\n",
|
||||
}}), nil
|
||||
}}
|
||||
|
||||
content := "A short editorial update to the book's listing. No new claims here."
|
||||
result, err := Run(context.Background(), cfg, dir, content, "shape-up", false)
|
||||
require.NoError(t, err)
|
||||
require.False(t, result.Skipped)
|
||||
|
||||
got, err := os.ReadFile(page)
|
||||
require.NoError(t, err)
|
||||
|
||||
want := "---\ntitle: 'Shape Up'\nlast_updated: " + time.Now().UTC().Format("2006-01-02") +
|
||||
"\nsource_hash: " + hashBody(content) + "\n---\n\n" +
|
||||
"## Summary\n\nA book about shaping product work.\n\n" +
|
||||
"## Key Claims\n\n- Appetite beats estimation.\n"
|
||||
assert.Equal(t, want, string(got),
|
||||
"a revision pass that echoes the existing page back must not duplicate or alter its content")
|
||||
}
|
||||
|
||||
func TestRun_FrontmatterOnlyChangeIsNotDrift(t *testing.T) {
|
||||
// The hash covers the body, so re-fetching a page whose metadata moved
|
||||
// (a re-crawl stamping a new date) is not treated as the source changing.
|
||||
|
||||
Reference in New Issue
Block a user