Repository navigation
Expand file tree
/
Copy pathnotebook.go
More file actions
758 lines (723 loc) · 23.4 KB
/
Copy pathnotebook.go
File metadata and controls
758 lines (723 loc) · 23.4 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package resident
import (
"context"
"errors"
"fmt"
"path"
"sort"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/Agent-Field/codeaf/internal/guard"
"github.com/Agent-Field/codeaf/internal/plan"
"github.com/Agent-Field/codeaf/internal/store"
)
const (
consolidationInterval = time.Minute
consolidationThreshold = 12 // registry default retained for deterministic fixtures
playbookConsolidationThreshold = 8
consolidationScanLimit = 10_000
consolidationOutputLimit = 8
contractPlaybookBytes = 800
contractPlaybookHeader = "Earned method notes for this territory:\n"
)
// knownToolWords is deliberately a list: adding a tool should not require
// changing cue extraction's control flow.
var knownToolWords = []string{
"sh", "write", "edit", "web", "git", "curl", "python", "pytest", "go", "npm",
"ffmpeg", "fal", "seedance",
}
// WithConsolidator installs the notebook's sleep pass and returns the
// reconciler for chaining. A nil consolidator leaves the notebook untouched.
func (r *Reconciler) WithConsolidator(consolidate ConsolidateFunc) *Reconciler {
r.consolidate = consolidate
return r
}
// ExtractCues turns free text into ordered notebook scopes. Paths lead with
// their nearest repository parents, tools follow, and general scopes close
// every query so stable user and environment facts remain available.
func ExtractCues(text string) []string {
seen := make(map[string]bool)
cues := make([]string, 0, 8)
add := func(cue string) {
key := strings.ToLower(cue)
if cue == "" || seen[key] {
return
}
seen[key] = true
cues = append(cues, cue)
}
paths := make([]string, 0, 2)
for _, field := range strings.Fields(text) {
if filename, ok := cuePath(field); ok {
paths = append(paths, filename)
add("file:" + filename)
}
}
for _, filename := range paths {
dir := path.Dir(filename)
for depth := 0; depth < 2 && dir != "" && dir != "." && dir != "/"; depth++ {
add("repo:" + dir)
next := path.Dir(dir)
if next == dir {
break
}
dir = next
}
}
tools := make(map[string]bool, len(knownToolWords))
for _, tool := range knownToolWords {
tools[tool] = true
}
for _, field := range strings.Fields(text) {
word := strings.ToLower(strings.Trim(field, "\"'`()[]{}<>,.;:!?"))
if tools[word] {
add("tool:" + word)
}
}
add("user")
add("env")
return cues
}
// ContractPlaybook builds the optional earned-doctrine lookup used by the
// contract pass. SearchFacts counts every returned bullet as a real use.
func ContractPlaybook(graph *store.Store) plan.ContractPlaybook {
if graph == nil {
return nil
}
return func(node plan.Node) string {
work := strings.Join([]string{
node.Title,
node.Summary,
strings.Join(node.Sources, "\n"),
node.Brief,
}, "\n")
facts, err := graph.SearchFacts(store.FactQuery{
Cues: ExtractCues(work),
Terms: work,
Kind: store.FactPlaybook,
PreferUseful: true,
MaxBytes: contractPlaybookBytes - len(contractPlaybookHeader),
Limit: consolidationOutputLimit,
})
if err != nil || len(facts) == 0 {
return ""
}
var notes strings.Builder
for _, fact := range facts {
fmt.Fprintf(¬es, "- [%s] %s\n", fact.Scope, fact.Body)
}
return strings.TrimSuffix(notes.String(), "\n")
}
}
// NotebookDigest retrieves the facts relevant to one piece of work and
// renders the bounded block workers receive with their inputs. A non-empty
// nodeID attributes the whole injected batch to that node in one event.
func NotebookDigest(graph *store.Store, nodeID, brief, goal string, limit int) string {
if graph == nil {
return ""
}
work := strings.TrimSpace(strings.TrimSpace(brief) + "\n" + strings.TrimSpace(goal))
facts, err := graph.SearchFacts(store.FactQuery{
Cues: ExtractCues(work),
Terms: work,
Limit: limit,
})
if err != nil || len(facts) == 0 {
return ""
}
if nodeID != "" {
seqs := make([]int64, 0, len(facts))
for _, fact := range facts {
seqs = append(seqs, fact.Seq)
}
_ = graph.RecordFactInjection(nodeID, seqs)
}
facts = statedFirst(facts)
var digest strings.Builder
digest.WriteString("notebook (lessons from earlier work; a line marked \"they told you this\" is the person's own standing instruction and is followed, not weighed; if your own experience in this task contradicts one, trust the experience and state the correction explicitly in your final message — that is how the notebook stays true):\n")
for _, fact := range facts {
if fact.Kind == store.FactUnsettled && fact.Unsettled != nil {
digest.WriteString("- ")
digest.WriteString(store.UnsettledFactFlag)
digest.WriteString(fmt.Sprintf("%d\n", fact.Seq))
digest.WriteString(" ")
digest.WriteString(store.FormatUnsettledPair(*fact.Unsettled))
digest.WriteByte('\n')
continue
}
if fact.Kind == store.FactSkill {
fmt.Fprintf(&digest, "- #%d skill: ", fact.Seq)
} else if fact.Channel == store.FactChannelStated {
fmt.Fprintf(&digest, "- #%d (they told you this) ", fact.Seq)
} else {
fmt.Fprintf(&digest, "- #%d ", fact.Seq)
}
digest.WriteString(fact.Body)
digest.WriteByte('\n')
}
return strings.TrimSuffix(digest.String(), "\n")
}
// statedFirst lifts what the person said in their own voice above what the
// machine inferred, without dropping or reordering anything else.
//
// Retrieval decides what is relevant; it has nothing to say about what carries
// authority, and until now the digest did not either. A lesson taught in the
// thread twenty minutes ago — "always run what you build once before telling me
// it's done" — arrived as line six of eight, in the same typeface as a distilled
// guess about a file layout, and the next job read past it. The channel already
// records which of those two a line is; this is the only place that reads it.
func statedFirst(facts []store.Fact) []store.Fact {
stated := make([]store.Fact, 0, len(facts))
rest := make([]store.Fact, 0, len(facts))
for _, fact := range facts {
if fact.Channel == store.FactChannelStated {
stated = append(stated, fact)
continue
}
rest = append(rest, fact)
}
if len(stated) == 0 || len(rest) == 0 {
return facts
}
return append(stated, rest...)
}
// RetractedBlock renders what the user has explicitly thrown away, for the
// prompts that derive new beliefs.
//
// The store has refused to let a derivation lift a human veto since the day
// RetractedFacts was written, and its own doc comment called this the other
// half — "the lines a derivation prompt can be shown as already-rejected". That
// half was never wired. So the distiller re-proposed a vetoed belief every week
// off the same evidence, the store swallowed it every week, and nobody upstream
// learned anything. Showing the model what was refused is how the refusal
// becomes knowledge instead of a wall it keeps walking into.
func RetractedBlock(graph *store.Store, limit int) string {
if graph == nil {
return ""
}
retracted, err := graph.RetractedFacts(limit)
if err != nil || len(retracted) == 0 {
return ""
}
var block strings.Builder
block.WriteString("Already rejected — the person threw these lines away. Do not write them again in any wording, and do not treat the same evidence as reason to revive them:\n")
now := time.Now()
for _, fact := range retracted {
fmt.Fprintf(&block, "- [%s · %s] %s\n", fact.Scope, store.AgeLabel(fact.Time, now), fact.Body)
}
return strings.TrimSuffix(block.String(), "\n")
}
// consolidateNotebook rewrites at most one overgrown scope. Maintenance is
// best effort: model and store failures leave ordinary reconciliation alone.
// consolidationDue answers the interval question from the journal, not from a
// field. Consolidation retires and rewrites standing beliefs, so a memory-only
// guard meant every restart bought another pass: five restarts in an hour was
// five paid calls and five rounds of belief churn. The retrospective next door
// already had the durable answer; this implementation borrows its shape.
func (r *Reconciler) consolidationDue(now time.Time) bool {
if !r.lastConsolidation.IsZero() {
return now.Sub(r.lastConsolidation) >= consolidationInterval
}
watermark, found, err := r.store.ResidentWatermarkFor(store.LaneConsolidation)
if err != nil {
return true
}
if !found {
return true
}
r.lastConsolidation = watermark.At
return now.Sub(watermark.At) >= consolidationInterval
}
func (r *Reconciler) consolidateNotebook(ctx context.Context) {
now := r.now()
if !r.consolidationDue(now) {
return
}
r.lastConsolidation = now
if _, err := r.store.MarkResidentWatermark(store.LaneConsolidation, 0); err != nil {
// The pass is best effort and so is its watermark: a failed write costs
// one duplicated consolidation after a restart, never the pass itself.
_ = guard.Note("resident/consolidation watermark", err)
}
_, _ = r.store.AgeFactsBounded(now)
if r.consolidate == nil {
return
}
facts, err := r.store.ActiveFacts("", consolidationScanLimit)
if err != nil {
return
}
pendingTrials := make(map[int64]bool)
if stats, err := r.store.TrialStats(); err == nil {
for _, outcome := range stats.Outcomes {
if outcome.Status == store.TrialPending {
pendingTrials[outcome.TrialOf] = true
}
}
}
byScope := make(map[string][]store.Fact)
playbooksByScope := make(map[string]int)
blockedScopes := make(map[string]bool)
var scopeOrder []string
for _, fact := range facts {
if _, seen := byScope[fact.Scope]; !seen {
scopeOrder = append(scopeOrder, fact.Scope)
}
byScope[fact.Scope] = append(byScope[fact.Scope], fact)
if fact.Kind == store.FactPlaybook {
playbooksByScope[fact.Scope]++
}
if pendingTrials[fact.Seq] {
blockedScopes[fact.Scope] = true
}
}
worstScope := ""
worstCount := 0
for _, scope := range scopeOrder {
if blockedScopes[scope] {
continue
}
scoped := byScope[scope]
if len(scoped) <= int(r.store.Parameter(store.ParameterConsolidationThreshold)) &&
playbooksByScope[scope] <= playbookConsolidationThreshold {
continue
}
if count := len(scoped); count > worstCount {
worstScope = scope
worstCount = count
}
}
candidates := scopeAliasCandidates(scopeOrder)
var candidate *ScopePair
if len(candidates) > 0 {
candidate = &candidates[0]
}
if worstScope == "" && candidate == nil {
return
}
originals := byScope[worstScope]
consolidated, err := r.consolidate(ctx, worstScope, originals, candidate)
if err != nil {
return
}
defer r.applyScopeAliasJudgment(candidate, consolidated.ScopeAlias)
if worstScope == "" {
return
}
rewritten := consolidated.Facts
if len(rewritten) > consolidationOutputLimit {
rewritten = rewritten[:consolidationOutputLimit]
}
outcomes, err := r.store.FactOutcomes()
if err != nil {
return
}
originalBySeq := make(map[int64]store.Fact, len(originals))
originalByBody := make(map[string]int64, len(originals))
for _, original := range originals {
originalBySeq[original.Seq] = original
originalByBody[strings.ToLower(strings.TrimSpace(original.Body))] = original.Seq
}
type plannedRewrite struct {
learned Learned
sources []int64
nodeID string
skillSource int64
}
type plannedQuarantine struct {
seq int64
evidenceSeq int64
}
planned := make([]plannedRewrite, 0, len(rewritten))
quarantines := make([]plannedQuarantine, 0)
claimedSources := make(map[int64]bool, len(originals))
seenBodies := make(map[string]bool, len(rewritten))
for _, learned := range rewritten {
quarantineSeqs := uniqueFactSeqs(learned.Quarantines)
for _, quarantineSeq := range quarantineSeqs {
if _, ok := originalBySeq[quarantineSeq]; !ok || claimedSources[quarantineSeq] {
return
}
outcome := outcomes[quarantineSeq]
// One bad job is an anecdote. The store enforces the same pattern bar
// the prompt teaches before accepting a model-requested quarantine.
if outcome.Bad < 2 || outcome.LatestBadSeq == 0 {
return
}
claimedSources[quarantineSeq] = true
quarantines = append(quarantines, plannedQuarantine{
seq: quarantineSeq, evidenceSeq: outcome.LatestBadSeq,
})
}
if learned.Kind == store.FactUnsettled {
if learned.Unsettled == nil || learned.Unsettled.Validate() != nil {
return
}
learned.Body = store.FormatUnsettledPair(*learned.Unsettled)
}
bodyKey := strings.ToLower(strings.TrimSpace(learned.Body))
if bodyKey == "" {
if len(quarantineSeqs) == 0 || len(learned.Sources) > 0 || learned.Replaces > 0 {
return
}
continue
}
if !strings.EqualFold(strings.TrimSpace(learned.Scope), worstScope) || seenBodies[bodyKey] {
return
}
seenBodies[bodyKey] = true
sourceSeqs := append([]int64(nil), learned.Sources...)
if learned.Replaces > 0 {
sourceSeqs = append(sourceSeqs, learned.Replaces)
}
sourceSeqs = uniqueFactSeqs(sourceSeqs)
if len(sourceSeqs) == 0 {
return
}
nodeID := ""
for _, sourceSeq := range sourceSeqs {
original, ok := originalBySeq[sourceSeq]
if !ok || claimedSources[sourceSeq] {
return
}
claimedSources[sourceSeq] = true
if nodeID == "" && original.NodeID != "" {
nodeID = original.NodeID
}
}
var skillSource int64
if learned.Kind == store.FactSkill {
for _, sourceSeq := range sourceSeqs {
source := originalBySeq[sourceSeq]
if source.Kind == store.FactSkill && source.Artifact != "" {
skillSource = source.Seq
break
}
}
if skillSource == 0 {
return
}
}
// RecordFact's ordinary duplicate hygiene may supersede an unchanged
// original while recording. Require that original to belong to this
// output so its automatic event remains the truthful mapping.
if duplicateSeq := originalByBody[bodyKey]; duplicateSeq != 0 && !containsFactSeq(sourceSeqs, duplicateSeq) {
return
}
planned = append(planned, plannedRewrite{learned: learned, sources: sourceSeqs, nodeID: nodeID, skillSource: skillSource})
}
// Mapping validation is all-or-nothing. A malformed model mapping must not
// add rewrites or leave a source attached to an invented replacement.
if len(planned)+len(quarantines) == 0 || len(claimedSources) != len(originals) {
return
}
replacementFor := make(map[int64]int64, len(originals))
for _, rewrite := range planned {
var fact store.Fact
var err error
if rewrite.learned.Kind == store.FactSkill {
fact, err = r.store.RewriteActiveSkillFrom(store.FactWriterDistiller, rewrite.nodeID, rewrite.learned.Scope, clipFactBody(rewrite.learned.Body), rewrite.skillSource)
} else {
fact, err = r.recordLearnedFact(rewrite.nodeID, rewrite.learned)
}
// A rewrite the user has already vetoed is dropped, not fatal, and its
// sources stay unmapped so they stay standing. The swallow used to hand
// back the quarantined row with a nil error, and this loop mapped live
// beliefs onto it — the pass retired good lines in favour of a row that
// no retrieval can ever return.
if errors.Is(err, store.ErrFactVetoed) {
continue
}
if err != nil {
return
}
for _, sourceSeq := range rewrite.sources {
replacementFor[sourceSeq] = fact.Seq
}
}
active, err := r.store.ActiveFacts(worstScope, len(originals)+consolidationOutputLimit)
if err != nil {
return
}
stillActive := make(map[int64]bool, len(active))
for _, fact := range active {
stillActive[fact.Seq] = true
}
for _, original := range originals {
// Recording an unchanged line already supersedes its prior copy. Only
// originals still active need an explicit consolidation event.
if !stillActive[original.Seq] {
continue
}
replacementSeq, mapped := replacementFor[original.Seq]
if !mapped {
continue
}
if err := r.store.SupersedeFact(original.Seq, replacementSeq); err != nil {
return
}
}
for _, quarantine := range quarantines {
if err := r.store.QuarantineFact(quarantine.seq, quarantine.evidenceSeq,
store.FactOriginConsolidator); err != nil {
return
}
}
}
func (r *Reconciler) applyScopeAliasJudgment(candidate *ScopePair, judgment *ScopeAliasJudgment) {
if candidate == nil || judgment == nil || !judgment.Merge {
return
}
canonical := strings.ToLower(strings.TrimSpace(judgment.Canonical))
first := strings.ToLower(strings.TrimSpace(candidate.First))
second := strings.ToLower(strings.TrimSpace(candidate.Second))
from := ""
switch canonical {
case first:
from = second
case second:
from = first
default:
return
}
_ = r.store.AliasScope(from, canonical)
}
type scoredScopePair struct {
ScopePair
score int
}
// scopeAliasCandidates compares only canonical domain, repo, and tool shelves.
// Token normalization makes punctuation and simple inflection differences
// cheap to spot; the model still decides whether similar names mean one thing.
func scopeAliasCandidates(scopes []string) []ScopePair {
unique := make(map[string]bool, len(scopes))
canonical := make([]string, 0, len(scopes))
for _, scope := range scopes {
scope = strings.ToLower(strings.TrimSpace(scope))
if _, _, ok := gardenScopeParts(scope); !ok || unique[scope] {
continue
}
unique[scope] = true
canonical = append(canonical, scope)
}
sort.Strings(canonical)
var scored []scoredScopePair
for first := 0; first < len(canonical); first++ {
firstPrefix, firstTokens, _ := gardenScopeParts(canonical[first])
for second := first + 1; second < len(canonical); second++ {
secondPrefix, secondTokens, _ := gardenScopeParts(canonical[second])
if firstPrefix != secondPrefix {
continue
}
score := normalizedTokenSimilarity(firstTokens, secondTokens)
if score == 0 {
continue
}
scored = append(scored, scoredScopePair{
ScopePair: ScopePair{First: canonical[first], Second: canonical[second]},
score: score,
})
}
}
sort.SliceStable(scored, func(i, j int) bool {
if scored[i].score != scored[j].score {
return scored[i].score > scored[j].score
}
if scored[i].First != scored[j].First {
return scored[i].First < scored[j].First
}
return scored[i].Second < scored[j].Second
})
result := make([]ScopePair, len(scored))
for index := range scored {
result[index] = scored[index].ScopePair
}
return result
}
func gardenScopeParts(scope string) (string, []string, bool) {
for _, prefix := range []string{"domain:", "repo:", "tool:"} {
if !strings.HasPrefix(scope, prefix) {
continue
}
name := strings.TrimSpace(strings.TrimPrefix(scope, prefix))
if name == "" {
return "", nil, false
}
fields := strings.FieldsFunc(name, func(char rune) bool {
return !unicode.IsLetter(char) && !unicode.IsDigit(char)
})
seen := make(map[string]bool, len(fields))
tokens := make([]string, 0, len(fields))
for _, field := range fields {
token := singularScopeToken(strings.ToLower(field))
if token == "" || seen[token] {
continue
}
seen[token] = true
tokens = append(tokens, token)
}
sort.Strings(tokens)
return prefix, tokens, len(tokens) > 0
}
return "", nil, false
}
func singularScopeToken(token string) string {
switch {
case len(token) > 4 && strings.HasSuffix(token, "ies"):
return strings.TrimSuffix(token, "ies") + "y"
case len(token) > 4 && strings.HasSuffix(token, "sses"):
return strings.TrimSuffix(token, "es")
case len(token) > 4 && (strings.HasSuffix(token, "xes") ||
strings.HasSuffix(token, "ches") || strings.HasSuffix(token, "shes") ||
strings.HasSuffix(token, "zes")):
return strings.TrimSuffix(token, "es")
case len(token) > 3 && strings.HasSuffix(token, "s") && !strings.HasSuffix(token, "ss"):
return strings.TrimSuffix(token, "s")
default:
return token
}
}
// normalizedTokenSimilarity is the shelf-name scorer: two scope names mean one
// thing when their tokens agree. The containment shortcut belongs to that job
// and only to that job — "repo:parser" inside "repo:parser-tests" is a genuine
// alias candidate — so it is opt-in per call rather than the default.
func normalizedTokenSimilarity(first, second []string) int {
return tokenSimilarity(first, second, true)
}
// sentenceTokenSimilarity scores two whole sentences. Containment cannot be a
// shortcut here: applied to sentences it made "shorter" score 801 against every
// correction that happened to contain the word "shorter", so one bogus match
// birthed a candidate rule and three of them held the delivery gate to a
// preference the user had stated once, about unrelated work. Two sentences are
// the same correction only when they agree in both directions.
// The caller's floor is the whole bar, which is why no second one is baked in
// here: the shelf-name scorer's built-in 0.6 gate meant every non-zero score it
// could ever return was already 60 or more, so TasteSimilarityFloor's stated
// value of 50 could not discriminate anything and the documented bar was inert.
func sentenceTokenSimilarity(first, second []string) int {
return tokenSimilarity(first, second, false)
}
func tokenSimilarity(first, second []string, shelfNames bool) int {
if len(first) == 0 || len(second) == 0 {
return 0
}
firstSet := make(map[string]bool, len(first))
for _, token := range first {
firstSet[token] = true
}
overlap := 0
for _, token := range second {
if firstSet[token] {
overlap++
}
}
if overlap == len(first) && overlap == len(second) {
return 1000 + overlap
}
union := len(first) + len(second) - overlap
if !shelfNames {
return overlap * 100 / union
}
if overlap == min(len(first), len(second)) {
return 800 + overlap
}
if overlap*5 >= union*3 {
return overlap * 100 / union
}
return 0
}
func uniqueFactSeqs(seqs []int64) []int64 {
seen := make(map[int64]bool, len(seqs))
result := make([]int64, 0, len(seqs))
for _, seq := range seqs {
if seq <= 0 || seen[seq] {
continue
}
seen[seq] = true
result = append(result, seq)
}
return result
}
func containsFactSeq(seqs []int64, want int64) bool {
for _, seq := range seqs {
if seq == want {
return true
}
}
return false
}
func cuePath(field string) (string, bool) {
filename := strings.Trim(field, "\"'`()[]{}<>,;!?")
filename = strings.TrimRight(filename, ".")
if strings.HasPrefix(strings.ToLower(filename), "file:") {
filename = filename[len("file:"):]
}
if fragment := strings.Index(filename, "#L"); fragment >= 0 {
filename = filename[:fragment]
}
filename = trimPosition(filename)
filename = strings.ReplaceAll(filename, "\\", "/")
if filename == "" || strings.Contains(filename, "://") {
return "", false
}
if !strings.Contains(filename, "/") && !looksLikeFilename(filename) {
return "", false
}
filename = path.Clean(filename)
if filename == "." || filename == "/" {
return "", false
}
return filename, true
}
func trimPosition(filename string) string {
for range 2 {
colon := strings.LastIndexByte(filename, ':')
if colon < 0 || colon == len(filename)-1 || !decimal(filename[colon+1:]) {
break
}
filename = filename[:colon]
}
return filename
}
func decimal(value string) bool {
for _, char := range value {
if char < '0' || char > '9' {
return false
}
}
return value != ""
}
func looksLikeFilename(value string) bool {
base := path.Base(value)
if strings.HasPrefix(base, ".") && len(base) > 1 {
return true
}
extension := strings.TrimPrefix(path.Ext(base), ".")
if extension == "" || len(extension) > 12 {
return false
}
hasLetter := false
for _, char := range extension {
switch {
case 'a' <= char && char <= 'z', 'A' <= char && char <= 'Z':
hasLetter = true
case '0' <= char && char <= '9':
default:
return false
}
}
return hasLetter
}
func clipFactBody(body string) string {
body = strings.TrimSpace(body)
if len(body) <= store.MaxFactBytes {
return body
}
cut := store.MaxFactBytes - len("...")
for cut > 0 && !utf8.ValidString(body[:cut]) {
cut--
}
return strings.TrimSpace(body[:cut]) + "..."
}