@moo.md/hum 0.2.0 → 0.3.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.
@@ -8,7 +8,8 @@ import { hashJson } from "../config/hash.ts";
8
8
  import { Json } from "../config/json.ts";
9
9
  import { migrateConfig } from "../config/migrate.ts";
10
10
  import { noGitIdentity, type Refusal, type Result } from "../config/refusal.ts";
11
- import { Kind, READABLE_SCHEMA_VERSIONS, SCHEMA_VERSION } from "../config/schema.ts";
11
+ import { type HumConfig, Kind, READABLE_SCHEMA_VERSIONS, SCHEMA_VERSION } from "../config/schema.ts";
12
+ import { isGitHubHosted } from "../config/wake-job.ts";
12
13
  import { ciWorkflows } from "../service/workflows.ts";
13
14
  import { renderUnit, type UnitKind } from "../service/unit.ts";
14
15
  import { atomicWrite } from "../state/fs.ts";
@@ -28,6 +29,8 @@ export const SetupAnswers = z.strictObject({
28
29
  waitHours: z.record(z.string(), z.number().positive()).default({}),
29
30
  /** The schema version the writing skill produces; a newer one than this engine reads is refused. */
30
31
  schemaVersion: z.int().optional(),
32
+ /** Set only after the owner said yes: CI wakes may run on a runner GitHub does not host although anyone may comment on the repo. */
33
+ ownRunnerOnPublicRepo: z.literal(true).optional(),
31
34
  });
32
35
 
33
36
  export type SetupAnswers = z.infer<typeof SetupAnswers>;
@@ -41,26 +44,43 @@ export type SetupEnv = {
41
44
  readonly runtime?: { readonly bun: string; readonly path: string };
42
45
  /** The owner's name and email as git config holds them: the identity hum.json takes unless it names one already. */
43
46
  readonly gitIdentity: GitIdentityRead;
47
+ /** Whether anyone may read and comment on the repo, as GitHub says; left out where nothing could say. */
48
+ readonly isPublicRepo?: boolean;
44
49
  };
45
50
 
46
51
  export type SetupResult = { readonly ok: true; readonly written: readonly string[]; readonly config: Config } | { readonly ok: false; readonly refusal: Refusal };
47
52
 
48
53
  const UPDATE_ENGINE = "Update the engine: bun add --exact @moo.md/hum@latest.";
49
54
 
