@@ -323,22 +323,79 @@ def handle_ingest_source(
323323 registry = _load_registry (output_dir )
324324
325325 # 1) Content-level dedup: identical content anywhere → duplicate.
326+ # Mirrors the name-conflict guard below: the tool MUST NOT decide on the
327+ # caller's behalf. It stores nothing, reports the clash and surfaces the
328+ # paths the user can choose from; re-running with overwrite=true is the
329+ # explicit "user agreed" signal.
330+ overwrite = bool (arguments .get ("overwrite" , False ))
331+ duplicate_of : Optional [str ] = None
326332 if content_hash :
327333 for existing_name , info in registry .get ("sources" , {}).items ():
328334 if isinstance (info , dict ) and info .get ("content_hash" ) == hash_key :
329335 if info .get ("status" ) != "retracted" :
330- return json .dumps (
331- {
332- "status" : "duplicate" ,
333- "name" : name ,
334- "existing_name" : existing_name ,
335- "content_hash" : f"sha256:{ content_hash [:16 ]} ..." ,
336- "message" : f"Content identical to existing source '{ existing_name } '. "
337- f"Use a different file or retract the existing source first." ,
338- },
339- indent = 2 ,
340- ensure_ascii = False ,
341- )
336+ duplicate_of = existing_name
337+ break
338+
339+ if duplicate_of :
340+ dup_info = registry ["sources" ][duplicate_of ]
341+ # A confirmed re-import is only meaningful when it targets the SAME
342+ # identifier. Overwriting under a *different* name would store a second
343+ # copy of byte-identical content and pollute retrieval with duplicates.
344+ if overwrite and duplicate_of != name :
345+ return json .dumps (
346+ {
347+ "status" : "error" ,
348+ "name" : name ,
349+ "duplicate_of" : duplicate_of ,
350+ "message" : (
351+ f"Content is already registered as '{ duplicate_of } '; refusing to "
352+ f"store a second copy under '{ name } '. Ask the user: reuse "
353+ f"'{ duplicate_of } ' as-is, or retract_source('{ duplicate_of } ') "
354+ "first and then import under the new name."
355+ ),
356+ },
357+ indent = 2 ,
358+ ensure_ascii = False ,
359+ )
360+ if not overwrite :
361+ return json .dumps (
362+ {
363+ "status" : "duplicate" ,
364+ "name" : name ,
365+ "existing_name" : duplicate_of ,
366+ "existing" : {
367+ "name" : duplicate_of ,
368+ "path" : dup_info .get ("path" , "" ),
369+ "original_path" : dup_info .get ("original_path" , "" ),
370+ "imported_at" : dup_info .get ("imported_at" , "" ),
371+ "description" : dup_info .get ("description" , "" ),
372+ "content_hash" : dup_info .get ("content_hash" , "" ),
373+ },
374+ "content_hash" : f"sha256:{ content_hash [:16 ]} ..." ,
375+ "requires_user_confirmation" : True ,
376+ "user_options" : [
377+ f"reuse (recommended): skip this import and work with the already "
378+ f"registered source '{ duplicate_of } ' — the bytes are identical" ,
379+ f"overwrite: only after the user agrees, re-run with "
380+ f"overwrite=true and name='{ duplicate_of } ' to re-store the same "
381+ "document under its canonical name (the old raw file moves to .trash)" ,
382+ f"rename: only if the user wants a separate identifier — "
383+ f"retract_source('{ duplicate_of } ') first, then import under a new name" ,
384+ ],
385+ "message" : (
386+ f"Content is byte-identical to existing source '{ duplicate_of } ' "
387+ f"({ dup_info .get ('path' ) or dup_info .get ('original_path' )} , imported "
388+ f"{ dup_info .get ('imported_at' , 'unknown' )} ). Nothing was stored. "
389+ "ASK THE USER which path to take before calling this tool again — "
390+ "do not silently skip, rename or overwrite on your own."
391+ ),
392+ },
393+ indent = 2 ,
394+ ensure_ascii = False ,
395+ )
396+ # Confirmed overwrite of the same identifier: retire the old raw file so
397+ # the fresh copy takes the canonical path (handled further below).
398+ _retire_registered_file (output_dir , registry , duplicate_of )
342399
343400 # 2) Name-level conflict guard: the identifier is already registered to a
344401 # *different* document. Never overwrite silently — surface the conflict
@@ -349,7 +406,6 @@ def handle_ingest_source(
349406 and existing .get ("status" ) != "retracted"
350407 and existing .get ("content_hash" ) != hash_key
351408 )
352- overwrite = bool (arguments .get ("overwrite" , False ))
353409 if name_conflict and not overwrite :
354410 return json .dumps (
355411 {
@@ -362,12 +418,20 @@ def handle_ingest_source(
362418 "content_hash" : existing .get ("content_hash" , "" ),
363419 "description" : existing .get ("description" , "" ),
364420 },
421+ "requires_user_confirmation" : True ,
422+ "user_options" : [
423+ "overwrite: only after the user agrees, re-run with overwrite=true to "
424+ "replace the existing document (the old raw file moves to .trash)" ,
425+ "rename: pass a different 'name' to keep both documents" ,
426+ "cancel: drop this import and keep the existing source untouched" ,
427+ ],
365428 "message" : (
366429 f"Source name '{ name } ' is already registered to a different document "
367430 f"({ existing .get ('path' ) or existing .get ('original_path' )} , imported "
368431 f"{ existing .get ('imported_at' , 'unknown' )} ). The new file was NOT stored. "
369- "After user confirmation, re-run ingest_source with overwrite=true to "
370- "replace it (the existing raw file will be moved to .trash), or pass a "
432+ "ASK THE USER first — do not silently skip, rename or overwrite. After the "
433+ "user confirms a replacement, re-run ingest_source with overwrite=true "
434+ "(the existing raw file will be moved to .trash); otherwise pass a "
371435 "different 'name' to keep both documents."
372436 ),
373437 },
0 commit comments