clawwork-ai / clawwork-ai/ClawWork

[Cleanup] Extract duplicated message-merge logic in syncFromGateway

Open Beginner friendly
#228 2 comments 0 reactions 0 assignees View on GitHub
area/core kind/cleanup
Dominant language
TypeScript
Stars
532
Forks
75
Avg merge
5h 31m
Merged PRs (30d)
1

Description

## What problem are you trying to solve?

`syncFromGateway` in session-sync has two branches (`hasLocalData` / `!hasLocalData`) that contain nearly identical message mapping and persistence logic (~50 lines of copy-paste). This makes the function harder to maintain — a fix applied to one branch can easily be missed in the other.

## Where

`packages/core/src/services/session-sync.ts` — the `syncFromGateway` function, around lines 280-330.

## What needs to be done

1. Open `packages/core/src/services/session-sync.ts`
2. Find the `syncFromGateway` function
3. Identify the duplicated pattern between the `hasLocalData` and `!hasLocalData` branches — both do:
- Map `collapsedMessages` into `Message[]` with sessionKey/agentId assignment
- Call `messageStore.bulkLoad(taskId, mapped)`
- Persist each message via `deps.persistence.persistMessage()`
4. Extract the common logic into a local helper function (e.g. `loadAndPersistMessages`)
5. Call the helper from both branches, passing only the differing parameters
6. Run `pnpm check` to verify nothing breaks

## Why does this matter?

Duplicated logic is a maintenance hazard — when one branch gets updated, the other often gets forgotten. This is a straightforward extract-function refactor.

## Primary area

Task execution

Contributor guide

Open the contributing guide

Research direction

Open packages/core/src/services/session-sync.ts and read syncFromGateway around lines 280-330. Compare the hasLocalData and !hasLocalData branches, then extract their shared message-loading and persistence logic into a local helper with only the differing parameters. Run pnpm check; done means both branches use the helper and the check passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.