teamsmith 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/package.json +1 -1
- package/skills/teamsmith/CHANGELOG.md +30 -0
- package/skills/teamsmith/SKILL.md +83 -163
- package/skills/teamsmith/extension/team-notify.ts +1 -1
- package/skills/teamsmith/extension/team-reply.ts +105 -0
- package/skills/teamsmith/references/agent-adapters.md +1 -1
- package/skills/teamsmith/references/config.md +20 -11
- package/skills/teamsmith/references/migration.md +47 -3
- package/skills/teamsmith/references/protocol.md +64 -31
- package/skills/teamsmith/references/reminders.md +131 -0
- package/skills/teamsmith/references/troubleshooting.md +19 -5
- package/skills/teamsmith/references/workflows.md +18 -16
- package/skills/teamsmith/scripts/lib/cmd-agents.sh +77 -49
- package/skills/teamsmith/scripts/lib/cmd-project.sh +193 -21
- package/skills/teamsmith/scripts/lib/cmd-remind.sh +103 -0
- package/skills/teamsmith/scripts/lib/cmd-update.sh +7 -4
- package/skills/teamsmith/scripts/lib/common.sh +44 -4
- package/skills/teamsmith/scripts/lib/outbox.sh +72 -18
- package/skills/teamsmith/scripts/remind-store.mjs +529 -0
- package/skills/teamsmith/scripts/replies.mjs +9 -3
- package/skills/teamsmith/scripts/reply-capture.mjs +596 -0
- package/skills/teamsmith/scripts/state-authority.mjs +500 -0
- package/skills/teamsmith/scripts/strings/commands.tsv +2 -1
- package/skills/teamsmith/scripts/strings/help.en.tsv +16 -2
- package/skills/teamsmith/scripts/strings/help.zh.tsv +16 -2
- package/skills/teamsmith/scripts/team +1 -0
- package/skills/teamsmith/templates/AGENTS.section.md.tmpl +68 -123
- package/skills/teamsmith/templates/PROTOCOL.md.tmpl +48 -55
- package/skills/teamsmith/templates/pm-prompt.md.tmpl +126 -119
- package/skills/teamsmith/tests/checkout-shape-probe.sh +6 -5
- package/skills/teamsmith/tests/delivery-truth.sh +82 -47
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +71 -23
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/__pycache__/judge-second.cpython-313.pyc +0 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-dirty-ab.py +143 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +26 -9
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +92 -20
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +47 -3
- package/skills/teamsmith/tests/fixtures/reply-capture-provider.ts +76 -0
- package/skills/teamsmith/tests/flip-p410-order2.sh +205 -0
- package/skills/teamsmith/tests/frames/README.md +3 -1
- package/skills/teamsmith/tests/gate-guard.sh +20 -4
- package/skills/teamsmith/tests/help-surface.sh +60 -2
- package/skills/teamsmith/tests/install-shape.sh +312 -0
- package/skills/teamsmith/tests/key-replies-pty.sh +197 -21
- package/skills/teamsmith/tests/key-replies.sh +187 -68
- package/skills/teamsmith/tests/ledger-claim.sh +8 -6
- package/skills/teamsmith/tests/lib/box-judge.sh +2 -1
- package/skills/teamsmith/tests/lib/main-team-fingerprint.sh +104 -0
- package/skills/teamsmith/tests/lib/pm-window.sh +127 -0
- package/skills/teamsmith/tests/lib/reply-fixture.sh +7 -19
- package/skills/teamsmith/tests/lib/reply-guard.sh +188 -0
- package/skills/teamsmith/tests/lib/section-calls.pl +171 -0
- package/skills/teamsmith/tests/loop-inventory.tsv +7 -5
- package/skills/teamsmith/tests/meta-suite.sh +215 -0
- package/skills/teamsmith/tests/panel-b3-pty-mouse.py +154 -17
- package/skills/teamsmith/tests/panel-b3.sh +33 -4
- package/skills/teamsmith/tests/remind-cli.sh +574 -0
- package/skills/teamsmith/tests/reply-capture-real.mjs +360 -0
- package/skills/teamsmith/tests/reply-capture-real.sh +5 -0
- package/skills/teamsmith/tests/reply-capture.mjs +622 -0
- package/skills/teamsmith/tests/section-paths.tsv +40 -9
- package/skills/teamsmith/tests/section-select.sh +180 -26
- package/skills/teamsmith/tests/smoke.sh +1235 -238
- package/skills/teamsmith/tests/state-authority-node.mjs +42 -0
- package/skills/teamsmith/tests/state-authority.sh +629 -0
- package/skills/teamsmith/tests/workflow-entry.sh +123 -3
- package/skills/teamsmith-init/SKILL.md +1 -1
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
// remind-store.mjs — the reminder domain of the project-state authority (P409 · change:
|
|
2
|
+
// agent-self-reminder 2.1/2.3; design.md "One snapshot is the authority" and "API, limits and lifecycle").
|
|
3
|
+
//
|
|
4
|
+
// This module never touches the filesystem: scripts/state-authority.mjs reads, validates, mutates under
|
|
5
|
+
// its flock transaction and writes the versioned snapshot, and calls applyReminder() inside that
|
|
6
|
+
// transaction. So there is exactly one state file, one lock and one durable order; the CLI and (later)
|
|
7
|
+
// the PM tool differ only in their presentation.
|
|
8
|
+
//
|
|
9
|
+
// What the snapshot stores is the delivery truth: phase, due instant, request, full text, source and the
|
|
10
|
+
// transitions. What a person sees is *derived* from that truth on every read -- `scheduled` gains the
|
|
11
|
+
// clock-derived `derived-due` view, and no counter is stored a second time, so a hand-corrected snapshot
|
|
12
|
+
// or a later Pulse admission is visible without a migration.
|
|
13
|
+
//
|
|
14
|
+
// Scope of this slice: the durable create/list/show/cancel lifecycle and the derived views. Batching,
|
|
15
|
+
// intent, delivery, observation, Standby and Pulse admission belong to 3.x/4.x; their phases are
|
|
16
|
+
// validated and derived here already so a snapshot written by those slices reads correctly, but nothing
|
|
17
|
+
// in this file produces them.
|
|
18
|
+
|
|
19
|
+
const PHASES = new Set(['scheduled', 'queued', 'intent', 'observed', 'unknown', 'cancelled', 'resolved']);
|
|
20
|
+
// Terminal = the outcome is known and the reminder can no longer fire. A key survives `unknown` (a
|
|
21
|
+
// delivery attempt is outstanding, so the logical reminder is still "in play") and is released by any
|
|
22
|
+
// terminal phase (design: "A key remains reserved for scheduled, queued, intent and unknown records").
|
|
23
|
+
const TERMINAL = new Set(['observed', 'cancelled', 'resolved']);
|
|
24
|
+
const KEY_HOLDING = new Set(['scheduled', 'queued', 'intent', 'unknown']);
|
|
25
|
+
const VIEWS = ['scheduled', 'derived-due', 'queued', 'intent', 'observed', 'unknown', 'cancelled', 'resolved'];
|
|
26
|
+
|
|
27
|
+
// Bounded terminal history (task 2.3). Active and unknown records are never evicted; the issued-ID
|
|
28
|
+
// high-water lives in the snapshot's sequence and only ever moves forward.
|
|
29
|
+
const TERMINAL_KEEP = 1000;
|
|
30
|
+
const AUDIT_KEEP = 1000;
|
|
31
|
+
const RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
32
|
+
|
|
33
|
+
// An unquoted number in `--after` and a date-time without an offset are the two shapes a person most
|
|
34
|
+
// often means and the design explicitly rejects (an offset-free local time is ambiguous across machines).
|
|
35
|
+
const INSTANT_RE = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?([Zz]|[+-]\d{2}:\d{2})$/;
|
|
36
|
+
const DELAY_RE = /^(\d+)([smh])$/;
|
|
37
|
+
const KEY_RE = /^[\x20-\x7e]+$/; // printable ASCII: a key is a durable identifier, not prose
|
|
38
|
+
const CONTROL_RE = /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/;
|
|
39
|
+
|
|
40
|
+
const fail = message => { throw new Error(message); };
|
|
41
|
+
const isTerminal = phase => TERMINAL.has(phase);
|
|
42
|
+
const iso = ms => new Date(ms).toISOString();
|
|
43
|
+
const msOf = text => Date.parse(text);
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------- time parsing
|
|
46
|
+
function daysInMonth(year, month) { return new Date(Date.UTC(year, month, 0)).getUTCDate(); }
|
|
47
|
+
|
|
48
|
+
function parseInstant(text) {
|
|
49
|
+
const raw = String(text ?? '').trim();
|
|
50
|
+
if (!raw) return { error: 'time is empty: give --at <RFC 3339 date-time with offset> or --after <30s|5m|2h>' };
|
|
51
|
+
const m = INSTANT_RE.exec(raw);
|
|
52
|
+
if (!m) {
|
|
53
|
+
if (/^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}(:\d{2})?(\.\d+)?$/.test(raw)) {
|
|
54
|
+
return { error: `--at '${raw}' has no time zone: an absolute reminder must name its offset (…+08:00 or …Z)` };
|
|
55
|
+
}
|
|
56
|
+
return { error: `--at '${raw}' is not an RFC 3339 date-time (expected YYYY-MM-DDTHH:MM:SS with Z or ±HH:MM)` };
|
|
57
|
+
}
|
|
58
|
+
const year = Number(m[1]); const month = Number(m[2]); const day = Number(m[3]);
|
|
59
|
+
const hour = Number(m[4]); const minute = Number(m[5]); const second = Number(m[6]);
|
|
60
|
+
const frac = m[7] ? Number((m[7] + '000').slice(0, 3)) : 0;
|
|
61
|
+
const zone = m[8];
|
|
62
|
+
if (month < 1 || month > 12) return { error: `--at '${raw}' has no month ${month}` };
|
|
63
|
+
if (day < 1 || day > daysInMonth(year, month)) return { error: `--at '${raw}' is not a real calendar date` };
|
|
64
|
+
if (hour > 23) return { error: `--at '${raw}' has no hour ${hour}` };
|
|
65
|
+
if (minute > 59) return { error: `--at '${raw}' has no minute ${minute}` };
|
|
66
|
+
if (second > 59) return { error: `--at '${raw}' has no second ${second} (leap seconds are not representable)` };
|
|
67
|
+
let offsetMinutes = 0;
|
|
68
|
+
if (zone !== 'Z' && zone !== 'z') {
|
|
69
|
+
const sign = zone[0] === '-' ? -1 : 1;
|
|
70
|
+
const oh = Number(zone.slice(1, 3)); const om = Number(zone.slice(4, 6));
|
|
71
|
+
if (oh > 23 || om > 59) return { error: `--at '${raw}' has an invalid offset ${zone}` };
|
|
72
|
+
offsetMinutes = sign * (oh * 60 + om);
|
|
73
|
+
}
|
|
74
|
+
const utc = Date.UTC(year, month - 1, day, hour, minute, second, frac) - offsetMinutes * 60000;
|
|
75
|
+
if (!Number.isFinite(utc)) return { error: `--at '${raw}' is outside the representable date range` };
|
|
76
|
+
// Date.UTC normalizes overflow (Feb 30 -> Mar 2); a round trip through the same offset must land on
|
|
77
|
+
// the components the caller wrote, or the date was not real.
|
|
78
|
+
const back = new Date(utc + offsetMinutes * 60000);
|
|
79
|
+
if (back.getUTCFullYear() !== year || back.getUTCMonth() !== month - 1 || back.getUTCDate() !== day ||
|
|
80
|
+
back.getUTCHours() !== hour || back.getUTCMinutes() !== minute || back.getUTCSeconds() !== second) {
|
|
81
|
+
return { error: `--at '${raw}' is not a real instant` };
|
|
82
|
+
}
|
|
83
|
+
return { ms: utc, value: raw };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function parseDelay(text) {
|
|
87
|
+
const raw = String(text ?? '').trim();
|
|
88
|
+
if (!raw) return { error: '--after is empty: give a positive number of seconds, minutes or hours (30s, 5m, 2h)' };
|
|
89
|
+
const m = DELAY_RE.exec(raw);
|
|
90
|
+
if (!m) {
|
|
91
|
+
if (/^-/.test(raw)) return { error: `--after '${raw}' is negative: a reminder is in the future` };
|
|
92
|
+
if (/^0+[smh]$/.test(raw)) return { error: `--after '${raw}' is zero: a reminder is in the future` };
|
|
93
|
+
return { error: `--after '${raw}' is not a whole number followed by s, m or h (30s, 5m, 2h)` };
|
|
94
|
+
}
|
|
95
|
+
const value = Number(m[1]);
|
|
96
|
+
if (!Number.isSafeInteger(value)) return { error: `--after '${raw}' is too large to represent` };
|
|
97
|
+
if (value < 1) return { error: `--after '${raw}' is zero: a reminder is in the future` };
|
|
98
|
+
const unit = { s: 1000, m: 60000, h: 3600000 }[m[2]];
|
|
99
|
+
const delta = value * unit;
|
|
100
|
+
if (!Number.isSafeInteger(delta) || delta > 8.64e15) return { error: `--after '${raw}' is too large to represent` };
|
|
101
|
+
return { ms: delta, value: raw };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------- input validation
|
|
105
|
+
function normKey(key) {
|
|
106
|
+
if (key === undefined || key === null || key === '') return { key: null };
|
|
107
|
+
const value = String(key).trim();
|
|
108
|
+
if (!value) return { error: 'key is empty (omit the flag instead)' };
|
|
109
|
+
if (Buffer.byteLength(value, 'utf8') > 128) return { error: 'key is longer than 128 bytes' };
|
|
110
|
+
if (!KEY_RE.test(value)) return { error: `key '${value}' has characters outside printable ASCII (a key is a durable identifier)` };
|
|
111
|
+
return { key: value };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function normTask(task) {
|
|
115
|
+
if (task === undefined || task === null || task === '') return { task: null };
|
|
116
|
+
const value = String(task);
|
|
117
|
+
if (CONTROL_RE.test(value)) return { error: 'task reference contains control characters' };
|
|
118
|
+
if (Buffer.byteLength(value, 'utf8') > 200) return { error: 'task reference is longer than 200 bytes' };
|
|
119
|
+
return { task: value };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A message is stored, shown and delivered byte for byte; nothing here trims, folds or shortens it.
|
|
123
|
+
function normMessage(message) {
|
|
124
|
+
if (typeof message !== 'string') return { error: 'message is missing' };
|
|
125
|
+
if (message === '') return { error: 'message is empty: a reminder needs the text the future agent should read' };
|
|
126
|
+
if (message.indexOf('\u0000') !== -1) return { error: 'message contains a NUL byte' };
|
|
127
|
+
if (message.trim() === '') return { error: 'message is blank: a reminder needs readable text' };
|
|
128
|
+
return { message };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const SOURCES = new Set(['cli', 'pm-tool']);
|
|
132
|
+
function normSource(source) {
|
|
133
|
+
const value = source === undefined || source === null || source === '' ? 'cli' : String(source);
|
|
134
|
+
if (!SOURCES.has(value)) return { error: `unknown source '${value}' (cli|pm-tool)` };
|
|
135
|
+
return { source: value };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ---------------------------------------------------------------- views (derived, never stored)
|
|
139
|
+
function viewOf(record, nowMs) {
|
|
140
|
+
if (record.phase === 'scheduled') return msOf(record.dueAt) <= nowMs ? 'derived-due' : 'scheduled';
|
|
141
|
+
return record.phase;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function viewOfRecord(record, nowMs) {
|
|
145
|
+
const dueMs = msOf(record.dueAt);
|
|
146
|
+
return {
|
|
147
|
+
id: record.id,
|
|
148
|
+
seq: record.seq,
|
|
149
|
+
phase: record.phase,
|
|
150
|
+
view: viewOf(record, nowMs),
|
|
151
|
+
dueAt: record.dueAt,
|
|
152
|
+
dueInMs: dueMs - nowMs,
|
|
153
|
+
createdAt: record.createdAt,
|
|
154
|
+
key: record.key,
|
|
155
|
+
source: record.source,
|
|
156
|
+
owner: record.owner,
|
|
157
|
+
task: record.task,
|
|
158
|
+
message: record.message,
|
|
159
|
+
endedAt: record.endedAt ?? null,
|
|
160
|
+
batchId: record.batchId ?? null,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function countsOf(records, nowMs) {
|
|
165
|
+
const counts = Object.fromEntries(VIEWS.map(view => [view, 0]));
|
|
166
|
+
for (const record of records) counts[viewOf(record, nowMs)] = (counts[viewOf(record, nowMs)] ?? 0) + 1;
|
|
167
|
+
return counts;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---------------------------------------------------------------- request identity (key idempotency)
|
|
171
|
+
function sameRequest(record, request) {
|
|
172
|
+
return record.request.form === request.form && record.request.value === request.value &&
|
|
173
|
+
record.message === request.message && (record.task ?? null) === (request.task ?? null);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ---------------------------------------------------------------- snapshot validation
|
|
177
|
+
// Runs under the lock before any mutation. A snapshot that contradicts itself (an id at or above the
|
|
178
|
+
// high-water, a high-water that forgot its own pruned records) is refused instead of used: a lower
|
|
179
|
+
// high-water would silently turn a pruned identity into a never-issued one and make an old ID look
|
|
180
|
+
// reusable.
|
|
181
|
+
export function validateReminders(snapshot, failWith) {
|
|
182
|
+
const bad = message => failWith(message);
|
|
183
|
+
const highWater = snapshot.sequences.reminder;
|
|
184
|
+
if (snapshot.pruned > highWater - 1) {
|
|
185
|
+
bad(`state snapshot pruned count ${snapshot.pruned} exceeds the issued high-water ${highWater - 1}; the high-water went backwards`);
|
|
186
|
+
}
|
|
187
|
+
const seen = new Set();
|
|
188
|
+
for (const record of snapshot.reminders) {
|
|
189
|
+
if (typeof record.id !== 'string') bad('a Reminder record has no id');
|
|
190
|
+
const parsed = parseId(record.id, snapshot.project.id);
|
|
191
|
+
if (!parsed) bad(`invalid Reminder id: ${record.id}`);
|
|
192
|
+
if (parsed.seq >= highWater) bad(`Reminder ${record.id} claims sequence ${parsed.seq} at or above the issued high-water ${highWater - 1}`);
|
|
193
|
+
if (seen.has(record.id)) bad(`duplicate Reminder id: ${record.id}`);
|
|
194
|
+
seen.add(record.id);
|
|
195
|
+
if (record.seq !== parsed.seq) bad(`Reminder ${record.id} carries seq ${record.seq}, its id says ${parsed.seq}`);
|
|
196
|
+
if (record.owner !== 'pm') bad(`Reminder ${record.id} has an unknown owner: ${record.owner}`);
|
|
197
|
+
if (!SOURCES.has(record.source)) bad(`Reminder ${record.id} has an unknown source: ${record.source}`);
|
|
198
|
+
if (!PHASES.has(record.phase)) bad(`Reminder ${record.id} has an unknown phase: ${record.phase}`);
|
|
199
|
+
if (typeof record.message !== 'string' || record.message === '' || record.message.indexOf('\u0000') !== -1) {
|
|
200
|
+
bad(`Reminder ${record.id} has no usable message`);
|
|
201
|
+
}
|
|
202
|
+
if (typeof record.dueAt !== 'string' || !Number.isFinite(msOf(record.dueAt))) bad(`Reminder ${record.id} has an unreadable due time`);
|
|
203
|
+
if (typeof record.createdAt !== 'string' || !Number.isFinite(msOf(record.createdAt))) bad(`Reminder ${record.id} has an unreadable creation time`);
|
|
204
|
+
if (!record.request || typeof record.request !== 'object' || !['at', 'after'].includes(record.request.form) || typeof record.request.value !== 'string') {
|
|
205
|
+
bad(`Reminder ${record.id} has no readable time request`);
|
|
206
|
+
}
|
|
207
|
+
if (record.key !== null && record.key !== undefined && typeof record.key !== 'string') bad(`Reminder ${record.id} has an unreadable key`);
|
|
208
|
+
if (record.task !== null && record.task !== undefined && typeof record.task !== 'string') bad(`Reminder ${record.id} has an unreadable task reference`);
|
|
209
|
+
if (isTerminal(record.phase) && (typeof record.endedAt !== 'string' || !Number.isFinite(msOf(record.endedAt)))) {
|
|
210
|
+
bad(`terminal Reminder ${record.id} has no ended time`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (snapshot.audit.some(entry => !entry || typeof entry !== 'object' || typeof entry.op !== 'string' ||
|
|
214
|
+
typeof entry.at !== 'string' || !Number.isFinite(msOf(entry.at)))) {
|
|
215
|
+
bad('the state audit has an unreadable entry');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function parseId(id, projectId) {
|
|
220
|
+
const prefix = `rem-${projectId}-`;
|
|
221
|
+
if (typeof id !== 'string' || !id.startsWith(prefix)) return null;
|
|
222
|
+
const tail = id.slice(prefix.length);
|
|
223
|
+
if (!/^[1-9][0-9]*$/.test(tail)) return null;
|
|
224
|
+
const seq = Number(tail);
|
|
225
|
+
return Number.isSafeInteger(seq) ? { seq } : null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ---------------------------------------------------------------- retention
|
|
229
|
+
// Same-transaction compaction: keep the newest TERMINAL_KEEP terminal outcomes that are also inside the
|
|
230
|
+
// retention window, and never touch a record that is not terminal. The count pruned is cumulative and
|
|
231
|
+
// the sequence high-water is untouched, so an issued ID can never be handed out a second time.
|
|
232
|
+
function endedMs(record) { return msOf(record.endedAt ?? record.createdAt); }
|
|
233
|
+
|
|
234
|
+
function pruneHistory(snapshot, nowMs) {
|
|
235
|
+
const terminal = snapshot.reminders.filter(record => isTerminal(record.phase));
|
|
236
|
+
const ranked = terminal.slice().sort((a, b) => (endedMs(b) - endedMs(a)) || (b.seq - a.seq));
|
|
237
|
+
const keep = new Set();
|
|
238
|
+
for (let i = 0; i < ranked.length; i++) {
|
|
239
|
+
if (i < TERMINAL_KEEP && nowMs - endedMs(ranked[i]) <= RETENTION_MS) keep.add(ranked[i].id);
|
|
240
|
+
}
|
|
241
|
+
const dropped = terminal.filter(record => !keep.has(record.id));
|
|
242
|
+
if (dropped.length) {
|
|
243
|
+
const ids = new Set(dropped.map(record => record.id));
|
|
244
|
+
snapshot.reminders = snapshot.reminders.filter(record => !ids.has(record.id));
|
|
245
|
+
snapshot.pruned += dropped.length;
|
|
246
|
+
snapshot.audit.push({
|
|
247
|
+
at: iso(nowMs), op: 'retention-prune', count: dropped.length,
|
|
248
|
+
ids: dropped.slice().sort((a, b) => b.seq - a.seq).slice(0, 20).map(record => record.id),
|
|
249
|
+
detail: `kept the newest ${TERMINAL_KEEP} terminal outcomes inside 30 days; the sequence high-water never moves back`,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const audit = snapshot.audit.filter(entry => nowMs - msOf(entry.at) <= RETENTION_MS);
|
|
253
|
+
snapshot.audit = audit.length > AUDIT_KEEP ? audit.slice(audit.length - AUDIT_KEEP) : audit;
|
|
254
|
+
return dropped.length;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function note(snapshot, nowMs, op, fields) {
|
|
258
|
+
snapshot.audit.push({ at: iso(nowMs), op, ...fields });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ---------------------------------------------------------------- operations (inside the lock)
|
|
262
|
+
function newRecord(snapshot, fields, nowMs) {
|
|
263
|
+
const seq = snapshot.sequences.reminder;
|
|
264
|
+
snapshot.sequences.reminder = seq + 1;
|
|
265
|
+
const record = {
|
|
266
|
+
id: `rem-${snapshot.project.id}-${seq}`,
|
|
267
|
+
seq,
|
|
268
|
+
owner: 'pm',
|
|
269
|
+
source: fields.source,
|
|
270
|
+
task: fields.task,
|
|
271
|
+
key: fields.key,
|
|
272
|
+
phase: 'scheduled',
|
|
273
|
+
createdAt: iso(nowMs),
|
|
274
|
+
request: { form: fields.form, value: fields.value },
|
|
275
|
+
dueAt: iso(fields.dueMs),
|
|
276
|
+
message: fields.message,
|
|
277
|
+
endedAt: null,
|
|
278
|
+
};
|
|
279
|
+
snapshot.reminders.push(record);
|
|
280
|
+
return record;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function addReminder(snapshot, request, nowMs) {
|
|
284
|
+
const source = normSource(request.source);
|
|
285
|
+
if (source.error) return { ok: false, refusal: 'bad-source', reason: source.error };
|
|
286
|
+
const key = normKey(request.key);
|
|
287
|
+
if (key.error) return { ok: false, refusal: 'bad-key', reason: key.error };
|
|
288
|
+
const task = normTask(request.task);
|
|
289
|
+
if (task.error) return { ok: false, refusal: 'bad-task', reason: task.error };
|
|
290
|
+
const message = normMessage(request.message);
|
|
291
|
+
if (message.error) return { ok: false, refusal: 'bad-message', reason: message.error };
|
|
292
|
+
|
|
293
|
+
const hasAt = request.at !== undefined && request.at !== null && String(request.at) !== '';
|
|
294
|
+
const hasAfter = request.after !== undefined && request.after !== null && String(request.after) !== '';
|
|
295
|
+
if (hasAt && hasAfter) return { ok: false, refusal: 'both-times', reason: 'give exactly one of --at or --after, not both' };
|
|
296
|
+
if (!hasAt && !hasAfter) return { ok: false, refusal: 'no-time', reason: 'give one of --at <instant> or --after <duration>' };
|
|
297
|
+
const form = hasAt ? 'at' : 'after';
|
|
298
|
+
const parsed = hasAt ? parseInstant(request.at) : parseDelay(request.after);
|
|
299
|
+
if (parsed.error) return { ok: false, refusal: form === 'at' ? 'bad-at' : 'bad-after', reason: parsed.error };
|
|
300
|
+
|
|
301
|
+
// An active key is answered before the deadline is recomputed and before the past-time rule: a retry
|
|
302
|
+
// of the same request after its due is the *same* reminder, not a new one and not a bad time.
|
|
303
|
+
if (key.key) {
|
|
304
|
+
const active = snapshot.reminders.find(record => record.key === key.key && KEY_HOLDING.has(record.phase));
|
|
305
|
+
if (active) {
|
|
306
|
+
const candidate = { form, value: parsed.value, message: message.message, task: task.task };
|
|
307
|
+
if (!sameRequest(active, candidate)) {
|
|
308
|
+
return {
|
|
309
|
+
ok: false, refusal: 'key-conflict',
|
|
310
|
+
reason: `key '${key.key}' is already held by ${active.id} (${active.phase}, due ${active.dueAt}) with a different request; ` +
|
|
311
|
+
'cancel or wait for it, or use another key',
|
|
312
|
+
existingId: active.id,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
return { ok: true, created: false, duplicate: true, now: iso(nowMs), reminder: viewOfRecord(active, nowMs) };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const dueMs = form === 'at' ? parsed.ms : nowMs + parsed.ms;
|
|
320
|
+
if (!Number.isFinite(dueMs) || Math.abs(dueMs) > 8.64e15) {
|
|
321
|
+
return { ok: false, refusal: 'bad-time-range', reason: 'the computed due instant is outside the representable date range' };
|
|
322
|
+
}
|
|
323
|
+
if (dueMs <= nowMs) {
|
|
324
|
+
return {
|
|
325
|
+
ok: false, refusal: 'past-time',
|
|
326
|
+
reason: `${form === 'at' ? `--at '${parsed.value}'` : 'the computed due instant'} is not in the future (now ${iso(nowMs)}); a reminder cannot fire in the past`,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const record = newRecord(snapshot, {
|
|
331
|
+
source: source.source, task: task.task, key: key.key, message: message.message,
|
|
332
|
+
form, value: parsed.value, dueMs,
|
|
333
|
+
}, nowMs);
|
|
334
|
+
note(snapshot, nowMs, 'remind-add', { ids: [record.id], source: source.source, detail: record.key ? `key=${record.key}` : null });
|
|
335
|
+
pruneHistory(snapshot, nowMs);
|
|
336
|
+
return { ok: true, created: true, now: iso(nowMs), reminder: viewOfRecord(record, nowMs) };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function findReminder(snapshot, id, projectId) {
|
|
340
|
+
const record = snapshot.reminders.find(item => item.id === id) ?? null;
|
|
341
|
+
if (record) return { record };
|
|
342
|
+
const parsed = parseId(id, projectId);
|
|
343
|
+
if (!parsed) return { refusal: 'unknown-id', reason: `'${id}' is not a reminder id of this project (rem-<project id>-<n>)` };
|
|
344
|
+
if (parsed.seq >= snapshot.sequences.reminder) {
|
|
345
|
+
return { refusal: 'unknown-id', reason: `'${id}' was never issued by this project (issued high-water ${snapshot.sequences.reminder - 1})` };
|
|
346
|
+
}
|
|
347
|
+
return {
|
|
348
|
+
refusal: 'history-pruned',
|
|
349
|
+
reason: `'${id}' was issued and has left the retained history (issued high-water ${snapshot.sequences.reminder - 1}, ` +
|
|
350
|
+
`${snapshot.pruned} terminal outcomes pruned); it cannot be revived or re-sent`,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function showReminder(snapshot, request, nowMs) {
|
|
355
|
+
const found = findReminder(snapshot, String(request.id ?? ''), snapshot.project.id);
|
|
356
|
+
if (!found.record) {
|
|
357
|
+
return { ok: false, refusal: found.refusal, reason: found.reason, highWater: snapshot.sequences.reminder - 1, pruned: snapshot.pruned };
|
|
358
|
+
}
|
|
359
|
+
return { ok: true, found: true, now: iso(nowMs), highWater: snapshot.sequences.reminder - 1, pruned: snapshot.pruned, reminder: viewOfRecord(found.record, nowMs) };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function cancelReminder(snapshot, request, nowMs) {
|
|
363
|
+
const found = findReminder(snapshot, String(request.id ?? ''), snapshot.project.id);
|
|
364
|
+
if (!found.record) {
|
|
365
|
+
return { ok: false, refusal: found.refusal, reason: found.reason, highWater: snapshot.sequences.reminder - 1 };
|
|
366
|
+
}
|
|
367
|
+
const record = found.record;
|
|
368
|
+
if (record.phase === 'cancelled') {
|
|
369
|
+
return { ok: true, outcome: 'already-cancelled', now: iso(nowMs), reminder: viewOfRecord(record, nowMs), reason: 'it was already cancelled; nothing changed' };
|
|
370
|
+
}
|
|
371
|
+
if (isTerminal(record.phase)) {
|
|
372
|
+
return {
|
|
373
|
+
ok: false, refusal: `already-${record.phase}`,
|
|
374
|
+
reason: `${record.id} is already ${record.phase} (${record.endedAt}); a terminal reminder cannot be cancelled`,
|
|
375
|
+
reminder: viewOfRecord(record, nowMs),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
if (record.phase === 'intent' || record.phase === 'unknown') {
|
|
379
|
+
return {
|
|
380
|
+
ok: false, refusal: 'in-flight',
|
|
381
|
+
reason: `${record.id} is ${record.phase}: the batch was already frozen for delivery, so withdrawal cannot be guaranteed; ` +
|
|
382
|
+
'nothing was changed (inspect the batch and its delivery evidence first)',
|
|
383
|
+
reminder: viewOfRecord(record, nowMs),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
const from = record.phase;
|
|
387
|
+
record.phase = 'cancelled';
|
|
388
|
+
record.cancelledFrom = from;
|
|
389
|
+
record.endedAt = iso(nowMs);
|
|
390
|
+
note(snapshot, nowMs, 'remind-cancel', { ids: [record.id], source: record.source, detail: `from ${from}` });
|
|
391
|
+
pruneHistory(snapshot, nowMs);
|
|
392
|
+
return { ok: true, outcome: 'cancelled', from, now: iso(nowMs), reminder: viewOfRecord(record, nowMs) };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function listReminders(snapshot, request, nowMs) {
|
|
396
|
+
const all = request.all === true;
|
|
397
|
+
const active = snapshot.reminders.filter(record => !isTerminal(record.phase))
|
|
398
|
+
.sort((a, b) => (msOf(a.dueAt) - msOf(b.dueAt)) || (a.seq - b.seq))
|
|
399
|
+
.map(record => viewOfRecord(record, nowMs));
|
|
400
|
+
const terminal = snapshot.reminders.filter(record => isTerminal(record.phase))
|
|
401
|
+
.sort((a, b) => (endedMs(b) - endedMs(a)) || (b.seq - a.seq))
|
|
402
|
+
.map(record => viewOfRecord(record, nowMs));
|
|
403
|
+
return {
|
|
404
|
+
ok: true,
|
|
405
|
+
now: iso(nowMs),
|
|
406
|
+
projectId: snapshot.project.id,
|
|
407
|
+
stateRoot: snapshot.project.stateRoot,
|
|
408
|
+
highWater: snapshot.sequences.reminder - 1,
|
|
409
|
+
pruned: snapshot.pruned,
|
|
410
|
+
counts: countsOf(snapshot.reminders, nowMs),
|
|
411
|
+
active,
|
|
412
|
+
terminal: all ? terminal : [],
|
|
413
|
+
terminalTotal: terminal.length,
|
|
414
|
+
terminalShown: all,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function applyReminder(snapshot, request, nowMs) {
|
|
419
|
+
switch (request.op) {
|
|
420
|
+
case 'remind-add': return addReminder(snapshot, request, nowMs);
|
|
421
|
+
case 'remind-list': return listReminders(snapshot, request, nowMs);
|
|
422
|
+
case 'remind-show': return showReminder(snapshot, request, nowMs);
|
|
423
|
+
case 'remind-cancel': return cancelReminder(snapshot, request, nowMs);
|
|
424
|
+
default: fail(`unknown reminder operation: ${request.op}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export const REMIND_OPS = new Set(['remind-add', 'remind-list', 'remind-show', 'remind-cancel']);
|
|
429
|
+
|
|
430
|
+
// ---------------------------------------------------------------- presentation
|
|
431
|
+
// Tokens (id, phase/view names, source, times) stay machine-shaped: they are the same words the spec and
|
|
432
|
+
// the JSON use, so a person and a script read one vocabulary. Prose is Chinese, like the rest of the CLI.
|
|
433
|
+
|
|
434
|
+
function humanDelta(ms) {
|
|
435
|
+
const a = Math.abs(ms);
|
|
436
|
+
const suffix = a < 1000 ? '<1s'
|
|
437
|
+
: a < 60000 ? `${Math.ceil(a / 1000)}s`
|
|
438
|
+
: a < 3600000 ? `${Math.floor(a / 60000)}m`
|
|
439
|
+
: a < 86400000 ? `${Math.floor(a / 3600000)}h`
|
|
440
|
+
: `${Math.floor(a / 86400000)}d`;
|
|
441
|
+
return ms >= 0 ? `还有 ${suffix}` : `已过 ${suffix}`;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function dueLine(record, nowMs) {
|
|
445
|
+
const delta = msOf(record.dueAt) - nowMs;
|
|
446
|
+
const rel = record.view === 'derived-due' ? '已到期,等 Pulse 入队' : humanDelta(delta);
|
|
447
|
+
return `到期 ${record.dueAt}(${rel})`;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function attrLine(record) {
|
|
451
|
+
const parts = [`来源 ${record.source}`];
|
|
452
|
+
if (record.key) parts.push(`key ${record.key}`);
|
|
453
|
+
if (record.task) parts.push(`task ${record.task}`);
|
|
454
|
+
return parts.join(' · ');
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function renderAdd(result) {
|
|
458
|
+
if (!result.ok) return `team remind add:${result.reason}\n`;
|
|
459
|
+
const record = result.reminder;
|
|
460
|
+
const head = result.created
|
|
461
|
+
? `已创建 ${record.id} · ${record.view}`
|
|
462
|
+
: `已存在 ${record.id} · ${record.view}(key '${record.key}' 下的同一请求;due 不重算)`;
|
|
463
|
+
return `${head}\n ${dueLine(record, Date.parse(result.now))}\n ${attrLine(record)} · owner ${record.owner}\n`;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function renderList(result) {
|
|
467
|
+
const c = result.counts;
|
|
468
|
+
const activeN = result.active.length;
|
|
469
|
+
const lines = [
|
|
470
|
+
`提醒 · ${activeN} 条活跃 · 已签发 ${result.highWater} · 已修剪 ${result.pruned} · 现在 ${result.now}`,
|
|
471
|
+
`视图 ${VIEWS.map(view => `${view}=${c[view] ?? 0}`).join(' ')}`,
|
|
472
|
+
];
|
|
473
|
+
if (!activeN && !result.terminalTotal) {
|
|
474
|
+
lines.push('(还没有提醒:team remind add --after 20m --message "…")');
|
|
475
|
+
return lines.join('\n') + '\n';
|
|
476
|
+
}
|
|
477
|
+
for (const record of result.active) {
|
|
478
|
+
lines.push(`${record.id} · ${record.view} · ${dueLine(record, Date.parse(result.now))} · ${attrLine(record)}`);
|
|
479
|
+
lines.push(record.message);
|
|
480
|
+
lines.push('');
|
|
481
|
+
}
|
|
482
|
+
if (result.terminalShown) {
|
|
483
|
+
for (const record of result.terminal) {
|
|
484
|
+
lines.push(`${record.id} · ${record.view} · 到期 ${record.dueAt} · 结束 ${record.endedAt} · ${attrLine(record)}`);
|
|
485
|
+
lines.push(record.message);
|
|
486
|
+
lines.push('');
|
|
487
|
+
}
|
|
488
|
+
} else if (result.terminalTotal) {
|
|
489
|
+
lines.push(`(另有 ${result.terminalTotal} 条已终结:team remind list --all 看它们)`);
|
|
490
|
+
}
|
|
491
|
+
return lines.join('\n') + '\n';
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function renderShow(result) {
|
|
495
|
+
if (!result.ok) return `team remind show:${result.reason}\n`;
|
|
496
|
+
const record = result.reminder;
|
|
497
|
+
const ended = record.endedAt ? `\n 结束 ${record.endedAt}` : '';
|
|
498
|
+
return [
|
|
499
|
+
`${record.id} · ${record.view}(phase ${record.phase})`,
|
|
500
|
+
` 到期 ${record.dueAt}`,
|
|
501
|
+
` 创建 ${record.createdAt}${ended}`,
|
|
502
|
+
` 视图 ${record.view}`,
|
|
503
|
+
` 归属 owner ${record.owner}${record.key ? ` · key ${record.key}` : ''} · 来源 ${record.source}${record.task ? ` · task ${record.task}` : ''}`,
|
|
504
|
+
' 正文(逐字,未截断):',
|
|
505
|
+
record.message,
|
|
506
|
+
'',
|
|
507
|
+
].join('\n');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function renderCancel(result) {
|
|
511
|
+
if (result.outcome === 'cancelled') {
|
|
512
|
+
return `已取消 ${result.reminder.id}(原 phase ${result.from})· 到期时不会再响\n`;
|
|
513
|
+
}
|
|
514
|
+
if (result.outcome === 'already-cancelled') {
|
|
515
|
+
return `已取消 ${result.reminder.id}:${result.reason}\n`;
|
|
516
|
+
}
|
|
517
|
+
const record = result.reminder;
|
|
518
|
+
return `team remind cancel:${result.reason}${record ? `\n ${record.id} · phase ${record.phase}` : ''}\n`;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export function renderRemind(op, result) {
|
|
522
|
+
switch (op) {
|
|
523
|
+
case 'remind-add': return renderAdd(result);
|
|
524
|
+
case 'remind-list': return renderList(result);
|
|
525
|
+
case 'remind-show': return renderShow(result);
|
|
526
|
+
case 'remind-cancel': return renderCancel(result);
|
|
527
|
+
default: return JSON.stringify(result) + '\n';
|
|
528
|
+
}
|
|
529
|
+
}
|
|
@@ -107,7 +107,7 @@ export function sanitize(value) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
// 标题的输入校验比显示净化更严:终端控制与双向/不可见格式控制一律拒绝(不是删掉)。
|
|
110
|
-
const TITLE_FORBIDDEN_RE = new RegExp(
|
|
110
|
+
export const TITLE_FORBIDDEN_RE = new RegExp(
|
|
111
111
|
`[\\u0000-\\u001F\\u007F-\\u009F\\u00AD\\u061C\\u200B-\\u200F\\u2028-\\u202E\\u2060-\\u206F\\uFEFF]`,
|
|
112
112
|
)
|
|
113
113
|
|
|
@@ -425,7 +425,7 @@ function validateLabel(from) {
|
|
|
425
425
|
throw input(`--from 必须是 1–${MAX_LABEL} 个 ASCII 字母/数字/点/下划线/连字符的平坦标识符`)
|
|
426
426
|
}
|
|
427
427
|
|
|
428
|
-
function validateTitle(title) {
|
|
428
|
+
export function validateTitle(title) {
|
|
429
429
|
if (typeof title !== 'string' || title.length === 0) throw input('缺少 --title <title>')
|
|
430
430
|
const points = Array.from(title).length
|
|
431
431
|
if (points > MAX_TITLE_CODEPOINTS) throw input(`--title 超过 ${MAX_TITLE_CODEPOINTS} 个码点`)
|
|
@@ -459,14 +459,20 @@ function readStdinBounded() {
|
|
|
459
459
|
chunks.push(Buffer.from(buf.subarray(0, n)))
|
|
460
460
|
}
|
|
461
461
|
const body = Buffer.concat(chunks, total)
|
|
462
|
+
validateBody(body)
|
|
463
|
+
return body
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Pure input validation shared with directed capture; CLI still owns bounded stdin reads. */
|
|
467
|
+
export function validateBody(body) {
|
|
462
468
|
if (body.length === 0) throw input('正文不能为空')
|
|
469
|
+
if (body.length > MAX_BODY_BYTES) throw input(`正文超过上界 ${MAX_BODY_BYTES} 字节(不截断)`)
|
|
463
470
|
if (body.includes(0)) throw input('正文里不许有 NUL 字节')
|
|
464
471
|
try {
|
|
465
472
|
new TextDecoder('utf-8', { fatal: true }).decode(body)
|
|
466
473
|
} catch {
|
|
467
474
|
throw input('正文不是合法 UTF-8')
|
|
468
475
|
}
|
|
469
|
-
return body
|
|
470
476
|
}
|
|
471
477
|
|
|
472
478
|
// ---------------------------------------------------------------- 锁
|