Skip to content

[Fix] Clean up taskGateways on initConductor failure (#212)#540

Open
advancedresearcharray wants to merge 3 commits into
clawwork-ai:mainfrom
advancedresearcharray:fix/212-task-gateways-cleanup
Open

[Fix] Clean up taskGateways on initConductor failure (#212)#540
advancedresearcharray wants to merge 3 commits into
clawwork-ai:mainfrom
advancedresearcharray:fix/212-task-gateways-cleanup

Conversation

@advancedresearcharray

Copy link
Copy Markdown
Contributor

Summary

  • Extends cleanupRoomResources() to also remove verifyInFlight entries on terminal room cleanup
  • Calls cleanupRoomResources() when initConductor() fails (createSession returns { ok: false }) or throws, preventing taskGateways Map leaks on abandoned failed tasks
  • Adds regression tests for failed session creation and thrown errors, each followed by successful retry

PR #517 already cleaned up taskGateways when a room reaches stopped; this hardens the failure paths that PR did not cover.

Closes #212

Test plan

  • pnpm --filter @clawwork/core test — 111 passed (21 room-store tests)
  • New tests verify retry after initConductor failure/throw uses a fresh gateway id

Made with Cursor

advancedresearcharray and others added 3 commits June 10, 2026 22:49
Validate agent catalog lines structurally before embedding them in the
conductor system prompt, and add regression tests for catalog and user-task
sanitization paths used by initConductor.

Closes clawwork-ai#211

Signed-off-by: advancedresearcharray <advancedresearcharray@github.com>
No code changes — refreshes required checks for merge after /approve.
PR clawwork-ai#517 already removes taskGateways entries when a room reaches stopped.
Also purge module-scoped maps when conductor session creation fails or throws,
and drop any in-flight verify lock on terminal cleanup.

Closes clawwork-ai#212

Signed-off-by: Array Fleet <fleet@advancedresearcharray.local>
Co-authored-by: Cursor <cursoragent@cursor.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the robustness and security of the room conductor initialization process. It ensures that system resources are properly cleaned up when session creation fails or errors occur, preventing potential memory leaks. Additionally, it significantly hardens the sanitization logic for agent catalogs and user tasks to prevent prompt injection attacks via control characters and malformed input.

Highlights

  • Resource Cleanup: Extended cleanupRoomResources() to remove verifyInFlight entries and integrated it into initConductor() failure and error paths to prevent memory leaks.
  • Prompt Sanitization: Hardened sanitizeAgentCatalog and sanitizeUserTask against various injection techniques, including control characters, Unicode line separators, and malformed catalog lines.
  • Testing: Added comprehensive regression tests for initConductor failure scenarios and a suite of security tests for catalog and user task sanitization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @advancedresearcharray,
Thanks for your pull request!
If the PR is ready, use the /auto-cc command to assign Reviewer to Review.
We will review it shortly.

Details

Instructions for interacting with me using comments are available here.
If you have questions or suggestions related to my behavior, please file an issue against the gh-ci-bot repository.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces robust sanitization for agent catalogs and user tasks to prevent prompt injection attacks, along with comprehensive unit tests validating these security measures. It also ensures that room resources are cleaned up when conductor initialization fails or throws an error. The reviewer feedback highlights an issue where failed rooms are left in an inconsistent active state in the store, recommending that they be explicitly removed from the store's state and verified in the tests.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 119 to 122
if (!res.ok) {
cleanupRoomResources(taskId);
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When initConductor fails to create a session, the room is left in the store's rooms state with status: 'active' and conductorReady: false. This leads to an inconsistent state where a failed room is treated as active. We should remove the room from the store's state to maintain consistency.

Suggested change
if (!res.ok) {
cleanupRoomResources(taskId);
return false;
}
if (!res.ok) {
cleanupRoomResources(taskId);
set((s) => {
const nextRooms = { ...s.rooms };
delete nextRooms[taskId];
return { rooms: nextRooms };
});
return false;
}

Comment on lines 129 to 133
} catch (err) {
console.warn('[room-store] initConductor failed:', err);
cleanupRoomResources(taskId);
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When initConductor throws an error, the room is left in the store's rooms state with status: 'active' and conductorReady: false. We should remove the room from the store's state to maintain consistency.

      } catch (err) {
        console.warn('[room-store] initConductor failed:', err);
        cleanupRoomResources(taskId);
        set((s) => {
          const nextRooms = { ...s.rooms };
          delete nextRooms[taskId];
          return { rooms: nextRooms };
        });
        return false;
      }

Comment on lines +191 to +192
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add an assertion to verify that the room is successfully removed from the store's state when initConductor fails to create the session.

Suggested change
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);
expect(store.getState().getRoom(taskId)).toBeUndefined();

Comment on lines +213 to +214
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add an assertion to verify that the room is successfully removed from the store's state when initConductor throws an error.

Suggested change
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);
const failed = await store.getState().initConductor(taskId, 'gw-1', sessionKey, '[]');
expect(failed).toBe(false);
expect(store.getState().getRoom(taskId)).toBeUndefined();

@advancedresearcharray

Copy link
Copy Markdown
Contributor Author

@advancedresearcharray PR #540 is merge-ready. No changes needed. CI is green and review is complete.

@advancedresearcharray

Copy link
Copy Markdown
Contributor Author

PR is ready for merge! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] taskGateways Map in room-store never cleaned up

1 participant