50
- /** Refuses a schema the installed engine does not read, whether the existing file has it or the writer would produce it. */
51
- const schemaRefusal = (existing: Json | undefined, target: number): Refusal | undefined => {
52
- if (!READABLE_SCHEMA_VERSIONS.includes(target)) {
53
- return {
54
- code: "unknown-schema-version",
55
- found: target,
56
- readable: READABLE_SCHEMA_VERSIONS,
57
- message: `Setup would write hum.json schemaVersion ${target}, and this engine reads ${READABLE_SCHEMA_VERSIONS.join(", ")}. ${UPDATE_ENGINE} Nothing was written.`,
58
- };
59
- }
55
+ /** Refuses answers written for a schema the installed engine does not read. */
56
+ const targetRefusal = (target: number): Refusal | undefined =>
57
+ READABLE_SCHEMA_VERSIONS.includes(target)
58
+ ? undefined
59
+ : {
60
+ code: "unknown-schema-version",
61
+ found: target,
62
+ readable: READABLE_SCHEMA_VERSIONS,
63
+ message: `Setup would write hum.json schemaVersion ${target}, and this engine reads ${READABLE_SCHEMA_VERSIONS.join(", ")}. ${UPDATE_ENGINE} Nothing was written.`,
64
+ };
60
65
 
61
- const migrated = existing === undefined ? undefined : migrateConfig(existing);
62
-
63
- return migrated && !migrated.ok ? { ...migrated.refusal, message: `${migrated.refusal.message} Nothing was written.` } : undefined;
66
+ /**
67
+ * A wake runs an agent that holds the owner's secrets. On a runner GitHub does not host, what a wake leaves behind
68
+ * stays on the machine, and on a public repo more people reach what starts a wake. So the owner says yes to the two
69
+ * together, and a repo whose visibility nothing could read counts as public.
70
+ */
71
+ const ownRunnerRefusal = ({ runPlaces, ciRunner }: HumConfig, answers: SetupAnswers, isPublicRepo: boolean | undefined): Refusal | undefined =>
72
+ !runPlaces.includes("ci") || isGitHubHosted(ciRunner) || isPublicRepo === false || answers.ownRunnerOnPublicRepo === true
73
+ ? undefined
74
+ : {
75
+ code: "own-runner-on-public-repo",
76
+ message: `ciRunner names a runner GitHub does not host, and this repo ${isPublicRepo ? "is public" : "may be public: gh could not say"}. A wake there runs an agent with your secrets on a machine that keeps what the wake leaves. Name a runner GitHub hosts, or ask the owner and set "ownRunnerOnPublicRepo": true in the answers. Nothing was written.`,
77
+ };
78
+
79
+ /** Settings migrated to the schema this engine writes, so the repo's file and the answers merge at one version; refused at a schema it does not read. */
80
+ const atCurrentSchema = (hum: Json): Result<Readonly<Record<string, Json>>> => {
81
+ const migrated = migrateConfig(hum);
82
+
83
+ return migrated.ok ? migrated : { ok: false, refusal: { ...migrated.refusal, message: `${migrated.refusal.message} Nothing was written.` } };
64
84
  };
65
85
 
66
86
  /** The identity hum.json already holds, else the one git config holds, else the refusal that says what to set. */
@@ -99,14 +119,23 @@ const asJson = (value: Kind | Readonly<Record<string, Json>>): Json => Json.pars
99
119
  */
100
120
  export const writeSetup = (repo: string, answers: SetupAnswers, env: SetupEnv): SetupResult => {
101
121
  const existing = readExisting(repo);
102
- const refused = schemaRefusal(existing?.hum, answers.schemaVersion ?? SCHEMA_VERSION);
122
+ const target = answers.schemaVersion ?? SCHEMA_VERSION;
123
+ const refused = targetRefusal(target);
103
124
 
104
125
  if (refused) return { ok: false, refusal: refused };
105
126
 
106
127
  const shipped = shippedRaw();
107
128
  const base = existing ?? shipped;
129
+ const fromRepo = atCurrentSchema(base.hum);
130
+
131
+ if (!fromRepo.ok) return fromRepo;
132
+
133
+ const answered = atCurrentSchema({ ...answers.hum, schemaVersion: target });
134
+
135
+ if (!answered.ok) return answered;
136
+
108
137
  const shippedHum = HumObject.parse(shipped.hum);
109
- const given = { ...HumObject.parse(base.hum), ...answers.hum };
138
+ const given = { ...fromRepo.value, ...answered.value };
110
139
  const identity = identityOf(given, env.gitIdentity);
111
140
 
112
141
  if (!identity.ok) return identity;
@@ -119,6 +148,10 @@ export const writeSetup = (repo: string, answers: SetupAnswers, env: SetupEnv):
119
148
 
120
149
  if (!loaded.ok) return { ok: false, refusal: loaded.refusal };
121
150
 
151
+ const ownRunner = ownRunnerRefusal(loaded.value.hum, answers, env.isPublicRepo);
152
+
153
+ if (ownRunner) return { ok: false, refusal: ownRunner };
154
+
122
155
  const files = [
123
156
  { path: join(repo, ".hum", "hum.json"), text: jsonText(ownerKeys(hum, shippedHum)) },
124
157
  ...Object.entries(kinds).map(([name, kind]) => ({ path: join(repo, ".hum", "kinds", `${name}.json`), text: jsonText(kind) })),
@@ -164,5 +197,5 @@ const serviceFile = (repo: string, config: Config, env: SetupEnv): FileToWrite =
164
197
  };
165
198
  };
166
199
 
167
- /** The answers file the skill writes: `{ "hum": {...}, "waitHours": {...}, "schemaVersion": 1 }`. */
200
+ /** The answers file the skill writes: `{ "hum": {...}, "waitHours": {...}, "schemaVersion": 2 }`, with `"ownRunnerOnPublicRepo": true` once the owner agreed to it. */
168
201
  export const readAnswers = (file: string): SetupAnswers => SetupAnswers.parse(JSON.parse(readFileSync(file, "utf8")));