The Atlas BigLaw / Big Michael — documentation bound to its code
7 documents

Inside a DyTopo round

Open one round of Dynamic Topology Routing: agents declare what they Need and Offer, cosine similarity wires them into a comm graph, jurisdiction-ineligible agents are dropped, then everyone runs an agentic loop whose findings are gated, debated, verified, and rolled up into memory.

biglaw-go/internal/dytopo/engine.go552 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Discover Legal

// DyTopo Engine — Dynamic Topology Routing for Multi-Agent Reasoning.
// Based on arXiv:2602.06039 (Lu et al., 2026).

package dytopo

import (
	"context"
	"fmt"
	"log/slog"
	"sort"
	"strings"
	"time"

	"github.com/google/uuid"
	"golang.org/x/sync/errgroup"

	"github.com/discover-legal/biglaw-go/internal/agents"
	"github.com/discover-legal/biglaw-go/internal/audit"
	"github.com/discover-legal/biglaw-go/internal/config"
	"github.com/discover-legal/biglaw-go/internal/cost"
	"github.com/discover-legal/biglaw-go/internal/embeddings"
	"github.com/discover-legal/biglaw-go/internal/learning"
	"github.com/discover-legal/biglaw-go/internal/memory"
	"github.com/discover-legal/biglaw-go/internal/providers"
	"github.com/discover-legal/biglaw-go/internal/routing"
	"github.com/discover-legal/biglaw-go/internal/strutil"
	"github.com/discover-legal/biglaw-go/internal/types"
)

type AgentBillingCtx struct {
	ResponsibleLawyerID   string
	ResponsibleLawyerName string
	MatterNumber          string
	ClientNumber          string
}

type Engine struct {
	registry   *agents.Registry
	memory     *memory.InterRoundStore
	memAdapter *memory.Adapter
	knowledge  agents.KnowledgeStore
	pinned     []types.AgentDefinition
	cfg        *config.Config
	provReg    *providers.Registry
	costs      *cost.Store
	embedC     *embeddings.Client
	tools      agents.ToolRegistry
	learning   *learning.Engine
}

type Options struct {
	Registry  *agents.Registry
	Memory    *memory.InterRoundStore
	Knowledge agents.KnowledgeStore
	Pinned    []types.AgentDefinition
	Tools     agents.ToolRegistry
	Learning  *learning.Engine
}

func New(cfg *config.Config, prov *providers.Registry, costs *cost.Store, embedC *embeddings.Client, opts Options) *Engine {
	return &Engine{
		registry:   opts.Registry,
		memory:     opts.Memory,
		memAdapter: memory.NewAdapter(opts.Memory),
		knowledge:  opts.Knowledge,
		pinned:     opts.Pinned,
		tools:      opts.Tools,
		cfg:        cfg,
		provReg:    prov,
		costs:      costs,
		embedC:     embedC,
		learning:   opts.Learning,
	}
}

