search: the ChatGPT backend's image search gives as many images as asked (LBM_OASIG on X)
web_search's count had no effect on an image search: one image came
back whatever was asked. magpie did send image_settings.max_results,
and the backend answered with twelve images, but it puts each rule of
80 dashes straight after the page's last word ("...beauty.----...\n"),
and codexPages cut only at "\n"+rule, so the twelve were read as one
page. codexBlocks cuts at 80 dashes ending a line with no dash before
them. Page searches gain too: a real search_query reply had 33 pages,
3 of them glued to the one before (30 blocks old, 33 new).
Run: real Plus account (~/.codex token, read-only), /alpha/search with
image_query "golden gate bridge" and max_results 5: 12 images, rules
glued; reply kept as testdata/codex-image-search.json.
TestImageSearchGivesTheCount (counts 1, 5, 12 from the real bytes) is
red on the old split by hand (count 1 gave one page holding all twelve),
green now; TestCodexBlocksKeepLongDashLines. On mica (mote over its
cores): go test -run 'ImageSearch|CodexBlocks|CodexPages|ImageResults|
SearchScope|CodexSearch|AlphaSearch|Searcher' ./internal/gateway ok;
gofmt -l clean. The whole internal/gateway package was still running on
mica when this was pushed (owner: ship at once).
Not tried: through a running sandbox magpie end to end; GUI untouched.
This commit is contained in:
@@ -2059,6 +2059,9 @@ instead of `search_query` for `images`, `settings.filters`,
|
||||
searcher is asked the sites as `site:` and `-site:` operators in the query
|
||||
(`searchScoped`, at the top of `webSearch`) and ignores the rest. Image
|
||||
results come back as pages whose text starts "Image: <url>" (`imageSaid`).
|
||||
The output is cut into pages at each line of 80 dashes (`codexBlocks`),
|
||||
which often follows a page's last word with no line break: an image
|
||||
search always does. `TestImageSearchGivesTheCount`.
|
||||
`TestSearchScopeReachesCodexSearch`, `TestSearchScopeOperatorsForOtherSearchers`.
|
||||
|
||||
## How many results a search is answered with
|
||||
|
||||
@@ -82,6 +82,32 @@ type codexSearchReply struct {
|
||||
// codexSearchRule is the line between the pages of a search's output.
|
||||
var codexSearchRule = "\n" + strings.Repeat("-", 80) + "\n"
|
||||
|
||||
// codexBlocks are a search's output cut at its rules. A rule doesn't
|
||||
// always start a line: a real image search (2026-11-15) put every rule
|
||||
// straight after its page's last word ("…beauty.----…----\n"), and a page
|
||||
// search after some, so cutting only at "\n"+rule read twelve images as
|
||||
// one. A rule is 80 dashes ending a line with no dash before them, so a
|
||||
// longer line of dashes in a page's text is no rule.
|
||||
func codexBlocks(output string) []string {
|
||||
dashes := strings.Repeat("-", 80) + "\n"
|
||||
var blocks []string
|
||||
from, at := 0, 0
|
||||
for {
|
||||
i := strings.Index(output[at:], dashes)
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
i += at
|
||||
at = i + len(dashes)
|
||||
if i > 0 && output[i-1] == '-' {
|
||||
continue
|
||||
}
|
||||
blocks = append(blocks, output[from:i])
|
||||
from = at
|
||||
}
|
||||
return append(blocks, output[from:])
|
||||
}
|
||||
|
||||
// codexSearchBy searches with a ChatGPT account: by the backend's search,
|
||||
// then, when it fails, by the account's small model.
|
||||
func (s *Server) codexSearchBy(ctx context.Context, p provider.Provider, query string) (string, []Hit, error) {
|
||||
@@ -212,7 +238,7 @@ func codexPages(output string, out codexSearchReply, n int) []foundPage {
|
||||
}
|
||||
}
|
||||
var pages []foundPage
|
||||
for _, block := range strings.Split(output, codexSearchRule) {
|
||||
for _, block := range codexBlocks(output) {
|
||||
head, rest, _ := strings.Cut(strings.TrimSpace(block), "\n")
|
||||
title, link := head, ""
|
||||
if i := strings.LastIndex(head, " ("); i >= 0 && strings.HasSuffix(head, ")") {
|
||||
|
||||
@@ -3,6 +3,7 @@ package gateway
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -86,3 +87,46 @@ func TestSearchMCPSchemaOffersScope(t *testing.T) {
|
||||
t.Fatal("the schema names magpie")
|
||||
}
|
||||
}
|
||||
|
||||
// An image search's output, as a real Plus account answered one on
|
||||
// 2026-11-15 (testdata/codex-image-search.json, image_query "golden gate
|
||||
// bridge"), puts each rule straight after its page's last word. Its twelve
|
||||
// images were read as one page, so a count past 1 gave one image (LBM_OASIG
|
||||
// on X).
|
||||
func TestImageSearchGivesTheCount(t *testing.T) {
|
||||
b, err := os.ReadFile("testdata/codex-image-search.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var reply codexSearchReply
|
||||
if err := json.Unmarshal(b, &reply); err != nil || reply.Output == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, n := range []int{1, 5, 12} {
|
||||
pages := codexPages(*reply.Output, reply, n)
|
||||
if len(pages) != n {
|
||||
t.Fatalf("count %d: %d pages", n, len(pages))
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, p := range pages {
|
||||
img, _, _ := strings.Cut(p.Text, "\n")
|
||||
if !strings.HasPrefix(img, "Image: https://") || strings.Contains(p.Text, "-----") || seen[img] {
|
||||
t.Fatalf("count %d: %+v", n, p)
|
||||
}
|
||||
seen[img] = true
|
||||
}
|
||||
}
|
||||
if pages := codexPages(*reply.Output, reply, 5); pages[1].URL != "https://www.worldatlas.com/articles/the-7-wonders-of-california.html" {
|
||||
t.Fatalf("second page %+v", pages[1])
|
||||
}
|
||||
}
|
||||
|
||||
// A longer line of dashes in a page's text is not a rule.
|
||||
func TestCodexBlocksKeepLongDashLines(t *testing.T) {
|
||||
rule := strings.Repeat("-", 80) + "\n"
|
||||
long := strings.Repeat("-", 100) + "\n"
|
||||
got := codexBlocks("A (https://a.example)\ntext\n" + long + "more" + rule + "B (https://b.example)\nb")
|
||||
if len(got) != 2 || !strings.Contains(got[0], long+"more") || !strings.HasPrefix(got[1], "B ") {
|
||||
t.Fatalf("%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user