Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/memos-local-plugin/core/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const DEFAULT_CONFIG: ResolvedConfig = {
minContentCharsForCompletion: 40,
toolHeavyRatio: 0.7,
minAssistantCharsForToolHeavy: 80,
cronSentinels: [],
},
l2Induction: {
minSimilarity: 0.65,
Expand Down
9 changes: 9 additions & 0 deletions apps/memos-local-plugin/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ const AlgorithmSchema = Type.Object({
* scored normally even if tool calls dominate. Default 80.
*/
minAssistantCharsForToolHeavy: NumberInRange(80, 0, 10_000),
/**
* User-turn prefixes that identify cron/scheduled episodes. When
* the first user turn starts with any of these, the exchange-count
* gate is bypassed. Empty by default to preserve existing scoring
* behavior until an operator explicitly configures a sentinel.
*/
cronSentinels: Type.Array(Type.String({ minLength: 1, pattern: "^\\S" }), {
default: [],
}),
}, { default: {} }),
l2Induction: Type.Object({
/** Cosine ≥ this to associate a new trace with an existing L2 policy. */
Expand Down
38 changes: 35 additions & 3 deletions apps/memos-local-plugin/core/reward/reward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,14 @@ function looksLikeTrivialContent(text: string): boolean {
function decideSkipReason(
snapshot: import("../session/types.js").EpisodeSnapshot,
traces: readonly TraceRow[],
cfg: Pick<RewardConfig, "minExchangesForCompletion" | "minContentCharsForCompletion" | "toolHeavyRatio" | "minAssistantCharsForToolHeavy">,
cfg: Pick<
RewardConfig,
| "minExchangesForCompletion"
| "minContentCharsForCompletion"
| "toolHeavyRatio"
| "minAssistantCharsForToolHeavy"
| "cronSentinels"
>,
): string | null {
// Prefer the live snapshot's turn list; fall back to traces when the
// snapshot came from a SQLite row (no turns materialised).
Expand Down Expand Up @@ -448,9 +455,34 @@ function decideSkipReason(
// 1. Not enough real conversation turns (need at least N user-assistant exchanges)
const exchanges = Math.min(userTurns, assistantTurns);
if (exchanges < cfg.minExchangesForCompletion) {
return (
`对话轮次不足(${exchanges} 轮),需要至少 ${cfg.minExchangesForCompletion} 轮完整的问答交互才能生成摘要。`
// Cron episodes always have exactly 1 user turn (the task prompt) so this
// gate always fires for them. Bypass it when the first user content starts
// with a known cron sentinel — cron jobs are inherently substantive, and
// the content/triviality checks below provide the real substance filter.
//
// Fallback: if the first materialised user turn is empty (including the
// recovery path), also check snapshot.meta?.initialUserText. This field is
// set by the pipeline on episode creation and is generally reliable, but
// could be absent or stale in unusual recovery scenarios — if so, the
// episode falls through to the old "skip" behavior.
const metaInitialUserText = snapshot.meta?.initialUserText;
const firstUserContent =
userContents[0]?.trim().length
? userContents[0]
: typeof metaInitialUserText === "string"
? metaInitialUserText
: "";
const isCronEpisode = (cfg.cronSentinels ?? []).some(
(sentinel) =>
typeof sentinel === "string" &&
sentinel.trim().length > 0 &&
firstUserContent.startsWith(sentinel),
);
if (!isCronEpisode) {
return (
`对话轮次不足(${exchanges} 轮),需要至少 ${cfg.minExchangesForCompletion} 轮完整的问答交互才能生成摘要。`
);
}
}

// 2. No user messages at all
Expand Down
9 changes: 9 additions & 0 deletions apps/memos-local-plugin/core/reward/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ export interface RewardConfig {
* that the tool-heavy heuristic would otherwise skip. Default 80.
*/
minAssistantCharsForToolHeavy: number;
/**
* User-turn prefixes that identify cron/scheduled episodes. When the
* first user turn starts with any of these prefixes, the exchange-count
* gate (minExchangesForCompletion) is bypassed — cron jobs are
* inherently substantive regardless of turn count. Content and
* triviality checks still apply. Empty by default, so operators must
* explicitly opt in by configuring one or more non-blank prefixes.
*/
cronSentinels?: string[];
}

// ─── User feedback inputs ──────────────────────────────────────────────────
Expand Down
31 changes: 31 additions & 0 deletions apps/memos-local-plugin/tests/unit/config/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,37 @@ viewer:
expect("dimensions" in cfg.embedding).toBe(false);
});

describe("algorithm.reward.cronSentinels", () => {
it("is opt-in and defaults to an empty list", () => {
const cfg = resolveConfig({});
expect(cfg.algorithm.reward.cronSentinels).toEqual([]);
});

it("accepts explicit non-blank prefixes", () => {
const cronSentinels = [
"[IMPORTANT: You are running as a scheduled cron job",
];
const cfg = resolveConfig({ algorithm: { reward: { cronSentinels } } });
expect(cfg.algorithm.reward.cronSentinels).toEqual(cronSentinels);
});

it.each(["", " "])("rejects a blank prefix (%j)", (sentinel) => {
expect(() =>
resolveConfig({
algorithm: { reward: { cronSentinels: [sentinel] } },
}),
).toThrow(/schema validation/);
});

it("rejects a prefix with leading whitespace", () => {
expect(() =>
resolveConfig({
algorithm: { reward: { cronSentinels: [" CRON:"] } },
}),
).toThrow(/schema validation/);
});
});

// ─── Issue #1929 — vectorScanMaxAgeMs contract ──────────────────────
// The schema must reject obviously bad values (negative, larger than
// a year, or non-numbers) so a "dirty" `PATCH /api/v1/config` cannot
Expand Down
209 changes: 209 additions & 0 deletions apps/memos-local-plugin/tests/unit/reward/reward.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,213 @@ describe("reward/integration", () => {
expect(res.feedbackCount).toBe(2);
expect(res.rHuman).toBeGreaterThan(0);
});

it("cron episode with 1 exchange is not skipped when sentinel matches", async () => {
const sid = "s_cron_1";
const eid = "ep_cron_1";
seedEpisode(handle, eid, sid, ["tr_cron_1"]);
seedTrace(handle, "tr_cron_1", eid, sid, {
userText:
"[IMPORTANT: You are running as a scheduled cron job. Please review recent activity and write a reflection card.",
agentText:
"Reviewed the last 48 hours of conversation traces and wrote a reflection card to ~/Faye/memory/reflections/2026-05-31.md. Commit succeeded.",
});

const events: RewardEvent[] = [];
const bus = createRewardEventBus();
bus.onAny((e) => events.push(e));

const runner = createRewardRunner({
tracesRepo: handle.repos.traces,
episodesRepo: handle.repos.episodes,
feedbackRepo: handle.repos.feedback,
llm: fakeLlm({
completeJson: {
"reward.reward.r_human.v3": {
goal_achievement: 0.8,
process_quality: 0.8,
user_satisfaction: 0.8,
label: "success",
reason: "cron reflection card written and committed",
},
},
}),
bus,
cfg: {
...cfg(),
minExchangesForCompletion: 2,
minContentCharsForCompletion: 40,
cronSentinels: ["[IMPORTANT: You are running as a scheduled cron job"],
},
now: () => NOW,
});

const res = await runner.run({
episodeId: eid as unknown as Parameters<typeof runner.run>[0]["episodeId"],
feedback: [],
trigger: "implicit_fallback",
});

// Must reach the LLM scorer, not be abandoned as trivial.
expect(res.humanScore.source).toBe("llm");
expect(res.rHuman).toBeGreaterThan(0);
expect(events.some((e) => e.kind === "reward.updated")).toBe(true);
// Must NOT be heuristic-skipped (reward.scored with source=heuristic means skipped).
const scoredEvent = events.find((e) => e.kind === "reward.scored");
expect((scoredEvent as { source?: string } | undefined)?.source).not.toBe("heuristic");
});

it("uses initialUserText when the first materialized user turn is empty", async () => {
const sid = "s_cron_meta";
const eid = "ep_cron_meta";
seedEpisode(handle, eid, sid, ["tr_cron_meta"]);
seedTrace(handle, "tr_cron_meta", eid, sid);
const snapshot: EpisodeSnapshot = {
...rewardSnapshot(eid, sid, ["tr_cron_meta"]),
turnCount: 3,
turns: [
{ role: "user", content: "", ts: NOW, meta: {} },
{
role: "assistant",
content:
"Reviewed recent activity and prepared a detailed reflection card for the user.",
ts: NOW,
meta: {},
},
{
role: "user",
content:
"Include the completed work, remaining risks, and concrete follow-up actions.",
ts: NOW,
meta: {},
},
],
meta: { initialUserText: "[CRON] Review recent activity." },
};
const llm = fakeLlm({
completeJson: {
"reward.reward.r_human.v3": {
goal_achievement: 0.8,
process_quality: 0.8,
user_satisfaction: 0.8,
label: "success",
reason: "cron reflection card prepared",
},
},
});
const runner = createRewardRunner({
tracesRepo: handle.repos.traces,
episodesRepo: handle.repos.episodes,
feedbackRepo: handle.repos.feedback,
getEpisodeSnapshot: () => snapshot,
llm,
bus: createRewardEventBus(),
cfg: {
...cfg(),
minExchangesForCompletion: 2,
cronSentinels: ["[CRON]"],
},
now: () => NOW,
});

const res = await runner.run({
episodeId: eid as unknown as Parameters<typeof runner.run>[0]["episodeId"],
feedback: [],
trigger: "implicit_fallback",
});

expect(res.humanScore.source).toBe("llm");
expect(llm.stats().requests).toBe(1);
});

it.each([
{
label: "the configured sentinel does not match",
cronSentinels: ["[IMPORTANT: You are running as a scheduled cron job"],
userText: "Please review recent activity and write a reflection card.",
},
{
label: "the feature is disabled",
cronSentinels: [],
userText:
"[IMPORTANT: You are running as a scheduled cron job. Please review recent activity.",
},
{
label: "an invalid empty sentinel reaches the runtime",
cronSentinels: [""],
userText: "Please review recent activity and write a reflection card.",
},
])("keeps the exchange gate when $label", async ({ cronSentinels, userText }) => {
const sid = "s_cron_rejected";
const eid = "ep_cron_rejected";
seedEpisode(handle, eid, sid, ["tr_cron_rejected"]);
seedTrace(handle, "tr_cron_rejected", eid, sid, {
userText,
agentText:
"Reviewed recent activity and prepared a substantive reflection card for the user.",
});
const llm = fakeLlm();
const runner = createRewardRunner({
tracesRepo: handle.repos.traces,
episodesRepo: handle.repos.episodes,
feedbackRepo: handle.repos.feedback,
llm,
bus: createRewardEventBus(),
cfg: {
...cfg(),
minExchangesForCompletion: 2,
cronSentinels,
},
now: () => NOW,
});

const res = await runner.run({
episodeId: eid as unknown as Parameters<typeof runner.run>[0]["episodeId"],
feedback: [],
trigger: "implicit_fallback",
});

expect(res.humanScore.source).toBe("heuristic");
expect(res.humanScore.reason).toContain("对话轮次不足");
expect(llm.stats().requests).toBe(0);
const episode = handle.repos.episodes.getById(
eid as unknown as EpisodeRow["id"],
)!;
expect(episode.meta?.reward).toMatchObject({ skipped: true });
});

it("still applies the content gate to a matching cron episode", async () => {
const sid = "s_cron_short";
const eid = "ep_cron_short";
seedEpisode(handle, eid, sid, ["tr_cron_short"]);
seedTrace(handle, "tr_cron_short", eid, sid, {
userText: "[CRON] Review recent activity.",
agentText: "Done.",
});
const llm = fakeLlm();
const runner = createRewardRunner({
tracesRepo: handle.repos.traces,
episodesRepo: handle.repos.episodes,
feedbackRepo: handle.repos.feedback,
llm,
bus: createRewardEventBus(),
cfg: {
...cfg(),
minExchangesForCompletion: 2,
minContentCharsForCompletion: 100,
cronSentinels: ["[CRON]"],
},
now: () => NOW,
});

const res = await runner.run({
episodeId: eid as unknown as Parameters<typeof runner.run>[0]["episodeId"],
feedback: [],
trigger: "implicit_fallback",
});

expect(res.humanScore.source).toBe("heuristic");
expect(res.humanScore.reason).toContain("对话内容过短");
expect(llm.stats().requests).toBe(0);
});
});
Loading