Repository navigation
chore: absorb mqtt into packages/mqtt - #13892
Conversation
The adapter pins worker_num to 1, so a single process holds every connection and a plain array is a complete view. Apps that scale workers keep their own authoritative connection state (as the Appwrite MQTT messaging adapter does), so the transport list is a local convenience — no need for a Swoole\Table's cross-worker guarantee.
The transport is a byte pipe: start/shutdown/send/close plus the lifecycle hooks. It no longer tracks which fds are connected — an application that needs that keeps its own map, learning of a client from the CONNECT packet (onReceive) and of a drop from onClose, keyed by whatever domain state it attaches to each fd. Removes getConnections from the Adapter contract, the Swoole registry, and the Server wrapper.
outputs: type=cacheonly discarded the image that load: true and the mqtt-<php>-dev tag were meant to export, so docker compose up rebuilt it from scratch on every job. Drop the cacheonly output; the image is now loaded and compose reuses it, while cache-from/cache-to still warm the gha layer cache.
Mqtt broker
Feat/disconnect reason string
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Absorbs are pure moves: revert Rector rewrites to src/ and skip the rules that made them, so src/ matches 0.0.2 apart from the path move and Pint import ordering. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
# Conflicts: # composer.lock
|
|
||
| [$protocolName, $offset] = Packet::readString($packet->body, 0); | ||
| $this->assertSame('MQTT', $protocolName); | ||
| $this->assertSame(V3::PROTOCOL_LEVEL, ord($packet->body[$offset])); |
There was a problem hiding this comment.
Tests mirror implementation values The expected protocol level comes from the same constant used to encode the packet. If that constant changes incorrectly, this test still passes. The same pattern appears in
V5Test.php:69; ConnectionTest.php:26 also repeats an internal deadline calculation instead of testing keep-alive behavior. The repository requires tests to check observable behavior, not mirror source values or configuration. This requirement must be satisfied before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/V3Test.php
Line: 56
Comment:
**Tests mirror implementation values** The expected protocol level comes from the same constant used to encode the packet. If that constant changes incorrectly, this test still passes. The same pattern appears in `V5Test.php:69`; `ConnectionTest.php:26` also repeats an internal deadline calculation instead of testing keep-alive behavior. The repository requires tests to check observable behavior, not mirror source values or configuration. This requirement must be satisfied before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| $adapter | ||
| ->onStart(fn (): int => print("mqtt broker started\n")) | ||
| ->onReceive(function (int $fd, string $data) use ($adapter, $matches, &$protocol, &$subscriptions): void { | ||
| $packet = Packet::parse($data); |
There was a problem hiding this comment.
E2E bypasses the shipped broker This fixture handles packets and subscriptions itself rather than running
Utopia\Mqtt\Server. As a result, passing TCP and WebSocket tests do not check the shipped broker’s CONNECT decisions, subscription recording, keep-alive reaping, or QoS replay. A regression in those paths could go unnoticed; exercise the actual Server with a test Handler in at least one session.
Knowledge Base Used: MQTT protocol gateway
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/Fixtures/Swoole/server.php
Line: 52-55
Comment:
**E2E bypasses the shipped broker** This fixture handles packets and subscriptions itself rather than running `Utopia\Mqtt\Server`. As a result, passing TCP and WebSocket tests do not check the shipped broker’s CONNECT decisions, subscription recording, keep-alive reaping, or QoS replay. A regression in those paths could go unnoticed; exercise the actual `Server` with a test `Handler` in at least one session.
**Knowledge Base Used:** [MQTT protocol gateway](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/mqtt-protocol-gateway.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.|
|
||
| # The dollar signs below belong to PHP, not the shell. | ||
| # shellcheck disable=SC2016 | ||
| until php -r '$socket = @fsockopen("127.0.0.1", (int) $argv[1]); if ($socket === false) { exit(1); } fclose($socket);' "$port"; do |
There was a problem hiding this comment.
Port checks accept stale brokers If a previous fixture is still listening on these fixed ports, the new broker can fail to bind while this check succeeds. The tests then run against the old process, and cleanup kills only the newly launched PID. Check that this run’s broker owns the listeners, or isolate its ports, so a passing run tests the current code.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/e2e.sh
Line: 21
Comment:
**Port checks accept stale brokers** If a previous fixture is still listening on these fixed ports, the new broker can fail to bind while this check succeeds. The tests then run against the old process, and cleanup kills only the newly launched PID. Check that this run’s broker owns the listeners, or isolate its ports, so a passing run tests the current code.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| ```bash | ||
| composer test | ||
| composer test:e2e | ||
| ``` | ||
|
|
||
| ## Copyright and license |
There was a problem hiding this comment.
Standalone test commands lack PHPUnit The README presents
composer test and composer test:e2e as package-local commands, but installing this package’s development dependencies does not install PHPUnit. Both commands need a separately supplied phpunit binary. Document the root-monorepo commands or that standalone prerequisite so readers can run the advertised tests.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/README.md
Line: 235-240
Comment:
**Standalone test commands lack PHPUnit** The README presents `composer test` and `composer test:e2e` as package-local commands, but installing this package’s development dependencies does not install PHPUnit. Both commands need a separately supplied `phpunit` binary. Document the root-monorepo commands or that standalone prerequisite so readers can run the advertised tests.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| "require-dev": { | ||
| "swoole/ide-helper": "5.1.2" | ||
| } |
There was a problem hiding this comment.
Swoole Missing From Test Requirements
The new E2E script starts a Swoole broker, but this package does not declare ext-swoole. The package CI job installs extensions listed in the package manifest. If its runner does not already have Swoole, the broker cannot start and the E2E suite fails before testing MQTT. Declare the test-time extension requirement so CI provisions it.
| "require-dev": { | |
| "swoole/ide-helper": "5.1.2" | |
| } | |
| "require-dev": { | |
| "ext-swoole": "*", | |
| "swoole/ide-helper": "5.1.2" | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/composer.json
Line: 33-35
Comment:
**Swoole Missing From Test Requirements**
The new E2E script starts a Swoole broker, but this package does not declare `ext-swoole`. The package CI job installs extensions listed in the package manifest. If its runner does not already have Swoole, the broker cannot start and the E2E suite fails before testing MQTT. Declare the test-time extension requirement so CI provisions it.
```suggestion
"require-dev": {
"ext-swoole": "*",
"swoole/ide-helper": "5.1.2"
}
```
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| private function onWire(Disconnect $disconnect): array | ||
| { | ||
| $packet = Packet::parse(V5::disconnect($disconnect->reasonCode, $disconnect->properties)); | ||
| $this->assertSame(Packet::DISCONNECT, $packet->type); | ||
|
|
||
| [$properties] = Properties::parse($packet->body, 1); // past the reason code byte |
There was a problem hiding this comment.
onWire() uses the production V5 encoder to build a packet, then uses production parsing to interpret it. Both can agree on the same incorrect wire format while the test stays green. The repository requires tests to check observable behavior, not mirror the implementation. This requirement must be satisfied before merging; use an independent wire expectation or peer.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/DisconnectTest.php
Line: 53-58
Comment:
**Disconnect Test Checks Itself**
`onWire()` uses the production V5 encoder to build a packet, then uses production parsing to interpret it. Both can agree on the same incorrect wire format while the test stays green. The repository requires tests to check observable behavior, not mirror the implementation. This requirement must be satisfied before merging; use an independent wire expectation or peer.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| $this->send(V5::connect('ws-client', 60, true, $this->auth())); | ||
| $connack = $this->receive(); | ||
| $this->assertSame(Packet::CONNACK, $connack->type); | ||
| $this->assertSame(V5::REASON_SUCCESS, ord($connack->body[1])); | ||
|
|
||
| $this->send(V5::subscribe(1, ['sensors/+/temp'], 1)); | ||
| $suback = $this->receive(); | ||
| $this->assertSame(Packet::SUBACK, $suback->type); | ||
| $this->assertSame(chr(Packet::QOS_1), substr($suback->body, 3, 1)); | ||
|
|
||
| $this->send(Packet::pingreq()); | ||
| $this->assertSame(Packet::PINGRESP, $this->receive()->type); |
There was a problem hiding this comment.
Reassembly Paths Remain Untested
This test sends each complete MQTT packet in its own WebSocket frame, although it claims to cover reassembly. It never splits a packet across frames or combines packets in one frame, so the E2E suite would miss a regression in either path. Add exchanges that exercise both cases.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/E2E/WebSocketAdapterTest.php
Line: 43-54
Comment:
**Reassembly Paths Remain Untested**
This test sends each complete MQTT packet in its own WebSocket frame, although it claims to cover reassembly. It never splits a packet across frames or combines packets in one frame, so the E2E suite would miss a regression in either path. Add exchanges that exercise both cases.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
# Conflicts: # composer.lock
The package job reads ext-* from require, require-dev and suggest to configure setup-php; websocket declares Swoole the same way. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # composer.lock # rfc/monorepo.md
# Conflicts: # composer.lock
| $encoded = Packet::encodeLength($value); | ||
| [$decoded, $bytes] = Packet::decodeLength($encoded, 0); | ||
|
|
||
| $this->assertSame($value, $decoded); | ||
| $this->assertSame(strlen($encoded), $bytes); |
There was a problem hiding this comment.
Implementation-coupled round-trip test
testVariableLengthRoundTrips decodes bytes produced by the package’s own encoder, so matching mistakes in both methods could leave it green. PropertiesTest uses the same pattern for property encoding and parsing. The repository requires tests to check observable behavior rather than mirror the implementation; this requirement must be satisfied before merging. Use an independent wire expectation or an observable exchange.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mqtt/tests/PacketTest.php
Line: 37-41
Comment:
**Implementation-coupled round-trip test** `testVariableLengthRoundTrips` decodes bytes produced by the package’s own encoder, so matching mistakes in both methods could leave it green. `PropertiesTest` uses the same pattern for property encoding and parsing. The repository requires tests to check observable behavior rather than mirror the implementation; this requirement must be satisfied before merging. Use an independent wire expectation or an observable exchange.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
# Conflicts: # composer.lock # rfc/monorepo.md
# Conflicts: # composer.lock
Summary
This moves
utopia-php/mqttintopackages/mqtt, so Appwrite loads it directly. It's part of wave 1 in #13828, it follows the playbook inrfc/monorepo.md, and it comes in the usual three commits plus a follow-up:Add 'packages/mqtt/' from commit '44684468…': subtree import fromutopia-php/mqttwith full history. The mirror head is the0.0.2tag Appwrite already locks, so the dependency graph doesn't change.chore(mqtt): mirror plumbing:mirror.ymlreplaces the repo'slinter.ymlandtests.yml, the README gets the mirror banner,composer.lockis dropped and ignored, and the hoisted QA tooling leavesrequire-dev.refactor: load mqtt from packages/:replaceentries, drops theutopia-php/mqttrequire line and itsvcsrepository entry, and removes the package from the lock.src/Mqtt/*moves up tosrc/.tests/unit/*moves up totests/, and the namespace goes fromUtopia\Mqtt\Tests\UnittoUtopia\Mqtt\Tests.tests/e2ebecomestests/E2E, and the broker intests/servers/Swoolemoves totests/Fixtures/Swoole.docker-compose.ymland thephp-8.2/php-8.3Dockerfiles only built a PHP image to run the fixture broker. They're gone. Instead,tests/e2e.shstarts the broker with host Swoole, the same waywebsocketdoes it. The broker listens on127.0.0.1at offset ports 18830 (TCP) and 18831 (WebSocket) instead of 1883 and 9001. Scripts aretest(unit) andtest:e2e. The README's test section says the same.phpstan.neon(level max oversrcandtests) andrector.php. The package's own repo analysed it at level max, but under PHPStan 1. PHPStan 2 finds 82 findings, and they go in aphpstan-baseline.neon: 49 insrc, 33 intests. Most arechr()arguments not narrowed toint<0, 255>and casts frommixedin the packet codecs andProperty. The rest are untyped Swoole client and request fields inClientand the Swoole adapter, plus loosely typed data providers and e2e assertions. The RFC's phase 8 burn-down list now includes them.rfc/monorepo.md:mqttwasn't in the inventory, so it's added to the current-state VCS and standalone lists and to the phase 2 leaves (now 23, with 9 standalone).chore(mqtt): keep library source as released: absorbs are pure moves, as with balancer (chore: absorb balancer into packages/balancer #13886) and detector. This reverts the Rector rewrites tosrc/, andrector.phpnow skips the rules that made them:ReadOnlyPropertyRector,ClassPropertyAssignToConstructorPromotionRector,IfToNullCoalescingAssignRector,NullCoalescingOperatorRector,FunctionFirstClassCallableRectorandArrayToFirstClassCallableRector.src/now matches 0.0.2 exactly, apart from the path move and Pint's import ordering inHandler.phpandServer.php. The Rector changes in the tests stay. The baseline is still 82.There's nothing to hoist. The package requires only
utopia-php/telemetry, which already lives inpackages/telemetryand is covered byreplace. Appwrite uses it inapp/mqtt.php,src/Appwrite/Mqtt/Handler.php,src/Appwrite/Messaging/Adapter/Push/Appwrite.php,src/Appwrite/Platform/Workers/Messaging.phpandtests/e2e/Services/Mqtt/{MqttServerTest,MqttSubscriber}.php. Root PHPStan passes on all of them.Lock diff:
composer update --lockremoves onlyutopia-php/mqtt(0.0.2). It's 1 removal, with no other install or update.Validation
bin/monorepo validate: all packages validbin/monorepo check mqtt: Rector, Pint, and PHPStan level max (with baseline) passbin/monorepo test mqtt: unit 95 tests / 208 assertions, and e2e 6 tests / 47 assertions against the host Swoole fixture broker (TCP and WebSocket)vendor/bin/phpunit --testsuite packages --filter 'Utopia\\Mqtt': 95 tests pass under the root autoloader, with E2E excludedUtopia\Mqtt\Serverresolves topackages/mqtt/srcfrom the root autoloadercomposer validate --no-check-publish: valid (only the existingplatformexact-version warning)bin/monorepo split mqtt --dry-run:4c871b0e, which fast-forwards from the mirror head44684468git diff --checkMerge and follow-up
Warning
Merge with a merge commit, not a squash. A squash drops the
git-subtree-*annotation, and the split then gets rejected by the mirror.utopia-php/mqtthad a repo-levelmainruleset (id21640073) that required one approval and let org admins and a repository role bypass it, but not the split app.absorbupdated it in place to the canonical config: no required approvals, and the split app (4016286) as the only always-bypass actor.After merge:
Splitpushes toutopia-php/mqtt.utopia-php/mqtton Packagist. It was never published there, which is why Appwrite pulled it through avcsrepository. Someone with Packagist access for theutopia-phpvendor has to do this.🤖 Generated with Claude Code