dsh-lowtide 0.2.3 → 0.2.5

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/lib/index.js CHANGED
@@ -7,6 +7,10 @@ import { installModelSelection } from "@deepseek-ai/dsh-agent";
7
7
  import { ReasoningEffortId, createUserMessage } from "@deepseek-ai/dsh-llm";
8
8
  import { SessionId, SessionLogOffset } from "@deepseek-ai/dsh-session";
9
9
  //#region ../core/src/windows.ts
10
+ /** The effective run-window list: `windows` when non-empty, else `[window]`. */
11
+ function batchWindowList(batch) {
12
+ return batch.windows !== void 0 && batch.windows.length > 0 ? [...batch.windows] : [batch.window];
13
+ }
10
14
  const DEFAULT_MULTIPLIER = {
11
15
  peak: 2,
12
16
  off: 1,
@@ -145,13 +149,21 @@ function nextOffPeakStart(when, windows, defaultTz = systemTimeZone()) {
145
149
  }
146
150
  return best;
147
151
  }
148
- /** Next batch start from a "HH:MM-HH:MM" window (local time in batch tz). */
152
+ /**
153
+ * Next batch start across the configured run window(s) (local time in batch
154
+ * tz). With multiple windows (issue #5) the earliest upcoming start wins —
155
+ * that is the moment the countdown and the confirm gate point at.
156
+ */
149
157
  function nextBatchAt(now, batch, defaultTz = systemTimeZone()) {
150
- const { start } = parseWindowRange(batch.window);
151
158
  const tz = batch.tz ?? defaultTz;
152
- let target = new Date(dayStartInTz(now, tz).getTime() + start * 6e4);
153
- if (target.getTime() <= now.getTime()) target = new Date(addDaysInTz(now, tz, 1).getTime() + start * 6e4);
154
- return target;
159
+ let best = null;
160
+ for (const range of batchWindowList(batch)) {
161
+ const { start } = parseWindowRange(range);
162
+ let target = new Date(dayStartInTz(now, tz).getTime() + start * 6e4);
163
+ if (target.getTime() <= now.getTime()) target = new Date(addDaysInTz(now, tz, 1).getTime() + start * 6e4);
164
+ if (best === null || target.getTime() < best.getTime()) best = target;
165
+ }
166
+ return best;
155
167
  }
156
168
  /** Minutes from `now` until `at` (floor), 0 when past. */
