@drawbridge/drawbridge-agents 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/claude/CLAUDE.md
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
@../conventions/karpathy-guidelines.md
|
|
1
2
|
@../conventions/rules.md
|
|
2
3
|
@../conventions/javascript-formatting.md
|
|
3
4
|
@../conventions/nested-objects.md
|
|
4
5
|
@../conventions/jsx-fragments.md
|
|
5
6
|
@../conventions/transactions.md
|
|
7
|
+
@../conventions/cascade-cleanup.md
|
|
8
|
+
@../conventions/dry.md
|
|
6
9
|
@../conventions/property-shorthand.md
|
|
7
10
|
@../conventions/drawbridge-packages.md
|
|
8
11
|
@../conventions/sentry-sdk.md
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Cascade cleanup belongs in drawbridge-sync
|
|
2
|
+
|
|
3
|
+
When a Drawbridge document delete should trigger cleanup of dependent rows in another collection — steps tied to a workflow, workflows tied to a connection, exports tied to a user, anything where deleting X requires also deleting Y — the cleanup belongs in a change-stream listener in `drawbridge-sync`, **not** inline in the API route handler that triggered the original delete.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
- API routes stay focused on the user's request. Cascade work shouldn't block the HTTP response or balloon the request's transaction scope.
|
|
8
|
+
- A sync-side listener fires on **every** delete path: the direct route, a cascade from a different route, an admin tool, a manual Mongo write. An inline cascade only runs on the one route that has it.
|
|
9
|
+
- Change streams are configured with `fullDocumentBeforeChange: 'required'`, so the listener receives the full prior document and has everything it needs to reason about the dependents.
|
|
10
|
+
|
|
11
|
+
## How to apply
|
|
12
|
+
|
|
13
|
+
- In an API route delete handler, do **not** add `controllers.base.delete` (or `bulk` with `deleteMany`) for child collections. Just delete the parent. The sync listener cleans up.
|
|
14
|
+
- Add the cleanup handler in `drawbridge-sync/queue/<collection>.js`, in the `handlers.delete` slot, signature `async ({ fullDocumentBeforeChange : doc }) => { ... }`. Copy the shape from `queue/user.js` or `queue/organization.js`.
|
|
15
|
+
- For the listener to fire at all, the collection must be in both:
|
|
16
|
+
- `drawbridge-sync/stream.js` `collections` array
|
|
17
|
+
- `drawbridge-sync/lib/queue.js` queue map
|
|
18
|
+
- **And** registered in `drawbridge-sync/queue/index.js`'s workers array.
|
|
19
|
+
- If any of these are missing, change-stream events get enqueued and silently dropped, or never enqueued at all. Audit all four sites when adding a new collection.
|
|
20
|
+
- For deep cascades (parent → child → grandchild), let the chain ride the change streams: the child's own listener handles its own dependents. Don't reach down two levels from the parent's listener.
|
|
21
|
+
|
|
22
|
+
## Counter-examples (these stay inline in the API)
|
|
23
|
+
|
|
24
|
+
- Decrementing `organization.totals.<resource>` and `usage.totals.<resource>` on delete. These are counter mutations against the *same* request, not cleanup of dependent rows. Done inline in the API route handler.
|
|
25
|
+
- The deleted document itself. The route deletes it; the listener reacts to the deletion.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Don't Repeat Yourself (DRY)
|
|
2
|
+
|
|
3
|
+
Before writing a new function or handler that *does work* (external API call, doc creation, counter increment, cascade pattern, dispatch), check whether existing code already does that work. If so, **reuse it**. If the same work happens in 2+ places, **extract a helper** before adding a third copy.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Repeated implementations drift. Two paths that did the same thing yesterday do *almost* the same thing tomorrow — and the divergence is the bug. Examples from this codebase:
|
|
8
|
+
|
|
9
|
+
- `step.sendgrid.email.send` was implemented as a separate handler that called `sendgrid.send()` directly, while `queue/notification.js`'s `send()` already did exactly that with the right provider selection from org connections. Two paths, same external service, slightly different code — a future SendGrid auth change would have to be made in both.
|
|
10
|
+
- `usage.totals.actions` increments are hand-rolled in `stream/otc.js`, `queue/segment.js`, `queue/contact.js`, and historically `queue/workflow.js` — billing logic spread across 4+ files, each independent.
|
|
11
|
+
- Connection-fetch + `decrypt(connection.settings)` appears 7+ times across `stream/` and `queue/`. The `status: 'active'` guard is implicit in some callers and missing in others — a latent security gap.
|
|
12
|
+
|
|
13
|
+
Each pattern is small individually. Together they make debugging issues like "why is the action count wrong?" require grepping every counter increment to find the right one.
|
|
14
|
+
|
|
15
|
+
## How to apply
|
|
16
|
+
|
|
17
|
+
1. **Name the existing function you're calling before writing the new function.** "I'll call `send()` from `queue/notification.js`" — not "I'll call `sendgrid.send()` directly". If the function you'd call doesn't exist yet, you might still need to write it — but first verify it's not just one grep away.
|
|
18
|
+
2. **Grep the work, not the name.** Before adding a `controller.update({ $inc: { 'totals.X': … }})`, grep for `totals.X` to find every other site doing the same. Same for SDK calls (`sendgrid.send`, `stripe.customers.update`, etc.).
|
|
19
|
+
3. **At 2 copies, extract.** If you find the same shape in two places, factor before adding a third. Don't wait for the 5th.
|
|
20
|
+
4. **Centralize step handlers, queue workers, and lifecycle emitters in one file each.** drawbridge-sync's pattern: all `step.*` bodies live in `queue/step.js`; all messaging delivery routes through `queue/notification.js`'s `send()`; per-collection cleanup lives in `stream/<collection>.js`. Don't sprinkle related logic across multiple files for no reason.
|
|
21
|
+
|
|
22
|
+
## Watch list
|
|
23
|
+
|
|
24
|
+
Patterns that have been duplicated in this family before — confirm before adding new instances:
|
|
25
|
+
|
|
26
|
+
- External SDK calls (SendGrid / Mailchimp / Twilio / Stripe / Shopify Admin / Shopify Storefront)
|
|
27
|
+
- `controller.update({ $inc: { 'totals.X': … }})` counter increments on the `usage`, `organization`, `campaign`, `page`, `range`, `lead` collections
|
|
28
|
+
- `controller.get({ collection: 'connection', … })` followed by `decrypt(connection.settings)`
|
|
29
|
+
- Notification doc creation (`controller.create({ collection: 'notification', … })`) when you actually want to send an email/SMS — use `send()` directly
|
|
30
|
+
- Cache invalidation chains where multiple `cache.delete([…])` calls fire as a pair
|
|
31
|
+
- `logger.emit('<entity>.<verb>', …)` lifecycle telemetry wrappers
|
|
32
|
+
- Cascade-delete patterns (delete X → also cleanup Y, Z) — these belong as stream listeners in `drawbridge-sync`, see `cascade-cleanup.md`
|
|
33
|
+
|
|
34
|
+
If you find a duplicate while implementing something new, surface it: either factor it as part of the same change, or document it and propose a follow-up cleanup PR.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Karpathy coding guidelines
|
|
2
|
+
|
|
3
|
+
Adapted from Andrej Karpathy's observations on common LLM coding mistakes — see https://github.com/multica-ai/andrej-karpathy-skills. These are general behavioral guidelines that bias toward caution over speed; use judgment for trivial tasks.
|
|
4
|
+
|
|
5
|
+
## 1. Think Before Coding
|
|
6
|
+
|
|
7
|
+
**Don't assume. Don't hide confusion. Surface tradeoffs.**
|
|
8
|
+
|
|
9
|
+
Before implementing:
|
|
10
|
+
- State your assumptions explicitly. If uncertain, ask.
|
|
11
|
+
- If multiple interpretations exist, present them — don't pick silently.
|
|
12
|
+
- If a simpler approach exists, say so. Push back when warranted.
|
|
13
|
+
- If something is unclear, stop. Name what's confusing. Ask.
|
|
14
|
+
|
|
15
|
+
## 2. Simplicity First
|
|
16
|
+
|
|
17
|
+
**Minimum code that solves the problem. Nothing speculative.**
|
|
18
|
+
|
|
19
|
+
- No features beyond what was asked.
|
|
20
|
+
- No abstractions for single-use code.
|
|
21
|
+
- No "flexibility" or "configurability" that wasn't requested.
|
|
22
|
+
- No error handling for impossible scenarios.
|
|
23
|
+
- If you write 200 lines and it could be 50, rewrite it.
|
|
24
|
+
|
|
25
|
+
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
|
|
26
|
+
|
|
27
|
+
## 3. Surgical Changes
|
|
28
|
+
|
|
29
|
+
**Touch only what you must. Clean up only your own mess.**
|
|
30
|
+
|
|
31
|
+
When editing existing code:
|
|
32
|
+
- Don't "improve" adjacent code, comments, or formatting.
|
|
33
|
+
- Don't refactor things that aren't broken.
|
|
34
|
+
- Match existing style, even if you'd do it differently.
|
|
35
|
+
- If you notice unrelated dead code, mention it — don't delete it.
|
|
36
|
+
|
|
37
|
+
When your changes create orphans:
|
|
38
|
+
- Remove imports/variables/functions that YOUR changes made unused.
|
|
39
|
+
- Don't remove pre-existing dead code unless asked.
|
|
40
|
+
|
|
41
|
+
The test: Every changed line should trace directly to the user's request.
|
|
42
|
+
|
|
43
|
+
## 4. Goal-Driven Execution
|
|
44
|
+
|
|
45
|
+
**Define success criteria. Loop until verified.**
|
|
46
|
+
|
|
47
|
+
Transform tasks into verifiable goals:
|
|
48
|
+
- "Add validation" → "Write tests for invalid inputs, then make them pass"
|
|
49
|
+
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
|
|
50
|
+
- "Refactor X" → "Ensure tests pass before and after"
|
|
51
|
+
|
|
52
|
+
For multi-step tasks, state a brief plan:
|
|
53
|
+
```
|
|
54
|
+
1. [Step] → verify: [check]
|
|
55
|
+
2. [Step] → verify: [check]
|
|
56
|
+
3. [Step] → verify: [check]
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
**These guidelines are working if:** fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.
|
package/package.json
CHANGED