|
9 | 9 | import time |
10 | 10 | from collections import Counter |
11 | 11 | from dataclasses import dataclass, replace |
12 | | -from pathlib import Path |
| 12 | +from pathlib import Path, PurePosixPath |
13 | 13 |
|
14 | 14 | from ..knowledge_service.lifecycle import Page |
15 | 15 | from ..knowledge_service.ops import page_over_capacity |
@@ -397,7 +397,65 @@ def _slot_result(entry, facet, status, reason): |
397 | 397 | if visits[key] >= 3: |
398 | 398 | slot["blocked"] = "three unsuccessful repairs with the same pinned evidence; needs new evidence or a corrected candidate" |
399 | 399 |
|
400 | | - def _docs(self, tree: Path, feature, owner) -> list[dict]: |
| 400 | + def _docs(self, tree: Path, feature, owner, *, context=None, facets=(), |
| 401 | + previous_review=None, evidence_round=0) -> list[dict]: |
| 402 | + if context is not None and context.mode == "lightweight" and context.index is not None: |
| 403 | + # A source-only feature can still have a README/SKILL/design guide |
| 404 | + # beside its implementation. Select only real indexed ancestor |
| 405 | + # documents, never another component's sibling directory. |
| 406 | + by_parent = {} |
| 407 | + for path, fact in context.index.files.items(): |
| 408 | + document = PurePosixPath(path) |
| 409 | + if (document.suffix.lower() not in |
| 410 | + {".md", ".mdx", ".rst", ".txt", ".adoc"} or fact["sha256"] is None): |
| 411 | + continue |
| 412 | + name = document.stem.lower() |
| 413 | + priority = next((number for number, prefix in enumerate( |
| 414 | + ("skill", "readme", "design", "architecture", "reference")) |
| 415 | + if name.startswith(prefix)), 5) |
| 416 | + by_parent.setdefault(document.parent.as_posix(), []).append((priority, path)) |
| 417 | + adjacent = [] |
| 418 | + for source in context._scope(feature): |
| 419 | + for distance, parent in enumerate(PurePosixPath(source).parents): |
| 420 | + siblings = by_parent.get(parent.as_posix(), ()) |
| 421 | + adjacent.extend((distance, priority, path) for priority, path in siblings) |
| 422 | + if any(priority < 4 for priority, _ in siblings): |
| 423 | + break # the nearest component guide is the local boundary |
| 424 | + paths = list(dict.fromkeys([*feature.docs, *(path for _, _, path in sorted(adjacent))])) |
| 425 | + filenames = {PurePosixPath(path).name for path in context._scope(feature)} |
| 426 | + preferred = [] |
| 427 | + for path in paths: |
| 428 | + matches, examples, fenced = [], [], False |
| 429 | + for number, line in enumerate(context.index.files.get(path, {}).get("lines", ()), 1): |
| 430 | + if line.lstrip().startswith(("```", "~~~")): |
| 431 | + fenced = not fenced |
| 432 | + if any(name in line for name in filenames): |
| 433 | + matches.append(number) |
| 434 | + if fenced: |
| 435 | + examples.append(number) |
| 436 | + # Existing executable examples beat prose lists of unrelated |
| 437 | + # tools. Nearby output shapes/criteria remain real line ranges. |
| 438 | + preferred.extend((path, max(1, number - 6), |
| 439 | + min(len(context.index.files[path]["lines"]), number + 6)) |
| 440 | + for number in (examples or matches)) |
| 441 | + # The immutable index supplies complete real line ranges, including |
| 442 | + # late manual acceptance steps. The existing 8KB packet cap applies. |
| 443 | + docs, used = [], 0 |
| 444 | + for number, path in enumerate(paths): |
| 445 | + selected = context.document_slices([], paths=(path,), |
| 446 | + limit=(8000 - used) // (len(paths) - number), facets=facets, |
| 447 | + previous_review=previous_review, evidence_round=evidence_round, |
| 448 | + preferred_ranges=preferred) |
| 449 | + docs.extend(selected) |
| 450 | + used += sum(len(line.encode("utf-8")) + 1 for item in selected for line in item["text"]) |
| 451 | + if docs: |
| 452 | + # build() rereads whole indexed documents. Offering a generic |
| 453 | + # owner path here would let that reread crowd out the local |
| 454 | + # command/criteria despite this first selection's byte cap. |
| 455 | + return docs |
| 456 | + generic = [item for item in self._docs_for(tree, owner) if item["path"] not in paths] |
| 457 | + return context.document_slices(generic, limit=8000, facets=facets, |
| 458 | + previous_review=previous_review, evidence_round=evidence_round) |
401 | 459 | docs = self._sources(tree, list(feature.docs), MAX_DOC_BYTES // 2) |
402 | 460 | used = sum(len(d["text"].encode("utf-8")) for d in docs) |
403 | 461 | for item in self._docs_for(tree, owner): |
@@ -444,17 +502,28 @@ def _attempt(self, tree, context, feature, entry): |
444 | 502 | owner = next((o for o in self.owners if o.owner == feature.owner), |
445 | 503 | Owner(feature.owner, feature.page, tuple(p.split("*", 1)[0] for p in feature.source_globs))) |
446 | 504 | guidance = getattr(self, "_repair_guidance", None) |
447 | | - docs = [] if guidance else self._docs(tree, feature, owner) |
| 505 | + requested = entry.get("requested_facets") or [f for f in FACETS if f not in old_blocks] |
| 506 | + docs = [] if guidance else self._docs(tree, feature, owner, context=context, |
| 507 | + facets=requested, previous_review=entry.get("reason"), |
| 508 | + evidence_round=entry.get("evidence_round", 0)) |
448 | 509 | existing = self._bounded_context(self._existing_knowledge(owner, self.head.get(feature.page, ""))) |
449 | 510 | if lightweight: |
450 | 511 | docs = self._bounded_slices(docs, 8000) |
451 | 512 | existing = self._bounded_texts(existing, 4000) |
452 | | - requested = entry.get("requested_facets") or [f for f in FACETS if f not in old_blocks] |
453 | 513 | retrieval = context.guided(feature, requested, guidance) if guidance else context.build( |
454 | 514 | feature, self.head.get(feature.page, ""), docs, facets=requested, |
455 | 515 | previous_review=entry.get("reason"), evidence_round=entry.get("evidence_round", 0)) |
456 | 516 | if lightweight: |
457 | | - docs = retrieval.get("docs", docs) |
| 517 | + if context.index is not None and not guidance: |
| 518 | + # Preserve the already selected local document ranges; a second |
| 519 | + # full-index rerank can replace their commands/criteria. Bind |
| 520 | + # the actual final packet, without changing any saved payload. |
| 521 | + retrieval["docs"] = docs |
| 522 | + retrieval["context_sha256"] = hashlib.sha256(json.dumps( |
| 523 | + {key: value for key, value in retrieval.items() if key != "context_sha256"}, |
| 524 | + sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()).hexdigest() |
| 525 | + else: |
| 526 | + docs = retrieval.get("docs", docs) |
458 | 527 | files = retrieval["files"] |
459 | 528 | if not files and not (lightweight and docs): |
460 | 529 | entry["attempts"] = entry.get("attempts", 0) + 1 |
|
0 commit comments