// RunRound executes one round of DyTopo orchestration.
func (e *Engine) RunRound(task *types.Task, goal types.RoundGoal, lawyerTone *types.ToneProfile, billing *AgentBillingCtx) (*types.RoundState, error) {
	roundID := uuid.New().String()
	intra := memory.NewIntraRound(roundID)

	audit.Default.Write(audit.WriteRequest{
		Event:   "round.start",
		ActorID: audit.ActorSystem,
		TaskID:  task.ID,
		Data:    map[string]interface{}{"round": goal.Round, "phase": goal.Phase, "roundId": roundID},
	})

	// Step 1: Recruit agents.
	recruited, err := e.recruitAgents(goal, task)
	if err != nil {
		return nil, fmt.Errorf("recruit agents: %w", err)
	}
	agentMap := map[string]types.AgentDefinition{}
	for _, a := range e.pinned {
		agentMap[a.ID] = a
	}
	for _, a := range recruited {
		agentMap[a.ID] = a
	}
	var activeDefs []types.AgentDefinition
	for _, a := range agentMap {
		if JurisdictionMatch(a, task.Jurisdiction) {
			activeDefs = append(activeDefs, a)
		}
		if len(activeDefs) >= e.cfg.DyTopo.MaxAgentsPerRound {
			break
		}
	}
	activeAgents := make([]*agents.Agent, len(activeDefs))
	for i, def := range activeDefs {
		activeAgents[i] = agents.NewAgent(def, e.cfg, e.provReg, e.costs)
	}

	// Step 2: Fetch inter-round memory per agent.
	agentMemories, err := e.fetchAgentMemories(activeDefs, task, goal)
	if err != nil {
		return nil, err
	}

	// Step 3: Need/Offer descriptors (parallel).
	type noResult struct {
		need  types.NeedDescriptor
		offer types.OfferDescriptor
	}
	noResults := make([]noResult, len(activeAgents))
	var g1 errgroup.Group
	for i, ag := range activeAgents {
		i, ag := i, ag
		g1.Go(func() error {
			ctx := agents.AgentContext{
				RoundGoal:       goal,
				MemoryEntries:   agentMemories[ag.Def.ID],
				TaskDescription: task.Description,
				TaskID:          task.ID,
			}
			need, offer, err := ag.GenerateNeedOffer(ctx)
			if err != nil {
				need = types.NeedDescriptor{AgentID: ag.Def.ID, Text: "No specific need."}
				offer = types.OfferDescriptor{AgentID: ag.Def.ID, Text: "General expertise."}
			}
			noResults[i] = noResult{need, offer}
			return nil
		})
	}
	g1.Wait()

	needs := make([]types.NeedDescriptor, len(noResults))
	offers := make([]types.OfferDescriptor, len(noResults))
	for i, r := range noResults {
		needs[i] = r.need
		offers[i] = r.offer
	}

	// Step 4: Build sparse comm graph.
	edges, err := e.buildCommGraph(needs, offers, activeDefs)
	if err != nil {
		return nil, err
	}

	// Step 5: Route messages.
	msgs := e.routeMessages(edges, offers, goal.Round)
	for _, msg := range msgs {
		intra.RecordMessage(msg.To, msg)
	}

	// List the matter's documents (title + ID) so agents know what exists and can
	// pull verbatim passages on demand via search_knowledge — the map, not the
	// territory. Keeps a small model's context lean and quoting on the tool path.
	documentIndex := e.buildDocumentIndex(task)

	// Step 6: Process agents (parallel).
	findingsCh := make([][]types.Finding, len(activeAgents))
	roundTimeout := time.Duration(e.cfg.Agents.RoundTimeoutMs) * time.Millisecond
	var g2 errgroup.Group
	for i, ag := range activeAgents {
		i, ag := i, ag
		g2.Go(func() error {
			ctx := agents.AgentContext{
				RoundGoal:          goal,
				IncomingMessages:   intra.GetMessagesFor(ag.Def.ID),
				MemoryEntries:      agentMemories[ag.Def.ID],
				TaskDescription:    task.Description,
				TaskID:             task.ID,
				DocumentIndex:      documentIndex,
				ToolRegistry:       e.tools,
				KnowledgeStore:     e.knowledge,
				MemoryStore:        e.memAdapter,
				OwnerID:            task.CreatedByProfileID,
				AssignedLawyerTone: lawyerTone,
			}
			if billing != nil {
				ctx.ResponsibleLawyerID = billing.ResponsibleLawyerID
				ctx.ResponsibleLawyerName = billing.ResponsibleLawyerName
				ctx.MatterNumber = billing.MatterNumber
				ctx.ClientNumber = billing.ClientNumber
			}
			// Per-agent wall-clock cap (AGENT_ROUND_TIMEOUT_MS) so one hung
			// provider/tool call can't stall the whole round. Process takes
			// an AgentContext, not a context.Context, so the deadline cannot
			// be propagated into the call; the call is raced against it
			// instead (mirroring the TS Promise.race) and its findings are
			// discarded on timeout while the abandoned goroutine drains into
			// the buffered channel.
			tctx, cancel := context.WithTimeout(context.Background(), roundTimeout)
			defer cancel()
			type procResult struct {
				findings []types.Finding
				err      error
			}
			done := make(chan procResult, 1)
			go func() {
				findings, err := ag.Process(ctx)
				done <- procResult{findings: findings, err: err}
			}()
			select {
			case r := <-done:
				if r.err != nil {
					return nil
				}
				findingsCh[i] = r.findings
			case <-tctx.Done():
				slog.Warn("agent exceeded round timeout; recording no findings for it this round",
					"agentId", ag.Def.ID, "round", goal.Round, "phase", goal.Phase,
					"timeoutMs", e.cfg.Agents.RoundTimeoutMs)
			}
			return nil
		})
	}
	g2.Wait()

	// Non-nil even when empty: these slices are part of the REST/MCP JSON
	// contract (the UI iterates them), and nil marshals to null.
	allFindings := []types.Finding{}
	for _, findings := range findingsCh {
		for _, f := range findings {
			f.Round = goal.Round
			intra.RecordFinding(f.AgentID, f)
			intra.AddSharedContext(fmt.Sprintf("[%s] %s", f.AgentName, truncate(f.Content, 200)))
			allFindings = append(allFindings, f)
		}
	}

	// Step 7: Persist round memory.
	e.persistRoundMemory(task, goal, allFindings, intra)

	now := time.Now()
	state := &types.RoundState{
		RoundID:        roundID,
		Goal:           goal,
		ActiveAgentIDs: agentIDs(activeDefs),
		Edges:          edges,
		Messages:       msgs,
		Findings:       allFindings,
		Status:         "complete",
		StartedAt:      now,
		CompletedAt:    &now,
	}

	audit.Default.Write(audit.WriteRequest{
		Event:   "round.complete",
		ActorID: audit.ActorSystem,
		TaskID:  task.ID,
		Data: map[string]interface{}{
			"round":    goal.Round,
			"phase":    goal.Phase,
			"roundId":  roundID,
			"findings": len(allFindings),
			"edges":    len(edges),
		},
	})

	return state, nil
}

