Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/server/src/relay/AgentAwarenessRelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,35 @@ describe("AgentAwarenessRelay", () => {
}).pipe(Effect.scoped, Effect.provide(ProjectionStore.layerMemory)),
);
});

it.effect("catches up after startup without polling after success", () =>
Effect.gen(function* () {
const { relay, secrets, secretReads, catchUp } = yield* makeTestRelay();
yield* relay.start();
yield* TestClock.adjust("1 second");
assert.equal(catchUp.shellSnapshotReads, 1);

yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
yield* relay.requestCatchUp();
yield* TestClock.adjust("1 second");
assert.equal(catchUp.shellSnapshotReads, 1);

yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
yield* relay.requestCatchUp();
yield* TestClock.adjust("1 second");
assert.equal(catchUp.shellSnapshotReads, 2);

const readsBeforeIdle = secretReads.length;
yield* TestClock.adjust("10 minutes");
assert.equal(catchUp.shellSnapshotReads, 2);
assert.equal(secretReads.length, readsBeforeIdle);

yield* secrets.set(
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
new TextEncoder().encode("credential-2"),
);
yield* relay.requestCatchUp();
yield* TestClock.adjust("1 second");
assert.equal(catchUp.shellSnapshotReads, 3);
}).pipe(Effect.scoped),
);
13 changes: 7 additions & 6 deletions apps/server/src/relay/AgentAwarenessRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ export const make = Effect.gen(function* () {
const scope = yield* Effect.scope;
const cloudLinkKeyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets);
const startedAt = (yield* DateTime.now).epochMilliseconds;
const activeSnapshotPublishedRef = yield* Ref.make(false);
const catchUpRequests = yield* Queue.dropping<void>(1);
const publishedStateByThreadRef = yield* Ref.make(new Map<ThreadId, string>());

Expand Down Expand Up @@ -755,23 +754,25 @@ export const make = Effect.gen(function* () {
// s to 60 s. A linked environment keeps the 5 s retry, because `t3 connect
// publish` can turn publishing on from another process. Keep this untraced:
// an unlinked environment can retry for the lifetime of the process.
const publishActiveThreadsOnceWhenConfigured = Effect.fnUntraced(function* (
const publishActiveThreadsWhenConfigured = Effect.fnUntraced(function* (
logEnabledWhenReady: boolean,
) {
let unlinkedRetryDelayMs = 5_000;
while (!(yield* Ref.get(activeSnapshotPublishedRef))) {
while (true) {
const result = yield* publishActiveThreadsUnsafe.pipe(
Effect.orElseSucceed(() => "failed" as const),
);
if (result === "published") {
yield* Ref.set(activeSnapshotPublishedRef, true);
if (logEnabledWhenReady) {
const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));
yield* Effect.logInfo("agent activity publishing enabled after link reconciliation", {
relayUrl: relayConfig?.url,
});
}
return;
logEnabledWhenReady = false;
unlinkedRetryDelayMs = 5_000;
yield* Queue.take(catchUpRequests);
continue;
}
const retryDelayMs = result === "unlinked" ? unlinkedRetryDelayMs : 5_000;
yield* Effect.race(Effect.sleep(retryDelayMs), Queue.take(catchUpRequests));
Expand Down Expand Up @@ -822,7 +823,7 @@ export const make = Effect.gen(function* () {
}
yield* forkParked(
Effect.sleep("1 second").pipe(
Effect.andThen(publishActiveThreadsOnceWhenConfigured(startupState !== "enabled")),
Effect.andThen(publishActiveThreadsWhenConfigured(startupState !== "enabled")),
),
);
yield* forkParked(
Expand Down
Loading