Skip to content

Commit 7eeed45

Browse files
mayoalexanderclaude
andcommitted
fix(opportunities): sync money fields + verify writes actually persisted (#176521)
diff and push each carried their own inline field list, and BOTH omitted every money and linkage field. Editing a contest's prize table locally then running diff reported 'No differences', and push reported 'Pushed' — while sending nothing. That left Song Wars anomalyco#668 as a placement bounty with no reward_tiers, i.e. one that would pay every winner $0. - SYNC_FIELDS: one shared list for diff + push, now including bounty_type, reward_tiers, rate_per_mille_cents, per_creator_cap_cents, budget_pool_cents, event_id, program_id, profile_id. - normalizeForCompare(): the API serializes reward_tiers as a {rank: amount} map while local files hold [{rank, amount_cents}] — compare normalized so we don't report false differences. - WRITE-CONFIRMATION: after PUT, re-read and assert each sent field persisted. On mismatch, print sent-vs-live per field and exit 1. 'Pushed' now means 'verified persisted' — a 200 alone is not proof (see #176520). Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 8eead2c commit 7eeed45

1 file changed

Lines changed: 97 additions & 15 deletions

File tree

‎packages/opencode/src/cli/cmd/platform-opportunities.ts‎

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,49 @@ const CreateCommand = cmd({
299299
},
300300
})
301301

302+
// #176521 — single source of truth for what pull/diff/push agree on.
303+
//
304+
// These lists used to be inline and divergent, and BOTH omitted every money and
305+
// linkage field. Result: edit a contest's prize table locally → `diff` reports
306+
// "No differences" → `push` reports "Pushed" → nothing was sent, leaving a
307+
// placement bounty with no reward_tiers, i.e. one that pays every winner $0.
308+
const SYNC_FIELDS = [
309+
"title", "description", "status",
310+
"price_min", "price_max", "application_deadline",
311+
"funding_goal_cents", "equity_pool_bps", "roles", "pitch_sections",
312+
"preview_mode", "is_public", "lead_id",
313+
// money / payout — omitting these is how prize tables got silently dropped
314+
"bounty_type", "reward_tiers", "rate_per_mille_cents",
315+
"per_creator_cap_cents", "budget_pool_cents",
316+
// linkage
317+
"event_id", "program_id", "profile_id",
318+
] as const
319+
320+
// The API serializes reward_tiers as a {rank: amount_cents} map (toPublicArray →
321+
// rewardTiers()), while the local file may hold the authoring shape
322+
// [{rank, amount_cents}, …] or a bare [amount_cents, …]. Compare on the
323+
// normalized map so we don't report a false difference.
324+
function normalizeForCompare(field: string, value: unknown): string {
325+
if (field === "reward_tiers" && value != null) {
326+
const map: Record<string, number> = {}
327+
if (Array.isArray(value)) {
328+
value.forEach((t: any, i: number) => {
329+
const rank = Number(t?.rank ?? i + 1)
330+
const amount = Number(t?.amount_cents ?? (typeof t === "number" ? t : 0))
331+
if (rank >= 1 && amount > 0) map[String(rank)] = amount
332+
})
333+
} else if (typeof value === "object") {
334+
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
335+
const rank = Number(k)
336+
const amount = Number(v)
337+
if (rank >= 1 && amount > 0) map[String(rank)] = amount
338+
}
339+
}
340+
return JSON.stringify(map)
341+
}
342+
return JSON.stringify(value ?? null)
343+
}
344+
302345
// #166095: previously the only way to change an opportunity's content was the
303346
// file-based `push` (pull → edit JSON → push). This gives a direct, flag-driven,
304347
// headless-safe update path — only the flags you pass are sent (PATCH-like PUT).
@@ -476,14 +519,17 @@ const PushCommand = cmd({
476519
return
477520
}
478521

479-
const payload: Record<string, unknown> = {
480-
title: entity.title, description: entity.description, skills_required: skills,
481-
price_min: entity.price_min ?? entity.min_budget, price_max: entity.price_max ?? entity.max_budget, application_deadline: entity.application_deadline ?? entity.deadline,
482-
funding_goal_cents: entity.funding_goal_cents, equity_pool_bps: entity.equity_pool_bps,
483-
roles: entity.roles, pitch_sections: entity.pitch_sections,
484-
preview_mode: entity.preview_mode, is_public: entity.is_public,
485-
lead_id: entity.lead_id,
522+
// #176521 — build from SYNC_FIELDS so money/linkage fields can never be
523+
// silently omitted again (the old inline list dropped reward_tiers et al).
524+
const payload: Record<string, unknown> = {}
525+
for (const f of SYNC_FIELDS) {
526+
if (entity[f] !== undefined) payload[f] = entity[f]
486527
}
528+
// Legacy aliases from older pulled files.
529+
payload.skills_required = skills
530+
if (payload.price_min === undefined && entity.min_budget !== undefined) payload.price_min = entity.min_budget
531+
if (payload.price_max === undefined && entity.max_budget !== undefined) payload.price_max = entity.max_budget
532+
if (payload.application_deadline === undefined && entity.deadline !== undefined) payload.application_deadline = entity.deadline
487533
for (const k of Object.keys(payload)) { if (payload[k] === undefined) delete payload[k] }
488534

489535
const res = await irisFetch(`/api/v1/marketplace/opportunities/${args.id}`, { method: "PUT", body: JSON.stringify(payload) })
@@ -492,12 +538,54 @@ const PushCommand = cmd({
492538

493539
const data = (await res.json()) as { data?: any }
494540
const result = data?.data ?? data
541+
542+
// WRITE-CONFIRMATION (#176521): a 200 is not proof of persistence. The API has
543+
// silently dropped fields that weren't mass-assignable (reward_tiers, #176520)
544+
// while still returning success. Re-read and assert, so "Pushed" means
545+
// "verified persisted" — and fail loudly (exit 1) when it doesn't.
546+
spinner.message?.("Verifying…")
547+
const unpersisted: { field: string; sent: unknown; live: unknown }[] = []
548+
try {
549+
const verifyRes = await irisFetch(`/api/v1/marketplace/opportunities/${args.id}`)
550+
if (verifyRes.ok) {
551+
const vJson = (await verifyRes.json()) as { data?: any }
552+
const liveNow = vJson?.data?.opportunity ?? vJson?.data ?? vJson
553+
for (const f of Object.keys(payload)) {
554+
if (f === "skills_required") continue // server may normalize/rename
555+
if (normalizeForCompare(f, liveNow?.[f]) !== normalizeForCompare(f, payload[f])) {
556+
unpersisted.push({ field: f, sent: payload[f], live: liveNow?.[f] })
557+
}
558+
}
559+
}
560+
} catch {
561+
// verification is best-effort; never mask a successful write with a network blip
562+
}
563+
564+
if (unpersisted.length > 0) {
565+
spinner.stop("Pushed, but some fields did NOT persist", 1)
566+
printDivider()
567+
printKV("ID", args.id)
568+
for (const u of unpersisted) {
569+
console.log(` ${UI.Style.TEXT_DANGER}✗ ${u.field}${UI.Style.TEXT_NORMAL}`)
570+
console.log(` sent: ${String(JSON.stringify(u.sent)).slice(0, 120)}`)
571+
console.log(` live: ${String(JSON.stringify(u.live ?? null)).slice(0, 120)}`)
572+
}
573+
console.log()
574+
console.log(` ${UI.Style.TEXT_WARNING}The API accepted the request but did not store these fields.${UI.Style.TEXT_NORMAL}`)
575+
console.log(` ${dim("Likely a server-side mass-assignment ($fillable) gap — see #176520.")}`)
576+
printDivider()
577+
prompts.outro("Done")
578+
process.exitCode = 1
579+
return
580+
}
581+
495582
spinner.stop(success("Pushed"))
496583

497584
printDivider()
498585
printKV("Title", result.title)
499586
printKV("ID", args.id)
500587
printKV("From", filepath)
588+
printKV("Verified", `${Object.keys(payload).length} field(s) persisted`)
501589
printDivider()
502590

503591
prompts.outro(dim(`iris opportunities diff ${args.id}`))
@@ -547,16 +635,10 @@ const DiffCommand = cmd({
547635

548636
const local = JSON.parse(readFileSync(filepath, "utf-8"))
549637

550-
const fields = [
551-
"title", "description", "status",
552-
"price_min", "price_max", "application_deadline",
553-
"funding_goal_cents", "equity_pool_bps", "roles", "pitch_sections",
554-
"preview_mode", "is_public", "lead_id",
555-
]
556638
const changes: { field: string; live: unknown; local: unknown }[] = []
557639

558-
for (const f of fields) {
559-
if (JSON.stringify(live[f] ?? null) !== JSON.stringify(local[f] ?? null)) {
640+
for (const f of SYNC_FIELDS) {
641+
if (normalizeForCompare(f, live[f]) !== normalizeForCompare(f, local[f])) {
560642
changes.push({ field: f, live: live[f], local: local[f] })
561643
}
562644
}

0 commit comments

Comments
 (0)