my-durable 0.0.41 → 0.0.43

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.
Files changed (26) hide show
  1. package/apps/dpi-backend/src/awake.ts +136 -0
  2. package/apps/dpi-backend/src/backup-cli.ts +220 -0
  3. package/apps/dpi-backend/src/backup.ts +523 -0
  4. package/apps/dpi-backend/src/diagnostics-cli.ts +134 -0
  5. package/apps/dpi-backend/src/diagnostics.ts +326 -0
  6. package/apps/dpi-backend/src/env-vars.ts +234 -0
  7. package/apps/dpi-backend/src/main.ts +1 -0
  8. package/apps/dpi-backend/src/projects.ts +33 -1
  9. package/apps/dpi-backend/src/routes.ts +21 -1
  10. package/apps/dpi-backend/src/store.ts +85 -3
  11. package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-BgKiQEMm.js +81 -0
  12. package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-EulngMVY.css +1 -0
  13. package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +2 -2
  14. package/apps/dpi-playground/apps/dpi-playground-fe/src/components/EnvPanel.vue +268 -0
  15. package/apps/dpi-playground/apps/dpi-playground-fe/src/components/HostPanel.vue +11 -0
  16. package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Inspector.vue +4 -0
  17. package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NewSessionDialog.vue +37 -0
  18. package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ProjectBar.vue +41 -1
  19. package/apps/dpi-sdk/src/api.ts +29 -2
  20. package/apps/dpi-sdk/src/client.ts +12 -0
  21. package/apps/dpi-sdk/src/protocol/rest-routes.ts +3 -0
  22. package/bin/dpi.mjs +16 -0
  23. package/docs/host-project-checklist.md +8 -8
  24. package/package.json +1 -1
  25. package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-D6ClJqd8.css +0 -1
  26. package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-DMb-n4O-.js +0 -81
@@ -50,6 +50,8 @@ import { DatabaseSync } from "node:sqlite";
50
50
  import { mkdir, readdir, realpath, rename as renamePath, stat } from "node:fs/promises";
51
51
  import { deviceVitals, type DeviceVitals } from "./vitals.js";
52
52
  import { ProjectRegistry, type ProjectRecord } from "./projects.js";
53
+ import { EnvFileEditor, type EnvFileView } from "./env-vars.js";
54
+ import { KeepAwake } from "./awake.js";
53
55
  import { ScheduleRegistry, type ScheduleCreateInput, type ScheduleRecord } from "./schedules.js";
54
56
  import { NotifySinks, type DeliveryResult, type SinkInput, type SinkRecord } from "./notify-sinks.js";
55
57
  import { zipSync, type ZipEntry } from "./zip.js";
@@ -143,6 +145,8 @@ export interface StoreOptions {
143
145
  startDirs: string[];
144
146
  name: string;
145
147
  provider: "openrouter" | "faux";
148
+ /** The env file the daemon booted from (config.ts's `envFile`); backs the env UI. */
149
+ envFile?: string;
146
150
  }
147
151
 
148
152
  /** Agent documents a client may read or stream, by kind. */
