mirror of
https://github.com/block/buzz.git
synced 2026-11-30 05:02:57 +00:00
Prefer fresh typing over stale agent turns
Signed-off-by: kenny lopez <klopez4212@gmail.com>
This commit is contained in:
@@ -8,6 +8,9 @@ import '../channel_typing_provider.dart';
|
||||
import 'active_agent_turns.dart';
|
||||
import 'observer_subscription.dart';
|
||||
|
||||
const _observerLivenessSlack = Duration(seconds: 30);
|
||||
const _maximumTypingSupersedeGrace = Duration(seconds: 30);
|
||||
|
||||
/// Composer activity is scoped either to a channel or to one thread.
|
||||
typedef ComposerActivityKey = ({String channelId, String? threadHeadId});
|
||||
|
||||
@@ -123,9 +126,17 @@ final composerActivityStateProvider = Provider.autoDispose
|
||||
humans.add(entry);
|
||||
continue;
|
||||
}
|
||||
if (signals[pubkey]?.isWorking == true) continue;
|
||||
final turn = activeByAgent[pubkey];
|
||||
final liveTurn = turn?.isWorking == true ? turn : null;
|
||||
final supersedesWorkingTurn =
|
||||
turn != null &&
|
||||
turn.isWorking &&
|
||||
_typingSupersedesWorkingTurn(entry, turn);
|
||||
if (signals[pubkey]?.isWorking == true && !supersedesWorkingTurn) {
|
||||
continue;
|
||||
}
|
||||
final liveTurn = turn?.isWorking == true && !supersedesWorkingTurn
|
||||
? turn
|
||||
: null;
|
||||
signals[pubkey] = WorkingAgentSignal(
|
||||
pubkey: pubkey,
|
||||
source: AgentWorkingSource.typing,
|
||||
@@ -187,3 +198,20 @@ int _compareComposerTurnRecency(AgentTurnState a, AgentTurnState b) {
|
||||
if (a.isWorking != b.isWorking) return a.isWorking ? 1 : -1;
|
||||
return a.turnId.compareTo(b.turnId);
|
||||
}
|
||||
|
||||
bool _typingSupersedesWorkingTurn(TypingEntry typing, AgentTurnState turn) {
|
||||
final advertisedCadenceMs =
|
||||
turn.livenessTimeout.inMilliseconds -
|
||||
_observerLivenessSlack.inMilliseconds;
|
||||
final graceMs = advertisedCadenceMs <= 0
|
||||
? _maximumTypingSupersedeGrace.inMilliseconds
|
||||
: advertisedCadenceMs
|
||||
.clamp(
|
||||
TypingEntry.ttl.inMilliseconds,
|
||||
_maximumTypingSupersedeGrace.inMilliseconds,
|
||||
)
|
||||
.toInt();
|
||||
return typing.receivedAt.isAfter(
|
||||
turn.lastActivityAt.add(Duration(milliseconds: graceMs)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import '../../shared/relay/relay.dart';
|
||||
/// A single typing indicator entry.
|
||||
@immutable
|
||||
class TypingEntry {
|
||||
static const ttl = Duration(seconds: 8);
|
||||
|
||||
final String pubkey;
|
||||
final String? threadHeadId;
|
||||
final int expiresAtMs;
|
||||
@@ -17,6 +19,12 @@ class TypingEntry {
|
||||
this.threadHeadId,
|
||||
required this.expiresAtMs,
|
||||
});
|
||||
|
||||
/// Local receipt time reconstructed from the fixed typing TTL.
|
||||
DateTime get receivedAt => DateTime.fromMillisecondsSinceEpoch(
|
||||
expiresAtMs - ttl.inMilliseconds,
|
||||
isUtc: true,
|
||||
);
|
||||
}
|
||||
|
||||
/// Tracks who is currently typing in a specific channel.
|
||||
@@ -24,7 +32,6 @@ class TypingEntry {
|
||||
/// Subscribes to kind:20002 (typing indicator) events via websocket.
|
||||
/// Entries expire after 8 seconds (matching the desktop TTL).
|
||||
class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
|
||||
static const _ttlMs = 8000;
|
||||
static const _pruneIntervalMs = 1000;
|
||||
|
||||
final String channelId;
|
||||
@@ -70,7 +77,7 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
|
||||
final entry = TypingEntry(
|
||||
pubkey: event.pubkey,
|
||||
threadHeadId: event.getTagValue('e'),
|
||||
expiresAtMs: now + _ttlMs,
|
||||
expiresAtMs: now + TypingEntry.ttl.inMilliseconds,
|
||||
);
|
||||
|
||||
// Upsert: replace existing entry for same pubkey+thread, or add.
|
||||
|
||||
@@ -14,7 +14,10 @@ const _channelId = 'channel-1';
|
||||
|
||||
void main() {
|
||||
test('prefers observer work and keeps human typing separate', () {
|
||||
final observerTurn = _turn('agent-a');
|
||||
final observerTurn = _turn(
|
||||
'agent-a',
|
||||
livenessTimeout: const Duration(seconds: 40),
|
||||
);
|
||||
final container = ProviderContainer(
|
||||
overrides: [
|
||||
currentPubkeyProvider.overrideWith((ref) => 'owner'),
|
||||
@@ -22,10 +25,13 @@ void main() {
|
||||
_channelId,
|
||||
).overrideWith((ref) async => const <ChannelMember>[]),
|
||||
channelTypingProvider(_channelId).overrideWith(
|
||||
() => _FakeTypingNotifier(const [
|
||||
TypingEntry(pubkey: 'agent-a', expiresAtMs: 9999999999999),
|
||||
TypingEntry(pubkey: 'agent-b', expiresAtMs: 9999999999999),
|
||||
TypingEntry(pubkey: 'human', expiresAtMs: 9999999999999),
|
||||
() => _FakeTypingNotifier([
|
||||
_typingEntry(
|
||||
'agent-a',
|
||||
receivedAt: DateTime.utc(2026, 8, 16, 12, 0, 9),
|
||||
),
|
||||
_typingEntry('agent-b'),
|
||||
_typingEntry('human'),
|
||||
]),
|
||||
),
|
||||
agentMentionPubkeysProvider(
|
||||
@@ -177,6 +183,62 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
test('fresh typing supersedes a stale working turn in the same scope', () {
|
||||
final staleTurn = _turn(
|
||||
'agent-a',
|
||||
threadHeadId: 'thread-1',
|
||||
lastActivityAt: DateTime.utc(2026, 8, 16, 12),
|
||||
livenessTimeout: const Duration(days: 7, seconds: 30),
|
||||
);
|
||||
final container = ProviderContainer(
|
||||
overrides: [
|
||||
currentPubkeyProvider.overrideWith((ref) => 'owner'),
|
||||
channelMembersProvider(
|
||||
_channelId,
|
||||
).overrideWith((ref) async => const <ChannelMember>[]),
|
||||
channelTypingProvider(_channelId).overrideWith(
|
||||
() => _FakeTypingNotifier([
|
||||
_typingEntry(
|
||||
'agent-a',
|
||||
threadHeadId: 'thread-1',
|
||||
receivedAt: DateTime.utc(2026, 8, 16, 12, 0, 31),
|
||||
),
|
||||
]),
|
||||
),
|
||||
agentMentionPubkeysProvider(
|
||||
_channelId,
|
||||
).overrideWith((ref) => const {'agent-a'}),
|
||||
agentOwnersProvider.overrideWithValue(
|
||||
const AsyncData({'agent-a': 'owner'}),
|
||||
),
|
||||
userCacheProvider.overrideWith(_FakeUserCacheNotifier.new),
|
||||
observerRelayProvider.overrideWith(
|
||||
() => _FakeObserverRelayNotifier({
|
||||
'agent-a': [_observerFrame('agent-a')],
|
||||
}),
|
||||
),
|
||||
composerAgentTurnStatesProvider.overrideWithValue([staleTurn]),
|
||||
],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
final signal = container
|
||||
.read(
|
||||
composerActivityStateProvider((
|
||||
channelId: _channelId,
|
||||
threadHeadId: 'thread-1',
|
||||
)),
|
||||
)
|
||||
.agents
|
||||
.single;
|
||||
|
||||
expect(signal.source, AgentWorkingSource.typing);
|
||||
expect(signal.isWorking, isTrue);
|
||||
expect(signal.turnId, isNull);
|
||||
expect(signal.startedAt, isNull);
|
||||
expect(container.read(workingBotPubkeysProvider(_channelId)), {'agent-a'});
|
||||
});
|
||||
|
||||
test(
|
||||
'keeps a scoped terminal outcome reachable after thread typing stops',
|
||||
() {
|
||||
@@ -435,6 +497,7 @@ AgentTurnState _turn(
|
||||
String? threadHeadId,
|
||||
DateTime? startedAt,
|
||||
DateTime? lastActivityAt,
|
||||
Duration livenessTimeout = const Duration(seconds: 30),
|
||||
}) => AgentTurnState(
|
||||
agentPubkey: pubkey,
|
||||
channelId: _channelId,
|
||||
@@ -442,13 +505,26 @@ AgentTurnState _turn(
|
||||
turnId: turnId ?? 'turn-$pubkey',
|
||||
startedAt: startedAt ?? DateTime.utc(2026, 8, 16, 12),
|
||||
lastActivityAt: lastActivityAt ?? DateTime.utc(2026, 8, 16, 12),
|
||||
livenessTimeout: const Duration(seconds: 30),
|
||||
livenessTimeout: livenessTimeout,
|
||||
phase: phase,
|
||||
terminalAt: phase == AgentTurnPhase.working
|
||||
? null
|
||||
: DateTime.utc(2026, 8, 16, 12, 0, 5),
|
||||
);
|
||||
|
||||
TypingEntry _typingEntry(
|
||||
String pubkey, {
|
||||
String? threadHeadId,
|
||||
DateTime? receivedAt,
|
||||
}) {
|
||||
final received = receivedAt ?? DateTime.utc(2026, 8, 16, 12);
|
||||
return TypingEntry(
|
||||
pubkey: pubkey,
|
||||
threadHeadId: threadHeadId,
|
||||
expiresAtMs: received.add(TypingEntry.ttl).millisecondsSinceEpoch,
|
||||
);
|
||||
}
|
||||
|
||||
ObserverFrame _observerFrame(String pubkey) => ObserverFrame(
|
||||
seq: 1,
|
||||
timestamp: DateTime.utc(2026, 8, 16, 12).toIso8601String(),
|
||||
|
||||
Reference in New Issue
Block a user