iterate.com / stdlib

git clone https://git.iterate.com/repos/stdlib

stdlib / context-descriptions.tsmain at 41005ca

// context-descriptions.ts — WHAT EACH CONTEXT OF THE MIND IS CALLED AND IS FOR: a short title
// and one line. The mind's catalog shows both (`await itx.cd("/").contexts.list()`, `description`
// and `metadata.title`). A context says it with the event
// `events.iterate.com/itx/described` on its own log: the newest one stands, null clears it, and
// anyone who can append there can write it. every-event.ts calls `contextDescriptionsEvent` for
// every event, and this file writes the line from the table below for each context that the
// platform and these repos make. An agent writes its own line (iterate/agents, the catalog on
// /agents), and has its title in the agents' own catalog (`itx.agents.list()`).
//
// It writes at two moments:
//
//   itx/created            a context is born: its line, if a row of the table matches its path
//   mind/worker-updated    every publish of the mind's code: the line of every context the catalog
//                          lists (at most 1,000, the most recently active first), so a context
//                          born before a row was added gets it too
//
// It never writes to a path that has no context: an append there would give birth to one.
//
// Each text lands once on each context: the append's key names the path and the text. So a
// repeated delivery appends nothing, and a description that a person wrote later stands until the
// table's text for that context changes. Then the new text lands once. A title is an append of
// its own (`metadata.title`), with a key of its own, by the same rule.
//
// THE TABLE. A row's path matches a context's path segment by segment. `<name>` matches one
// segment, and the text says the same word where it was. The first row that matches wins, so a
// row for one path comes before a row of its kind. Add a row for each kind of context that your
// own code makes.
import { errorCode, reportIssue } from "iterate/lib";

const CONTEXT_DESCRIPTIONS: { path: string; title: string; description: string }[] = [
  {
    path: "/",
    title: "Mind root",
    description:
      "The mind's root: each publication of the mind's code, where its hosts go, and the facts of the whole mind.",
  },
  {
    path: "/agents",
    title: "Agents",
    description:
      "The list of this mind's agents, which the Dash shows: each context below /agents is an agent.",
  },
  {
    path: "/agents/email",
    title: "Email agents",
    description:
      "The agents of email threads: one for each thread a member started, /agents/email/t<thread>.",
  },
  {
    path: "/repos/config",
    title: "Config repo",
    description:
      "This mind's wiring: worker.ts, the module the mind loads first. Its AGENTS.md says how the repos fit together.",
  },
  {
    path: "/repos/stdlib",
    title: "Stdlib repo",
    description:
      "The agent, and what the wiring calls at each publish and each event. A push to main publishes the mind.",
  },
  {
    path: "/repos/task-board",
    title: "Task board repo",
    description:
      "The Tasks board's files, and nothing else: each markdown file under todo/ is a task. A push changes no code.",
  },
  {
    path: "/repos/kernel",
    title: "Kernel repo",
    description: "The platform this mind's stack was deployed from. A push takes effect at the next deploy.",
  },
  { path: "/repos/<name>", title: "Repo <name>", description: "The git repository <name>." },
  {
    path: "/task-files",
    title: "Task change feeds",
    description:
      "The change feeds of the repos' tasks: one log for each repo, /task-files/<repo> (feed.js of /repos/tasks).",
  },
  {
    path: "/task-files/<repo>",
    title: "Task changes of <repo>",
    description:
      "What changed in the tasks of /repos/<repo>: one tasks/task-changed for each task file that a commit changed. An agent follows a task with itx.config.followTasks (feed.js of /repos/tasks).",
  },
  {
    path: "/docs",
    title: "Open docs",
    description: "The Docs app's open docs: /docs/<repo>/<path>, one for each doc.",
  },
  {
    path: "/docs/<repo>",
    title: "Open docs of <repo>",
    description: "The Docs app's open docs of /repos/<repo>.",
  },
  {
    path: "/secrets",
    title: "Secrets",
    description: "The mind's secrets: each context below /secrets is one secret.",
  },
  {
    path: "/secrets/<name>",
    title: "Secret <name>",
    description:
      "The secret <name>: its value stays in the vault, and this log records when it was set, refreshed and used.",
  },
  {
    path: "/integrations/email",
    title: "Email",
    description: "The mind's email: each mail it received and sent, folded into threads.",
  },
  {
    path: "/approvals",
    title: "Approvals",
    description:
      "The gate for acts that wait for a member's yes: its rules, each held act and what became of it.",
  },
];

/** The title and the description the table gives `path`, or undefined when no row matches. */
export function contextLine(path: string): { title: string; description: string } | undefined {
  const segments = path.split("/").filter(Boolean);
  for (const row of CONTEXT_DESCRIPTIONS) {
    const words = matchRow(row.path.split("/").filter(Boolean), segments);
    if (!words) continue;
    const said = (text: string) =>
      text.replace(/<([a-z]+)>/g, (placeholder, name: string) => words[name] || placeholder);
    return { title: said(row.title), description: said(row.description) };
  }
  return undefined;
}

/** The words of `segments` that a row's placeholders take, or undefined when the row does not
 *  match. */
function matchRow(pattern: string[], segments: string[]): Record<string, string> | undefined {
  if (pattern.length !== segments.length) return undefined;
  const words: Record<string, string> = {};
  for (const [index, part] of pattern.entries()) {
    const placeholder = /^<([a-z]+)>$/.exec(part);
    if (placeholder) words[placeholder[1]!] = segments[index]!;
    else if (part !== segments[index]) return undefined;
  }
  return words;
}

/** An append's key: what is written (`described` for a description, `titled` for a title), the
 *  path and a short hash of the text (FNV-1a), so each text lands once on each context. */
