Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(activity): attribute EditDocument collab edits to the AI bot (#6051)

teoc758571f2c32
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadata.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadata.ts
index 42bd7a7400..ed43634d1c 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadata.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadata.ts
@@ -4,17 +4,23 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
+import type { DocumentSyncContentUpdatedMetadataActor } from './documentSyncContentUpdatedMetadataActor';
import type { DocumentSyncContentUpdatedMetadataDocumentVersionId } from './documentSyncContentUpdatedMetadataDocumentVersionId';
+import type { DocumentSyncContentUpdatedMetadataOnBehalfOf } from './documentSyncContentUpdatedMetadataOnBehalfOf';
import type { FileType } from './fileType';
/**
* Metadata for [`DocumentTopicEvent::SyncContentUpdated`].
*/
export interface DocumentSyncContentUpdatedMetadata {
+ /** Who mechanically changed the content. Absent on events published
+before attribution, and on human-only collab sessions. */
+ actor?: DocumentSyncContentUpdatedMetadataActor;
/** The id of the live-collab document whose content changed. */
document_id: string;
/** Version marker for the sync snapshot, when the caller supplies one. */
document_version_id?: DocumentSyncContentUpdatedMetadataDocumentVersionId;
/** File type of the sync document (markdown today). */
file_type: FileType;
+ on_behalf_of?: DocumentSyncContentUpdatedMetadataOnBehalfOf;
}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataActor.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataActor.ts
new file mode 100644
index 0000000000..987d8ea2d5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataActor.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Who mechanically changed the content. Absent on events published
+before attribution, and on human-only collab sessions.
+ */
+export type DocumentSyncContentUpdatedMetadataActor = string | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataOnBehalfOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataOnBehalfOf.ts
new file mode 100644
index 0000000000..c6cab4ac49
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/documentSyncContentUpdatedMetadataOnBehalfOf.ts
@@ -0,0 +1,10 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { MacroUserIdStr } from './macroUserIdStr';
+
+export type DocumentSyncContentUpdatedMetadataOnBehalfOf =
+ null | MacroUserIdStr;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
index 1e6e23a9a2..d3a3cf116a 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
@@ -544,7 +544,9 @@ export * from './documentResponsePresignedUrl';
export * from './documentStorageServiceApiVersion';
export * from './documentSubType';
export * from './documentSyncContentUpdatedMetadata';
+export * from './documentSyncContentUpdatedMetadataActor';
export * from './documentSyncContentUpdatedMetadataDocumentVersionId';
+export * from './documentSyncContentUpdatedMetadataOnBehalfOf';
export * from './documentTeamShareResponse';
export * from './documentTeamShareResponseTeamId';
export * from './editAnchorRequest';
diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.json
index 05fcd8a2db..6a561759d8 100644
--- a/apps/web/src/lib/service-clients/service-storage/openapi.json
+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json
@@ -18818,6 +18818,10 @@
"description": "Metadata for [`DocumentTopicEvent::SyncContentUpdated`].",
"required": ["document_id", "file_type"],
"properties": {
+ "actor": {
+ "type": ["string", "null"],
+ "description": "Who mechanically changed the content. Absent on events published\nbefore attribution, and on human-only collab sessions."
+ },
"document_id": {
"type": "string",
"description": "The id of the live-collab document whose content changed."
@@ -18829,6 +18833,17 @@
"file_type": {
"$ref": "#/components/schemas/FileType",
"description": "File type of the sync document (markdown today)."
+ },
+ "on_behalf_of": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/MacroUserIdStr",
+ "description": "The user whose feed this edit belongs on, when different from\n[`Self::actor`]."
+ }
+ ]
}
}
},
diff --git a/crates/activity/src/domain/models.rs b/crates/activity/src/domain/models.rs
index ab7fbcae91..eafad2e3fd 100644
--- a/crates/activity/src/domain/models.rs
+++ b/crates/activity/src/domain/models.rs
@@ -62,6 +62,15 @@ impl Attribution {
Self::Delegated { actor, subject }
}
+ /// Delegated to `on_behalf_of` when set, otherwise direct. Mirrors how
+ /// events carry attribution as an actor plus an optional subject.
+ pub fn new(actor: Actor<'static>, on_behalf_of: Option<MacroUserIdStr<'static>>) -> Self {
+ match on_behalf_of {
+ Some(subject) => Self::Delegated { actor, subject },
+ None => Self::Direct { actor },
+ }
+ }
+
/// Who mechanically acted.
pub fn actor(&self) -> Actor<'static> {
match self {
diff --git a/crates/documents/src/domain/activity.rs b/crates/documents/src/domain/activity.rs
index 2a5619efac..3c49321e88 100644
--- a/crates/documents/src/domain/activity.rs
+++ b/crates/documents/src/domain/activity.rs
@@ -41,12 +41,8 @@ impl ActivitySource for DocumentTopicEvent {
.actor
.clone()
.unwrap_or_else(|| Actor::new_from_user(metadata.owner.clone()));
- let attribution = match metadata.on_behalf_of.clone() {
- Some(subject) => Attribution::delegated(actor, subject),
- None => Attribution::direct(actor),
- };
single(
- attribution,
+ Attribution::new(actor, metadata.on_behalf_of.clone()),
CommonAction::Created,
&metadata.document_id,
metadata.created_at.unwrap_or_else(|| event_time(event_id)),
@@ -82,9 +78,17 @@ impl ActivitySource for DocumentTopicEvent {
}
// Extraction-pipeline noise, not user activity.
DocumentTopicEvent::ContentUploaded(_) => Ingest::Ignore,
- // Carries no actor today; becomes an Edited activity once collab
- // edits are attributed.
- DocumentTopicEvent::SyncContentUpdated(_) => Ingest::Ignore,
+ // Only AI-attributed sessions carry an actor; human-only collab
+ // sessions stay unattributed.
+ DocumentTopicEvent::SyncContentUpdated(metadata) => match metadata.actor.clone() {
+ Some(actor) => single(
+ Attribution::new(actor, metadata.on_behalf_of.clone()),
+ CommonAction::Edited,
+ &metadata.document_id,
+ event_time(event_id),
+ ),
+ None => Ingest::Ignore,
+ },
// Session lifecycle (first join / last leave), no actor.
DocumentTopicEvent::Interaction(_) => Ingest::Ignore,
}
diff --git a/crates/documents/src/domain/activity/test.rs b/crates/documents/src/domain/activity/test.rs
index ec1b542cae..53a9f4caf2 100644
--- a/crates/documents/src/domain/activity/test.rs
+++ b/crates/documents/src/domain/activity/test.rs
@@ -191,6 +191,8 @@ fn pipeline_and_session_events_are_ignored() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
},
));
assert_eq!(sync.event.ingest(sync.event_id), Ingest::Ignore);
@@ -207,6 +209,26 @@ fn pipeline_and_session_events_are_ignored() {
);
}
+#[test]
+fn attributed_sync_content_is_an_edited_activity() {
+ let event = envelope(DocumentTopicEvent::SyncContentUpdated(
+ DocumentSyncContentUpdatedMetadata {
+ document_id: DOCUMENT_ID.to_string(),
+ file_type: FileType::Md,
+ document_version_id: None,
+ actor: Some(Actor::new_from_bot(bot_id::MACRO_AI_BOT_ID)),
+ on_behalf_of: Some(user("macro|[email protected]")),
+ },
+ ));
+ let activity = single_activity(event.event.ingest(event.event_id));
+ assert_eq!(activity.action, Action::Edited);
+ assert_eq!(
+ activity.actor.as_ref(),
+ bot_id::MACRO_AI_BOT_ID.into_storage_id().as_ref()
+ );
+ assert_eq!(activity.subject_id, "macro|[email protected]");
+}
+
#[test]
fn replaying_an_event_derives_identical_activity_ids() {
let event = envelope(DocumentTopicEvent::Deleted(DocumentDeletedMetadata {
diff --git a/crates/documents/src/domain/events.rs b/crates/documents/src/domain/events.rs
index e65deeafdb..e9e8cb06bf 100644
--- a/crates/documents/src/domain/events.rs
+++ b/crates/documents/src/domain/events.rs
@@ -110,6 +110,35 @@ pub struct DocumentSyncContentUpdatedMetadata {
pub file_type: FileType,
/// Version marker for the sync snapshot, when the caller supplies one.
pub document_version_id: Option<String>,
+ /// Who mechanically changed the content. Absent on events published
+ /// before attribution, and on human-only collab sessions.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ #[cfg_attr(feature = "schema", schema(value_type = Option<String>))]
+ pub actor: Option<Actor<'static>>,
+ /// The user whose feed this edit belongs on, when different from
+ /// [`Self::actor`].
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub on_behalf_of: Option<MacroUserIdStr<'static>>,
+}
+
+impl DocumentSyncContentUpdatedMetadata {
+ /// Build metadata from extract-sync strings. Invalid actor or subject
+ /// ids are dropped so a bad payload still extracts the document.
+ pub fn from_extract(
+ document_id: String,
+ file_type: FileType,
+ document_version_id: Option<String>,
+ actor: Option<String>,
+ on_behalf_of: Option<String>,
+ ) -> Self {
+ Self {
+ document_id,
+ file_type,
+ document_version_id,
+ actor: actor.and_then(|id| Actor::try_from(id).ok()),
+ on_behalf_of: on_behalf_of.and_then(|id| MacroUserIdStr::try_from(id).ok()),
+ }
+ }
}
/// Metadata for [`DocumentTopicEvent::Purged`].
diff --git a/crates/documents/src/domain/events/test.rs b/crates/documents/src/domain/events/test.rs
index 210393a8f8..80ced5f1a9 100644
--- a/crates/documents/src/domain/events/test.rs
+++ b/crates/documents/src/domain/events/test.rs
@@ -63,6 +63,8 @@ fn sync_content_updated_serializes_to_the_exact_envelope() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
}),
);
@@ -116,6 +118,8 @@ fn optional_document_versions_support_present_and_absent_values() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: Some("snapshot-7".to_string()),
+ actor: None,
+ on_behalf_of: None,
}),
];
@@ -162,6 +166,8 @@ fn search_event_constructors_use_the_document_key_and_schema_v1() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
};
let sync_content_updated =
DocumentMacroEvent::sync_content_updated(DOCUMENT_ID, sync_metadata.clone());
@@ -243,3 +249,16 @@ fn existing_v1_document_events_still_decode() {
})
);
}
+
+#[test]
+fn sync_extract_drops_invalid_attribution_strings() {
+ let metadata = DocumentSyncContentUpdatedMetadata::from_extract(
+ DOCUMENT_ID.to_string(),
+ FileType::Md,
+ None,
+ Some("not-an-actor".to_string()),
+ Some("also-not-a-user".to_string()),
+ );
+ assert_eq!(metadata.actor, None);
+ assert_eq!(metadata.on_behalf_of, None);
+}
diff --git a/crates/documents/src/domain/permission_token.rs b/crates/documents/src/domain/permission_token.rs
index 78980f4fd8..6a58da9d0c 100644
--- a/crates/documents/src/domain/permission_token.rs
+++ b/crates/documents/src/domain/permission_token.rs
@@ -4,16 +4,30 @@
use crate::domain::models::DocumentError;
use macro_sync_service_jwt::{DocumentPermissionToken, ISSUER, TOKEN_TTL_SECS};
use macro_user_id::user_id::MacroUserIdStr;
-use model::document::DocumentPermissionsToken;
use models_permissions::share_permission::access_level::AccessLevel;
use std::time::{SystemTime, UNIX_EPOCH};
+/// Claims minted for the sync service. `actor` is not on the public
+/// document-permissions token type so the OpenAPI validate API stays unchanged.
+#[derive(serde::Serialize, serde::Deserialize)]
+pub(crate) struct PermissionTokenClaims {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub(crate) user_id: Option<MacroUserIdStr<'static>>,
+ document_id: String,
+ access_level: AccessLevel,
+ exp: usize,
+ iss: String,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub(crate) actor: Option<String>,
+}
+
/// Sign a document permission token for the given user and document.
pub fn encode_permission_token(
user_id: Option<String>,
document_id: String,
access_level: AccessLevel,
jwt_secret: &str,
+ actor: Option<String>,
) -> Result<DocumentPermissionToken, DocumentError> {
let user_id = user_id
.map(MacroUserIdStr::try_from)
@@ -26,13 +40,23 @@ pub fn encode_permission_token(
.as_secs() as usize;
Ok(macro_sync_service_jwt::encode(
- &DocumentPermissionsToken {
+ &PermissionTokenClaims {
user_id,
document_id,
access_level,
exp: now + TOKEN_TTL_SECS,
iss: ISSUER.to_string(),
+ actor,
},
jwt_secret,
)?)
}
+
+/// Read claims from a token minted by [`encode_permission_token`].
+#[cfg(test)]
+pub(crate) fn decode_permission_token(
+ token: &DocumentPermissionToken,
+ jwt_secret: &str,
+) -> Result<PermissionTokenClaims, DocumentError> {
+ Ok(macro_sync_service_jwt::decode(token.as_str(), jwt_secret)?)
+}
diff --git a/crates/documents/src/inbound/axum_router/create_markdown.rs b/crates/documents/src/inbound/axum_router/create_markdown.rs
index 89210357e6..4488157a03 100644
--- a/crates/documents/src/inbound/axum_router/create_markdown.rs
+++ b/crates/documents/src/inbound/axum_router/create_markdown.rs
@@ -79,6 +79,7 @@ pub async fn create_markdown_handler<
document_id.clone(),
AccessLevel::Edit,
&state.document_permission_jwt_secret,
+ None,
)
.map_err(|e| {
tracing::error!(error=?e, "failed to encode permission token");
diff --git a/crates/documents/src/inbound/axum_router/create_task.rs b/crates/documents/src/inbound/axum_router/create_task.rs
index dfb1374a1d..9ad1677544 100644
--- a/crates/documents/src/inbound/axum_router/create_task.rs
+++ b/crates/documents/src/inbound/axum_router/create_task.rs
@@ -106,6 +106,7 @@ pub async fn create_task_handler<
document_id.clone(),
AccessLevel::Edit,
&state.document_permission_jwt_secret,
+ None,
)
.map_err(|e| {
tracing::error!(error=?e, "failed to encode permission token");
diff --git a/crates/documents/src/inbound/toolset/edit_document.rs b/crates/documents/src/inbound/toolset/edit_document.rs
index 229794befb..ac1dfe496c 100644
--- a/crates/documents/src/inbound/toolset/edit_document.rs
+++ b/crates/documents/src/inbound/toolset/edit_document.rs
@@ -120,6 +120,7 @@ where
self.document_id.clone(),
AccessLevel::Edit,
&ctx.document_permission_jwt_secret,
+ Some(bot_id::MACRO_AI_BOT_ID.into_storage_id().to_string()),
)
.map_err(|e| ToolCallError {
description: "failed to mint document token".to_string(),
diff --git a/crates/documents/src/inbound/toolset/edit_document/test.rs b/crates/documents/src/inbound/toolset/edit_document/test.rs
index 7959363270..4ca2351a85 100644
--- a/crates/documents/src/inbound/toolset/edit_document/test.rs
+++ b/crates/documents/src/inbound/toolset/edit_document/test.rs
@@ -9,6 +9,7 @@ use crate::domain::models::{
DocumentTeamShareResponse, EditDocumentServiceArgs, GithubPullRequestsResponse,
ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,
};
+use crate::domain::permission_token::decode_permission_token;
use crate::domain::ports::editing::{EditResult, EditingWorkerService};
use crate::domain::response::{
CreateDocumentResponseData, DocumentResponse, GetDocumentResponseData, LocationResponseV3,
@@ -391,19 +392,24 @@ impl EntityAccessService for FakeEntityAccessService {
#[derive(Clone, Default)]
struct FakeEditingWorker {
edit_calls: Arc<Mutex<Vec<String>>>,
+ tokens: Arc<Mutex<Vec<DocumentPermissionToken>>>,
}
impl EditingWorkerService for FakeEditingWorker {
async fn edit(
&self,
document_id: &str,
- _document_token: &DocumentPermissionToken,
+ document_token: &DocumentPermissionToken,
_instructions: &str,
) -> anyhow::Result<EditResult> {
self.edit_calls
.lock()
.expect("edit calls lock poisoned")
.push(document_id.to_string());
+ self.tokens
+ .lock()
+ .expect("edit tokens lock poisoned")
+ .push(document_token.clone());
Ok(EditResult {
edits_applied: 1,
@@ -505,6 +511,24 @@ async fn allows_markdown_document() {
*editing.edit_calls.lock().expect("edit calls lock poisoned"),
vec![TEST_DOCUMENT_ID.to_string()]
);
+
+ let token = editing
+ .tokens
+ .lock()
+ .expect("edit tokens lock poisoned")
+ .first()
+ .expect("edit minted a document token")
+ .clone();
+ let claims =
+ decode_permission_token(&token, "unused-jwt-secret").expect("edit token should decode");
+ assert_eq!(
+ claims.user_id.as_ref().map(|user| user.as_ref()),
+ Some(TEST_USER_ID)
+ );
+ assert_eq!(
+ claims.actor.as_deref(),
+ Some(bot_id::MACRO_AI_BOT_ID.into_storage_id().as_ref())
+ );
}
#[test]
diff --git a/crates/soup_realtime/src/inbound/kafka_consumer/test.rs b/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
index 2868b908ec..2c6f873b13 100644
--- a/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
+++ b/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
@@ -197,6 +197,8 @@ fn search_only_document_events_do_not_emit_patches() {
document_id: DOCUMENT_ID.to_string(),
file_type: "md".parse().expect("valid file type"),
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
}),
DocumentTopicEvent::Purged(DocumentPurgedMetadata {
document_id: DOCUMENT_ID.to_string(),
diff --git a/crates/webhook/src/domain/ingestion/test.rs b/crates/webhook/src/domain/ingestion/test.rs
index fcf6513fb1..563274886b 100644
--- a/crates/webhook/src/domain/ingestion/test.rs
+++ b/crates/webhook/src/domain/ingestion/test.rs
@@ -544,6 +544,8 @@ fn search_only_document_event_cases() -> Vec<(&'static str, Event<DocumentTopicE
document_id: DOCUMENT_ID.to_string(),
file_type: "md".parse().expect("valid file type"),
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
},
)),
),
diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.ts
index 26cece0959..3f59c560f4 100644
--- a/packages/sdk/generated/storage/types.gen.ts
+++ b/packages/sdk/generated/storage/types.gen.ts
@@ -4422,6 +4422,11 @@ export type DocumentSubType = 'task' | 'snippet' | 'skill';
* Metadata for [`DocumentTopicEvent::SyncContentUpdated`].
*/
export type DocumentSyncContentUpdatedMetadata = {
+ /**
+ * Who mechanically changed the content. Absent on events published
+ * before attribution, and on human-only collab sessions.
+ */
+ actor?: string | null;
/**
* The id of the live-collab document whose content changed.
*/
@@ -4434,6 +4439,7 @@ export type DocumentSyncContentUpdatedMetadata = {
* File type of the sync document (markdown today).
*/
file_type: FileType;
+ on_behalf_of?: null | MacroUserIdStr;
};
/**
diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.json
index 05fcd8a2db..6a561759d8 100644
--- a/packages/sdk/specs/storage.json
+++ b/packages/sdk/specs/storage.json
@@ -18818,6 +18818,10 @@
"description": "Metadata for [`DocumentTopicEvent::SyncContentUpdated`].",
"required": ["document_id", "file_type"],
"properties": {
+ "actor": {
+ "type": ["string", "null"],
+ "description": "Who mechanically changed the content. Absent on events published\nbefore attribution, and on human-only collab sessions."
+ },
"document_id": {
"type": "string",
"description": "The id of the live-collab document whose content changed."
@@ -18829,6 +18833,17 @@
"file_type": {
"$ref": "#/components/schemas/FileType",
"description": "File type of the sync document (markdown today)."
+ },
+ "on_behalf_of": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/MacroUserIdStr",
+ "description": "The user whose feed this edit belongs on, when different from\n[`Self::actor`]."
+ }
+ ]
}
}
},
diff --git a/services/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs b/services/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs
index 3936f78f58..1590be0e20 100644
--- a/services/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs
+++ b/services/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs
@@ -78,6 +78,7 @@ pub async fn handler(
document_id,
access_level,
state.config.document_permission_jwt.as_ref(),
+ None,
)
.map_err(|e| {
tracing::error!(error=?e, "unable to encode jwt");
diff --git a/services/search_processing_service/src/api/internal/extract_sync.rs b/services/search_processing_service/src/api/internal/extract_sync.rs
index b7c1d3c335..d3f36d851d 100644
--- a/services/search_processing_service/src/api/internal/extract_sync.rs
+++ b/services/search_processing_service/src/api/internal/extract_sync.rs
@@ -18,6 +18,10 @@ pub struct SyncDocument {
#[serde(skip_serializing_if = "Option::is_none")]
pub document_version_id: Option<String>,
pub file_type: FileType,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub actor: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub on_behalf_of: Option<String>,
}
#[derive(serde::Deserialize)]
@@ -33,11 +37,13 @@ fn documents_to_events(documents: Vec<SyncDocument>) -> Vec<DocumentMacroEvent>
let document_id = document.document_id;
DocumentMacroEvent::sync_content_updated(
document_id.clone(),
- DocumentSyncContentUpdatedMetadata {
+ DocumentSyncContentUpdatedMetadata::from_extract(
document_id,
- file_type: document.file_type,
- document_version_id: document.document_version_id,
- },
+ document.file_type,
+ document.document_version_id,
+ document.actor,
+ document.on_behalf_of,
+ ),
)
})
.collect()
diff --git a/services/search_processing_service/src/api/internal/extract_sync/test.rs b/services/search_processing_service/src/api/internal/extract_sync/test.rs
index f1a75ccc78..6c1c237b48 100644
--- a/services/search_processing_service/src/api/internal/extract_sync/test.rs
+++ b/services/search_processing_service/src/api/internal/extract_sync/test.rs
@@ -11,11 +11,15 @@ fn documents_map_to_events_with_matching_keys_and_exact_metadata() {
document_id: "document-a".to_string(),
document_version_id: Some("version-42".to_string()),
file_type: FileType::Md,
+ actor: None,
+ on_behalf_of: None,
},
SyncDocument {
document_id: "document-b".to_string(),
document_version_id: None,
file_type: FileType::Pdf,
+ actor: None,
+ on_behalf_of: None,
},
]);
@@ -27,6 +31,8 @@ fn documents_map_to_events_with_matching_keys_and_exact_metadata() {
document_id: "document-a".to_string(),
file_type: FileType::Md,
document_version_id: Some("version-42".to_string()),
+ actor: None,
+ on_behalf_of: None,
})
);
assert_eq!(events[1].key(), "document-b");
@@ -36,6 +42,8 @@ fn documents_map_to_events_with_matching_keys_and_exact_metadata() {
document_id: "document-b".to_string(),
file_type: FileType::Pdf,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
})
);
}
@@ -49,6 +57,8 @@ fn documents_map_to_one_event_each_in_input_order() {
document_id: document_id.to_string(),
document_version_id: None,
file_type: FileType::Md,
+ actor: None,
+ on_behalf_of: None,
})
.collect(),
);
@@ -56,3 +66,26 @@ fn documents_map_to_one_event_each_in_input_order() {
let event_keys: Vec<&str> = events.iter().map(|event| event.key()).collect();
assert_eq!(event_keys, ["document-a", "document-c", "document-b"]);
}
+
+#[test]
+fn documents_forward_sync_attribution() {
+ let events = documents_to_events(vec![SyncDocument {
+ document_id: "document-a".to_string(),
+ document_version_id: None,
+ file_type: FileType::Md,
+ actor: Some("bot|00000000-0000-0000-0000-00000000a1a1".to_string()),
+ on_behalf_of: Some("macro|[email protected]".to_string()),
+ }]);
+
+ let DocumentTopicEvent::SyncContentUpdated(metadata) = &events[0].event().event else {
+ panic!("expected sync_content_updated");
+ };
+ assert_eq!(
+ metadata.actor.as_ref().map(|actor| actor.as_ref()),
+ Some("bot|00000000-0000-0000-0000-00000000a1a1")
+ );
+ assert_eq!(
+ metadata.on_behalf_of.as_ref().map(|user| user.as_ref()),
+ Some("macro|[email protected]")
+ );
+}
diff --git a/services/search_processing_service/src/inbound/kafka_consumer/test.rs b/services/search_processing_service/src/inbound/kafka_consumer/test.rs
index 571cc57549..f5483b9a4f 100644
--- a/services/search_processing_service/src/inbound/kafka_consumer/test.rs
+++ b/services/search_processing_service/src/inbound/kafka_consumer/test.rs
@@ -913,6 +913,8 @@ fn document_event_cases() -> Vec<(DocumentTopicEvent, DocumentEventDescription)>
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
}),
DocumentEventDescription {
action: DocumentIndexAction::ExtractSync {
@@ -1414,6 +1416,8 @@ fn document_extraction_actions_preserve_optional_versions() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: Some("snapshot-7".to_string()),
+ actor: None,
+ on_behalf_of: None,
});
assert_eq!(
describe_document_event(&sync_content_updated).action,
@@ -1698,6 +1702,8 @@ fn exact_macro_documents_envelopes_decode_into_document_events() {
document_id: DOCUMENT_ID.to_string(),
file_type: FileType::Md,
document_version_id: None,
+ actor: None,
+ on_behalf_of: None,
}),
),
),
diff --git a/services/sync-service/src/auth.rs b/services/sync-service/src/auth.rs
index 225a795783..221375ceeb 100644
--- a/services/sync-service/src/auth.rs
+++ b/services/sync-service/src/auth.rs
@@ -33,6 +33,8 @@ pub struct AuthToken {
pub user_id: Option<String>,
document_id: String,
pub access_level: AccessLevel,
+ #[serde(default)]
+ pub actor: Option<String>,
}
impl AuthToken {
@@ -103,6 +105,7 @@ did not match expected value: {}",
user_id: None,
document_id: "TODO should be option".to_string(),
access_level: AccessLevel::Admin,
+ actor: None,
});
}
diff --git a/services/sync-service/src/durable_object.rs b/services/sync-service/src/durable_object.rs
index 65598d05a9..45656fea4b 100644
--- a/services/sync-service/src/durable_object.rs
+++ b/services/sync-service/src/durable_object.rs
@@ -99,12 +99,75 @@ macro_rules! or_unauth {
pub struct WebSocketMetadata {
pub user_id: Option<String>,
pub access_level: AccessLevel,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub actor: Option<String>,
#[serde(with = "u64_serde_strings")]
pub peer_ids: BTreeSet<u64>,
}
pub type WsMetaMap = BTreeMap<String, WebSocketMetadata>;
+/// Who a published snapshot's edits are attributed to: the non-human peer that
+/// wrote them and, when its token carried one, the user it acted for.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct EditAttribution {
+ pub actor: String,
+ pub on_behalf_of: Option<String>,
+}
+
+/// Attribution from currently-connected websocket metadata only.
+///
+/// Closed sockets must not contribute an `actor`: the isolate stays warm while
+/// anyone is connected, so a disconnected AI peer would otherwise keep
+/// attributing later human edits.
+fn edit_attribution_from_meta<'a>(
+ metas: impl IntoIterator<Item = &'a WebSocketMetadata>,
+) -> Option<EditAttribution> {
+ metas.into_iter().find_map(|meta| {
+ meta.actor.clone().map(|actor| EditAttribution {
+ actor,
+ on_behalf_of: meta.user_id.clone(),
+ })
+ })
+}
+
+/// Why a snapshot is published from `websocket_close`.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum CloseFlush {
+ /// The last peer left; the pre-attribution snapshot-on-idle behaviour.
+ LastLeave,
+ /// The attributed peer left while humans stay. Its pending edits must
+ /// publish now, while its metadata still supplies the actor; the next
+ /// alarm would publish them unattributed.
+ ActorLeft,
+}
+
+impl CloseFlush {
+ /// A human leaving while the actor stays never flushes: the next alarm
+ /// still has the actor, and flushing here would `mark_exported` and then
+ /// republish the same content on last-leave.
+ fn decide(
+ is_last_leave: bool,
+ leaving_socket_has_actor: bool,
+ should_save: bool,
+ ) -> Option<Self> {
+ if is_last_leave {
+ Some(Self::LastLeave)
+ } else if leaving_socket_has_actor && should_save {
+ Some(Self::ActorLeft)
+ } else {
+ None
+ }
+ }
+
+ fn interaction_reason(self) -> InteractionReason {
+ match self {
+ Self::LastLeave => InteractionReason::LastLeave,
+ Self::ActorLeft => InteractionReason::Edited,
+ }
+ }
+}
+
#[durable_object]
pub struct DocumentSyncSession {
state: State,
@@ -164,6 +227,70 @@ mod u64_serde_strings {
}
}
+#[cfg(test)]
+mod actor_attribution_test {
+ use super::{
+ AccessLevel, CloseFlush, EditAttribution, WebSocketMetadata, edit_attribution_from_meta,
+ };
+
+ fn meta(actor: Option<&str>, user_id: Option<&str>) -> WebSocketMetadata {
+ WebSocketMetadata {
+ user_id: user_id.map(str::to_string),
+ access_level: AccessLevel::Edit,
+ actor: actor.map(str::to_string),
+ peer_ids: Default::default(),
+ }
+ }
+
+ #[test]
+ fn ignores_sockets_without_an_actor() {
+ let human = meta(None, Some("macro|[email protected]"));
+ assert_eq!(edit_attribution_from_meta([&human]), None);
+ }
+
+ #[test]
+ fn uses_the_connected_actor_and_its_user() {
+ let stale = meta(Some("bot|stale"), Some("macro|[email protected]"));
+ let human = meta(None, Some("macro|[email protected]"));
+ assert_eq!(
+ edit_attribution_from_meta([&human]),
+ None,
+ "closed AI metadata must not be consulted"
+ );
+ assert_eq!(
+ edit_attribution_from_meta([&stale, &human]),
+ Some(EditAttribution {
+ actor: "bot|stale".to_string(),
+ on_behalf_of: Some("macro|[email protected]".to_string()),
+ })
+ );
+ }
+
+ #[test]
+ fn close_flushes_on_last_leave_or_when_the_actor_leaves_with_pending_edits() {
+ assert_eq!(
+ CloseFlush::decide(false, true, true),
+ Some(CloseFlush::ActorLeft),
+ "AI leave while a human stays must flush pending edits"
+ );
+ assert_eq!(
+ CloseFlush::decide(false, false, true),
+ None,
+ "human leave while the AI stays must not flush"
+ );
+ assert_eq!(
+ CloseFlush::decide(true, true, true),
+ Some(CloseFlush::LastLeave),
+ "last-leave publishes one attributed snapshot"
+ );
+ assert_eq!(
+ CloseFlush::decide(true, false, false),
+ Some(CloseFlush::LastLeave)
+ );
+ assert_eq!(CloseFlush::decide(false, true, false), None);
+ }
+}
+
pub struct Wsm<'a> {
dss: &'a DocumentSyncSession,
ws: &'a WebSocket,
@@ -274,7 +401,12 @@ pub fn get_ws_id(state: &State, ws: &WebSocket) -> Result<String> {
/// - every few seconds
/// - on creation
/// - on everyone being disconnected
-async fn report_new_doc_state(document_id: &str, snapshot: &[u8], env: &Env) {
+async fn report_new_doc_state(
+ document_id: &str,
+ snapshot: &[u8],
+ env: &Env,
+ attribution: Option<EditAttribution>,
+) {
if let Err(err) = DssInternalClient::new(env)
.publish_shallow_snapshot(document_id, snapshot)
.await
@@ -282,7 +414,7 @@ async fn report_new_doc_state(document_id: &str, snapshot: &[u8], env: &Env) {
warn!(error=?err, "failed to push snapshot to DSS");
}
#[cfg(feature = "search-service")]
- if let Err(err) = crate::sps::update(document_id, env).await {
+ if let Err(err) = crate::sps::update(document_id, env, attribution).await {
warn!(error=?err, "failed to update search index");
}
}
@@ -317,6 +449,41 @@ impl DocumentSyncSession {
self.state.get_websockets()
}
+ fn edit_attribution(&self) -> Option<EditAttribution> {
+ let connected: BTreeSet<String> = self
+ .state
+ .get_websockets()
+ .iter()
+ .filter_map(|ws| get_ws_id(&self.state, ws).ok())
+ .collect();
+ let map = self
+ .ws_meta_map
+ .lock("DocumentSyncSession::edit_attribution");
+ edit_attribution_from_meta(connected.iter().filter_map(|ws_id| map.get(ws_id)))
+ }
+
+ fn websocket_has_actor(&self, ws: &WebSocket) -> bool {
+ let Ok(ws_id) = get_ws_id(&self.state, ws) else {
+ return false;
+ };
+ self.ws_meta_map
+ .lock("DocumentSyncSession::websocket_has_actor")
+ .get(&ws_id)
+ .is_some_and(|meta| meta.actor.is_some())
+ }
+
+ async fn forget_websocket_metadata(&self, ws: &WebSocket) {
+ let Ok(ws_id) = get_ws_id(&self.state, ws) else {
+ return;
+ };
+ self.ws_meta_map
+ .lock("DocumentSyncSession::forget_websocket_metadata")
+ .remove(&ws_id);
+ if let Err(err) = self.state.storage().delete(&ws_id).await {
+ warn!(error=?err, ws_id, "failed to delete websocket metadata");
+ }
+ }
+
pub fn push_blame_events(&self, events: Vec<crate::d1::BlameEvent>) {
if events.is_empty() {
return;
@@ -477,8 +644,9 @@ impl DocumentSyncSession {
}
let document_id_owned = document_id.to_string();
let env = self.env.clone();
+ let attribution = self.edit_attribution();
self.state.wait_until(async move {
- report_new_doc_state(&document_id_owned, &snapshot, &env).await;
+ report_new_doc_state(&document_id_owned, &snapshot, &env, attribution).await;
});
}
@@ -666,6 +834,7 @@ impl DocumentSyncSession {
let ws_meta = WebSocketMetadata {
user_id: claims.user_id,
access_level: claims.access_level,
+ actor: claims.actor,
peer_ids: Default::default(),
};
@@ -1081,11 +1250,12 @@ impl DurableObject for DocumentSyncSession {
let document_id = self.document_id().await.ok();
let env = self.env.clone();
+ let attribution = self.edit_attribution();
self.state.wait_until(async move {
if let Some(document_id) = document_id
&& let Ok(snapshot) = doc_state.export_shallow_snapshot()
{
- report_new_doc_state(&document_id, &snapshot, &env).await;
+ report_new_doc_state(&document_id, &snapshot, &env, attribution).await;
report_interaction(&document_id, &env, InteractionReason::Edited).await;
}
});
@@ -1135,17 +1305,30 @@ impl DurableObject for DocumentSyncSession {
.context("failed to broadcast awareness")?;
}
- if self.state.get_websockets().len() == 1
+ // The closing socket still counts as connected here, so its actor
+ // is still part of the attribution.
+ let state = self.document_state().await.ok();
+ let flush = CloseFlush::decide(
+ self.state.get_websockets().len() == 1,
+ self.websocket_has_actor(&ws),
+ state.as_ref().is_some_and(|s| s.should_save()),
+ );
+ if let Some(flush) = flush
+ && let Some(state) = state
&& let Ok(document_id) = self.document_id().await
- && let Ok(state) = self.document_state().await
&& let Ok(snapshot) = state.export_shallow_snapshot()
{
+ if flush == CloseFlush::ActorLeft {
+ state.mark_exported();
+ }
+ let attribution = self.edit_attribution();
let env = self.env.clone();
self.state.wait_until(async move {
- report_new_doc_state(&document_id, &snapshot, &env).await;
- report_interaction(&document_id, &env, InteractionReason::LastLeave).await;
+ report_new_doc_state(&document_id, &snapshot, &env, attribution).await;
+ report_interaction(&document_id, &env, flush.interaction_reason()).await;
});
}
+ self.forget_websocket_metadata(&ws).await;
Ok(())
})
.await
diff --git a/services/sync-service/src/sps.rs b/services/sync-service/src/sps.rs
index 49516a56a7..481150a845 100644
--- a/services/sync-service/src/sps.rs
+++ b/services/sync-service/src/sps.rs
@@ -1,9 +1,14 @@
use crate::constants::header_names::MACRO_INTERNAL_AUTH_KEY_HEADER_KEY;
+use crate::durable_object::EditAttribution;
use serde_json::json;
use tracing::{debug, error};
use worker::{Fetch, Method, Request, RequestInit};
-pub async fn update(document_id: &str, env: &worker::Env) -> worker::Result<()> {
+pub async fn update(
+ document_id: &str,
+ env: &worker::Env,
+ attribution: Option<EditAttribution>,
+) -> worker::Result<()> {
let internal_auth_key = env
.secret("SPS_API_SECRET_KEY")
.inspect_err(|e| error!(error=%e, "Could not find API SPS key binding"))?
@@ -18,6 +23,8 @@ pub async fn update(document_id: &str, env: &worker::Env) -> worker::Result<()>
"documents": [{
"document_id": document_id,
"file_type": "md",
+ "actor": attribution.as_ref().map(|a| &a.actor),
+ "on_behalf_of": attribution.as_ref().and_then(|a| a.on_behalf_of.as_ref()),
}]
});