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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

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.

Notes

shipped production is serving it deploy aba151a

opened this

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 inbound table with a DB trigger that refuses texts without consent. The answer is a normal message row numbered ANSWER_DAY (1000) plus the reply's id, so it never collides with cadence days. contract.json is untouched.
  • Action: logLeadReply in src/actions.ts goes through audited (new act inbound, worded on the Activity page) and is gated by update lead via permit.

- 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: answerDraft in src/drafts.ts uses 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_at is 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 advance skips a lead whose answer is owed. Sending the answer calls a new respace, which reopen now calls too, so there is one spacing rule. Sent touches are not changed.
  • Response record: it still reads replied_at only, 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.md has 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.day in 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.

Closes #29

3 earlier comments · 1 event
commented

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 inbound table with a DB trigger that refuses texts without consent. The answer is a normal message row numbered ANSWER_DAY (1000) plus the reply's id, so it never collides with cadence days. contract.json is untouched.
  • Action: logLeadReply in src/actions.ts goes through audited (new act inbound, worded on the Activity page) and is gated by update lead via permit.

- 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: answerDraft in src/drafts.ts uses 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_at is 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 advance skips a lead whose answer is owed. Sending the answer calls a new respace, which reopen now calls too, so there is one spacing rule. Sent touches are not changed.
  • Response record: it still reads replied_at only, 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.md has 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.day in 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.
commented

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: ACTS now holds both opted_out (from main) and inbound (mine).
  • src/db.ts: main's opted_out_at migration stays where it was, and my inbound migrations follow it. The inbound table'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 has w (they wrote back) and t (asked to stop texts), both in the help line.

Interaction between the two features

  • The bug: optOut redrafted 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, u2 and 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.

commented

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.

Changes requested — before the latest push

The feature is well built and no contract.json cell is loosened. Two things need fixing before it lands.

  1. Type error in recordInbound (src/leads.ts). It calls answerDraft(found, body, agent), but answerDraft in src/drafts.ts takes two parameters, (lead, wrote). tsc rejects the extra argument, and the agent parameter is then dead. Either drop the argument and the unused agent parameter, or use the agent in the draft. Run a type check; if the repo has no tsconfig, do it with explicit flags.
  1. The new door has no row in src/contract.test.ts. The diff does not touch that file. POST /leads/:id/inbound and the MCP tool log_lead_reply are new doors gated by permit(lead, update). Add the row so the audit covers them: anon is refused, user is allowed, and an attempt is audited.

Smaller points:

  • redraftUnsent re-reads an answer's own body from the database just to write it back. Skip the body in the UPDATE for answers instead.
  • The history merge in src/pages/lead.ts uses 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.

commented

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: recordInbound now calls answerDraft(found, body). I kept its agent parameter, because the draft's signature still uses it. Only the extra argument was wrong.
  • Contract test: the lead.update door in src/contract.test.ts now also calls logLeadReply, the action behind POST /leads/:id/inbound and log_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 existing setState update, 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 HistoryEntry type in src/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.ts or static/app.css any further than the diff you saw.
  • No contract.json cell is loosened.
Approved

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.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs