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

Merged opened this

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

Merged

mason/deal-clock:mason/107-a-reminder-held-until-tomorrow-comes-bac → main 2 commits fork 1 thread · 1 unresolved

  • Merged. mason/107-a-reminder-held-until-tomorrow-comes-bac was merged into main as 33187a6.

Review

Sign in to review this change.
8 files changed +137 −18
contract/openapi.json +12 −2
@@ -1469,7 +1469,7 @@
14691469 },
14701470 "/api/deals/{id}/reminders/hold": {
14711471 "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.",
14731473 "operationId": "postApiDealsIdRemindersHold",
14741474 "parameters": [
14751475 {
@@ -1501,6 +1501,16 @@
15011501 "late",
15021502 "moved"
15031503 ]
1504 },
1505 "subject": {
1506 "type": "string",
1507 "minLength": 1,
1508 "maxLength": 150
1509 },
1510 "body": {
1511 "type": "string",
1512 "minLength": 1,
1513 "maxLength": 2000
15041514 }
15051515 },
15061516 "required": [
@@ -1514,7 +1524,7 @@
15141524 },
15151525 "responses": {
15161526 "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.",
15181528 "content": {
15191529 "application/json": {
15201530 "schema": {
src/db.ts +3
@@ -395,6 +395,9 @@ export const migrations: string[] = [
395395 `ALTER TABLE deal ADD COLUMN fell_at TEXT`,
396396 `ALTER TABLE deal ADD COLUMN fell_reason TEXT`,
397397 `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 ''`,
398401 ];
399402
400403 export function openDb(dir = dataDir): Database {
src/deals.ts +15 −5
@@ -97,6 +97,9 @@ export interface Held {
9797 /** The day it was held. */
9898 readonly held_on: string;
9999 readonly by_name: string;
100 /** The words held with it; '' while none were. */
101 readonly subject: string;
102 readonly body: string;
100103 }
101104 export interface Sent {
102105 readonly id: number;
@@ -1165,7 +1168,7 @@ export function declineReminder(
11651168 export function holdReminder(
11661169 db: Database,
11671170 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 },
11691172 ): Result<Held> {
11701173 const { dealId, found } = draftOf(db, who, input);
11711174 if (!found.ok) return within(db, who, "reminder.hold", dealId, found);
@@ -1179,13 +1182,20 @@ export function holdReminder(
11791182 const day = input.on ?? today();
11801183 if (draft.on > day)
11811184 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.`));
11821191 const until = addDays(draft.held !== null && draft.held.until > day ? draft.held.until : day, 1);
11831192 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 *`,
11871197 )
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 ?? "")!;
11891199 return within(db, who, `reminder.hold:${until}`, dealId, ok(held));
11901200 }
11911201
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
133133 // The deal's sheet offers it too, and the API documents the route.
134134 expect(await (await fetch(`${base}/openapi.json`, { headers: signed })).text()).toContain("/api/deals/{id}/reminders/hold");
135135 });
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[] = [
274274 {
275275 name: "hold_reminder",
276276 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.",
278278 inputSchema: {
279279 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" } },
281281 required: ["deadline_id", "party_id", "moment"],
282282 },
283283 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 }),
285285 },
286286 {
287287 name: "undo_decline_reminder",
src/pages/review.ts +2 −2
@@ -14,7 +14,7 @@ import { escape } from "../http.ts";
1414 import { heldFrom, momentLabel, type Draft } from "../reminders.ts";
1515 import type { Who } from "../who.ts";
1616 import { layout } from "./layout.ts";
17 import { hidden, holdForm } from "./sheet.ts";
17 import { hidden } from "./sheet.ts";
1818
1919 export const REVIEW_PATH = "/reminders/today";
2020
@@ -97,8 +97,8 @@ function form(
9797 ${problem("body")}
9898 <label>Message<textarea class="input" name="body" rows="9" maxlength="${BODY_MAX}" required${invalid("body")}>${escape(typed.body)}</textarea></label>
9999 <button class="button">Send</button>
100 ${draft.moment === "status" ? "" : `<button class="button button-quiet" formaction="${REVIEW_PATH}/hold">Tomorrow</button>`}
100101 </form>
101 ${holdForm(draft, "", `${REVIEW_PATH}/hold`, keys)}
102102 <form class="move" method="post" action="${REVIEW_PATH}/decline">${hidden(keys)}
103103 ${problem("reason")}
104104 <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):
217217 move_id: key,
218218 on: moment === "moved" ? today(new Date(lastMove(deadline)!.at)) : sendOn(deadline.due, moment, deadline.lead_days),
219219 // 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,
222222 sent_at: sent?.sent_at ?? null,
223223 declined: declined === undefined ? null : { reason: declined.reason, by_name: declined.by_name },
224224 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[] = [
10981098 {
10991099 method: "POST",
11001100 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),
11021102 },
11031103 {
11041104 method: "POST",
@@ -1311,14 +1311,14 @@ export const routes: readonly Route[] = [
13111311 const body = await readJson(c.request);
13121312 if (body === undefined) return json({ error: "too_large" }, 413);
13131313 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);

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.

13151315 },
13161316 api: {
13171317 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.",
13191319 body: {
13201320 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 } },
13221322 required: ["deadline_id", "party_id", "moment"],
13231323 },
13241324 answer: { type: "object", properties: { id: { type: "integer" }, until: { type: "string" }, by_name: { type: "string" } }, required: ["id", "until"] },

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs