intent-record 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ require_relative "search_index"
2
+ require_relative "search_snippet"
3
+
4
+ module IntentRecord
5
+ # The order a search answers in.
6
+ #
7
+ # Which records match is settled by the substring search, because that is what
8
+ # a person typing `100%` or `ACME-4` is asking for and FTS5 cannot answer it:
9
+ # it tokenises, so it neither sees a substring inside a word nor tells `100%`
10
+ # from `100 percent`. What it can do is say which of the matches the terms are
11
+ # actually about, and that is all it is asked here.
12
+ #
13
+ # bm25 returns a negative score, better the lower, so ascending is best first.
14
+ # A record the index cannot see scores nothing and sorts after every record it
15
+ # can, which is the right place for a hit the terms only touch as a substring.
16
+ module SearchRanking
17
+ TABLE = SearchIndex::TABLE
18
+ ALIAS = "ranking".freeze
19
+
20
+ # Weights per indexed column, in the order the index declares them. A term in
21
+ # a one-line summary is a stronger signal than the same term somewhere in a
22
+ # body that may run for pages, and bm25's own length normalisation does not
23
+ # say so on its own.
24
+ SUMMARY_WEIGHT = 10.0
25
+ BODY_WEIGHT = 1.0
26
+
27
+ # Every ranked list needs a total order, or the rows that tie come back in
28
+ # whatever order the plan produces. Newest first among equals, then id, which
29
+ # is the order `recent` already answers in.
30
+ TIE_BREAK = "intent_records.created_at DESC, intent_records.id DESC".freeze
31
+
32
+ NO_MATCH_SCORE = 0.0
33
+
34
+ # The relevance and the fragment for every record the terms match, computed
35
+ # once for the search and joined to the candidates. Once, not once per
36
+ # record: a correlated subquery measured eight seconds for a common word on
37
+ # twenty-five thousand records, this a quarter of a second. `LIMIT -1` is
38
+ # what keeps SQLite from flattening the subquery into the outer query, where
39
+ # bm25 and snippet lose their context; the plan reports it as MATERIALIZE,
40
+ # and SearchRankingPlanTest holds it there. The `?` is filled by the caller
41
+ # through sanitize_sql_array.
42
+ JOIN = <<~SQL.squish.freeze
43
+ LEFT JOIN (
44
+ SELECT rowid AS indexed_id,
45
+ bm25(#{TABLE}, #{SUMMARY_WEIGHT}, #{BODY_WEIGHT}) AS relevance,
46
+ #{SearchSnippet::EXPRESSION} AS #{SearchSnippet::COLUMN}
47
+ FROM #{TABLE} WHERE #{TABLE} MATCH ? LIMIT -1
48
+ ) AS #{ALIAS} ON #{ALIAS}.indexed_id = intent_records.id
49
+ SQL
50
+
51
+ ORDER = "COALESCE(#{ALIAS}.relevance, #{NO_MATCH_SCORE}) ASC, #{TIE_BREAK}".freeze
52
+
53
+ # What a search selects when it ranks: the record, and the fragment the
54
+ # subquery found for it.
55
+ SELECTION = "intent_records.*, #{ALIAS}.#{SearchSnippet::COLUMN} AS #{SearchSnippet::COLUMN}".freeze
56
+ end
57
+ end
@@ -0,0 +1,52 @@
1
+ require_relative "search_index"
2
+
3
+ module IntentRecord
4
+ # What a search result shows instead of the whole body.
5
+ #
6
+ # A result carries the whole body, which is right for an agent reading one
7
+ # record and wrong for a person scanning two hundred. The index can say which
8
+ # part of a body the terms landed in, and that is the part worth reading first.
9
+ #
10
+ # It can only say it for a record it matched, though, and a term matched as a
11
+ # substring inside a longer word is one it never saw. Those fall back to the
12
+ # opening of the body, so every result carries something rather than leaving a
13
+ # blank in a list where every other row has text.
14
+ module SearchSnippet
15
+ # Roughly a sentence or two either side of the match, which is enough to see
16
+ # why a record answered without turning the list back into a wall of prose.
17
+ INDEX_TOKENS = 20
18
+ FALLBACK_LENGTH = 200
19
+ ELLIPSIS = "…".freeze
20
+
21
+ # Marks are left empty deliberately. A fragment with `[` and `]` around the
22
+ # match reads as punctuation the writer typed, and a fragment with HTML in it
23
+ # is either escaped into visible markup or trusted, and neither is wanted.
24
+ # Column 1 is the body. Evaluated inside SearchRanking's subquery, which is
25
+ # the one place a search still holds the index's context.
26
+ EXPRESSION = "snippet(#{SearchIndex::TABLE}, 1, '', '', '#{ELLIPSIS}', #{INDEX_TOKENS})".freeze
27
+
28
+ COLUMN = "snippet".freeze
29
+
30
+ module_function
31
+
32
+ def text(indexed:, body:)
33
+ return indexed unless indexed.nil? || indexed.empty?
34
+
35
+ shortened(body.to_s)
36
+ end
37
+
38
+ # Cut at the last space where there is one, so the reader is not shown a
39
+ # fragment of a word. Japanese and Chinese prose has no spaces, and neither
40
+ # does a long url or a hash, and a cut that found no space and kept nothing
41
+ # showed an ellipsis on its own.
42
+ def shortened(body)
43
+ return body if body.length <= FALLBACK_LENGTH
44
+
45
+ cut = body[0, FALLBACK_LENGTH]
46
+ at_word = cut.rpartition(/\s/).first.rstrip
47
+ "#{at_word.empty? ? cut : at_word}#{ELLIPSIS}"
48
+ end
49
+
50
+ private_class_method :shortened
51
+ end
52
+ end
@@ -0,0 +1,59 @@
1
+ module IntentRecord
2
+ # One term a person typed, read into the fields it asks about and the text to
3
+ # look for in them.
4
+ #
5
+ # A term may name a field, as in `summary:retry`, for the case where a common
6
+ # word appears everywhere and ranking it first is not enough. The hazard that
7
+ # comes with it is that a ticket uri carries a colon too, so only the known
8
+ # names count as a name and anything else before a colon stays part of the term.
9
+ class SearchTerm
10
+ RECORD_FIELDS = { "summary" => "intent_records.summary", "body" => "intent_records.body" }.freeze
11
+ STAKEHOLDER_FIELDS = { "uri" => "stakeholder_sources.uri", "title" => "stakeholder_sources.title" }.freeze
12
+ FIELDS = RECORD_FIELDS.merge(STAKEHOLDER_FIELDS).freeze
13
+ ALL_FIELDS = FIELDS.values.freeze
14
+
15
+ # Case-insensitive, as every other name the tool takes is.
16
+ NAMED = /\A(#{FIELDS.keys.join("|")}):(.*)\z/mi
17
+
18
+ # Text the index may be trusted with: letters, digits and the spaces between
19
+ # them, with at least one letter or digit to tokenise. Anything else the
20
+ # index would drop, and dropping punctuation is how `100%` comes to match a
21
+ # record that only ever said `100 percent`.
22
+ WORD = /\A(?=.*[[:alnum:]])[[:alnum:][:space:]]+\z/
23
+
24
+ attr_reader :text, :fields, :index_column
25
+
26
+ def self.parse(raw)
27
+ match = NAMED.match(raw)
28
+ return new(text: raw, fields: ALL_FIELDS, index_column: nil) if match.nil?
29
+
30
+ named(match[1].downcase, match[2])
31
+ end
32
+
33
+ # The shell hands `uri: acme-42` over as one word with the space inside,
34
+ # and the space is not part of what is being looked for.
35
+ def self.named(name, text)
36
+ text = text.strip
37
+ raise ValidationError, "#{name}: needs something to look for" if text.empty?
38
+
39
+ new(text: text, fields: [FIELDS.fetch(name)], index_column: RECORD_FIELDS.key?(name) ? name : nil)
40
+ end
41
+
42
+ def initialize(text:, fields:, index_column:)
43
+ @text = text
44
+ @fields = fields
45
+ @index_column = index_column
46
+ # Named a stakeholder field, which the index does not hold. Distinguished
47
+ # from a plain term, where a nil column means every column of the index.
48
+ @off_index = index_column.nil? && fields != ALL_FIELDS
49
+ end
50
+
51
+ # Whether the index may answer this term at all: not for a field it does
52
+ # not hold, and not for text it would read differently from the person.
53
+ def indexable?
54
+ !@off_index && WORD.match?(text)
55
+ end
56
+
57
+ private_class_method :named
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  module IntentRecord
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.2.0".freeze
3
3
  end
@@ -11,7 +11,9 @@
11
11
  · <code class="id"><%= h r["intent_id"] %></code>
12
12
  </div>
13
13
  </header>
14
- <div class="body"><%= h r["body"] %></div>
14
+ <%# A search result carries the fragment its terms landed in; every other
15
+ page shows one record, where the body is what the reader came for. %>
16
+ <div class="body"><%= h(r["snippet"] || r["body"]) %></div>
15
17
  <% if r["asset_versions"].any? %>
16
18
  <div class="links">
17
19
  <span class="label">Commits</span>
@@ -1,5 +1,5 @@
1
1
  <header>
2
2
  <h1>Search: <%= h query %></h1>
3
- <p class="subtitle">Intents whose summary, body, or linked ticket URL or title contain every word</p>
3
+ <p class="subtitle">Intents matching every word, most relevant first</p>
4
4
  </header>
5
5
  <%= intent_cards(intents) %>
data/lib/intent_record.rb CHANGED
@@ -16,6 +16,12 @@ end
16
16
  require "intent_record/config"
17
17
  require "intent_record/database"
18
18
  require "intent_record/global_id"
19
+ require "intent_record/match_expression"
20
+ require "intent_record/search_index"
21
+ require "intent_record/search_ranking"
22
+ require "intent_record/search_term"
23
+ require "intent_record/search_membership"
24
+ require "intent_record/search_snippet"
19
25
  require "intent_record/application_record"
20
26
  require "intent_record/models/vcs_system"
21
27
  require "intent_record/models/asset_version"
@@ -25,4 +31,7 @@ require "intent_record/models/stakeholder_system"
25
31
  require "intent_record/models/stakeholder_source"
26
32
  require "intent_record/models/stakeholder_reference"
27
33
  require "intent_record/models/intent_record_link"
34
+ require "intent_record/blame/spans"
35
+ require "intent_record/blame/porcelain"
36
+ require "intent_record/backfill"
28
37
  require "intent_record/cli"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intent-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik T. Madsen
@@ -107,18 +107,30 @@ files:
107
107
  - LICENSE.txt
108
108
  - README.md
109
109
  - db/migrate/20260918000001_create_initial_schema.rb
110
+ - db/migrate/20260920000001_create_search_index.rb
110
111
  - exe/intent-record
111
112
  - lib/intent_record.rb
112
113
  - lib/intent_record/application_record.rb
113
114
  - lib/intent_record/asset_version_normalizer.rb
114
115
  - lib/intent_record/asset_version_resolver.rb
116
+ - lib/intent_record/backfill.rb
117
+ - lib/intent_record/backfill/backfilled_intent.rb
118
+ - lib/intent_record/backfill/commit_specs.rb
119
+ - lib/intent_record/backfill/inspection.rb
120
+ - lib/intent_record/backfill/predecessor_finder.rb
121
+ - lib/intent_record/backfill/reference_scanner.rb
122
+ - lib/intent_record/blame/porcelain.rb
123
+ - lib/intent_record/blame/spans.rb
115
124
  - lib/intent_record/cli.rb
116
125
  - lib/intent_record/cli/argv_parser.rb
126
+ - lib/intent_record/cli/blame_input.rb
117
127
  - lib/intent_record/cli/dispatch.rb
118
128
  - lib/intent_record/cli/stdin_json.rb
119
129
  - lib/intent_record/cli/streams.rb
120
130
  - lib/intent_record/cli/usage.rb
121
131
  - lib/intent_record/commands/attach.rb
132
+ - lib/intent_record/commands/backfill.rb
133
+ - lib/intent_record/commands/blame.rb
122
134
  - lib/intent_record/commands/by_source.rb
123
135
  - lib/intent_record/commands/lookup.rb
124
136
  - lib/intent_record/commands/recent.rb
@@ -136,6 +148,7 @@ files:
136
148
  - lib/intent_record/linkers/asset_version_specs.rb
137
149
  - lib/intent_record/linkers/intent_linker.rb
138
150
  - lib/intent_record/linkers/stakeholder_linker.rb
151
+ - lib/intent_record/match_expression.rb
139
152
  - lib/intent_record/models/asset_version.rb
140
153
  - lib/intent_record/models/intent_record.rb
141
154
  - lib/intent_record/models/intent_record_asset_version.rb
@@ -144,6 +157,11 @@ files:
144
157
  - lib/intent_record/models/stakeholder_source.rb
145
158
  - lib/intent_record/models/stakeholder_system.rb
146
159
  - lib/intent_record/models/vcs_system.rb
160
+ - lib/intent_record/search_index.rb
161
+ - lib/intent_record/search_membership.rb
162
+ - lib/intent_record/search_ranking.rb
163
+ - lib/intent_record/search_snippet.rb
164
+ - lib/intent_record/search_term.rb
147
165
  - lib/intent_record/seeds.rb
148
166
  - lib/intent_record/sqlite_connection_setup.rb
149
167
  - lib/intent_record/stakeholder_normalizer.rb