Repository navigation
Expand file tree
/
Copy pathobservation.lua
More file actions
740 lines (685 loc) · 24.6 KB
/
Copy pathobservation.lua
File metadata and controls
740 lines (685 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
local M = {}
local resource_names = {
session = true,
children = true,
messages = true,
inbox = true,
execution = true,
permissions = true,
questions = true,
files = true,
}
---@alias OpencodeObservedResource 'session'|'children'|'messages'|'inbox'|'execution'|'permissions'|'questions'|'files'
---@class OpencodeObservationSync
---@field state 'unread'|'loading'|'current'|'stale'|'error'|'unsupported'
---@field error? string|table
---@class OpencodeMessage
---@field id string Stable message identifier
---@field kind string Message kind, such as user or assistant
---@field content table[] Message content parts
---@field queued? boolean User input awaiting assistant consumption, projected by the protocol adapter
---@class OpencodePermissionPreview
---@field title? string Request title, independent of tool description
---@field command? string Exact command requiring approval
---@field diff? string Unified diff requiring approval
---@field path? string Target file path
---@field source? {message_id: string, call_id: string} Linked tool supplying preview details
---@class OpencodeObservationState
---@field session OpencodeSession
---@field sync table<OpencodeObservedResource, OpencodeObservationSync>
---@field children {by_id: table<string, table|nil>, order: string[]}
---@field entries_by_id table<string, OpencodeMessage|nil>
---@field entry_order string[]
---@field inbox {items_by_id: table<string, table|nil>, order: string[]}
---@field execution {activity: string, last_outcome?: string, last_idle?: number, retry?: table, error?: table}
---@field permission_requests_by_id table<string, {id: string, status: string, choices: table[], answer?: string, preview?: OpencodePermissionPreview}|nil>
---@field question_requests_by_id table<string, {id: string, status: string, unavailable_reason?: string, fields: table[], answers?: table}|nil>
---@field files {revision: integer, path?: string, change?: string, last?: {path: string, event: string}}
---Adapters interpret native payloads; the shared lifecycle owns requests and publication.
---@class OpencodeObservationRuntime
---@field name string
---@field request_resource fun(observation: OpencodeObservation, resource: OpencodeObservedResource): Promise<any>
---@field apply_resource fun(observation: OpencodeObservation, resource: OpencodeObservedResource, value: any) Validate and commit a snapshot; must not publish it
---@field route_event fun(connection: table, event: table) Commit native event data, then call _event_changed for affected resources
---@field refresh_after_event fun(resource: OpencodeObservedResource, sync: table): boolean Whether published events leave the resource needing a fresh snapshot
---@field find_reply? fun(observation: OpencodeObservation, input_id: string): table|nil
---@field local_resource? fun(resource: OpencodeObservedResource): boolean
---@field stream_resource? fun(resource: OpencodeObservedResource): boolean
---@field operations_need_stream? boolean Defaults to true
---@field on_release_resource? fun(observation: OpencodeObservation, resource: OpencodeObservedResource)
---@field on_unused? fun(observation: OpencodeObservation)
---@field on_stream_error? fun(observation: OpencodeObservation, message: string)
---@field on_close? fun(observation: OpencodeObservation)
---@class OpencodeMessageSelection
---@field mode? string
---@field model? string
---@field variant? string
---@field default_mode? string
---@field default_model? string Server config model, used when no model is selected
---@field available_agents? string[] Primary agents; only these may update the tab mode
---@class OpencodeObservation
---@field _connection table
---@field _session_id string
---@field _session_ref table
---@field _state OpencodeObservationState Mutable normalized state, owned by this observation
---@field _runtime OpencodeObservationRuntime
---@field _watchers table<table, boolean>
---@field _local_operations integer Operations retain the observation even without watchers
---@field _loading table<OpencodeObservedResource, {revision: integer}|nil> One active snapshot token per resource
---@field _event_revisions table<OpencodeObservedResource, integer>
---@field reply_permission fun(self: OpencodeObservation, request_id: string, answer: {choice: 'once'|'always'|'reject', message?: string}): Promise<boolean>
---@field validate_message_options fun(self: OpencodeObservation, opts: SendMessageOpts, default_system?: string)
---@field prepare_message fun(self: OpencodeObservation, opts: SendMessageOpts, selected: OpencodeMessageSelection): table, OpencodeSessionTabModelUpdate
---@field needs_message_defaults? boolean Caller must fill default_model/available_agents in the selection
local Observation = {}
Observation.__index = Observation
function Observation.validate_message_options(_) end
function Observation.prepare_message(_)
return {}, {}
end
--- Decode an editor-context payload (selection / diagnostics / cursor-data /
--- file-content / git-diff) into the protocol-neutral contract entry.
--- Both protocol adapters map their wire shapes onto this one: V1 carries it
--- as a synthetic text part with metadata.context_type (or a validated legacy
--- JSON context_type declaration), V2 as a file
--- attachment whose name is prefixed with "editor-context:".
--- @param context_type string the wire-declared context type
--- @param text string JSON payload for selection/diagnostics/cursor-data,
--- plain text for file-content/git-diff
--- @param part_id string stable identity for the rendered entry
--- @param synthetic boolean|nil
--- @param ignored boolean|nil
--- @return table|nil entry
--- @return string|nil err
function M.decode_editor_context(context_type, text, part_id, synthetic, ignored)
local base = { id = part_id, kind = 'editor_context', synthetic = synthetic, ignored = ignored }
if context_type == 'file-content' then
base.source = { kind = 'buffer', media_type = 'text/plain' }
base.text = text
return base
end
if context_type == 'git-diff' then
base.source = { kind = 'git_diff' }
base.text = text
return base
end
if
context_type ~= 'selection'
and context_type ~= 'review-comment'
and context_type ~= 'diagnostics'
and context_type ~= 'cursor-data'
then
return nil, 'unsupported editor context type: ' .. tostring(context_type)
end
local ok, decoded = pcall(vim.json.decode, text)
if not ok or type(decoded) ~= 'table' or decoded.context_type ~= context_type then
return nil, 'invalid ' .. tostring(context_type) .. ' editor context JSON'
end
local file_name = type(decoded.file) == 'table' and (decoded.file.name or decoded.file.path)
or type(decoded.file) == 'string' and decoded.file
or nil
if context_type == 'review-comment' then
if decoded.comments then
if type(decoded.comments) ~= 'table' or #decoded.comments == 0 then
return nil, 'invalid review-comment editor context'
end
for _, comment in ipairs(decoded.comments) do
if
type(comment) ~= 'table'
or type(comment.comment) ~= 'string'
or type(comment.code) ~= 'string'
or type(comment.lines) ~= 'string'
or (comment.side ~= 'before' and comment.side ~= 'after')
then
return nil, 'invalid review-comment editor context'
end
end
base.source = { kind = 'review_comment', file_name = file_name }
base.comments = decoded.comments
return base
end
local snapshot = type(decoded.snapshot) == 'table' and decoded.snapshot or decoded
if
type(decoded.comment) ~= 'string'
or type(snapshot.code) ~= 'string'
or type(snapshot.lines) ~= 'string'
or (snapshot.side ~= 'before' and snapshot.side ~= 'after')
then
return nil, 'invalid review-comment editor context'
end
base.source = { kind = 'review_comment', file_name = file_name, range = snapshot.lines, side = snapshot.side }
base.text = decoded.comment
base.code = snapshot.code
return base
end
if context_type == 'selection' then
if type(decoded.content) ~= 'string' or (decoded.lines ~= nil and type(decoded.lines) ~= 'string') then
return nil, 'invalid selection editor context'
end
base.source = { kind = 'selection', file_name = file_name, range = decoded.lines }
base.text = decoded.content
return base
end
if context_type == 'diagnostics' then
if type(decoded.content) ~= 'table' then
return nil, 'invalid diagnostics editor context'
end
local diagnostics = {}
for _, item in ipairs(decoded.content) do
if
type(item) ~= 'table'
or type(item.msg) ~= 'string'
or type(item.severity) ~= 'number'
or type(item.pos) ~= 'string'
then
return nil, 'invalid diagnostics editor context'
end
diagnostics[#diagnostics + 1] = { message = item.msg, severity = item.severity, position = item.pos }
end
base.source = { kind = 'diagnostics', file_name = file_name }
base.diagnostics = diagnostics
return base
end
-- cursor-data
if
type(decoded.line) ~= 'number'
or type(decoded.column) ~= 'number'
or type(decoded.line_content) ~= 'string'
or (decoded.lines_before ~= nil and type(decoded.lines_before) ~= 'table')
or (decoded.lines_after ~= nil and type(decoded.lines_after) ~= 'table')
then
return nil, 'invalid cursor editor context'
end
base.source = { kind = 'cursor', file_name = file_name }
base.line = decoded.line
base.column = decoded.column
base.line_content = decoded.line_content
base.lines_before = decoded.lines_before and vim.deepcopy(decoded.lines_before) or nil
base.lines_after = decoded.lines_after and vim.deepcopy(decoded.lines_after) or nil
return base
end
function M.unread_sync()
return { state = 'unread' }
end
function M.sync_error(source, err)
local message = type(err) == 'table' and (err.message or err.code) or nil
return {
state = 'error',
error = { kind = source, message = tostring(message or err or 'unknown error') },
}
end
---Empty state per resource. A new state seeds itself from these and releasing a
---resource restores them, so the two cannot drift apart. `session` is absent: its
---empty value is the observation's own session reference.
---@type table<OpencodeObservedResource, fun(state: table)>
local clear_state = {
children = function(state)
state.children = { by_id = {}, order = {} }
end,
messages = function(state)
state.entries_by_id, state.entry_order = {}, {}
end,
inbox = function(state)
state.inbox = { items_by_id = {}, order = {} }
end,
execution = function(state)
state.execution = { activity = 'unknown' }
end,
permissions = function(state)
state.permission_requests_by_id = {}
end,
questions = function(state)
state.question_requests_by_id = {}
end,
files = function(state)
state.files = { revision = 0 }
end,
}
---@param session table
---@param unsupported? table<string, string>
---@return OpencodeObservationState
function M.new_state(session, unsupported)
local sync = {}
for resource in pairs(resource_names) do
local reason = unsupported and unsupported[resource]
sync[resource] = reason and { state = 'unsupported', error = reason } or M.unread_sync()
end
local state = { session = session, sync = sync }
for _, clear in pairs(clear_state) do
clear(state)
end
---@cast state OpencodeObservationState
return state
end
---Borrow the current state. Consumers must not mutate it or use identity to detect changes.
---@return OpencodeObservationState
function Observation:read()
return self._state
end
---Submit one prompt to a fresh, exclusively owned session and await its response.
---@param input table Protocol-independent submission input
---@return OpencodeReplyRequest
function Observation:request_reply(input)
return require('opencode.protocols.reply').start(self, input)
end
function Observation:_is_current()
return self._connection:is_ready() and self._connection.observations[self._session_id] == self
end
function Observation:_watches(resource)
for watcher in pairs(self._watchers) do
if watcher.resources[resource] then
return true
end
end
return false
end
function Observation:_notify(resource)
local callbacks = {}
for watcher in pairs(self._watchers) do
if watcher.resources[resource] then
callbacks[#callbacks + 1] = watcher.changed
end
end
for _, changed in ipairs(callbacks) do
changed(self, resource)
end
end
---Publish committed event data before evaluating the protocol's refresh policy.
---@param resource OpencodeObservedResource
function Observation:_event_changed(resource)
self._event_revisions[resource] = self._event_revisions[resource] + 1
self:_notify(resource)
local sync = self._state.sync[resource]
---@cast sync OpencodeObservationSync
if self._runtime.refresh_after_event(resource, sync) then
self:_start_resource(resource)
end
end
local function stream_resource(observation, resource)
local select_resource = observation._runtime.stream_resource
return not select_resource or select_resource(resource)
end
local function has_stream_demand(connection)
if not connection:is_ready() then
return false
end
for _, observation in pairs(connection.observations) do
if observation._local_operations > 0 and observation._runtime.operations_need_stream ~= false then
return true
end
for watcher in pairs(observation._watchers) do
for resource in pairs(watcher.resources) do
if stream_resource(observation, resource) then
return true
end
end
end
end
return false
end
local function release_if_unused(observation)
if next(observation._watchers) or observation._local_operations > 0 then
return
end
if observation._connection.observations[observation._session_id] == observation then
observation._connection.observations[observation._session_id] = nil
end
if observation._runtime.on_unused then
observation._runtime.on_unused(observation)
end
end
local function close_stream(connection)
local retry = connection._observation_retry
connection._observation_retry = nil
if retry then
retry:stop()
retry:close()
end
local owner = connection._observation_stream
connection._observation_stream = nil
if not owner then
return
end
if connection._stream == owner.handle then
connection:set_stream(nil)
end
if owner.handle and owner.handle.shutdown then
owner.handle:shutdown()
end
end
function M.stop_stream_if_unused(connection)
if not has_stream_demand(connection) then
close_stream(connection)
end
end
function Observation:_begin_local_operation()
self._local_operations = self._local_operations + 1
local active = true
return function()
if not active then
return
end
active = false
self._local_operations = self._local_operations - 1
release_if_unused(self)
M.stop_stream_if_unused(self._connection)
end
end
---@param operation function
---@param ... any Operation arguments after the connection
---@return Promise<any>
function Observation:_start_action(operation, ...)
local finish = self:_begin_local_operation()
local ok, request = pcall(operation, self._connection, ...)
if not ok then
finish()
error(request, 0)
end
return request:finally(finish)
end
function Observation:_start_state_action(operation, apply, ...)
local finish = self:_begin_local_operation()
local ok, request = pcall(operation, self._connection, ...)
if not ok then
finish()
error(request, 0)
end
return request
:and_then(function(value)
if not self:_is_current() then
error('Observation action response arrived after release', 0)
end
return apply(value)
end)
:finally(finish)
end
function Observation:_fail_watched(source, message)
for resource, sync in pairs(self._state.sync) do
if self:_watches(resource) and sync.state ~= 'unsupported' then
self._loading[resource] = nil
self._state.sync[resource] = M.sync_error(source, message)
self:_notify(resource)
end
end
end
---@type fun(connection: table, runtime: OpencodeObservationRuntime)
local ensure_stream
---@type fun(connection: table)
local schedule_stream_recovery
local function stream_failure(connection, owner, reason)
if connection._observation_stream ~= owner then
return
end
close_stream(connection)
local message = type(reason) == 'table' and tostring(reason.message or reason.code or 'event stream disconnected')
or tostring(reason or 'event stream disconnected')
for _, observation in pairs(connection.observations) do
if observation._runtime.on_stream_error then
observation._runtime.on_stream_error(observation, message)
end
observation:_fail_watched('event_stream', message)
end
schedule_stream_recovery(connection)
end
local function decode_record(connection, owner)
if #owner.data == 0 then
return
end
local payload = table.concat(owner.data, '\n')
owner.data = {}
local ok, event = pcall(vim.json.decode, payload)
if not ok or type(event) ~= 'table' then
stream_failure(connection, owner, 'invalid ' .. owner.runtime.name .. ' event JSON')
return
end
owner.runtime.route_event(connection, event)
end
local function consume_stream_chunk(connection, owner, chunk)
if connection._observation_stream ~= owner or type(chunk) ~= 'string' then
return
end
owner.buffer = owner.buffer .. chunk
while true do
local newline = owner.buffer:find('\n', 1, true)
if not newline then
return
end
---@cast newline integer
local line = owner.buffer:sub(1, newline - 1):gsub('\r$', '')
owner.buffer = owner.buffer:sub(newline + 1)
if line == '' then
decode_record(connection, owner)
if connection._observation_stream ~= owner then
return
end
else
local data = line:match('^data:%s?(.*)$')
if data then
owner.data[#owner.data + 1] = data
end
end
end
end
ensure_stream = function(connection, runtime)
if connection._observation_stream then
return
end
local owner = { buffer = '', data = {}, runtime = runtime }
connection._observation_stream = owner
local ok, handle = pcall(connection.operations.subscribe_events, connection, function(chunk)
consume_stream_chunk(connection, owner, chunk)
end, function(reason)
stream_failure(connection, owner, reason)
end)
if not ok then
connection._observation_stream = nil
error(handle, 0)
end
owner.handle = handle
end
function M.ensure_stream(connection, observation)
ensure_stream(connection, observation._runtime)
end
local RECOVERY_DELAY_MS = 100
---Reopen the shared stream for a connection that still has demand.
---@return boolean reopened Whether watched resources should be reloaded
local function retry_stream(connection)
if not has_stream_demand(connection) or connection._observation_stream then
return false
end
local _, observation = next(connection.observations)
if not (observation and pcall(M.ensure_stream, connection, observation)) then
schedule_stream_recovery(connection)
return false
end
return true
end
local function reload_watched_resources(connection)
for _, observation in pairs(connection.observations) do
for resource in pairs(resource_names) do
if observation:_watches(resource) then
observation:_start_resource(resource)
end
end
end
end
schedule_stream_recovery = function(connection)
if not has_stream_demand(connection) or connection._observation_retry then
return
end
local timer = vim.uv.new_timer()
---@cast timer uv.uv_timer_t
connection._observation_retry = timer
timer:start(
RECOVERY_DELAY_MS,
0,
vim.schedule_wrap(function()
if connection._observation_retry ~= timer then
return
end
connection._observation_retry = nil
timer:stop()
timer:close()
if retry_stream(connection) then
reload_watched_resources(connection)
end
end)
)
end
function Observation:_start_resource(resource)
if not self:_is_current() then
return
end
local sync = self._state.sync[resource]
if sync.state == 'unsupported' or self._loading[resource] or not self:_watches(resource) then
return
end
if self._runtime.local_resource and self._runtime.local_resource(resource) then
self._state.sync[resource] = { state = 'current' }
self:_notify(resource)
return
end
-- Only the token still stored in `_loading` may commit its response. Release and
-- stream loss clear it, so holding it also proves a watcher still wants the snapshot.
local token = { revision = self._event_revisions[resource] }
self._loading[resource] = token
local function owns_request()
return self:_is_current() and self._loading[resource] == token
end
local function failed(err)
if owns_request() then
self._loading[resource] = nil
self._state.sync[resource] = M.sync_error('operation', err)
self:_notify(resource)
end
end
self._state.sync[resource] = { state = 'loading' }
self:_notify(resource)
if not owns_request() then
return
end
local ok, request = pcall(self._runtime.request_resource, self, resource)
if not ok then
failed(request)
return
end
request
:and_then(function(value)
if not owns_request() then
return
end
self._loading[resource] = nil
if self._event_revisions[resource] ~= token.revision then
self._state.sync[resource] = { state = 'stale' }
self:_notify(resource)
self:_start_resource(resource)
return
end
local applied, err = pcall(self._runtime.apply_resource, self, resource, value)
if not applied then
self._state.sync[resource] = M.sync_error('protocol_contract', err)
elseif self._state.sync[resource].state == 'loading' then
self._state.sync[resource] = { state = 'current' }
end
self:_notify(resource)
end)
:catch(failed)
end
function Observation:_release_resource(resource)
if self._state.sync[resource].state == 'unsupported' then
return
end
self._loading[resource] = nil
local state = self._state
if resource == 'session' then
state.session = vim.deepcopy(self._session_ref)
else
clear_state[resource](state)
end
if self._runtime.on_release_resource then
self._runtime.on_release_resource(self, resource)
end
state.sync[resource] = M.unread_sync()
end
---The last unsubscribe for a resource clears its state and invalidates its pending snapshot.
---@param resources string[]
---@param changed fun(observation: OpencodeObservation, resource: OpencodeObservedResource)
---@return fun() unsubscribe
function Observation:watch(resources, changed)
if type(resources) ~= 'table' or type(changed) ~= 'function' then
error('watch requires resources and a changed callback')
end
local selected, to_start = {}, {}
for _, resource in ipairs(resources) do
if not resource_names[resource] then
error('unsupported Observation resource: ' .. tostring(resource))
end
---@cast resource OpencodeObservedResource
if not selected[resource] and not self:_watches(resource) then
to_start[#to_start + 1] = resource
end
selected[resource] = true
end
local watcher = { resources = selected, changed = changed }
self._watchers[watcher] = true
local ok, err = pcall(function()
if has_stream_demand(self._connection) then
M.ensure_stream(self._connection, self)
end
for _, resource in ipairs(to_start) do
self:_start_resource(resource)
end
end)
if not ok then
self._watchers[watcher] = nil
release_if_unused(self)
error(err, 0)
end
local subscribed = true
return function()
if not subscribed then
return
end
subscribed = false
self._watchers[watcher] = nil
for resource in pairs(selected) do
if not self:_watches(resource) then
self:_release_resource(resource)
end
end
release_if_unused(self)
M.stop_stream_if_unused(self._connection)
end
end
---@param connection table
---@param session table
---@param state table
---@param runtime OpencodeObservationRuntime
---@return OpencodeObservation
function M.attach(connection, session, state, runtime)
local revisions = {}
for resource in pairs(resource_names) do
revisions[resource] = 0
end
return setmetatable({
_connection = connection,
_session_id = session.id,
_session_ref = vim.deepcopy(session),
_state = state,
_runtime = runtime,
_watchers = {},
_local_operations = 0,
_loading = {},
_event_revisions = revisions,
}, Observation)
end
function M.close(connection)
close_stream(connection)
for _, observation in pairs(connection.observations) do
if observation._runtime.on_close then
observation._runtime.on_close(observation)
end
end
connection.observations = {}
end
return M