export function descriptionKey(kind: "described" | "titled", path: string, text: string): string {
  let hash = 0x811c9dc5;
  for (const char of text) {
    hash ^= char.codePointAt(0)!;
    hash = Math.imul(hash, 0x01000193) >>> 0;
  }
  return `config/${kind}:${path}:${hash.toString(36)}`;
}

/** The part of the mind's root that this file uses. */
type DescriptionsItx = {
  cd(path: string): {
    append(event: {
      type: "events.iterate.com/itx/described";
      idempotencyKey: string;
      payload: { description: string } | { metadata: Record<string, unknown> };
    }): Promise<unknown>;
  };
  contexts: {
    list(options: { limit: number; under?: string }): Promise<
      {
        path: string;
        description?: string;
        metadata?: Record<string, unknown>;
        eventCount?: number;
      }[]
    >;
  };
};

/** A context's title and line, from the table: at its birth, and for every listed context at each
 *  publish. Any other event returns at once and makes no call. */
export async function contextDescriptionsEvent(
  itx: DescriptionsItx,
  event: { type: string; path: string },
): Promise<void> {
  if (event.type === "events.iterate.com/itx/created") {
    const line = contextLine(event.path);
    if (!line) return;
    await describe(itx, event.path, line.description);
    // The event says that the context was born, not what its metadata holds by now: a caller can
    // write metadata before this runs. So the title goes in beside what the catalog lists for
    // the context. That must be a stored row: the catalog also lists an awake context that has
    // sent no summary yet, as a placeholder with `eventCount: 0` and no metadata, which says
    // nothing of what the context holds. With no stored row, or a read that fails, nothing is
    // known and no title is written here: the next publish writes it, from the row it lists.
    const listed = await itx.contexts
      .list({ under: event.path, limit: 50 })
      .then((rows) => rows.find((row) => row.path === event.path))
      .catch(() => undefined);
    if (listed?.eventCount && listed.metadata?.title !== line.title)
      await title(itx, event.path, line.title, listed.metadata);
    return;
  }
  if (event.type !== "events.iterate.com/mind/worker-updated" || event.path !== "/") return;
  const contexts = await itx.contexts.list({ limit: 1_000 });
  // Each context by itself: a row of the table that is wrong (a row with no title, say) or an
  // append that fails skips that one context, and the publish goes on for the others.
  const faults: { path: string; error: unknown }[] = [];
  // A context that says this already needs no call. One that says something else gets the text
  // once: when a person wrote theirs after it, the key finds the text and adds nothing.
  const writes = contexts.flatMap((context): { path: string; write: () => Promise<void> }[] => {
    try {
      const line = contextLine(context.path);
      if (!line) return [];
      return [
        ...(context.description !== line.description
          ? [{ path: context.path, write: () => describe(itx, context.path, line.description) }]
          : []),
        ...(context.metadata?.title !== line.title
          ? [
              {
                path: context.path,
                write: () => title(itx, context.path, line.title, context.metadata),
              },
            ]
          : []),
      ];
    } catch (error) {
      faults.push({ path: context.path, error });
      return [];
    }
  });
  // a few at a time: each append wakes its context, and a publish may find hundreds to write
  for (let start = 0; start < writes.length; start += WRITES_AT_ONCE)
    await Promise.all(
      writes.slice(start, start + WRITES_AT_ONCE).map(({ path, write }) =>
        write().catch((error: unknown) => {
          faults.push({ path, error });
        }),
      ),
    );
  // once for the publish, with the first fault and how many contexts it left as they were
  if (faults[0])
    reportIssue("config.context-descriptions.publish", faults[0].error, {
      path: faults[0].path,
      contexts: new Set(faults.map((fault) => fault.path)).size,
    });
}

/** How many contexts one publish writes to at the same time. */
const WRITES_AT_ONCE = 8;

async function describe(itx: DescriptionsItx, path: string, description: string) {
  await once(
    itx.cd(path).append({
      type: "events.iterate.com/itx/described",
      idempotencyKey: descriptionKey("described", path, description),
      payload: { description },
    }),
  );
}

/** The longest metadata the mind's catalog keeps, as JSON (core/itx/src/context/context-summary.ts,
 *  `METADATA_MAX_CHARS`): it drops a longer record whole. */
const METADATA_MAX_CHARS = 4_000;

/** The contexts whose title could not be written, each reported once while this instance lives. */
const titleFaults = new Set<string>();

/** Writes a context's title as `metadata.title`, and leaves its description as it stands. The
 *  metadata of `itx/described` is one record and the newest stands, so the title goes in beside
 *  what the context's metadata already holds (`metadata`, as the catalog lists it). A record that
 *  the catalog would drop for its length is never written: the title would be lost with all the
 *  rest, and the append would have used its key. That context keeps what it has, and says so once. */
async function title(
  itx: DescriptionsItx,
  path: string,
  text: string,
  metadata: Record<string, unknown> | undefined,
) {
  const merged = { ...metadata, title: text };
  if (JSON.stringify(merged).length > METADATA_MAX_CHARS) {
    if (titleFaults.has(path)) return;
    titleFaults.add(path);
    reportIssue(
      "config.context-descriptions.titled",
      new Error("The context's metadata is too long to take a title."),
      { path },
    );
    return;
  }
  await once(
    itx.cd(path).append({
      type: "events.iterate.com/itx/described",
      idempotencyKey: descriptionKey("titled", path, text),
      payload: { metadata: merged },
    }),
  );
}

/** An append whose key already landed is done. */
async function once(append: Promise<unknown>) {
  await append.catch((error: unknown) => {
    if (errorCode(error) !== "IDEMPOTENCY_CONFLICT") throw error;
  });
}