Repository navigation
Expand file tree
/
Copy pathcontext_pack.go
More file actions
548 lines (509 loc) · 17.9 KB
/
Copy pathcontext_pack.go
File metadata and controls
548 lines (509 loc) · 17.9 KB
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
package tools
import (
"fmt"
"strings"
"time"
"unicode"
"github.com/Detective-XH/docgraph/internal/store"
)
type contextPackOptions struct {
IncludeContent bool
MaxContentBytes int
ImpactDepth int
ReferenceLimit int
}
func (opts contextPackOptions) normalized() contextPackOptions {
if opts.MaxContentBytes <= 0 {
opts.MaxContentBytes = 2000
}
if opts.MaxContentBytes > 6000 {
opts.MaxContentBytes = 6000
}
if opts.ImpactDepth < 1 {
opts.ImpactDepth = 1
}
if opts.ImpactDepth > 3 {
opts.ImpactDepth = 3
}
if opts.ReferenceLimit <= 0 {
opts.ReferenceLimit = 5
}
if opts.ReferenceLimit > 20 {
opts.ReferenceLimit = 20
}
return opts
}
func (h *handler) renderContextPack(task string, results []store.SearchResult, opts contextPackOptions) string {
opts = opts.normalized()
var sb strings.Builder
fmt.Fprintf(&sb, "# Context Pack: %s\n\n", task)
sb.WriteString("## Manifest\n")
sb.WriteString("- **Format:** docgraph.context_pack.v1\n")
fmt.Fprintf(&sb, "- **Query:** %s\n", task)
fmt.Fprintf(&sb, "- **Items:** %d\n", len(results))
sb.WriteString("- **Snapshot source:** indexed `section_chunks` when available; live file reads are not used in context packs.\n")
sb.WriteString("- **Compatibility:** pack fields are additive; consumers should ignore unknown future fields.\n")
for i, sr := range results {
h.appendContextPackItem(&sb, i+1, sr.Node, opts)
}
return sb.String()
}
func (h *handler) appendContextPackItem(sb *strings.Builder, index int, node store.Node, opts contextPackOptions) {
st := h.getStoreForResolvedNode(&node)
docID := contextPackDocID(node)
var docNode *store.Node
if st != nil {
docNode = h.getNodeByIDFromStore(st, docID)
}
if docNode == nil && node.Kind == "document" {
docNode = &node
}
fmt.Fprintf(sb, "\n## Evidence %d: %s\n", index, node.Name)
sb.WriteString("### Identity\n")
fmt.Fprintf(sb, "- **Node ID:** %s\n", node.ID)
fmt.Fprintf(sb, "- **Document ID:** %s\n", docID)
fmt.Fprintf(sb, "- **Kind:** %s\n", node.Kind)
fmt.Fprintf(sb, "- **Path:** %s\n", formatNodePath(node))
if node.QualifiedName != "" {
fmt.Fprintf(sb, "- **Qualified name:** %s\n", node.QualifiedName)
}
if node.StartLine > 0 {
fmt.Fprintf(sb, "- **Lines:** %d-%d\n", node.StartLine, node.EndLine)
}
h.appendContextPackSnapshot(sb, st, &node, opts)
h.appendContextPackMetadata(sb, st, docID)
h.appendContextPackQuality(sb, st, docID)
h.appendContextPackReferences(sb, st, docNode, opts.ReferenceLimit)
h.appendContextPackImpact(sb, st, docID, opts.ImpactDepth, opts.ReferenceLimit)
}
func (h *handler) appendContextPackSnapshot(sb *strings.Builder, st *store.Store, node *store.Node, opts contextPackOptions) {
sb.WriteString("\n### Evidence Snapshot\n")
if st == nil {
sb.WriteString("- **Status:** unavailable; store could not be resolved.\n")
return
}
chunk, ok, err := st.GetSectionChunk(node.ID)
if err != nil {
fmt.Fprintf(sb, "- **Status:** unavailable; %v.\n", err)
return
}
if !ok {
if fileHash, err := st.GetFileHash(node.FilePath); err == nil && fileHash != "" {
fmt.Fprintf(sb, "- **Content hash:** %s\n", fileHash)
}
sb.WriteString("- **Section hash:** unavailable; indexed section snapshot missing.\n")
sb.WriteString("- **Snapshot note:** run `docgraph index --force` to rebuild section chunks.\n")
return
}
fmt.Fprintf(sb, "- **Source path:** %s\n", chunk.FilePath)
if chunk.StartLine > 0 {
fmt.Fprintf(sb, "- **Indexed lines:** %d-%d\n", chunk.StartLine, chunk.EndLine)
}
if chunk.HeadingPath != "" {
fmt.Fprintf(sb, "- **Heading path:** %s\n", chunk.HeadingPath)
}
fmt.Fprintf(sb, "- **Content hash:** %s\n", chunk.ContentHash)
fmt.Fprintf(sb, "- **Section hash:** %s\n", chunk.SectionHash)
if !opts.IncludeContent {
return
}
text := strings.TrimRight(chunk.Text, "\n")
if text == "" {
return
}
if len(text) > opts.MaxContentBytes {
text = text[:opts.MaxContentBytes] + fmt.Sprintf("\n[content truncated at %d bytes]", opts.MaxContentBytes)
}
sb.WriteString("\n#### Section Content\n")
sb.WriteString("```markdown\n")
sb.WriteString(text)
sb.WriteString("\n```\n")
}
func (h *handler) appendContextPackMetadata(sb *strings.Builder, st *store.Store, docID string) {
if st == nil {
return
}
summary, _ := st.GetAISummary(docID)
gov, _ := st.GetGovernanceMetadata(docID)
research, _ := st.GetResearchMetadata(docID)
if (summary == nil || summary.Summary == "") && store.IsGovernanceEmpty(gov) && store.IsResearchEmpty(research) {
return
}
sb.WriteString("\n### Retrieval Metadata\n")
if summary != nil && summary.Summary != "" {
sb.WriteString("#### Agent-Inferred Summary\n")
writeContextPackField(sb, "Summary", summary.Summary)
writeContextPackField(sb, "Source", "agent_inferred")
writeContextPackField(sb, "Authority", "advisory, non-authoritative")
writeContextPackField(sb, "Model", summary.ModelID)
writeContextPackField(sb, "Agent", summary.AgentID)
writeContextPackField(sb, "Run ID", summary.RunID)
writeContextPackField(sb, "Content hash", summary.ContentHash)
}
if !store.IsGovernanceEmpty(gov) {
sb.WriteString("#### Governance\n")
writeContextPackField(sb, "Status", gov.Status)
writeContextPackField(sb, "Sensitivity", gov.Sensitivity)
writeContextPackField(sb, "Owner", gov.Owner)
writeContextPackField(sb, "Approver", gov.Approver)
writeContextPackField(sb, "Department", gov.Department)
writeContextPackField(sb, "Effective date", gov.EffectiveDate)
writeContextPackField(sb, "Review due", gov.ReviewDue)
writeContextPackField(sb, "Supersedes", gov.Supersedes)
writeContextPackField(sb, "Superseded by", gov.SupersededBy)
writeContextPackField(sb, "Allowed audience", gov.AllowedAudience)
writeContextPackField(sb, "Canonical source", gov.CanonicalSource)
}
if !store.IsResearchEmpty(research) {
sb.WriteString("#### Research\n")
writeContextPackField(sb, "Claim ID", research.ClaimID)
writeContextPackField(sb, "Evidence", research.Evidence)
writeContextPackField(sb, "Source type", research.SourceType)
writeContextPackField(sb, "Confidence", research.Confidence)
writeContextPackField(sb, "Event date", research.EventDate)
writeContextPackField(sb, "Assessment date", research.AssessmentDate)
writeContextPackField(sb, "Last verified", research.LastVerified)
writeContextPackField(sb, "Valid until", research.ValidUntil)
writeContextPackField(sb, "Analyst status", research.AnalystStatus)
writeContextPackField(sb, "Client", research.Client)
writeContextPackField(sb, "Deliverable ID", research.DeliverableID)
}
}
func (h *handler) appendContextPackQuality(sb *strings.Builder, st *store.Store, docID string) {
if st == nil || docID == "" {
return
}
quality, err := st.GetMetadataQuality(docID, time.Time{})
if err != nil || quality == nil {
return
}
sb.WriteString("\n### Metadata Quality\n")
fmt.Fprintf(sb, "- **Score:** %d/100\n", quality.Score)
fmt.Fprintf(sb, "- **Level:** %s\n", quality.Level)
fmt.Fprintf(sb, "- **As of:** %s\n", quality.AsOf)
fmt.Fprintf(sb, "- **Incoming references:** %d\n", quality.IncomingReferences)
fmt.Fprintf(sb, "- **Outgoing references:** %d\n", quality.OutgoingReferences)
if len(quality.Issues) == 0 {
sb.WriteString("- **Issues:** none\n")
return
}
sb.WriteString("- **Issues:**\n")
limit := len(quality.Issues)
if limit > 8 {
limit = 8
}
for _, issue := range quality.Issues[:limit] {
fmt.Fprintf(sb, " - `%s` (%s, -%d): %s\n", issue.Code, issue.Severity, issue.Penalty, issue.Message)
if issue.Remediation != "" {
fmt.Fprintf(sb, " - Fix: %s\n", issue.Remediation)
}
}
if len(quality.Issues) > limit {
fmt.Fprintf(sb, " - ... %d more quality issues omitted\n", len(quality.Issues)-limit)
}
}
func (h *handler) appendContextPackReferences(sb *strings.Builder, st *store.Store, docNode *store.Node, limit int) {
if st == nil || docNode == nil {
return
}
incoming, _ := st.GetIncomingEdges(docNode.ID)
outgoing, _ := st.GetOutgoingEdges(docNode.ID)
sb.WriteString("\n### Citation Paths\n")
fmt.Fprintf(sb, "- **Incoming references:** %d\n", len(incoming))
for _, edge := range limitEdges(incoming, limit) {
src := h.getNodeByIDFromStore(st, edge.Source)
fmt.Fprintf(sb, " - %s --%s--> %s%s\n",
contextPackNodeLabel(src, edge.Source), edge.Kind, contextPackNodeLabel(docNode, docNode.ID), contextPackLineSuffix(edge.Line))
}
if len(incoming) > limit {
fmt.Fprintf(sb, " - ... %d more incoming references omitted\n", len(incoming)-limit)
}
fmt.Fprintf(sb, "- **Outgoing references:** %d\n", len(outgoing))
for _, edge := range limitEdges(outgoing, limit) {
if edge.Kind == "links_external" {
fmt.Fprintf(sb, " - %s --%s--> %s%s\n",
contextPackNodeLabel(docNode, docNode.ID), edge.Kind, extractURL(edge.Metadata), contextPackLineSuffix(edge.Line))
continue
}
tgt := h.getNodeByIDFromStore(st, edge.Target)
fmt.Fprintf(sb, " - %s --%s--> %s%s\n",
contextPackNodeLabel(docNode, docNode.ID), edge.Kind, contextPackNodeLabel(tgt, edge.Target), contextPackLineSuffix(edge.Line))
}
if len(outgoing) > limit {
fmt.Fprintf(sb, " - ... %d more outgoing references omitted\n", len(outgoing)-limit)
}
}
func (h *handler) appendContextPackImpact(sb *strings.Builder, st *store.Store, docID string, depth, limit int) {
if st == nil || docID == "" {
return
}
levels := h.contextPackImpactLevels(st, docID, depth)
// Pattern 3: batch-load all render-phase node IDs in one query.
var renderIDs []string
renderSeen := make(map[string]bool)
for _, entries := range levels {
for _, entry := range entries {
if !renderSeen[entry.docID] {
renderSeen[entry.docID] = true
renderIDs = append(renderIDs, entry.docID)
}
if entry.via != "" && !renderSeen[entry.via] {
renderSeen[entry.via] = true
renderIDs = append(renderIDs, entry.via)
}
}
}
nodeCache, _ := st.GetNodesByIDs(renderIDs)
if nodeCache == nil {
nodeCache = make(map[string]*store.Node)
}
sb.WriteString("\n### Impacted Documents\n")
total := 0
for level := 1; level <= depth; level++ {
entries := levels[level]
total += len(entries)
fmt.Fprintf(sb, "- **Depth %d:** %d documents\n", level, len(entries))
shown := entries
if len(shown) > limit {
shown = shown[:limit]
}
for _, entry := range shown {
n := nodeCache[entry.docID]
if entry.via != "" {
via := nodeCache[entry.via]
fmt.Fprintf(sb, " - %s via %s through %s\n",
contextPackNodeLabel(n, entry.docID), entry.kind, contextPackNodeLabel(via, entry.via))
} else {
fmt.Fprintf(sb, " - %s via %s\n", contextPackNodeLabel(n, entry.docID), entry.kind)
}
}
if len(entries) > limit {
fmt.Fprintf(sb, " - ... %d more impacted documents omitted\n", len(entries)-limit)
}
}
fmt.Fprintf(sb, "- **Total impacted:** %d\n", total)
}
func (h *handler) contextPackImpactLevels(st *store.Store, startID string, depth int) map[int][]impactEntry {
visited := map[string]bool{startID: true}
queue := []string{startID}
levels := make(map[int][]impactEntry)
for level := 1; level <= depth && len(queue) > 0; level++ {
// Pattern 1: one batch call per level.
edgeMap, err := st.GetIncomingEdgesBatch(queue)
if err != nil {
break
}
// Pattern 2: batch-resolve edge sources for contextPackDocIDFromEdgeSource.
var sources []string
seenSrc := make(map[string]bool)
for _, edges := range edgeMap {
for _, edge := range edges {
if !seenSrc[edge.Source] {
seenSrc[edge.Source] = true
sources = append(sources, edge.Source)
}
}
}
srcNodes, _ := st.GetNodesByIDs(sources)
var next []string
for _, id := range queue {
for _, edge := range edgeMap[id] {
src := contextPackDocIDFromNodeCache(edge.Source, srcNodes)
if visited[src] {
continue
}
visited[src] = true
next = append(next, src)
via := ""
if level > 1 {
via = id
}
levels[level] = append(levels[level], impactEntry{docID: src, kind: edge.Kind, via: via})
}
}
queue = next
}
return levels
}
func contextPackDocID(node store.Node) string {
if node.Kind == "document" {
return node.ID
}
if node.FilePath != "" {
return node.FilePath
}
return node.ID
}
// contextPackDocIDFromNodeCache replicates contextPackDocIDFromEdgeSource using a pre-loaded cache.
func contextPackDocIDFromNodeCache(nodeID string, cache map[string]*store.Node) string {
if n, ok := cache[nodeID]; ok && n != nil {
return contextPackDocID(*n)
}
return nodeID
}
func contextPackNodeLabel(node *store.Node, fallback string) string {
if node == nil {
return fallback
}
if node.FilePath == "" {
return node.Name
}
return fmt.Sprintf("%s (%s)", node.Name, node.FilePath)
}
func contextPackLineSuffix(line int) string {
if line <= 0 {
return ""
}
return fmt.Sprintf(" [line %d]", line)
}
func limitEdges(edges []store.Edge, limit int) []store.Edge {
if limit > 0 && len(edges) > limit {
return edges[:limit]
}
return edges
}
func writeContextPackField(sb *strings.Builder, label, value string) {
value = strings.TrimSpace(value)
if value == "" {
return
}
fmt.Fprintf(sb, "- **%s:** %s\n", label, value)
}
// renderDriftAudit runs the policy/process drift audit and formats findings as
// a Markdown report. In workspace mode it fans out across projects; a non-empty
// projectFilter (the docgraph_context `project` arg) scopes the audit to that one
// project, mirroring how SearchWithOptions scopes the other formats. The task
// label is used for the report header only; findings are not filtered by topic —
// the audit scans all indexed documents within the selected scope.
func (h *handler) renderDriftAudit(task, projectFilter string) string {
var sb strings.Builder
sb.WriteString("# Drift Audit Report\n\n")
if task != "" {
fmt.Fprintf(&sb, "**Context:** %s\n\n", task)
}
sb.WriteString("- **Format:** docgraph.drift_audit.v1\n")
sb.WriteString("- **Packs:** policy_process, assessment_drift, code_doc (when enabled)\n")
if projectFilter != "" {
fmt.Fprintf(&sb, "- **Project scope:** %s\n", projectFilter)
}
sb.WriteString("- **Findings are advisory** — they highlight candidates for human review, not authoritative rulings.\n\n")
opts := store.DriftAuditOpts{}
if h.workspace != nil {
matched := false
for _, p := range h.workspace.Projects {
if projectFilter != "" && p.Name != projectFilter {
continue
}
matched = true
findings, err := p.Store.GetDriftFindings(opts)
if err != nil {
fmt.Fprintf(&sb, "## %s\n\n_Error running drift audit: %v_\n\n", p.Name, err)
continue
}
fmt.Fprintf(&sb, "## %s\n\n", p.Name)
appendDriftFindingsMarkdown(&sb, findings)
}
if projectFilter != "" && !matched {
fmt.Fprintf(&sb, "_No project named %q in this workspace._\n", projectFilter)
}
return sb.String()
}
if h.store == nil {
sb.WriteString("_No store available._\n")
return sb.String()
}
findings, err := h.store.GetDriftFindings(opts)
if err != nil {
fmt.Fprintf(&sb, "_Error running drift audit: %v_\n", err)
return sb.String()
}
appendDriftFindingsMarkdown(&sb, findings)
return sb.String()
}
// appendDriftFindingsMarkdown writes a grouped Markdown section for drift findings.
func appendDriftFindingsMarkdown(sb *strings.Builder, findings []store.DriftFinding) {
if len(findings) == 0 {
sb.WriteString("No drift findings.\n\n")
return
}
stats := store.SummarizeDriftFindings(findings)
fmt.Fprintf(sb, "**Total findings:** %d", stats.TotalFindings)
if e := stats.BySeverity["error"]; e > 0 {
fmt.Fprintf(sb, " | **Errors:** %d", e)
}
if w := stats.BySeverity["warning"]; w > 0 {
fmt.Fprintf(sb, " | **Warnings:** %d", w)
}
sb.WriteString("\n\n")
// Group by code for readability.
codeOrder := []string{
store.CodePolicyConflicting,
store.CodePolicySupersedeReferenced,
store.CodePolicyStaleReview,
store.CodePolicyDuplicate,
store.CodePolicyNonCanonical,
store.CodeResearchSupersededClaim,
store.CodeResearchCompetingInterpretations,
store.CodeResearchStaleAssessment,
store.CodeResearchUnverifiedEvidence,
store.CodeResearchImpactedDeliverable,
store.CodeStaleByGit,
store.CodeCodeMissingSymbol,
store.CodeCodeUndocumentedExport,
store.CodeCodeUnanchoredFeature,
}
byCode := make(map[string][]store.DriftFinding)
for _, f := range findings {
byCode[f.Code] = append(byCode[f.Code], f)
}
// Defensive: append any finding code not in the curated order above (a
// future pack code) so it still renders rather than being silently dropped.
seen := make(map[string]bool)
for _, c := range codeOrder {
seen[c] = true
}
for _, f := range findings {
if !seen[f.Code] {
seen[f.Code] = true
codeOrder = append(codeOrder, f.Code)
}
}
for _, code := range codeOrder {
group := byCode[code]
if len(group) == 0 {
continue
}
fmt.Fprintf(sb, "### `%s` (%d)\n\n", code, len(group))
for _, f := range group {
fmt.Fprintf(sb, "- **%s**", sanitizeDriftField(f.FilePath))
if f.RelatedPath != "" {
fmt.Fprintf(sb, " ↔ %s", sanitizeDriftField(f.RelatedPath))
}
fmt.Fprintf(sb, "\n - %s\n", sanitizeDriftField(f.Message))
if f.Evidence != "" {
fmt.Fprintf(sb, " - Evidence: %s\n", sanitizeDriftField(f.Evidence))
}
if f.Remediation != "" {
fmt.Fprintf(sb, " - Fix: %s\n", sanitizeDriftField(f.Remediation))
}
}
sb.WriteString("\n")
}
}
// sanitizeDriftField neutralizes a drift-finding value before it is rendered into
// the Markdown drift report an LLM consumes. Finding fields carry untrusted
// document-derived content — a file path, or a Message/Evidence that interpolates
// frontmatter (status, owner, claim_id, …). A newline or other control character
// in such a value could otherwise break it out of its bullet line and inject a
// fake "### finding" section, a fake bullet, or pseudo-instructions into the
// report. Control runes (CR/LF/tab/…) and the Unicode line/paragraph separators
// collapse to a single space; ordinary printable content is unchanged, so
// legitimate paths and messages render identically.
func sanitizeDriftField(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsControl(r) || r == '
' || r == '
' {
return ' '
}
return r
}, s)
}