@operad-io/engine 0.1.0 → 0.2.0

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 (80) hide show
  1. package/config.schema.json +2 -1
  2. package/package.json +1 -1
  3. package/src/config/config.ts +74 -45
  4. package/src/flow/types.ts +4 -0
  5. package/src/flow/validate.ts +5 -0
  6. package/src/http/index.ts +2 -0
  7. package/src/http/plugins.ts +66 -0
  8. package/src/http/server.ts +342 -48
  9. package/src/kernel/decide.ts +115 -30
  10. package/src/kernel/state.ts +9 -0
  11. package/src/kernel/types.ts +25 -40
  12. package/src/log/open-log.ts +2 -5
  13. package/src/log/sqlite.ts +127 -85
  14. package/src/log/types.ts +35 -9
  15. package/src/loop/code-watch.ts +108 -2
  16. package/src/loop/older-code.ts +3 -4
  17. package/src/loop/serve.ts +122 -95
  18. package/src/notify/notify.ts +2 -6
  19. package/src/platform/atomic-write.ts +163 -0
  20. package/src/platform/file-lock.ts +43 -21
  21. package/src/platform/index.ts +2 -0
  22. package/src/platform/kept-workers.ts +290 -0
  23. package/src/platform/process-group.ts +11 -3
  24. package/src/platform/sqlite-open.ts +20 -6
  25. package/src/platform/sqlite-schema.ts +30 -0
  26. package/src/platform/step-reap.ts +33 -18
  27. package/src/platform/watch.ts +1 -11
  28. package/src/plugin/action-schema.ts +59 -1
  29. package/src/plugin/choice-info.ts +133 -0
  30. package/src/plugin/context.ts +279 -0
  31. package/src/plugin/declared-flows.ts +3 -10
  32. package/src/plugin/errors.ts +9 -0
  33. package/src/plugin/freeze.ts +12 -0
  34. package/src/plugin/harness.ts +28 -0
  35. package/src/plugin/index.ts +8 -7
  36. package/src/plugin/install.ts +265 -17
  37. package/src/plugin/keys.ts +2 -12
  38. package/src/plugin/load.ts +47 -23
  39. package/src/plugin/logger.ts +14 -0
  40. package/src/plugin/open-store.ts +0 -6
  41. package/src/plugin/question-form.ts +323 -0
  42. package/src/plugin/registry.ts +512 -76
  43. package/src/plugin/secrets.ts +14 -24
  44. package/src/plugin/semver.ts +52 -0
  45. package/src/plugin/settings.ts +1 -9
  46. package/src/plugin/sqlite-store.ts +94 -64
  47. package/src/plugin/store.ts +5 -0
  48. package/src/plugin/types.ts +566 -21
  49. package/src/plugin/validate.ts +396 -26
  50. package/src/runner/answer.ts +26 -68
  51. package/src/runner/ask.ts +227 -28
  52. package/src/runner/cancel.ts +10 -7
  53. package/src/runner/grants.ts +2 -2
  54. package/src/runner/index.ts +1 -3
  55. package/src/runner/limits.ts +2 -2
  56. package/src/runner/runner.ts +15 -14
  57. package/src/runner/service.ts +52 -19
  58. package/src/runner/step-worker.ts +9 -0
  59. package/src/testing/answer.ts +69 -0
  60. package/src/testing/clock-manual.ts +152 -0
  61. package/src/testing/config.ts +16 -0
  62. package/src/testing/fs.ts +172 -0
  63. package/src/testing/home.ts +43 -0
  64. package/src/testing/index.ts +19 -0
  65. package/src/testing/types.ts +2 -0
  66. package/src/testing/world.ts +9 -2
  67. package/src/trigger/kernel.ts +13 -46
  68. package/src/trigger/rules.ts +22 -12
  69. package/src/trigger/types.ts +2 -6
  70. package/src/util.ts +99 -0
  71. package/src/views/event-line.ts +22 -0
  72. package/src/views/history.ts +36 -19
  73. package/src/views/live-logs.ts +313 -0
  74. package/src/views/pending.ts +23 -5
  75. package/src/views/presentation.ts +629 -81
  76. package/src/views/running.ts +5 -5
  77. package/src/views/settings.ts +205 -66
  78. package/src/views/types.ts +3 -2
  79. package/src/views/uploads.ts +15 -7
  80. package/triggers.schema.json +4 -0
