Skip to content

Commit defee79

Browse files
committed
fix(mobile): gate background stop on thread operate scope
1 parent d7964d0 commit defee79

3 files changed

Lines changed: 4 additions & 62 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
453453
}
454454
})();
455455
const threadSyncLabel = useDelayedStatus(selectedThreadKey, realThreadSyncLabel);
456-
const canInterrupt = useAtomValue(
457-
threadEnvironment.interruptTurn.permissionAtom(props.environmentId),
458-
);
459456
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, {
460457
reportFailure: false,
461458
});
@@ -500,7 +497,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
500497
latestRunCompletedAt: props.selectedThread.latestRun?.completedAt ?? null,
501498
});
502499
const runStatusAction = async (action: "stop" | "wake" | "unsettle" | "dismiss") => {
503-
if (statusPending || (action === "stop" ? !canInterrupt : !props.canOperateThread)) return;
500+
if (statusPending || !props.canOperateThread) return;
504501
const request = { threadKey: selectedThreadKey };
505502
setStatusRequest(request);
506503
const target = {
@@ -614,7 +611,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
614611
action: {
615612
label: statusPending ? "Stopping..." : "Stop",
616613
accessibilityLabel: "Stop background work",
617-
disabled: !canInterrupt || statusPending,
614+
disabled: !props.canOperateThread || statusPending,
618615
onPress: () => void runStatusAction("stop"),
619616
},
620617
};

‎packages/client-runtime/src/state/threadCommands.test.ts‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { vi } from "vite-plus/test";
21
import { describe, expect, it } from "@effect/vitest";
32
import {
4-
AuthOrchestrationOperateScope,
5-
type AuthSessionState,
63
CommandId,
74
RuntimeRequestId,
85
EnvironmentId,
@@ -21,20 +18,13 @@ import * as Layer from "effect/Layer";
2118
import * as Option from "effect/Option";
2219
import * as Queue from "effect/Queue";
2320
import * as SubscriptionRef from "effect/SubscriptionRef";
24-
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
21+
import { Atom, AtomRegistry } from "effect/reactivity";
2522

2623
import * as EnvironmentRegistry from "../connection/registry.ts";
2724
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
2825
import type { RpcSession } from "../rpc/session.ts";
2926
import { createThreadEnvironmentAtoms } from "./threadCommands.ts";
3027

31-
vi.mock("./session.ts", () => ({
32-
createEnvironmentSessionAtoms: () => ({ sessionStateAtom: sessions }),
33-
}));
34-
const sessions = Atom.family((_id: EnvironmentId) =>
35-
Atom.make<AsyncResult.AsyncResult<AuthSessionState, string>>(AsyncResult.initial()),
36-
);
37-
3828
const ENVIRONMENT_ID = EnvironmentId.make("remote");
3929
const THREAD_ID = ThreadId.make("thread");
4030
const NOW = DateTime.makeUnsafe("2026-09-12T10:00:00.000Z");
@@ -135,34 +125,6 @@ const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* () {
135125
});
136126

137127
describe("remote thread lifecycle commands", () => {
138-
it.effect("offers background stop only while the destination grants thread operations", () =>
139-
Effect.gen(function* () {
140-
const h = yield* makeHarness();
141-
const permission = h.commands.interruptTurn.permissionAtom(ENVIRONMENT_ID);
142-
expect(h.registry.get(permission)).toBe(false);
143-
const session: AuthSessionState = {
144-
authenticated: true,
145-
auth: {
146-
policy: "remote-reachable",
147-
bootstrapMethods: [],
148-
sessionMethods: [],
149-
sessionCookieName: "test",
150-
},
151-
scopes: [AuthOrchestrationOperateScope],
152-
permissions: [AuthOrchestrationOperateScope],
153-
};
154-
h.registry.set(sessions(ENVIRONMENT_ID), AsyncResult.success(session));
155-
expect(h.registry.get(permission)).toBe(true);
156-
expect(
157-
h.registry.get(h.commands.interruptTurn.permissionAtom(EnvironmentId.make("other"))),
158-
).toBe(false);
159-
h.registry.set(
160-
sessions(ENVIRONMENT_ID),
161-
AsyncResult.success({ ...session, scopes: [], permissions: [] }),
162-
);
163-
expect(h.registry.get(permission)).toBe(false);
164-
}),
165-
);
166128
const actions = [
167129
["settle", {}, { settledOverride: "settled", pinnedAt: null, snoozedUntil: null }],
168130
["unsettle", { reason: "user" }, { settledOverride: "active", settledAt: null }],

‎packages/client-runtime/src/state/threadCommands.ts‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ import type { ThreadId } from "@t3tools/contracts";
22
import * as Crypto from "effect/Crypto";
33
import * as Effect from "effect/Effect";
44
import * as Option from "effect/Option";
5-
import { AsyncResult, Atom } from "effect/reactivity";
6-
import { createEnvironmentSessionAtoms } from "./session.ts";
5+
import { Atom } from "effect/reactivity";
76
import {
87
WS_METHODS,
9-
AuthOrchestrationOperateScope,
10-
sessionGrantsScope,
118
type EnvironmentId,
129
type OrchestrationV2ShellSnapshot,
1310
} from "@t3tools/contracts";
@@ -146,19 +143,6 @@ export function createThreadEnvironmentAtoms<R, E>(
146143
runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
147144
snapshotAtom: (environmentId: EnvironmentId) => Atom.Atom<OrchestrationV2ShellSnapshot | null>,
148145
) {
149-
const sessions = createEnvironmentSessionAtoms(runtime);
150-
const interruptPermissionAtom = Atom.family((environmentId: EnvironmentId | null) =>
151-
Atom.make((get) => {
152-
if (environmentId === null) return false;
153-
const result = get(sessions.sessionStateAtom(environmentId));
154-
const session = Option.getOrNull(AsyncResult.value(result));
155-
return (
156-
result._tag !== "Failure" &&
157-
session !== null &&
158-
sessionGrantsScope(session, AuthOrchestrationOperateScope)
159-
);
160-
}),
161-
);
162146
const scheduler = createAtomCommandScheduler();
163147
const concurrency = {
164148
mode: "serial" as const,
@@ -425,7 +409,6 @@ export function createThreadEnvironmentAtoms<R, E>(
425409
return {
426410
...commands,
427411
snapshotAtom: optimistic.snapshotAtom,
428-
interruptTurn: { ...commands.interruptTurn, permissionAtom: interruptPermissionAtom },
429412
settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
430413
!accepted &&
431414
(thread.pendingRuntimeRequest !== null ||

0 commit comments

Comments
 (0)