// ─── Private helpers ──────────────────────────────────────────────────────────

var phaseToTier = map[types.TaskPhase]*types.AgentTier{
	types.PhaseIntake:       tierPtr(types.TierManager),
	types.PhaseResearch:     tierPtr(types.TierSpecialist),
	types.PhaseAnalysis:     tierPtr(types.TierSpecialist),
	types.PhaseDrafting:     tierPtr(types.TierSpecialist),
	types.PhaseReview:       tierPtr(types.TierSpecialist),
	types.PhaseVerification: tierPtr(types.TierSpecialist),
	types.PhaseDelivery:     tierPtr(types.TierManager),
}

func tierPtr(t types.AgentTier) *types.AgentTier { return &t }

// buildDocumentIndex assembles a short, sanitized list of the task's documents
// (title + ID) for injection into agent prompts — the MAP of what is on the
// matter, not the territory. Agents call search_knowledge to pull verbatim
// passages on demand, which keeps a small model's context window lean and keeps
// quoting on the tool-calling path where the citation gate verifies it. Returns
// "" when the task has no documents.
func (e *Engine) buildDocumentIndex(task *types.Task) string {
	if len(task.DocumentIDs) == 0 || e.knowledge == nil {
		return ""
	}
	var b strings.Builder
	for _, id := range task.DocumentIDs {
		doc := e.knowledge.GetByID(id)
		if doc == nil {
			continue
		}
		title := strings.TrimSpace(doc.Title)
		if title == "" {
			title = id
		}
		b.WriteString("[doc: ")
		b.WriteString(title)
		b.WriteString("] (id: ")
		b.WriteString(id)
		b.WriteString(")\n")
	}
	return strings.TrimSpace(b.String())
}

func (e *Engine) recruitAgents(goal types.RoundGoal, task *types.Task) ([]types.AgentDefinition, error) {
	topK := e.cfg.DyTopo.MaxAgentsPerRound - 1
	opts := agents.SearchOpts{Tier: phaseToTier[goal.Phase], TopK: topK}

	var positive, negative []string
	for _, round := range task.Rounds {
		for _, f := range round.Findings {
			if f.Challenged {
				negative = append(negative, f.AgentID)
			} else {
				positive = append(positive, f.AgentID)
			}
		}
	}
	positive = unique(positive)[:min(8, len(unique(positive)))]
	negative = unique(negative)[:min(4, len(unique(negative)))]

	var candidates []types.AgentDefinition
	var err error
	if len(positive) > 0 {
		candidates, err = e.registry.Recommend(goal.Description, positive, negative, opts)
	} else {
		candidates, err = e.registry.Search(goal.Description, opts)
	}
	if err != nil {
		return nil, err
	}

	// Q-learning rerank.
	if e.learning != nil {
		ids := make([]string, len(candidates))
		for i, c := range candidates {
			ids[i] = c.ID
		}
		rankedIDs := e.learning.RankCandidates(goal.Phase, task.Jurisdiction, task.WorkflowType, ids)
		byID := map[string]types.AgentDefinition{}
		for _, c := range candidates {
			byID[c.ID] = c
		}
		ranked := make([]types.AgentDefinition, 0, len(rankedIDs))
		for _, id := range rankedIDs {
			if def, ok := byID[id]; ok {
				ranked = append(ranked, def)
			}
		}
		return ranked, nil
	}
	return candidates, nil
}

func (e *Engine) fetchAgentMemories(defs []types.AgentDefinition, task *types.Task, goal types.RoundGoal) (map[string][]types.MemoryEntry, error) {
	result := map[string][]types.MemoryEntry{}
	for _, def := range defs {
		agentMem, _ := e.memory.Query(goal.Description, memory.QueryOpts{
			TaskID:      task.ID,
			AgentID:     def.ID,
			BeforeRound: goal.Round,
			TopK:        6,
		})
		taskMem, _ := e.memory.Query(goal.Description, memory.QueryOpts{
			TaskID:      task.ID,
			BeforeRound: goal.Round,
			TopK:        4,
		})
		result[def.ID] = append(agentMem, taskMem...)
	}
	return result, nil
}

