@caelo-cms/shared 0.12.4 → 0.12.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.
Files changed (51) hide show
  1. package/dist/ai-tools.d.ts +21 -0
  2. package/dist/ai-tools.d.ts.map +1 -1
  3. package/dist/ai-tools.js +11 -0
  4. package/dist/ai-tools.js.map +1 -1
  5. package/dist/change-journal.d.ts +1 -1
  6. package/dist/change-journal.d.ts.map +1 -1
  7. package/dist/change-journal.js +9 -0
  8. package/dist/change-journal.js.map +1 -1
  9. package/dist/document-language.d.ts +11 -0
  10. package/dist/document-language.d.ts.map +1 -1
  11. package/dist/document-language.js +27 -1
  12. package/dist/document-language.js.map +1 -1
  13. package/dist/gateway-refresh.d.ts +52 -0
  14. package/dist/gateway-refresh.d.ts.map +1 -0
  15. package/dist/gateway-refresh.js +55 -0
  16. package/dist/gateway-refresh.js.map +1 -0
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/live-events.d.ts.map +1 -1
  22. package/dist/live-events.js +10 -3
  23. package/dist/live-events.js.map +1 -1
  24. package/dist/redirect-plan.d.ts +45 -5
  25. package/dist/redirect-plan.d.ts.map +1 -1
  26. package/dist/redirect-plan.js +97 -13
  27. package/dist/redirect-plan.js.map +1 -1
  28. package/dist/redirect-release.d.ts +105 -0
  29. package/dist/redirect-release.d.ts.map +1 -0
  30. package/dist/redirect-release.js +104 -0
  31. package/dist/redirect-release.js.map +1 -0
  32. package/dist/untrusted-tool-result.d.ts +13 -0
  33. package/dist/untrusted-tool-result.d.ts.map +1 -1
  34. package/dist/untrusted-tool-result.js +15 -0
  35. package/dist/untrusted-tool-result.js.map +1 -1
  36. package/dist/version.d.ts +2 -2
  37. package/dist/version.js +1 -1
  38. package/package.json +1 -1
  39. package/src/ai-tools.ts +12 -0
  40. package/src/change-journal.ts +14 -0
  41. package/src/document-language.test.ts +35 -0
  42. package/src/document-language.ts +31 -2
  43. package/src/gateway-refresh.ts +64 -0
  44. package/src/index.ts +2 -0
  45. package/src/live-events.ts +10 -3
  46. package/src/redirect-plan.test.ts +62 -0
  47. package/src/redirect-plan.ts +98 -12
  48. package/src/redirect-release.test.ts +167 -0
  49. package/src/redirect-release.ts +162 -0
  50. package/src/untrusted-tool-result.ts +17 -0
  51. package/src/version.ts +1 -1
