Skip to content

Fix _logId() collision in SyncRepository with per-instance counter#8

Merged
Harsh4114 merged 2 commits into
codex/design-bloc-like-architecture-for-sync-enginefrom
copilot/sub-pr-6-again
Feb 20, 2026
Merged

Fix _logId() collision in SyncRepository with per-instance counter#8
Harsh4114 merged 2 commits into
codex/design-bloc-like-architecture-for-sync-enginefrom
copilot/sub-pr-6-again

Conversation

Copilot AI commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

SyncRepository._logId() used only microsecondsSinceEpoch, allowing ID collisions when multiple logs are created within the same microsecond (e.g. rapid sequential ops, tight test loops).

Changes

  • SyncRepository: Added int _logCounter = 0; per-instance field and updated _logId() to append it, matching the SyncManager._generateOpId() pattern.
// Before
String _logId() {
  final micros = DateTime.now().microsecondsSinceEpoch;
  return 'sync_log_$micros';
}

// After
String _logId() {
  _logCounter += 1;
  final micros = DateTime.now().microsecondsSinceEpoch;
  return 'sync_log_${micros}_$_logCounter';
}

Log IDs are now sync_log_<microseconds>_<counter>, guaranteed unique within a SyncRepository instance.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: Harsh4114 <71206223+Harsh4114@users.noreply.github.com>
Copilot AI changed the title [WIP] Update to address feedback on Bloc-like architecture layer Fix _logId() collision in SyncRepository with per-instance counter Feb 20, 2026
Copilot AI requested a review from Harsh4114 February 20, 2026 13:25
@Harsh4114 Harsh4114 marked this pull request as ready for review February 20, 2026 13:25
Copilot AI review requested due to automatic review settings February 20, 2026 13:25
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@Harsh4114 Harsh4114 merged commit 814cbe9 into codex/design-bloc-like-architecture-for-sync-engine Feb 20, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes potential SyncRepository._logId() collisions by adding a per-instance counter to the generated log IDs, aligning the scheme with SyncManager’s timestamp+counter pattern.

Changes:

  • Add per-instance _logCounter state to SyncRepository.
  • Update _logId() to generate sync_log_<micros>_<counter>.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +14 to +15
int _logCounter = 0;

Copilot AI Feb 20, 2026

Copy link

Choose a reason for hiding this comment

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

_logId() now guarantees uniqueness only within a single SyncRepository instance. If multiple repositories share the same SyncLogStore (or if a store persists across app restarts and the counter resets), collisions can still happen because the ID lacks a repository/device/component discriminator. Consider including a configurable prefix (e.g., repository/device ID) or passing a logIdNamespace/instanceId into the repository so IDs are unique across all writers to the same store.

Copilot uses AI. Check for mistakes.
Comment on lines 146 to +149
String _logId() {
_logCounter += 1;
final micros = DateTime.now().microsecondsSinceEpoch;
return 'sync_log_$micros';
return 'sync_log_${micros}_$_logCounter';

Copilot AI Feb 20, 2026

Copy link

Choose a reason for hiding this comment

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

This change addresses a timing-resolution collision, but there’s no deterministic test covering it (calling add() in a tight loop may not reliably reproduce the original bug on all platforms). To make this behavior testable and prevent regressions, consider injecting a time provider/clock into SyncRepository (used by _logId() and SyncLog.timestamp) so a test can force identical timestamps and assert IDs remain unique.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants