clock: Cleanup ReplicaId, Lamport and Global (#40600)

- Notable change is the use of a newtype for `ReplicaId`
- Fixes `WorktreeStore::create_remote_worktree` creating a remote
worktree with the local replica id, though this is not currently used
- Fixes observing the `Agent` (that is following the agent) causing
global clocks to allocate 65535 elements
- Shrinks the size of `Global` a bit. In a local or non-collab remote
session it won't ever allocate still.

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-10-20 13:26:20 +02:00
committed by GitHub
parent 37e264ab99
commit 43a9368dff
33 changed files with 427 additions and 284 deletions
@@ -486,7 +486,7 @@ pub enum ContextSummary {
Error,
}
#[derive(Default, Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContextSummaryContent {
pub text: String,
pub done: bool,
@@ -523,7 +523,11 @@ impl ContextSummary {
match self {
ContextSummary::Content(content) => content,
ContextSummary::Pending | ContextSummary::Error => {
let content = ContextSummaryContent::default();
let content = ContextSummaryContent {
text: "".to_string(),
done: false,
timestamp: clock::Lamport::MIN,
};
*self = ContextSummary::Content(content);
self.content_as_mut().unwrap()
}
@@ -796,7 +800,7 @@ impl AssistantContext {
};
let first_message_id = MessageId(clock::Lamport {
replica_id: 0,
replica_id: ReplicaId::LOCAL,
value: 0,
});
let message = MessageAnchor {
@@ -2692,7 +2696,7 @@ impl AssistantContext {
self.summary = ContextSummary::Content(ContextSummaryContent {
text: "".to_string(),
done: false,
timestamp: clock::Lamport::default(),
timestamp: clock::Lamport::MIN,
});
replace_old = true;
}
@@ -3117,7 +3121,7 @@ impl SavedContext {
let mut first_message_metadata = None;
for message in self.messages {
if message.id == MessageId(clock::Lamport::default()) {
if message.id == MessageId(clock::Lamport::MIN) {
first_message_metadata = Some(message.metadata);
} else {
operations.push(ContextOperation::InsertMessage {
@@ -3141,7 +3145,7 @@ impl SavedContext {
if let Some(metadata) = first_message_metadata {
let timestamp = next_timestamp.tick();
operations.push(ContextOperation::UpdateMessage {
message_id: MessageId(clock::Lamport::default()),
message_id: MessageId(clock::Lamport::MIN),
metadata: MessageMetadata {
role: metadata.role,
status: metadata.status,
@@ -741,7 +741,7 @@ async fn test_serialization(cx: &mut TestAppContext) {
);
}
#[gpui::test(iterations = 100)]
#[gpui::test(iterations = 25)]
async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: StdRng) {
cx.update(init_test);
@@ -771,7 +771,7 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
let context = cx.new(|cx| {
AssistantContext::new(
context_id.clone(),
i as ReplicaId,
ReplicaId::new(i as u16),
language::Capability::ReadWrite,
registry.clone(),
prompt_builder.clone(),
@@ -789,7 +789,7 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
if let ContextEvent::Operation(op) = event {
network
.lock()
.broadcast(i as ReplicaId, vec![op.to_proto()]);
.broadcast(ReplicaId::new(i as u16), vec![op.to_proto()]);
}
}
})
@@ -797,7 +797,7 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
});
contexts.push(context);
network.lock().add_peer(i as ReplicaId);
network.lock().add_peer(ReplicaId::new(i as u16));
}
let mut mutation_count = operations;
@@ -943,9 +943,9 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
mutation_count -= 1;
}
_ => {
let replica_id = context_index as ReplicaId;
let replica_id = ReplicaId::new(context_index as u16);
if network.lock().is_disconnected(replica_id) {
network.lock().reconnect_peer(replica_id, 0);
network.lock().reconnect_peer(replica_id, ReplicaId::new(0));
let (ops_to_send, ops_to_receive) = cx.read(|cx| {
let host_context = &contexts[0].read(cx);
@@ -971,7 +971,7 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
network.lock().broadcast(replica_id, ops_to_send);
context.update(cx, |context, cx| context.apply_ops(ops_to_receive, cx));
} else if rng.random_bool(0.1) && replica_id != 0 {
} else if rng.random_bool(0.1) && replica_id != ReplicaId::new(0) {
log::info!("Context {}: disconnecting", context_index);
network.lock().disconnect_peer(replica_id);
} else if network.lock().has_unreceived(replica_id) {
@@ -996,25 +996,25 @@ async fn test_random_context_collaboration(cx: &mut TestAppContext, mut rng: Std
assert_eq!(
context.buffer.read(cx).text(),
first_context.buffer.read(cx).text(),
"Context {} text != Context 0 text",
"Context {:?} text != Context 0 text",
context.buffer.read(cx).replica_id()
);
assert_eq!(
context.message_anchors,
first_context.message_anchors,
"Context {} messages != Context 0 messages",
"Context {:?} messages != Context 0 messages",
context.buffer.read(cx).replica_id()
);
assert_eq!(
context.messages_metadata,
first_context.messages_metadata,
"Context {} message metadata != Context 0 message metadata",
"Context {:?} message metadata != Context 0 message metadata",
context.buffer.read(cx).replica_id()
);
assert_eq!(
context.slash_command_output_sections,
first_context.slash_command_output_sections,
"Context {} slash command output sections != Context 0 slash command output sections",
"Context {:?} slash command output sections != Context 0 slash command output sections",
context.buffer.read(cx).replica_id()
);
}