@@ -0,0 +1,167 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ /**
4
+ * #1550 — which Staged redirect changes a build serves: a change waits for
5
+ * its target, an automatic removal waits for its page, and the live state
6
+ * stays where a change waits.
7
+ */
8
+
9
+ import { describe, expect, it } from "bun:test";
10
+ import type { RedirectTableRow } from "./redirect-plan.js";
11
+ import { redirectPathServed } from "./redirect-plan.js";
12
+ import {
13
+ applyRedirectChanges,
14
+ type PendingRedirectChange,
15
+ planRedirectRelease,
16
+ redirectAddress,
17
+ } from "./redirect-release.js";
18
+
19
+ const row = (fromPath: string, toPath: string, statusCode = 301): RedirectTableRow => ({
20
+ fromPath,
21
+ toPath,
22
+ statusCode,
23
+ });
24
+
25
+ const change = (
26
+ id: string,
27
+ fromPath: string,
28
+ toPath: string | null,
29
+ extra: Partial<PendingRedirectChange> = {},
30
+ ): PendingRedirectChange => ({
31
+ id,
32
+ address: redirectAddress(fromPath),
33
+ fromPath,
34
+ toPath,
35
+ statusCode: toPath === null ? null : 301,
36
+ automatic: false,
37
+ ...extra,
38
+ });
39
+
40
+ const files = (...paths: string[]) => new Set(paths);
41
+ const plan = (
42
+ live: RedirectTableRow[],
43
+ changes: PendingRedirectChange[],
44
+ servedFiles: ReadonlySet<string>,
45
+ ) => planRedirectRelease({ live, changes, servedFiles, pageUrlStyle: "directory" });
46
+
47
+ describe("redirectAddress / redirectPathServed", () => {
48
+ it("one address for both spellings, the root stays the root", () => {
49
+ expect(redirectAddress("/old/")).toBe("/old");
50
+ expect(redirectAddress("/old")).toBe("/old");
51
+ expect(redirectAddress("/")).toBe("/");
52
+ });
53
+
54
+ it("a page's directory index, a bare-slug file and a file answer their path", () => {
55
+ const served = files("index.html", "about/index.html", "terms", "doc.pdf");
56
+ expect(redirectPathServed("/", served)).toBe(true);
57
+ expect(redirectPathServed("/about/", served)).toBe(true);
58
+ expect(redirectPathServed("/about?x=1", served)).toBe(true);
59
+ expect(redirectPathServed("/terms", served)).toBe(true);
60
+ expect(redirectPathServed("/doc.pdf", served)).toBe(true);
61
+ expect(redirectPathServed("/missing", served)).toBe(false);
62
+ });
63
+ });
64
+
65
+ describe("applyRedirectChanges", () => {
66
+ it("replaces both spellings of an address, removes, creates, keeps the rest", () => {
67
+ const live = [row("/a/", "/x"), row("/b", "/y"), row("/c", "/z")];
68
+ const rows = applyRedirectChanges(live, [
69
+ change("1", "/a", "/new"),
70
+ change("2", "/b", null),
71
+ change("3", "/d", "/w"),
72
+ ]);
73
+ expect(rows).toEqual([row("/a", "/new"), row("/c", "/z"), row("/d", "/w")]);
74
+ });
75
+ });
76
+
77
+ describe("planRedirectRelease", () => {
78
+ const served = files("index.html", "about/index.html", "new/index.html");
79
+
80
+ it("applies a change whose target the build serves", () => {
81
+ const r = plan([], [change("1", "/old", "/new")], served);
82
+ expect(r.applied).toEqual(["1"]);
83
+ expect(r.waiting).toEqual([]);
84
+ expect(r.rows).toEqual([row("/old", "/new")]);
85
+ });
86
+
87
+ it("a change to a page the build does not have waits, and the address keeps its live rule", () => {
88
+ const r = plan([row("/old", "/about")], [change("1", "/old", "/draft-page")], served);
89
+ expect(r.applied).toEqual([]);
90
+ expect(r.waiting).toEqual([{ id: "1", reason: "target" }]);
91
+ expect(r.rows).toEqual([row("/old", "/about")]);
92
+ });
93
+
94
+ it("an external target, an /api target and a 410 never wait", () => {
95
+ const r = plan(
96
+ [],
97
+ [
98
+ change("1", "/qr", "https://booking.example.org/x", { statusCode: 302 }),
99
+ change("2", "/hook", "/api/plugin/x"),
100
+ change("3", "/gone", "/410", { statusCode: 410 }),
101
+ ],
102
+ served,
103
+ );
104
+ expect(r.applied.sort()).toEqual(["1", "2", "3"]);
105
+ expect(r.waiting).toEqual([]);
106
+ });
107
+
108
+ it("a target that is itself a served redirect counts as served (chains collapse)", () => {
109
+ const r = plan([row("/mid", "/about")], [change("1", "/old", "/mid")], served);
110
+ expect(r.applied).toEqual(["1"]);
111
+ expect(r.plan.redirects.find((p) => p.fromPath === "/old")?.location).toBe("/about/");
112
+ });
113
+
114
+ it("a target with a query on a served redirect's path counts as served", () => {
115
+ // The plan keeps `/mid?utm=x` as written; the rule on /mid answers it.
116
+ const r = plan([row("/mid", "/about")], [change("1", "/old", "/mid?utm_source=x")], served);
117
+ expect(r.applied).toEqual(["1"]);
118
+ expect(r.waiting).toEqual([]);
119
+ expect(r.plan.redirects.find((p) => p.fromPath === "/old")?.location).toBe("/mid?utm_source=x");
120
+ });
121
+
122
+ it("leaving one change out leaves out the change whose chain went through it", () => {
123
+ // /a → /b → /c, where /c is not in the build: /b waits, then /a ends at
124
+ // /b (no rule, no page) and waits too.
125
+ const r = plan([], [change("a", "/a", "/b"), change("b", "/b", "/c")], served);
126
+ expect(r.applied).toEqual([]);
127
+ expect(r.waiting.map((w) => w.id).sort()).toEqual(["a", "b"]);
128
+ expect(r.rows).toEqual([]);
129
+ });
130
+
131
+ it("an automatic removal waits until the build serves the page at its address", () => {
132
+ const takeOver = change("1", "/new-home", null, { automatic: true });
133
+ const before = plan([row("/new-home", "/about")], [takeOver], served);
134
+ expect(before.waiting).toEqual([{ id: "1", reason: "page" }]);
135
+ expect(before.rows).toEqual([row("/new-home", "/about")]);
136
+ const after = plan([row("/new-home", "/about")], [takeOver], files("new-home/index.html"));
137
+ expect(after.applied).toEqual(["1"]);
138
+ expect(after.rows).toEqual([]);
139
+ });
140
+
141
+ it("a manual removal never waits", () => {
142
+ const r = plan([row("/old", "/about")], [change("1", "/old", null)], served);
143
+ expect(r.applied).toEqual(["1"]);
144
+ expect(r.rows).toEqual([]);
145
+ });
146
+
147
+ it("a rename's removal and 301 wait together for the build that ships the move", () => {
148
+ // A page moved /a → /b over a rule /b → /c: the take-over of /b and the
149
+ // 301 /a → /b go live with the build that serves the page at /b.
150
+ const live = [row("/b", "/about")];
151
+ const changes = [
152
+ change("rm", "/b", null, { automatic: true }),
153
+ change("301", "/a", "/b", { automatic: true }),
154
+ ];
155
+ const oldBuild = plan(live, changes, files("a/index.html", "about/index.html"));
156
+ expect(oldBuild.waiting.map((w) => w.id)).toEqual(["rm"]);
157
+ // The 301 collapses through the live /b → /about and is shadowed by /a anyway.
158
+ expect(oldBuild.applied).toEqual(["301"]);
159
+ const newBuild = plan(live, changes, files("b/index.html", "about/index.html"));
160
+ expect(newBuild.applied.sort()).toEqual(["301", "rm"]);
161
+ expect(newBuild.rows).toEqual([row("/a", "/b")]);
162
+ });
163
+
164
+ it("refuses a table no host can serve, with the plan's message", () => {
165
+ expect(() => plan([row("/a", "/b")], [change("1", "/b", "/a")], served)).toThrow(/loop/);
166
+ });
167
+ });
@@ -0,0 +1,162 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ /**
4
+ * #1550 — redirects are draft state. An address (a from path, both
5
+ * trailing-slash spellings together) has a live state (a row of the
6
+ * `redirects` table, or none) and at most one pending change: a create, a
7
+ * change or a removal, in state Draft or Staged. Draft is served nowhere.
8
+ * A Staged change is served by staging and goes live with the Publish live
9
+ * that ships it.
10
+ *
11
+ * Which Staged changes a build serves is decided here, against the files of
12
+ * that build, so the staging gateway (the staging view, against the staged
13
+ * build) and the admin (Publish live, against the build going live; Go live
14
+ * now, against the live build) decide the same way:
15
+ *
16
+ * - **A change waits for its target.** A change whose same-site target the
17
+ * build does not serve (after the plan's chain collapse) is left out: the
18
+ * address keeps its live state, and the change stays Staged until a build
19
+ * that serves the target goes live. An address on another domain, a
20
+ * target under `/api/` and a 410 never wait.
21
+ * - **An automatic removal waits for its page.** A page that moves onto an
22
+ * address takes it over from the rule there (`applyPathMoveEffects`). That
23
+ * removal goes live with the build that serves the page at the address, so
24
+ * the rule keeps answering until then instead of leaving a 404.
25
+ *
26
+ * Leaving a change out can make another change's chain end at a target the
27
+ * build does not serve, so the check repeats until nothing more is left out.
28
+ * Each round leaves out every unserved change at once, so the rounds are
29
+ * bounded by the length of the longest chain, not by the number of changes.
30
+ */
31
+
32
+ import {
33
+ buildRedirectPlan,
34
+ type RedirectPageUrlStyle,
35
+ type RedirectPlan,
36
+ type RedirectTableRow,
37
+ redirectFromPathSpellings,
38
+ redirectPathServed,
39
+ redirectsToUnservedPaths,
40
+ } from "./redirect-plan.js";
41
+
42
+ /** A pending change of one address, as the planner reads it. */
43
+ export interface PendingRedirectChange {
44
+ readonly id: string;
45
+ /** The bare from path (`/old`, `/` for the root): {@link redirectAddress}. */
46
+ readonly address: string;
47
+ /** The spelling the live row gets (`/old` or `/old/`). */
48
+ readonly fromPath: string;
49
+ /** Null: the change removes the address's live rule. */
50
+ readonly toPath: string | null;
51
+ /** Null exactly when `toPath` is. */
52
+ readonly statusCode: number | null;
53
+ /** A change a page write or a plugin made (never chosen by a person). */
54
+ readonly automatic: boolean;
55
+ }
56
+
57
+ /** Why a change is left out of a build. */
58
+ export type RedirectWaitReason =
59
+ /** Its same-site target is not served by the build. */
60
+ | "target"
61
+ /** An automatic removal whose page the build does not serve at the address yet. */
62
+ | "page";
63
+
64
+ /** What {@link planRedirectRelease} decided. */
65
+ export interface RedirectRelease {
66
+ /** The table the build is served with: the live rows with every applied change. */
67
+ readonly rows: readonly RedirectTableRow[];
68
+ /** {@link buildRedirectPlan} of {@link rows} against the build. */
69
+ readonly plan: RedirectPlan;
70
+ /** Ids of the changes in {@link rows}. */
71
+ readonly applied: readonly string[];
72
+ /** The changes left out, with why. */
73
+ readonly waiting: readonly { readonly id: string; readonly reason: RedirectWaitReason }[];
74
+ }
75
+
76
+ /** Arguments of {@link planRedirectRelease}. */
77
+ export interface PlanRedirectReleaseArgs {
78
+ /** The live state: every row of the `redirects` table. */
79
+ readonly live: readonly RedirectTableRow[];
80
+ /** The changes that may go live with this build. */
81
+ readonly changes: readonly PendingRedirectChange[];
82
+ /** Every file of the build, relative and without a leading slash. */
83
+ readonly servedFiles: ReadonlySet<string>;
84
+ readonly pageUrlStyle: RedirectPageUrlStyle;
85
+ }
86
+
87
+ /**
88
+ * The address a from path belongs to: the path without its trailing slash
89
+ * (`/` stays `/`). Both spellings of a path are one address.
90
+ *
91
+ * @example
92
+ * redirectAddress("/old/") // → "/old"
93
+ */
94
+ export function redirectAddress(fromPath: string): string {
95
+ const [bare] = redirectFromPathSpellings(fromPath);
96
+ if (bare === undefined) throw new Error(`redirectAddress: no spelling for ${fromPath}`);
97
+ return bare;
98
+ }
99
+
100
+ /**
101
+ * The live rows with `changes` applied: a change replaces every live row of
102
+ * its address (both spellings), a removal leaves none. Ordered by from path,
103
+ * like the table dump every serving layer reads.
104
+ */
105
+ export function applyRedirectChanges(
106
+ live: readonly RedirectTableRow[],
107
+ changes: readonly PendingRedirectChange[],
108
+ ): RedirectTableRow[] {
109
+ const changed = new Set(changes.map((c) => c.address));
110
+ const rows: RedirectTableRow[] = live.filter((r) => !changed.has(redirectAddress(r.fromPath)));
111
+ for (const c of changes) {
112
+ if (c.toPath === null || c.statusCode === null) continue;
113
+ rows.push({ fromPath: c.fromPath, toPath: c.toPath, statusCode: c.statusCode });
114
+ }
115
+ return rows.sort((a, b) => (a.fromPath < b.fromPath ? -1 : a.fromPath > b.fromPath ? 1 : 0));
116
+ }
117
+
118
+ /**
119
+ * Decide which of `changes` a build serves (see the module comment) and the
120
+ * table it is served with. Throws what {@link buildRedirectPlan} throws for a
121
+ * table no host can serve (a loop, two rules for one address): the caller
122
+ * refuses with that message, it never serves a guess.
123
+ *
124
+ * @example
125
+ * planRedirectRelease({
126
+ * live: [],
127
+ * changes: [{ id: "c1", address: "/old", fromPath: "/old", toPath: "/new",
128
+ * statusCode: 301, automatic: false }],
129
+ * servedFiles: new Set(["index.html"]),
130
+ * pageUrlStyle: "directory",
131
+ * }).waiting
132
+ * // → [{ id: "c1", reason: "target" }]: the build has no page at /new
133
+ */
134
+ export function planRedirectRelease(args: PlanRedirectReleaseArgs): RedirectRelease {
135
+ const waiting: { id: string; reason: RedirectWaitReason }[] = [];
136
+ let applied: PendingRedirectChange[] = [];
137
+ for (const c of args.changes) {
138
+ if (c.automatic && c.toPath === null && !redirectPathServed(c.address, args.servedFiles)) {
139
+ waiting.push({ id: c.id, reason: "page" });
140
+ } else {
141
+ applied.push(c);
142
+ }
143
+ }
144
+ for (;;) {
145
+ const rows = applyRedirectChanges(args.live, applied);
146
+ const plan = buildRedirectPlan({
147
+ rows,
148
+ pageUrlStyle: args.pageUrlStyle,
149
+ servedFiles: args.servedFiles,
150
+ });
151
+ const unserved = new Set(
152
+ redirectsToUnservedPaths(plan, args.servedFiles).map((r) => r.fromPath),
153
+ );
154
+ const late = applied.filter((c) => c.toPath !== null && unserved.has(c.address));
155
+ if (late.length === 0) {
156
+ return { rows, plan, applied: applied.map((c) => c.id), waiting };
157
+ }
158
+ const lateIds = new Set(late.map((c) => c.id));
159
+ applied = applied.filter((c) => !lateIds.has(c.id));
160
+ for (const c of late) waiting.push({ id: c.id, reason: "target" });
161
+ }
162
+ }
@@ -53,3 +53,20 @@ export function untrustedToolResultValue(content: string): unknown {
53
53
  }
54
54
  return JSON.parse(lines[2] as string);
55
55
  }
56
+
57
+ /** The element that marks content crawled from another website (#1360). */
58
+ export const SITE_DATA_TAG = "site-data";
59
+
60
+ /**
61
+ * Mark `text` as crawled from another website, for the admin AI (#1360): a
62
+ * site import's pages, titles, URLs and what its crawl reported about them
63
+ * come from a job that read third-party pages, so any of it may be an
64
+ * attempt to instruct the AI. Inline, so the tool's own guidance around it
65
+ * stays Caelo's; the static system prompt says what the element means. A
66
+ * closing tag inside the text is escaped, so crawled text cannot end the
67
+ * element early and go on as if it were outside it; everything else stays
68
+ * as it is, so the HTML and Markdown the AI rebuilds from stay readable.
69
+ */
70
+ export function siteData(text: string): string {
71
+ return `<${SITE_DATA_TAG}>${text.replace(/<\/(site-data)/gi, "<\\/$1")}</${SITE_DATA_TAG}>`;
72
+ }
package/src/version.ts CHANGED
@@ -22,7 +22,7 @@
22
22
  * follow standard SemVer.
23
23
  */
24
24
 
25
- export const CAELO_VERSION = "0.12.4";
25
+ export const CAELO_VERSION = "0.12.5";
26
26
 
27
27
  /**
28
28
  * Deprecated alias for back-compat — early P17 work spelled this