Skip to content

fix(#7): use auto-incrementing counter for item IDs - #10

Open
fullsend-ai-coder[bot] wants to merge 2 commits into
mainfrom
agent/7-fix-id-collision
Open

fix(#7): use auto-incrementing counter for item IDs#10
fullsend-ai-coder[bot] wants to merge 2 commits into
mainfrom
agent/7-fix-id-collision

Conversation

@fullsend-ai-coder

Copy link
Copy Markdown

The create_item() function generated IDs via len(_get_items()) + 1, which produced duplicate IDs after deletions reduced the list length.

Replace the length-based ID with a module-level monotonically increasing counter (_next_id) that never reuses values. The reset_items() helper resets the counter for test isolation.

Added test_no_id_collision_after_delete to verify that creating an item after a deletion does not produce a colliding ID.


Closes #7

Post-script verification

  • Branch is not main/master (agent/7-fix-id-collision)
  • Secret scan passed (gitleaks — 47f7f1511df8ad7036c082d9b2179085cd9ec107..HEAD)
  • Pre-commit hooks passed (authoritative run on runner)
  • Tests ran inside sandbox

The create_item() function generated IDs via len(_get_items()) + 1,
which produced duplicate IDs after deletions reduced the list length.

Replace the length-based ID with a module-level monotonically
increasing counter (_next_id) that never reuses values. The
reset_items() helper resets the counter for test isolation.

Added test_no_id_collision_after_delete to verify that creating an
item after a deletion does not produce a colliding ID.

Closes #7
@fullsend-ai-review

Copy link
Copy Markdown

Review

Findings

Low

  • [race-condition] app.py:69 — The _new_id() function uses a non-atomic read-increment-return on a global counter, which is not thread-safe under threaded WSGI servers. However, this is a pre-existing concern — the original len(_get_items()) + 1 was equally non-thread-safe, and the shared _items_store list itself is unprotected. This PR does not regress thread safety. For future hardening, consider itertools.count(1) with next(), which is implemented in C and effectively atomic under CPython.

  • [test-adequacy] test_app.py:68 — The new test_no_id_collision_after_delete validates the primary bug scenario (create-delete-create) well. A complementary edge case — multiple deletions followed by multiple creates — would provide stronger regression protection against alternative broken implementations (e.g., max(ids) + 1).

Info

  • [test-docstring-convention] test_app.py:68test_no_id_collision_after_delete() includes a docstring, but none of the existing 8 test functions use docstrings. Minor convention deviation; the test name is already descriptive.

  • [test-integrity] test_app.py — No existing test assertions were weakened or removed. The new test is purely additive and the reset_items fixture correctly resets the new _next_id counter, preserving test isolation. No split-payload concern detected.

Comment thread app.py
_next_id = 1


def _new_id():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] race-condition

The _new_id() function uses a non-atomic read-increment-return on a global counter, which is not thread-safe under threaded WSGI servers. However, this is a pre-existing concern — the original len(_get_items()) + 1 was equally non-thread-safe. This PR does not regress thread safety.

Suggested fix: Use itertools.count(1) with next() for an effectively atomic counter under CPython, or protect with threading.Lock if broader runtime support is needed.

Comment thread test_app.py
assert resp.status_code == 404


def test_no_id_collision_after_delete(client):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] test-adequacy

The new test validates the primary bug scenario (single delete then create) but does not cover multi-delete scenarios, which would provide stronger regression protection.

Suggested fix: Add a test that deletes multiple items then creates multiple new ones, verifying no ID collisions occur.

Comment thread test_app.py
assert resp.status_code == 404


def test_no_id_collision_after_delete(client):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[info] test-docstring-convention

test_no_id_collision_after_delete() includes a docstring, but none of the existing 8 test functions use docstrings. Minor convention deviation.

@fullsend-ai-review fullsend-ai-review Bot added the ready-for-merge All reviewers approved — ready to merge label Jun 5, 2026
@manish-jangra

Copy link
Copy Markdown

/fs-fix fix low findings (race-condition and test-adequacy) feedback from the review agent

- Replace manual global counter with itertools.count(1) for effectively
  atomic ID generation under CPython (addresses race-condition finding).
- Add test_no_id_reuse_after_multiple_deletes to cover bulk delete+create
  edge case (addresses test-adequacy finding).

Addresses review feedback on #10

Signed-off-by: fullsend-fix <fullsend-code@users.noreply.github.com>
@fullsend-ai-coder

Copy link
Copy Markdown
Author

🔧 Fix agent — iteration 1 (human-triggered)

Addressed both low findings: replaced global counter with itertools.count for thread-safe ID generation, and added multi-delete regression test.

Fixed (2):

  1. race-condition: _new_id() uses non-atomic read-increment-return on global counter (app.py): Replaced manual global _next_id counter with itertools.count(1) and next(), which is implemented in C and effectively atomic under CPython. Updated reset_items() to reassign the counter.
  2. test-adequacy: missing edge case for multiple deletions followed by multiple creates (test_app.py): Added test_no_id_reuse_after_multiple_deletes that creates 3 items, deletes all 3, then creates 2 more and asserts all 5 assigned IDs are unique.

Tests: passed

Updated by fullsend fix agent

@manish-jangra

Copy link
Copy Markdown

/fs-review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-merge All reviewers approved — ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ID collision after deleting items causes duplicate IDs

1 participant