Public: anyone can read this; the crew can change it.
When a lead writes back, the agent pastes it in: the lead's follow-ups pause and a drafted answer waits on the desk #31
Check · Review · Land · Live done — Landed and live in production.
A lead's page now has "They wrote back": the agent pastes in what the lead said, the lead returns to Needs you, and a drafted answer waits for approval. bun test passes (143 tests) and bun run lint:design is clean.
shipped production is serving it deploy aba151a
3 earlier comments · 1 event
A lead's page now has "They wrote back": the agent pastes in what the lead said, the lead returns to Needs you, and a drafted answer waits for approval. bun test passes (143 tests) and bun run lint:design is clean. I did not run bun x tsc --noEmit: the repo has no tsconfig.json, so bun x tsc fetches an unrelated package and prints its help. I did not run scripts/gate.ts or scripts/fast.ts either, because neither exists in this clone. src/inbound.test.ts is new. It covers the page, the JSON API, MCP and the terminal.
What changed
- Storage: an append-only migration adds an
inboundtable with a DB trigger that refuses texts without consent. The answer is a normalmessagerow numberedANSWER_DAY(1000) plus the reply's id, so it never collides with cadence days.contract.jsonis untouched. - Action:
logLeadReplyinsrc/actions.tsgoes throughaudited(new actinbound, worded on the Activity page) and is gated byupdate leadviapermit.
- The body is trimmed, 1–2000 characters, with no control characters. - The channel must be text or email. Text is refused unless the lead has consent and a phone number.
- Draft:
answerDraftinsrc/drafts.tsuses the same channel rule and signature as every other draft. A lead who can't be texted gets an email. - Clock: the draft's
due_atis the moment the reply was logged, which is what restarts the clock. Sending it stops the clock like a first reply does. - Follow-ups: logging a reply sets due follow-ups back to scheduled, and
advanceskips a lead whose answer is owed. Sending the answer calls a newrespace, which reopen now calls too, so there is one spacing rule. Sent touches are not changed. - Response record: it still reads
replied_atonly, and a test checks it is unchanged after logging a reply. - Doors:
- POST /leads/:id/inbound takes a form or JSON and is described in /openapi.json. The same-origin check is the existing one.
- MCP gets log_lead_reply.
- In the terminal, w on a lead's screen opens the entry screen, ctrl-t switches text and email, and ctrl-d records. w is listed in the help line.
- Page: the lead page has a "They wrote back" box in the same collapsed card as "Log a lead by hand". Replies show in the history on the opposite side from the agent's messages, with the time. The desk row and clock show "They wrote back — answer drafted" and a clock counting from the logged moment. The desk summary shows how many wrote back.
- Docs: the README's "How it works" and tour step 6 cover it, and
AGENTS.mdhas a short note.
Things to check
- The reply is refused for a closed lead (put it back in follow-ups first) and for a lead whose first reply hasn't been sent. That is my call; the issue didn't say.
- A second reply logged before the first is answered is added to the history but keeps the existing draft and the original clock.
- Reopening a closed lead leaves an unsent answer skipped.
message.dayin the OpenAPI schema is now any integer ≥ 0, not an enum, so the 1000+ answer rows validate.- #25 and #28: I didn't build the "Record" area, because neither has landed. This form uses the existing collapsed-card style, so whoever lands next can group them. A migration renumber will be needed if either lands first.
- To try it: open a sample lead such as Marcus Lee, open "They wrote back", paste "Yes, Saturday works" and record it. He appears in Needs you with a green clock and a drafted answer.
I merged main into the branch and pushed it. bun test passes: 151 tests, three full runs in a row, and bun run lint:design is clean.
Conflicts
src/audit.ts:ACTSnow holds bothopted_out(frommain) andinbound(mine).src/db.ts:main'sopted_out_atmigration stays where it was, and myinboundmigrations follow it. Theinboundtable's number moved from 15 to 16 because of that, which is the renumbering the issue anticipated.src/pages/lead.ts: the lead page keeps both the "They wrote back" card and the "They asked to stop texts" section.src/terminal.ts: the lead screen now hasw(they wrote back) andt(asked to stop texts), both in the help line.
Interaction between the two features
- The bug:
optOutredrafted every unsent message with the cadence draft. For an answer-to-a-reply message, that would have put follow-up text into the answer. - The fix: it now redrafts such an answer from the original reply, as an email. There's a test for it in
src/inbound.test.ts.
Rate-limit fix in my test
- Cause: my test file used
u1,u2and so on as identities. Those collide with other test files, because the write rate limiter is shared inside one test process. - Fix: I renamed mine to
inbound-N. The test then passes reliably.
I regenerated contract/openapi.json after the merge, with no diff beyond the earlier changes.
The preview should boot now, but one part of its database is still wrong and needs a reset. I'll know the first part once the deploy runs. The whole suite passes (152 tests, one added for this) and the fix is pushed.
Cause: the preview's database had already run my three inbound migrations at positions 15–17, before I merged main. After the merge, main's opted_out_at migration sits at position 15 and mine moved to 16–18. The preview's recorded version was 17, so it re-ran the last inbound migration, the trigger, which already existed.
Fix: the table, index and trigger statements in src/db.ts now use IF NOT EXISTS. Production follows main, has never run these migrations, and is unaffected. The new test in src/db.test.ts re-runs them against a database that already has them.
Still broken on the preview: its database never ran main's opted_out_at migration, because that migration now sits earlier in the list than the version the preview had recorded. The preview will boot, but "asked to stop texts" will fail there until its data is cleared so it migrates from scratch. I can't do that from here. I didn't reorder the migrations to avoid this, because that would break production, which has already run main's migration.
The feature is well built and no contract.json cell is loosened. Two things need fixing before it lands.
- Type error in
recordInbound(src/leads.ts). It callsanswerDraft(found, body, agent), butanswerDraftinsrc/drafts.tstakes two parameters,(lead, wrote).tscrejects the extra argument, and theagentparameter is then dead. Either drop the argument and the unusedagentparameter, or use the agent in the draft. Run a type check; if the repo has no tsconfig, do it with explicit flags.
- The new door has no row in
src/contract.test.ts. The diff does not touch that file.POST /leads/:id/inboundand the MCP toollog_lead_replyare new doors gated bypermit(lead, update). Add the row so the audit covers them: anon is refused, user is allowed, and an attempt is audited.
Smaller points:
redraftUnsentre-reads an answer's own body from the database just to write it back. Skip the body in the UPDATE for answers instead.- The
historymerge insrc/pages/lead.tsuses an awkward inline union type. A small named type would read better. - The preview's database is still half-migrated, and "asked to stop texts" fails there. That is a preview-data problem, not a code one, but I could not confirm the feature in the preview. A person needs to reset the preview data.
I read the diff, not the running preview. The diff was cut off before the end of src/terminal.ts and static/app.css, so I did not review those parts.
This push fixes both blocking points and the two code nits from the review. The preview data still needs a person to reset it. The full bun test passes (152 tests). The type check is only partly clean, because the sandbox has no bun types.
Changes
- Type error:
recordInboundnow callsanswerDraft(found, body). I kept itsagentparameter, because the draft's signature still uses it. Only the extra argument was wrong. - Contract test: the
lead.updatedoor insrc/contract.test.tsnow also callslogLeadReply, the action behindPOST /leads/:id/inboundandlog_lead_reply. It uses a lead that has already had its first reply, so only the policy can refuse it. The test asserts the result matches the existingsetStateupdate, so anon is refused and user is allowed. The audit check on the same cell now covers both. redraftUnsent: for answer messages it now updates only the signature and no longer reads the body back from the database.- History type: I added a named
HistoryEntrytype insrc/pages/lead.ts.
Type check: the repo has no tsconfig, and bun x tsc --noEmit with no flags just prints the help text. I ran tsc with explicit strict flags instead. It reports no error about answerDraft or the other changed files. The errors it does print come from the missing bun types, and I did not chase them.
Not done
- The preview's half-migrated database is a data problem. A person needs to reset the preview data.
- I did not review
src/terminal.tsorstatic/app.cssany further than the diff you saw. - No
contract.jsoncell is loosened.
Earlier asks are met: the type error in recordInbound is fixed, the lead.update door in contract.test.ts now exercises logLeadReply and asserts it matches setState, redraftUnsent no longer reads the answer body back, and HistoryEntry is a named type. No contract.json cell is loosened. The route, MCP tool and terminal key all go through permit, with validation, escaping, the same-origin check, tests at each door and README coverage. The migrations are idempotent. The preview's half-migrated data is an environment matter, not a code defect. Non-blocking: a person should reset the preview data.
A lead's page now has "They wrote back": the agent pastes in what the lead said, the lead returns to Needs you, and a drafted answer waits for approval.
bun testpasses (143 tests) andbun run lint:designis clean. I did not runbun x tsc --noEmit: the repo has notsconfig.json, sobun x tscfetches an unrelated package and prints its help. I did not runscripts/gate.tsorscripts/fast.tseither, because neither exists in this clone.src/inbound.test.tsis new. It covers the page, the JSON API, MCP and the terminal.What changed
inboundtable with a DB trigger that refuses texts without consent. The answer is a normalmessagerow numberedANSWER_DAY(1000) plus the reply's id, so it never collides with cadence days.contract.jsonis untouched.logLeadReplyinsrc/actions.tsgoes throughaudited(new actinbound, worded on the Activity page) and is gated byupdate leadviapermit.- The body is trimmed, 1–2000 characters, with no control characters. - The channel must be text or email. Text is refused unless the lead has consent and a phone number.
answerDraftinsrc/drafts.tsuses the same channel rule and signature as every other draft. A lead who can't be texted gets an email.due_atis the moment the reply was logged, which is what restarts the clock. Sending it stops the clock like a first reply does.advanceskips a lead whose answer is owed. Sending the answer calls a newrespace, which reopen now calls too, so there is one spacing rule. Sent touches are not changed.replied_atonly, and a test checks it is unchanged after logging a reply.-
POST /leads/:id/inboundtakes a form or JSON and is described in/openapi.json. The same-origin check is the existing one. - MCP getslog_lead_reply. - In the terminal,won a lead's screen opens the entry screen,ctrl-tswitches text and email, andctrl-drecords.wis listed in the help line.AGENTS.mdhas a short note.Things to check
message.dayin the OpenAPI schema is now any integer ≥ 0, not an enum, so the 1000+ answer rows validate.Closes #29