惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园 - Franky
罗磊的独立博客
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
V
V2EX
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
U
Unit 42
GbyAI
GbyAI
A
About on SuperTechFans
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
D
DataBreaches.Net
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(discord): preserve preference recency under invalid c...
steipete · 2026-05-30 · via Recent Commits to openclaw:main

@@ -3,7 +3,12 @@ import os from "node:os";

33

import path from "node:path";

44

import { normalizeAccountId as normalizeSharedAccountId } from "openclaw/plugin-sdk/account-id";

55

import { readJsonFileWithFallback } from "openclaw/plugin-sdk/json-store";

6-

import { MAX_DATE_TIMESTAMP_MS, timestampMsToIsoString } from "openclaw/plugin-sdk/number-runtime";

6+

import {

7+

MAX_DATE_TIMESTAMP_MS,

8+

resolveDateTimestampMs,

9+

resolveTimestampMsToIsoString,

10+

timestampMsToIsoString,

11+

} from "openclaw/plugin-sdk/number-runtime";

712

import { normalizeProviderId } from "openclaw/plugin-sdk/provider-model-shared";

813

import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";

914

import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";

@@ -14,11 +19,13 @@ const PREFERENCE_MAX_ENTRIES = 2_000;

1419

const MAX_PLUGIN_STATE_KEY_BYTES = 512;

1520

const textEncoder = new TextEncoder();

1621

let lastPreferenceTimestampMs = 0;

22+

let lastPreferenceOrder = 0;

17231824

type ModelPickerPreferencesEntry = {

1925

scopeKey: string;

2026

modelRef: string;

2127

updatedAt: string;

28+

updatedOrder?: number;

2229

};

23302431

type LegacyModelPickerPreferencesEntry = {

@@ -125,6 +132,7 @@ function sanitizeStoredPreferenceEntry(value: unknown): ModelPickerPreferencesEn

125132

scopeKey?: unknown;

126133

modelRef?: unknown;

127134

updatedAt?: unknown;

135+

updatedOrder?: unknown;

128136

};

129137

if (typeof typedValue.scopeKey !== "string" || typeof typedValue.modelRef !== "string") {

130138

return undefined;

@@ -137,6 +145,10 @@ function sanitizeStoredPreferenceEntry(value: unknown): ModelPickerPreferencesEn

137145

scopeKey: typedValue.scopeKey,

138146

modelRef,

139147

updatedAt: typeof typedValue.updatedAt === "string" ? typedValue.updatedAt : "",

148+

updatedOrder:

149+

typeof typedValue.updatedOrder === "number" && Number.isSafeInteger(typedValue.updatedOrder)

150+

? typedValue.updatedOrder

151+

: undefined,

140152

};

141153

}

142154

@@ -153,6 +165,21 @@ function timestampMs(value: string): number {

153165

return Number.isFinite(parsed) ? parsed : 0;

154166

}

155167168+

function timestampOrder(value?: number): number {

169+

return value !== undefined && value >= 0 ? value : 0;

170+

}

171+172+

function comparePreferenceEntries(

173+

left: { key: string; value: ModelPickerPreferencesEntry },

174+

right: { key: string; value: ModelPickerPreferencesEntry },

175+

): number {

176+

return (

177+

timestampMs(right.value.updatedAt) - timestampMs(left.value.updatedAt) ||

178+

timestampOrder(right.value.updatedOrder) - timestampOrder(left.value.updatedOrder) ||

179+

left.key.localeCompare(right.key)

180+

);

181+

}

182+156183

function legacyUpdatedAtForIndex(updatedAt: string, index: number, total: number): string {

157184

const baseMs = timestampMs(updatedAt);

158185

const anchorMs = Math.min(baseMs + Math.max(0, total), MAX_DATE_TIMESTAMP_MS);

@@ -165,9 +192,31 @@ function legacyUpdatedAtForIndex(updatedAt: string, index: number, total: number

165192

);

166193

}

167194168-

function nextPreferenceTimestampIso(): string {

169-

lastPreferenceTimestampMs = Math.max(Date.now(), lastPreferenceTimestampMs + 1);

170-

return new Date(lastPreferenceTimestampMs).toISOString();

195+

function nextPreferenceTimestamp(existingEntries: ModelPickerPreferencesEntry[]): {

196+

updatedAt: string;

197+

updatedOrder: number;

198+

} {

199+

const existingMaxTimestampMs = existingEntries.reduce(

200+

(max, entry) => Math.max(max, timestampMs(entry.updatedAt)),

201+

0,

202+

);

203+

lastPreferenceTimestampMs = Math.min(

204+

Math.max(

205+

resolveDateTimestampMs(Date.now(), 0),

206+

lastPreferenceTimestampMs + 1,

207+

existingMaxTimestampMs + 1,

208+

),

209+

MAX_DATE_TIMESTAMP_MS,

210+

);

211+

const existingMaxOrder = existingEntries.reduce(

212+

(max, entry) => Math.max(max, timestampOrder(entry.updatedOrder)),

213+

0,

214+

);

215+

lastPreferenceOrder = Math.max(lastPreferenceOrder + 1, existingMaxOrder + 1);

216+

return {

217+

updatedAt: resolveTimestampMsToIsoString(lastPreferenceTimestampMs),

218+

updatedOrder: lastPreferenceOrder,

219+

};

171220

}

172221173222

function normalizeLegacyPreferenceKey(key: string): string | undefined {

@@ -246,10 +295,13 @@ export async function readDiscordModelPickerRecentModels(params: {

246295

await importLegacyPreferences(params.env);

247296

const store = openPreferenceStore(params.env);

248297

const recent = (await store.entries())

249-

.map((entry) => sanitizeStoredPreferenceEntry(entry.value))

250-

.filter((entry): entry is ModelPickerPreferencesEntry => entry?.scopeKey === key)

251-

.toSorted((left, right) => timestampMs(right.updatedAt) - timestampMs(left.updatedAt))

252-

.map((entry) => entry.modelRef);

298+

.map((entry) => ({ key: entry.key, value: sanitizeStoredPreferenceEntry(entry.value) }))

299+

.filter(

300+

(entry): entry is { key: string; value: ModelPickerPreferencesEntry } =>

301+

entry.value?.scopeKey === key,

302+

)

303+

.toSorted(comparePreferenceEntries)

304+

.map((entry) => entry.value.modelRef);

253305

if (!params.allowedModelRefs || params.allowedModelRefs.size === 0) {

254306

return sanitizeRecentModels(recent, limit);

255307

}

@@ -277,10 +329,14 @@ export async function recordDiscordModelPickerRecentModel(params: {

277329

try {

278330

await importLegacyPreferences(params.env);

279331

const store = openPreferenceStore(params.env);

332+

const existingEntries = (await store.entries())

333+

.map((entry) => sanitizeStoredPreferenceEntry(entry.value))

334+

.filter((entry): entry is ModelPickerPreferencesEntry => entry?.scopeKey === key);

335+

const timestamp = nextPreferenceTimestamp(existingEntries);

280336

await store.register(buildPreferenceModelKey(key, normalizedModelRef), {

281337

scopeKey: key,

282338

modelRef: normalizedModelRef,

283-

updatedAt: nextPreferenceTimestampIso(),

339+

...timestamp,

284340

});

285341

const limit = Math.max(1, Math.min(params.limit ?? DEFAULT_RECENT_LIMIT, 10));

286342

const scopedEntries = (await store.entries())

@@ -289,11 +345,7 @@ export async function recordDiscordModelPickerRecentModel(params: {

289345

(entry): entry is { key: string; value: ModelPickerPreferencesEntry } =>

290346

entry.value?.scopeKey === key,

291347

)

292-

.toSorted(

293-

(left, right) =>

294-

timestampMs(right.value.updatedAt) - timestampMs(left.value.updatedAt) ||

295-

left.key.localeCompare(right.key),

296-

);

348+

.toSorted(comparePreferenceEntries);

297349

await Promise.all(scopedEntries.slice(limit).map((entry) => store.delete(entry.key)));

298350

} catch {

299351

return;