Public: anyone can read this; the crew can change it.
A reminder held until tomorrow comes back with the words the agent edited, not the default draft #110
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/107-a-reminder-held-until-tomorrow-comes-bac → main 2 commits fork 1 thread · 1 unresolved
8 files
changed +137
−18
contract/openapi.json +12 −2
| @@ -1469,7 +1469,7 @@ | ||
| 1469 | 1469 | }, |
| 1470 | 1470 | "/api/deals/{id}/reminders/hold": { |
| 1471 | 1471 | "post": { |
| 1472 | "summary": "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Held again, it waits one more day. A status update is not held.", | |
| 1472 | "summary": "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any words already held. Held again, it waits one more day. A status update is not held.", | |
| 1473 | 1473 | "operationId": "postApiDealsIdRemindersHold", |
| 1474 | 1474 | "parameters": [ |
| 1475 | 1475 | { |
| @@ -1501,6 +1501,16 @@ | ||
| 1501 | 1501 | "late", |
| 1502 | 1502 | "moved" |
| 1503 | 1503 | ] |
| 1504 | }, | |
| 1505 | "subject": { | |
| 1506 | "type": "string", | |
| 1507 | "minLength": 1, | |
| 1508 | "maxLength": 150 | |
| 1509 | }, | |
| 1510 | "body": { | |
| 1511 | "type": "string", | |
| 1512 | "minLength": 1, | |
| 1513 | "maxLength": 2000 | |
| 1504 | 1514 | } |
| 1505 | 1515 | }, |
| 1506 | 1516 | "required": [ |
| @@ -1514,7 +1524,7 @@ | ||
| 1514 | 1524 | }, |
| 1515 | 1525 | "responses": { |
| 1516 | 1526 | "201": { |
| 1517 | "description": "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Held again, it waits one more day. A status update is not held.", | |
| 1527 | "description": "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any words already held. Held again, it waits one more day. A status update is not held.", | |
| 1518 | 1528 | "content": { |
| 1519 | 1529 | "application/json": { |
| 1520 | 1530 | "schema": { |
src/db.ts +3
| @@ -395,6 +395,9 @@ export const migrations: string[] = [ | ||
| 395 | 395 | `ALTER TABLE deal ADD COLUMN fell_at TEXT`, |
| 396 | 396 | `ALTER TABLE deal ADD COLUMN fell_reason TEXT`, |
| 397 | 397 | `ALTER TABLE deal ADD COLUMN fell_by TEXT`, |
| 398 | // The words the agent edited before holding a reminder; '' while none were held with it. | |
| 399 | `ALTER TABLE reminder_hold ADD COLUMN subject TEXT NOT NULL DEFAULT ''`, | |
| 400 | `ALTER TABLE reminder_hold ADD COLUMN body TEXT NOT NULL DEFAULT ''`, | |
| 398 | 401 | ]; |
| 399 | 402 | |
| 400 | 403 | export function openDb(dir = dataDir): Database { |
src/deals.ts +15 −5
| @@ -97,6 +97,9 @@ export interface Held { | ||
| 97 | 97 | /** The day it was held. */ |
| 98 | 98 | readonly held_on: string; |
| 99 | 99 | readonly by_name: string; |
| 100 | /** The words held with it; '' while none were. */ | |
| 101 | readonly subject: string; | |
| 102 | readonly body: string; | |
| 100 | 103 | } |
| 101 | 104 | export interface Sent { |
| 102 | 105 | readonly id: number; |
| @@ -1165,7 +1168,7 @@ export function declineReminder( | ||
| 1165 | 1168 | export function holdReminder( |
| 1166 | 1169 | db: Database, |
| 1167 | 1170 | who: Who, |
| 1168 | input: { deadlineId: number; dealId?: number; partyId: number; moment: unknown; agent?: Agent; on?: string }, | |
| 1171 | input: { deadlineId: number; dealId?: number; partyId: number; moment: unknown; subject?: unknown; body?: unknown; agent?: Agent; on?: string }, | |
| 1169 | 1172 | ): Result<Held> { |
| 1170 | 1173 | const { dealId, found } = draftOf(db, who, input); |
| 1171 | 1174 | if (!found.ok) return within(db, who, "reminder.hold", dealId, found); |
| @@ -1179,13 +1182,20 @@ export function holdReminder( | ||
| 1179 | 1182 | const day = input.on ?? today(); |
| 1180 | 1183 | if (draft.on > day) |
| 1181 | 1184 | return within(db, who, "reminder.hold", dealId, fail("invalid", `This reminder goes out on ${spoken(draft.on)}; it can be held from that morning.`)); |
| 1185 | const subject = input.subject === undefined ? undefined : line(input.subject, 1, SUBJECT_MAX); | |
| 1186 | if (input.subject !== undefined && subject === undefined) | |
| 1187 | return within(db, who, "reminder.hold", dealId, fail("invalid", `The subject is 1 to ${SUBJECT_MAX} characters.`)); | |
| 1188 | const body = input.body === undefined ? undefined : message(input.body, 1, BODY_MAX); | |
| 1189 | if (input.body !== undefined && body === undefined) | |
| 1190 | return within(db, who, "reminder.hold", dealId, fail("invalid", `The message is 1 to ${BODY_MAX} characters.`)); | |
| 1182 | 1191 | const until = addDays(draft.held !== null && draft.held.until > day ? draft.held.until : day, 1); |
| 1183 | 1192 | const held = db |
| 1184 | .query<Held, [number, number, number, string, string, number, string, string, string]>( | |
| 1185 | `INSERT INTO reminder_hold (deal_id, deadline_id, party_id, moment, due, move_id, until, held_on, by_name) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) | |
| 1186 | ON CONFLICT (deadline_id, party_id, moment, due, move_id) DO UPDATE SET until = excluded.until, held_on = excluded.held_on, by_name = excluded.by_name, at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') RETURNING *`, | |
| 1193 | .query<Held, [number, number, number, string, string, number, string, string, string, string, string]>( | |
| 1194 | `INSERT INTO reminder_hold (deal_id, deadline_id, party_id, moment, due, move_id, until, held_on, by_name, subject, body) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11) | |
| 1195 | ON CONFLICT (deadline_id, party_id, moment, due, move_id) DO UPDATE SET until = excluded.until, held_on = excluded.held_on, by_name = excluded.by_name, | |
| 1196 | subject = CASE WHEN ?10 = '' THEN subject ELSE ?10 END, body = CASE WHEN ?11 = '' THEN body ELSE ?11 END, at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') RETURNING *`, | |
| 1187 | 1197 | ) |
| 1188 | .get(deal.id, draft.deadline_id, draft.party_id, draft.moment, draft.due, draft.move_id, until, day, authorOf(who).name)!; | |
| 1198 | .get(deal.id, draft.deadline_id, draft.party_id, draft.moment, draft.due, draft.move_id, until, day, authorOf(who).name, subject ?? "", body ?? "")!; | |
| 1189 | 1199 | return within(db, who, `reminder.hold:${until}`, dealId, ok(held)); |
| 1190 | 1200 | } |
| 1191 | 1201 |
src/hold.test.ts +96
| @@ -133,3 +133,99 @@ test("Tomorrow is a form on the review page and posts; another origin and an ano | ||
| 133 | 133 | // The deal's sheet offers it too, and the API documents the route. |
| 134 | 134 | expect(await (await fetch(`${base}/openapi.json`, { headers: signed })).text()).toContain("/api/deals/{id}/reminders/hold"); |
| 135 | 135 | }); |
| 136 | ||
| 137 | // ---- the words held with it ---- | |
| 138 | ||
| 139 | const otherDraft: Draft = mine(todaysDrafts([fresh()], now, agent)).find((d) => d.moment === "before" && d.party_id !== first.party_id)!; | |
| 140 | const otherAsk = () => ({ dealId: deal.id, deadlineId: otherDraft.deadline_id, partyId: otherDraft.party_id, moment: otherDraft.moment }); | |
| 141 | const backTomorrow = () => mine(todaysDrafts([fresh()], tomorrow, agent)).find((d) => d.party_id === otherDraft.party_id && d.moment === "before")!; | |
| 142 | ||
| 143 | test("edited words held today come back tomorrow, in /tick's list too, and are what is sent", async () => { | |
| 144 | const k = otherAsk(); | |
| 145 | const held = holdReminder(db, alice, { ...k, subject: "My subject", body: "My own words" }); | |
| 146 | expect(held.ok).toBe(true); | |
| 147 | const back = mine(dueOn([fresh()], tomorrow, agent)).find((d) => d.party_id === k.partyId && d.moment === "before")!; | |
| 148 | expect(back.subject).toBe("My subject"); | |
| 149 | expect(back.body).toBe("My own words"); | |
| 150 | expect(backTomorrow().body).toBe("My own words"); | |
| 151 | // A plain hold keeps the words held earlier; holding again with words keeps the newest. | |
| 152 | expect(holdReminder(db, alice, { ...k, on: tomorrow }).ok).toBe(true); | |
| 153 | expect(holdReminder(db, alice, { ...k, on: tomorrow, body: "Newer words" }).ok).toBe(true); | |
| 154 | const later = mine(todaysDrafts([fresh()], addDays(now, 3), agent)).find((d) => d.party_id === k.partyId && d.moment === "before")!; | |
| 155 | expect(later.subject).toBe("My subject"); | |
| 156 | expect(later.body).toBe("Newer words"); | |
| 157 | // Sent without edits, the record shows the held words. | |
| 158 | const sent = sendReminder(db, alice, k); | |
| 159 | expect(sent.ok && sent.value.body).toBe("Newer words"); | |
| 160 | expect(sent.ok && sent.value.subject).toBe("My subject"); | |
| 161 | }); | |
| 162 | ||
| 163 | test("an over-long or empty held word is a 422 that keeps what was typed, and nothing is held", async () => { | |
| 164 | const k = otherAsk(); | |
| 165 | const bad = holdReminder(db, alice, { ...k, body: "x".repeat(2001) }); | |
| 166 | expect(!bad.ok && bad.error).toBe("invalid"); | |
| 167 | expect(holdReminder(db, alice, { ...k, subject: "s".repeat(151) }).ok).toBe(false); | |
| 168 | expect(holdReminder(db, alice, { ...k, subject: "" }).ok).toBe(false); | |
| 169 | const page = await (await fetch(`${base}/reminders/today`, { headers: signed })).text(); | |
| 170 | const hidden = (name: string) => new RegExp(`name="${name}" value="([^"]*)"`).exec(page)![1]!; | |
| 171 | const keys = { deadline: hidden("deadline"), party: hidden("party"), moment: hidden("moment"), at: "0", done: "0", q: "" }; | |
| 172 | const long = "y".repeat(2001); | |
| 173 | const res = await post("/reminders/today/hold", { ...keys, subject: "Kept subject", body: long }); | |
| 174 | expect(res.status).toBe(422); | |
| 175 | const text = await res.text(); | |
| 176 | expect(text).toContain("Kept subject"); | |
| 177 | expect(text).toContain(long); | |
| 178 | }); | |
| 179 | ||
| 180 | // ---- the API and the MCP tool carry the words ---- | |
| 181 | ||
| 182 | const send = (method: string, path: string, body?: unknown) => | |
| 183 | fetch(base + path, { method, headers: { ...signed, "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) }); | |
| 184 | const openOnServer = async (address: string): Promise<number> => { | |
| 185 | const res = await send("POST", "/api/deals", { address, buyer: "Ann Buyer", seller: "Sam Seller", contract_date: addDays(now, -5), closing_date: addDays(now, 40), inspection: 8 }); | |
| 186 | expect(res.status).toBe(201); | |
| 187 | return ((await res.json()) as { id: number }).id; | |
| 188 | }; | |
| 189 | const mcp = async (name: string, args: object): Promise<any> => { | |
| 190 | const out = (await (await send("POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } })).json()) as any; | |
| 191 | return out.result; | |
| 192 | }; | |
| 193 | const todayOf = async (address: string, party?: number): Promise<any[]> => | |
| 194 | (JSON.parse((await mcp("reminders_today", {})).content[0].text) as any[]).filter((d) => d.address === address && d.moment === "before" && (party === undefined || d.party_id === party)); | |
| 195 | const heldOf = async (id: number): Promise<any[]> => ((await (await send("GET", `/api/deals/${id}`)).json()) as any).held; | |
| 196 | ||
| 197 | test("the API holds the agent's words for the next day, and refuses an over-long body", async () => { | |
| 198 | const id = await openOnServer("21 Api Hold Rd"); | |
| 199 | const draft = (await todayOf("21 Api Hold Rd"))[0]; | |
| 200 | const keys = { deadline_id: draft.deadline_id, party_id: draft.party_id, moment: draft.moment }; | |
| 201 | const long = await send("POST", `/api/deals/${id}/reminders/hold`, { ...keys, subject: "Mine", body: "x".repeat(2001) }); | |
| 202 | expect(long.status).toBe(422); | |
| 203 | expect(await heldOf(id)).toEqual([]); | |
| 204 | const res = await send("POST", `/api/deals/${id}/reminders/hold`, { ...keys, subject: "Api subject", body: "Api words" }); | |
| 205 | expect(res.status).toBe(201); | |
| 206 | expect((await todayOf("21 Api Hold Rd", draft.party_id)).length).toBe(0); | |
| 207 | const [held] = await heldOf(id); | |
| 208 | expect(held.until).toBe(tomorrow); | |
| 209 | expect([held.subject, held.body]).toEqual(["Api subject", "Api words"]); | |
| 210 | }); | |
| 211 | ||
| 212 | test("the MCP hold_reminder tool holds the agent's words, and the reminder leaves today's list", async () => { | |
| 213 | const id = await openOnServer("22 Mcp Hold Rd"); | |
| 214 | const draft = (await todayOf("22 Mcp Hold Rd"))[0]; | |
| 215 | const keys = { deadline_id: draft.deadline_id, party_id: draft.party_id, moment: draft.moment }; | |
| 216 | const bad = await mcp("hold_reminder", { ...keys, body: "x".repeat(2001) }); | |
| 217 | expect(bad.isError).toBe(true); | |
| 218 | expect((await todayOf("22 Mcp Hold Rd", draft.party_id)).length).toBe(1); | |
| 219 | const done = await mcp("hold_reminder", { ...keys, subject: "Mcp subject", body: "Mcp words" }); | |
| 220 | expect(done.isError).not.toBe(true); | |
| 221 | expect((await todayOf("22 Mcp Hold Rd", draft.party_id)).length).toBe(0); | |
| 222 | const [held] = await heldOf(id); | |
| 223 | expect([held.subject, held.body]).toEqual(["Mcp subject", "Mcp words"]); | |
| 224 | }); | |
| 225 | ||
| 226 | test("the review page's Tomorrow button sits inside the Send form and posts to the hold route", async () => { | |
| 227 | const page = await (await fetch(`${base}/reminders/today`, { headers: signed })).text(); | |
| 228 | const form = /<form[^>]*action="\/reminders\/today\/send"[\s\S]*?<\/form>/.exec(page)![0]; | |
| 229 | expect(form).toMatch(/<button[^>]*formaction="\/reminders\/today\/hold"[^>]*>Tomorrow<\/button>/); | |
| 230 | expect(form).toContain('name="body"'); | |
| 231 | }); |
src/mcp.ts +3 −3
| @@ -274,14 +274,14 @@ export const tools: readonly Tool[] = [ | ||
| 274 | 274 | { |
| 275 | 275 | name: "hold_reminder", |
| 276 | 276 | description: |
| 277 | "Hold a reminder until tomorrow, for a message the agent is not ready to approve: it leaves reminders_today and comes back the next morning with its words, labelled held. Held again, it waits one more day. A status update is not held.", | |
| 277 | "Hold a reminder until tomorrow, for a message the agent is not ready to approve: it leaves reminders_today and comes back the next morning with its words, labelled held. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any already held. Held again, it waits one more day. A status update is not held.", | |
| 278 | 278 | inputSchema: { |
| 279 | 279 | type: "object", |
| 280 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] } }, | |
| 280 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] }, subject: { type: "string" }, body: { type: "string" } }, | |
| 281 | 281 | required: ["deadline_id", "party_id", "moment"], |
| 282 | 282 | }, |
| 283 | 283 | run: (db, who, args) => |
| 284 | holdReminder(db, who, { deadlineId: Number(args.deadline_id), partyId: Number(args.party_id), moment: args.moment }), | |
| 284 | holdReminder(db, who, { deadlineId: Number(args.deadline_id), partyId: Number(args.party_id), moment: args.moment, subject: args.subject, body: args.body }), | |
| 285 | 285 | }, |
| 286 | 286 | { |
| 287 | 287 | name: "undo_decline_reminder", |
src/pages/review.ts +2 −2
| @@ -14,7 +14,7 @@ import { escape } from "../http.ts"; | ||
| 14 | 14 | import { heldFrom, momentLabel, type Draft } from "../reminders.ts"; |
| 15 | 15 | import type { Who } from "../who.ts"; |
| 16 | 16 | import { layout } from "./layout.ts"; |
| 17 | import { hidden, holdForm } from "./sheet.ts"; | |
| 17 | import { hidden } from "./sheet.ts"; | |
| 18 | 18 | |
| 19 | 19 | export const REVIEW_PATH = "/reminders/today"; |
| 20 | 20 | |
| @@ -97,8 +97,8 @@ function form( | ||
| 97 | 97 | ${problem("body")} |
| 98 | 98 | <label>Message<textarea class="input" name="body" rows="9" maxlength="${BODY_MAX}" required${invalid("body")}>${escape(typed.body)}</textarea></label> |
| 99 | 99 | <button class="button">Send</button> |
| 100 | ${draft.moment === "status" ? "" : `<button class="button button-quiet" formaction="${REVIEW_PATH}/hold">Tomorrow</button>`} | |
| 100 | 101 | </form> |
| 101 | ${holdForm(draft, "", `${REVIEW_PATH}/hold`, keys)} | |
| 102 | 102 | <form class="move" method="post" action="${REVIEW_PATH}/decline">${hidden(keys)} |
| 103 | 103 | ${problem("reason")} |
| 104 | 104 | <label>Why not<input class="input" name="reason" value="${escape(typed.reason)}" maxlength="${REASON_MAX}" required placeholder="e.g. Buyer already confirmed by phone"${invalid("reason")}></label> |
src/reminders.ts +2 −2
| @@ -217,8 +217,8 @@ export function draftsFor(deal: DealView, deadline: DeadlineView, agent: Agent): | ||
| 217 | 217 | move_id: key, |
| 218 | 218 | on: moment === "moved" ? today(new Date(lastMove(deadline)!.at)) : sendOn(deadline.due, moment, deadline.lead_days), |
| 219 | 219 | // A sent reminder keeps the words it went with, whatever the deal says now. |
| 220 | subject: sent !== undefined && sent.subject !== "" ? sent.subject : drafted.subject, | |
| 221 | body: sent !== undefined && sent.subject !== "" ? sent.body : drafted.body, | |
| 220 | subject: sent !== undefined && sent.subject !== "" ? sent.subject : held !== undefined && held.subject !== "" ? held.subject : drafted.subject, | |
| 221 | body: sent !== undefined && sent.subject !== "" ? sent.body : held !== undefined && held.body !== "" ? held.body : drafted.body, | |
| 222 | 222 | sent_at: sent?.sent_at ?? null, |
| 223 | 223 | declined: declined === undefined ? null : { reason: declined.reason, by_name: declined.by_name }, |
| 224 | 224 | held: held === undefined ? null : { until: held.until, from: held.held_on, by_name: held.by_name }, |
src/routes.ts +4 −4
| @@ -1098,7 +1098,7 @@ export const routes: readonly Route[] = [ | ||
| 1098 | 1098 | { |
| 1099 | 1099 | method: "POST", |
| 1100 | 1100 | path: "/reminders/today/hold", |
| 1101 | handle: (c) => reviewed(c, (f) => holdReminder(c.db, c.who, { deadlineId: Number(f.deadline), partyId: Number(f.party), moment: f.moment }), HELD), | |
| 1101 | handle: (c) => reviewed(c, (f) => holdReminder(c.db, c.who, { deadlineId: Number(f.deadline), partyId: Number(f.party), moment: f.moment, subject: f.subject, body: f.body }), HELD), | |
| 1102 | 1102 | }, |
| 1103 | 1103 | { |
| 1104 | 1104 | method: "POST", |
| @@ -1311,14 +1311,14 @@ export const routes: readonly Route[] = [ | ||
| 1311 | 1311 | const body = await readJson(c.request); |
| 1312 | 1312 | if (body === undefined) return json({ error: "too_large" }, 413); |
| 1313 | 1313 | const f = fields(body); |
| 1314 | return answer(holdReminder(c.db, c.who, { dealId: Number(c.params.id), deadlineId: Number(f.deadline_id), partyId: Number(f.party_id), moment: f.moment }), 201); | |
| 1314 | return answer(holdReminder(c.db, c.who, { dealId: Number(c.params.id), deadlineId: Number(f.deadline_id), partyId: Number(f.party_id), moment: f.moment, subject: f.subject, body: f.body }), 201); | |
| 1315 | 1315 | }, |
| 1316 | 1316 | api: { |
| 1317 | 1317 | summary: |
| 1318 | "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Held again, it waits one more day. A status update is not held.", | |
| 1318 | "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any words already held. Held again, it waits one more day. A status update is not held.", | |
| 1319 | 1319 | body: { |
| 1320 | 1320 | type: "object", |
| 1321 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] } }, | |
| 1321 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] }, subject: { type: "string", minLength: 1, maxLength: 150 }, body: { type: "string", minLength: 1, maxLength: 2000 } }, | |
| 1322 | 1322 | required: ["deadline_id", "party_id", "moment"], |
| 1323 | 1323 | }, |
| 1324 | 1324 | answer: { type: "object", properties: { id: { type: "integer" }, until: { type: "string" }, by_name: { type: "string" } }, required: ["id", "until"] }, |
new above
offline
The subject/body pass-through here has no test: removing it keeps the suite green. Add an API test that holds with words and sees them the next day, plus a 422 for an over-long body.