Public: anyone can read this; the crew can change it.

Add a date offers the dates contracts usually add, like HOA documents review, survey or repair request deadline, so the agent picks one instead of typing it #153

Merged opened this

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

Merged

mason/deal-clock:mason/131-add-a-date-offers-the-dates-contracts-us → main 1 commit fork

  • Merged. mason/131-add-a-date-offers-the-dates-contracts-us was merged into main as 470de53.

Review

Sign in to review this change.
5 files changed +84 −7
src/deadlines.ts +18
@@ -57,6 +57,24 @@ export const LEAD_MAX = 30;
5757 export const leadOf = (kind: Kind): number =>
5858 kind === "financing" ? 7 : kind === "earnest" ? 1 : DEFAULT_LEAD;
5959
60 /** A date contracts often add beyond the standard seven: offered as a suggestion, always stored as `custom`. */
61 export interface Suggestion {
62 readonly label: string;
63 /** Days before it is due that its first reminder goes out. */
64 readonly lead: number;
65 }
66 export const SUGGESTED_DATES: readonly Suggestion[] = [
67 { label: "Seller disclosures due", lead: 3 },
68 { label: "HOA documents review ends", lead: 5 },
69 { label: "Survey due", lead: 5 },
70 { label: "Repair request deadline", lead: 2 },
71 { label: "Home warranty ordered", lead: 3 },
72 { label: "Lead paint inspection ends", lead: 3 },
73 ];
74 /** The lead a typed label starts from: its suggestion's when it matches exactly, else the default. */
75 export const leadOfLabel = (label: string): number =>
76 SUGGESTED_DATES.find((s) => s.label === label)?.lead ?? DEFAULT_LEAD;
77
6078 export interface KindInfo {
6179 readonly label: string;
6280 /** Two or three letters for a marker on the clock. */
src/deals.ts +4 −3
@@ -19,6 +19,7 @@ import {
1919 LEAD_MAX,
2020 LEAD_MIN,
2121 leadOf,
22 leadOfLabel,
2223 PARTY_LABEL,
2324 schedule,
2425 type Kind,
@@ -1030,10 +1031,10 @@ export function addDeadline(
10301031 if (what === undefined)
10311032 return within(db, who, "date.add", dealId, fail("invalid", "What is owed is up to 200 characters."));
10321033 const made = db
1033 .query<Deadline, [number, string, string, string, string]>(
1034 "INSERT INTO deadline (deal_id, kind, label, due, owes, what) VALUES (?1, 'custom', ?2, ?3, ?4, ?5) RETURNING *",
1034 .query<Deadline, [number, string, string, string, string, number]>(
1035 "INSERT INTO deadline (deal_id, kind, label, due, owes, what, lead_days) VALUES (?1, 'custom', ?2, ?3, ?4, ?5, ?6) RETURNING *",
10351036 )
1036 .get(deal.value.id, label, input.due, input.owes, what)!;
1037 .get(deal.value.id, label, input.due, input.owes, what, leadOfLabel(label))!;
10371038 return within(db, who, "date.add", dealId, ok(made));
10381039 }
10391040
src/mcp.ts +2 −2
@@ -10,7 +10,7 @@ import type { Database } from "bun:sqlite";
1010 import { restoreSamples, type Result } from "./actions.ts";
1111 import { today } from "./dates.ts";
1212 import { fail, ok } from "./result.ts";
13 import { DEFAULT_LENGTHS, PARTY_ROLES } from "./deadlines.ts";
13 import { DEFAULT_LENGTHS, PARTY_ROLES, SUGGESTED_DATES } from "./deadlines.ts";
1414 import {
1515 addDeadline,
1616 conflictsOf,
@@ -373,7 +373,7 @@ export const tools: readonly Tool[] = [
373373 },
374374 {
375375 name: "add_date",
376 description: "Add a custom date to a deal, saying which party owes it.",
376 description: `Add a custom date to a deal, saying which party owes it. Common labels, which also set a suitable reminder lead when typed exactly: ${SUGGESTED_DATES.map((s) => s.label).join("; ")}. Any other label is fine.`,
377377 inputSchema: {
378378 type: "object",
379379 properties: {
src/pages/deal.ts +2 −2
@@ -8,7 +8,7 @@ import { addDays, relative, short, spoken } from "../dates.ts";
88 import { canChange, canRemove, closedOn, EMAIL_MAX, fellOn, PHONE_MAX, REASON_MAX, type DealView, type Party, type Plan, type PlannedDate, type Sent } from "../deals.ts";
99 import { matchKnown, type Known } from "../people.ts";
1010 import { agent as defaults, standard } from "../env.ts";
11 import { DEFAULT_LENGTHS, PARTY_LABEL, PARTY_ROLES } from "../deadlines.ts";
11 import { DEFAULT_LENGTHS, PARTY_LABEL, PARTY_ROLES, SUGGESTED_DATES } from "../deadlines.ts";
1212 import { escape } from "../http.ts";
1313 import { closingLine } from "../closing.ts";
1414 import { commission, dollars, percent } from "../money.ts";
@@ -298,7 +298,7 @@ export function dealPage(input: {
298298 <h2>Add a date</h2>
299299 <p class="muted small">A date the contract adds: an HOA review, a repair deadline, a rate lock.</p>
300300 <form class="stack" method="post" action="${path}/deadlines">
301 <label>What<input class="input" name="label" required maxlength="100" placeholder="HOA documents review ends"></label>
301 <label>What<input class="input" name="label" required maxlength="100" list="suggested-dates" autocomplete="off" placeholder="HOA documents review ends"><datalist id="suggested-dates">${SUGGESTED_DATES.map((s) => `<option value="${escape(s.label)}"></option>`).join("")}</datalist></label>
302302 <div class="pair">
303303 <label>Date<input class="input" type="date" name="due" required value="${escape(addDays(on, 7))}"></label>
304304 <label>Owed by<select class="input" name="owes">${roleOptions("buyer")}</select></label>
src/suggested.test.ts added +58
@@ -0,0 +1,58 @@
1 /** Suggested extra dates: the datalist on a deal's page, the MCP description, and the lead an exact label gets. */
2 import { afterAll, expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { DEFAULT_LEAD, SUGGESTED_DATES, leadOfLabel } from "./deadlines.ts";
7 import { addDeadline, createDeal } from "./deals.ts";
8 import { openDb } from "./db.ts";
9 import { serve } from "./server.ts";
10 import { sign } from "./who.ts";
11 import type { Who } from "./who.ts";
12
13 const KEY = "a-key-only-the-forge-holds";
14 const server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-suggested-")), identityKey: KEY });
15 const base = `http://${server.hostname}:${server.port}`;
16 afterAll(() => server.stop(true));
17 const identity = `u1|alice|user|${Date.now() + 600_000}`;
18 const headers = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
19
20 test("a deal's page offers the suggested dates in a plain datalist, escaped, and the field keeps its label", async () => {
21 const home = await (await fetch(base + "/", { headers })).text();
22 const path = home.match(/href="(\/deals\/\d+)"/)?.[1];
23 expect(path).toBeDefined();
24 const page = await (await fetch(base + path, { headers })).text();
25 expect(page).toContain('<datalist id="suggested-dates">');
26 const options = [...page.matchAll(/<datalist id="suggested-dates">(.*?)<\/datalist>/gs)][0]![1]!;
27 expect([...options.matchAll(/<option value="([^"]*)"><\/option>/g)].map((m) => m[1])).toEqual(SUGGESTED_DATES.map((s) => s.label));
28 expect(options).not.toMatch(/<(?!\/?option)/);
29 expect(page).toMatch(/<label>What<input class="input" name="label"[^>]*list="suggested-dates"/);
30 });
31
32 test("the MCP add_date description names the suggestions", async () => {
33 const res = await fetch(base + "/mcp", {
34 method: "POST",
35 headers: { ...headers, "content-type": "application/json" },
36 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }),
37 });
38 const text = await res.text();
39 for (const s of SUGGESTED_DATES) expect(text).toContain(s.label);
40 });
41
42 test("an exact suggested label gets its lead; a typed label keeps the default", () => {
43 const alice: Who = { role: "user", id: "u1", name: "alice" };
44 const db = openDb(mkdtempSync(join(tmpdir(), "app-suggested-db-")));
45 const made = createDeal(db, alice, { openAnyway: true, address: "9 Lead Ln", buyer: "Ann", seller: "Sam", contractDate: "2026-10-01", closingDate: "2026-12-20" });
46 if (!made.ok) throw new Error(made.message);
47 const add = (label: string) => {
48 const r = addDeadline(db, alice, { dealId: made.value.id, label, due: "2026-11-20", owes: "buyer" });
49 if (!r.ok) throw new Error(r.message);
50 return r.value;
51 };
52 const repair = add("Repair request deadline");
53 expect(repair.kind).toBe("custom");
54 expect(repair.lead_days).toBe(2);
55 expect(leadOfLabel("Repair request deadline")).not.toBe(DEFAULT_LEAD);
56 expect(add("repair request deadline").lead_days).toBe(DEFAULT_LEAD);
57 expect(add("HOA docs").lead_days).toBe(DEFAULT_LEAD);
58 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs