portacode 1.3.26__tar.gz → 1.3.27.dev0__tar.gz

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.

Potentially problematic release.


This version of portacode might be problematic. Click here for more details.

Files changed (92) hide show
  1. {portacode-1.3.26 → portacode-1.3.27.dev0}/.claude/settings.local.json +1 -2
  2. portacode-1.3.27.dev0/MANIFEST.in +3 -0
  3. {portacode-1.3.26 → portacode-1.3.27.dev0}/PKG-INFO +2 -1
  4. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/_version.py +2 -2
  5. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/WEBSOCKET_PROTOCOL.md +131 -13
  6. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/base.py +13 -5
  7. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/file_handlers.py +5 -10
  8. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/handlers.py +9 -6
  9. portacode-1.3.27.dev0/portacode/static/js/test-ntp-clock.html +63 -0
  10. portacode-1.3.27.dev0/portacode/static/js/utils/ntp-clock.js +139 -0
  11. portacode-1.3.27.dev0/portacode/utils/__init__.py +1 -0
  12. portacode-1.3.27.dev0/portacode/utils/ntp_clock.py +151 -0
  13. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/PKG-INFO +2 -1
  14. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/SOURCES.txt +5 -0
  15. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/requires.txt +1 -0
  16. {portacode-1.3.26 → portacode-1.3.27.dev0}/setup.py +7 -0
  17. portacode-1.3.27.dev0/test_request_id.py +146 -0
  18. portacode-1.3.26/MANIFEST.in +0 -2
  19. {portacode-1.3.26 → portacode-1.3.27.dev0}/.claude/agents/communication-manager.md +0 -0
  20. {portacode-1.3.26 → portacode-1.3.27.dev0}/.gitignore +0 -0
  21. {portacode-1.3.26 → portacode-1.3.27.dev0}/.gitmodules +0 -0
  22. {portacode-1.3.26 → portacode-1.3.27.dev0}/LICENSE +0 -0
  23. {portacode-1.3.26 → portacode-1.3.27.dev0}/Makefile +0 -0
  24. {portacode-1.3.26 → portacode-1.3.27.dev0}/README.md +0 -0
  25. {portacode-1.3.26 → portacode-1.3.27.dev0}/backup.sh +0 -0
  26. {portacode-1.3.26 → portacode-1.3.27.dev0}/connect.py +0 -0
  27. {portacode-1.3.26 → portacode-1.3.27.dev0}/connect.sh +0 -0
  28. {portacode-1.3.26 → portacode-1.3.27.dev0}/docker-compose.yaml +0 -0
  29. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/README.md +0 -0
  30. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/__init__.py +0 -0
  31. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/__main__.py +0 -0
  32. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/cli.py +0 -0
  33. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/README.md +0 -0
  34. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/__init__.py +0 -0
  35. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/client.py +0 -0
  36. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/README.md +0 -0
  37. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/__init__.py +0 -0
  38. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/chunked_content.py +0 -0
  39. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_aware_file_handlers.py +0 -0
  40. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/README.md +0 -0
  41. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/__init__.py +0 -0
  42. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/file_system_watcher.py +0 -0
  43. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/git_manager.py +0 -0
  44. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/manager.py +0 -0
  45. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/models.py +0 -0
  46. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state/utils.py +0 -0
  47. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/project_state_handlers.py +0 -0
  48. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/registry.py +0 -0
  49. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/session.py +0 -0
  50. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/system_handlers.py +0 -0
  51. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/tab_factory.py +0 -0
  52. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/handlers/terminal_handlers.py +0 -0
  53. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/multiplex.py +0 -0
  54. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/connection/terminal.py +0 -0
  55. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/data.py +0 -0
  56. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/keypair.py +0 -0
  57. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/logging_categories.py +0 -0
  58. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode/service.py +0 -0
  59. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/dependency_links.txt +0 -0
  60. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/entry_points.txt +0 -0
  61. {portacode-1.3.26 → portacode-1.3.27.dev0}/portacode.egg-info/top_level.txt +0 -0
  62. {portacode-1.3.26 → portacode-1.3.27.dev0}/pyproject.toml +0 -0
  63. {portacode-1.3.26 → portacode-1.3.27.dev0}/restore.sh +0 -0
  64. {portacode-1.3.26 → portacode-1.3.27.dev0}/run_tests.py +0 -0
  65. {portacode-1.3.26 → portacode-1.3.27.dev0}/setup.cfg +0 -0
  66. {portacode-1.3.26 → portacode-1.3.27.dev0}/test.sh +0 -0
  67. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/README.md +0 -0
  68. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/__init__.py +0 -0
  69. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_device_online.py +0 -0
  70. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_file_operations.py +0 -0
  71. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_git_status_ui.py +0 -0
  72. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_login_flow.py +0 -0
  73. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_navigate_testing_folder.py +0 -0
  74. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_terminal_buffer_performance.py +0 -0
  75. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_terminal_interaction.py +0 -0
  76. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_terminal_loading_race_condition.py +0 -0
  77. {portacode-1.3.26 → portacode-1.3.27.dev0}/test_modules/test_terminal_start.py +0 -0
  78. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/.env.example +0 -0
  79. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/README.md +0 -0
  80. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/__init__.py +0 -0
  81. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/cli.py +0 -0
  82. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/__init__.py +0 -0
  83. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/base_test.py +0 -0
  84. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/cli_manager.py +0 -0
  85. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/hierarchical_runner.py +0 -0
  86. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/playwright_manager.py +0 -0
  87. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/runner.py +0 -0
  88. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/shared_cli_manager.py +0 -0
  89. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/core/test_discovery.py +0 -0
  90. {portacode-1.3.26 → portacode-1.3.27.dev0}/testing_framework/requirements.txt +0 -0
  91. {portacode-1.3.26 → portacode-1.3.27.dev0}/todo/issues/indefinite_resource_loading.md +0 -0
  92. {portacode-1.3.26 → portacode-1.3.27.dev0}/todo/issues/premature_terminal_exit.md +0 -0
@@ -17,7 +17,6 @@
17
17
  "Bash(git -C /home/menas/testing_folder status --porcelain)",
18
18
  "Bash(docker-compose restart:*)"
19
19
  ],
20
- "deny": [],
21
- "defaultMode": "acceptEdits"
20
+ "deny": []
22
21
  }
23
22
  }
@@ -0,0 +1,3 @@
1
+ prune server
2
+ exclude .pypirc
3
+ recursive-include portacode/static *.js *.html
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: portacode
3
- Version: 1.3.26
3
+ Version: 1.3.27.dev0
4
4
  Summary: Portacode CLI client and SDK
5
5
  Home-page: https://github.com/portacode/portacode
6
6
  Author: Meena Erian
@@ -23,6 +23,7 @@ Requires-Dist: GitPython>=3.1.45
23
23
  Requires-Dist: watchdog>=3.0
24
24
  Requires-Dist: diff-match-patch>=20230430
25
25
  Requires-Dist: Pygments>=2.14.0
26
+ Requires-Dist: ntplib>=0.4.0
26
27
  Provides-Extra: dev
27
28
  Requires-Dist: black; extra == "dev"
28
29
  Requires-Dist: flake8; extra == "dev"
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '1.3.26'
32
- __version_tuple__ = version_tuple = (1, 3, 26)
31
+ __version__ = version = '1.3.27.dev0'
32
+ __version_tuple__ = version_tuple = (1, 3, 27, 'dev0')
33
33
 
34
34
  __commit_id__ = commit_id = None
@@ -1,10 +1,38 @@
1
1
  # WebSocket Communication Protocol
2
2
 
3
- This document outlines the WebSocket communication protocol between the Portacode server and the connected client devices.
3
+ This document outlines the WebSocket communication protocol used in Portacode. The protocol involves three main participants: client sessions, the Portacode server, and devices.
4
+
5
+ ## Architecture Overview
6
+
7
+ ```
8
+ ┌─────────────┐ ┌──────────────────┐ ┌─────────────────────────┐
9
+ │ Client │ │ Portacode │ │ Device │
10
+ │ Session │◄────────►│ Server │◄────────►│ (Portacode CLI or │
11
+ │ │ │ │ │ Python package) │
12
+ └─────────────┘ └──────────────────┘ └─────────────────────────┘
13
+ │ │ │
14
+ │ │ │
15
+ Client-Side Acts as middleman Device-Side
16
+ Protocol - Routes messages Protocol
17
+ - Manages sessions
18
+ ```
19
+
20
+ The Portacode server acts as a **routing middleman** between client sessions and devices. It manages routing fields that are included in messages to specify routing destinations but are removed or transformed before reaching the final recipient:
21
+
22
+ **Routing Fields Behavior:**
23
+
24
+ - **`device_id`** (Client → Server): Client includes this to specify which device to route to. Server uses it for routing, then **removes it** before forwarding to the device (the device knows the message is for them). Server **adds it** when routing device responses back to clients (so clients know which device the message came from).
25
+
26
+ - **`client_sessions`** (Device → Server): Device includes this to specify which client session(s) to route to. Server uses it for routing, then **removes it** before forwarding to clients (clients just receive the message without seeing routing metadata).
27
+
28
+ - **`source_client_session`** (Server → Device): Server **adds this** when forwarding client commands to devices (so device knows which client sent the command and can target responses back). Clients never include this field.
29
+
30
+ This document describes the complete protocol for communicating with devices through the server, guiding app developers on how to get their client sessions to communicate with devices.
4
31
 
5
32
  ## Table of Contents
6
33
 
7
- - [Raw Message Format](#raw-message-format)
34
+ - [Raw Message Format On Device Side](#raw-message-format-on-device-side)
35
+ - [Raw Message Format On Client Side](#raw-message-format-on-client-side)
8
36
  - [Actions](#actions)
9
37
  - [Terminal Actions](#terminal-actions)
10
38
  - [`terminal_start`](#terminal_start)
@@ -82,11 +110,11 @@ This document outlines the WebSocket communication protocol between the Portacod
82
110
  - [`device_status`](#device_status)
83
111
  - [`devices`](#devices)
84
112
 
85
- ## Raw Message Format
113
+ ## Raw Message Format On Device Side
86
114
 
87
- All communication over the WebSocket is managed by a [multiplexer](./multiplex.py) that wraps every message in a JSON object with a `channel` and a `payload`. This allows for multiple virtual communication channels over a single connection.
115
+ Communication between the server and devices uses a [multiplexer](./multiplex.py) that wraps every message in a JSON object with a `channel` and a `payload`. This allows for multiple virtual communication channels over a single WebSocket connection.
88
116
 
89
- **Raw Message Structure:**
117
+ **Device-Side Message Structure:**
90
118
 
91
119
  ```json
92
120
  {
@@ -97,9 +125,99 @@ All communication over the WebSocket is managed by a [multiplexer](./multiplex.p
97
125
  }
98
126
  ```
99
127
 
100
- * `channel` (string|integer, mandatory): Identifies the virtual channel the message is for. When sending control commands to the device, they should be sent to channel 0 and when the device responsed to such control commands or sends system events, they will also be send on the zero channel. When a terminal session is created in the device, it is assigned a uuid, the uuid becomes the channel for communicating to that specific terminal.
128
+ **Field Descriptions:**
129
+
130
+ * `channel` (string|integer, mandatory): Identifies the virtual channel the message is for. When sending control commands to the device, they should be sent to channel 0 and when the device responds to such control commands or sends system events, they will also be sent on the zero channel. When a terminal session is created in the device, it is assigned a uuid, the uuid becomes the channel for communicating to that specific terminal.
101
131
  * `payload` (object, mandatory): The content of the message, which will be either an [Action](#actions) or an [Event](#events) object.
102
132
 
133
+ **Channel Types:**
134
+ - **Channel 0** (control channel): Used for system commands, terminal management, file operations, and project state management
135
+ - **Channel UUID** (terminal channel): Used for terminal I/O to a specific terminal session
136
+
137
+ ---
138
+
139
+ ## Raw Message Format On Client Side
140
+
141
+ Client sessions communicate with the server using a unified message format with the same field names as the device protocol, plus routing information.
142
+
143
+ **Client-Side Message Structure (Client → Server):**
144
+
145
+ ```json
146
+ {
147
+ "device_id": <number>,
148
+ "channel": <number|string>,
149
+ "payload": {
150
+ "cmd": "<command_name>",
151
+ ...command-specific fields
152
+ }
153
+ }
154
+ ```
155
+
156
+ **Field Descriptions:**
157
+
158
+ * `device_id` (number, mandatory): Routing field - specifies which device to send the message to. The server validates that the client has access to this device before forwarding.
159
+ * `channel` (number|string, mandatory): Same as device protocol - the target channel (0 for control, UUID for terminal). Uses the same field name for consistency.
160
+ * `payload` (object, mandatory): Same as device protocol - the command payload. Uses the same field name for consistency.
161
+
162
+ **Server Transformation (Client → Device):**
163
+
164
+ When the server receives a client message, it:
165
+ 1. Validates client has access to the specified `device_id`
166
+ 2. **Removes** `device_id` from the message (device doesn't need to be told "this is for you")
167
+ 3. **Adds** `source_client_session` to the payload (so device knows which client to respond to)
168
+ 4. Forwards to device: `{channel, payload: {...payload, source_client_session}}`
169
+
170
+ **Server Transformation (Device → Client):**
171
+
172
+ When the server receives a device response, it:
173
+ 1. **Adds** `device_id` to the message (so client knows which device it came from, based on authenticated device connection)
174
+ 2. **Removes** `client_sessions` routing metadata (clients don't need to see routing info)
175
+ 3. Routes to appropriate client session(s)
176
+
177
+ **Server Response Format (Server → Client):**
178
+
179
+ ```json
180
+ {
181
+ "event": "<event_name>",
182
+ "device_id": <number>,
183
+ ...event-specific fields
184
+ }
185
+ ```
186
+
187
+ **Field Descriptions:**
188
+
189
+ * `event` (string, mandatory): The name of the event being sent.
190
+ * `device_id` (number, mandatory): Authenticated field - identifies which device the event came from (added by server based on authenticated device connection).
191
+ * Additional fields depend on the specific event type.
192
+
193
+ **Example Client Message:**
194
+ ```json
195
+ {
196
+ "device_id": 42,
197
+ "channel": 0,
198
+ "payload": {
199
+ "cmd": "terminal_start",
200
+ "shell": "bash",
201
+ "cwd": "/home/user/project"
202
+ }
203
+ }
204
+ ```
205
+
206
+ **Example Server Response:**
207
+ ```json
208
+ {
209
+ "event": "terminal_started",
210
+ "device_id": 42,
211
+ "terminal_id": "uuid-1234-5678",
212
+ "channel": "uuid-1234-5678",
213
+ "pid": 12345
214
+ }
215
+ ```
216
+
217
+ **Note:** The server acts as a translator between the client-side and device-side protocols:
218
+ - When a client sends a command, the server transforms it from the client format to the device format
219
+ - When a device sends an event, the server adds the `device_id` and routes it to the appropriate client sessions
220
+
103
221
  ---
104
222
 
105
223
  ## Actions
@@ -110,18 +228,18 @@ Actions are messages sent from the server to the device, placed within the `payl
110
228
 
111
229
  ```json
112
230
  {
113
- "command": "<command_name>",
114
- "payload": {
115
- "arg1": "value1",
116
- "...": "..."
117
- },
231
+ "cmd": "<command_name>",
232
+ "arg1": "value1",
233
+ "arg2": "value2",
118
234
  "source_client_session": "channel.abc123"
119
235
  }
120
236
  ```
121
237
 
122
- * `command` (string, mandatory): The name of the action to be executed (e.g., `terminal_start`).
123
- * `payload` (object, mandatory): An object containing the specific arguments for the action.
238
+ **Field Descriptions:**
239
+
240
+ * `cmd` (string, mandatory): The name of the action to be executed (e.g., `terminal_start`, `file_read`, `system_info`).
124
241
  * `source_client_session` (string, mandatory): The channel name of the client session that originated this command. This field is automatically added by the server and allows devices to identify which specific client sent the command.
242
+ * Additional fields depend on the specific command (see individual command documentation below).
125
243
 
126
244
  **Note**: Actions do not require targeting information - responses are automatically routed using the client session management system.
127
245
 
@@ -107,18 +107,22 @@ class AsyncHandler(BaseHandler):
107
107
 
108
108
  async def handle(self, message: Dict[str, Any], reply_channel: Optional[str] = None) -> None:
109
109
  """Handle the command by executing it and sending the response."""
110
- logger.info("handler: Processing command %s with reply_channel=%s",
110
+ logger.info("handler: Processing command %s with reply_channel=%s",
111
111
  self.command_name, reply_channel)
112
-
112
+
113
113
  try:
114
114
  response = await self.execute(message)
115
115
  logger.info("handler: Command %s executed successfully", self.command_name)
116
-
116
+
117
117
  # Handle cases where execute() sends responses directly and returns None
118
118
  if response is not None:
119
+ # Automatically copy request_id if present in the incoming message
120
+ if "request_id" in message and "request_id" not in response:
121
+ response["request_id"] = message["request_id"]
122
+
119
123
  # Extract project_id from response for session targeting
120
124
  project_id = response.get("project_id")
121
- logger.info("handler: %s response project_id=%s, response=%s",
125
+ logger.info("handler: %s response project_id=%s, response=%s",
122
126
  self.command_name, project_id, response)
123
127
  await self.send_response(response, reply_channel, project_id)
124
128
  else:
@@ -150,7 +154,11 @@ class SyncHandler(BaseHandler):
150
154
  try:
151
155
  loop = asyncio.get_running_loop()
152
156
  response = await loop.run_in_executor(None, self.execute, message)
153
-
157
+
158
+ # Automatically copy request_id if present in the incoming message
159
+ if "request_id" in message and "request_id" not in response:
160
+ response["request_id"] = message["request_id"]
161
+
154
162
  # Extract project_id from response for session targeting
155
163
  project_id = response.get("project_id")
156
164
  await self.send_response(response, reply_channel, project_id)
@@ -382,22 +382,18 @@ class ContentRequestHandler(AsyncHandler):
382
382
  async def execute(self, message: Dict[str, Any]) -> None:
383
383
  """Return content by hash if available, chunked for large content."""
384
384
  content_hash = message.get("content_hash")
385
- request_id = message.get("request_id")
386
385
  source_client_session = message.get("source_client_session")
387
-
386
+
388
387
  if not content_hash:
389
388
  raise ValueError("content_hash parameter is required")
390
- if not request_id:
391
- raise ValueError("request_id parameter is required")
392
-
389
+
393
390
  # Check if content is in cache
394
391
  content = _content_cache.get(content_hash)
395
-
392
+
396
393
  if content is not None:
397
- # Create base response
394
+ # Create base response (request_id will be added automatically by base class)
398
395
  base_response = {
399
396
  "event": "content_response",
400
- "request_id": request_id,
401
397
  "content_hash": content_hash,
402
398
  "success": True,
403
399
  }
@@ -411,10 +407,9 @@ class ContentRequestHandler(AsyncHandler):
411
407
 
412
408
  logger.info(f"Sent content response in {len(responses)} chunk(s) for hash: {content_hash[:16]}...")
413
409
  else:
414
- # Content not found in cache
410
+ # Content not found in cache (request_id will be added automatically by base class)
415
411
  response = {
416
412
  "event": "content_response",
417
- "request_id": request_id,
418
413
  "content_hash": content_hash,
419
414
  "content": None,
420
415
  "success": False,
@@ -683,9 +683,8 @@ class ProjectStateDiffContentHandler(AsyncHandler):
683
683
  from_hash = message.get("from_hash")
684
684
  to_hash = message.get("to_hash")
685
685
  content_type = message.get("content_type") # 'original', 'modified', 'html_diff'
686
- request_id = message.get("request_id")
687
686
  source_client_session = message.get("source_client_session")
688
-
687
+
689
688
  # Validate required fields
690
689
  if not server_project_id:
691
690
  raise ValueError("project_id is required")
@@ -697,8 +696,6 @@ class ProjectStateDiffContentHandler(AsyncHandler):
697
696
  raise ValueError("to_ref is required")
698
697
  if not content_type:
699
698
  raise ValueError("content_type is required")
700
- if not request_id:
701
- raise ValueError("request_id is required")
702
699
  if not source_client_session:
703
700
  raise ValueError("source_client_session is required")
704
701
 
@@ -816,9 +813,12 @@ class ProjectStateDiffContentHandler(AsyncHandler):
816
813
  "from_ref": from_ref,
817
814
  "to_ref": to_ref,
818
815
  "content_type": content_type,
819
- "request_id": request_id,
820
816
  "success": success
821
817
  }
818
+
819
+ # Add request_id if present in original message
820
+ if "request_id" in message:
821
+ base_response["request_id"] = message["request_id"]
822
822
 
823
823
  if from_hash:
824
824
  base_response["from_hash"] = from_hash
@@ -850,9 +850,12 @@ class ProjectStateDiffContentHandler(AsyncHandler):
850
850
  "from_ref": from_ref,
851
851
  "to_ref": to_ref,
852
852
  "content_type": content_type,
853
- "request_id": request_id,
854
853
  "success": False,
855
854
  "error": str(e),
856
855
  "chunked": False
857
856
  }
857
+
858
+ # Add request_id if present in original message
859
+ if "request_id" in message:
860
+ error_response["request_id"] = message["request_id"]
858
861
  await self.send_response(error_response, project_id=server_project_id)
@@ -0,0 +1,63 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>NTP Clock Test</title>
5
+ <style>
6
+ body {
7
+ font-family: monospace;
8
+ padding: 20px;
9
+ background: #1e1e1e;
10
+ color: #d4d4d4;
11
+ }
12
+ h1 {
13
+ color: #4ec9b0;
14
+ }
15
+ #status {
16
+ background: #252526;
17
+ padding: 15px;
18
+ border-radius: 5px;
19
+ border: 1px solid #3e3e42;
20
+ }
21
+ .synced {
22
+ color: #4ec9b0;
23
+ }
24
+ .not-synced {
25
+ color: #f48771;
26
+ }
27
+ .label {
28
+ color: #9cdcfe;
29
+ font-weight: bold;
30
+ }
31
+ </style>
32
+ </head>
33
+ <body>
34
+ <h1>NTP Clock Test - time.cloudflare.com</h1>
35
+ <div id="status"></div>
36
+ <script type="module">
37
+ import ntpClock from './utils/ntp-clock.js';
38
+
39
+ function updateDisplay() {
40
+ const status = ntpClock.getStatus();
41
+ const syncClass = status.isSynced ? 'synced' : 'not-synced';
42
+
43
+ document.getElementById('status').innerHTML = `
44
+ <p><span class="label">Sync Status:</span> <span class="${syncClass}">${status.isSynced ? '✅ SYNCED' : '❌ NOT SYNCED'}</span></p>
45
+ <p><span class="label">NTP Server:</span> ${status.server}</p>
46
+ <p><span class="label">NTP Time:</span> ${ntpClock.nowISO() || 'null (not synced)'}</p>
47
+ <p><span class="label">Local Time:</span> ${new Date().toISOString()}</p>
48
+ <p><span class="label">Offset:</span> ${status.offset !== null ? status.offset.toFixed(2) + 'ms' : 'null'}</p>
49
+ <p><span class="label">Last Sync:</span> ${status.lastSync || 'Never'}</p>
50
+ <p><span class="label">Time Since Sync:</span> ${status.timeSinceSync ? (status.timeSinceSync / 1000).toFixed(0) + 's' : 'N/A'}</p>
51
+ `;
52
+ }
53
+
54
+ // Update display every 100ms
55
+ setInterval(updateDisplay, 100);
56
+
57
+ // Log status to console every 5 seconds
58
+ setInterval(() => {
59
+ console.log('NTP Clock Status:', ntpClock.getStatus());
60
+ }, 5000);
61
+ </script>
62
+ </body>
63
+ </html>
@@ -0,0 +1,139 @@
1
+ /**
2
+ * NTP Clock - Synchronized time source for distributed tracing
3
+ *
4
+ * Provides NTP-synchronized timestamps for accurate distributed tracing.
5
+ * Uses HTTP-based time API since browsers cannot make UDP NTP requests.
6
+ *
7
+ * IMPORTANT: All entities (client, server, device) MUST sync to time.cloudflare.com
8
+ * If sync fails, timestamps will be null to indicate sync failure.
9
+ */
10
+ class NTPClock {
11
+ constructor() {
12
+ this.ntpServer = 'time.cloudflare.com'; // Hardcoded - no fallback
13
+ this.offset = null; // Offset from local clock to NTP time (milliseconds), null if not synced
14
+ this.lastSync = null;
15
+ this.syncInterval = 5 * 60 * 1000; // Re-sync every 5 minutes
16
+ this._syncInProgress = false;
17
+ this._syncAttempts = 0;
18
+ this._maxSyncAttempts = 3;
19
+ }
20
+
21
+ /**
22
+ * Parse Cloudflare trace response to extract timestamp
23
+ */
24
+ _parseCloudflareTime(text) {
25
+ const tsMatch = text.match(/ts=([\d.]+)/);
26
+ if (tsMatch) {
27
+ return parseFloat(tsMatch[1]) * 1000; // Convert to milliseconds
28
+ }
29
+ throw new Error('Failed to parse Cloudflare timestamp');
30
+ }
31
+
32
+ /**
33
+ * Synchronize with Cloudflare NTP via HTTP
34
+ */
35
+ async sync() {
36
+ if (this._syncInProgress) {
37
+ console.log('NTP sync already in progress, skipping');
38
+ return false;
39
+ }
40
+
41
+ this._syncInProgress = true;
42
+ this._syncAttempts++;
43
+
44
+ try {
45
+ const t0 = performance.now();
46
+ const response = await fetch('https://cloudflare.com/cdn-cgi/trace');
47
+ const t1 = performance.now();
48
+
49
+ const text = await response.text();
50
+ const serverTime = this._parseCloudflareTime(text);
51
+
52
+ const localTime = Date.now();
53
+ const latency = (t1 - t0) / 2; // Estimate one-way latency
54
+
55
+ // Calculate offset accounting for latency
56
+ this.offset = serverTime - localTime + latency;
57
+ this.lastSync = Date.now();
58
+
59
+ console.log(
60
+ `✅ NTP sync successful: offset=${this.offset.toFixed(2)}ms, ` +
61
+ `latency=${latency.toFixed(2)}ms, server=${this.ntpServer}`
62
+ );
63
+
64
+ this._syncAttempts = 0; // Reset on success
65
+ return true;
66
+
67
+ } catch (error) {
68
+ console.warn(`❌ NTP sync failed (attempt ${this._syncAttempts}/${this._maxSyncAttempts}):`, error);
69
+
70
+ // If all attempts fail, set offset to null to indicate sync failure
71
+ if (this._syncAttempts >= this._maxSyncAttempts) {
72
+ this.offset = null;
73
+ this.lastSync = null;
74
+ console.error(`⚠️ NTP sync failed after ${this._maxSyncAttempts} attempts. Timestamps will be null.`);
75
+ this._syncAttempts = 0;
76
+ }
77
+
78
+ return false;
79
+ } finally {
80
+ this._syncInProgress = false;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Get current NTP-synchronized timestamp in milliseconds since epoch
86
+ * Returns null if not synced
87
+ */
88
+ now() {
89
+ if (this.offset === null) {
90
+ return null;
91
+ }
92
+ return Date.now() + this.offset;
93
+ }
94
+
95
+ /**
96
+ * Get current NTP-synchronized timestamp in ISO format
97
+ * Returns null if not synced
98
+ */
99
+ nowISO() {
100
+ const ts = this.now();
101
+ if (ts === null) {
102
+ return null;
103
+ }
104
+ return new Date(ts).toISOString();
105
+ }
106
+
107
+ /**
108
+ * Get sync status for debugging
109
+ */
110
+ getStatus() {
111
+ return {
112
+ server: this.ntpServer,
113
+ offset: this.offset,
114
+ lastSync: this.lastSync ? new Date(this.lastSync).toISOString() : null,
115
+ timeSinceSync: this.lastSync ? Date.now() - this.lastSync : null,
116
+ isSynced: this.offset !== null
117
+ };
118
+ }
119
+
120
+ /**
121
+ * Start automatic periodic synchronization
122
+ */
123
+ startAutoSync() {
124
+ // Initial sync
125
+ this.sync();
126
+
127
+ // Periodic re-sync
128
+ setInterval(() => {
129
+ console.log('🔄 Starting periodic NTP sync...');
130
+ this.sync();
131
+ }, this.syncInterval);
132
+ }
133
+ }
134
+
135
+ // Global instance - auto-starts sync
136
+ const ntpClock = new NTPClock();
137
+ ntpClock.startAutoSync();
138
+
139
+ export default ntpClock;
@@ -0,0 +1 @@
1
+ """Portacode utility modules."""
@@ -0,0 +1,151 @@
1
+ """
2
+ NTP Clock - Synchronized time source for distributed tracing
3
+
4
+ Provides NTP-synchronized timestamps for accurate distributed tracing.
5
+ Thread-safe implementation with automatic periodic synchronization.
6
+
7
+ IMPORTANT: All entities (client, server, device) MUST sync to time.cloudflare.com
8
+ If sync fails, timestamps will be None to indicate sync failure.
9
+ """
10
+ import ntplib
11
+ import time
12
+ import threading
13
+ import logging
14
+ from datetime import datetime, timezone
15
+ from typing import Optional
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ class NTPClock:
21
+ """Thread-safe NTP-synchronized clock."""
22
+
23
+ def __init__(self, ntp_server: str = 'time.cloudflare.com'):
24
+ """Initialize NTP clock.
25
+
26
+ Args:
27
+ ntp_server: NTP server hostname (default: time.cloudflare.com, hardcoded, no fallback)
28
+ """
29
+ self.ntp_server = ntp_server
30
+ self.offset: Optional[float] = None # Offset from local clock to NTP time (seconds), None if not synced
31
+ self.last_sync: Optional[float] = None
32
+ self.sync_interval = 300 # Re-sync every 5 minutes
33
+ self._lock = threading.Lock()
34
+ self._sync_in_progress = False
35
+ self._client = ntplib.NTPClient()
36
+ self._sync_attempts = 0
37
+ self._max_sync_attempts = 3
38
+
39
+ def sync(self) -> bool:
40
+ """Synchronize with NTP server.
41
+
42
+ Returns:
43
+ True if sync successful, False otherwise
44
+ """
45
+ if self._sync_in_progress:
46
+ logger.debug("NTP sync already in progress, skipping")
47
+ return False
48
+
49
+ self._sync_in_progress = True
50
+
51
+ try:
52
+ self._sync_attempts += 1
53
+ response = self._client.request(self.ntp_server, version=3, timeout=2)
54
+
55
+ with self._lock:
56
+ # Offset is difference between NTP time and local time
57
+ self.offset = response.offset
58
+ self.last_sync = time.time()
59
+
60
+ logger.info(
61
+ f"✅ NTP sync successful: offset={self.offset*1000:.2f}ms, "
62
+ f"latency={response.delay*1000:.2f}ms, server={self.ntp_server}"
63
+ )
64
+
65
+ self._sync_attempts = 0 # Reset on success
66
+ return True
67
+
68
+ except Exception as e:
69
+ logger.warning(f"❌ NTP sync failed (attempt {self._sync_attempts}/{self._max_sync_attempts}): {e}")
70
+
71
+ # If all attempts fail, set offset to None to indicate sync failure
72
+ if self._sync_attempts >= self._max_sync_attempts:
73
+ with self._lock:
74
+ self.offset = None
75
+ self.last_sync = None
76
+ logger.error(f"⚠️ NTP sync failed after {self._max_sync_attempts} attempts. Timestamps will be None.")
77
+ self._sync_attempts = 0
78
+
79
+ return False
80
+
81
+ finally:
82
+ self._sync_in_progress = False
83
+
84
+ def now(self) -> Optional[float]:
85
+ """Get current NTP-synchronized timestamp (seconds since epoch).
86
+
87
+ Returns:
88
+ Timestamp in seconds (Unix epoch) or None if not synced
89
+ """
90
+ with self._lock:
91
+ if self.offset is None:
92
+ return None
93
+ return time.time() + self.offset
94
+
95
+ def now_ms(self) -> Optional[int]:
96
+ """Get current NTP-synchronized timestamp in milliseconds.
97
+
98
+ Returns:
99
+ Timestamp in milliseconds (Unix epoch) or None if not synced
100
+ """
101
+ ts = self.now()
102
+ if ts is None:
103
+ return None
104
+ return int(ts * 1000)
105
+
106
+ def now_iso(self) -> Optional[str]:
107
+ """Get current NTP-synchronized timestamp in ISO format.
108
+
109
+ Returns:
110
+ ISO 8601 formatted timestamp with UTC timezone or None if not synced
111
+ """
112
+ ts = self.now()
113
+ if ts is None:
114
+ return None
115
+ dt = datetime.fromtimestamp(ts, tz=timezone.utc)
116
+ return dt.isoformat()
117
+
118
+ def get_status(self) -> dict:
119
+ """Get sync status for debugging.
120
+
121
+ Returns:
122
+ Dictionary with sync status information
123
+ """
124
+ with self._lock:
125
+ return {
126
+ 'server': self.ntp_server,
127
+ 'offset_ms': self.offset * 1000 if self.offset is not None else None,
128
+ 'last_sync': datetime.fromtimestamp(self.last_sync, tz=timezone.utc).isoformat() if self.last_sync else None,
129
+ 'time_since_sync_sec': time.time() - self.last_sync if self.last_sync else None,
130
+ 'is_synced': self.offset is not None
131
+ }
132
+
133
+ def start_auto_sync(self):
134
+ """Start automatic periodic synchronization in background thread."""
135
+ # Initial sync
136
+ self.sync()
137
+
138
+ def _sync_loop():
139
+ while True:
140
+ time.sleep(self.sync_interval)
141
+ logger.info("🔄 Starting periodic NTP sync...")
142
+ self.sync()
143
+
144
+ thread = threading.Thread(target=_sync_loop, daemon=True, name='ntp-sync')
145
+ thread.start()
146
+ logger.info(f"Started NTP auto-sync thread (interval: {self.sync_interval}s, server: {self.ntp_server})")
147
+
148
+
149
+ # Global instance - auto-starts sync on import
150
+ ntp_clock = NTPClock()
151
+ ntp_clock.start_auto_sync()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: portacode
3
- Version: 1.3.26
3
+ Version: 1.3.27.dev0
4
4
  Summary: Portacode CLI client and SDK
5
5
  Home-page: https://github.com/portacode/portacode
6
6
  Author: Meena Erian
@@ -23,6 +23,7 @@ Requires-Dist: GitPython>=3.1.45
23
23
  Requires-Dist: watchdog>=3.0
24
24
  Requires-Dist: diff-match-patch>=20230430
25
25
  Requires-Dist: Pygments>=2.14.0
26
+ Requires-Dist: ntplib>=0.4.0
26
27
  Provides-Extra: dev
27
28
  Requires-Dist: black; extra == "dev"
28
29
  Requires-Dist: flake8; extra == "dev"
@@ -13,6 +13,7 @@ restore.sh
13
13
  run_tests.py
14
14
  setup.py
15
15
  test.sh
16
+ test_request_id.py
16
17
  .claude/settings.local.json
17
18
  .claude/agents/communication-manager.md
18
19
  portacode/README.md
@@ -56,6 +57,10 @@ portacode/connection/handlers/project_state/handlers.py
56
57
  portacode/connection/handlers/project_state/manager.py
57
58
  portacode/connection/handlers/project_state/models.py
58
59
  portacode/connection/handlers/project_state/utils.py
60
+ portacode/static/js/test-ntp-clock.html
61
+ portacode/static/js/utils/ntp-clock.js
62
+ portacode/utils/__init__.py
63
+ portacode/utils/ntp_clock.py
59
64
  test_modules/README.md
60
65
  test_modules/__init__.py
61
66
  test_modules/test_device_online.py
@@ -9,6 +9,7 @@ GitPython>=3.1.45
9
9
  watchdog>=3.0
10
10
  diff-match-patch>=20230430
11
11
  Pygments>=2.14.0
12
+ ntplib>=0.4.0
12
13
 
13
14
  [:platform_system == "Windows"]
14
15
  pywinpty>=2.0
@@ -17,6 +17,12 @@ setup(
17
17
  author_email="hi@menas.pro",
18
18
  url="https://github.com/portacode/portacode",
19
19
  packages=find_packages(exclude=("tests", "server")),
20
+ package_data={
21
+ "portacode": [
22
+ "static/js/**/*.js",
23
+ "static/js/**/*.html",
24
+ ],
25
+ },
20
26
  python_requires=">=3.8",
21
27
  install_requires=[
22
28
  "click>=8.0",
@@ -31,6 +37,7 @@ setup(
31
37
  "watchdog>=3.0",
32
38
  "diff-match-patch>=20230430",
33
39
  "Pygments>=2.14.0",
40
+ "ntplib>=0.4.0",
34
41
  ],
35
42
  extras_require={
36
43
  "dev": ["black", "flake8", "pytest"],
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test request_id automatic passthrough in handlers
4
+ """
5
+ import asyncio
6
+ import sys
7
+ import os
8
+
9
+ # Add parent directory to path
10
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
11
+
12
+ from portacode.connection.handlers.terminal_handlers import TerminalStartHandler
13
+ from portacode.connection.handlers.file_handlers import ContentRequestHandler
14
+
15
+
16
+ class MockChannel:
17
+ """Mock channel for testing"""
18
+ def __init__(self):
19
+ self.sent_messages = []
20
+
21
+ async def send(self, data):
22
+ self.sent_messages.append(data)
23
+ print(f"📤 Sent: {data}")
24
+
25
+
26
+ class MockSessionManager:
27
+ """Mock session manager"""
28
+ async def create_session(self, shell=None, cwd=None, project_id=None):
29
+ return {
30
+ "terminal_id": "test_terminal_123",
31
+ "channel": "test_channel_456",
32
+ "pid": 12345,
33
+ "shell": shell or "bash",
34
+ "cwd": cwd,
35
+ "project_id": project_id
36
+ }
37
+
38
+
39
+ async def test_terminal_start_with_request_id():
40
+ """Test that terminal_start now automatically includes request_id"""
41
+ print("\n" + "=" * 70)
42
+ print("TEST 1: terminal_start with request_id")
43
+ print("=" * 70)
44
+
45
+ mock_channel = MockChannel()
46
+ context = {
47
+ "session_manager": MockSessionManager(),
48
+ "client_session_manager": None
49
+ }
50
+
51
+ handler = TerminalStartHandler(mock_channel, context)
52
+
53
+ # Message with request_id
54
+ message = {
55
+ "cmd": "terminal_start",
56
+ "shell": "bash",
57
+ "request_id": "req_test_001"
58
+ }
59
+
60
+ print(f"📥 Sending message: {message}")
61
+
62
+ await handler.handle(message)
63
+
64
+ # Check response
65
+ assert len(mock_channel.sent_messages) > 0, "No response sent"
66
+ response = mock_channel.sent_messages[0]
67
+
68
+ print(f"📬 Response: {response}")
69
+
70
+ # Verify request_id was automatically added
71
+ assert "request_id" in response, "❌ request_id not found in response"
72
+ assert response["request_id"] == "req_test_001", f"❌ request_id mismatch: {response['request_id']}"
73
+ assert response["event"] == "terminal_started", f"❌ Wrong event: {response['event']}"
74
+
75
+ print("✅ TEST PASSED: request_id automatically added to terminal_started response")
76
+
77
+
78
+ async def test_terminal_start_without_request_id():
79
+ """Test backward compatibility - terminal_start without request_id"""
80
+ print("\n" + "=" * 70)
81
+ print("TEST 2: terminal_start without request_id (backward compatibility)")
82
+ print("=" * 70)
83
+
84
+ mock_channel = MockChannel()
85
+ context = {
86
+ "session_manager": MockSessionManager(),
87
+ "client_session_manager": None
88
+ }
89
+
90
+ handler = TerminalStartHandler(mock_channel, context)
91
+
92
+ # Message without request_id
93
+ message = {
94
+ "cmd": "terminal_start",
95
+ "shell": "bash"
96
+ }
97
+
98
+ print(f"📥 Sending message: {message}")
99
+
100
+ await handler.handle(message)
101
+
102
+ # Check response
103
+ assert len(mock_channel.sent_messages) > 0, "No response sent"
104
+ response = mock_channel.sent_messages[0]
105
+
106
+ print(f"📬 Response: {response}")
107
+
108
+ # Verify request_id is NOT in response
109
+ assert "request_id" not in response, f"❌ request_id should not be in response: {response}"
110
+ assert response["event"] == "terminal_started", f"❌ Wrong event: {response['event']}"
111
+
112
+ print("✅ TEST PASSED: No request_id in response when not in request")
113
+
114
+
115
+ async def main():
116
+ """Run all tests"""
117
+ print("\n🧪 Testing Centralized request_id Handling")
118
+ print("=" * 70)
119
+
120
+ try:
121
+ await test_terminal_start_with_request_id()
122
+ await test_terminal_start_without_request_id()
123
+
124
+ print("\n" + "=" * 70)
125
+ print("🎉 ALL TESTS PASSED")
126
+ print("=" * 70)
127
+ print("\nSummary:")
128
+ print("✅ request_id automatically passed through when present")
129
+ print("✅ Backward compatible - works without request_id")
130
+ print("✅ terminal_start now supports request_id (previously didn't)")
131
+
132
+ return 0
133
+
134
+ except AssertionError as e:
135
+ print(f"\n❌ TEST FAILED: {e}")
136
+ return 1
137
+ except Exception as e:
138
+ print(f"\n❌ ERROR: {e}")
139
+ import traceback
140
+ traceback.print_exc()
141
+ return 1
142
+
143
+
144
+ if __name__ == "__main__":
145
+ exit_code = asyncio.run(main())
146
+ sys.exit(exit_code)
@@ -1,2 +0,0 @@
1
- prune server
2
- exclude .pypirc
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes