Skip to content

Commit 9662aa9

Browse files
rustyconoverclaude
andcommitted
Add attach_id field to Invocation for database attachment tracing
Add optional attach_id field (bytes) to Invocation to support tracing VGI calls back to specific DuckDB database attachments. Changes: - Add attach_id: bytes | None field to Invocation dataclass - Update Invocation serialize/deserialize for attach_id - Add attach_id parameter to Client constructor - Add --attach-id CLI option (accepts hex string) The attach_id is optional and defaults to None for cases where VGI is used outside of an attached database context. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent fd6d53a commit 9662aa9

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎vgi/client/cli.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ def main() -> None:
192192
'table input at position 1, resulting in ("prefix", TABLE_INPUT).'
193193
),
194194
)
195+
@click.option(
196+
"--attach-id",
197+
"attach_id",
198+
type=str,
199+
default=None,
200+
help=(
201+
"Unique identifier for the DuckDB database attachment as a hex string. "
202+
"Used to trace calls back to a specific attachment."
203+
),
204+
)
195205
def cli(
196206
input_file: str | None,
197207
output_file: str | None,
@@ -203,6 +213,7 @@ def cli(
203213
projection_ids: tuple[int, ...],
204214
max_workers: int | None,
205215
table_input_position: int | None,
216+
attach_id: str | None,
206217
) -> None:
207218
"""Invoke a VGI function and display results."""
208219
try:
@@ -232,12 +243,25 @@ def cli(
232243
# Convert args_list to PyArrow scalars
233244
positional_args = tuple(pa.scalar(arg) for arg in args_list)
234245

246+
# Parse attach_id from hex string if provided
247+
attach_id_bytes: bytes | None = None
248+
if attach_id is not None:
249+
try:
250+
attach_id_bytes = bytes.fromhex(attach_id)
251+
except ValueError as e:
252+
raise click.ClickException(
253+
f"Invalid --attach-id: must be a valid hex string: {e}"
254+
) from e
255+
235256
log.info("starting_server", function=function_name, server_path=server_path)
236257

237258
output_writer: OutputWriter | None = None
238259
try:
239260
with Client(
240-
server_path, passthrough_stderr=worker_stderr, max_workers=max_workers
261+
server_path,
262+
passthrough_stderr=worker_stderr,
263+
max_workers=max_workers,
264+
attach_id=attach_id_bytes,
241265
) as client:
242266
if input_file is None:
243267
# Table function (no input) - use table_function method

‎vgi/client/client.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def _initialize_stream_common(
417417
correlation_id=self.correlation_id,
418418
invocation_id=None,
419419
client_features=client_features,
420+
attach_id=self._attach_id,
420421
)
421422
call_parameters_batch_bytes = initial_request.serialize()
422423

@@ -650,6 +651,7 @@ def __init__(
650651
correlation_id: str = "",
651652
passthrough_stderr: bool = False,
652653
max_workers: int | None = None,
654+
attach_id: bytes | None = None,
653655
):
654656
"""Initialize the VGI client.
655657
@@ -672,6 +674,9 @@ def __init__(
672674
overrides the function's max_processes when that value exceeds
673675
this limit. Also capped by os.cpu_count(). If None, uses the
674676
function's max_processes (still capped by CPU count).
677+
attach_id: Optional unique identifier for the DuckDB database
678+
attachment. When VGI is used from an attached database, this
679+
allows tracing calls back to that specific attachment.
675680
676681
Example:
677682
>>> client = Client("vgi-example-worker", max_workers=4)
@@ -684,6 +689,7 @@ def __init__(
684689
self.correlation_id = correlation_id
685690
self.passthrough_stderr = passthrough_stderr
686691
self._max_workers = max_workers
692+
self._attach_id = attach_id
687693
self._proc: subprocess.Popen[bytes] | None = None
688694
self._stdout_buffered: io.BufferedReader | None = None
689695
self._stdin_sink: pa.PythonFile | None = None

‎vgi/function.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ class Invocation:
194194
client_features: Feature flags supported by the client. The worker will
195195
respond with active_features in OutputSpec indicating which features
196196
will be used for this invocation.
197+
attach_id: Optional unique identifier for the DuckDB database attachment.
198+
When VGI is used from an attached database, this allows tracing calls
199+
back to that specific attachment. None when not using attached databases.
197200
198201
Example:
199202
invocation = Invocation(
@@ -216,6 +219,7 @@ class Invocation:
216219
global_init_identifier: GlobalInitResult | None = None
217220
arguments: Arguments = Arguments()
218221
client_features: frozenset[str] = frozenset()
222+
attach_id: bytes | None = None
219223

220224
def with_global_init_identifier(
221225
self, global_init_identifier: GlobalInitResult
@@ -257,6 +261,7 @@ def serialize(self) -> bytes:
257261
else None
258262
),
259263
"client_features": list(self.client_features),
264+
"attach_id": self.attach_id,
260265
}
261266
],
262267
schema=pa.schema(
@@ -274,6 +279,7 @@ def serialize(self) -> bytes:
274279
nullable=True,
275280
),
276281
pa.field("client_features", pa.list_(pa.utf8()), nullable=False),
282+
pa.field("attach_id", pa.binary(), nullable=True),
277283
]
278284
),
279285
)
@@ -326,6 +332,11 @@ def deserialize(data: pa.RecordBatch) -> "Invocation":
326332
if features_list is not None:
327333
client_features = frozenset(features_list)
328334

335+
# Parse attach_id - optional field for database attachment tracking
336+
attach_id: bytes | None = None
337+
if "attach_id" in data.schema.names:
338+
attach_id = first_row.get("attach_id")
339+
329340
return Invocation(
330341
function_name=first_row["function_name"],
331342
arguments=Arguments.decode(data.column("arguments")[0]),
@@ -334,6 +345,7 @@ def deserialize(data: pa.RecordBatch) -> "Invocation":
334345
correlation_id=first_row["correlation_id"],
335346
global_init_identifier=global_init_identifier,
336347
client_features=client_features,
348+
attach_id=attach_id,
337349
)
338350

339351
@staticmethod

0 commit comments

Comments
 (0)