@sellable/mcp 0.1.554 → 0.1.555
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/dist/index-dev.js +0 -0
- package/dist/index.js +0 -0
- package/dist/refill-contract.d.ts +157 -0
- package/dist/refill-contract.js +487 -0
- package/dist/refill-run-client.d.ts +5 -0
- package/dist/refill-run-client.js +15 -0
- package/dist/refill-run-loop.d.ts +12 -1
- package/dist/refill-run-loop.js +158 -13
- package/dist/tools/campaign-message-preparation.d.ts +62 -0
- package/dist/tools/campaign-message-preparation.js +41 -0
- package/dist/tools/evergreen-refill-plan.d.ts +3 -0
- package/dist/tools/evergreen-refill-plan.js +29 -7
- package/dist/tools/prompts.js +9 -0
- package/dist/tools/refill-executors.d.ts +38 -0
- package/dist/tools/refill-executors.js +222 -3
- package/dist/tools/refill-sends-v2.d.ts +112 -1
- package/dist/tools/refill-sends-v2.js +302 -2
- package/dist/tools/refill-sends.d.ts +678 -32
- package/dist/tools/refill-sends.js +274 -13
- package/dist/tools/refill-target-plan.js +486 -14
- package/dist/tools/registry.d.ts +96 -27
- package/dist/tools/registry.js +1 -3
- package/dist/tools/scheduler-fill-capacity.js +1 -1
- package/dist/tools/scheduler-run.d.ts +71 -0
- package/dist/tools/scheduler-run.js +203 -1
- package/dist/tools/workspaces.d.ts +4 -6
- package/dist/tools/workspaces.js +11 -13
- package/package.json +1 -1
- package/skills/refill-sends/SKILL.md +91 -353
- package/skills/refill-sends-v2/SKILL.md +6 -6
- package/skills/refill-sends-v2-workflow/SKILL.md +5 -5
- package/skills/refill-sends-v2-workflow/core/flow.v1.json +8 -8
- package/skills/refill-sends-workflow/SKILL.md +100 -743
- package/skills/refill-sends-workflow/core/contract.v2.json +543 -0
- package/skills/refill-sends-workflow/core/flow.v1.json +185 -1
- package/dist/refill-date-window.d.ts +0 -34
- package/dist/refill-date-window.js +0 -210
- package/dist/tools/refill-sends-evergreen.d.ts +0 -28
- package/dist/tools/refill-sends-evergreen.js +0 -47
- package/skills/research/config.json +0 -9
|
@@ -1,11 +1,26 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { REFILL_CONTRACT_CACHE_VERSION, REFILL_CONTRACT_HASH, REFILL_CONTRACT_VERSION, renderRefillSafetyGuidance, } from "../refill-contract.js";
|
|
1
3
|
import { appendIndexLine, appendJournalEvent, createRunJournal, renderBootstrapSection, renderCrashedAbandonedSection, renderExecutionEventSection, renderPlanSection, renderTerminalSection, } from "../refill-journal.js";
|
|
2
4
|
import { writeRefillWorkspaceState } from "../refill-local-state.js";
|
|
3
5
|
import { advanceRefillRunGateRemote, completeRefillRun, heartbeatRefillRun, recordRunOutcome, recordRunPlanned, refillRunStatus, startRefillRunRemote, } from "../refill-run-client.js";
|
|
4
6
|
import { runRefillV2Loop } from "../refill-run-loop.js";
|
|
5
|
-
import { getPrepareCampaignMessagesStatus } from "./campaign-message-preparation.js";
|
|
7
|
+
import { getPrepareCampaignMessagesStatus, stopSatisfiedPrepareCampaignMessages, } from "./campaign-message-preparation.js";
|
|
6
8
|
import { getRefillPlanV2 } from "./evergreen-refill-plan.js";
|
|
7
9
|
import { executeOneYoloPrimitive, executeStartCampaignPrimitive, prepareRowSelectorValue, refillPrepareRequestHash, refreshPaidInmailCreditsWithRetry, } from "./refill-executors.js";
|
|
8
10
|
import { runSchedulerSweep } from "./scheduler-run.js";
|
|
11
|
+
export const REFILL_V2_RUNTIME_IDENTITY = {
|
|
12
|
+
contract: {
|
|
13
|
+
version: REFILL_CONTRACT_VERSION,
|
|
14
|
+
hash: REFILL_CONTRACT_HASH,
|
|
15
|
+
cacheKey: REFILL_CONTRACT_CACHE_VERSION,
|
|
16
|
+
},
|
|
17
|
+
installed: {
|
|
18
|
+
mcpVersion: "phase98-local",
|
|
19
|
+
installVersion: "phase98-local",
|
|
20
|
+
pluginVersion: "phase98-local",
|
|
21
|
+
cacheIdentity: "phase98-local-plugin-cache",
|
|
22
|
+
},
|
|
23
|
+
};
|
|
9
24
|
const FORBIDDEN_ACTIONS = [
|
|
10
25
|
"Do not schedule sends.",
|
|
11
26
|
"Do not send messages.",
|
|
@@ -18,7 +33,11 @@ const BOUNDED_AUTHORITY = "Within the refill run, bounded approvals, preparation
|
|
|
18
33
|
export const refillSendsV2ToolDefinitions = [
|
|
19
34
|
{
|
|
20
35
|
name: "refill_sends_v2",
|
|
21
|
-
description:
|
|
36
|
+
description: [
|
|
37
|
+
"Execute the server-internal refill loop behind public refill_sends; it is retained for dispatch compatibility but is not advertised.",
|
|
38
|
+
`contractHash=${REFILL_CONTRACT_HASH}`,
|
|
39
|
+
renderRefillSafetyGuidance(),
|
|
40
|
+
].join(" "),
|
|
22
41
|
inputSchema: {
|
|
23
42
|
type: "object",
|
|
24
43
|
properties: {
|
|
@@ -51,6 +70,18 @@ export const refillSendsV2ToolDefinitions = [
|
|
|
51
70
|
type: "string",
|
|
52
71
|
enum: ["auto", "evergreen", "plain", "active"],
|
|
53
72
|
},
|
|
73
|
+
targetDate: {
|
|
74
|
+
type: "string",
|
|
75
|
+
description: "Exact sender-local YYYY-MM-DD date.",
|
|
76
|
+
},
|
|
77
|
+
untilDate: {
|
|
78
|
+
type: "string",
|
|
79
|
+
description: "Inclusive sender-local YYYY-MM-DD through-date.",
|
|
80
|
+
},
|
|
81
|
+
horizonSendDays: {
|
|
82
|
+
type: "number",
|
|
83
|
+
description: "Positive sender-local send-day horizon.",
|
|
84
|
+
},
|
|
54
85
|
},
|
|
55
86
|
required: ["workspaceId"],
|
|
56
87
|
additionalProperties: false,
|
|
@@ -68,6 +99,234 @@ function resumeHandle(input) {
|
|
|
68
99
|
? { runId: input.runId, fence: input.fence }
|
|
69
100
|
: undefined;
|
|
70
101
|
}
|
|
102
|
+
function validDate(value, field) {
|
|
103
|
+
const trimmed = value.trim();
|
|
104
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed) ||
|
|
105
|
+
new Date(`${trimmed}T00:00:00.000Z`).toISOString().slice(0, 10) !== trimmed) {
|
|
106
|
+
throw new Error(`${field} must be a valid YYYY-MM-DD date.`);
|
|
107
|
+
}
|
|
108
|
+
return trimmed;
|
|
109
|
+
}
|
|
110
|
+
function normalizeDateSelector(input) {
|
|
111
|
+
const selected = [
|
|
112
|
+
input.targetDate !== undefined,
|
|
113
|
+
input.untilDate !== undefined,
|
|
114
|
+
input.horizonSendDays !== undefined,
|
|
115
|
+
].filter(Boolean).length;
|
|
116
|
+
if (selected > 1) {
|
|
117
|
+
throw new Error("Provide exactly one refill date selector.");
|
|
118
|
+
}
|
|
119
|
+
if (input.targetDate !== undefined) {
|
|
120
|
+
const targetDate = validDate(input.targetDate, "targetDate");
|
|
121
|
+
return {
|
|
122
|
+
dateSelector: { kind: "target_date", targetDate },
|
|
123
|
+
targetDate,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (input.untilDate !== undefined) {
|
|
127
|
+
const untilDate = validDate(input.untilDate, "untilDate");
|
|
128
|
+
return {
|
|
129
|
+
dateSelector: { kind: "until_date", untilDate },
|
|
130
|
+
untilDate,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (input.horizonSendDays !== undefined) {
|
|
134
|
+
if (!Number.isInteger(input.horizonSendDays) ||
|
|
135
|
+
input.horizonSendDays <= 0) {
|
|
136
|
+
throw new Error("horizonSendDays must be a positive integer.");
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
dateSelector: {
|
|
140
|
+
kind: "horizon_send_days",
|
|
141
|
+
horizonSendDays: input.horizonSendDays,
|
|
142
|
+
},
|
|
143
|
+
horizonSendDays: input.horizonSendDays,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return { dateSelector: { kind: "scheduler_default" } };
|
|
147
|
+
}
|
|
148
|
+
function record(value) {
|
|
149
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
150
|
+
? value
|
|
151
|
+
: null;
|
|
152
|
+
}
|
|
153
|
+
function text(value) {
|
|
154
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
155
|
+
}
|
|
156
|
+
function list(value) {
|
|
157
|
+
return Array.isArray(value) ? value : [];
|
|
158
|
+
}
|
|
159
|
+
function sortedUnique(values) {
|
|
160
|
+
return [
|
|
161
|
+
...new Set(values.filter((value) => Boolean(value))),
|
|
162
|
+
].sort((left, right) => left.localeCompare(right));
|
|
163
|
+
}
|
|
164
|
+
function sha256(value) {
|
|
165
|
+
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
166
|
+
}
|
|
167
|
+
function targetPlanParts(targetPlan) {
|
|
168
|
+
const target = record(targetPlan.target) ?? {};
|
|
169
|
+
const plans = list(target.senderRefillPlans)
|
|
170
|
+
.map(record)
|
|
171
|
+
.filter((value) => value != null);
|
|
172
|
+
const actions = list(target.globalActionQueue)
|
|
173
|
+
.map(record)
|
|
174
|
+
.filter((value) => value != null);
|
|
175
|
+
return { target, plans, actions };
|
|
176
|
+
}
|
|
177
|
+
function stableIdsFromTargetPlan(targetPlan, input) {
|
|
178
|
+
const { plans, actions } = targetPlanParts(targetPlan);
|
|
179
|
+
const actionRecords = actions.map((action) => ({
|
|
180
|
+
action,
|
|
181
|
+
ids: record(action.ids) ?? {},
|
|
182
|
+
toolInput: record(action.toolInput) ?? {},
|
|
183
|
+
}));
|
|
184
|
+
const senderIds = sortedUnique([
|
|
185
|
+
...(input.senderIds ?? []),
|
|
186
|
+
...plans.map((plan) => text(plan.senderId)),
|
|
187
|
+
...actionRecords.map(({ ids, toolInput }) => text(ids.senderId) ?? text(toolInput.senderId)),
|
|
188
|
+
]);
|
|
189
|
+
const campaignIds = sortedUnique(actionRecords.map(({ ids, toolInput }) => text(ids.campaignId) ?? text(toolInput.campaignId)));
|
|
190
|
+
const tableIds = sortedUnique(actionRecords.map(({ ids, toolInput }) => text(ids.tableId) ?? text(toolInput.tableId)));
|
|
191
|
+
const actionIds = sortedUnique(actionRecords.map(({ action }) => text(action.actionKey) ?? text(action.type)));
|
|
192
|
+
const senderTimezones = Object.fromEntries(plans
|
|
193
|
+
.flatMap((plan) => list(plan.selectedDays))
|
|
194
|
+
.map(record)
|
|
195
|
+
.filter((day) => day != null)
|
|
196
|
+
.map((day) => [
|
|
197
|
+
text(day.senderId),
|
|
198
|
+
text(day.senderTimeZone) ?? text(day.timeZone),
|
|
199
|
+
])
|
|
200
|
+
.filter((entry) => entry[0] != null && entry[1] != null)
|
|
201
|
+
.sort(([left], [right]) => left.localeCompare(right)));
|
|
202
|
+
return {
|
|
203
|
+
senderIds,
|
|
204
|
+
campaignIds,
|
|
205
|
+
tableIds,
|
|
206
|
+
actionIds,
|
|
207
|
+
senderTimezones,
|
|
208
|
+
actions: actionRecords,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function buildApprovalEnvelope(input) {
|
|
212
|
+
const workspaceId = normalizeWorkspaceId(input.workspaceId);
|
|
213
|
+
const date = normalizeDateSelector(input);
|
|
214
|
+
const stable = stableIdsFromTargetPlan(input.targetPlan, input);
|
|
215
|
+
const schedulerAction = stable.actions.find(({ action }) => action.type === "run_scheduler_sweep");
|
|
216
|
+
const schedulerInput = schedulerAction?.toolInput ?? {};
|
|
217
|
+
const expectedTargets = list(schedulerInput.expectedTargets);
|
|
218
|
+
const expectedTargetsHash = text(schedulerInput.expectedTargetsHash) ?? sha256(expectedTargets);
|
|
219
|
+
const maxPlacements = typeof schedulerInput.maxPlacements === "number" &&
|
|
220
|
+
Number.isInteger(schedulerInput.maxPlacements) &&
|
|
221
|
+
schedulerInput.maxPlacements > 0
|
|
222
|
+
? schedulerInput.maxPlacements
|
|
223
|
+
: 150;
|
|
224
|
+
const scope = {
|
|
225
|
+
version: 2,
|
|
226
|
+
workspaceId,
|
|
227
|
+
intent: input.intent ?? "auto",
|
|
228
|
+
senderIds: stable.senderIds,
|
|
229
|
+
campaignIds: stable.campaignIds,
|
|
230
|
+
tableIds: stable.tableIds,
|
|
231
|
+
actionIds: stable.actionIds,
|
|
232
|
+
approvalMode: input.approvalMode ?? "approve",
|
|
233
|
+
dateSelector: date.dateSelector,
|
|
234
|
+
senderTimezones: stable.senderTimezones,
|
|
235
|
+
expectedTargetsHash,
|
|
236
|
+
maxPlacements,
|
|
237
|
+
contract: { ...REFILL_V2_RUNTIME_IDENTITY.contract },
|
|
238
|
+
installed: { ...REFILL_V2_RUNTIME_IDENTITY.installed },
|
|
239
|
+
};
|
|
240
|
+
const scopeHash = sha256(scope);
|
|
241
|
+
const targetShapeRevision = text(input.targetPlan.targetShapeRevision) ??
|
|
242
|
+
text(record(input.targetPlan.target)?.targetShapeRevision) ??
|
|
243
|
+
"missing_target_shape_revision";
|
|
244
|
+
const actionFingerprint = text(stable.actions[0]?.action.actionKey) ??
|
|
245
|
+
text(stable.actions[0]?.action.type) ??
|
|
246
|
+
"no_action";
|
|
247
|
+
const sideEffectEnvelope = sortedUnique(stable.actions.map(({ action }) => text(action.type)));
|
|
248
|
+
const approvalExpiresAt = input.approvalExpiresAt ??
|
|
249
|
+
new Date((input.now ?? new Date()).getTime() + 15 * 60_000).toISOString();
|
|
250
|
+
const fingerprintFields = {
|
|
251
|
+
scopeHash,
|
|
252
|
+
targetShapeRevision,
|
|
253
|
+
actionFingerprint,
|
|
254
|
+
expectedTargetsHash,
|
|
255
|
+
sideEffectEnvelope,
|
|
256
|
+
maxPlacements,
|
|
257
|
+
approvalExpiresAt,
|
|
258
|
+
};
|
|
259
|
+
const approval = {
|
|
260
|
+
status: "awaiting_approval",
|
|
261
|
+
...fingerprintFields,
|
|
262
|
+
approvalFingerprint: sha256(fingerprintFields),
|
|
263
|
+
};
|
|
264
|
+
return { scope, scopeHash, approval, date };
|
|
265
|
+
}
|
|
266
|
+
export async function prepareRefillSendsV2Approval(input) {
|
|
267
|
+
const envelope = buildApprovalEnvelope(input);
|
|
268
|
+
const started = await startRefillRunRemote({
|
|
269
|
+
workspaceId: envelope.scope.workspaceId,
|
|
270
|
+
config: {
|
|
271
|
+
scope: envelope.scope,
|
|
272
|
+
scopeHash: envelope.scopeHash,
|
|
273
|
+
approval: envelope.approval,
|
|
274
|
+
},
|
|
275
|
+
runState: {
|
|
276
|
+
version: 1,
|
|
277
|
+
scope: envelope.scope,
|
|
278
|
+
scopeHash: envelope.scopeHash,
|
|
279
|
+
approval: envelope.approval,
|
|
280
|
+
senderCursors: [],
|
|
281
|
+
laneCooldowns: [],
|
|
282
|
+
passRates: [],
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
const result = record(started) ?? {};
|
|
286
|
+
const runId = text(result.runId);
|
|
287
|
+
const fence = typeof result.fence === "number" && Number.isFinite(result.fence)
|
|
288
|
+
? result.fence
|
|
289
|
+
: null;
|
|
290
|
+
if (!runId || fence == null) {
|
|
291
|
+
return {
|
|
292
|
+
status: "blocked",
|
|
293
|
+
blocker: text(result.blocker) ?? "approval_run_unavailable",
|
|
294
|
+
response: started,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const persistedConfig = record(result.config);
|
|
298
|
+
const persistedScope = record(persistedConfig?.scope) ?? envelope.scope;
|
|
299
|
+
const persistedApproval = record(persistedConfig?.approval) ??
|
|
300
|
+
envelope.approval;
|
|
301
|
+
return {
|
|
302
|
+
...persistedApproval,
|
|
303
|
+
status: "awaiting_approval",
|
|
304
|
+
runId,
|
|
305
|
+
fence,
|
|
306
|
+
scope: persistedScope,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function validateApprovalEcho(envelope, echo, now = new Date()) {
|
|
310
|
+
const fields = [
|
|
311
|
+
"scopeHash",
|
|
312
|
+
"targetShapeRevision",
|
|
313
|
+
"actionFingerprint",
|
|
314
|
+
"expectedTargetsHash",
|
|
315
|
+
"approvalExpiresAt",
|
|
316
|
+
"approvalFingerprint",
|
|
317
|
+
];
|
|
318
|
+
const changed = fields.filter((field) => echo[field] !== envelope.approval[field]);
|
|
319
|
+
if (Date.parse(envelope.approval.approvalExpiresAt) <= now.getTime()) {
|
|
320
|
+
return { ok: false, blocker: "approval_expired", changed };
|
|
321
|
+
}
|
|
322
|
+
return changed.length === 0
|
|
323
|
+
? { ok: true }
|
|
324
|
+
: {
|
|
325
|
+
ok: false,
|
|
326
|
+
blocker: "approval_fingerprint_mismatch",
|
|
327
|
+
changed,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
71
330
|
async function maybeAddLostFenceGuidance(result, input) {
|
|
72
331
|
if (result.status !== "blocked" ||
|
|
73
332
|
result.blocker !== "lease_lost" ||
|
|
@@ -94,12 +353,45 @@ async function maybeAddLostFenceGuidance(result, input) {
|
|
|
94
353
|
}
|
|
95
354
|
export async function refillSendsV2Command(input) {
|
|
96
355
|
const workspaceId = normalizeWorkspaceId(input.workspaceId);
|
|
356
|
+
const date = normalizeDateSelector(input);
|
|
357
|
+
let resolvedScope = input.scope;
|
|
358
|
+
let resolvedScopeHash = input.scopeHash;
|
|
359
|
+
let resolvedApproval = input.approval;
|
|
360
|
+
if (input.targetPlan && input.approvalEcho) {
|
|
361
|
+
const envelope = buildApprovalEnvelope({
|
|
362
|
+
workspaceId,
|
|
363
|
+
intent: input.intent,
|
|
364
|
+
senderIds: input.senderIds,
|
|
365
|
+
approvalMode: input.approvalMode,
|
|
366
|
+
targetDate: input.targetDate,
|
|
367
|
+
untilDate: input.untilDate,
|
|
368
|
+
horizonSendDays: input.horizonSendDays,
|
|
369
|
+
targetPlan: input.targetPlan,
|
|
370
|
+
approvalExpiresAt: input.approvalEcho.approvalExpiresAt,
|
|
371
|
+
});
|
|
372
|
+
const validation = validateApprovalEcho(envelope, input.approvalEcho);
|
|
373
|
+
if (!validation.ok) {
|
|
374
|
+
return {
|
|
375
|
+
status: "blocked",
|
|
376
|
+
blocker: validation.blocker,
|
|
377
|
+
guidance: "The durable refill approval no longer matches fresh scope; render and approve a new packet before mutation.",
|
|
378
|
+
report: { changed: validation.changed, fresh: envelope.approval },
|
|
379
|
+
readOnly: false,
|
|
380
|
+
forbiddenActions: FORBIDDEN_ACTIONS,
|
|
381
|
+
boundedAuthority: BOUNDED_AUTHORITY,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
resolvedScope = envelope.scope;
|
|
385
|
+
resolvedScopeHash = envelope.scopeHash;
|
|
386
|
+
resolvedApproval = envelope.approval;
|
|
387
|
+
}
|
|
97
388
|
if (input.dryRun) {
|
|
98
389
|
const plan = await getRefillPlanV2({
|
|
99
390
|
workspaceId,
|
|
100
391
|
intent: input.intent,
|
|
101
392
|
senderIds: input.senderIds,
|
|
102
393
|
journal: true,
|
|
394
|
+
...date,
|
|
103
395
|
});
|
|
104
396
|
return {
|
|
105
397
|
...plan,
|
|
@@ -115,6 +407,10 @@ export async function refillSendsV2Command(input) {
|
|
|
115
407
|
senderIds: input.senderIds,
|
|
116
408
|
approvalMode: input.approvalMode ?? "approve",
|
|
117
409
|
resume: resumeHandle(input),
|
|
410
|
+
...date,
|
|
411
|
+
scope: resolvedScope,
|
|
412
|
+
scopeHash: resolvedScopeHash,
|
|
413
|
+
approval: resolvedApproval,
|
|
118
414
|
}, {
|
|
119
415
|
runClient: {
|
|
120
416
|
startRefillRunRemote,
|
|
@@ -131,12 +427,16 @@ export async function refillSendsV2Command(input) {
|
|
|
131
427
|
senderIds: planInput.senderIds,
|
|
132
428
|
runState: planInput.runState,
|
|
133
429
|
journal: planInput.journal,
|
|
430
|
+
targetDate: planInput.targetDate,
|
|
431
|
+
untilDate: planInput.untilDate,
|
|
432
|
+
horizonSendDays: planInput.horizonSendDays,
|
|
134
433
|
}),
|
|
135
434
|
executors: {
|
|
136
435
|
executeOneYoloPrimitive,
|
|
137
436
|
executeStartCampaignPrimitive,
|
|
138
437
|
refreshPaidInmailCreditsWithRetry,
|
|
139
438
|
getPrepareCampaignMessagesStatus,
|
|
439
|
+
stopSatisfiedPrepareCampaignMessages,
|
|
140
440
|
refillPrepareRequestHash,
|
|
141
441
|
prepareRowSelectorValue,
|
|
142
442
|
},
|