157
169
  function minutesUntil(now, at) {
@@ -4934,6 +4946,8 @@ const windowSchema = object({
4934
4946
  });
4935
4947
  const batchSchema = object({
4936
4948
  window: string(),
4949
+ /** Multi-window list (issue #5); absent in pre-0.2.4 state files = [window]. */
4950
+ windows: array(string()).optional(),
4937
4951
  tz: string().optional(),
4938
4952
  gateLeadMin: number(),
4939
4953
  maxTasksPerNight: number(),
@@ -4950,6 +4964,7 @@ const strictWindowSchema = windowSchema.extend({
4950
4964
  });
4951
4965
  const strictBatchSchema = batchSchema.extend({
4952
4966
  window: string().regex(/^([01]\d|2[0-3]):[0-5]\d-([01]\d|2[0-3]):[0-5]\d$/, "窗口须为 HH:MM-HH:MM"),
4967
+ windows: array(string().regex(/^([01]\d|2[0-3]):[0-5]\d-([01]\d|2[0-3]):[0-5]\d$/, "窗口须为 HH:MM-HH:MM")).min(1, "至少保留一个运行窗口").max(6, "运行窗口最多 6 个").optional(),
4953
4968
  tz: string().refine(isIanaTz, "非法时区").optional(),
4954
4969
  gateLeadMin: number().min(0).max(120),
4955
4970
  maxTasksPerNight: number().int().min(1),
@@ -4999,6 +5014,28 @@ const configUpdateSchema = object({
4999
5014
  budgetDailyYuan: number().min(0).optional(),
5000
5015
  maxReportHistory: number().int().min(0).optional()
5001
5016
  });
5017
+ /**
5018
+ * Keep `batch.window` and `batch.windows` consistent (issue #5): a patch
5019
+ * carrying `windows` is authoritative and mirrors its first entry into the
5020
+ * legacy `window` field (so a downgrade keeps running window #1); a patch
5021
+ * carrying only `window` (single-window client) collapses the list to it.
5022
+ * Patches touching neither leave both untouched.
5023
+ */
5024
+ function normalizeBatchWindows(merged, patch) {
5025
+ if (patch.windows !== void 0) {
5026
+ const list = [...patch.windows];
5027
+ return {
5028
+ ...merged,
5029
+ window: list[0] ?? merged.window,
5030
+ windows: list
5031
+ };
5032
+ }
5033
+ if (patch.window !== void 0) return {
5034
+ ...merged,
5035
+ windows: [patch.window]
5036
+ };
5037
+ return merged;
5038
+ }
5002
5039
  const stateSchema = object({
5003
5040
  version: literal(1),
5004
5041
  config: configSchema,
@@ -5170,10 +5207,10 @@ var LowtideStore = class LowtideStore {
5170
5207
  ...patch.maxReportHistory !== void 0 ? { maxReportHistory: patch.maxReportHistory } : {},
5171
5208
  ...patch.windows !== void 0 ? { windows: patch.windows } : {},
5172
5209
  ...patch.prices !== void 0 ? { prices: patch.prices } : {},
5173
- batch: patch.batch === void 0 ? this.state.config.batch : {
5210
+ batch: patch.batch === void 0 ? this.state.config.batch : normalizeBatchWindows({
5174
5211
  ...this.state.config.batch,
5175
5212
  ...patch.batch
5176
- }
5213
+ }, patch.batch)
5177
5214
  };
5178
5215
  return this.state.config;
5179
5216
  });
@@ -6679,8 +6716,10 @@ function assembleReportRows(queue, results) {
6679
6716
  * concurrency (`batch.maxConcurrency`, default 3). Stops launching new tasks
6680
6717
  * past windowEndAt but never interrupts a running task (PLAN §7.2). Returns
6681
6718
  * null when nothing executed — no empty execution reports (review round 1, B2).
6719
+ * `windowLabel` is the run window that triggered this batch (multi-window,
6720
+ * issue #5); absent = the first configured window, matching legacy reports.
6682
6721
  */
6683
- async function runBatch(ctx, store, windowEndAt, forced = false) {
6722
+ async function runBatch(ctx, store, windowEndAt, forced = false, windowLabel) {
6684
6723
  const startedAt = /* @__PURE__ */ new Date();
6685
6724
  const queue = [...store.tasks].filter((t) => t.status === "queued").sort((a, b) => a.priority - b.priority || a.createdAt.localeCompare(b.createdAt)).slice(0, store.config.batch.maxTasksPerNight);
6686
6725
  const results = /* @__PURE__ */ new Map();
@@ -6705,7 +6744,7 @@ async function runBatch(ctx, store, windowEndAt, forced = false) {
6705
6744
  const report = {
6706
6745
  id: `rpt-${Date.now()}`,
6707
6746
  date: localDateKey$1(/* @__PURE__ */ new Date()),
6708
- window: store.config.batch.window,
6747
+ window: windowLabel ?? store.config.batch.window,
6709
6748
  startedAt: startedAt.toISOString(),
6710
6749
  finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
6711
6750
  tasks: rows,
@@ -7443,6 +7482,18 @@ function validateConfigUpdate(patch) {
7443
7482
  } catch (error) {
7444
7483
  return `运行窗口格式不对:${error instanceof Error ? error.message : String(error)}`;
7445
7484
  }
7485
+ if (batch?.windows !== void 0) {
7486
+ const seenRanges = /* @__PURE__ */ new Set();
7487
+ for (const range of batch.windows) {
7488
+ if (seenRanges.has(range)) return `运行窗口重复:${range}`;
7489
+ seenRanges.add(range);
7490
+ try {
7491
+ parseWindowRange(range);
7492
+ } catch (error) {
7493
+ return `运行窗口格式不对(${range}):${error instanceof Error ? error.message : String(error)}`;
7494
+ }
7495
+ }
7496
+ }
7446
7497
  if (windows !== void 0) {
7447
7498
  const seen = /* @__PURE__ */ new Set();
7448
7499
  for (const window of windows) {
@@ -7486,11 +7537,7 @@ async function statePayload(ctx, routes) {
7486
7537
  const activeLevel = match?.level ?? "off";
7487
7538
  const multiplier = match?.multiplier ?? 1;
7488
7539
  const row = rowForLevel(tier, activeLevel, multiplier);
7489
- const nextBatch = nextBatchAt(now, {
7490
- window: config.batch.window,
7491
- tz: config.batch.tz,
7492
- gateLeadMin: config.batch.gateLeadMin
7493
- });
7540
+ const nextBatch = nextBatchAt(now, config.batch);
7494
7541
  const nextOff = nextOffPeakStart(now, windows);
7495
7542
  const systemTz = systemTimeZone();
7496
7543
  const officialInLocal = windowsInTz(OFFICIAL_PEAK_WINDOWS, systemTz, now);
@@ -7548,6 +7595,8 @@ async function statePayload(ctx, routes) {
7548
7595
  priceNotice: priceRouteNotice(modelId, now),
7549
7596
  batch: {
7550
7597
  window: config.batch.window,
7598
+ /** Effective run-window list (multi-window, issue #5). */
7599
+ windows: batchWindowList(config.batch),
7551
7600
  paused: config.batch.paused,
7552
7601
  running: scheduler.isRunning(),
7553
7602
  startedAt: scheduler.batchStartedAt()?.toISOString() ?? null,
@@ -7576,17 +7625,24 @@ async function statePayload(ctx, routes) {
7576
7625
  }
7577
7626
  };
7578
7627
  }
7579
- /** Whether `now` is inside [start, end) of the batch window (local tz). */
7580
- function inBatchWindow(now, window, tz) {
7581
- const { start, end } = parseWindowRange(window);
7628
+ /** Ranges covering `now`, in list order (midnight-crossing aware). */
7629
+ function activeWindows(now, windows, tz) {
7582
7630
  const { minutes } = localParts(now, tz ?? systemTimeZone());
7583
- if (end > start) return minutes >= start && minutes < end;
7584
- if (end < start) return minutes >= start || minutes < end;
7585
- return true;
7631
+ return windows.filter((range) => {
7632
+ const { start, end } = parseWindowRange(range);
7633
+ if (end > start) return minutes >= start && minutes < end;
7634
+ if (end < start) return minutes >= start || minutes < end;
7635
+ return true;
7636
+ });
7586
7637
  }
7587
- /** The batch window's end as an absolute Date (today's or the next occurrence). */
7638
+ /** The end of the ACTIVE batch window as an absolute Date (today's or the
7639
+ * next occurrence). With multiple windows the first covering range wins —
7640
+ * overlapping windows are a user choice the once-per-window latch and the
7641
+ * running guard absorb (documented in README/CHANGELOG). */
7588
7642
  function batchWindowEnd(now, window, tz) {
7589
- const { start, end } = parseWindowRange(window);
7643
+ const list = Array.isArray(window) ? window : [window];
7644
+ const active = activeWindows(now, list, tz);
7645
+ const { end } = parseWindowRange(active.length > 0 ? active[0] : list[0]);
7590
7646
  const resolvedTz = tz ?? systemTimeZone();
7591
7647
  const dayStart = dayStartInTz(now, resolvedTz);
7592
7648
  let target = new Date(dayStart.getTime() + end * 6e4);
@@ -7603,15 +7659,19 @@ function localDateKey(date, tz) {
7603
7659
  }
7604
7660
  /**
7605
7661
  * Identity of the batch window currently in progress, keyed by the calendar
7606
- * day the window STARTED (so a midnight-crossing window cannot run twice).
7607
- * Returns null when `now` is outside the window.
7662
+ * day the window STARTED (so a midnight-crossing window cannot run twice)
7663
+ * plus the window's range string — with multiple windows (issue #5) each
7664
+ * window earns its own once-per-occurrence latch. Returns null when `now`
7665
+ * is outside every window.
7608
7666
  */
7609
7667
  function currentWindowKey(now, window, tz) {
7610
- if (!inBatchWindow(now, window, tz)) return null;
7668
+ const active = activeWindows(now, Array.isArray(window) ? window : [window], tz);
7669
+ if (active.length === 0) return null;
7670
+ const range = active[0];
7611
7671
  const resolvedTz = tz ?? systemTimeZone();
7612
- const { start } = parseWindowRange(window);
7672
+ const { start } = parseWindowRange(range);
7613
7673
  const { minutes } = localParts(now, resolvedTz);
7614
- return `${localDateKey(minutes >= start ? now : addDaysInTz(now, resolvedTz, -1), resolvedTz)}|${window}|${resolvedTz}`;
7674
+ return `${localDateKey(minutes >= start ? now : addDaysInTz(now, resolvedTz, -1), resolvedTz)}|${range}|${resolvedTz}`;
7615
7675
  }
7616
7676
  /** Window-start recovery for deferred tasks (PLAN §2.1 + review B3). */
7617
7677
  function recoverDeferred(store) {
@@ -7635,11 +7695,13 @@ function startScheduler(ctx, store, tickMs = 3e4) {
7635
7695
  if (running) return;
7636
7696
  if (!forced && store.config.batch.paused) return;
7637
7697
  const now = /* @__PURE__ */ new Date();
7638
- if (!forced && !inBatchWindow(now, store.config.batch.window, store.config.batch.tz)) {
7698
+ const wins = batchWindowList(store.config.batch);
7699
+ const activeRange = forced ? void 0 : activeWindows(now, wins, store.config.batch.tz)[0];
7700
+ if (!forced && activeRange === void 0) {
7639
7701
  if (lastWindowKey !== null) lastWindowKey = null;
7640
7702
  return;
7641
7703
  }
7642
- const key = forced ? null : currentWindowKey(now, store.config.batch.window, store.config.batch.tz);
7704
+ const key = activeRange === void 0 ? null : currentWindowKey(now, wins, store.config.batch.tz);
7643
7705
  if (!forced) {
7644
7706
  if (key !== null && key === lastWindowKey) return;
7645
7707
  if (key !== null) lastWindowKey = key;
@@ -7649,7 +7711,7 @@ function startScheduler(ctx, store, tickMs = 3e4) {
7649
7711
  running = true;
7650
7712
  startedAt = /* @__PURE__ */ new Date();
7651
7713
  try {
7652
- await runBatch(ctx, store, forced ? new Date(Date.now() + 864e5) : batchWindowEnd(/* @__PURE__ */ new Date(), store.config.batch.window, store.config.batch.tz), forced);
7714
+ await runBatch(ctx, store, forced ? new Date(Date.now() + 864e5) : batchWindowEnd(/* @__PURE__ */ new Date(), wins, store.config.batch.tz), forced, activeRange ?? wins[0]);
7653
7715
  } catch (error) {
7654
7716
  ctx.logger("lowtide").warn("batch failed: %s", error instanceof Error ? error.message : String(error));
7655
7717
  } finally {
@@ -7693,7 +7755,7 @@ const inject = [
7693
7755
  ];
7694
7756
  function apply(ctx) {
7695
7757
  const store = LowtideStore.load(stateFilePath());
7696
- ctx.logger("lowtide").info("host half loaded (store: %d tasks, batch window %s)", store.tasks.length, store.config.batch.window);
7758
+ ctx.logger("lowtide").info("host half loaded (store: %d tasks, batch windows: %s)", store.tasks.length, batchWindowList(store.config.batch).join(", "));
7697
7759
  const hasRegistry = ctx.workspaceRegistry !== void 0;
7698
7760
  const hasSessions = ctx.sessions !== void 0;
7699
7761
  ctx.logger("lowtide").info("services: workspaceRegistry=%s, sessions=%s", hasRegistry, hasSessions);
@@ -286,6 +286,12 @@ declare const zh: {
286
286
  'settings.batchTitle': string;
287
287
  'settings.start': string;
288
288
  'settings.end': string;
289
+ 'settings.batchWindowList': string;
290
+ 'settings.addBatchWindow': string;
291
+ 'settings.batchWindowsHint': string;
292
+ 'settings.batchWindowOverlap': string;
293
+ 'settings.batchWindowInvalid': string;
294
+ 'settings.batchWindowMin': string;
289
295
  'settings.tz': string;
290
296
  'settings.gateLeadMin': string;
291
297
  'settings.maxTasksPerNight': string;
@@ -56,6 +56,7 @@ export interface HostState {
56
56
  officialDrift?: boolean;
57
57
  batch: {
58
58
  window: string;
59
+ windows?: string[];
59
60
  paused: boolean;
60
61
  running: boolean;
61
62
  startedAt: string | null;
@@ -82,8 +82,10 @@ export declare function assembleReportRows(queue: Task[], results: Map<string, R
82
82
  * concurrency (`batch.maxConcurrency`, default 3). Stops launching new tasks
83
83
  * past windowEndAt but never interrupts a running task (PLAN §7.2). Returns
84
84
  * null when nothing executed — no empty execution reports (review round 1, B2).
85
+ * `windowLabel` is the run window that triggered this batch (multi-window,
86
+ * issue #5); absent = the first configured window, matching legacy reports.
85
87
  */
86
- export declare function runBatch(ctx: Context, store: LowtideStore, windowEndAt: Date, forced?: boolean): Promise<MorningReport | null>;
88
+ export declare function runBatch(ctx: Context, store: LowtideStore, windowEndAt: Date, forced?: boolean, windowLabel?: string): Promise<MorningReport | null>;
87
89
  /** Helper for the /state aggregate: yesterday-style formatting for the report header. */
88
90
  export declare function reportDateLabel(dateIso: string): string;
89
91
  export {};
@@ -22,16 +22,22 @@ export interface Scheduler {
22
22
  }
23
23
  /** Maximum consecutive preflight deferrals before a task is marked failed. */
24
24
  export declare const MAX_PREFLIGHT_DEFER = 3;
25
- /** Whether `now` is inside [start, end) of the batch window (local tz). */
26
- export declare function inBatchWindow(now: Date, window: string, tz?: string): boolean;
27
- /** The batch window's end as an absolute Date (today's or the next occurrence). */
28
- export declare function batchWindowEnd(now: Date, window: string, tz?: string): Date;
25
+ /** Whether `now` is inside any of the batch windows (local tz). Accepts the
26
+ * legacy single-range string or the multi-window list (issue #5). */
27
+ export declare function inBatchWindow(now: Date, window: string | string[], tz?: string): boolean;
28
+ /** The end of the ACTIVE batch window as an absolute Date (today's or the
29
+ * next occurrence). With multiple windows the first covering range wins —
30
+ * overlapping windows are a user choice the once-per-window latch and the
31
+ * running guard absorb (documented in README/CHANGELOG). */
32
+ export declare function batchWindowEnd(now: Date, window: string | string[], tz?: string): Date;
29
33
  /**
30
34
  * Identity of the batch window currently in progress, keyed by the calendar
31
- * day the window STARTED (so a midnight-crossing window cannot run twice).
32
- * Returns null when `now` is outside the window.
35
+ * day the window STARTED (so a midnight-crossing window cannot run twice)
36
+ * plus the window's range string — with multiple windows (issue #5) each
37
+ * window earns its own once-per-occurrence latch. Returns null when `now`
38
+ * is outside every window.
33
39
  */
34
- export declare function currentWindowKey(now: Date, window: string, tz?: string): string | null;
40
+ export declare function currentWindowKey(now: Date, window: string | string[], tz?: string): string | null;
35
41
  /** Window-start recovery for deferred tasks (PLAN §2.1 + review B3). */
36
42
  export declare function recoverDeferred(store: LowtideStore): void;
37
43
  export declare function startScheduler(ctx: Context, store: LowtideStore, tickMs?: number): Scheduler;
@@ -15,6 +15,7 @@ export declare const configUpdateSchema: z.ZodObject<{
15
15
  paused: z.ZodOptional<z.ZodBoolean>;
16
16
  maxConcurrency: z.ZodOptional<z.ZodOptional<z.ZodNumber>>;
17
17
  window: z.ZodOptional<z.ZodString>;
18
+ windows: z.ZodOptional<z.ZodOptional<z.ZodArray<z.ZodString>>>;
18
19
  tz: z.ZodOptional<z.ZodOptional<z.ZodString>>;
19
20
  gateLeadMin: z.ZodOptional<z.ZodNumber>;
20
21
  maxTasksPerNight: z.ZodOptional<z.ZodNumber>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lowtide",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "lowtide: human-adjudicated off-peak batch task pipeline for dsh (peak/valley pricing aware)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -37,6 +37,13 @@
37
37
  "lib/types/**/*.d.ts",
38
38
  "cordis.patch.yml"
39
39
  ],
40
+ "scripts": {
41
+ "bundle": "tsdown && tsc -p tsconfig.build.json",
42
+ "watch": "tsdown --watch",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run",
45
+ "dev": "npx @deepseek-ai/dsh@0.1.5-rc.1 web --patch ./cordis.dev.yml"
46
+ },
40
47
  "license": "MIT",
41
48
  "keywords": [
42
49
  "dsh-plugin",
@@ -55,11 +62,11 @@
55
62
  "directory": "packages/dsh-lowtide"
56
63
  },
57
64
  "peerDependencies": {
58
- "@deepseek-ai/dsh-agent": "^0.1.5-rc.1",
59
- "@deepseek-ai/dsh-llm": "^0.1.5-rc.1",
60
- "@deepseek-ai/dsh-session": "^0.1.5-rc.1",
61
- "@deepseek-ai/dsh-settings": "^0.1.5-rc.1",
62
- "@deepseek-ai/dsh-storage-domain": "^0.1.5-rc.1"
65
+ "@deepseek-ai/dsh-agent": "^0.1.0-rc.7",
66
+ "@deepseek-ai/dsh-llm": "^0.1.0-rc.7",
67
+ "@deepseek-ai/dsh-session": "^0.1.0-rc.7",
68
+ "@deepseek-ai/dsh-settings": "^0.1.0-rc.7",
69
+ "@deepseek-ai/dsh-storage-domain": "^0.1.0-rc.7"
63
70
  },
64
71
  "devDependencies": {
65
72
  "@deepseek-ai/cordis": "^4.0.1",
@@ -86,19 +93,12 @@
86
93
  "@types/react": "~18.3.1",
87
94
  "immer": "^10.1.1",
88
95
  "lightningcss": "^1.32.0",
96
+ "lowtide-core": "workspace:*",
89
97
  "react": "^18.2.0",
90
98
  "tsdown": "^0.22.2",
91
99
  "typescript": "^6.0.3",
92
100
  "vitest": "^4.1.8",
93
101
  "zod": "^4.4.3",
94
- "zustand": "~4.4.7",
95
- "lowtide-core": "0.2.3"
96
- },
97
- "scripts": {
98
- "bundle": "tsdown && tsc -p tsconfig.build.json",
99
- "watch": "tsdown --watch",
100
- "typecheck": "tsc --noEmit",
101
- "test": "vitest run",
102
- "dev": "npx @deepseek-ai/dsh@0.1.5-rc.1 web --patch ./cordis.dev.yml"
102
+ "zustand": "~4.4.7"
103
103
  }
104
- }
104
+ }