func (e *Engine) buildCommGraph(needs []types.NeedDescriptor, offers []types.OfferDescriptor, defs []types.AgentDefinition) ([]types.CommunicationEdge, error) {
	allTexts := make([]string, 0, len(needs)+len(offers))
	for _, n := range needs {
		allTexts = append(allTexts, n.Text)
	}
	for _, o := range offers {
		allTexts = append(allTexts, o.Text)
	}

	results, err := e.embedC.EmbedBatch(allTexts)
	if err != nil {
		return nil, fmt.Errorf("embed descriptors: %w", err)
	}

	needEmbs := make([][]float32, len(needs))
	offerEmbs := make([][]float32, len(offers))
	for i := range needs {
		needEmbs[i] = results[i].Embedding
	}
	for i := range offers {
		offerEmbs[i] = results[len(needs)+i].Embedding
	}

	threshold := e.cfg.DyTopo.SimilarityThreshold
	// Non-nil even when no pair clears the threshold — see RunRound: this
	// slice is serialized to the UI, and nil marshals to null.
	edges := []types.CommunicationEdge{}
	for i, need := range needs {
		for j, offer := range offers {
			if need.AgentID == offer.AgentID {
				continue
			}
			sim := embeddings.CosineSimilarity(needEmbs[i], offerEmbs[j])
			if sim >= threshold {
				edges = append(edges, types.CommunicationEdge{
					From:       offer.AgentID,
					To:         need.AgentID,
					Similarity: sim,
					OfferText:  offer.Text,
				})
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].Similarity > edges[j].Similarity })
	return edges, nil
}

func (e *Engine) routeMessages(edges []types.CommunicationEdge, offers []types.OfferDescriptor, round int) []types.AgentMessage {
	offerMap := map[string]string{}
	for _, o := range offers {
		offerMap[o.AgentID] = o.Text
	}
	msgs := make([]types.AgentMessage, len(edges))
	for i, edge := range edges {
		text := offerMap[edge.From]
		if len(text) > 500 {
			text = strutil.Truncate(text, 500)
		}
		msgs[i] = types.AgentMessage{
			ID:        uuid.New().String(),
			From:      edge.From,
			To:        edge.To,
			Content:   fmt.Sprintf("[Offer from %s] %s", edge.From, text),
			Round:     round,
			Timestamp: time.Now(),
		}
	}
	return msgs
}

func (e *Engine) persistRoundMemory(task *types.Task, goal types.RoundGoal, findings []types.Finding, intra *memory.IntraRoundStore) {
	for _, f := range findings {
		e.memory.WriteFindingMemory(memory.WriteFindingOpts{
			TaskID:  task.ID,
			Round:   goal.Round,
			Phase:   goal.Phase,
			AgentID: f.AgentID,
			Finding: f,
		})
	}

	summary := fmt.Sprintf("Round %d (%s): No findings produced.", goal.Round, goal.Phase)
	if len(findings) > 0 {
		// Call Haiku for a rollup summary.
		bullets := ""
		max := 12
		if len(findings) < max {
			max = len(findings)
		}
		for _, f := range findings[:max] {
			c := f.Content
			if len(c) > 150 {
				c = strutil.Truncate(c, 150)
			}
			bullets += fmt.Sprintf("- [%s] %s\n", f.AgentName, c)
		}

		tier := types.TierTool
		model := routing.SelectModel(e.cfg, routing.SelectParams{Tier: &tier, TaskType: routing.TaskDescriptor})
		prov, err := e.provReg.Get(model)
		if err == nil {
			resp, err := prov.Chat(providers.ChatParams{
				Model:     routing.ResolveModelID(model),
				MaxTokens: 300,
				System:    "You are a legal analysis synthesizer. Produce a concise inter-round memory digest.",
				Messages: []providers.Message{{
					Role:    "user",
					Content: fmt.Sprintf("Round %d (%s) findings:\n%s\n\nSummarise the key legal conclusions in 2-3 sentences.", goal.Round, goal.Phase, bullets),
				}},
			})
			if err == nil {
				for _, b := range resp.Content {
					if b.Type == providers.BlockText && b.Text != "" {
						summary = b.Text
						break
					}
				}
			}
		}
	}

	e.memory.WriteRoundSummary(memory.WriteRoundSummaryOpts{
		TaskID:       task.ID,
		Round:        goal.Round,
		Phase:        goal.Phase,
		Summary:      summary,
		FindingCount: len(findings),
	})
}

// ─── Utility ──────────────────────────────────────────────────────────────────

func agentIDs(defs []types.AgentDefinition) []string {
	ids := make([]string, len(defs))
	for i, d := range defs {
		ids[i] = d.ID
	}
	return ids
}

func unique(s []string) []string {
	seen := map[string]bool{}
	var out []string
	for _, v := range s {
		if !seen[v] {
			seen[v] = true
			out = append(out, v)
		}
	}
	return out
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func truncate(s string, max int) string {
	return strutil.Truncate(s, max)
}