@@ -277,6 +281,11 @@ export class Store {
277
281
  schedules!: ScheduleRegistry;
278
282
  /** Outbound notifications (webhook/ntfy). Delivery never throws into a turn. */
279
283
  sinks!: NotifySinks;
284
+ /** The daemon's env file, behind the guarded editor (secrets masked, managed keys refused). */
285
+ readonly env: EnvFileEditor;
286
+ /** Keep-awake (Wave 3): holds the OS awake while any session is busy. */
287
+ readonly awake = new KeepAwake();
288
+ private awakeTimer?: ReturnType<typeof setTimeout>;
280
289
  uid!: string;
281
290
  modelList: ModelInfo[] = [];
282
291
  private unlockCatalog: () => void;
@@ -316,6 +325,9 @@ export class Store {
316
325
  }
317
326
 
318
327
  private constructor(readonly options: StoreOptions) {
328
+ // dataDir defaults to <home>/backend, so its parent is the config.ts home
329
+ // when the caller did not name an env file explicitly.
330
+ this.env = new EnvFileEditor(options.envFile ?? join(dirname(options.dataDir), "backend.env"));
319
331
  if (options.provider === "faux") installFaux(this.models);
320
332
  else {
321
333
  // The device is pi: register EVERY built-in provider (the same registry
@@ -406,8 +418,38 @@ export class Store {
406
418
  }
407
419
  notify = () => {
408
420
  for (const fn of this.listeners) fn();
421
+ this.scheduleAwakeSync();
409
422
  };
410
423
 
424
+ /**
425
+ * Keep-awake piggybacks on notify (every state change) with a debounce, so
426
+ * there is no extra timer while the device is idle. The inhibitor itself is
427
+ * idempotent — start when the first session turns busy, stop at zero.
428
+ */
429
+ private scheduleAwakeSync() {
430
+ if (this.awakeTimer) return;
431
+ this.awakeTimer = setTimeout(() => {
432
+ this.awakeTimer = undefined;
433
+ void this.syncAwake();
434
+ }, 1000);
435
+ this.awakeTimer.unref?.();
436
+ }
437
+
438
+ /** Count busy sessions and tell the inhibitor. Never throws into a turn. */
439
+ private async syncAwake() {
440
+ try {
441
+ let busy = 0;
442
+ for (const [id, opened] of this.opened) {
443
+ if (!this.opened.has(id)) continue;
444
+ const tasks = await this.liveTasks(opened.harness);
445
+ if (tasks.some((t) => !t.background)) busy += 1;
446
+ }
447
+ this.awake.sync(busy);
448
+ } catch {
449
+ /* keep-awake must never break a turn */
450
+ }
451
+ }
452
+
411
453
  /** The git fs-watcher pokes the tree so every connected UI hears "dirty". */
412
454
  private gitWired = false;
413
455
  wireGitWatcher() {
@@ -435,7 +477,9 @@ export class Store {
435
477
  ...(this.modelsKeyless ? { providerKeyMissing: true } : {}),
436
478
  storage: { dataDir: this.options.dataDir, catalogPath: this.catalogPath, appStatePath: this.sharedStatePath },
437
479
  codeDir: CODE_DIR,
438
- capabilities: ["events", "docs", "demo", "jobs"],
480
+ capabilities: this.awake.mode === "none" ? ["events", "docs", "demo", "jobs"] : ["events", "docs", "demo", "jobs", "keep-awake"],
481
+ /** How this device holds the OS awake while sessions run ("none" = it cannot). */
482
+ keepAwake: this.awake.mode,
439
483
  jobs: JOB_DEFS,
440
484
  };
441
485
  }
@@ -734,6 +778,25 @@ export class Store {
734
778
  });
735
779
  }
736
780
 
781
+ /** The env-file view (secrets masked, process-only vars read-only). */
782
+ envView(): Promise<EnvFileView> {
783
+ return this.env.view();
784
+ }
785
+
786
+ /** Set one env key in the device's env file. Applies after the next restart. */
787
+ async setEnvVar(key: string, value: string) {
788
+ const view = await this.env.set(key, value);
789
+ this.notify();
790
+ return view;
791
+ }
792
+
793
+ /** Remove one env key. Only file-sourced keys can be removed. */
794
+ async removeEnvVar(key: string) {
795
+ const result = await this.env.unset(key);
796
+ this.notify();
797
+ return result;
798
+ }
799
+
737
800
  /** `spawnKey` records which tool call created the session, in the same transaction as the session row. */
738
801
  async create(body: CreateSessionBody, spawnKey?: string): Promise<SessionSummary> {
739
802
  if (typeof body?.name !== "string") throw invalid("name is required");
@@ -753,7 +816,12 @@ export class Store {
753
816
  await this.projects
754
817
  .ensure({ rootPath: status?.repo ? (status.root ?? cwd) : cwd, kind: status?.repo ? "git" : "plain" })
755
818
  .catch(() => undefined);
756
- const model = this.validateModel(body.model);
819
+ // Per-project default model (Wave 2: per-project settings): a session that
820
+ // starts without an explicit model in a project that chose one uses the
821
+ // project's choice. The project of a path is its longest registered root.
822
+ const explicit = body.model as ModelRef | undefined;
823
+ const projectDefault = explicit ? undefined : this.projectForPath(cwd)?.model;
824
+ const model = this.validateModel(explicit ?? projectDefault);
757
825
  if (body.thinkingLevel !== undefined && !THINKING_LEVELS.includes(body.thinkingLevel))
758
826
  throw invalid("Invalid thinking level");
759
827
  const id = randomUUID();
@@ -1867,6 +1935,17 @@ export class Store {
1867
1935
 
1868
1936
  // ------------------------------------------------- projects (Wave 2)
1869
1937
 
1938
+ /** The project a path belongs to: its longest registered root. */
1939
+ private projectForPath(cwd: string): ProjectRecord | undefined {
1940
+ let best: ProjectRecord | undefined;
1941
+ for (const project of this.projects.list({ includeArchived: false })) {
1942
+ const root = project.rootPath.endsWith("/") ? project.rootPath : `${project.rootPath}/`;
1943
+ const isUnder = cwd === project.rootPath || cwd.startsWith(root);
1944
+ if (isUnder && (!best || project.rootPath.length > best.rootPath.length)) best = project;
1945
+ }
1946
+ return best;
1947
+ }
1948
+
1870
1949
  /** Projects, newest activity first: the tree's group-by-cwd as an entity. */
1871
1950
  projectList(includeArchived = false): ProjectRecord[] {
1872
1951
  return this.projects.list({ includeArchived });
@@ -1880,9 +1959,10 @@ export class Store {
1880
1959
  async updateProject(id: string, patch: ProjectPatchBody): Promise<ProjectRecord> {
1881
1960
  // Only the keys the caller actually sent: passing `customName: undefined`
1882
1961
  // must not read as "clear the name" (that is `customName: ""`).
1883
- const next: { customName?: string; labels?: string[] } = {};
1962
+ const next: { customName?: string; labels?: string[]; model?: { provider: string; modelId: string } | "" } = {};
1884
1963
  if (patch.customName !== undefined) next.customName = patch.customName;
1885
1964
  if (patch.labels !== undefined) next.labels = patch.labels;
1965
+ if (patch.model !== undefined) next.model = patch.model;
1886
1966
  return this.projects.update(id, next);
1887
1967
  }
1888
1968
 
@@ -2049,6 +2129,8 @@ export class Store {
2049
2129
 
2050
2130
  async close() {
2051
2131
  this.stopScheduler();
2132
+ if (this.awakeTimer) clearTimeout(this.awakeTimer);
2133
+ this.awake.stop();
2052
2134
  for (const { harness, unlock, unsubscribe } of this.opened.values()) {
2053
2135
  unsubscribe();
2054
2136
  try {