Skip to content

Commit e733c8f

Browse files
fix: reject ambiguous --source/--track, clear index cache on throw
- ingest_knowledge: --track is repeatable but --source writes to exactly one index, so taking args.tracks[0] let argument order silently decide the destination. That combination is now an argparse error. The --source help also claimed it "overrides --track" while --index-name documented the default as the track index; rewritten so the precedence (--index-name, else the single --track, else the default track) is stated once. Multiple --track without --source is unaffected. - moss-vscode: clearIndexCache() only ran in the else-branch, so a rebuild() that threw after deleting the previous documents — from readFileForIndex() or addDocs() — jumped to catch and left the on-disk cache describing documents that no longer exist. Moved into a finally keyed on indexer.isIndexed(), which the throw path cannot skip. Verified: argparse across 6 invocations (multi-track rejected with exit 2, single track honoured, --index-name precedence intact, multi-track still fine without --source); and the try/catch/finally shape across cancelled, thrown and successful runs — the throw path now clears, the success path still does not. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a6d31ec commit e733c8f

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

apps/moss-interview-coach/backend/ingest_knowledge.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
243243
"--source",
244244
type=Path,
245245
default=None,
246-
help="JSON file or markdown directory (overrides --track; uses --index-name).",
246+
help=(
247+
"JSON file or markdown directory to ingest instead of the built-in "
248+
"rubrics. Destination index comes from --index-name, else from the "
249+
"single --track given, else the default track."
250+
),
247251
)
248252
parser.add_argument(
249253
"--index-name",
@@ -263,7 +267,15 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
263267
"with --source, omit to skip the sample query."
264268
),
265269
)
266-
return parser.parse_args(argv)
270+
args = parser.parse_args(argv)
271+
# --track is repeatable, but --source writes to exactly one index. Silently
272+
# taking the first would make argument order decide the destination.
273+
if args.source is not None and args.tracks and len(args.tracks) > 1:
274+
parser.error(
275+
"--source ingests into a single index, so pass at most one --track "
276+
"(or use --index-name to name the destination explicitly)."
277+
)
278+
return args
267279

268280

269281
def main() -> None:

apps/moss-vscode/src/extension.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,6 @@ async function runCreateIndex(
501501
vscode.window.showInformationMessage(
502502
`Moss index ready — ${files} files indexed (saved for next time).`,
503503
);
504-
} else {
505-
// rebuild() deletes the previous documents before scanning, so a
506-
// cancelled or failed run leaves the on-disk cache describing documents
507-
// that no longer exist. persistIndex() refuses to write while unindexed,
508-
// so drop the cache instead — otherwise the next launch restores stale
509-
// metadata for an index that is gone.
510-
await clearIndexCache(context).catch(() => undefined);
511-
log("Index not created; cleared stale index cache.");
512504
}
513505
} catch (err) {
514506
const message = err instanceof Error ? err.message : String(err);
@@ -519,6 +511,18 @@ async function runCreateIndex(
519511
statusBarItem.text = "$(error) Moss: error";
520512
statusBarItem.tooltip = message;
521513
}
514+
} finally {
515+
// rebuild() deletes the previous documents before scanning, so *any* run
516+
// that does not end in a ready index leaves the on-disk cache describing
517+
// documents that no longer exist — cancelled, or thrown from
518+
// readFileForIndex()/addDocs() midway. persistIndex() refuses to write
519+
// while unindexed, so drop the cache here instead, in a finally so the
520+
// throw path cannot skip it; otherwise the next launch restores stale
521+
// metadata for an index that is gone.
522+
if (!indexer.isIndexed()) {
523+
await clearIndexCache(context).catch(() => undefined);
524+
log("Index not ready; cleared stale index cache.");
525+
}
522526
}
523527
}
524528

0 commit comments

Comments
 (0)