# Browser-local names: one worker owns the bounded cache; UI polls a RAM mailbox. export def clean [text: string] { $text | str replace --all ' ' ' ' | str replace --all ' ' ' ' | str replace --all --regex '&#(?:[xX][0-9a-fA-F]+|[0-9]+);?' ' ' | str replace --all '&' '&' | str replace --all '<' '<' | str replace --all '>' '>' | str replace --all --regex '\x1b\[[0-?]*[ -/]*[@-~]' '' | str replace --all --regex '[\p{Cc}\p{Cf}\s]+' ' ' | str trim | str substring --grapheme-clusters 0..<80 } # Inspect only structural tokens and tiny discriminator strings, not JSON payloads. def relevant-line [line: string] { let tokens = $line | parse --regex '(?P"(?:\\\\.|[^"\\\\])*"|[{}\\[\\]:,]|[^\\s{}\\[\\]:,]+)' | get token mut depth = 0 mut message = false mut kind = '' mut role = '' for item in ($tokens | enumerate) { let token = $item.item let i = $item.index if $token in ['{' '['] { if $depth == 1 and $i >= 2 and ($tokens | get ($i - 2)) == '"message"' { $message = true } $depth = $depth + 1 } else if $token in ['}' ']'] { if $depth == 2 { $message = false } $depth = $depth - 1 } else if ($i + 2) < ($tokens | length) and ($tokens | get ($i + 1)) == ':' { if $depth == 1 and $token == '"type"' { let value = try { $tokens | get ($i + 2) | from json } catch { null } if ($value | describe) != string { return false } $kind = $value if $kind == session_info { return true } if $kind != message { return false } } else if $depth == 2 and $message and $token == '"role"' { let value = try { $tokens | get ($i + 2) | from json } catch { null } if ($value | describe) != string { return false } $role = $value } if $kind == message and $role != '' { return ($role == user) } } } false } export def read-name [path: path, id: string] { mut name = '' mut first = '' # Regex token filtering handles arbitrary field order without JSON-decoding tools/thinking. for line in (open --raw $path | lines | where {|line| relevant-line $line }) { let entry = try { $line | from json } catch { null } if ($entry | describe) !~ '^record' { continue } if $entry.type? == session_info and ($entry.name? | describe) == string { $name = clean $entry.name } else if $first == '' and $entry.type? == message and $entry.message.role? == user { let content = $entry.message.content? let text = if ($content | describe) == string { $content } else if ($content | describe) =~ '^list|^table' { $content | where {|block| ($block | describe) =~ '^record' and $block.type? == text and ($block.text? | describe) == string } | get -o text | default [] | str join ' ' } else { '' } $first = clean $text } } if $name != '' { $name } else if $first != '' { $first } else { clean ($id | str substring --grapheme-clusters 0..<12) } } # Cache is returned explicitly: caller owns lifetime, eviction, and all writes. export def cached-name [cache: list, row: record] { let stat = try { ls --all $row.path | first } catch { null } if $stat == null { return {name: (clean ($row.id | str substring --grapheme-clusters 0..<12)) cache: $cache hit: false} } let hit = $cache | where {|item| $item.path == $row.path and $item.modified == $stat.modified and $item.size == $stat.size } if ($hit | is-not-empty) { return {name: $hit.0.name cache: $cache hit: true} } let name = try { read-name $row.path $row.id } catch { clean ($row.id | str substring --grapheme-clusters 0..<12) } let updated = $cache | where path != $row.path | append {path: $row.path modified: $stat.modified size: $stat.size name: $name} | last 256 {name: $name cache: $updated hit: false} } export def mailbox-read [table: string, key: string] { stor open | query db $"SELECT seq, data FROM ($table) WHERE key = '($key)'" | first } export def mailbox-write [table: string, key: string, seq: int, data: any] { stor update --table-name $table --where-clause $"key = '($key)'" --update-record {seq: $seq data: ($data | to json --raw)} | ignore } export def start [] { let table = 'pi_names_' + (random uuid | str replace --all '-' '') stor create --table-name $table --columns {key: str seq: int data: str} | ignore for key in [view selected ack result] { stor insert --table-name $table --data-record {key: $key seq: 0 data: 'null'} | ignore } let worker = job spawn { mut cache = [] mut version = 0 mut rows = [] mut queue = [] mut done = [] mut selected = '' mut dirty = false mut serial = 0 loop { let incoming = stor open | query db $"SELECT seq FROM ($table) WHERE key = 'view'" | first | get seq if $incoming != $version { $version = $incoming $rows = (mailbox-read $table view | get data | from json) $queue = ($rows | first 32 | get -o path | default []) $done = [] $selected = '' $dirty = false } let priority = mailbox-read $table selected | get data | from json if $priority != null and $priority != $selected { $selected = $priority if $priority not-in $done { $queue = ($queue | where {|path| $path != $priority } | prepend $priority) } } for path in ($queue | first 1) { let matches = $rows | where path == $path if ($matches | is-not-empty) { let result = cached-name $cache $matches.0 $cache = $result.cache $rows = ($rows | each {|row| if $row.path == $path { $row | update session ((if $row.parentOutsideScope { '↳ ' } else { '' }) + $result.name) } else { $row } }) $dirty = true } $done = ($done | append $path) $queue = ($queue | where {|item| $item != $path }) } let ack = mailbox-read $table ack | get seq if $dirty and $ack == $serial { $serial = $serial + 1 mailbox-write $table result $serial {version: $version rows: $rows} $dirty = false } sleep 30ms } } {table: $table worker: $worker} } export def stop [browser: record] { try { job kill $browser.worker } try { stor delete --table-name $browser.table | ignore } } export def refresh [table: string, version: int, state: record] { let path = $state.values.sessions?.row?.path? | default $state.selected?.path? let previous = mailbox-read $table selected | get data | from json if $path != null and $path != $previous { mailbox-write $table selected 0 $path } # Full replacements preserve index, not identity: freeze filter membership while navigating. let query = $state.values.query? | default '' if $query != '' and $state.focused != query { return null } let seq = stor open | query db $"SELECT seq FROM ($table) WHERE key = 'result'" | first | get seq if $seq == (mailbox-read $table ack | get seq) { return null } let result = mailbox-read $table result | get data | from json mailbox-write $table ack $seq null if $result.version == $version { $result.rows } else { null } }