@anthusai/papyrus 5.132.0-next.1 → 5.132.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.
- package/amplify/agents/item-change-feed.js +97 -0
- package/amplify/agents/item-change-feed.ts +121 -0
- package/amplify/agents/pieces.js +3 -2
- package/amplify/agents/pieces.ts +6 -2
- package/amplify/functions/item-change-feed/handler.py +46 -0
- package/amplify/site-backend.js +17 -1
- package/amplify/site-backend.ts +22 -1
- package/infra/agent-sync.test.js +7 -1
- package/infra/agent-sync.test.ts +7 -1
- package/infra/build-specs.js +5 -1
- package/infra/build-specs.ts +6 -1
- package/lib/images/operations.generated.ts +18 -2
- package/package.json +1 -1
- package/routes.manifest.json +1 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
|
|
3
|
+
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { Architecture, CfnEventSourceMapping, Function as LambdaFunction, Runtime } from "aws-cdk-lib/aws-lambda";
|
|
5
|
+
import { pythonLambdaCode } from "../functions/shared/python-bundle.js";
|
|
6
|
+
import { connectToSiteApi } from "../site-api-access.js";
|
|
7
|
+
import { allowWakeSending } from "./wake-queue.js";
|
|
8
|
+
/**
|
|
9
|
+
* The Item change feed (docs/design/always-on-agents-architecture.md, slice B2, PPY-8a374e): a DynamoDB stream consumer on the Item table that turns each
|
|
10
|
+
* change to a Story or its checklist into one wake per listening agent on the site's wake queue. All the logic is src/papyrus_content/item_change_feed.py;
|
|
11
|
+
* the handler is amplify/functions/item-change-feed/handler.py.
|
|
12
|
+
*
|
|
13
|
+
* This piece holds four resources: the function, its role and its policy, and the mapping from the stream. It creates no queue and no alarm: the queue is the
|
|
14
|
+
* wake-queue piece, and the alarm on feed lag is the observability slice's (PPY-45ef76). The stream itself is a property of the Item table, switched on by
|
|
15
|
+
* `enableItemStream` (the Item table had no stream and no reader before this; it holds one reader now, the feed).
|
|
16
|
+
*
|
|
17
|
+
* What the function may do, and nothing else: read the Item stream; read the site's `SiteDocument` rows through its own site's API (`getSiteDocument`: the live
|
|
18
|
+
* version of the listener index) and that one document's objects in the site's bucket (`tenants/<tenant>/site/listeners/`); look up and send to its own
|
|
19
|
+
* site's wake queue. It reads no table, receives and deletes no wake, and calls no model.
|
|
20
|
+
*/
|
|
21
|
+
export const ITEM_STREAM_VIEW = dynamodb.StreamViewType.NEW_AND_OLD_IMAGES;
|
|
22
|
+
export const ITEM_FEED_API_FIELDS = ["Query/fields/getSiteDocument"];
|
|
23
|
+
export const LISTENER_INDEX_KIND = "listeners";
|
|
24
|
+
const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
|
|
25
|
+
/**
|
|
26
|
+
* Switches on the Item table's stream, with both images (the feed compares them to tell a board move from any other update), and returns its ARN.
|
|
27
|
+
* The Item table had no stream before. Returns undefined when the site's schema has no Item table.
|
|
28
|
+
*/
|
|
29
|
+
export function enableItemStream(backend) {
|
|
30
|
+
const cfnResources = backend.data.resources.cfnResources;
|
|
31
|
+
const wrapper = cfnResources.amplifyDynamoDbTables?.Item;
|
|
32
|
+
if (wrapper !== undefined) {
|
|
33
|
+
wrapper.streamSpecification = { streamViewType: ITEM_STREAM_VIEW };
|
|
34
|
+
return wrapper.resource.getAtt("TableStreamArn").toString();
|
|
35
|
+
}
|
|
36
|
+
const cfnTables = cfnResources.cfnTables ?? {};
|
|
37
|
+
const found = cfnTables.Item ??
|
|
38
|
+
cfnTables.ItemTable ??
|
|
39
|
+
Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "item")?.[1];
|
|
40
|
+
if (!found)
|
|
41
|
+
return undefined;
|
|
42
|
+
found.streamSpecification = { streamViewType: ITEM_STREAM_VIEW };
|
|
43
|
+
return found.attrStreamArn;
|
|
44
|
+
}
|
|
45
|
+
export function itemChangeFeedPiece(brandId, source) {
|
|
46
|
+
return {
|
|
47
|
+
id: "item-change-feed",
|
|
48
|
+
add(scope) {
|
|
49
|
+
const stack = Stack.of(scope);
|
|
50
|
+
const feed = new LambdaFunction(scope, "ItemChangeFeed", {
|
|
51
|
+
runtime: Runtime.PYTHON_3_12,
|
|
52
|
+
architecture: Architecture.ARM_64,
|
|
53
|
+
handler: "handler.handler",
|
|
54
|
+
timeout: Duration.seconds(60),
|
|
55
|
+
memorySize: 256,
|
|
56
|
+
description: "Puts a wake on the site's agent wake queue for each agent that listens for a change to a Story or its checklist (PPY-8a374e).",
|
|
57
|
+
code: pythonLambdaCode({ functionDir: "item-change-feed", modules: ["papyrus_content"], noDeps: true }, [
|
|
58
|
+
"bash",
|
|
59
|
+
"-c",
|
|
60
|
+
[
|
|
61
|
+
"set -euo pipefail",
|
|
62
|
+
"mkdir -p /asset-output/papyrus_content",
|
|
63
|
+
"cp amplify/functions/item-change-feed/handler.py /asset-output/handler.py",
|
|
64
|
+
"cp -R src/papyrus_content/. /asset-output/papyrus_content/",
|
|
65
|
+
].join(" && "),
|
|
66
|
+
]),
|
|
67
|
+
});
|
|
68
|
+
connectToSiteApi(feed, source.graphqlUrl);
|
|
69
|
+
feed.addEnvironment("PAPYRUS_AGENT_PUBLICATION_ID", brandId);
|
|
70
|
+
feed.addEnvironment("PAPYRUS_TENANT_ID", source.tenantId);
|
|
71
|
+
feed.addEnvironment("PAPYRUS_SITE_BUCKET", source.bucketName);
|
|
72
|
+
feed.addToRolePolicy(new PolicyStatement({
|
|
73
|
+
effect: Effect.ALLOW,
|
|
74
|
+
actions: ["appsync:GraphQL"],
|
|
75
|
+
resources: ITEM_FEED_API_FIELDS.map((field) => `arn:${stack.partition}:appsync:${stack.region}:${stack.account}:apis/${source.apiId}/types/${field}`),
|
|
76
|
+
}));
|
|
77
|
+
feed.addToRolePolicy(new PolicyStatement({
|
|
78
|
+
effect: Effect.ALLOW,
|
|
79
|
+
actions: ["s3:GetObject"],
|
|
80
|
+
resources: [`${source.bucketArn}/tenants/${source.tenantId}/site/${LISTENER_INDEX_KIND}/*`],
|
|
81
|
+
}));
|
|
82
|
+
allowWakeSending(feed, brandId);
|
|
83
|
+
feed.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [source.streamArn] }));
|
|
84
|
+
new CfnEventSourceMapping(scope, "ItemStreamMapping", {
|
|
85
|
+
eventSourceArn: source.streamArn,
|
|
86
|
+
functionName: feed.functionArn,
|
|
87
|
+
startingPosition: "LATEST",
|
|
88
|
+
batchSize: 10,
|
|
89
|
+
maximumBatchingWindowInSeconds: 2,
|
|
90
|
+
maximumRetryAttempts: 5,
|
|
91
|
+
functionResponseTypes: ["ReportBatchItemFailures"],
|
|
92
|
+
// A removed row is never a change the feed acts on: it does not start the function.
|
|
93
|
+
filterCriteria: { filters: [{ pattern: JSON.stringify({ eventName: ["INSERT", "MODIFY"] }) }] },
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
|
|
3
|
+
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { Architecture, CfnEventSourceMapping, Function as LambdaFunction, Runtime } from "aws-cdk-lib/aws-lambda";
|
|
5
|
+
import type { Construct } from "constructs";
|
|
6
|
+
import { pythonLambdaCode } from "../functions/shared/python-bundle";
|
|
7
|
+
import { connectToSiteApi } from "../site-api-access";
|
|
8
|
+
import { allowWakeSending } from "./wake-queue";
|
|
9
|
+
import type { AgentPiece } from "./stack";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The Item change feed (docs/design/always-on-agents-architecture.md, slice B2, PPY-8a374e): a DynamoDB stream consumer on the Item table that turns each
|
|
13
|
+
* change to a Story or its checklist into one wake per listening agent on the site's wake queue. All the logic is src/papyrus_content/item_change_feed.py;
|
|
14
|
+
* the handler is amplify/functions/item-change-feed/handler.py.
|
|
15
|
+
*
|
|
16
|
+
* This piece holds four resources: the function, its role and its policy, and the mapping from the stream. It creates no queue and no alarm: the queue is the
|
|
17
|
+
* wake-queue piece, and the alarm on feed lag is the observability slice's (PPY-45ef76). The stream itself is a property of the Item table, switched on by
|
|
18
|
+
* `enableItemStream` (the Item table had no stream and no reader before this; it holds one reader now, the feed).
|
|
19
|
+
*
|
|
20
|
+
* What the function may do, and nothing else: read the Item stream; read the site's `SiteDocument` rows through its own site's API (`getSiteDocument`: the live
|
|
21
|
+
* version of the listener index) and that one document's objects in the site's bucket (`tenants/<tenant>/site/listeners/`); look up and send to its own
|
|
22
|
+
* site's wake queue. It reads no table, receives and deletes no wake, and calls no model.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const ITEM_STREAM_VIEW = dynamodb.StreamViewType.NEW_AND_OLD_IMAGES;
|
|
26
|
+
export const ITEM_FEED_API_FIELDS = ["Query/fields/getSiteDocument"];
|
|
27
|
+
export const LISTENER_INDEX_KIND = "listeners";
|
|
28
|
+
const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
|
|
29
|
+
|
|
30
|
+
/** What the feed needs from the rest of the site's backend. Absent for a backend that cannot run it (a shared stack, which acts for no one tenant). */
|
|
31
|
+
export type ItemChangeFeedSource = {
|
|
32
|
+
/** The Item table's stream ARN (`enableItemStream`). */
|
|
33
|
+
streamArn: string;
|
|
34
|
+
graphqlUrl: string;
|
|
35
|
+
apiId: string;
|
|
36
|
+
tenantId: string;
|
|
37
|
+
bucketName: string;
|
|
38
|
+
bucketArn: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Switches on the Item table's stream, with both images (the feed compares them to tell a board move from any other update), and returns its ARN.
|
|
43
|
+
* The Item table had no stream before. Returns undefined when the site's schema has no Item table.
|
|
44
|
+
*/
|
|
45
|
+
export function enableItemStream(backend: any): string | undefined {
|
|
46
|
+
const cfnResources = backend.data.resources.cfnResources;
|
|
47
|
+
const wrapper = cfnResources.amplifyDynamoDbTables?.Item;
|
|
48
|
+
if (wrapper !== undefined) {
|
|
49
|
+
wrapper.streamSpecification = { streamViewType: ITEM_STREAM_VIEW };
|
|
50
|
+
return wrapper.resource.getAtt("TableStreamArn").toString();
|
|
51
|
+
}
|
|
52
|
+
const cfnTables = cfnResources.cfnTables ?? {};
|
|
53
|
+
const found =
|
|
54
|
+
cfnTables.Item ??
|
|
55
|
+
cfnTables.ItemTable ??
|
|
56
|
+
Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "item")?.[1];
|
|
57
|
+
if (!found) return undefined;
|
|
58
|
+
(found as any).streamSpecification = { streamViewType: ITEM_STREAM_VIEW };
|
|
59
|
+
return (found as any).attrStreamArn;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function itemChangeFeedPiece(brandId: string, source: ItemChangeFeedSource): AgentPiece {
|
|
63
|
+
return {
|
|
64
|
+
id: "item-change-feed",
|
|
65
|
+
add(scope: Construct) {
|
|
66
|
+
const stack = Stack.of(scope);
|
|
67
|
+
const feed = new LambdaFunction(scope, "ItemChangeFeed", {
|
|
68
|
+
runtime: Runtime.PYTHON_3_12,
|
|
69
|
+
architecture: Architecture.ARM_64,
|
|
70
|
+
handler: "handler.handler",
|
|
71
|
+
timeout: Duration.seconds(60),
|
|
72
|
+
memorySize: 256,
|
|
73
|
+
description: "Puts a wake on the site's agent wake queue for each agent that listens for a change to a Story or its checklist (PPY-8a374e).",
|
|
74
|
+
code: pythonLambdaCode(
|
|
75
|
+
{ functionDir: "item-change-feed", modules: ["papyrus_content"], noDeps: true },
|
|
76
|
+
[
|
|
77
|
+
"bash",
|
|
78
|
+
"-c",
|
|
79
|
+
[
|
|
80
|
+
"set -euo pipefail",
|
|
81
|
+
"mkdir -p /asset-output/papyrus_content",
|
|
82
|
+
"cp amplify/functions/item-change-feed/handler.py /asset-output/handler.py",
|
|
83
|
+
"cp -R src/papyrus_content/. /asset-output/papyrus_content/",
|
|
84
|
+
].join(" && "),
|
|
85
|
+
],
|
|
86
|
+
),
|
|
87
|
+
});
|
|
88
|
+
connectToSiteApi(feed, source.graphqlUrl);
|
|
89
|
+
feed.addEnvironment("PAPYRUS_AGENT_PUBLICATION_ID", brandId);
|
|
90
|
+
feed.addEnvironment("PAPYRUS_TENANT_ID", source.tenantId);
|
|
91
|
+
feed.addEnvironment("PAPYRUS_SITE_BUCKET", source.bucketName);
|
|
92
|
+
feed.addToRolePolicy(
|
|
93
|
+
new PolicyStatement({
|
|
94
|
+
effect: Effect.ALLOW,
|
|
95
|
+
actions: ["appsync:GraphQL"],
|
|
96
|
+
resources: ITEM_FEED_API_FIELDS.map((field) => `arn:${stack.partition}:appsync:${stack.region}:${stack.account}:apis/${source.apiId}/types/${field}`),
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
feed.addToRolePolicy(
|
|
100
|
+
new PolicyStatement({
|
|
101
|
+
effect: Effect.ALLOW,
|
|
102
|
+
actions: ["s3:GetObject"],
|
|
103
|
+
resources: [`${source.bucketArn}/tenants/${source.tenantId}/site/${LISTENER_INDEX_KIND}/*`],
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
allowWakeSending(feed, brandId);
|
|
107
|
+
feed.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [source.streamArn] }));
|
|
108
|
+
new CfnEventSourceMapping(scope, "ItemStreamMapping", {
|
|
109
|
+
eventSourceArn: source.streamArn,
|
|
110
|
+
functionName: feed.functionArn,
|
|
111
|
+
startingPosition: "LATEST",
|
|
112
|
+
batchSize: 10,
|
|
113
|
+
maximumBatchingWindowInSeconds: 2,
|
|
114
|
+
maximumRetryAttempts: 5,
|
|
115
|
+
functionResponseTypes: ["ReportBatchItemFailures"],
|
|
116
|
+
// A removed row is never a change the feed acts on: it does not start the function.
|
|
117
|
+
filterCriteria: { filters: [{ pattern: JSON.stringify({ eventName: ["INSERT", "MODIFY"] }) }] },
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
package/amplify/agents/pieces.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { itemChangeFeedPiece } from "./item-change-feed.js";
|
|
1
2
|
import { scheduleGroupPiece } from "./schedule-group.js";
|
|
2
3
|
import { wakeQueuePiece } from "./wake-queue.js";
|
|
3
4
|
/**
|
|
@@ -6,6 +7,6 @@ import { wakeQueuePiece } from "./wake-queue.js";
|
|
|
6
7
|
* `scripts/test-agents-stack.ts` prints what each costs against the first-deploy budget. The pieces are named after the
|
|
7
8
|
* site, so they are built for one site's brand id.
|
|
8
9
|
*/
|
|
9
|
-
export function agentPieces(brandId) {
|
|
10
|
-
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId)];
|
|
10
|
+
export function agentPieces(brandId, sources = {}) {
|
|
11
|
+
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId), ...(sources.itemChangeFeed ? [itemChangeFeedPiece(brandId, sources.itemChangeFeed)] : [])];
|
|
11
12
|
}
|
package/amplify/agents/pieces.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
import { itemChangeFeedPiece, type ItemChangeFeedSource } from "./item-change-feed";
|
|
1
2
|
import type { AgentPiece } from "./stack";
|
|
2
3
|
import { scheduleGroupPiece } from "./schedule-group";
|
|
3
4
|
import { wakeQueuePiece } from "./wake-queue";
|
|
4
5
|
|
|
6
|
+
/** What a site's backend gives the pieces that read its own resources. A piece whose source is absent is not added (a shared stack acts for no one tenant). */
|
|
7
|
+
export type AgentPieceSources = { itemChangeFeed?: ItemChangeFeedSource };
|
|
8
|
+
|
|
5
9
|
/**
|
|
6
10
|
* The agent pieces a site's `agents` stack holds. Each slice of docs/design/always-on-agents-architecture.md adds its
|
|
7
11
|
* piece here (the wake queue, the schedule group and reconciler, the change feeds, the alarms) and nothing else:
|
|
8
12
|
* `scripts/test-agents-stack.ts` prints what each costs against the first-deploy budget. The pieces are named after the
|
|
9
13
|
* site, so they are built for one site's brand id.
|
|
10
14
|
*/
|
|
11
|
-
export function agentPieces(brandId: string): readonly AgentPiece[] {
|
|
12
|
-
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId)];
|
|
15
|
+
export function agentPieces(brandId: string, sources: AgentPieceSources = {}): readonly AgentPiece[] {
|
|
16
|
+
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId), ...(sources.itemChangeFeed ? [itemChangeFeedPiece(brandId, sources.itemChangeFeed)] : [])];
|
|
13
17
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""The Item change feed: a DynamoDB stream consumer on the Item table that puts wakes on the site's agent wake queue (PPY-8a374e).
|
|
2
|
+
|
|
3
|
+
All the logic is ``papyrus_content.item_change_feed``. This file decodes nothing itself: it builds the collaborators with the function's own role: the
|
|
4
|
+
site's listener index (``agent_listeners.ListenerIndex`` over the site store: one point read of the live version, the body from S3 only when the version
|
|
5
|
+
moved) and the site's wake queue (``agent_wakes.wake_sender``). Nothing here lists a table.
|
|
6
|
+
|
|
7
|
+
A batch is processed in order. A record whose wakes cannot all be put on the queue is reported as failed, and Lambda retries from it; every wake key is the same on
|
|
8
|
+
the retry, so what was already sent starts nothing twice. Records that are no change (a version marked superseded, a card's counts refreshed) cost nothing.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
_INDEX: Any = None # held across invocations of one container: the listener index re-reads the body only when the live version moves
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def listener_index(site) -> Any:
|
|
20
|
+
global _INDEX
|
|
21
|
+
if _INDEX is None:
|
|
22
|
+
import boto3
|
|
23
|
+
|
|
24
|
+
from papyrus_content.agent_listeners import ListenerIndex
|
|
25
|
+
from papyrus_content.agent_policy.run_gate import SiteCatalogue
|
|
26
|
+
from papyrus_content.graphql_authoring import PapyrusGraphQLAuthoringClient
|
|
27
|
+
from papyrus_content.site_store import SiteStore
|
|
28
|
+
|
|
29
|
+
if not (site.tenant_id and site.site_bucket):
|
|
30
|
+
raise RuntimeError("the change feed needs the site's tenant id and bucket (PAPYRUS_TENANT_ID, PAPYRUS_SITE_BUCKET) to find who listens")
|
|
31
|
+
os.environ.setdefault("PAPYRUS_GRAPHQL_AUTH", "iam") # the function's own role signs the calls
|
|
32
|
+
client = PapyrusGraphQLAuthoringClient(endpoint=site.appsync_endpoint)
|
|
33
|
+
_INDEX = ListenerIndex(SiteStore(boto3.client("s3"), site.site_bucket, SiteCatalogue(client)), site.tenant_id)
|
|
34
|
+
return _INDEX
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def handler(event: dict[str, Any], context: Any = None, *, index=None, send=None, environment=None) -> dict[str, Any]:
|
|
38
|
+
from papyrus_content import item_change_feed
|
|
39
|
+
from papyrus_content.agent_wakes import wake_sender
|
|
40
|
+
from papyrus_content.localize.dispatch import decode_image
|
|
41
|
+
|
|
42
|
+
site = item_change_feed.Site.from_environment(environment if environment is not None else os.environ)
|
|
43
|
+
result = item_change_feed.handle(event.get("Records", []), decode=decode_image, listeners=lambda: (index or listener_index(site)).document(),
|
|
44
|
+
send=send or wake_sender(), site=site)
|
|
45
|
+
print(json.dumps({"event": "papyrus_item_change_feed", "records": len(event.get("Records", [])), "wakesSent": result["wakesSent"], "failed": len(result["batchItemFailures"])}))
|
|
46
|
+
return {"batchItemFailures": result["batchItemFailures"]}
|
package/amplify/site-backend.js
CHANGED
|
@@ -34,6 +34,7 @@ import { addCliRemoteLogin, planCliEnrollment, planCliRemoteLogin } from "./cli-
|
|
|
34
34
|
import { cognitoAuthorizeEndpoint } from "./cli-login/cognito-endpoint.js";
|
|
35
35
|
import { defineSiteData } from "./data/resource.js";
|
|
36
36
|
import { agentPieces } from "./agents/pieces.js";
|
|
37
|
+
import { enableItemStream } from "./agents/item-change-feed.js";
|
|
37
38
|
import { ADMIN_AGENT_POLICY_GRANT_STACK_NAME, ADMIN_AGENT_POLICY_GROUP, addAdminAgentPolicyGrant } from "./agents/admin-policy-grant.js";
|
|
38
39
|
import { addAgentsStack } from "./agents/stack.js";
|
|
39
40
|
import { isBootstrapStage, resolveExcludedModelGroups } from "./data/schema.js";
|
|
@@ -785,7 +786,22 @@ export function defineSiteBackend(papyrusSite, options = {}) {
|
|
|
785
786
|
// Announcements are off until the owner turns them on; the parameters and the lease are per site (the brand id names the site).
|
|
786
787
|
wireAnnouncements(backend, siteBackendIdentity.brandId);
|
|
787
788
|
// Every always-on agent resource is in the one `agents` stack, which the first deploy of a new site leaves out (amplify/agents/stack.ts).
|
|
788
|
-
|
|
789
|
+
// The Item change feed (slice B2) reads the Item table's stream, so the stream is switched on with the stack that reads it, never in the first deploy. A dedicated site
|
|
790
|
+
// with a tenant id only: a shared stack's rows carry tenants it does not act for, and the listener index is one site's document.
|
|
791
|
+
const bootstrapStage = isBootstrapStage(process.env);
|
|
792
|
+
const itemStreamArn = tenancyMode !== "shared" && backendTenantId !== "" && !bootstrapStage ? enableItemStream(backend) : undefined;
|
|
793
|
+
addAgentsStack((name) => backend.createStack(name), agentPieces(siteBackendIdentity.brandId, {
|
|
794
|
+
itemChangeFeed: itemStreamArn === undefined
|
|
795
|
+
? undefined
|
|
796
|
+
: {
|
|
797
|
+
streamArn: itemStreamArn,
|
|
798
|
+
graphqlUrl: backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl,
|
|
799
|
+
apiId: backend.data.resources.graphqlApi.apiId,
|
|
800
|
+
tenantId: backendTenantId,
|
|
801
|
+
bucketName: storageBucket.bucketName,
|
|
802
|
+
bucketArn: storageBucket.bucketArn,
|
|
803
|
+
},
|
|
804
|
+
}), bootstrapStage);
|
|
789
805
|
// The signed-in admin may put and get the agent policy document's versions (`papyrus agents set-rung --apply`). A dedicated site only: the grant names every
|
|
790
806
|
// tenant's prefix, which a shared stack's admin must not hold.
|
|
791
807
|
if (tenancyMode !== "shared") {
|
package/amplify/site-backend.ts
CHANGED
|
@@ -41,6 +41,7 @@ import { addCliRemoteLogin, planCliEnrollment, planCliRemoteLogin } from "./cli-
|
|
|
41
41
|
import { cognitoAuthorizeEndpoint } from "./cli-login/cognito-endpoint";
|
|
42
42
|
import { defineSiteData } from "./data/resource";
|
|
43
43
|
import { agentPieces } from "./agents/pieces";
|
|
44
|
+
import { enableItemStream } from "./agents/item-change-feed";
|
|
44
45
|
import { ADMIN_AGENT_POLICY_GRANT_STACK_NAME, ADMIN_AGENT_POLICY_GROUP, addAdminAgentPolicyGrant } from "./agents/admin-policy-grant";
|
|
45
46
|
import { addAgentsStack } from "./agents/stack";
|
|
46
47
|
import { isBootstrapStage, resolveExcludedModelGroups } from "./data/schema";
|
|
@@ -943,7 +944,27 @@ export function defineSiteBackend(papyrusSite: PapyrusSite, options: DefineSiteB
|
|
|
943
944
|
// Announcements are off until the owner turns them on; the parameters and the lease are per site (the brand id names the site).
|
|
944
945
|
wireAnnouncements(backend, siteBackendIdentity.brandId);
|
|
945
946
|
// Every always-on agent resource is in the one `agents` stack, which the first deploy of a new site leaves out (amplify/agents/stack.ts).
|
|
946
|
-
|
|
947
|
+
// The Item change feed (slice B2) reads the Item table's stream, so the stream is switched on with the stack that reads it, never in the first deploy. A dedicated site
|
|
948
|
+
// with a tenant id only: a shared stack's rows carry tenants it does not act for, and the listener index is one site's document.
|
|
949
|
+
const bootstrapStage = isBootstrapStage(process.env);
|
|
950
|
+
const itemStreamArn = tenancyMode !== "shared" && backendTenantId !== "" && !bootstrapStage ? enableItemStream(backend) : undefined;
|
|
951
|
+
addAgentsStack(
|
|
952
|
+
(name) => backend.createStack(name),
|
|
953
|
+
agentPieces(siteBackendIdentity.brandId, {
|
|
954
|
+
itemChangeFeed:
|
|
955
|
+
itemStreamArn === undefined
|
|
956
|
+
? undefined
|
|
957
|
+
: {
|
|
958
|
+
streamArn: itemStreamArn,
|
|
959
|
+
graphqlUrl: backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl as string,
|
|
960
|
+
apiId: backend.data.resources.graphqlApi.apiId as string,
|
|
961
|
+
tenantId: backendTenantId,
|
|
962
|
+
bucketName: storageBucket.bucketName,
|
|
963
|
+
bucketArn: storageBucket.bucketArn,
|
|
964
|
+
},
|
|
965
|
+
}),
|
|
966
|
+
bootstrapStage,
|
|
967
|
+
);
|
|
947
968
|
// The signed-in admin may put and get the agent policy document's versions (`papyrus agents set-rung --apply`). A dedicated site only: the grant names every
|
|
948
969
|
// tenant's prefix, which a shared stack's admin must not hold.
|
|
949
970
|
if (tenancyMode !== "shared") {
|
package/infra/agent-sync.test.js
CHANGED
|
@@ -27,8 +27,14 @@ for (const name of siteNames) {
|
|
|
27
27
|
assert.equal(commands[schedules], `PAPYRUS_ROOT="$PWD" PAPYRUS_GRAPHQL_AUTH=iam papyrus agents schedules --site ${config.brand.toLowerCase()}`, `${name}: build role, operator lane, the site's brand id`);
|
|
28
28
|
assert.ok(!commands[schedules].includes("||"), `${name}: a failing schedule step fails the build`);
|
|
29
29
|
assert.ok(!/dry-run|--check/.test(commands[schedules]), `${name}: the build reconciles`);
|
|
30
|
+
const listeners = commands.findIndex((line) => line.includes("papyrus agents listeners"));
|
|
31
|
+
assert.ok(listeners === schedules + 1, `${name}: the listener index is written right after the schedule group is reconciled`);
|
|
32
|
+
assert.equal(commands[listeners], 'PAPYRUS_ROOT="$PWD" PAPYRUS_GRAPHQL_AUTH=iam papyrus agents listeners', `${name}: build role, operator lane, the site's checkout`);
|
|
33
|
+
assert.ok(!commands[listeners].includes("||"), `${name}: a failing listener step fails the build`);
|
|
34
|
+
assert.ok(!/dry-run|--check/.test(commands[listeners]), `${name}: the build writes the index`);
|
|
30
35
|
const alexa = commands.findIndex((line) => line.includes("alexa-sync"));
|
|
31
|
-
assert.ok(alexa >
|
|
36
|
+
assert.ok(alexa > listeners, `${name}: the Alexa sync, which may fail quietly, stays last`);
|
|
32
37
|
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents apply"), `${name}: the staging build does not apply agent definitions`);
|
|
33
38
|
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents schedules"), `${name}: the staging build does not touch the schedule group`);
|
|
39
|
+
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents listeners"), `${name}: the staging build does not write the listener index`);
|
|
34
40
|
}
|
package/infra/agent-sync.test.ts
CHANGED
|
@@ -28,8 +28,14 @@ for (const name of siteNames) {
|
|
|
28
28
|
assert.equal(commands[schedules], `PAPYRUS_ROOT="$PWD" PAPYRUS_GRAPHQL_AUTH=iam papyrus agents schedules --site ${config.brand.toLowerCase()}`, `${name}: build role, operator lane, the site's brand id`);
|
|
29
29
|
assert.ok(!commands[schedules].includes("||"), `${name}: a failing schedule step fails the build`);
|
|
30
30
|
assert.ok(!/dry-run|--check/.test(commands[schedules]), `${name}: the build reconciles`);
|
|
31
|
+
const listeners = commands.findIndex((line) => line.includes("papyrus agents listeners"));
|
|
32
|
+
assert.ok(listeners === schedules + 1, `${name}: the listener index is written right after the schedule group is reconciled`);
|
|
33
|
+
assert.equal(commands[listeners], 'PAPYRUS_ROOT="$PWD" PAPYRUS_GRAPHQL_AUTH=iam papyrus agents listeners', `${name}: build role, operator lane, the site's checkout`);
|
|
34
|
+
assert.ok(!commands[listeners].includes("||"), `${name}: a failing listener step fails the build`);
|
|
35
|
+
assert.ok(!/dry-run|--check/.test(commands[listeners]), `${name}: the build writes the index`);
|
|
31
36
|
const alexa = commands.findIndex((line) => line.includes("alexa-sync"));
|
|
32
|
-
assert.ok(alexa >
|
|
37
|
+
assert.ok(alexa > listeners, `${name}: the Alexa sync, which may fail quietly, stays last`);
|
|
33
38
|
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents apply"), `${name}: the staging build does not apply agent definitions`);
|
|
34
39
|
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents schedules"), `${name}: the staging build does not touch the schedule group`);
|
|
40
|
+
assert.ok(!cmsStagingBuildSpec(config).includes("papyrus agents listeners"), `${name}: the staging build does not write the listener index`);
|
|
35
41
|
}
|
package/infra/build-specs.js
CHANGED
|
@@ -67,8 +67,12 @@ const AGENT_SYNC = `${CHECKOUT_AS_PAPYRUS_ROOT} ${BUILD_ROLE_LANE} papyrus agent
|
|
|
67
67
|
function scheduleSync(config) {
|
|
68
68
|
return `${CHECKOUT_AS_PAPYRUS_ROOT} ${BUILD_ROLE_LANE} papyrus agents schedules --site ${config.brand.toLowerCase()}`;
|
|
69
69
|
}
|
|
70
|
+
// Right after the schedule group: write the site's listener index (slice B2, PPY-8a374e), the one small document the Item change feed reads to find the agents
|
|
71
|
+
// that listen for a change, so a change never lists the Automation table. It is derived from the same definitions, writes nothing when they have not changed,
|
|
72
|
+
// and holds no rung or pause. Never allowed to fail quietly either (features/agent-sync.feature).
|
|
73
|
+
const LISTENER_SYNC = `${CHECKOUT_AS_PAPYRUS_ROOT} ${BUILD_ROLE_LANE} papyrus agents listeners`;
|
|
70
74
|
function procedureSyncCommands(config) {
|
|
71
|
-
return [...pythonProvisioningCommands(config), PROCEDURE_SYNC, AGENT_SYNC, scheduleSync(config)];
|
|
75
|
+
return [...pythonProvisioningCommands(config), PROCEDURE_SYNC, AGENT_SYNC, scheduleSync(config), LISTENER_SYNC];
|
|
72
76
|
}
|
|
73
77
|
const GATE_FAILURE_MESSAGE_PREFIX = "No LeftBlock CI receipt for";
|
|
74
78
|
/**
|
package/infra/build-specs.ts
CHANGED
|
@@ -96,8 +96,13 @@ function scheduleSync(config: AmplifyAppShellSiteConfig): string {
|
|
|
96
96
|
return `${CHECKOUT_AS_PAPYRUS_ROOT} ${BUILD_ROLE_LANE} papyrus agents schedules --site ${config.brand.toLowerCase()}`;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
// Right after the schedule group: write the site's listener index (slice B2, PPY-8a374e), the one small document the Item change feed reads to find the agents
|
|
100
|
+
// that listen for a change, so a change never lists the Automation table. It is derived from the same definitions, writes nothing when they have not changed,
|
|
101
|
+
// and holds no rung or pause. Never allowed to fail quietly either (features/agent-sync.feature).
|
|
102
|
+
const LISTENER_SYNC = `${CHECKOUT_AS_PAPYRUS_ROOT} ${BUILD_ROLE_LANE} papyrus agents listeners`;
|
|
103
|
+
|
|
99
104
|
function procedureSyncCommands(config: AmplifyAppShellSiteConfig): string[] {
|
|
100
|
-
return [...pythonProvisioningCommands(config), PROCEDURE_SYNC, AGENT_SYNC, scheduleSync(config)];
|
|
105
|
+
return [...pythonProvisioningCommands(config), PROCEDURE_SYNC, AGENT_SYNC, scheduleSync(config), LISTENER_SYNC];
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
const GATE_FAILURE_MESSAGE_PREFIX = "No LeftBlock CI receipt for";
|
|
@@ -1287,14 +1287,30 @@ export type AnnouncementsUnlockResult = {
|
|
|
1287
1287
|
id: string;
|
|
1288
1288
|
};
|
|
1289
1289
|
|
|
1290
|
-
/** Correct a Delivery with a new public text: the next generation, which supersedes the one that went. */
|
|
1290
|
+
/** Correct a Delivery that went with a new public text: the next generation, which supersedes the one that went. */
|
|
1291
1291
|
export type AnnouncementsCorrectInput = {
|
|
1292
|
-
/** The Delivery
|
|
1292
|
+
/** The id of the Delivery that went and is to be corrected. */
|
|
1293
1293
|
delivery: string;
|
|
1294
|
+
/** The corrected public text. It is checked like the text of a new Delivery. */
|
|
1295
|
+
text: string;
|
|
1294
1296
|
};
|
|
1295
1297
|
|
|
1296
1298
|
export type AnnouncementsCorrectResult = {
|
|
1299
|
+
/** The new Delivery's id. */
|
|
1297
1300
|
id: string;
|
|
1301
|
+
/** The new Delivery's generation. */
|
|
1302
|
+
generation: number;
|
|
1303
|
+
/** The id of the Delivery it replaces. */
|
|
1304
|
+
supersedes: string;
|
|
1305
|
+
/** The new Delivery's state. */
|
|
1306
|
+
state: string;
|
|
1307
|
+
/** The review the new Delivery goes under: the one the old Delivery had. */
|
|
1308
|
+
approval: string;
|
|
1309
|
+
/** The checks the new text passed. */
|
|
1310
|
+
checks: {
|
|
1311
|
+
name: string;
|
|
1312
|
+
passed: boolean;
|
|
1313
|
+
}[];
|
|
1298
1314
|
};
|
|
1299
1315
|
|
|
1300
1316
|
/** Record the address of a post a person made by hand for a manual Delivery: the Delivery becomes sent and its attempt names that person. */
|
package/package.json
CHANGED
package/routes.manifest.json
CHANGED