lanternkb-ui 0.1.0__tar.gz

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,17 @@
1
+ .DS_Store
2
+ .venv/
3
+ __pycache__/
4
+ *.egg-info/
5
+ dist/
6
+
7
+ # never commit indexes, configs or personal data
8
+ *.db
9
+ *.db-wal
10
+ *.db-shm
11
+ config.toml
12
+ data/
13
+ runs/
14
+ .env*
15
+
16
+ # third-party tool installs for the bake-off
17
+ node_modules/
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ lanternkb
2
+ Copyright 2026 ankitb073
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
+ Third-party code and data it ships or downloads are listed in THIRD_PARTY_NOTICES.md.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.5
2
+ Name: lanternkb-ui
3
+ Version: 0.1.0
4
+ Summary: Optional settings page for lanternkb: runs only on demand, on loopback, and exits when closed.
5
+ Author: ankitb073
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ License-File: NOTICE
9
+ Requires-Python: >=3.12
10
+ Requires-Dist: lanternkb==0.1.0
11
+ Description-Content-Type: text/markdown
12
+
13
+ # lanternkb-ui
14
+
15
+ The optional settings page for [lanternkb](https://github.com/ankitb073/lanternkb): folders and tiers, features,
16
+ search in the browser, and sync progress. It runs only while you use it (`lanternkb ui`), listens on loopback only,
17
+ and exits when closed. It uses the Python standard library and nothing else.
18
+
19
+ Install it with lanternkb: `uv tool install "lanternkb[embed,ui]"`. Licence: Apache-2.0.
@@ -0,0 +1,7 @@
1
+ # lanternkb-ui
2
+
3
+ The optional settings page for [lanternkb](https://github.com/ankitb073/lanternkb): folders and tiers, features,
4
+ search in the browser, and sync progress. It runs only while you use it (`lanternkb ui`), listens on loopback only,
5
+ and exits when closed. It uses the Python standard library and nothing else.
6
+
7
+ Install it with lanternkb: `uv tool install "lanternkb[embed,ui]"`. Licence: Apache-2.0.
@@ -0,0 +1,20 @@
1
+ [project]
2
+ name = "lanternkb-ui"
3
+ version = "0.1.0"
4
+ description = "Optional settings page for lanternkb: runs only on demand, on loopback, and exits when closed."
5
+ readme = "README.md"
6
+ license = "Apache-2.0" # lanternkb decision 0028
7
+ license-files = ["LICENSE", "NOTICE"]
8
+ requires-python = ">=3.12"
9
+ authors = [{ name = "ankitb073" }]
10
+ dependencies = ["lanternkb==0.1.0"] # and nothing else: the page uses the standard library only. Released together, so pinned
11
+
12
+ [build-system]
13
+ requires = ["hatchling"]
14
+ build-backend = "hatchling.build"
15
+
16
+ [tool.hatch.build.targets.wheel]
17
+ packages = ["src/lanternkb_ui"]
18
+
19
+ [tool.uv.sources]
20
+ lanternkb = { workspace = true }
@@ -0,0 +1 @@
1
+ """lanternkb-ui: the optional, on-demand settings page (decision 0007)."""
@@ -0,0 +1,627 @@
1
+ """The settings page server: on demand, loopback only, gone when the tab closes.
2
+
3
+ Threat model T10 (docs/threat-model.md) and decision 0007:
4
+ - **Loopback only, random port, random path.** The server binds 127.0.0.1 and serves
5
+ under a random path prefix; the session cookie is scoped to that path, so other servers
6
+ on 127.0.0.1 (cookies ignore ports) never receive it.
7
+ - **A one-time token** in the opening link is exchanged for the session cookie (HttpOnly,
8
+ SameSite=Strict), then refused if used again. The link reaches the browser through a
9
+ private file (mode 600 in a folder only the user can enter), never through a command line
10
+ other users could read.
11
+ - **Host, Origin and Sec-Fetch-Site checks** on every request (against DNS rebinding and
12
+ cross-site requests), and a per-session form token on every change. The referrer policy
13
+ is `same-origin`, so browsers send the page's real Origin with its forms.
14
+ - **Every change goes through `lanternkb.control`,** which validates it, writes the config
15
+ atomically and records it in the audit log.
16
+ - **Strict headers:** a Content-Security-Policy with per-response nonces, no framing, no
17
+ caching, no cross-origin access. Nothing is logged, and errors never reach the terminal.
18
+ - **It exits** when its last open page says goodbye or stops its heartbeat, or after 10
19
+ minutes without a user action.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import hmac
25
+ import http.server
26
+ import json
27
+ import logging
28
+ import os
29
+ import secrets
30
+ import shutil
31
+ import subprocess
32
+ import sys
33
+ import tempfile
34
+ import threading
35
+ import time
36
+ import urllib.parse
37
+ import webbrowser
38
+ from dataclasses import dataclass, field
39
+ from html import escape
40
+ from http import HTTPStatus
41
+ from pathlib import Path
42
+ from typing import Any
43
+
44
+ from lanternkb.control import Control, ControlError
45
+ from lanternkb_ui import page
46
+
47
+ IDLE_S = 600 # exit after this long without a user action
48
+ BEAT_GRACE_S = 150 # a page whose heartbeat stopped this long is gone (hidden tabs beat slowly)
49
+ BYE_GRACE_S = 3 # after the last page says goodbye, wait this long for a reload
50
+ LOGIN_WINDOW_S = 300 # the opening link works this long
51
+ MAX_BODY = 64_000
52
+
53
+
54
+ # A looser tier needs a fresh read on an open worker first (review 11), so say so.
55
+ OPEN_LATER = " Its files become open after the next sync reads them again."
56
+
57
+
58
+ SEARCH_K = 20 # results on the search page
59
+ RESULTS_KEPT = 20 # searches whose results the page can show again (back, reload)
60
+ RESULTS_KEPT_S = 1800.0
61
+ SEARCH_TIMEOUT_S = 120.0 # the first search may load the models
62
+
63
+
64
+ @dataclass
65
+ class State:
66
+ control: Control
67
+ port: int = 0
68
+ prefix: str = field(default_factory=lambda: "/" + secrets.token_urlsafe(12) + "/")
69
+ token: str = field(default_factory=lambda: secrets.token_urlsafe(32))
70
+ token_used: bool = False
71
+ started: float = field(default_factory=time.monotonic)
72
+ session: str | None = None
73
+ csrf: str = field(default_factory=lambda: secrets.token_urlsafe(24))
74
+ searcher: Searcher | None = None # made on the first search
75
+ results: dict[str, tuple[float, dict[str, Any]]] = field(default_factory=dict)
76
+ last_action: float = field(default_factory=time.monotonic)
77
+ pages: dict[str, float] = field(default_factory=dict) # page id -> last heartbeat
78
+ seen_page: bool = False # a page has beaten at least once
79
+ bye_at: float | None = None
80
+ flash: list[tuple[str, str]] = field(default_factory=list) # (kind, message)
81
+ sync: dict[str, Any] = field(default_factory=lambda: {"running": False})
82
+ lock: threading.Lock = field(default_factory=threading.Lock)
83
+
84
+ def keep(self, shown: dict[str, Any]) -> str:
85
+ """Keep a search's results for the page that shows them; returns their handle."""
86
+ handle = secrets.token_urlsafe(16)
87
+ now = time.monotonic()
88
+ with self.lock:
89
+ for h, (at, _) in list(self.results.items()):
90
+ if now - at > RESULTS_KEPT_S:
91
+ del self.results[h]
92
+ while len(self.results) >= RESULTS_KEPT:
93
+ del self.results[next(iter(self.results))] # the oldest
94
+ self.results[handle] = (now, shown)
95
+ return handle
96
+
97
+ def result(self, handle: str) -> dict[str, Any] | None:
98
+ with self.lock:
99
+ kept = self.results.get(handle)
100
+ if kept is None or time.monotonic() - kept[0] > RESULTS_KEPT_S:
101
+ return None
102
+ return kept[1]
103
+
104
+ def hosts(self) -> set[str]:
105
+ return {f"127.0.0.1:{self.port}", f"localhost:{self.port}"}
106
+
107
+ def origins(self) -> set[str]:
108
+ return {f"http://{h}" for h in self.hosts()}
109
+
110
+ @property
111
+ def cookie(self) -> str:
112
+ return f"lk_session_{self.port}"
113
+
114
+
115
+ class Handler(http.server.BaseHTTPRequestHandler):
116
+ server_version = "lanternkb-ui"
117
+ sys_version = ""
118
+ timeout = 10 # an idle connection cannot hold a thread
119
+ state: State # set on the subclass made by Server
120
+
121
+ def log_message(self, format: str, *args: Any) -> None: # noqa: A002 - stdlib signature
122
+ pass # never log: request lines carry the token and paths
123
+
124
+ # -- checks -------------------------------------------------------------------------------
125
+
126
+ def _host_ok(self) -> bool:
127
+ return self.headers.get("Host", "") in self.state.hosts()
128
+
129
+ def _session_ok(self) -> bool:
130
+ session = self.state.session
131
+ if session is None:
132
+ return False
133
+ for part in self.headers.get("Cookie", "").split(";"):
134
+ name, _, value = part.strip().partition("=")
135
+ if name == self.state.cookie and hmac.compare_digest(
136
+ value.encode("utf-8", "replace"), session.encode()
137
+ ):
138
+ return True
139
+ return False
140
+
141
+ def _site_ok(self) -> bool:
142
+ """Browsers say where a request comes from; only this page itself, or the user
143
+ typing or following the link (`none`), may reach us."""
144
+ site = self.headers.get("Sec-Fetch-Site")
145
+ return site is None or site in ("same-origin", "none")
146
+
147
+ def _guard(self, write: bool) -> bool:
148
+ """Host and site for everything; session after login; Origin for changes."""
149
+ ok = self._host_ok() and self._site_ok() and self._session_ok()
150
+ if ok and write:
151
+ ok = self.headers.get("Origin", "") in self.state.origins()
152
+ if not ok:
153
+ self._send(HTTPStatus.FORBIDDEN, page.forbidden(), "text/html")
154
+ return ok
155
+
156
+ def _route(self) -> tuple[str, dict[str, list[str]]] | None:
157
+ """The path inside the app's random prefix, or None (not found)."""
158
+ url = urllib.parse.urlsplit(self.path)
159
+ if not url.path.startswith(self.state.prefix):
160
+ return None
161
+ return url.path[len(self.state.prefix) :], urllib.parse.parse_qs(url.query)
162
+
163
+ # -- responses ----------------------------------------------------------------------------
164
+
165
+ def _send(self, status: HTTPStatus, body: str, ctype: str, nonce: str = "",
166
+ extra: dict[str, str] | None = None) -> None: # fmt: skip
167
+ data = body.encode()
168
+ self.send_response(status)
169
+ self.send_header("Content-Type", f"{ctype}; charset=utf-8")
170
+ self.send_header("Content-Length", str(len(data)))
171
+ script = f"'nonce-{nonce}'" if nonce else "'none'"
172
+ self.send_header("Content-Security-Policy",
173
+ f"default-src 'none'; script-src {script}; style-src {script}; "
174
+ "connect-src 'self'; form-action 'self'; base-uri 'none'; "
175
+ "frame-ancestors 'none'") # fmt: skip
176
+ self.send_header("X-Frame-Options", "DENY")
177
+ self.send_header("X-Content-Type-Options", "nosniff")
178
+ # same-origin, not no-referrer: with no-referrer browsers send `Origin: null` on
179
+ # the page's own form posts, which the Origin check must refuse.
180
+ self.send_header("Referrer-Policy", "same-origin")
181
+ self.send_header("Cache-Control", "no-store")
182
+ self.send_header("Cross-Origin-Opener-Policy", "same-origin")
183
+ self.send_header("Cross-Origin-Resource-Policy", "same-origin")
184
+ for k, v in (extra or {}).items():
185
+ self.send_header(k, v)
186
+ self.end_headers()
187
+ if self.command != "HEAD":
188
+ self.wfile.write(data)
189
+
190
+ def _redirect(self, location: str, extra: dict[str, str] | None = None) -> None:
191
+ self.send_response(HTTPStatus.SEE_OTHER)
192
+ self.send_header("Location", location)
193
+ self.send_header("Content-Length", "0")
194
+ self.send_header("Cache-Control", "no-store")
195
+ self.send_header("Referrer-Policy", "same-origin")
196
+ for k, v in (extra or {}).items():
197
+ self.send_header(k, v)
198
+ self.end_headers()
199
+
200
+ def _render(self, html_fn: Any, **kw: Any) -> None:
201
+ nonce = secrets.token_urlsafe(16)
202
+ with self.state.lock:
203
+ flash, self.state.flash = self.state.flash, []
204
+ body = html_fn(nonce=nonce, csrf=self.state.csrf, flash=flash, **kw)
205
+ self._send(HTTPStatus.OK, body, "text/html", nonce)
206
+
207
+ # -- GET ----------------------------------------------------------------------------------
208
+
209
+ def do_GET(self) -> None: # noqa: N802 - stdlib name
210
+ route = self._route()
211
+ if route is None:
212
+ return self._send(HTTPStatus.NOT_FOUND, page.not_found(), "text/html")
213
+ path, query = route
214
+ if path == "start":
215
+ return self._login(query.get("t", [""])[0])
216
+ if not self._guard(write=False):
217
+ return None
218
+ if path != "sync/status":
219
+ self.state.last_action = time.monotonic()
220
+ c = self.state.control
221
+ if path == "":
222
+ return self._render(page.main, data=_gather(c), sync=dict(self.state.sync))
223
+ if path == "purge":
224
+ try:
225
+ plan = c.purge(query.get("feature", [""])[0], confirm=False)
226
+ except (ControlError, ValueError, OSError) as e:
227
+ self._flash("error", _message(e))
228
+ return self._redirect(self.state.prefix)
229
+ return self._render(page.purge_preview, plan=plan)
230
+ if path == "sync/status":
231
+ return self._send(HTTPStatus.OK, json.dumps(self.state.sync), "application/json")
232
+ if path == "search": # the results of a search just sent (Post/Redirect/Get)
233
+ kept = self.state.result(query.get("r", [""])[0][:64])
234
+ searcher = self.state.searcher
235
+ if kept is None or searcher is None:
236
+ self._flash("error", "That search has expired; search again.")
237
+ return self._redirect(self.state.prefix)
238
+ # what today's config still shows (a hand edit of the config since the search)
239
+ still = searcher.still([r["path"] for r in kept["results"]])
240
+ kept = dict(kept, results=[r for r in kept["results"] if r["path"] in still])
241
+ return self._render(page.search, reveal=REVEAL_LABEL if _revealer() else "",
242
+ **kept) # fmt: skip
243
+ return self._send(HTTPStatus.NOT_FOUND, page.not_found(), "text/html")
244
+
245
+ def _login(self, token: str) -> None:
246
+ s = self.state
247
+ fresh = time.monotonic() - s.started < LOGIN_WINDOW_S
248
+ good = hmac.compare_digest(token.encode("utf-8", "replace"), s.token.encode())
249
+ if not (self._host_ok() and self._site_ok()) or s.token_used or not fresh or not good:
250
+ return self._send(HTTPStatus.FORBIDDEN, page.forbidden(), "text/html")
251
+ s.token_used = True
252
+ s.session = secrets.token_urlsafe(32)
253
+ s.last_action = time.monotonic()
254
+ cookie = f"{s.cookie}={s.session}; HttpOnly; SameSite=Strict; Path={s.prefix}"
255
+ return self._redirect(s.prefix, {"Set-Cookie": cookie})
256
+
257
+ # -- POST ---------------------------------------------------------------------------------
258
+
259
+ def do_POST(self) -> None: # noqa: N802 - stdlib name
260
+ route = self._route()
261
+ if route is None:
262
+ return self._send(HTTPStatus.NOT_FOUND, page.not_found(), "text/html")
263
+ path, _query = route
264
+ if not self._guard(write=True):
265
+ return None
266
+ try:
267
+ length = int(self.headers.get("Content-Length") or 0)
268
+ except ValueError:
269
+ length = -1
270
+ if not 0 <= length <= MAX_BODY:
271
+ return self._send(HTTPStatus.REQUEST_ENTITY_TOO_LARGE, page.forbidden(), "text/html")
272
+ raw = self.rfile.read(length).decode("utf-8", "replace")
273
+ form = {k: v[0] for k, v in urllib.parse.parse_qs(raw, keep_blank_values=True).items()}
274
+ if path in ("beat", "bye"): # page liveness, not user actions
275
+ self._liveness(path, form.get("page", "")[:64])
276
+ return self._send(HTTPStatus.NO_CONTENT, "", "text/plain")
277
+ if not hmac.compare_digest(form.get("csrf", "").encode(), self.state.csrf.encode()):
278
+ return self._send(HTTPStatus.FORBIDDEN, page.forbidden(), "text/html")
279
+ self.state.last_action = time.monotonic()
280
+ if path == "search":
281
+ return self._search(form)
282
+ if path == "reveal":
283
+ return self._reveal(form)
284
+ with self.state.lock: # a change can hide or purge what kept results show (review 26)
285
+ self.state.results.clear()
286
+ try:
287
+ self._act(path, form)
288
+ except (ControlError, ValueError, OSError) as e:
289
+ self._flash("error", _message(e))
290
+ return self._redirect(self.state.prefix)
291
+
292
+ def _search(self, form: dict[str, str]) -> None:
293
+ s = self.state
294
+ with s.lock:
295
+ if s.searcher is None:
296
+ s.searcher = Searcher(s.control.config_path)
297
+ searcher = s.searcher
298
+ t0 = time.monotonic()
299
+ results, error, note = searcher.search(form)
300
+ took = int((time.monotonic() - t0) * 1000)
301
+ s.last_action = time.monotonic() # a slow first search is not idle time
302
+ # Post/Redirect/Get (review 26): the results stay here under a random handle and the
303
+ # browser is sent to fetch them, so its history and session hold no form body (the
304
+ # query) and going back to the results is an ordinary page load.
305
+ form = {k: form.get(k, "") for k in ("q", "types", "since", "before")}
306
+ handle = s.keep({"form": form, "results": results, "error": error, "note": note,
307
+ "took_ms": took}) # fmt: skip
308
+ return self._redirect(f"{s.prefix}search?r={handle}")
309
+
310
+ def _reveal(self, form: dict[str, str]) -> None:
311
+ """Show a search result's file in the file manager (never open or run it). Only a
312
+ result id is accepted, and only a file today's config still shows is revealed."""
313
+ searcher = self.state.searcher
314
+ path = (searcher.locate(form.get("id", ""), form.get("path", ""))
315
+ if searcher is not None else None) # fmt: skip
316
+ if path is None or not _show(path):
317
+ return self._send(HTTPStatus.NOT_FOUND, "not available", "text/plain")
318
+ return self._send(HTTPStatus.NO_CONTENT, "", "text/plain")
319
+
320
+ def _liveness(self, what: str, page_id: str) -> None:
321
+ s = self.state
322
+ with s.lock:
323
+ if what == "beat" and page_id:
324
+ s.pages[page_id] = time.monotonic()
325
+ s.seen_page = True
326
+ s.bye_at = None
327
+ elif what == "bye":
328
+ s.pages.pop(page_id, None)
329
+ if not s.pages:
330
+ s.bye_at = time.monotonic()
331
+
332
+ def _act(self, path: str, form: dict[str, str]) -> None:
333
+ c = self.state.control
334
+ feature, tier = form.get("feature", ""), form.get("tier", "")
335
+ if path == "feature/enable":
336
+ c.enable(feature)
337
+ self._flash("ok", f"{feature} is on. Its files are searchable again; a sync reads "
338
+ "any that changed.") # fmt: skip
339
+ elif path == "feature/disable":
340
+ c.disable(feature)
341
+ self._flash("ok", f"{feature} is off. Its data is hidden now; nothing was deleted.")
342
+ elif path == "feature/tier":
343
+ c.set_feature_tier(feature, tier)
344
+ self._flash("ok", f"{feature} is now {tier}." + (OPEN_LATER if tier == "open" else ""))
345
+ elif path == "feature/purge":
346
+ if form.get("confirm") != "yes":
347
+ raise ControlError("purge needs the confirmation box ticked")
348
+ plan = c.purge(feature, confirm=True)
349
+ self._flash("ok", f"Purged {feature}: {plan.files} files and {plan.chunks} chunks "
350
+ "deleted securely.") # fmt: skip
351
+ elif path == "root/add":
352
+ raw = form.get("path", "").strip()
353
+ if not (raw.startswith("/") or raw.startswith("~")):
354
+ raise ControlError("type the folder's full path (starting with / or ~)")
355
+ c.add_root(raw, tier or "local-only")
356
+ added = Path(os.path.expanduser(raw)).resolve()
357
+ self._flash("ok", f"Added {added} as {tier or 'local-only'}. Run a sync to index "
358
+ "it.") # fmt: skip
359
+ elif path == "root/tier":
360
+ c.set_root_tier(form.get("path", ""), tier)
361
+ self._flash("ok", f"Folder is now {tier}." + (OPEN_LATER if tier == "open" else ""))
362
+ elif path == "root/remove":
363
+ if form.get("confirm") != "yes":
364
+ raise ControlError("removing a folder needs the confirmation box ticked")
365
+ try:
366
+ n = c.remove_root(form.get("path", ""), force=form.get("force") == "yes")
367
+ except ControlError as e:
368
+ if "--force" in str(e):
369
+ hint = ", or tick 'even if files become more visible'"
370
+ raise ControlError(str(e).replace(", or use --force", hint)) from e
371
+ raise
372
+ self._flash("ok", f"Folder removed; {n} indexed file records deleted.")
373
+ elif path == "sync":
374
+ self._start_sync()
375
+ else:
376
+ raise ControlError("unknown action")
377
+
378
+ def _flash(self, kind: str, message: str) -> None:
379
+ with self.state.lock:
380
+ self.state.flash.append((kind, message))
381
+
382
+ def _start_sync(self) -> None:
383
+ s = self.state
384
+ with s.lock:
385
+ if s.sync.get("running"):
386
+ return
387
+ s.sync = {"running": True, "started": time.time()}
388
+
389
+ def progress(stage: str, done: int, total: int) -> None:
390
+ with s.lock:
391
+ if s.sync.get("running"):
392
+ s.sync.update(stage=stage, done=done, total=total)
393
+
394
+ def run() -> None:
395
+ try:
396
+ r = s.control.sync(progress)
397
+ note = "; ".join(x for x in [*r.warnings, r.embed_note] if x)
398
+ result = {"ok": True, "seen": r.seen, "indexed": r.indexed,
399
+ "unchanged": r.unchanged, "removed": r.removed,
400
+ "skipped": sum(r.skipped.values()), "errors": r.errors,
401
+ "embedded": r.embedded, "pending": r.pending,
402
+ "note": note} # fmt: skip
403
+ except Exception as e: # noqa: BLE001 - shown on the page
404
+ result = {"ok": False, "error": _message(e)}
405
+ with s.lock:
406
+ s.sync = {"running": False, "finished": time.time(), "result": result}
407
+ s.results.clear() # a sync can remove or change what kept results show
408
+
409
+ threading.Thread(target=run, daemon=True).start()
410
+
411
+
412
+ def _message(e: BaseException) -> str:
413
+ """What the page may say about an error: control messages in full, others by type."""
414
+ return str(e) if isinstance(e, ControlError) else f"that did not work ({type(e).__name__})"
415
+
416
+
417
+ REVEAL_LABEL = "Show in Finder" if sys.platform == "darwin" else "Show in folder"
418
+
419
+
420
+ def _revealer() -> list[str] | None:
421
+ """The command that shows a file in the file manager here, if there is one."""
422
+ if sys.platform == "darwin":
423
+ return ["/usr/bin/open", "-R"]
424
+ opener = shutil.which("xdg-open")
425
+ return [opener] if opener else None
426
+
427
+
428
+ def _show(path: str) -> bool:
429
+ """Show the file (its folder, on Linux) in the file manager; True if that started."""
430
+ cmd = _revealer()
431
+ target = path if sys.platform == "darwin" else os.path.dirname(path)
432
+ if cmd is None or not os.path.isabs(path) or not os.path.isfile(path):
433
+ return False
434
+ if sys.platform != "darwin" and not os.path.isdir(target):
435
+ return False
436
+ try:
437
+ subprocess.run([*cmd, target], timeout=10, check=True, stdin=subprocess.DEVNULL,
438
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # fmt: skip
439
+ except (OSError, subprocess.SubprocessError):
440
+ return False
441
+ return True
442
+
443
+
444
+ class Searcher:
445
+ """The search page's searches, run by the MCP server's own search on one thread: the
446
+ index connection stays on that thread, the models load on the first search and unload
447
+ when idle, and today's config decides what may be shown, as `lanternkb serve` does.
448
+ Every tier, except inside Claude Code, where it shows open files only, as the terminal
449
+ does (docs/threat-model.md: a speed bump, not a wall)."""
450
+
451
+ def __init__(self, config_path: Path) -> None:
452
+ from concurrent.futures import ThreadPoolExecutor
453
+
454
+ from lanternkb.server import Server as Core
455
+ from lanternkb.tiers import ServerTier
456
+
457
+ self.open_only = bool(os.environ.get("CLAUDECODE"))
458
+ self._pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="lanternkb-search")
459
+ self._lock = threading.Lock()
460
+ self._pending: set[Any] = set()
461
+ self._core = Core(config_path, ServerTier.OPEN if self.open_only else ServerTier.ALL)
462
+
463
+ def search(self, form: dict[str, str]) -> tuple[list[dict[str, Any]], str, str]:
464
+ """(results, error, note) for the form's query and filters."""
465
+ from lanternkb.server import ToolError
466
+
467
+ args = {"query": form.get("q", ""), "k": SEARCH_K, "types": form.get("types", ""),
468
+ "modified_after": form.get("since", "").strip(),
469
+ "modified_before": form.get("before", "").strip()} # fmt: skip
470
+ if not args["query"].strip():
471
+ return [], "Type something to search for.", ""
472
+ fut = None
473
+ try:
474
+ fut = self._submit(self._core.tool_search, args)
475
+ out = fut.result(SEARCH_TIMEOUT_S)
476
+ except ToolError as e:
477
+ return [], str(e), ""
478
+ except TimeoutError:
479
+ if fut is not None:
480
+ fut.cancel() # not run later if it has not started (review 26)
481
+ return [], "The search took too long; try again in a moment.", ""
482
+ except Exception as e: # noqa: BLE001 - shown on the page, never a traceback
483
+ return [], f"the search did not work ({type(e).__name__})", ""
484
+ notes = [out["note"]] if out.get("note") else []
485
+ if self.open_only:
486
+ notes.append("inside Claude Code: open files only")
487
+ return out["results"], "", "; ".join(notes)
488
+
489
+ def locate(self, result_id: str, path: str) -> str | None:
490
+ """The file of a search result, if today's config still shows it. The path is
491
+ checked too: the same text at the same place in two folders has one result id
492
+ (review 26)."""
493
+ fut = None
494
+ try:
495
+ fut = self._submit(self._core.find_chunk, result_id[:40], path[:4096])
496
+ hit = fut.result(SEARCH_TIMEOUT_S)
497
+ except Exception: # noqa: BLE001 - no such result, a timeout, a closed index
498
+ if fut is not None:
499
+ fut.cancel()
500
+ return None
501
+ return str(hit.path) if hit is not None else None
502
+
503
+ def still(self, paths: list[str]) -> set[str]:
504
+ """Those of `paths` today's config still shows (none if that cannot be checked)."""
505
+ try:
506
+ fut = self._submit(lambda: {p for p in paths if self._core._still_visible(p)})
507
+ return set(fut.result(SEARCH_TIMEOUT_S))
508
+ except Exception: # noqa: BLE001 - shown as no results, never as an error page
509
+ return set()
510
+
511
+ def _submit(self, fn: Any, *args: Any) -> Any:
512
+ with self._lock:
513
+ self._pending = {f for f in self._pending if not f.done()}
514
+ fut = self._pool.submit(fn, *args)
515
+ self._pending.add(fut)
516
+ return fut
517
+
518
+ def close(self) -> None:
519
+ with self._lock:
520
+ for fut in self._pending:
521
+ fut.cancel() # queued searches never start
522
+ self._pool.submit(self._core.close)
523
+ self._pool.shutdown(wait=False)
524
+
525
+
526
+ def _gather(c: Control) -> dict[str, Any]:
527
+ """Everything the page shows, read through the control API; a read error is shown, not
528
+ raised."""
529
+ out: dict[str, Any] = {"config": str(c.config_path), "status": {}, "features": [],
530
+ "roots": (), "skipped": [], "notes": [], "error": None} # fmt: skip
531
+ try:
532
+ out["status"] = c.status()
533
+ out["features"] = c.features()
534
+ out["roots"] = c.roots()
535
+ out["skipped"] = c.skipped()
536
+ out["notes"] = c.notes()
537
+ except (ControlError, ValueError, OSError) as e:
538
+ out["error"] = _message(e)
539
+ return out
540
+
541
+
542
+ class _HTTPServer(http.server.ThreadingHTTPServer):
543
+ daemon_threads = True
544
+ request_queue_size = 64
545
+
546
+ def handle_error(self, request: Any, client_address: Any) -> None:
547
+ pass # never print tracebacks (they could carry paths or config text)
548
+
549
+
550
+ class Server:
551
+ """A running settings page. `url` opens it once; `wait()` returns when it has exited."""
552
+
553
+ def __init__(self, control: Control, idle_s: float = IDLE_S):
554
+ self.state = State(control)
555
+ handler = type("BoundHandler", (Handler,), {"state": self.state})
556
+ self.httpd = _HTTPServer(("127.0.0.1", 0), handler)
557
+ self.state.port = self.httpd.server_address[1]
558
+ self.idle_s = idle_s
559
+ self._stopped = threading.Event()
560
+ self._link_dir: str | None = None
561
+
562
+ @property
563
+ def url(self) -> str:
564
+ return f"http://127.0.0.1:{self.state.port}{self.state.prefix}start?t={self.state.token}"
565
+
566
+ def link_file(self) -> Path:
567
+ """A private file that forwards the browser to the one-time link, so the link never
568
+ appears on a command line (`ps`) when the browser is started."""
569
+ self._link_dir = tempfile.mkdtemp(prefix="lanternkb-ui-") # mode 700
570
+ path = Path(self._link_dir) / "open.html"
571
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
572
+ with os.fdopen(fd, "w") as f:
573
+ f.write('<!doctype html><meta charset="utf-8"><meta name="referrer" '
574
+ f'content="no-referrer"><meta http-equiv="refresh" content="0; url='
575
+ f'{escape(self.url, quote=True)}"><title>lanternkb</title>') # fmt: skip
576
+ return path
577
+
578
+ def _watch(self) -> None:
579
+ s = self.state
580
+ while not self._stopped.wait(0.5):
581
+ now = time.monotonic()
582
+ with s.lock:
583
+ for pid, beat in list(s.pages.items()):
584
+ if now - beat > BEAT_GRACE_S:
585
+ del s.pages[pid]
586
+ lapsed = s.seen_page and not s.pages and s.bye_at is None # beats stopped
587
+ said_bye = s.bye_at is not None and now - s.bye_at > BYE_GRACE_S
588
+ idle = now - s.last_action > self.idle_s
589
+ if s.token_used and self._link_dir:
590
+ shutil.rmtree(self._link_dir, ignore_errors=True)
591
+ self._link_dir = None
592
+ if (idle or said_bye or lapsed) and not s.sync.get("running"):
593
+ self.stop()
594
+
595
+ def start(self) -> None:
596
+ threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
597
+ threading.Thread(target=self._watch, daemon=True).start()
598
+
599
+ def stop(self) -> None:
600
+ if not self._stopped.is_set():
601
+ self._stopped.set()
602
+ threading.Thread(target=self.httpd.shutdown, daemon=True).start()
603
+
604
+ def wait(self) -> None:
605
+ self._stopped.wait()
606
+ self.httpd.server_close()
607
+ if self.state.searcher is not None:
608
+ self.state.searcher.close()
609
+ if self._link_dir:
610
+ shutil.rmtree(self._link_dir, ignore_errors=True)
611
+
612
+
613
+ def run(config_path: Path, open_browser: bool = True, idle_s: float = IDLE_S) -> int:
614
+ """`lanternkb ui`: serve the page until it is closed, then exit."""
615
+ # lanternkb's warnings (exception names only) stay off the terminal: no handler there
616
+ logging.getLogger("lanternkb").addHandler(logging.NullHandler())
617
+ server = Server(Control(config_path), idle_s=idle_s)
618
+ server.start()
619
+ print(f"Settings page (this link works once): {server.url}", flush=True)
620
+ print("It closes with the tab, or after 10 idle minutes.", flush=True)
621
+ if open_browser:
622
+ webbrowser.open(server.link_file().as_uri())
623
+ try:
624
+ server.wait()
625
+ except KeyboardInterrupt:
626
+ server.stop()
627
+ return 0
@@ -0,0 +1,418 @@
1
+ """HTML for the settings page, generated from feature manifests (no per-feature UI code).
2
+
3
+ Every value that comes from the index, the config or a manifest is escaped: file names,
4
+ reasons and third-party manifest text are untrusted. The only script is inline, carries
5
+ the response's CSP nonce, and does five things: a heartbeat, a goodbye when the tab
6
+ closes, sync progress, a search's progress, and showing a result's file without leaving
7
+ the page.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import os
13
+ import re
14
+ from collections import defaultdict
15
+ from html import escape
16
+ from typing import Any
17
+ from urllib.parse import quote
18
+
19
+ from lanternkb.plugins import trust_note
20
+
21
+ CSS = """
22
+ :root { color-scheme: light dark; --bg:#fbfaf7; --card:#fff; --ink:#1d1d1f; --mute:#6e6e73;
23
+ --line:#e5e3dd; --accent:#b4651e; --ok:#2e7d32; --err:#b3261e; --chip:#f1efe9; }
24
+ @media (prefers-color-scheme: dark) { :root { --bg:#151514; --card:#1f1f1d; --ink:#ecebe7;
25
+ --mute:#a1a09b; --line:#33322f; --accent:#e39a50; --ok:#7bc47f; --err:#f28b82;
26
+ --chip:#2a2927; } }
27
+ * { box-sizing: border-box; }
28
+ body { margin:0; font:15px/1.5 -apple-system, system-ui, sans-serif; background:var(--bg);
29
+ color:var(--ink); }
30
+ main { max-width: 980px; margin: 0 auto; padding: 24px 16px 64px; }
31
+ h1 { font-size: 22px; margin: 0 0 4px; } h2 { font-size: 17px; margin: 28px 0 10px; }
32
+ .sub { color: var(--mute); margin: 0 0 16px; font-size: 13px; word-break: break-all; }
33
+ .card { background: var(--card); border: 1px solid var(--line); border-radius: 12px;
34
+ padding: 14px 16px; margin: 10px 0; }
35
+ .row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
36
+ .grow { flex: 1 1 260px; min-width: 0; }
37
+ .chip { display: inline-block; background: var(--chip); border-radius: 999px;
38
+ padding: 1px 9px; font-size: 12px; color: var(--mute); margin: 2px 4px 2px 0; }
39
+ .on { color: var(--ok); } .off { color: var(--mute); }
40
+ .chip.warn, small.warn { color: var(--err); }
41
+ .stat { display: inline-block; margin-right: 18px; } .stat b { font-size: 18px; }
42
+ .flash { padding: 10px 14px; border-radius: 10px; margin: 8px 0; }
43
+ .flash.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); }
44
+ .flash.error { background: color-mix(in srgb, var(--err) 16%, transparent); }
45
+ button, select, input[type=text] { font: inherit; border-radius: 8px; border: 1px solid
46
+ var(--line); background: var(--card); color: var(--ink); padding: 5px 11px; }
47
+ button { cursor: pointer; } button.primary { background: var(--accent); color: #fff;
48
+ border-color: var(--accent); } button.danger { color: var(--err); }
49
+ form { display: inline; margin: 0; } input[type=text] { width: 100%; }
50
+ table { width: 100%; border-collapse: collapse; font-size: 13px; }
51
+ td, th { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line);
52
+ vertical-align: top; word-break: break-all; }
53
+ th { color: var(--mute); font-weight: 600; } small, .mute { color: var(--mute); }
54
+ details summary { cursor: pointer; color: var(--mute); }
55
+ form.search { display: block; } form.search .row { flex-wrap: nowrap; gap: 8px; }
56
+ input[type=search] { font: inherit; border-radius: 8px; border: 1px solid var(--line);
57
+ background: var(--card); color: var(--ink); padding: 5px 11px; flex: 1 1 auto;
58
+ min-width: 0; }
59
+ form.search button { flex: none; white-space: nowrap; }
60
+ .filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
61
+ gap: 8px; margin-top: 10px; max-width: 560px; }
62
+ .filters label { display: flex; flex-direction: column; gap: 2px; font-size: 12px;
63
+ color: var(--mute); } .filters input[type=text] { font-size: 14px; padding: 4px 9px; }
64
+ p.snippet { margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
65
+ .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden;
66
+ clip: rect(0 0 0 0); white-space: nowrap; }
67
+ mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit;
68
+ border-radius: 3px; padding: 0 1px; } form.reveal button { font-size: 13px;
69
+ padding: 2px 9px; }
70
+ a { color: var(--accent); } .controls { display: flex; gap: 6px; flex-wrap: wrap;
71
+ align-items: center; } .card, .grow, small { overflow-wrap: anywhere; }
72
+ """
73
+
74
+ SCRIPT = """
75
+ const pageId = crypto.randomUUID();
76
+ const body = () => new URLSearchParams({page: pageId});
77
+ const closed = () => {
78
+ const b = document.getElementById('closed');
79
+ if (b) b.hidden = false;
80
+ };
81
+ const beat = () => fetch('beat', {method: 'POST', body: body()})
82
+ .then(r => { if (!r.ok) closed(); }).catch(closed);
83
+ beat(); setInterval(beat, 10000);
84
+ addEventListener('pagehide', () => navigator.sendBeacon('bye', body()));
85
+ const say = (b, text) => {
86
+ b.textContent = text;
87
+ const a = document.getElementById('announce');
88
+ if (a) a.textContent = (b.getAttribute('aria-label') || '') + ': ' + text;
89
+ };
90
+ document.querySelectorAll('form.reveal').forEach(f => f.addEventListener('submit', e => {
91
+ e.preventDefault();
92
+ const b = f.querySelector('button');
93
+ fetch('reveal', {method: 'POST', body: new URLSearchParams(new FormData(f))})
94
+ .then(r => { say(b, r.ok ? 'Shown' : 'Not available'); })
95
+ .catch(() => { say(b, 'Not available'); });
96
+ }));
97
+ const sf = document.querySelector('form.search');
98
+ if (sf) sf.addEventListener('submit', () => {
99
+ sf.querySelector('button').textContent = 'Searching...';
100
+ });
101
+ const syncing = document.getElementById('syncing');
102
+ if (syncing) {
103
+ const poll = async () => {
104
+ try {
105
+ const s = await (await fetch('sync/status')).json();
106
+ if (!s.running) { location.reload(); return; }
107
+ const stages = {checking: 'checking files', reading: 'reading changed files',
108
+ embedding: 'embedding passages'};
109
+ const at = s.stage ? ': ' + stages[s.stage] + ' ' + s.done.toLocaleString() +
110
+ (s.total ? ' of ' + s.total.toLocaleString() : '') : '';
111
+ syncing.textContent = 'Sync running for ' +
112
+ Math.round(Date.now() / 1000 - s.started) + ' s' + (at || '...');
113
+ } catch (e) {}
114
+ setTimeout(poll, 1000);
115
+ };
116
+ poll();
117
+ }
118
+ """
119
+
120
+
121
+ def _e(value: Any) -> str:
122
+ return escape(str(value), quote=True)
123
+
124
+
125
+ CATEGORY_ORDER = ("documents", "code", "images", "audio", "search", "safety")
126
+
127
+
128
+ def _n(count: int, word: str) -> str:
129
+ return f"{count:,} {word}{'' if count == 1 else 's'}"
130
+
131
+
132
+ def _size(n: int) -> str:
133
+ for unit in ("bytes", "KB", "MB", "GB"):
134
+ if n < 1024 or unit == "GB":
135
+ return f"{n:,.0f} {unit}" if unit == "bytes" else f"{n:,.1f} {unit}"
136
+ n /= 1024 # type: ignore[assignment]
137
+ return str(n)
138
+
139
+
140
+ def _shell(title: str, body: str, nonce: str = "") -> str:
141
+ style = f'<style nonce="{_e(nonce)}">{CSS}</style>' if nonce else ""
142
+ script = f'<script nonce="{_e(nonce)}">{SCRIPT}</script>' if nonce else ""
143
+ return (f'<!doctype html><html lang="en"><head><meta charset="utf-8">'
144
+ f'<meta name="viewport" content="width=device-width, initial-scale=1">'
145
+ f"<title>{_e(title)}</title>{style}</head><body><main>{body}</main>"
146
+ f"{script}</body></html>") # fmt: skip
147
+
148
+
149
+ def _form(action: str, csrf: str, fields: str, label: str, cls: str = "") -> str:
150
+ return (f'<form method="post" action="{_e(action.lstrip("/"))}">'
151
+ f'<input type="hidden" name="csrf" value="{_e(csrf)}">{fields}'
152
+ f'<button class="{_e(cls)}" type="submit">{_e(label)}</button></form>') # fmt: skip
153
+
154
+
155
+ def _hidden(name: str, value: str) -> str:
156
+ return f'<input type="hidden" name="{_e(name)}" value="{_e(value)}">'
157
+
158
+
159
+ def _tier_select(current: str, what: str = "") -> str:
160
+ """The tier choice; `what` names the folder or feature for screen readers, which
161
+ otherwise hear a row of identical "tier" lists."""
162
+ opts = "".join(f'<option value="{t}"{" selected" if t == current else ""}>{t}</option>'
163
+ for t in ("open", "local-only")) # fmt: skip
164
+ label = f"tier for {what}" if what else "tier"
165
+ return f'<select name="tier" aria-label="{_e(label)}">{opts}</select>'
166
+
167
+
168
+ def _flash(flash: list[tuple[str, str]]) -> str:
169
+ closed = ('<div id="closed" class="flash error" role="alert" hidden>This settings page has '
170
+ "closed. Run <code>lanternkb ui</code> again to reopen it.</div>") # fmt: skip
171
+ return closed + "".join(f'<div class="flash {_e(k)}" role="status">{_e(m)}</div>'
172
+ for k, m in flash) # fmt: skip
173
+
174
+
175
+ def _search_form(csrf: str, q: str = "", types: str = "", since: str = "",
176
+ before: str = "") -> str: # fmt: skip
177
+ """The search box. It is sent by POST and answered with a redirect to the results under
178
+ a random handle (Post/Redirect/Get), so a query never enters the page's address, title,
179
+ history or saved session (which may sync to a cloud account); autocomplete is off, which
180
+ also keeps typed text out of the browser's saved form state."""
181
+
182
+ def box(name: str, value: str, label: str, hint: str) -> str:
183
+ return (f'<label>{_e(label)}<input type="text" name="{name}" value="{_e(value)}" '
184
+ f'placeholder="{_e(hint)}" autocomplete="off" spellcheck="false">'
185
+ "</label>") # fmt: skip
186
+
187
+ return ('<form method="post" action="search" class="card search" role="search">'
188
+ f'<input type="hidden" name="csrf" value="{_e(csrf)}"><div class="row">'
189
+ f'<input type="search" name="q" value="{_e(q)}" required maxlength="2000" '
190
+ 'aria-label="search your files" placeholder="Search your files, or * for the latest" '
191
+ 'autocomplete="off" spellcheck="false">'
192
+ '<button class="primary" type="submit">Search</button></div>'
193
+ '<div class="filters">' + box("types", types, "File types", "pdf, md")
194
+ + box("since", since, "Changed since", "30d")
195
+ + box("before", before, "Changed before", "2026-06-30") + "</div></form>") # fmt: skip
196
+
197
+
198
+ def _home(path: str) -> str:
199
+ home = os.path.expanduser("~").rstrip("/")
200
+ return "~" + path[len(home) :] if home and path.startswith(home + "/") else path
201
+
202
+
203
+ def _marked(snippet: str, query: str) -> str:
204
+ """A snippet, escaped, with the query's words marked where a word starts with them."""
205
+ from lanternkb.search import match_spans
206
+
207
+ text = re.sub(r"\n\s*\n", "\n", snippet.strip())
208
+ out, k = [], 0
209
+ for a, b in match_spans(text, query):
210
+ out += [_e(text[k:a]), f"<mark>{_e(text[a:b])}</mark>"]
211
+ k = b
212
+ return "".join(out) + _e(text[k:])
213
+
214
+
215
+ def search(nonce: str, csrf: str, flash: list[tuple[str, str]], form: dict[str, str],
216
+ results: list[dict[str, Any]], error: str, note: str, took_ms: int,
217
+ reveal: str = "") -> str: # fmt: skip
218
+ """Search results. The title never carries the query (browsers keep titles in their
219
+ history too). `reveal` labels a button that shows a result's file in the file manager."""
220
+ box = _search_form(csrf, *(form.get(k, "") for k in ("q", "types", "since", "before")))
221
+ parts = ['<h1>Search</h1><p class="sub"><a href="./">Back to settings</a></p>',
222
+ _flash(flash), box] # fmt: skip
223
+ if error:
224
+ parts.append(f'<div class="flash error" role="alert">{_e(error)}</div>')
225
+ else:
226
+ found = _n(len(results), "result") if results else "No results"
227
+ parts.append(f'<p class="mute" role="status">{found} in {took_ms} ms'
228
+ f"{' · ' + _e(note) if note else ''}</p>") # fmt: skip
229
+ for r in results:
230
+ folder, _, name = r["path"].rpartition("/")
231
+ label = reveal.replace(" in ", f" {name} in ", 1) # "Show notes.md in Finder"
232
+ show = (f'<form method="post" action="reveal" class="reveal">{_hidden("csrf", csrf)}'
233
+ f'{_hidden("id", r["id"])}{_hidden("path", r["path"])}'
234
+ f'<button type="submit" aria-label="{_e(label)}">{_e(reveal)}</button></form>'
235
+ if reveal else "") # fmt: skip
236
+ text = _marked(r["snippet"], form.get("q", ""))
237
+ parts.append(
238
+ f'<article class="card hit"><div class="row"><b class="grow">{_e(name)}</b>'
239
+ f'<span class="chip">{_e(r["tier"])}</span>{show}</div>'
240
+ f'<small>{_e(_home(folder))}/ · {_e(r["location"])}'
241
+ f'{" · " + _e(r["modified"][:16].replace("T", " ")) if r.get("modified") else ""}'
242
+ f'</small><p class="snippet">{text}</p></article>') # fmt: skip
243
+ parts.append('<p id="announce" class="sr-only" aria-live="polite"></p>')
244
+ return _shell("lanternkb search", "".join(parts), nonce)
245
+
246
+
247
+ def main(nonce: str, csrf: str, flash: list[tuple[str, str]], data: dict[str, Any],
248
+ sync: dict[str, Any]) -> str: # fmt: skip
249
+ st = data["status"]
250
+ parts = [f"<h1>lanternkb settings</h1><p class=\"sub\">{_e(data['config'])}</p>",
251
+ _flash(flash), _search_form(csrf)] # fmt: skip
252
+ if data.get("error"):
253
+ parts.append(f'<div class="flash error" role="alert">The index or config could not be '
254
+ f"read: {_e(data['error'])}. Run <code>lanternkb doctor</code>."
255
+ "</div>") # fmt: skip
256
+ # status and sync
257
+ by_tier = st.get("by_tier", {}) if isinstance(st, dict) else {}
258
+ stats = "".join(f'<span class="stat"><b>{_e(v)}</b> {_e(k)}</span>'
259
+ for k, v in (("files indexed", st.get("indexed", 0)),
260
+ ("open", by_tier.get("open", 0)),
261
+ ("local-only", by_tier.get("local-only", 0)),
262
+ ("chunks", st.get("chunks", 0)),
263
+ ("skipped", sum(st.get("skipped", {}).values())))
264
+ ) # fmt: skip
265
+ emb = st.get("embeddings", {})
266
+ emb_parts = []
267
+ for key, info in emb.items() if isinstance(emb, dict) else []:
268
+ if isinstance(info, dict):
269
+ done = "complete" if info.get("complete") else f"{info.get('pending', 0):,} pending"
270
+ emb_parts.append(f"{_e(key)}: {_e(done)}")
271
+ emb_line = "; ".join(emb_parts)
272
+ if sync.get("running"):
273
+ sync_ui = '<span id="syncing" class="mute">Sync running...</span>'
274
+ else:
275
+ sync_ui = _form("/sync", csrf, "", "Sync now", "primary")
276
+ result = sync.get("result")
277
+ if result and result.get("ok"):
278
+ sync_ui += (f' <span class="mute">Last: {result["indexed"]} indexed, '
279
+ f'{result["unchanged"]} unchanged, {result["removed"]} removed, '
280
+ f'{result["errors"]} errors; {result["pending"]} chunks waiting for '
281
+ f"embeddings.</span>") # fmt: skip
282
+ elif result:
283
+ sync_ui += f' <span class="flash error">Sync failed: {_e(result.get("error"))}</span>'
284
+ parts.append(
285
+ f'<div class="card"><div class="row"><div class="grow">{stats}</div>'
286
+ f"<div>{sync_ui}</div></div>"
287
+ f'<small>Last sync: {_e(st.get("last_sync") or "never")}'
288
+ f"{' · semantic search: ' + emb_line if emb_line else ''}"
289
+ f"{' · old pages wait in the WAL until the next sync' if st.get('wal_pending') else ''}"
290
+ f"</small></div>"
291
+ ) # fmt: skip
292
+ # features, grouped by category (generated from manifests)
293
+ groups: dict[str, list[Any]] = defaultdict(list)
294
+ for f in data["features"]:
295
+ groups[f.manifest.category].append(f)
296
+ parts.append("<h2>Features</h2>")
297
+ order = sorted(groups, key=lambda c: (CATEGORY_ORDER.index(c) if c in CATEGORY_ORDER
298
+ else len(CATEGORY_ORDER), c)) # fmt: skip
299
+ for category in order:
300
+ parts.append(f'<h3 class="mute">{_e(category.title())}</h3>')
301
+ for f in groups[category]:
302
+ parts.append(_feature_card(f, csrf))
303
+ # roots
304
+ parts.append("<h2>Folders</h2>")
305
+ for r in data["roots"]:
306
+ path = str(r.path)
307
+ parts.append(
308
+ f'<div class="card"><div class="row"><div class="grow"><b>{_e(path)}</b></div>'
309
+ + _form("/root/tier", csrf, _hidden("path", path) + _tier_select(r.tier.value, path),
310
+ "Set tier")
311
+ + "</div><details><summary>Remove this folder</summary>"
312
+ + _form("/root/remove", csrf, _hidden("path", path)
313
+ + '<label><input type="checkbox" name="confirm" value="yes" required> '
314
+ "Delete everything indexed from this folder</label> "
315
+ + '<label><input type="checkbox" name="force" value="yes"> even if files '
316
+ "become more visible (it sits inside an open folder)</label> ",
317
+ "Remove", "danger")
318
+ + "</details></div>"
319
+ ) # fmt: skip
320
+ parts.append(
321
+ '<div class="card"><div class="row">'
322
+ + _form("/root/add", csrf,
323
+ '<span class="grow"><input type="text" name="path" required autocomplete="off" '
324
+ 'placeholder="/full/path/to/a/folder or ~/folder" '
325
+ 'aria-label="folder path"></span> '
326
+ + _tier_select("local-only", "the new folder") + " ", "Add folder")
327
+ + '</div><small>New folders are local-only unless you choose open. Open files can '
328
+ "reach cloud assistants.</small></div>"
329
+ ) # fmt: skip
330
+ # skipped files and notes
331
+ skipped = data["skipped"]
332
+ rows = "".join(f"<tr><td>{_e(p)}</td><td>{_e(r)}</td></tr>" for p, r in skipped[:300])
333
+ more = f'<p class="mute">and {len(skipped) - 300} more</p>' if len(skipped) > 300 else ""
334
+ table = (f'<div class="card"><table><tr><th>File</th><th>Why</th></tr>{rows}</table>'
335
+ f"{more}</div>") # fmt: skip
336
+ parts.append(f"<h2>Skipped files ({len(skipped)})</h2>"
337
+ + (table if skipped else '<p class="mute">None.</p>')) # fmt: skip
338
+ if data["notes"]:
339
+ rows = "".join(f"<tr><td>{_e(p)}</td><td>{_e(n)}</td></tr>" for p, n in data["notes"][:300])
340
+ parts.append(f'<h2>Partly read files</h2><div class="card"><table>{rows}</table></div>')
341
+ return _shell("lanternkb settings", "".join(parts), nonce)
342
+
343
+
344
+ def _feature_card(f: Any, csrf: str) -> str:
345
+ m = f.manifest
346
+ caps = "".join(f'<span class="chip">reads {_e(r)}</span>' for r in m.reads)
347
+ caps += "".join(f'<span class="chip">needs {_e(p)}</span>' for p in m.os_permissions)
348
+ note = trust_note(m.id)
349
+ in_process = note is not None and m.kind != "extractor"
350
+ if in_process: # its declarations are not enforced: do not show them as if they were
351
+ caps = '<span class="chip warn">runs with your privileges</span>'
352
+ else:
353
+ caps += '<span class="chip">no network</span>'
354
+ if note:
355
+ caps += f'<br><small class="{"warn" if in_process else "mute"}">This is {_e(note)}</small>'
356
+ state = '<b class="on">On</b>' if f.enabled else '<b class="off">Off</b>'
357
+ pending = "" if f.applied else ' <span class="chip">applies at the next sync</span>'
358
+ if m.kind == "embedder": # its data is vectors; their tier is each file's own
359
+ counts = f"{_n(f.chunks, 'vector')}, {_size(f.bytes)}"
360
+ elif m.kind == "extractor":
361
+ counts = (f"{_n(f.files, 'file')}, {_n(f.chunks, 'chunk')}, {_size(f.bytes)}"
362
+ + (f", {f.skipped:,} skipped" if f.skipped else "")) # fmt: skip
363
+ else:
364
+ counts = ""
365
+ if m.required:
366
+ controls = '<span class="chip">required</span>'
367
+ elif m.kind == "embedder":
368
+ action = "/feature/disable" if f.enabled else "/feature/enable"
369
+ label = "Turn off" if f.enabled else "Turn on"
370
+ toggle = _form(action, csrf, _hidden("feature", m.id), label)
371
+ controls = f'{toggle} <a href="purge?feature={quote(m.id, safe="")}">Delete vectors...</a>'
372
+ else:
373
+ action = "/feature/disable" if f.enabled else "/feature/enable"
374
+ label = "Turn off" if f.enabled else "Turn on"
375
+ toggle = _form(action, csrf, _hidden("feature", m.id), label)
376
+ tier = _form("/feature/tier", csrf,
377
+ _hidden("feature", m.id) + _tier_select(f.tier.value, m.title),
378
+ "Set tier") # fmt: skip
379
+ purge = f'<a href="purge?feature={quote(m.id, safe="")}">Purge data...</a>'
380
+ controls = f"{toggle} {tier} {purge}"
381
+ return (f'<div class="card"><div class="row"><div class="grow"><b>{_e(m.title)}</b> '
382
+ f"{state}{pending}<br><small>{_e(m.description)}</small><br>{caps}<br>"
383
+ f'<small>{_e(counts)}</small></div><div class="controls">{controls}</div></div>'
384
+ "</div>") # fmt: skip
385
+
386
+
387
+ def purge_preview(nonce: str, csrf: str, flash: list[tuple[str, str]], plan: Any) -> str:
388
+ if plan.feature.startswith("embedder."):
389
+ what = (f"<b>{_n(plan.chunks, 'vector')}</b> ({_e(_size(plan.bytes))}) made by this "
390
+ "embedder. Search uses keywords until a sync embeds again "
391
+ "(if it is on).") # fmt: skip
392
+ body = (f"<h1>Delete the vectors of {_e(plan.feature)}?</h1>{_flash(flash)}"
393
+ f'<div class="card"><p>This deletes, securely and for good, {what}</p>'
394
+ + _form("/feature/purge", csrf, _hidden("feature", plan.feature)
395
+ + '<label><input type="checkbox" name="confirm" value="yes" required> '
396
+ "Yes, delete them</label> ", "Delete", "danger")
397
+ + ' <a href=".">Cancel</a></div>') # fmt: skip
398
+ return _shell(f"Delete vectors of {plan.feature}", body, nonce)
399
+ body = (f"<h1>Purge {_e(plan.feature)}?</h1>{_flash(flash)}"
400
+ f'<div class="card"><p>This deletes, securely and for good, everything indexed by '
401
+ f"this feature: <b>{plan.files:,}</b> files, <b>{plan.skipped:,}</b> skipped-file "
402
+ f"records, <b>{plan.chunks:,}</b> chunks ({_e(_size(plan.bytes))} of text), and "
403
+ f"their vectors. The files on disk are not touched; a later sync with the feature "
404
+ f"on would index them again.</p>"
405
+ + _form("/feature/purge", csrf, _hidden("feature", plan.feature)
406
+ + '<label><input type="checkbox" name="confirm" value="yes" required> '
407
+ "Yes, delete it</label> ", "Purge", "danger")
408
+ + ' <a href=".">Cancel</a></div>') # fmt: skip
409
+ return _shell(f"Purge {plan.feature}", body, nonce)
410
+
411
+
412
+ def forbidden() -> str:
413
+ return _shell("Not allowed", "<h1>Not allowed</h1><p>Open the settings page from the link "
414
+ "that <code>lanternkb ui</code> printed. Each link works once.</p>") # fmt: skip
415
+
416
+
417
+ def not_found() -> str:
418
+ return _shell("Not found", "<h1>Not found</h1>")