@@ -3,6 +3,7 @@
3
3
  * normalization of the fields a plugin declares, and the rules that span all
4
4
  * plugins of a set.
5
5
  */
6
+ import { isAbsolute } from "node:path";
6
7
  import { ALL_FILTER } from "./filters.ts";
7
8
  import type { FlowDefinition, StepType } from "../flow/types.ts";
8
9
  import type { JsonValue } from "../log/types.ts";
@@ -19,7 +20,12 @@ import {
19
20
  type ActionTypeDefinition,
20
21
  type AskRequest,
21
22
  type CommandDefinition,
23
+ type HarnessCapabilities,
22
24
  type HarnessEfforts,
25
+ type HarnessEffortsSource,
26
+ type HarnessModelCapability,
27
+ type HarnessModelContext,
28
+ type HarnessModelInfo,
23
29
  type HarnessModels,
24
30
  type KeyBindings,
25
31
  type MaybePromise,
@@ -27,9 +33,11 @@ import {
27
33
  type ObjectTypeDefinition,
28
34
  type ObjectViews,
29
35
  type PluginBaseContext,
36
+ type PluginCheckReturn,
30
37
  type PluginKind,
31
38
  type PluginRequires,
32
39
  type ProjectSettingsDeclaration,
40
+ type ProjectSettingsFile,
33
41
  type SourceContext,
34
42
  type SourceHandle,
35
43
  type StandingContext,
@@ -38,7 +46,7 @@ import {
38
46
  type StepTypeDefinition,
39
47
  type TickContext,
40
48
  } from "./types.ts";
41
- import { isRecord } from "../util.ts";
49
+ import { isRecord, parseObjectId } from "../util.ts";
42
50
 
43
51
  export interface NormalizedPlugin {
44
52
  readonly name: string;
@@ -60,8 +68,9 @@ export interface NormalizedPlugin {
60
68
  | ((ctx: SourceContext) => MaybePromise<SourceHandle> | SourceHandle)
61
69
  | undefined;
62
70
  readonly check:
63
- | ((ctx: PluginBaseContext) => MaybePromise<string | void>)
71
+ | ((ctx: PluginBaseContext) => MaybePromise<PluginCheckReturn>)
64
72
  | undefined;
73
+ readonly guide: string | undefined;
65
74
  readonly settingsSchema: ActionArgsSchema | undefined;
66
75
  /** The plugin's objects belong under a client's settings view. */
67
76
  readonly objectsUnderSettings: boolean;
@@ -69,9 +78,22 @@ export interface NormalizedPlugin {
69
78
  readonly enforces: readonly string[];
70
79
  readonly models:
71
80
  | HarnessModels
72
- | ((settings: Readonly<Record<string, JsonValue>>) => HarnessModels)
81
+ | ((
82
+ settings: Readonly<Record<string, JsonValue>>,
83
+ context: HarnessModelContext,
84
+ ) => HarnessModels)
85
+ | undefined;
86
+ readonly modelsReady:
87
+ | ((
88
+ settings: Readonly<Record<string, JsonValue>>,
89
+ context: HarnessModelContext,
90
+ ) => MaybePromise<HarnessModels>)
91
+ | undefined;
92
+ readonly efforts: HarnessEffortsSource | undefined;
93
+ readonly capabilities: HarnessCapabilities;
94
+ readonly signedIn:
95
+ | ((settings: Readonly<Record<string, JsonValue>>) => MaybePromise<boolean | undefined>)
73
96
  | undefined;
74
- readonly efforts: HarnessEfforts | undefined;
75
97
  readonly requires: PluginRequires | undefined;
76
98
  }
77
99
 
@@ -122,11 +144,7 @@ export function narrowerOf(...values: readonly unknown[]): number | undefined {
122
144
  }
123
145
 
124
146
  export function parseObjectKeyMaybe(key: string): { readonly type: string; readonly id: string } {
125
- const colon = key.indexOf(":");
126
- if (colon <= 0 || colon === key.length - 1) {
127
- return { type: "object", id: key };
128
- }
129
- return { type: key.slice(0, colon), id: key.slice(colon + 1) };
147
+ return parseObjectId(key) ?? { type: "object", id: key };
130
148
  }
131
149
 
132
150
  function isPluginKind(value: unknown): value is PluginKind {
@@ -170,6 +188,18 @@ function normalizeViews(
170
188
  if (raw.showScope !== undefined && typeof raw.showScope !== "function") {
171
189
  throw new PluginError(`${at}.${type}: showScope: must be a function`);
172
190
  }
191
+ if (raw.context !== undefined && typeof raw.context !== "function") {
192
+ throw new PluginError(`${at}.${type}: context: must be a function`);
193
+ }
194
+ if (raw.skills !== undefined && typeof raw.skills !== "function") {
195
+ throw new PluginError(`${at}.${type}: skills: must be a function`);
196
+ }
197
+ if (raw.file !== undefined && typeof raw.file !== "function") {
198
+ throw new PluginError(`${at}.${type}: file: must be a function`);
199
+ }
200
+ if (raw.tail !== undefined && typeof raw.tail !== "function") {
201
+ throw new PluginError(`${at}.${type}: tail: must be a function`);
202
+ }
173
203
  views[type] = {
174
204
  row: raw.row as ObjectViews["row"],
175
205
  sections: raw.sections as ObjectViews["sections"],
@@ -177,6 +207,18 @@ function normalizeViews(
177
207
  ...(raw.showScope === undefined
178
208
  ? {}
179
209
  : { showScope: raw.showScope as NonNullable<ObjectViews["showScope"]> }),
210
+ ...(raw.context === undefined
211
+ ? {}
212
+ : { context: raw.context as NonNullable<ObjectViews["context"]> }),
213
+ ...(raw.skills === undefined
214
+ ? {}
215
+ : { skills: raw.skills as NonNullable<ObjectViews["skills"]> }),
216
+ ...(raw.file === undefined
217
+ ? {}
218
+ : { file: raw.file as NonNullable<ObjectViews["file"]> }),
219
+ ...(raw.tail === undefined
220
+ ? {}
221
+ : { tail: raw.tail as NonNullable<ObjectViews["tail"]> }),
180
222
  };
181
223
  }
182
224
  return views;
@@ -221,7 +263,22 @@ function normalizeProjectSettings(
221
263
  throw new PluginError(messageOf(error), { cause: error });
222
264
  }
223
265
  if (schema === undefined) throw new PluginError(`${where}: schema: is required`);
224
- return { name, path, schema };
266
+ for (const hook of ["check", "derived"]) {
267
+ if (item[hook] !== undefined && typeof item[hook] !== "function") {
268
+ throw new PluginError(`${where}: ${hook}: must be a function`);
269
+ }
270
+ }
271
+ return {
272
+ name,
273
+ path,
274
+ schema,
275
+ ...(item.check === undefined
276
+ ? {}
277
+ : { check: item.check as ProjectSettingsFile["check"] }),
278
+ ...(item.derived === undefined
279
+ ? {}
280
+ : { derived: item.derived as ProjectSettingsFile["derived"] }),
281
+ };
225
282
  });
226
283
  return {
227
284
  repositories: value.repositories as ProjectSettingsDeclaration["repositories"],
@@ -570,11 +627,16 @@ function normalizeActions(
570
627
  throw new PluginError(`${at}[${index}]: must be an object`);
571
628
  }
572
629
  const name = requireNameText(entry.name, `${at}[${index}]: name`);
573
- const on = requireNameText(entry.on, `${at}[${index}]: on`);
574
- const key = `${name}\u0000${on}`;
630
+ const on =
631
+ entry.on === undefined
632
+ ? undefined
633
+ : requireNameText(entry.on, `${at}[${index}]: on`);
634
+ const key = `${name}\u0000${on ?? ""}`;
575
635
  if (names.has(key)) {
576
636
  throw new PluginError(
577
- `${at}: duplicate action "${name}" on object type "${on}"`,
637
+ on === undefined
638
+ ? `${at}: duplicate action "${name}" that targets no object`
639
+ : `${at}: duplicate action "${name}" on object type "${on}"`,
578
640
  );
579
641
  }
580
642
  names.add(key);
@@ -604,6 +666,13 @@ function normalizeActions(
604
666
  if (confirm !== undefined && typeof confirm !== "boolean") {
605
667
  throw new PluginError(`${at}[${index}]: confirm: must be a boolean`);
606
668
  }
669
+ if (
670
+ entry.command !== undefined &&
671
+ typeof entry.command !== "boolean" &&
672
+ (typeof entry.command !== "string" || entry.command.trim().length === 0)
673
+ ) {
674
+ throw new PluginError(`${at}[${index}]: command: must be a boolean or a non-empty string`);
675
+ }
607
676
  if (
608
677
  entry.creates !== undefined &&
609
678
  (typeof entry.creates !== "string" || entry.creates.length === 0)
@@ -612,10 +681,13 @@ function normalizeActions(
612
681
  }
613
682
  actions.push({
614
683
  name,
615
- on,
684
+ ...(on === undefined ? {} : { on }),
616
685
  ...(typeof entry.creates === "string" ? { creates: entry.creates } : {}),
617
686
  ...(args === undefined ? {} : { args }),
618
687
  ...(confirm === undefined ? {} : { confirm }),
688
+ ...(typeof entry.command === "string" || typeof entry.command === "boolean"
689
+ ? { command: entry.command }
690
+ : {}),
619
691
  ...(entry.ends === true ? { ends: true } : {}),
620
692
  when: entry.when as ActionTypeDefinition["when"],
621
693
  ...(typeof entry.rank === "function"
@@ -706,15 +778,22 @@ export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
706
778
  }
707
779
 
708
780
  let check:
709
- | ((ctx: PluginBaseContext) => MaybePromise<string | void>)
781
+ | ((ctx: PluginBaseContext) => MaybePromise<PluginCheckReturn>)
710
782
  | undefined;
783
+ let guide: string | undefined;
784
+ if (raw.guide !== undefined) {
785
+ if (typeof raw.guide !== "string" || !isAbsolute(raw.guide)) {
786
+ throw new PluginError(`${at}: guide: must be the absolute path of a SKILL.md`);
787
+ }
788
+ guide = raw.guide;
789
+ }
711
790
  if (raw.check !== undefined) {
712
791
  if (typeof raw.check !== "function") {
713
792
  throw new PluginError(`${at}: check: must be a function`);
714
793
  }
715
794
  check = raw.check as (
716
795
  ctx: PluginBaseContext,
717
- ) => MaybePromise<string | void>;
796
+ ) => MaybePromise<PluginCheckReturn>;
718
797
  }
719
798
 
720
799
  let start: NormalizedPlugin["start"];
@@ -766,12 +845,16 @@ export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
766
845
  standing,
767
846
  start,
768
847
  check,
848
+ guide,
769
849
  settingsSchema,
770
850
  objectsUnderSettings: raw.objectsUnderSettings === true,
771
851
  projectSettings,
772
852
  enforces: normalizeEnforces(raw.enforces, `${at}: enforces`) ?? [],
773
853
  models: normalizeModels(raw.models, kind, at),
854
+ modelsReady: normalizeModelsReady(raw.modelsReady, kind, at),
774
855
  efforts: normalizeEfforts(raw.efforts, kind, at),
856
+ capabilities: normalizeCapabilities(raw.capabilities, kind, at),
857
+ signedIn: normalizeSignedIn(raw.signedIn, kind, at),
775
858
  requires: normalizeRequires(raw.requires, at),
776
859
  };
777
860
  }
@@ -781,24 +864,224 @@ function normalizeModels(
781
864
  value: unknown,
782
865
  kind: string,
783
866
  at: string,
784
- ): HarnessModels | ((settings: Readonly<Record<string, JsonValue>>) => HarnessModels) | undefined {
867
+ ): HarnessModels | ((settings: Readonly<Record<string, JsonValue>>, context: HarnessModelContext) => HarnessModels) | undefined {
785
868
  if (typeof value === "function") {
786
869
  if (kind !== "harness") throw new PluginError(`${at}: models: only a harness plugin declares models`);
787
- const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
788
- return (settings) => {
789
- const levels = normalizeLevels(source(settings), kind, at, "models");
790
- return levels === undefined ? { list: [], default: null } : { list: levels.list, default: levels.fallback };
870
+ const source = value as (
871
+ settings: Readonly<Record<string, JsonValue>>,
872
+ context: HarnessModelContext,
873
+ ) => unknown;
874
+ return (settings, context) => {
875
+ const raw = source(settings, context);
876
+ const levels = normalizeModelLevels(raw, kind, at);
877
+ return levels === undefined ? { list: [], default: null } : levels;
791
878
  };
792
879
  }
793
- const levels = normalizeLevels(value, kind, at, "models");
794
- return levels === undefined ? undefined : { list: levels.list, default: levels.fallback };
880
+ return normalizeModelLevels(value, kind, at);
881
+ }
882
+
883
+ /** A harness plugin's `modelsReady` hook: a list, possibly after waiting for one refresh. */
884
+ function normalizeModelsReady(
885
+ value: unknown,
886
+ kind: string,
887
+ at: string,
888
+ ): ((settings: Readonly<Record<string, JsonValue>>, context: HarnessModelContext) => MaybePromise<HarnessModels>) | undefined {
889
+ if (value === undefined) return undefined;
890
+ if (kind !== "harness") throw new PluginError(`${at}: modelsReady: only a harness plugin declares modelsReady`);
891
+ if (typeof value !== "function") throw new PluginError(`${at}: modelsReady: must be a function`);
892
+ const source = value as (
893
+ settings: Readonly<Record<string, JsonValue>>,
894
+ context: HarnessModelContext,
895
+ ) => unknown;
896
+ return async (settings, context) => {
897
+ const raw = await source(settings, context);
898
+ const levels = normalizeModelLevels(raw, kind, at);
899
+ return levels === undefined ? { list: [], default: null } : levels;
900
+ };
901
+ }
902
+
903
+ /** The capabilities a model declares, as the names a client shows. */
904
+ const MODEL_CAPABILITIES = ["vision", "reasoning", "tools", "web"] as const;
905
+
906
+ function normalizeModelCapabilities(value: unknown, at: string): readonly HarnessModelCapability[] | undefined {
907
+ if (value === undefined) return undefined;
908
+ const found: HarnessModelCapability[] = [];
909
+ if (Array.isArray(value)) {
910
+ for (const item of value) {
911
+ if (typeof item !== "string" || !(MODEL_CAPABILITIES as readonly string[]).includes(item)) {
912
+ throw new PluginError(`${at}: must be one of ${MODEL_CAPABILITIES.join(", ")}`);
913
+ }
914
+ found.push(item as HarnessModelCapability);
915
+ }
916
+ } else if (isRecord(value)) {
917
+ for (const capability of MODEL_CAPABILITIES) {
918
+ const on = value[capability];
919
+ if (on !== undefined && typeof on !== "boolean") {
920
+ throw new PluginError(`${at}.${capability}: must be a boolean`);
921
+ }
922
+ if (on === true) found.push(capability);
923
+ }
924
+ for (const key of Object.keys(value)) {
925
+ if (!(MODEL_CAPABILITIES as readonly string[]).includes(key)) {
926
+ throw new PluginError(`${at}.${key}: not a model capability`);
927
+ }
928
+ }
929
+ } else {
930
+ throw new PluginError(`${at}: must be an array of capabilities or an object of booleans`);
931
+ }
932
+ return found.length === 0 ? undefined : found;
933
+ }
934
+
935
+ /** One model's description, as the harness declares it. */
936
+ function normalizeModelInfo(value: unknown, at: string, id: string): HarnessModelInfo {
937
+ if (!isRecord(value)) throw new PluginError(`${at}: must be an object`);
938
+ const declared = value.id;
939
+ if (declared !== undefined && (typeof declared !== "string" || declared.length === 0)) {
940
+ throw new PluginError(`${at}.id: must be a non-empty string`);
941
+ }
942
+ if (declared !== undefined && declared !== id) {
943
+ throw new PluginError(`${at}.id: must be "${id}"`);
944
+ }
945
+ const known = new Set(["id", "name", "provider", "window", "capabilities", ...MODEL_CAPABILITIES]);
946
+ for (const key of Object.keys(value)) {
947
+ if (!known.has(key)) throw new PluginError(`${at}.${key}: not a model field`);
948
+ }
949
+ const name = value.name;
950
+ if (name !== undefined && (typeof name !== "string" || name.length === 0)) {
951
+ throw new PluginError(`${at}.name: must be a non-empty string`);
952
+ }
953
+ const provider = value.provider;
954
+ if (provider !== undefined && (typeof provider !== "string" || provider.length === 0)) {
955
+ throw new PluginError(`${at}.provider: must be a non-empty string`);
956
+ }
957
+ const window = value.window;
958
+ if (window !== undefined && (typeof window !== "number" || !Number.isInteger(window) || window <= 0)) {
959
+ throw new PluginError(`${at}.window: must be a positive whole number`);
960
+ }
961
+ const found = new Set<HarnessModelCapability>();
962
+ for (const capability of MODEL_CAPABILITIES) {
963
+ const on = value[capability];
964
+ if (on === true) found.add(capability);
965
+ else if (on !== undefined && typeof on !== "boolean") {
966
+ throw new PluginError(`${at}.${capability}: must be a boolean`);
967
+ }
968
+ }
969
+ for (const capability of normalizeModelCapabilities(value.capabilities, `${at}.capabilities`) ?? []) {
970
+ found.add(capability);
971
+ }
972
+ const capabilities = MODEL_CAPABILITIES.filter((capability) => found.has(capability));
973
+ return {
974
+ id,
975
+ ...(typeof name === "string" ? { name } : {}),
976
+ ...(typeof provider === "string" ? { provider } : {}),
977
+ ...(typeof window === "number" ? { window } : {}),
978
+ ...(capabilities.length === 0 ? {} : { capabilities }),
979
+ };
980
+ }
981
+
982
+ /** The descriptions a harness gives beside its list, by model id. */
983
+ function normalizeModelInfoMap(
984
+ value: unknown,
985
+ list: readonly string[],
986
+ at: string,
987
+ ): Readonly<Record<string, HarnessModelInfo>> | undefined {
988
+ if (value === undefined) return undefined;
989
+ const info: Record<string, HarnessModelInfo> = {};
990
+ const add = (raw: unknown, id: string, where: string): void => {
991
+ if (!list.includes(id)) {
992
+ throw new PluginError(`${where}: must be one of the models in the list`);
993
+ }
994
+ info[id] = normalizeModelInfo(raw, where, id);
995
+ };
996
+ if (Array.isArray(value)) {
997
+ value.forEach((item, index) => {
998
+ if (!isRecord(item) || typeof item.id !== "string" || item.id.length === 0) {
999
+ throw new PluginError(`${at}: models.info[${index}].id: must be a non-empty string`);
1000
+ }
1001
+ add(item, item.id, `${at}: models.info[${index}]`);
1002
+ });
1003
+ } else if (isRecord(value)) {
1004
+ for (const [id, item] of Object.entries(value)) {
1005
+ add(item, id, `${at}: models.info.${id}`);
1006
+ }
1007
+ } else {
1008
+ throw new PluginError(`${at}: models.info: must be an object or an array`);
1009
+ }
1010
+ return Object.keys(info).length === 0 ? undefined : info;
1011
+ }
1012
+
1013
+ /** The list, fallback and descriptions of a harness plugin's `models`. */
1014
+ function normalizeModelLevels(
1015
+ value: unknown,
1016
+ kind: string,
1017
+ at: string,
1018
+ ): HarnessModels | undefined {
1019
+ if (value === undefined) return undefined;
1020
+ if (kind !== "harness") throw new PluginError(`${at}: models: only a harness plugin declares models`);
1021
+ if (!isRecord(value) || !Array.isArray(value.list)) throw new PluginError(`${at}: models: must be { list, default }`);
1022
+ const list: string[] = [];
1023
+ const described: HarnessModelInfo[] = [];
1024
+ value.list.forEach((item, index) => {
1025
+ if (typeof item === "string" && item.length > 0) {
1026
+ list.push(item);
1027
+ return;
1028
+ }
1029
+ if (isRecord(item)) {
1030
+ const id = item.id;
1031
+ if (typeof id !== "string" || id.length === 0) {
1032
+ throw new PluginError(`${at}: models.list[${index}].id: must be a non-empty string`);
1033
+ }
1034
+ list.push(id);
1035
+ described.push(normalizeModelInfo(item, `${at}: models.list[${index}]`, id));
1036
+ return;
1037
+ }
1038
+ throw new PluginError(`${at}: models.list[${index}]: must be a non-empty string or a model`);
1039
+ });
1040
+ const fallback = value.default === undefined ? null : value.default;
1041
+ if (fallback !== null && (typeof fallback !== "string" || !list.includes(fallback))) {
1042
+ throw new PluginError(`${at}: models.default: must be null or one of the list`);
1043
+ }
1044
+ const fromInfo = normalizeModelInfoMap(value.info, list, at);
1045
+ const info: Record<string, HarnessModelInfo> = { ...(fromInfo ?? {}) };
1046
+ for (const model of described) info[model.id] = model;
1047
+ const declaredWindows = normalizeWindows(value, list, at);
1048
+ const windows: Record<string, number> = { ...(declaredWindows ?? {}) };
1049
+ for (const model of Object.values(info)) {
1050
+ if (model.window !== undefined) windows[model.id] = model.window;
1051
+ }
1052
+ return {
1053
+ list,
1054
+ default: fallback,
1055
+ ...(Object.keys(windows).length === 0 ? {} : { windows }),
1056
+ ...(Object.keys(info).length === 0 ? {} : { info }),
1057
+ };
795
1058
  }
796
1059
 
797
1060
  /**
798
1061
  * The efforts a harness plugin declares: a list of levels, the default among
799
- * them or null, and the step argument that sets one.
1062
+ * them or null, the step argument that sets one, or a function of its settings.
800
1063
  */
801
- function normalizeEfforts(value: unknown, kind: string, at: string): HarnessEfforts | undefined {
1064
+ function normalizeEfforts(
1065
+ value: unknown,
1066
+ kind: string,
1067
+ at: string,
1068
+ ): HarnessEffortsSource | undefined {
1069
+ if (typeof value === "function") {
1070
+ if (kind !== "harness") {
1071
+ throw new PluginError(`${at}: efforts: only a harness plugin declares efforts`);
1072
+ }
1073
+ const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
1074
+ return (settings) =>
1075
+ normalizeEffortsValue(source(settings), "harness", at) ?? { list: [], default: null };
1076
+ }
1077
+ return normalizeEffortsValue(value, kind, at);
1078
+ }
1079
+
1080
+ function normalizeEffortsValue(
1081
+ value: unknown,
1082
+ kind: string,
1083
+ at: string,
1084
+ ): HarnessEfforts | undefined {
802
1085
  const levels = normalizeLevels(value, kind, at, "efforts");
803
1086
  if (levels === undefined) return undefined;
804
1087
  const flag = value === undefined ? undefined : (value as Record<string, unknown>).flag;
@@ -812,6 +1095,70 @@ function normalizeEfforts(value: unknown, kind: string, at: string): HarnessEffo
812
1095
  };
813
1096
  }
814
1097
 
1098
+ /** A harness plugin's declared capabilities: what its agent does itself. */
1099
+ function normalizeCapabilities(value: unknown, kind: string, at: string): HarnessCapabilities {
1100
+ if (value === undefined) return {};
1101
+ if (kind !== "harness") {
1102
+ throw new PluginError(`${at}: capabilities: only a harness plugin declares capabilities`);
1103
+ }
1104
+ if (!isRecord(value)) throw new PluginError(`${at}: capabilities: must be an object`);
1105
+ for (const key of Object.keys(value)) {
1106
+ if (
1107
+ key !== "backgroundModel" &&
1108
+ key !== "loadsSkills" &&
1109
+ key !== "takesMessagesMidRun" &&
1110
+ key !== "accessLevels"
1111
+ ) {
1112
+ throw new PluginError(`${at}: capabilities.${key}: not a capability`);
1113
+ }
1114
+ }
1115
+ const result: {
1116
+ backgroundModel?: string;
1117
+ loadsSkills?: boolean;
1118
+ takesMessagesMidRun?: boolean;
1119
+ accessLevels?: readonly string[];
1120
+ } = {};
1121
+ const backgroundModel = value["backgroundModel"];
1122
+ if (backgroundModel !== undefined) {
1123
+ if (typeof backgroundModel !== "string" || backgroundModel.length === 0) {
1124
+ throw new PluginError(`${at}: capabilities.backgroundModel: must be a non-empty string`);
1125
+ }
1126
+ result.backgroundModel = backgroundModel;
1127
+ }
1128
+ for (const key of ["loadsSkills", "takesMessagesMidRun"] as const) {
1129
+ const capability = value[key];
1130
+ if (capability !== undefined && typeof capability !== "boolean") {
1131
+ throw new PluginError(`${at}: capabilities.${key}: must be a boolean`);
1132
+ }
1133
+ if (capability === true) result[key] = true;
1134
+ }
1135
+ const levels = value["accessLevels"];
1136
+ if (levels !== undefined) {
1137
+ if (!Array.isArray(levels) || !levels.every((level) => typeof level === "string" && level.length > 0)) {
1138
+ throw new PluginError(`${at}: capabilities.accessLevels: must be an array of level ids`);
1139
+ }
1140
+ if (levels.length > 0) result.accessLevels = [...levels];
1141
+ }
1142
+ return result;
1143
+ }
1144
+
1145
+ /** A harness plugin's `signedIn` hook: true, false, or undefined when it cannot tell. */
1146
+ function normalizeSignedIn(
1147
+ value: unknown,
1148
+ kind: string,
1149
+ at: string,
1150
+ ): ((settings: Readonly<Record<string, JsonValue>>) => MaybePromise<boolean | undefined>) | undefined {
1151
+ if (value === undefined) return undefined;
1152
+ if (kind !== "harness") throw new PluginError(`${at}: signedIn: only a harness plugin declares signedIn`);
1153
+ if (typeof value !== "function") throw new PluginError(`${at}: signedIn: must be a function`);
1154
+ const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
1155
+ return async (settings) => {
1156
+ const answer = await source(settings);
1157
+ if (answer === undefined || answer === true || answer === false) return answer;
1158
+ throw new PluginError(`${at}: signedIn: must return true, false or undefined`);
1159
+ };
1160
+ }
1161
+
815
1162
  /** The list and fallback of a harness plugin's `models` or `efforts`. */
816
1163
  function normalizeLevels(
817
1164
  value: unknown,
@@ -833,6 +1180,29 @@ function normalizeLevels(
833
1180
  return { list, fallback };
834
1181
  }
835
1182
 
1183
+ /** The context windows a harness's model list declares, when it declares any. */
1184
+ function normalizeWindows(
1185
+ value: unknown,
1186
+ list: readonly string[],
1187
+ at: string,
1188
+ ): Readonly<Record<string, number>> | undefined {
1189
+ if (!isRecord(value)) return undefined;
1190
+ const raw = value["windows"];
1191
+ if (raw === undefined) return undefined;
1192
+ if (!isRecord(raw)) throw new PluginError(`${at}: models.windows: must be an object`);
1193
+ const windows: Record<string, number> = {};
1194
+ for (const [model, window] of Object.entries(raw)) {
1195
+ if (!list.includes(model)) {
1196
+ throw new PluginError(`${at}: models.windows.${model}: must be one of the models in the list`);
1197
+ }
1198
+ if (typeof window !== "number" || !Number.isInteger(window) || window <= 0) {
1199
+ throw new PluginError(`${at}: models.windows.${model}: must be a positive whole number`);
1200
+ }
1201
+ windows[model] = window;
1202
+ }
1203
+ return Object.keys(windows).length === 0 ? undefined : windows;
1204
+ }
1205
+
836
1206
  /** A harness step type ends only in the standard end results. */
837
1207
  function checkHarnessResults(
838
1208
  steps: readonly StepTypeDefinition[],
@@ -971,7 +1341,7 @@ export function validatePluginSet(plugins: readonly NormalizedPlugin[]): void {
971
1341
 
972
1342
  for (const plugin of plugins) {
973
1343
  for (const action of plugin.actions) {
974
- if (!objects.has(action.on)) {
1344
+ if (action.on !== undefined && !objects.has(action.on)) {
975
1345
  throw new PluginError(
976
1346
  `plugin "${plugin.name}": action "${action.name}": on: unknown object type "${action.on}"`,
977
1347
  );