autoclaw 0.2.24: campaign credits that lapse show on the balance window (#1157)
AutoClaw's weekly campaign credits are swept at their wallet's end, but the routing saw only the 6400 total with no date, so smart never spent them first. gcq909893267 found the date: every ledger entry carries a top-level wallet_expires_at (epoch seconds); the wallets endpoint has none. campaignBreakdown keeps each instance's wallet_expires_at and gives the soonest end of an instance with credits left and not yet past, and what the instances ending then have left. usage() puts that on the 积分余额 window as runsOutAt / runsOut, capped at the total. The reporter suggested a separate 活动积分 window with its own balance; magpie counts every balance window as a pool, so that would have counted 5000 twice. What magpie does with it: routing takes runsOutAt as the window's reset when it is sooner (routing.go:618), so the account counts as renewing tonight. Allowance.Lapsing needs a limit (Of) and skips a balance-only window, so the lapse share sort doesn't apply here. Tests use the reporter's literal credit and expire entries: 5000 lapse at 2026-11-15 23:59:59 +08, a spent earlier instance doesn't count, two ending in one second add up, past the end or without the field nothing is reported, and the usage window carries it with one balance window. Run: on mote, packages/autoclaw bun test 119 pass, check.mjs autoclaw ok. With index.mjs at 0.2.23 both new tests fail. Not tried: a live AutoClaw account.
This commit is contained in:
@@ -904,12 +904,15 @@ function campaignLabel(entry, md) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动流水 → { claimed, expired, remaining, labels }(窗口内事实)。
|
||||
* 活动流水 → { claimed, expired, remaining, labels, lapseAt, lapsing }(窗口内事实)。
|
||||
* 按 wallet 实例分组:每实例 remaining = credit − debit − expire(截 0)。
|
||||
* 入账掉出窗口的旧实例(窗口内只剩消耗/失效)按 0 计,其失效额仍进「过期」。
|
||||
* lapseAt:还有剩余、尚未到期的实例里最早的 wallet_expires_at(毫秒,0 = 不知道);
|
||||
* lapsing:在那一刻到期的实例剩余之和(plugins#1157:每条流水顶层都带
|
||||
* wallet_expires_at,epoch 秒;wallets 接口没有到期字段)。
|
||||
*/
|
||||
function campaignBreakdown(entries) {
|
||||
const instances = new Map(); // wallet_id → { credit, debit, expire, label }
|
||||
function campaignBreakdown(entries, now = Date.now()) {
|
||||
const instances = new Map(); // wallet_id → { credit, debit, expire, label, expires }
|
||||
let claimed = 0;
|
||||
let expired = 0;
|
||||
for (const e of Array.isArray(entries) ? entries : []) {
|
||||
@@ -923,7 +926,9 @@ function campaignBreakdown(entries) {
|
||||
if (!Number.isFinite(amount)) continue;
|
||||
const id = String(e.wallet_id ?? "?");
|
||||
let inst = instances.get(id);
|
||||
if (!inst) { inst = { credit: 0, debit: 0, expire: 0, label: "" }; instances.set(id, inst); }
|
||||
if (!inst) { inst = { credit: 0, debit: 0, expire: 0, label: "", expires: 0 }; instances.set(id, inst); }
|
||||
const exp = Number(e.wallet_expires_at);
|
||||
if (Number.isFinite(exp) && exp > 0) inst.expires = exp * 1000;
|
||||
if (type === "credit") {
|
||||
inst.credit += amount;
|
||||
claimed += amount;
|
||||
@@ -938,12 +943,17 @@ function campaignBreakdown(entries) {
|
||||
}
|
||||
if (instances.size === 0) return null; // 没有活动历史
|
||||
let remaining = 0;
|
||||
let lapseAt = 0;
|
||||
let lapsing = 0;
|
||||
const labels = [];
|
||||
for (const inst of instances.values()) {
|
||||
const left = inst.credit - inst.debit - inst.expire;
|
||||
if (left > 0) { remaining += left; if (inst.label) labels.push(inst.label); }
|
||||
if (!(left > 0 && inst.expires > now)) continue;
|
||||
if (!lapseAt || inst.expires < lapseAt) (lapseAt = inst.expires), (lapsing = left);
|
||||
else if (inst.expires === lapseAt) lapsing += left;
|
||||
}
|
||||
return { claimed, expired, remaining, labels: [...new Set(labels)] };
|
||||
return { claimed, expired, remaining, labels: [...new Set(labels)], lapseAt, lapsing };
|
||||
}
|
||||
|
||||
/** 活动积分的三位分组(确定性手写,不用 toLocaleString——CI/locale 无关)。 */
|
||||
@@ -1616,6 +1626,13 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
|
||||
try {
|
||||
const entries = await campaignCached(signinAccountKey(providerId, auth), () => fetchCampaignLedger(token, hosts));
|
||||
const camp = campaignBreakdown(entries);
|
||||
// 活动积分到期作废:在余额窗口上标出何时丢多少(runsOutAt / runsOut),
|
||||
// 路由会在那之前先花这个账号。不另开带 balance 的窗口——那会被
|
||||
// 当成第二个资金池。丢的不会多于账户总余额。
|
||||
if (camp?.lapseAt && camp.lapsing > 0 && total > 0) {
|
||||
const bal = windows.find((w) => w.name === "积分余额");
|
||||
if (bal) Object.assign(bal, { runsOutAt: new Date(camp.lapseAt).toISOString(), runsOut: Math.min(total, camp.lapsing) });
|
||||
}
|
||||
if (camp && (camp.claimed > 0 || camp.expired > 0)) {
|
||||
const label = camp.labels.length === 1 ? `(${camp.labels[0].slice(0, 40)})`
|
||||
: camp.labels.length > 1 ? `(${camp.labels.length} 个活动)` : "";
|
||||
|
||||
@@ -3188,3 +3188,72 @@ test("signInHolds: taken on any host is true, refused everywhere false, otherwis
|
||||
globalThis.fetch = realFetch
|
||||
}
|
||||
})
|
||||
|
||||
// plugins#1157(gcq909893267):活动积分到期作废。原样取自其账号的两条流水:
|
||||
// 一个还剩 5000、今晚 23:59:59 到期的实例,一个已被清零的旧实例。
|
||||
const liveCredit = (expires = 1794758399) => ({
|
||||
id: "2139248", asset_type: "point", wallet_type: "earned", wallet_id: "wlt_6872d6eff7be7a8990bd",
|
||||
wallet_scope: "campaign", mutation_type: "credit", amount: 5000, source: "campaign", description: "每周限时激励",
|
||||
metadata: {
|
||||
activity_id: "autoclaw_user_active_reward_202609", description: "每周限时激励", event_time: "1794154294",
|
||||
event_type: "autoclaw_user_active", grant_date: "2026-11-09", period_id: "514", public_wallet_type: "reward",
|
||||
reward_config_hash: "6504aa7a2d35e007", reward_points: "5000", reward_rule: "flat", reward_source: "flat",
|
||||
reward_strategy: "flat", wallet_id: "wlt_6872d6eff7be7a8990bd", wallet_instance_balance_after: "5000",
|
||||
wallet_instance_balance_before: "0", wallet_instance_balance_delta: "5000", wallet_scope: "campaign",
|
||||
},
|
||||
occurred_at: "2026-11-09T00:11:34.129+08:00", wallet_expires_at: expires,
|
||||
})
|
||||
const sweptExpire = {
|
||||
id: "2138987", wallet_id: "wlt_6bfa1bb0696371cc4553", wallet_scope: "campaign", mutation_type: "expire",
|
||||
amount: -5000, source: "manual", description: "活动积分到期失效",
|
||||
metadata: {
|
||||
expiration_reason: "expired_sweep", expiration_trigger: "wallet-expiry-balance-zeroing", expired_amount: "5000",
|
||||
expired_original_at: "1794153599", wallet_id: "wlt_6bfa1bb0696371cc4553", wallet_instance_balance_after: "0",
|
||||
wallet_instance_balance_before: "5000", wallet_instance_balance_delta: "-5000", wallet_scope: "campaign",
|
||||
writeoff_amount: "5000",
|
||||
},
|
||||
occurred_at: "2026-11-09T00:00:00.077+08:00", wallet_expires_at: 1794153599,
|
||||
}
|
||||
|
||||
test("campaign: a live instance's wallet_expires_at is when its credits lapse", () => {
|
||||
const noon = Date.parse("2026-11-15T12:00:00+08:00")
|
||||
const b = _internal.campaignBreakdown([liveCredit(), sweptExpire], noon)
|
||||
expect(b.remaining).toBe(5000)
|
||||
expect(b.lapseAt).toBe(1794758399 * 1000)
|
||||
expect(b.lapsing).toBe(5000)
|
||||
// 一个更早到期但已花完的实例不算;同一秒到期的两个实例相加
|
||||
const spent = { ...liveCredit(1794700000), wallet_id: "w-spent" }
|
||||
const spentDebit = { wallet_id: "w-spent", wallet_scope: "campaign", mutation_type: "debit", amount: -5000 }
|
||||
const twin = { ...liveCredit(), wallet_id: "w-twin", amount: 300 }
|
||||
const c = _internal.campaignBreakdown([liveCredit(), spent, spentDebit, twin], Date.parse("2026-11-14T00:00:00+08:00"))
|
||||
expect(c.lapseAt).toBe(1794758399 * 1000)
|
||||
expect(c.lapsing).toBe(5300)
|
||||
// 过了到期时间(清扫还没到)就不再报
|
||||
expect(_internal.campaignBreakdown([liveCredit()], Date.parse("2026-11-16T00:00:01+08:00")).lapseAt).toBe(0)
|
||||
// 没有 wallet_expires_at 的旧流水:不知道就不报,绝不猜
|
||||
const bare = liveCredit()
|
||||
delete bare.wallet_expires_at
|
||||
expect(_internal.campaignBreakdown([bare], noon)).toMatchObject({ remaining: 5000, lapseAt: 0, lapsing: 0 })
|
||||
})
|
||||
|
||||
test("campaign: the balance window says how much lapses and when, with no second pool", async () => {
|
||||
const realFetch = globalThis.fetch
|
||||
const dir = mkdtempSync(join(tmpdir(), "autoclaw-lapse-"))
|
||||
_internal.setUsageStatePath(join(dir, ".usage-state.json"))
|
||||
const tonight = Math.floor(Date.now() / 1000) + 3600
|
||||
globalThis.fetch = campFetch({ total: 6400, ledger: [liveCredit(tonight), sweptExpire] })
|
||||
try {
|
||||
const hooks = await campHooks()
|
||||
const u = await hooks.auth.usage(async () => campAuth("lapse"))
|
||||
const bal = u.windows.find((w) => w.name === "积分余额")
|
||||
expect(bal).toEqual({ name: "积分余额", balance: 6400, unit: "积分", runsOutAt: new Date(tonight * 1000).toISOString(), runsOut: 5000 })
|
||||
expect(u.windows.filter((w) => w.balance !== undefined)).toHaveLength(1)
|
||||
// 丢的不会多于账户总余额
|
||||
_internal.clearCampaignLedger()
|
||||
globalThis.fetch = campFetch({ total: 1200, ledger: [liveCredit(tonight)] })
|
||||
const low = await hooks.auth.usage(async () => campAuth("lapse-low"))
|
||||
expect(low.windows.find((w) => w.name === "积分余额").runsOut).toBe(1200)
|
||||
} finally {
|
||||
campCleanup(realFetch, dir)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-autoclaw-auth",
|
||||
"version": "0.2.23",
|
||||
"version": "0.2.24",
|
||||
"description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
Reference in New Issue
Block a user