A directory the model knows about, and @ to name a file in it

An agent chat used to open with the model knowing the name of a machine and
nothing about what was on it, so the first two rounds of every reply went on
finding out. It now gets a listing: one read-only command, `git ls-files` where
that works and `find` otherwise, falling back to an SFTP walk that always does.
git first because a repository already carries somebody's considered list of
what is not part of the project, and reproducing it by hand is how an index
ends up mostly build output.

The listing is budgeted rather than dumped. A tree of a thousand files is worse
than no tree -- it costs the window on every request forever and buries the four
names that mattered -- so directories that will not fit are shown as a count and
the model is told to open one itself. Collapsing picks the deepest and largest
first: by saving alone it would take `src/` before `src/web/static/vendor/`,
because it contains it, and lose every name worth having.

Read from a cache and never fetched. `harness.context_variables` is synchronous
and sits on the request path; the walk happens in the generation setup, which is
async and already doing network work, with a short wait. A chat whose first
reply outruns its first walk simply has no listing that turn and the fragment
disappears rather than appearing as an empty heading.

Then `@`, over the same index and over the library, and `/` for commands with an
Alt-based keyboard for the same jobs. A mentioned file arrives as contents, not
a reference -- a small model asked to call file_read often does not bother -- and
it arrives with its absolute path and the machine it came from, because a model
handed `main.py` cannot tell which of four it is and cannot name it back when
asked to change something.

The rule that matters for `/`: a message that merely starts with a slash still
sends. `//` escapes and an unrecognised command is posted as written. Swallowing
somebody's message is a much worse failure than an unknown command.

Two exceptions to Manual mode now, not one. Browsing and indexing are a person
acting, not a model, so neither passes through policy.py -- the same argument
the terminal panel rests on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-08-02 17:04:41 +02:00
parent 803d808723
commit b6cea42631
27 changed files with 2555 additions and 4 deletions
@@ -224,6 +224,42 @@
</div>
</section>
<section class="card">
<h2 class="section-title">The project directory</h2>
<p class="muted">
A listing of the directory a chat works in, so a reply does not spend its
first rounds finding out what is there — and so files can be attached by
name with <strong>@</strong>. Built by one read-only command
(<code>git ls-files</code> where it works, otherwise <code>find</code>),
cached briefly, and shared by every chat pointed at the same directory.
</p>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="index_enabled"
{{ 'checked' if values.index_enabled }}>
<span>List the project directory</span>
</label>
<p class="field__hint">
Off means no listing is built at all, and the file picker offers only
what is in the library.
</p>
</div>
<div class="field">
<label class="field__label" for="index_chars">Characters of it in the prompt</label>
<input class="input" id="index_chars" name="index_chars"
value="{{ values.index_chars }}" inputmode="numeric">
<p class="field__hint">
This is spent on <em>every</em> request in an agent chat, so it is a
budget rather than a limit: directories that will not fit are shown as
a count and the model is told to look inside them itself.
<strong>0</strong> keeps the listing for the file picker and puts none
of it in the prompt.
</p>
</div>
</section>
<div class="btn-row">
<button class="btn btn--primary" type="submit">Save changes</button>
</div>
+4
View File
@@ -54,6 +54,10 @@
<script src="{{ url_for('static', path='vendor/alpine.min.js') }}" defer></script>
<script src="{{ url_for('static', path='js/app.js') }}" defer></script>
<script src="{{ url_for('static', path='js/ui.js') }}" defer></script>
{# commands.js before composer.js: the second reads the first's table to draw
the `/` menu, and both are deferred so the order here is the run order. #}
<script src="{{ url_for('static', path='js/commands.js') }}" defer></script>
<script src="{{ url_for('static', path='js/composer.js') }}" defer></script>
<script src="{{ url_for('static', path='js/audio.js') }}" defer></script>
{#
+20 -1
View File
@@ -21,7 +21,13 @@
text they are in the same place as attach and send, which is where the hand
already is.
#}
<div class="composer" {% if can.get("files.upload") %}data-dropzone{% endif %}>
{# The chat, the connection and the directory, where composer.js can read them
without parsing them back out of a URL. On a new chat the connection is
still being chosen, so the select and the hidden field win over these. #}
<div class="composer" {% if can.get("files.upload") %}data-dropzone{% endif %}
{% if chat %}data-chat-id="{{ chat.id }}"{% endif %}
{% if chat and chat.ssh_profile_id %}data-profile-id="{{ chat.ssh_profile_id }}"
data-project-dir="{{ chat.project_dir }}"{% endif %}>
{% if can.get("files.upload") %}
{# Outside the form: it is only ever read by JavaScript, and inside it would
be submitted as an empty file part on every message.
@@ -117,6 +123,14 @@
{% endif %}
</div>
</div>
{# The same menu the `@` key opens, for anyone who would rather press
than type. It inserts the character and gets out of the way. #}
<button class="btn btn--icon composer__btn" type="button" data-mention-open
aria-label="Mention a file or a document"
title="Mention a file or a document">
{{ icon("at") }}
</button>
{% endif %}
</div>
@@ -243,6 +257,11 @@
<p class="composer__hint">
Enter to send, Shift+Enter for a new line.
<button class="composer__hint-link" type="button"
onclick="window.lembasCommands &amp;&amp; window.lembasCommands.help()">
/ for commands
</button>,
@ for files.
{% if can.get("files.upload") %}
Drag files in, or paste an image.
{% if current_model and not current_model.capabilities_json.get("vision") %}
@@ -0,0 +1,65 @@
{% from "_macros.html" import icon %}
{#
What `@` offers. Two sources, one list, because somebody typing `@readme` is
not thinking about which store the answer is in.
Swapped in whole on every keystroke, like the knowledge picker: the library
is searched with FTS rather than filtered in the browser, and the project
half is filtered beside it so the client stays one fetch and a list.
Every name here came off somebody's filesystem or out of their library, so
all of it is escaped by autoescaping and none of it is marked safe.
#}
<div id="mention-results">
{% if not files and not documents %}
<p class="muted text-sm" style="padding: var(--sp-3)">
{% if q %}
Nothing matches “{{ q }}”.
{% else %}
Nothing to mention yet.
{% endif %}
</p>
{% else %}
{% if files %}
<p class="picker__group">In the project</p>
<ul class="picker__list">
{% for file in files %}
<li>
<button class="picker__option" type="button"
data-mention-file="{{ file.path }}" data-mention-token="{{ file.path }}">
{{ icon("folder" if file.path.endswith("/") else "file-text", "icon--sm") }}
<span class="picker__option-body">
<span class="picker__option-name">{{ file.name }}</span>
<span class="picker__option-note mono">{{ file.path }}</span>
</span>
</button>
</li>
{% endfor %}
</ul>
{% endif %}
{% if documents %}
<p class="picker__group">In your library</p>
<ul class="picker__list">
{% for document in documents %}
<li>
<button class="picker__option" type="button"
data-mention-knowledge="{{ document.id }}"
data-mention-token="{{ document.title }}">
{{ icon("image" if document.is_image else "archive", "icon--sm") }}
<span class="picker__option-body">
<span class="picker__option-name">{{ document.title }}</span>
<span class="picker__option-note">
{% if document.base %}{{ document.base.name }} · {% endif %}{{ document.kind }}
{%- if document.owner_id != user.id %} · shared{% endif %}
</span>
</span>
</button>
</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
@@ -217,6 +217,7 @@
<button class="btn btn--icon btn--sm" type="button"
hx-get="/api/chats/{{ chat.id }}/messages/{{ message.id }}/edit"
hx-target="#msg-{{ message.id }}" hx-swap="outerHTML"
data-edit-message
aria-label="Edit and retry from here">
{{ icon("pencil", "icon--sm") }}
</button>
+80
View File
@@ -0,0 +1,80 @@
{#
What `/usage` shows.
Two different numbers, deliberately kept apart. **Used** is how full the
window is right now -- one reply's prompt plus its completion, which is what
decides when compaction fires. **Spent** is everything this conversation has
cost end to end, which is larger and grows forever: a three-round reply pays
for its prompt three times but only ever occupies the window once.
Anything derived from an estimate wears a `~`, because `services/tokens.py`
counts four characters to a token when an endpoint reports nothing, and a
precise-looking figure that is a guess is worse than an obvious guess.
#}
<table class="sheet">
<tbody>
<tr>
<td>In the window now</td>
<td>
{% if metrics.context_tokens %}
{{ '~' if metrics.estimated }}{{ '{:,}'.format(metrics.context_tokens) }} tokens
{% if metrics.percent %}
— {{ metrics.percent }}% of
{{ '{:,}'.format(metrics.context_limit) }}
{% endif %}
{% else %}
Nothing yet.
{% endif %}
</td>
</tr>
{% if not metrics.context_limit %}
{# Unknown is not zero. Nobody has said how big this model's window is, so
the percentage is omitted rather than computed, and automatic
compaction never fires. #}
<tr>
<td>Window size</td>
<td>
Not recorded for {{ model.label if model else "this model" }}, so there is
no percentage and this chat will never compact itself.
{% if user.is_admin %}
Set it under <a href="/admin/models">Models</a>.
{% endif %}
</td>
</tr>
{% endif %}
<tr>
<td>Spent in total</td>
<td>
{{ '~' if estimated }}{{ '{:,}'.format(totals.total) }} tokens
across {{ replies }} repl{{ 'y' if replies == 1 else 'ies' }}
</td>
</tr>
<tr>
<td>Of which sent</td>
<td>{{ '~' if estimated }}{{ '{:,}'.format(totals.prompt) }} tokens</td>
</tr>
<tr>
<td>Of which written</td>
<td>{{ '~' if estimated }}{{ '{:,}'.format(totals.completion) }} tokens</td>
</tr>
{% if metrics.tokens_per_second %}
<tr>
<td>Last reply</td>
<td>{{ '%.1f'|format(metrics.tokens_per_second) }} tokens/second</td>
</tr>
{% endif %}
{% if chat.compact_summary %}
<tr>
<td>Compacted</td>
<td>
Earlier turns are summarised. They are still in the transcript; they
just stop being sent.
</td>
</tr>
{% endif %}
</tbody>
</table>