A repeated document should not earn relevance credit twice in the same ranking. Finish the normalized gain calculation so duplicates and the ideal ranking are handled consistently.
Build the implementation in Python under /app, with /app/run.sh as its entry point. Read one JSON request from standard input and write one JSON result to standard output. Input has positive integer k, relevance mapping document IDs to nonnegative integer grades, and ranked IDs. Take the first k positions without deduplication; only the first occurrence of a document earns its grade, later occurrences earn zero but still occupy a rank. Gain is 2**grade-1 and discount log2(rank+1) with rank starting at one. Ideal ranking is the largest k grades of distinct relevance keys. Return ndcg rounded to six decimals or zero for zero ideal gain.
Reject inputs that violate an explicit validation rule with exit status 2, no standard output, and {"error":"invalid_input"} on standard error. For valid input, exit with status 0. Keep the implementation reusable for new inputs; it must work offline and must not depend on the example data being the only data it receives.
Repair or replace the unfinished /app/run.sh and add source files under /app. Malformed JSON must use the same invalid-input response. Do not read /tests or /oracle from the application.