Skip to content

[Factory Experiment] connect: use deterministic BTreeSet::first for peer/IP selection - #164

Open
timanglade wants to merge 3 commits into
NetSys:mainfrom
timanglade:tpa/im-58-nondeterministic-peerip-selection
Open

[Factory Experiment] connect: use deterministic BTreeSet::first for peer/IP selection#164
timanglade wants to merge 3 commits into
NetSys:mainfrom
timanglade:tpa/im-58-nondeterministic-peerip-selection

Conversation

@timanglade

Copy link
Copy Markdown
Collaborator

Part of an experiment with setting up an automated software factory. Authored by Gas Town on Kilo, using DeepSeek V4 Pro / DeepSeek V4 Flash running on Fireworks. The factory was only fed the content of IM-58, then left completely unsupervised.

Summary

  • Replace .iter().next() with .first() on BTreeSet in connect_name and connect_imid
  • BTreeSet::first() returns the minimum element deterministically per the Ord implementation
  • Document the selection strategy in doc comments
  • Add tests exercising multi-IMID and multi-IP resolution paths

Details

The previous code used s.iter().next() on BTreeSet collections to select a peer when multiple IMIDs or IPs were available. While BTreeSet is inherently ordered, using .first() makes the intent explicit and guarantees deterministic selection.

Toast (gastown) and others added 2 commits June 19, 2026 05:21
Replace s.iter().next() with s.first() in connect_name and connect_imid
to make the selection strategy explicit and documented. BTreeSet::first()
returns the minimum element per Ord, which is deterministic across runs.

Add test_connect_name_multi_imid to exercise name → multi-IMID resolution.
Add test_connect_imid_multi_ip to exercise IMID → multi-IP resolution.
@timanglade

Copy link
Copy Markdown
Collaborator Author

CI flagged some linting issues (& test failures) however were easy to fix by just prompting the model to fix them.

Gas Town on Kilo doesn't set up a true dev environment afaict, so it can't run commands like cargo local to make sure everything is fine. Technically it could rely on CI for that purpose but the way I've set it up currently only allows it to look at my fork, so it couldn't see the CI failures on this PR & act on them. This limitation should be easy to remove…

Anyway this should be good to review now, same comment about not being able to assign @ejj-agent to review it. cc @ejj

Comment thread src/connect.rs Outdated
}

#[tokio::test]
async fn test_connect_name_multi_imid() {

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.

I'm convince-able but I think this is probably overkill.

I'd suggest instead writing a pure helper function for name_to_imid and imid_to_ip that returns the first element or an anyhow error. Then you can write a very simple unit test for that to ensure the first one is chosen.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

OK let me see if the factory can get to a satisfactory result just by acting on this comment

@ejj

ejj commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

btw @timanglade I think the code in this is pretty good, had a comment but its a reasonable starting point.

@ejj-agent ejj-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI-generated review draft. This has not been reviewed by a human. Any comments made are non-binding; feel free to ignore them by resolving. This uses the same AI-review process Ethan uses to review his own code.

I reviewed the PR for correctness, simplicity, diff hygiene, and tests. I don't see any additional substantive concerns beyond the existing discussion about whether the new connection-level tests are more integration-heavy than needed for this deterministic selection change.

AI-PR-Review: #164 805cf96

…pers

Replace the heavy integration tests for multi-IMID and multi-IP selection
with simple unit tests on the pure helper functions.

@ejj-agent ejj-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI-generated review draft. This has not been reviewed by a human. Any comments made are non-binding; feel free to ignore them by resolving. This uses the same AI-review process Ethan uses to review his own code.

Helpers are well-scoped and address the prior reviewer suggestion. The behavior change is effectively a no-op (BTreeSet::iter().next() already returned the minimum), but the explicit .first() plus docstring makes the intent legible. One minor test gap noted inline; otherwise the diff is clean.

AI-PR-Review: #164 d653b76

Comment thread src/connect.rs
}

#[test]
fn test_resolve_imid_to_ip_first() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI-generated review draft. This has not been reviewed by a human. Any comments made are non-binding; feel free to ignore them by resolving.

This test puts only one IP in the BTreeSet, so it doesn't actually exercise the "select minimum of multiple candidates" behavior the test name implies — any selection strategy would pass. Mirror test_resolve_name_to_imid_first here: insert two IPs and assert the minimum is returned.

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