HEL-844: translate weighted operands with verified syntax identities

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-10-25 01:16:06 +08:00
co-authored by multica-agent
parent 6e7760e304
commit 13ff441378
4 changed files with 438 additions and 36 deletions
@@ -0,0 +1,70 @@
# HEL-844: weighted prompt translation and syntax ownership
The shared translate/polish router previously protected whole weighted expressions.
A valid `(霓虹灯:1.2)` → `(neon lights:1.2)` therefore retried and failed with
`502 fragment_lost`. Ordinary words inside groups are now editable.
Before calling the existing LLM client, the router replaces each syntax occurrence
with a unique opaque token scoped to that request. This includes parentheses,
square brackets, recognized weight suffixes, tags/dialogue spans, CLI parameters,
and escaped delimiters. Each occurrence has its own identity, even when literal
weights or tags repeat. The provider receives the mapped prompt and token meanings.
The result must contain exactly the original ordered identities, each once. The
server rejects added raw syntax, restores tokens itself, then tokenizes again to
reject newly formed syntax across restored boundaries (for example adding `:2`
before an encoded closing parenthesis). Nonempty group operands cannot become
empty. Nested group structure, tag occurrences, and suffix-to-group placement are
therefore checked by identity and topology, rather than a bag of numeric weights.
For example, `(猫:1.2), (狗:0.8)` has distinct identities for its two
suffixes. Exchanging those identities is rejected even though the numeric
multiset remains `{1.2, 0.8}`. Exchanging identities for two equal `:1.2`
suffixes in sibling/nested groups is also rejected. Keeping all identities while
changing `cat` to a semantically wrong word is outside the machine-checkable
boundary described below.
Text, whitespace and commas between tokens can change; polishing can move words
within a group without requiring every original text slot to stay occupied.
`protected_fragments` reports the actual literal tags, suffixes, parameters and
escaped delimiters in source order with duplicates. For `(霓虹灯:1.2)` it is
`[":1.2"]`; the editable operand is no longer counted as a literal fragment.
Brackets are verified structurally and do not inflate that list. Existing frontend
code already uses the returned array length. Tokens never appear in successful
responses. The existing correction-once / then-502 contract, timeout budget,
disconnect handling, profile snapshot and model attribution remain in place.
## Machine-checkable boundary
This guarantees syntactic group identity, nesting, literal syntax and multiplicity.
It cannot prove natural-language equivalence or detect a model silently swapping
translated meanings between two otherwise valid operand groups. Translation quality
still needs preview. The parser recognizes the existing bounded angle tags,
complete `<d>…</d>` spans and `--parameters`, round/square delimiters, escaped
ASCII delimiters and numeric round-group suffixes. It does not interpret arbitrary
prompt dialects or repair malformed input; original malformed delimiters are kept.
Unknown syntax and ordinary names/numbers are not promoted into literal tags.
Providers unable to preserve identities fail explicitly after one correction.
No endpoint, configuration key, dependency, frontend asset or deployment changed.
## Reproduction and validation
From `calliope-backend`, with the existing dev dependencies:
```sh
PYTHONPATH=src python3 scripts/verify_prompt_weights.py --output weight-results.json
ruff check src/calliope/routers/prompt_tools.py tests/test_prompt_tools.py scripts/verify_prompt_weights.py
```
The script runs the real FastAPI router and LLMClient against an ephemeral local
HTTP fake supplier, records request/response bodies plus supplier call counts and
model routing, and exits nonzero if any regression fails. Fixtures isolate SQLite,
restore settings, disable persistent config writes and close their HTTP servers.
No paid LLM, GPU or deployment service is contacted. The artifact includes baseline
failure logs, final raw logs and JSON responses. Frontend check, lint, test and
production build were run on the unchanged frontend tree; existing acceptance
screenshots remain applicable to these identical frontend bytes. No browser matrix
was repeated for this backend-only change. Existing cancellation (including no
first chunk and blocking fallback), timeout, model routing/defaults, profile freeze,
long input and error classification tests provide the shared regression coverage.