fix(notifications): retry desktop focus suppression

This commit is contained in:
Gabriel De Andrade committed 2026-07-30 16:29:09 -04:00
1 parent 6c4dfc8d80
commit 4d7f9bdea4
4 files changed
+84 -30

No files matched your search

@@ -799,6 +799,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
const context = yield* Effect.context<never>();
const runFork = Effect.runForkWith(context);
const events = yield* Queue.unbounded<OrchestrationEvent>();
const missingRelayCredentialRead = yield* Deferred.make<void>();
const fetches = yield* Queue.unbounded<{
readonly url: URL;
readonly state: RelayAgentActivityState | null;
@@ -885,6 +886,19 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
} satisfies ExecutionEnvironmentDescriptor;
let snapshotThreads: Array<OrchestrationThreadShell> = [thread];
const focusedDesktopClients = yield* FocusedDesktopClients.make;
const observedSecretStore = ServerSecretStore.ServerSecretStore.of({
...secrets.store,
get: (name) =>
secrets.store
.get(name)
.pipe(
Effect.tap((value) =>
name === RELAY_ENVIRONMENT_CREDENTIAL_SECRET && Option.isNone(value)
? Deferred.succeed(missingRelayCredentialRead, undefined)
: Effect.void,
),
),
});
let tombstoneAttempts = 0;
globalThis.fetch = (async (
@@ -919,7 +933,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
);
const layer = Layer.mergeAll(
Layer.succeed(ServerSecretStore.ServerSecretStore, secrets.store),
Layer.succeed(ServerSecretStore.ServerSecretStore, observedSecretStore),
Layer.succeed(ServerEnvironment.ServerEnvironment, {
getEnvironmentId: Effect.succeed(environmentId),
getDescriptor: Effect.succeed(descriptor),
@@ -974,8 +988,15 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
expect(productSpans).toContain("makePublishProof");
expect(userSpans).not.toContain("makePublishProof");
yield* TestClock.adjust("1 second");
yield* secrets.store.remove(RELAY_ENVIRONMENT_CREDENTIAL_SECRET);
const desktopFocusScope = yield* Scope.make();
yield* focusedDesktopClients.acquire.pipe(Scope.provide(desktopFocusScope));
yield* Deferred.await(missingRelayCredentialRead).pipe(Effect.timeout("2 seconds"));
yield* secrets.setString(
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
"relay-credential-restored",
);
for (let iteration = 0; iteration < 10; iteration++) {
if (tombstoneAttempts >= 2) {
break;
+29 -24
View File
@@ -354,11 +354,15 @@ export const make = Effect.gen(function* () {
threadId: ThreadId,
options?: { readonly desktopFocusTombstone?: boolean },
) {
if (!options?.desktopFocusTombstone && (yield* focusedDesktopClients.anyFocused)) {
yield* Effect.logDebug("agent activity publish skipped; desktop client focused", {
threadId,
});
return;
const desktopFocusTombstone = options?.desktopFocusTombstone === true;
if ((yield* focusedDesktopClients.anyFocused) !== desktopFocusTombstone) {
yield* Effect.logDebug(
desktopFocusTombstone
? "desktop focus tombstone skipped; desktop is no longer focused"
: "agent activity publish skipped; desktop client focused",
{ threadId },
);
return false;
}
const publishAgentActivity = yield* readPublishAgentActivityEnabled.pipe(
Effect.orElseSucceed(() => false),
@@ -367,14 +371,14 @@ export const make = Effect.gen(function* () {
yield* Effect.logDebug("agent activity publish skipped; publication disabled", {
threadId,
});
return;
return false;
}
const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));
if (!relayConfig) {
yield* Effect.logDebug("agent activity publish skipped; relay link credentials unavailable", {
threadId,
});
return;
return false;
}
const relayClient = yield* makeRelayClient(relayConfig);
const environmentId = yield* serverEnvironment.getEnvironmentId;
@@ -385,12 +389,12 @@ export const make = Effect.gen(function* () {
readonly reason: string;
}) =>
Effect.gen(function* () {
if (!options?.desktopFocusTombstone && (yield* focusedDesktopClients.anyFocused)) {
if ((yield* focusedDesktopClients.anyFocused) !== desktopFocusTombstone) {
yield* Effect.logDebug(
"agent activity publish skipped; desktop client focused during publish",
{
threadId,
},
desktopFocusTombstone
? "desktop focus tombstone skipped; focus ended during publish preparation"
: "agent activity publish skipped; desktop client focused during publish",
{ threadId },
);
return false;
}
@@ -432,20 +436,23 @@ export const make = Effect.gen(function* () {
return true;
});
if (options?.desktopFocusTombstone) {
if (desktopFocusTombstone) {
const publishIdentity = agentAwarenessPublishIdentity(null);
publishConfirmDeadlines.delete(threadId);
yield* publishState({
const published = yield* publishState({
projectId: null,
state: null,
reason: "desktop-focused",
});
if (!published) {
return false;
}
yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {
const nextPublishedStates = new Map(publishedStates);
nextPublishedStates.set(threadId, publishIdentity);
return nextPublishedStates;
});
return;
return true;
}
const thread = yield* snapshotQuery.getThreadShellById(threadId);
@@ -471,7 +478,7 @@ export const make = Effect.gen(function* () {
threadId,
reason: snapshot.reason,
});
return;
return false;
}
// Two projections need confirmation before publishing, because both can
@@ -501,10 +508,10 @@ export const make = Effect.gen(function* () {
shell: describeThreadShellForAwareness(thread),
});
yield* schedulePublishConfirm(threadId);
return;
return false;
}
if (nowMs < deadline) {
return;
return false;
}
publishConfirmDeadlines.delete(threadId);
yield* Effect.logInfo("agent activity deferred publish confirmed", {
@@ -537,19 +544,21 @@ export const make = Effect.gen(function* () {
reason: snapshot.reason,
});
if (!published) {
return;
return false;
}
yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {
const nextPublishedStates = new Map(publishedStates);
nextPublishedStates.set(threadId, publishIdentity);
return nextPublishedStates;
});
return true;
});
const publishThread: AgentAwarenessRelay["Service"]["publishThread"] = (threadId) =>
publishSemaphore
.withPermits(1)(publishThreadUnsafe(threadId))
.pipe(
Effect.asVoid,
Effect.catchCause((cause) => {
return Effect.logWarning("agent activity publish failed", {
threadId,
@@ -596,9 +605,6 @@ export const make = Effect.gen(function* () {
const publishDesktopFocusTombstone = Effect.fn("publishDesktopFocusTombstone")(function* (
threadId: ThreadId,
) {
if (!(yield* focusedDesktopClients.anyFocused)) {
return;
}
const published = yield* publishSemaphore
.withPermits(1)(
publishThreadUnsafe(threadId, {
@@ -606,7 +612,6 @@ export const make = Effect.gen(function* () {
}),
)
.pipe(
Effect.as(true),
Effect.catchCause((cause) =>
Effect.logWarning("desktop focus agent activity tombstone failed; retrying", {
threadId,
@@ -615,7 +620,7 @@ export const make = Effect.gen(function* () {
),
withRelayClientTracing,
);
if (!published) {
if (!published && (yield* focusedDesktopClients.anyFocused)) {
yield* scheduleDesktopFocusTombstoneRetry(threadId);
}
});
@@ -292,11 +292,18 @@ describe("EnvironmentSupervisor", () => {
Effect.gen(function* () {
const focused = yield* SubscriptionRef.make(true);
const activeReports = yield* Ref.make(0);
const reportAttempts = yield* Ref.make(0);
const focusRpc = () =>
Stream.fromEffect(Ref.update(activeReports, (count) => count + 1)).pipe(
Stream.drain,
Stream.concat(Stream.never),
Stream.ensuring(Ref.update(activeReports, (count) => count - 1)),
Stream.fromEffect(Ref.updateAndGet(reportAttempts, (count) => count + 1)).pipe(
Stream.flatMap((attempt) =>
attempt === 1
? Stream.fail(new Error("Transient focus report failure."))
: Stream.fromEffect(Ref.update(activeReports, (count) => count + 1)).pipe(
Stream.drain,
Stream.concat(Stream.never),
Stream.ensuring(Ref.update(activeReports, (count) => count - 1)),
),
),
);
const rpcClient = {
[WS_METHODS.clientReportDesktopFocus]: focusRpc,
@@ -315,6 +322,15 @@ describe("EnvironmentSupervisor", () => {
);
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
for (
let iteration = 0;
iteration < 100 && (yield* Ref.get(reportAttempts)) !== 1;
iteration++
) {
yield* Effect.yieldNow;
}
expect(yield* Ref.get(reportAttempts)).toBe(1);
yield* TestClock.adjust("1 second");
for (
let iteration = 0;
iteration < 100 && (yield* Ref.get(activeReports)) !== 1;
@@ -323,6 +339,7 @@ describe("EnvironmentSupervisor", () => {
yield* Effect.yieldNow;
}
expect(yield* Ref.get(activeReports)).toBe(1);
expect(yield* Ref.get(reportAttempts)).toBe(2);
yield* SubscriptionRef.set(focused, false);
for (
@@ -10,6 +10,7 @@ import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Schedule from "effect/Schedule";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
@@ -487,7 +488,17 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
desktopFocus.changes.pipe(
Stream.changes,
Stream.switchMap((focused) =>
focused ? rpcSession.client[WS_METHODS.clientReportDesktopFocus]({}) : Stream.empty,
focused
? rpcSession.client[WS_METHODS.clientReportDesktopFocus]({}).pipe(
Stream.tapError((error) =>
Effect.logDebug("Desktop focus reporting failed; retrying.", {
environmentId: target.environmentId,
...safeErrorLogAttributes(error),
}),
),
Stream.retry(Schedule.spaced("1 second")),
)
: Stream.empty,
),
Stream.runDrain,
Effect.catchCause((cause) =>