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(rules): more hex ast-grep rules (#5856)

teo5072a7cc823d
diff --git a/crates/chat/src/domain/models/error.rs b/crates/chat/src/domain/models/error.rs
index 270f48aa2d..c5dce8147c 100644
--- a/crates/chat/src/domain/models/error.rs
+++ b/crates/chat/src/domain/models/error.rs
@@ -20,26 +20,3 @@ pub enum ChatErr {
#[error(transparent)]
Access(#[from] AccessError),
}
-
-#[cfg(feature = "inbound")]
-impl axum::response::IntoResponse for ChatErr {
- fn into_response(self) -> axum::response::Response {
- use axum::http::StatusCode;
-
- let (status, msg) = match &self {
- ChatErr::NotFound => (StatusCode::NOT_FOUND, "Not found"),
- ChatErr::BadRequest(_) => (StatusCode::BAD_REQUEST, "Bad request"),
- ChatErr::Access(
- AccessError::Unauthorized | AccessError::UnauthorizedWithMessage(_),
- ) => (StatusCode::FORBIDDEN, "Forbidden"),
- ChatErr::Access(AccessError::NotFound(_)) => (StatusCode::NOT_FOUND, "Not found"),
- ChatErr::Access(AccessError::BadRequest(_)) => (StatusCode::BAD_REQUEST, "Bad request"),
- ChatErr::Unknown(_) | ChatErr::Access(_) => {
- tracing::error!(error=?self, "chat handler error");
- (StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
- }
- };
-
- (status, msg.to_string()).into_response()
- }
-}
diff --git a/crates/chat/src/inbound/error_response.rs b/crates/chat/src/inbound/error_response.rs
new file mode 100644
index 0000000000..52dc047244
--- /dev/null
+++ b/crates/chat/src/inbound/error_response.rs
@@ -0,0 +1,29 @@
+//! HTTP response mapping for chat domain errors.
+//!
+//! Lives in inbound so the domain error type stays transport-free.
+
+use axum::http::StatusCode;
+use axum::response::{IntoResponse, Response};
+use entity_access::domain::models::AccessError;
+
+use crate::domain::models::ChatErr;
+
+impl IntoResponse for ChatErr {
+ fn into_response(self) -> Response {
+ let (status, msg) = match &self {
+ ChatErr::NotFound => (StatusCode::NOT_FOUND, "Not found"),
+ ChatErr::BadRequest(_) => (StatusCode::BAD_REQUEST, "Bad request"),
+ ChatErr::Access(
+ AccessError::Unauthorized | AccessError::UnauthorizedWithMessage(_),
+ ) => (StatusCode::FORBIDDEN, "Forbidden"),
+ ChatErr::Access(AccessError::NotFound(_)) => (StatusCode::NOT_FOUND, "Not found"),
+ ChatErr::Access(AccessError::BadRequest(_)) => (StatusCode::BAD_REQUEST, "Bad request"),
+ ChatErr::Unknown(_) | ChatErr::Access(_) => {
+ tracing::error!(error=?self, "chat handler error");
+ (StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
+ }
+ };
+
+ (status, msg.to_string()).into_response()
+ }
+}
diff --git a/crates/chat/src/inbound/mod.rs b/crates/chat/src/inbound/mod.rs
index fc4b308b32..33946d3835 100644
--- a/crates/chat/src/inbound/mod.rs
+++ b/crates/chat/src/inbound/mod.rs
@@ -1,5 +1,7 @@
//! Inbound adapters for the chat domain.
+mod error_response;
+
#[cfg(feature = "attachment")]
pub mod attachment;
diff --git a/crates/notification/src/domain/ports.rs b/crates/notification/src/domain/ports.rs
index b6608ae5c5..473ab585af 100644
--- a/crates/notification/src/domain/ports.rs
+++ b/crates/notification/src/domain/ports.rs
@@ -526,6 +526,20 @@ pub trait NotificationIngressQueue: Send + Sync + 'static {
) -> impl Future<Output = Result<(), Report>> + Send;
}
+/// Outbound port for delivering one VoIP push to an already-resolved
+/// endpoint. Implemented by the SNS mobile adapter; the domain's
+/// [`VoipPushSender`] service depends on this instead of the concrete
+/// adapter type.
+pub trait VoipPushDelivery: Send + Sync + 'static {
+ /// Deliver one VoIP push to the endpoint, returning the provider
+ /// message id.
+ fn send_voip_push(
+ &self,
+ endpoint_arn: &str,
+ payload: &VoipPushPayload,
+ ) -> impl Future<Output = Result<String, Report>> + Send;
+}
+
/// Port for sending VoIP push notifications (PushKit / CallKit) to iOS devices.
///
/// VoIP pushes bypass the regular notification pipeline — they are delivered
diff --git a/crates/notification/src/domain/service/voip.rs b/crates/notification/src/domain/service/voip.rs
index 277b41b1d5..eb1b162b80 100644
--- a/crates/notification/src/domain/service/voip.rs
+++ b/crates/notification/src/domain/service/voip.rs
@@ -14,29 +14,27 @@ use std::collections::HashSet;
use crate::domain::models::VoipPushTarget;
use crate::domain::models::apple::VoipPushPayload;
use crate::domain::models::mobile::DeviceEndpoint;
-use crate::domain::ports::{NotificationRepository, VoipPushSender};
-use crate::outbound::mobile::MobilePushAdapter;
-use crate::outbound::mobile::MobilePushOps;
+use crate::domain::ports::{NotificationRepository, VoipPushDelivery, VoipPushSender};
const VOIP_PUSH_DELIVERY_CONCURRENCY: usize = 32;
/// Direct APNS_VOIP sender for CallKit pushes.
-pub struct VoipPushServiceImpl<R, P> {
+pub struct VoipPushServiceImpl<R, M> {
repository: R,
- mobile: MobilePushAdapter<P>,
+ mobile: M,
}
-impl<R, P> VoipPushServiceImpl<R, P> {
+impl<R, M> VoipPushServiceImpl<R, M> {
/// Builds a sender that resolves endpoints through the repository and delivers through SNS.
- pub fn new(repository: R, mobile: MobilePushAdapter<P>) -> Self {
+ pub fn new(repository: R, mobile: M) -> Self {
Self { repository, mobile }
}
}
-impl<R, P> VoipPushSender for VoipPushServiceImpl<R, P>
+impl<R, M> VoipPushSender for VoipPushServiceImpl<R, M>
where
R: NotificationRepository,
- P: MobilePushOps + Send + Sync + 'static,
+ M: VoipPushDelivery,
{
async fn get_voip_push_targets(
&self,
diff --git a/crates/notification/src/domain/service/voip/test.rs b/crates/notification/src/domain/service/voip/test.rs
index b34c2e2536..a705d2e88f 100644
--- a/crates/notification/src/domain/service/voip/test.rs
+++ b/crates/notification/src/domain/service/voip/test.rs
@@ -287,7 +287,10 @@ impl MobilePushOps for MockPush {
}
}
-fn make_service(repo: MockRepo, push: MockPush) -> VoipPushServiceImpl<MockRepo, MockPush> {
+fn make_service(
+ repo: MockRepo,
+ push: MockPush,
+) -> VoipPushServiceImpl<MockRepo, MobilePushAdapter<MockPush>> {
let adapter = MobilePushAdapter {
push_service: push,
apns_bundle_id: "com.example.app".to_string(),
diff --git a/crates/notification/src/outbound/mobile.rs b/crates/notification/src/outbound/mobile.rs
index 10f4df24f0..8145a89787 100644
--- a/crates/notification/src/outbound/mobile.rs
+++ b/crates/notification/src/outbound/mobile.rs
@@ -214,3 +214,15 @@ fn build_voip_sns_attributes(voip_bundle_id: &str) -> HashMap<String, MessageAtt
),
])
}
+
+impl<P: MobilePushOps + Send + Sync + 'static> crate::domain::ports::VoipPushDelivery
+ for MobilePushAdapter<P>
+{
+ async fn send_voip_push(
+ &self,
+ endpoint_arn: &str,
+ payload: &VoipPushPayload,
+ ) -> Result<String, Report> {
+ MobilePushAdapter::send_voip_push(self, endpoint_arn, payload).await
+ }
+}
diff --git a/docs/STYLE_GUIDE.md b/docs/STYLE_GUIDE.md
index c32027255d..ddb706ae6a 100644
--- a/docs/STYLE_GUIDE.md
+++ b/docs/STYLE_GUIDE.md
@@ -142,6 +142,20 @@ TypeScript · `[ui]` UI / UX conventions
in the implementation file. (#4647 · also: CLAUDE.md)
- **CS-50** `[test]` Update tests and run `just prepare_db` with any db-crate change.
(also: CLAUDE.md)
+- **CS-51** `[arch]` Domain modules reference no infrastructure or transport: no AWS
+ SDKs, redis, reqwest, opensearch, kafka, axum, or http types under `src/domain/**` —
+ wrap clients in outbound adapters behind ports; response mapping lives in inbound.
+ (enforced: ast-grep `rust-no-infra-in-domain` warning,
+ `rust-no-transport-in-domain` error · also: cloud-storage-hexagonal-architecture
+ skill)
+- **CS-52** `[arch]` Dependencies point inward: `src/domain/**` never imports
+ `crate::inbound`/`crate::outbound`, and `src/outbound/**` never imports
+ `crate::inbound` — define a port in domain and implement it in the adapter.
+ (enforced: ast-grep `rust-domain-no-adapter-imports` warning,
+ `rust-outbound-no-inbound-imports` error)
+- **CS-53** `[arch]` Inbound adapters run no database queries — handlers, tools, and
+ listeners call a domain service backed by an outbound repository, never sqlx
+ directly. (enforced: ast-grep `rust-no-sqlx-in-inbound`, warning)
## Frontend and shared TypeScript (`apps/web`, `packages/`)
diff --git a/rules/ast-grep/rust-domain-no-adapter-imports.yml b/rules/ast-grep/rust-domain-no-adapter-imports.yml
new file mode 100644
index 0000000000..7dcfaec709
--- /dev/null
+++ b/rules/ast-grep/rust-domain-no-adapter-imports.yml
@@ -0,0 +1,30 @@
+id: rust-domain-no-adapter-imports
+language: rust
+severity: warning
+message: Domain modules must not import from crate::inbound or crate::outbound — dependencies point inward.
+note: |-
+ Hexagonal architecture (cloud-storage-hexagonal-architecture skill):
+ adapters depend on the domain, never the reverse. If the domain needs an
+ outbound capability, define a port trait in domain/ports and implement it
+ in outbound (see notification's VoipPushDelivery). If it needs something
+ from inbound, the type is in the wrong layer — move it into the domain.
+ Tests in domain test.rs files are exempt (they may wire real adapters).
+ 1 legacy use exists (import's domain service builds its toolsets from
+ crate::inbound::toolset) — don't add new ones.
+files:
+ - crates/**/src/domain/**
+ - services/**/src/domain/**
+ignores:
+ - '**/test.rs'
+ - '**/tests.rs'
+ - '**/test/**'
+ - '**/tests/**'
+rule:
+ any:
+ - kind: scoped_identifier
+ regex: ^crate::(inbound|outbound)\b
+ not:
+ inside:
+ kind: scoped_identifier
+ - kind: use_declaration
+ regex: ^use crate::(inbound|outbound)\b
diff --git a/rules/ast-grep/rust-no-infra-in-domain.yml b/rules/ast-grep/rust-no-infra-in-domain.yml
new file mode 100644
index 0000000000..81bad97572
--- /dev/null
+++ b/rules/ast-grep/rust-no-infra-in-domain.yml
@@ -0,0 +1,31 @@
+id: rust-no-infra-in-domain
+language: rust
+severity: warning
+message: Domain modules must not reference infrastructure clients (AWS SDKs, redis, reqwest, opensearch, kafka) — put them in outbound adapters behind a port.
+note: |-
+ Hexagonal architecture (cloud-storage-hexagonal-architecture skill): the
+ domain layer depends only on its own models and port traits. Wrap the
+ client call in an outbound adapter and expose it through a port, the same
+ way sqlx is handled (see rust-no-sqlx-in-domain). Tests in domain test.rs
+ files are exempt. 2 legacy uses exist (the DynamoDB-backed backfill job
+ registry in search_processing_service and mcp_client's reqwest transport
+ construction) — don't add new ones.
+files:
+ - crates/**/src/domain/**
+ - services/**/src/domain/**
+ignores:
+ - '**/test.rs'
+ - '**/tests.rs'
+ - '**/test/**'
+ - '**/tests/**'
+rule:
+ any:
+ - kind: scoped_identifier
+ regex: ^(aws_sdk_\w+|aws_config|redis|reqwest|opensearch|rdkafka)\b
+ not:
+ inside:
+ kind: scoped_identifier
+ - kind: scoped_type_identifier
+ regex: ^(aws_sdk_\w+|aws_config|redis|reqwest|opensearch|rdkafka)\b
+ - kind: use_declaration
+ regex: ^use (aws_sdk_\w+|aws_config|redis|reqwest|opensearch|rdkafka)\b
diff --git a/rules/ast-grep/rust-no-sqlx-in-inbound.yml b/rules/ast-grep/rust-no-sqlx-in-inbound.yml
new file mode 100644
index 0000000000..5c7744d934
--- /dev/null
+++ b/rules/ast-grep/rust-no-sqlx-in-inbound.yml
@@ -0,0 +1,31 @@
+id: rust-no-sqlx-in-inbound
+language: rust
+severity: warning
+message: Inbound adapters must not run database queries — call a domain service backed by an outbound repository instead.
+note: |-
+ Hexagonal architecture (cloud-storage-hexagonal-architecture skill): a
+ handler, tool, or listener that knows SQL is doing the domain's job.
+ Route the operation through a domain service whose repository port is
+ implemented in outbound. Tests are exempt. 8 legacy uses exist (all of
+ search_processing_service's kafka consumers plus one spot in documents'
+ axum router) — don't add new ones; the search_processing extraction is
+ a tracked follow-up.
+files:
+ - crates/**/src/inbound/**
+ - services/**/src/inbound/**
+ignores:
+ - '**/test.rs'
+ - '**/tests.rs'
+ - '**/test/**'
+ - '**/tests/**'
+rule:
+ any:
+ - kind: scoped_identifier
+ regex: ^sqlx\b
+ not:
+ inside:
+ kind: scoped_identifier
+ - kind: scoped_type_identifier
+ regex: ^sqlx\b
+ - kind: use_declaration
+ regex: ^use sqlx\b
diff --git a/rules/ast-grep/rust-no-transport-in-domain.yml b/rules/ast-grep/rust-no-transport-in-domain.yml
new file mode 100644
index 0000000000..278239161d
--- /dev/null
+++ b/rules/ast-grep/rust-no-transport-in-domain.yml
@@ -0,0 +1,28 @@
+id: rust-no-transport-in-domain
+language: rust
+severity: error
+message: Domain modules must not reference transport types (axum, http) — response mapping belongs in inbound adapters.
+note: |-
+ Hexagonal architecture (cloud-storage-hexagonal-architecture skill): the
+ domain layer must not know HTTP. Move IntoResponse impls and status-code
+ mapping into the crate's inbound module (see chat's
+ inbound/error_response.rs). Tests in domain test.rs files are exempt.
+files:
+ - crates/**/src/domain/**
+ - services/**/src/domain/**
+ignores:
+ - '**/test.rs'
+ - '**/tests.rs'
+ - '**/test/**'
+ - '**/tests/**'
+rule:
+ any:
+ - kind: scoped_identifier
+ regex: ^(axum|http)\b
+ not:
+ inside:
+ kind: scoped_identifier
+ - kind: scoped_type_identifier
+ regex: ^(axum|http)\b
+ - kind: use_declaration
+ regex: ^use (axum|http)\b
diff --git a/rules/ast-grep/rust-outbound-no-inbound-imports.yml b/rules/ast-grep/rust-outbound-no-inbound-imports.yml
new file mode 100644
index 0000000000..fa8472ef76
--- /dev/null
+++ b/rules/ast-grep/rust-outbound-no-inbound-imports.yml
@@ -0,0 +1,26 @@
+id: rust-outbound-no-inbound-imports
+language: rust
+severity: error
+message: Outbound adapters must not import from crate::inbound — adapters depend on the domain, not on each other.
+note: |-
+ Hexagonal architecture (cloud-storage-hexagonal-architecture skill): an
+ outbound adapter that needs a type defined in inbound is a sign the type
+ is transport-agnostic and belongs in the domain — move it there. Tests
+ are exempt.
+files:
+ - crates/**/src/outbound/**
+ - services/**/src/outbound/**
+ignores:
+ - '**/test.rs'
+ - '**/tests.rs'
+ - '**/test/**'
+ - '**/tests/**'
+rule:
+ any:
+ - kind: scoped_identifier
+ regex: ^crate::inbound\b
+ not:
+ inside:
+ kind: scoped_identifier
+ - kind: use_declaration
+ regex: ^use crate::inbound\b
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 5922de48e4..9bde05c02c 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -376,7 +376,7 @@ pub(crate) type CallConnectionService =
pub(crate) type DssVoipPushSender = Option<
notification::domain::service::VoipPushServiceImpl<
notification::outbound::repository::DbNotificationRepository<sqlx::PgPool>,
- aws_sdk_sns::Client,
+ notification::outbound::mobile::MobilePushAdapter<aws_sdk_sns::Client>,
>,
>;