feat(6249): the write-path hint asks whether a new definition copies a close snippet
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 55s
CI & Build / integration (push) Successful in 1m2s
CI & Build / Python tests (push) Successful in 1m56s
CI & Build / Build & push image (push) Successful in 22s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 55s
CI & Build / integration (push) Successful in 1m2s
CI & Build / Python tests (push) Successful in 1m56s
CI & Build / Build & push image (push) Successful in 22s
The derive check matches by name, so a renamed copy of a recorded snippet passed it and showed only as "similar" prior art. When a write introduces a definition the file does not hold (shape_ledger.new_shapes_at) and an unseen snippet on the menu clears the write-path floor by WRITEPATH_COPY_MARGIN (0.80 at the default floor), the hint now says to reuse it or extract the shared shape before another copy. Names the derive check or the stamping feed already spoke for are left to them. Covers recorded snippets only: the ledger keeps no bodies to compare an unrecorded sibling against. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -143,6 +143,11 @@ WRITEPATH_ENABLED_KEY = "kb_writepath_enabled"
|
||||
WRITEPATH_THRESHOLD_KEY = SURFACES["write_path"].floor_key
|
||||
WRITEPATH_DEFAULT_ENABLED = True
|
||||
WRITEPATH_DEFAULT_THRESHOLD = SURFACES["write_path"].floor_default
|
||||
# How far past the write-path floor a snippet must resemble a NEW definition
|
||||
# before the hint asks whether the code is a copy of it. A margin, not a second
|
||||
# floor, so it moves when the operator tunes the write-path floor: at the
|
||||
# default 0.68 the copy bar is 0.80.
|
||||
WRITEPATH_COPY_MARGIN = 0.12
|
||||
|
||||
# The standing-rule arms get their own bar, for a third corpus: 0.68 above is
|
||||
# calibrated on code against note PROSE, not code against RULE TEXT.
|
||||
@@ -1444,6 +1449,9 @@ async def build_write_path_hint(
|
||||
note.title, note.note_type, note.data, note.body,
|
||||
),
|
||||
"seen": int(note.id) in excluded,
|
||||
# Read by the copy check, which asks for more
|
||||
# resemblance than the menu does.
|
||||
"score": float(score),
|
||||
# Carried, not re-read off the rendered marker. The
|
||||
# marker is prose assembled for a human and it already
|
||||
# varies by kind, language and the `seen` flag — a
|
||||
@@ -1498,6 +1506,29 @@ async def build_write_path_hint(
|
||||
derive = [d for d in found if d.get("key") not in skip]
|
||||
except Exception:
|
||||
logger.warning("write-time derive check failed", exc_info=True)
|
||||
# The COPY check: the derive check matches by name, so a renamed copy
|
||||
# of a recorded snippet passes it and shows only as "similar" prior art.
|
||||
# When this write introduces a definition and an unseen snippet on the
|
||||
# menu resembles it well past the menu's own floor, say that a copy is
|
||||
# the moment to extract. Only a recorded snippet can be seen this way —
|
||||
# the ledger keeps no bodies to compare an unrecorded sibling against.
|
||||
copies: list[dict] = []
|
||||
copy_bar = cfg["threshold"] + WRITEPATH_COPY_MARGIN
|
||||
strong = [
|
||||
it for _m, it in menu
|
||||
if it.get("kind") == "snippet" and not it.get("seen")
|
||||
and it.get("score", 0.0) >= copy_bar
|
||||
]
|
||||
if strong and stamp_shapes and project_id:
|
||||
named = {d["symbol"] for d in derive} | {s["symbol"] for s in suggested}
|
||||
try:
|
||||
fresh = await shape_ledger_svc.new_shapes_at(project_id, path, stamp_shapes)
|
||||
copies = [
|
||||
{"symbol": n, "kind": k, "snippet": strong[0]}
|
||||
for k, n in fresh if n not in named
|
||||
]
|
||||
except Exception:
|
||||
logger.warning("write-time copy check failed", exc_info=True)
|
||||
staleness: list[str] = []
|
||||
|
||||
# The design arm is decided HERE, above the guard, so a UI write
|
||||
@@ -1681,6 +1712,8 @@ async def build_write_path_hint(
|
||||
lines.append(_divergence_line(path, divergence))
|
||||
if derive:
|
||||
lines.append(_derive_line(path, derive))
|
||||
if copies:
|
||||
lines.append(_copy_line(copies))
|
||||
|
||||
# Split by arm, which is the whole reason this table exists. The place arm
|
||||
# carries no score and so has no home in retrieval_logs, yet it fires on
|
||||
@@ -2008,6 +2041,22 @@ def _divergence_line(path: str, divergence: list[dict]) -> str:
|
||||
)
|
||||
|
||||
|
||||
def _copy_line(copies: list[dict]) -> str:
|
||||
"""A new definition that closely resembles a recorded snippet under
|
||||
another name: the derive check cannot see a renamed copy, so this says
|
||||
it — as a question for the writer, since resemblance is not identity."""
|
||||
snip = copies[0]["snippet"]
|
||||
names = ", ".join(
|
||||
f"`{'.' if c['kind'] == 'css' else ''}{c['symbol']}`" for c in copies
|
||||
)
|
||||
return (
|
||||
f"> {names} {'is' if len(copies) == 1 else 'are'} new in this file and "
|
||||
f"this code resembles snippet #{snip['id']} `{snip.get('name') or snip['title']}` "
|
||||
f"(similar {snip['score']:.2f}). If it copies that shape, reuse #{snip['id']} "
|
||||
"or extract the shared shape now, before another copy is written."
|
||||
)
|
||||
|
||||
|
||||
def _suggest_line(path: str, suggested: list[dict]) -> str:
|
||||
"""One line naming the recorded shape this code looks like, as evidence
|
||||
for the judgment the agent gives at the end of the turn — not a record of
|
||||
|
||||
@@ -2706,6 +2706,29 @@ async def write_time_derive(
|
||||
return out
|
||||
|
||||
|
||||
async def new_shapes_at(
|
||||
project_id: int, path: str, shapes: list[tuple[str, str]]
|
||||
) -> list[tuple[str, str]]:
|
||||
"""The (kind, name) shapes the hook named at ``path`` that the ledger has
|
||||
no live row for there — definitions this write introduces, as opposed to
|
||||
ones it edits inside. Order follows ``shapes``."""
|
||||
wanted = [(k, n) for k, n in shapes if n]
|
||||
if not wanted:
|
||||
return []
|
||||
async with async_session() as session:
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(CodeShape.kind, CodeShape.symbol).where(
|
||||
CodeShape.project_id == project_id,
|
||||
CodeShape.path == path,
|
||||
CodeShape.vanished_at.is_(None),
|
||||
)
|
||||
)
|
||||
).all()
|
||||
here = {(k, _norm_symbol(s)) for k, s in rows}
|
||||
return [(k, n) for k, n in wanted if (k, _norm_symbol(n)) not in here]
|
||||
|
||||
|
||||
async def flag_divergence(project_id: int, *, since: datetime | None) -> int:
|
||||
"""Flag shapes created after ``since`` (the previous refresh) that sit
|
||||
where a canon dominates and were not proposed as that canon. With no
|
||||
|
||||
Reference in New Issue
Block a user