Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: scope useAppOperation onComplete to the targeted app (#8111)

[._.]/ Adam Eivyf5c38096bf19
diff --git a/client/src/hooks/useAppOperation.js b/client/src/hooks/useAppOperation.js
index b51a980a02..2f6d02296c 100644
--- a/client/src/hooks/useAppOperation.js
+++ b/client/src/hooks/useAppOperation.js
@@ -150,11 +150,20 @@ export function useAppOperation({ onComplete, appId: scopeAppId } = {}) {
: { ...current, steps, completed: true };
});
const appId = data?.appId || scopeAppId;
- if (appId) {
+ if (appId && inScope(appId)) {
+ // Scoped consumer in scope, or unscoped consumer with a resolvable app ID.
clearTimeout(clearTimersRef.current[appId]);
clearTimersRef.current[appId] = setTimeout(() => drop(appId), CLEAR_DELAY_MS);
+ onCompleteRef.current?.();
+ } else if (!scopeAppId) {
+ // Unscoped consumer always fires the callback, even for legacy frames with no app ID.
+ // Still schedule the timer only if we have an app ID to clean up.
+ onCompleteRef.current?.();
+ if (appId) {
+ clearTimeout(clearTimersRef.current[appId]);
+ clearTimersRef.current[appId] = setTimeout(() => drop(appId), CLEAR_DELAY_MS);
+ }
}
- onCompleteRef.current?.();
};
const requestActive = () => socket.emit('app:operations:list');
diff --git a/client/src/hooks/useAppOperation.test.jsx b/client/src/hooks/useAppOperation.test.jsx
index b683cd3cd2..a04cd0032c 100644
--- a/client/src/hooks/useAppOperation.test.jsx
+++ b/client/src/hooks/useAppOperation.test.jsx
@@ -120,3 +120,136 @@ describe('useAppOperation — PortOS self-update handoff', () => {
expect(mockCheckHealth).not.toHaveBeenCalled();
});
});
+
+describe('useAppOperation — scoped onComplete callback', () => {
+ beforeEach(() => {
+ socketHandlers.clear();
+ emitted.length = 0;
+ vi.clearAllMocks();
+ });
+
+ afterEach(() => { vi.useRealTimers(); });
+
+ it('calls onComplete only for the scoped app, not for other apps', () => {
+ const onCompleteA = vi.fn();
+ const onCompleteB = vi.fn();
+
+ renderHook(() => useAppOperation({ appId: 'app-a', onComplete: onCompleteA }));
+ renderHook(() => useAppOperation({ appId: 'app-b', onComplete: onCompleteB }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ appId: 'app-b',
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onCompleteA).not.toHaveBeenCalled();
+ expect(onCompleteB).toHaveBeenCalledTimes(1);
+ });
+
+ it('calls onComplete for scoped app on its completion frame', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ appId: 'app-a', onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ appId: 'app-a',
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ });
+
+ it('calls onComplete on a legacy frame for a scoped app', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ appId: 'app-a', onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ });
+
+ it('calls onComplete for every completion on an unscoped consumer', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ appId: 'app-a',
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ appId: 'app-b',
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(2);
+ });
+
+ it('ignores scoped completion frames for apps with failed status', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ appId: 'app-a', onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ appId: 'app-a',
+ success: false,
+ steps: [{ step: 'failed', status: 'error' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not call onComplete for out-of-scope apps on standardize completion', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ appId: 'app-a', onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:standardize:complete')({
+ appId: 'app-b',
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).not.toHaveBeenCalled();
+ });
+
+ it('calls onComplete for legacy frames on unscoped consumers', () => {
+ const onComplete = vi.fn();
+
+ renderHook(() => useAppOperation({ onComplete }));
+
+ act(() => {
+ socketHandlers.get('app:update:complete')({
+ success: true,
+ steps: [{ step: 'done', status: 'success' }],
+ });
+ });
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ });
+});