PATCH VERIFICATION MISSION
lastItem reads past the final array index
Bug description
The lastItem utility returns undefined for non-empty arrays because it reads past the final valid index. It should return the final element of a non-empty array and continue returning undefined for an empty array.
Reproduction
Run:
npm test- Execution status
- COMPLETED
- Base commit
e73248184ca7a6cefc25ee04d882f2047cc48350- Changed files
- 1file
- Investigation
- 4iterations
Flight Recorder
- BASELINE
- BASELINE
- INVESTIGATING
- INVESTIGATING
- PATCHING
- PATCHING
- PATCHING
- PATCHING
- PATCHING
- VERDICT
- COMPLETED
Proof Chain
- Configured failure reproducedPassed · Baseline · exit 101
- Candidate patch appliedPassed · 1 file changed02
- Git diff capturedPassed · Against e73248103
- Integrity checksPassed · No recognized integrity concerns detected04
- Reproduction passes after patchPassed · Configured reproduction · exit 005
- Repository test script passesPassed · Repository tests · exit 006
Repair verdicts apply to the recorded requirements and execution.
Integrity checks detect recognized risks; they do not guarantee correctness.
Candidate patch
The candidate change captured from the sandbox working tree.
diff --git a/src/last-item.js b/src/last-item.js | |||
index 84b65e9..bd22055 100644 | |||
--- a/src/last-item.js | |||
+++ b/src/last-item.js | |||
@@ -1,3 +1,3 @@ | |||
| 1 | 1 | export function lastItem(items) { | |
| 2 | − | return items[items.length]; | |
| 2 | + | return items[items.length - 1]; | |
| 3 | 3 | } |
AI investigation report
PROPOSED DIAGNOSIS
Investigation Report
Investigated the lastItem utility. The implementation in src/last-item.js returns items[items.length], which reads one position past the last valid array index. Arrays are 0-indexed, so the last valid index is items.length - 1. This explains why non-empty arrays return undefined. The fix is to change items[items.length] to items[items.length - 1]. Only src/last-item.js needs modification.
- Patch target
src/last-item.js- Test file
src/last-item.test.js- Reproduction command
npm test
Verification evidence
Recorded command results. Expand a run to inspect its output.
Baseline reproduction
npm test
Baseline reproduction
npm teststdout
> patchverdict-array-boundary@1.0.0 test
> vitest run
RUN v3.2.4 /tmp/patchverdict
❯ src/last-item.test.js (3 tests | 2 failed) 9ms
× lastItem > returns the final item from a populated array 6ms
→ expected undefined to be 'c' // Object.is equality
× lastItem > works for a single-item array 2ms
→ expected undefined to be 42 // Object.is equality
✓ lastItem > returns undefined for an empty array 0ms
Test Files 1 failed (1)
Tests 2 failed | 1 passed (3)
Start at 09:37:41
Duration 297ms (transform 39ms, setup 0ms, collect 42ms, tests 9ms, environment 0ms, prepare 99ms)
stderr
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯
FAIL src/last-item.test.js > lastItem > returns the final item from a populated array
AssertionError: expected undefined to be 'c' // Object.is equality
[32m- Expected:[39m
"c"
[31m+ Received:[39m
undefined
❯ src/last-item.test.js:6:39
4| describe("lastItem", () => {
5| it("returns the final item from a populated array", () => {
6| expect(lastItem(["a", "b", "c"])).toBe("c");
| ^
7| });
8|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯
FAIL src/last-item.test.js > lastItem > works for a single-item array
AssertionError: expected undefined to be 42 // Object.is equality
[32m- Expected:[39m
42
[31m+ Received:[39m
undefined
❯ src/last-item.test.js:10:28
8|
9| it("works for a single-item array", () => {
10| expect(lastItem([42])).toBe(42);
| ^
11| });
12|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/2]⎯
Post-patch reproduction
npm test
Post-patch reproduction
npm teststdout
> patchverdict-array-boundary@1.0.0 test
> vitest run
RUN v3.2.4 /tmp/patchverdict
✓ src/last-item.test.js (3 tests) 3ms
Test Files 1 passed (1)
Tests 3 passed (3)
Start at 09:38:06
Duration 265ms (transform 49ms, setup 0ms, collect 31ms, tests 3ms, environment 0ms, prepare 99ms)
stderr
(no output)Repository test script
npm test
Repository test script
npm teststdout
> patchverdict-array-boundary@1.0.0 test
> vitest run
RUN v3.2.4 /tmp/patchverdict
✓ src/last-item.test.js (3 tests) 2ms
Test Files 1 passed (1)
Tests 3 passed (3)
Start at 09:38:07
Duration 265ms (transform 50ms, setup 0ms, collect 30ms, tests 2ms, environment 0ms, prepare 94ms)
stderr
(no output)