Phase 2 part 4: discovery per signal (Claude web search, explee.com)

Each signal's page can search for organizations that show it, by hand or
every 7, 14 or 30 days. Claude with the web_search_20260209 server tool
(pause_turn resumed) works from the signal's "How to spot it" lines and
the profile's targets; a company is kept only if its evidence URL was
among the pages searched. explee.com's company search runs the signal's
description in the target sizes, scored against the hypothesis, keeping
strong matches. Our sites, the inventory, existing prospects, CRM
Accounts and suppressed domains are left out. New companies go to Review
tagged with the signal. Schema 8 adds discovery_runs (counts, searches,
why companies were left out) and the signal's schedule and explee.com
description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 18:27:35 -07:00
co-authored by Claude Opus 5.5
parent b025a97e4a
commit 157902f7e2
15 changed files with 1234 additions and 15 deletions
+183 -1
View File
@@ -10,9 +10,12 @@ import (
"strings"
"time"
"supermarket/internal/ai"
"supermarket/internal/api/explee"
"supermarket/internal/api/origami"
"supermarket/internal/discovery"
"supermarket/internal/leadsource"
"supermarket/internal/profile"
"supermarket/internal/prospects"
"supermarket/internal/secrets"
"supermarket/internal/signals"
@@ -83,6 +86,14 @@ type signalData struct {
Campaigns []signals.Campaign
Events []signals.Event
Prospects int // new prospects tagged with it
Runs []discovery.Run
Running bool
Research string // why Claude can't search, or ""
ExpleeKey bool
EveryDays []int
MaxSearch int
MaxResults int
}
type channelRow struct {
@@ -110,7 +121,12 @@ func (s *Server) signalPage(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) renderSignal(w http.ResponseWriter, r *http.Request, sig *signals.Signal, errMsg string) {
d := signalData{S: sig, Statuses: signals.Statuses, Stages: signals.Stages}
d := signalData{S: sig, Statuses: signals.Statuses, Stages: signals.Stages,
EveryDays: []int{0, 7, 14, 30}, MaxSearch: discovery.MaxSearches, MaxResults: discovery.MaxCompanies,
ExpleeKey: s.Secrets.Has(secrets.ExpleeAPIKey)}
if _, err := s.researcher(); err != nil {
d.Research = err.Error()
}
title := "New signal"
if sig.ID != 0 {
title = sig.Name
@@ -156,6 +172,13 @@ func (s *Server) renderSignal(w http.ResponseWriter, r *http.Request, sig *signa
return
}
d.Prospects = len(cs) + len(ps)
if d.Runs, err = discovery.Runs(s.DB, sig.ID, 8); err != nil {
s.serverError(w, err)
return
}
for _, r := range d.Runs {
d.Running = d.Running || r.Status == "running"
}
}
s.render(w, r, "signal", page{Title: title, Nav: "prospects/signals", Error: errMsg, Data: d})
}
@@ -180,6 +203,8 @@ func (s *Server) signalSave(w http.ResponseWriter, r *http.Request) {
f := r.PostFormValue
sig.Name, sig.Slug, sig.Hypothesis, sig.Spot, sig.Angle, sig.Status =
f("name"), f("slug"), f("hypothesis"), f("spot"), f("angle"), f("status")
sig.ExpleeDefinition = f("explee_definition")
sig.DiscoverEveryDays, _ = strconv.Atoi(f("discover_every_days"))
if err := signals.Save(s.DB, sig); err != nil {
s.renderSignal(w, r, sig, err.Error())
return
@@ -446,3 +471,160 @@ func (s *Server) RunImports(ctx context.Context) {
}
}
}
// --- Discovery ---
// researcher is the AI client, if it can search the web.
func (s *Server) researcher() (ai.Researcher, error) {
cfg, err := ai.LoadConfig(s.DB)
if err != nil {
return nil, err
}
c, err := ai.New(cfg, s.Secrets)
if err != nil {
return nil, err
}
r, ok := c.(ai.Researcher)
if !ok {
return nil, ai.ErrNoResearch
}
return r, nil
}
// discoverTimeout bounds one run: several web searches and a long reply.
const discoverTimeout = 15 * time.Minute
// discover runs one search for a signal. It runs in the background, since
// it takes minutes; the signal's page shows its progress.
func (s *Server) discover(sig *signals.Signal, source string) error {
prof, err := profile.Load(s.DB)
if err != nil {
return err
}
var run func(ctx context.Context) (*discovery.Run, error)
switch source {
case discovery.SourceClaude:
r, err := s.researcher()
if err != nil {
return err
}
run = func(ctx context.Context) (*discovery.Run, error) {
return discovery.FindWithClaude(ctx, s.DB, r, sig, prof, time.Now())
}
case discovery.SourceExplee:
key := s.Secrets.Get(secrets.ExpleeAPIKey)
if key == "" {
return errors.New("save your explee.com API key first, under Settings → Lead sources")
}
if strings.TrimSpace(sig.ExpleeDefinition) == "" {
return errors.New("describe what the companies do, for explee.com, in the signal's form below")
}
run = func(ctx context.Context) (*discovery.Run, error) {
return discovery.FindWithExplee(ctx, s.DB, explee.New(key), sig, prof, time.Now())
}
default:
return errors.New("unknown source")
}
started := make(chan error, 1)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), discoverTimeout)
defer cancel()
res, err := run(ctx)
if res == nil {
started <- err // didn't start: already running
return
}
started <- nil
if err != nil {
s.Log.Warn("discovery failed", "signal", sig.ID, "source", source, "err", err)
return
}
s.Log.Info("discovery finished", "signal", sig.ID, "source", source, "found", res.Found, "added", res.Added,
"searches", res.Searches)
}()
// Wait briefly, to report a run that couldn't start.
select {
case err := <-started:
return err
case <-time.After(1500 * time.Millisecond):
return nil
}
}
func (s *Server) signalDiscover(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
sig, err := signals.Get(s.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
if err := s.discover(sig, r.PathValue("source")); err != nil {
s.renderSignal(w, r, sig, err.Error())
return
}
redirect(w, r, "/signals/"+strconv.FormatInt(id, 10)+"#discovery", "discovering")
}
// signalRuns is the discovery card's run list, polled while a run is going.
func (s *Server) signalRuns(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
runs, err := discovery.Runs(s.DB, id, 8)
if err != nil {
s.serverError(w, err)
return
}
running := false
for _, run := range runs {
running = running || run.Status == "running"
}
s.fragment(w, "signal", "runs", map[string]any{"ID": id, "Runs": runs, "Running": running})
}
// RunDiscovery searches for signals that ask to be searched on a schedule.
func (s *Server) RunDiscovery(ctx context.Context) {
if err := discovery.RecoverInterrupted(s.DB); err != nil {
s.Log.Error("discovery: recovering interrupted runs", "err", err)
}
tick := time.NewTicker(30 * time.Minute)
defer tick.Stop()
for {
select {
case <-ctx.Done():
return
case <-tick.C:
}
list, err := signals.List(s.DB)
if err != nil {
s.Log.Error("discovery: listing signals", "err", err)
continue
}
for i := range list {
sig := &list[i]
if sig.DiscoverEveryDays <= 0 || sig.Status == signals.StatusRetired {
continue
}
due := time.Duration(sig.DiscoverEveryDays) * 24 * time.Hour
sources := []string{discovery.SourceClaude}
if sig.ExpleeDefinition != "" && s.Secrets.Has(secrets.ExpleeAPIKey) {
sources = append(sources, discovery.SourceExplee)
}
for _, src := range sources {
last, err := discovery.LastStarted(s.DB, sig.ID, src)
if err != nil || time.Since(last) < due {
continue
}
if err := s.discover(sig, src); err != nil {
s.Log.Warn("discovery: scheduled run didn't start", "signal", sig.ID, "source", src, "err", err)
}
}
}
}
}