langfun 0.1.2.dev202510310805__py3-none-any.whl → 0.1.2.dev202511020804__py3-none-any.whl

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 langfun might be problematic. Click here for more details.

@@ -41,6 +41,35 @@ class IdTest(unittest.TestCase):
41
41
  )
42
42
  self.assertIsNone(sandbox_id.working_dir(root_dir=None))
43
43
 
44
+ def test_feature_id(self):
45
+ # For non-sandboxed feature.
46
+ feature_id = interface.Feature.Id(
47
+ container_id=interface.Environment.Id('env'),
48
+ feature_name='feature'
49
+ )
50
+ self.assertEqual(str(feature_id), 'env/feature')
51
+ self.assertEqual(
52
+ feature_id.working_dir(root_dir='/tmp'),
53
+ '/tmp/env/feature'
54
+ )
55
+ self.assertIsNone(feature_id.working_dir(root_dir=None))
56
+
57
+ # For sandboxed feature.
58
+ feature_id = interface.Feature.Id(
59
+ container_id=interface.Sandbox.Id(
60
+ environment_id=interface.Environment.Id('env'),
61
+ image_id='image1',
62
+ sandbox_id='0'
63
+ ),
64
+ feature_name='feature'
65
+ )
66
+ self.assertEqual(str(feature_id), 'env/image1:0/feature')
67
+ self.assertEqual(
68
+ feature_id.working_dir(root_dir='/tmp'),
69
+ '/tmp/env/image1/0/feature'
70
+ )
71
+ self.assertIsNone(feature_id.working_dir(root_dir=None))
72
+
44
73
 
45
74
  class TestingSandbox(interface.Sandbox):
46
75
 
@@ -109,7 +138,7 @@ class DecoratorTest(unittest.TestCase):
109
138
 
110
139
  class SandboxB(TestingSandbox):
111
140
 
112
- @interface.log_sandbox_activity()
141
+ @interface.log_activity()
113
142
  def bar(self, x: str) -> None:
114
143
  pass
115
144
 
langfun/env/test_utils.py CHANGED
@@ -21,7 +21,6 @@ from langfun.env import base_environment
21
21
  from langfun.env import base_feature
22
22
  from langfun.env import base_sandbox
23
23
  from langfun.env import interface
24
- from langfun.env.event_handlers import base as event_handler_base
25
24
  import pyglove as pg
26
25
 
27
26
 
@@ -113,7 +112,7 @@ class TestingSandbox(base_sandbox.BaseSandbox):
113
112
  super()._shutdown()
114
113
 
115
114
  @interface.treat_as_sandbox_state_error(errors=(RuntimeError,))
116
- @interface.log_sandbox_activity()
115
+ @interface.log_activity()
117
116
  def shell(
118
117
  self,
119
118
  code: str,
@@ -185,19 +184,19 @@ class TestingFeature(base_feature.BaseFeature):
185
184
  if self.call_end_session_on_teardown_session:
186
185
  self.sandbox.end_session()
187
186
 
188
- @interface.log_sandbox_activity()
187
+ @interface.log_activity()
189
188
  def num_shell_calls(self) -> int:
190
189
  return len(self.sandbox._shell_history) # pylint: disable=protected-access
191
190
 
192
- @interface.log_sandbox_activity()
191
+ @interface.log_activity()
193
192
  def bad_shell_call(self) -> None:
194
193
  self.sandbox.shell('bad command', raise_error=RuntimeError)
195
194
 
196
- @interface.log_sandbox_activity()
195
+ @interface.log_activity()
197
196
  def show_session_id(self):
198
197
  return self.session_id
199
198
 
200
- @interface.log_sandbox_activity()
199
+ @interface.log_activity()
201
200
  def call_with_varargs(self, code: str, *args, **kwargs):
202
201
  del code, args, kwargs
203
202
  return 0
@@ -221,9 +220,44 @@ class TestingFeature(base_feature.BaseFeature):
221
220
  )
222
221
 
223
222
 
224
- class TestingEventHandler(
225
- pg.Object, event_handler_base.EventHandler
226
- ):
223
+ class TestingNonSandboxBasedFeature(base_feature.BaseFeature):
224
+ """Testing non-sandbox based feature for unit tests."""
225
+ is_sandbox_based: bool = False
226
+ simulate_setup_error: Type[BaseException] | None = None
227
+ simulate_teardown_error: Type[BaseException] | None = None
228
+ simulate_setup_session_error: Type[BaseException] | None = None
229
+ simulate_teardown_session_error: Type[BaseException] | None = None
230
+ simulate_housekeep_error: Type[BaseException] | None = None
231
+
232
+ def _setup(self) -> None:
233
+ if self.simulate_setup_error:
234
+ raise self.simulate_setup_error('Feature setup error')
235
+
236
+ def _teardown(self) -> None:
237
+ if self.simulate_teardown_error:
238
+ raise self.simulate_teardown_error('Feature teardown error')
239
+
240
+ def _setup_session(self) -> None:
241
+ if self.simulate_setup_session_error:
242
+ raise self.simulate_setup_session_error('Feature session setup error')
243
+
244
+ def _teardown_session(self) -> None:
245
+ if self.simulate_teardown_session_error:
246
+ raise self.simulate_teardown_session_error(
247
+ 'Feature session teardown error'
248
+ )
249
+
250
+ def _housekeep(self) -> None:
251
+ if self.simulate_housekeep_error:
252
+ raise self.simulate_housekeep_error('Feature housekeeping error')
253
+ _ = self.foo(1)
254
+
255
+ @interface.log_activity()
256
+ def foo(self, x: int) -> int:
257
+ return x + 1
258
+
259
+
260
+ class TestingEventHandler(pg.Object, interface.EventHandler):
227
261
  """Testing environment event handler for unit tests."""
228
262
 
229
263
  log_sandbox_status: bool = False
@@ -284,7 +318,6 @@ class TestingEventHandler(
284
318
 
285
319
  def on_sandbox_start(
286
320
  self,
287
- environment: interface.Environment,
288
321
  sandbox: interface.Sandbox,
289
322
  duration: float,
290
323
  error: BaseException | None
@@ -294,7 +327,6 @@ class TestingEventHandler(
294
327
 
295
328
  def on_sandbox_status_change(
296
329
  self,
297
- environment: interface.Environment,
298
330
  sandbox: interface.Sandbox,
299
331
  old_status: interface.Sandbox.Status,
300
332
  new_status: interface.Sandbox.Status,
@@ -309,7 +341,6 @@ class TestingEventHandler(
309
341
 
310
342
  def on_sandbox_shutdown(
311
343
  self,
312
- environment: interface.Environment,
313
344
  sandbox: interface.Sandbox,
314
345
  duration: float,
315
346
  lifetime: float,
@@ -318,9 +349,53 @@ class TestingEventHandler(
318
349
  assert duration > 0 and lifetime is not None
319
350
  self._add_message(f'[{sandbox.id}] sandbox shutdown', error)
320
351
 
352
+ def on_sandbox_session_start(
353
+ self,
354
+ sandbox: interface.Sandbox,
355
+ session_id: str,
356
+ duration: float,
357
+ error: BaseException | None
358
+ ) -> None:
359
+ """Called when a sandbox session starts."""
360
+ assert duration > 0
361
+ self._add_message(
362
+ f'[{sandbox.id}] session {session_id!r} started', error
363
+ )
364
+
365
+ def on_sandbox_session_end(
366
+ self,
367
+ sandbox: interface.Sandbox,
368
+ session_id: str,
369
+ duration: float,
370
+ lifetime: float,
371
+ error: BaseException | None
372
+ ) -> None:
373
+ """Called when a sandbox session ends."""
374
+ assert duration > 0 and lifetime > 0
375
+ self._add_message(
376
+ f'[{sandbox.id}] session {session_id!r} ended', error
377
+ )
378
+
379
+ def on_sandbox_activity(
380
+ self,
381
+ name: str,
382
+ sandbox: interface.Sandbox,
383
+ session_id: str | None,
384
+ duration: float,
385
+ error: BaseException | None,
386
+ *,
387
+ code: str | None = None,
388
+ **kwargs
389
+ ) -> None:
390
+ """Called when a sandbox activity is performed."""
391
+ del kwargs
392
+ log_id = f'{sandbox.id}@{session_id or "<idle>"}'
393
+ self._add_message(
394
+ f'[{log_id}] {name}: {code}', error
395
+ )
396
+
321
397
  def on_sandbox_housekeep(
322
398
  self,
323
- environment: interface.Environment,
324
399
  sandbox: interface.Sandbox,
325
400
  counter: int,
326
401
  duration: float,
@@ -335,8 +410,6 @@ class TestingEventHandler(
335
410
 
336
411
  def on_feature_setup(
337
412
  self,
338
- environment: interface.Environment,
339
- sandbox: interface.Sandbox,
340
413
  feature: interface.Feature,
341
414
  duration: float,
342
415
  error: BaseException | None
@@ -345,13 +418,11 @@ class TestingEventHandler(
345
418
  assert duration > 0
346
419
  if self.log_feature_setup:
347
420
  self._add_message(
348
- f'[{sandbox.id}/{feature.name}] feature setup', error
421
+ f'[{feature.id}] feature setup', error
349
422
  )
350
423
 
351
424
  def on_feature_teardown(
352
425
  self,
353
- environment: interface.Environment,
354
- sandbox: interface.Sandbox,
355
426
  feature: interface.Feature,
356
427
  duration: float,
357
428
  error: BaseException | None
@@ -360,13 +431,11 @@ class TestingEventHandler(
360
431
  assert duration > 0
361
432
  if self.log_feature_setup:
362
433
  self._add_message(
363
- f'[{sandbox.id}/{feature.name}] feature teardown', error
434
+ f'[{feature.id}] feature teardown', error
364
435
  )
365
436
 
366
437
  def on_feature_setup_session(
367
438
  self,
368
- environment: interface.Environment,
369
- sandbox: interface.Sandbox,
370
439
  feature: interface.Feature,
371
440
  session_id: str | None,
372
441
  duration: float,
@@ -376,13 +445,12 @@ class TestingEventHandler(
376
445
  assert duration > 0
377
446
  if self.log_session_setup:
378
447
  self._add_message(
379
- f'[{sandbox.id}/{feature.name}] feature setup session', error
448
+ f'[{feature.id}@{session_id or "<idle>"}] feature setup session',
449
+ error
380
450
  )
381
451
 
382
452
  def on_feature_teardown_session(
383
453
  self,
384
- environment: interface.Environment,
385
- sandbox: interface.Sandbox,
386
454
  feature: interface.Feature,
387
455
  session_id: str,
388
456
  duration: float,
@@ -392,77 +460,38 @@ class TestingEventHandler(
392
460
  assert duration > 0
393
461
  if self.log_session_setup:
394
462
  self._add_message(
395
- f'[{sandbox.id}/{feature.name}] feature teardown session', error
463
+ f'[{feature.id}@{session_id}] feature teardown session', error
396
464
  )
397
465
 
398
- def on_feature_housekeep(
466
+ def on_feature_activity(
399
467
  self,
400
- environment: interface.Environment,
401
- sandbox: interface.Sandbox,
468
+ name: str,
402
469
  feature: interface.Feature,
403
- counter: int,
470
+ session_id: str | None,
404
471
  duration: float,
405
472
  error: BaseException | None,
473
+ *,
474
+ code: str | None = None,
406
475
  **kwargs
407
476
  ) -> None:
408
- """Called when a sandbox feature is housekeeping."""
409
- assert duration > 0
410
- if self.log_housekeep:
411
- self._add_message(
412
- f'[{sandbox.id}/{feature.name}] feature housekeeping {counter}', error
413
- )
414
-
415
- def on_session_start(
416
- self,
417
- environment: interface.Environment,
418
- sandbox: interface.Sandbox,
419
- session_id: str,
420
- duration: float,
421
- error: BaseException | None
422
- ) -> None:
423
- """Called when a sandbox session starts."""
424
- assert duration > 0
425
- self._add_message(
426
- f'[{sandbox.id}] session {session_id!r} started', error
427
- )
428
-
429
- def on_session_end(
430
- self,
431
- environment: interface.Environment,
432
- sandbox: interface.Sandbox,
433
- session_id: str,
434
- duration: float,
435
- lifetime: float,
436
- error: BaseException | None
437
- ) -> None:
438
- """Called when a sandbox session ends."""
439
- assert duration > 0 and lifetime > 0
477
+ """Called when a sandbox activity is performed."""
478
+ del kwargs
479
+ log_id = f'{feature.id}@{session_id or "<idle>"}'
440
480
  self._add_message(
441
- f'[{sandbox.id}] session {session_id!r} ended', error
481
+ f'[{log_id}] {name}: {code}', error
442
482
  )
443
483
 
444
- def on_sandbox_activity(
484
+ def on_feature_housekeep(
445
485
  self,
446
- name: str,
447
- environment: interface.Environment,
448
- sandbox: interface.Sandbox,
449
- feature: interface.Feature | None,
450
- session_id: str | None,
486
+ feature: interface.Feature,
487
+ counter: int,
451
488
  duration: float,
452
489
  error: BaseException | None,
453
- *,
454
- code: str | None = None,
455
490
  **kwargs
456
491
  ) -> None:
457
- """Called when a sandbox activity is performed."""
458
- del environment, kwargs
459
- if session_id is None:
460
- log_id = sandbox.id
461
- else:
462
- log_id = f'{sandbox.id}/{session_id}'
463
-
464
- if feature is not None:
465
- log_id = f'{log_id}/{feature.name}'
466
- self._add_message(
467
- f'[{log_id}] {name}: {code}', error
468
- )
492
+ """Called when a sandbox feature is housekeeping."""
493
+ assert duration > 0
494
+ if self.log_housekeep:
495
+ self._add_message(
496
+ f'[{feature.id}] feature housekeeping {counter}', error
497
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langfun
3
- Version: 0.1.2.dev202510310805
3
+ Version: 0.1.2.dev202511020804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -89,7 +89,7 @@ langfun/core/eval/v2/progress_tracking_test.py,sha256=omOvSTvF-KgzwmXiOUj8sr5Dha
89
89
  langfun/core/eval/v2/reporting.py,sha256=yUIPCAMnp7InIzpv1DDWrcLO-75iiOUTpscj7smkfrA,8335
90
90
  langfun/core/eval/v2/reporting_test.py,sha256=CMK-vwho8cNRJwlbkCqm_v5fykE7Y3V6SaIOCY0CDyA,5671
91
91
  langfun/core/eval/v2/runners.py,sha256=bEniZDNu44AQgvqpwLsvBU4V_7WltAe-NPhYgIsLj1E,16848
92
- langfun/core/eval/v2/runners_test.py,sha256=spjkmqlls_vyERdZMdjv6dhIN9ZfxsDDvIQAWTj2kMk,11954
92
+ langfun/core/eval/v2/runners_test.py,sha256=Fw_OGaJZbMSwxBMRZ_PnCu4vPIlIfmzUYA9U2hq3FwQ,11996
93
93
  langfun/core/llms/__init__.py,sha256=UJ1UE41JPgkS56uNzJQ5ag9VWJN3OJuNna7aFR4CyOM,10184
94
94
  langfun/core/llms/anthropic.py,sha256=O_OuBiFhHou1Y15W2GwYFY1gV3FUsSUBOsAJp9UORqI,30710
95
95
  langfun/core/llms/anthropic_test.py,sha256=qA9vByp_cwwXNlXzcwHpPWFnO9lfFo8NKfDi5nBNqgI,9052
@@ -174,24 +174,27 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
174
174
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
175
175
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
176
176
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
177
- langfun/env/__init__.py,sha256=VTjLmS_SkxtkBCmr6hBb4iACeLLPmcbWJkK7MEXsATA,1652
178
- langfun/env/base_environment.py,sha256=8Cpwb4D37zHj5AK4qgmGIPyxIMEDxTIPHXqxTSNkE_M,25030
179
- langfun/env/base_feature.py,sha256=JDEhL9LkbBHB0c603guchry7cy_zaIReg5vqExyQQgg,6902
180
- langfun/env/base_sandbox.py,sha256=0dduNozBtPqawvSX-pv6sEweDNTi89lrT_5qKk5FWak,30656
181
- langfun/env/base_test.py,sha256=6yHtExP3Soa-l0ug73ZSV9ZfS0B4vu-uaS_K_xvsDwk,74261
182
- langfun/env/interface.py,sha256=2X-gGD41cnejde5DIQ7QxgW3r85oKP4Gp0iWP-ABmLM,32126
183
- langfun/env/interface_test.py,sha256=d8vdXL1PkrNQGwzfEI2r6esd6SBnTMgc-3GNArsnuj4,3295
177
+ langfun/env/__init__.py,sha256=Iwl5JQk6bGuScJcjx8NqKUCaZw1r8oRiuqFadu8x_Ss,1680
178
+ langfun/env/base_environment.py,sha256=CEDi4BXxfsGbkRZam2ywP0scpb-rbRb6bdMTFhNitB4,27013
179
+ langfun/env/base_environment_test.py,sha256=JltE2uPrruCpj0V50T3NGG3cf_8WDss67dEMk-REPRA,15457
180
+ langfun/env/base_feature.py,sha256=ohotob1Cnjd9M1BzHUS2ezGZgifcvXs6Ce0bOTGTkf8,8473
181
+ langfun/env/base_feature_test.py,sha256=n1IrU6tHRwHjkyD7yHJtfAQ8yrSSAteVdIqfTK49JI8,7094
182
+ langfun/env/base_sandbox.py,sha256=hTomEzky4Jfunt2Kse7uaboBYCCGAUf_xTY9kcPZDcY,27942
183
+ langfun/env/base_sandbox_test.py,sha256=j1WugtZBjvI9zo2_1tvuBhORMFENp8TanQwGfXwEUKE,60308
184
+ langfun/env/interface.py,sha256=tZGJ1RLmcsctz62pyBdravibUQL439Lkc4O40l7FtzY,51760
185
+ langfun/env/interface_test.py,sha256=AOosmhIIi4HVQ_WFmWha05oyj3-md4eJ4ODMTR6oJVw,4212
184
186
  langfun/env/load_balancers.py,sha256=qRhCthqzjZIQBwta8qC1C0s0J-VQAVomJQqI7Nqv-r4,1948
185
187
  langfun/env/load_balancers_test.py,sha256=Bg0h-AL7LpWb_aixFXs_FpgQp4dWLvodcsQj-mys6zs,4125
186
- langfun/env/test_utils.py,sha256=9fqhxxKERiyAte9bbtwqXqZ1ihNGyuOdCiPPY8G0I8w,14171
187
- langfun/env/event_handlers/__init__.py,sha256=H34n-TbKSgtxqBhE-yAti8fY6weF2_v3yw59M9_zmGM,443
188
- langfun/env/event_handlers/base.py,sha256=eGdJ6N5em9kX-c9wzm1TdnRP5_5IAltX5JTHILdjzLM,10124
189
- langfun/env/event_handlers/event_logger.py,sha256=3dbPjBe53dBgntYHlyLlj_77hVecPSXkmKeiGXMxlO0,12699
190
- langfun/env/event_handlers/event_logger_test.py,sha256=ryupxaEP9D8wdtSsSwZRSZwqFaHCaSD-bFSea_T7QJo,9133
191
- langfun/env/event_handlers/metric_writer.py,sha256=F_Gk1lpJX5SZ6-Hyrf_-utf4gvSKvMmcov8VkKogZCU,19618
192
- langfun/env/event_handlers/metric_writer_test.py,sha256=sntUifTPHGixUshIgVBX4Q9vJL-xbeS0Cpd5X5hSQyQ,5955
193
- langfun-0.1.2.dev202510310805.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
194
- langfun-0.1.2.dev202510310805.dist-info/METADATA,sha256=IKnFNK0k4M96hXuSz3sVo6QOFf9z36tKYsRaeJNvKqg,7522
195
- langfun-0.1.2.dev202510310805.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
196
- langfun-0.1.2.dev202510310805.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
197
- langfun-0.1.2.dev202510310805.dist-info/RECORD,,
188
+ langfun/env/test_utils.py,sha256=07Nszmqo3Xf5O5d_O2Qp7OcTB_wJ7QG53t6TqMgAchc,14946
189
+ langfun/env/event_handlers/__init__.py,sha256=EE3pV0BlhvYfTG3njhVTMqj88IO_gwjLqZDGSGL95DE,449
190
+ langfun/env/event_handlers/chain.py,sha256=MrAF7BTVJxQyYEFrBdyGCb0b3RotBVInJm0CHySYXNo,6946
191
+ langfun/env/event_handlers/chain_test.py,sha256=92xBFgL6NitbH8HSKZWQzK9jOm_SWyZwVywFyrxG6fc,8129
192
+ langfun/env/event_handlers/event_logger.py,sha256=ga8RN8qjwtAOCnV_MnhNPTktN8EJ-x1qw4Z_MsnvI5A,12554
193
+ langfun/env/event_handlers/event_logger_test.py,sha256=qSAcirtRz00H-1RL9ShELBiZKiPxsk_v6cVA6XdAk4k,9274
194
+ langfun/env/event_handlers/metric_writer.py,sha256=7ZrUp0rYvs7TfNpQ16Xbxg8vp-6ZbjuJ-qrhVSbhv2I,21085
195
+ langfun/env/event_handlers/metric_writer_test.py,sha256=bjdYXoXMPWpWz_-HUPM6vFP1ez5G386u0fmPfe-SR_M,5952
196
+ langfun-0.1.2.dev202511020804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
197
+ langfun-0.1.2.dev202511020804.dist-info/METADATA,sha256=DmFT3Ile2Lwy8PZz8qE2wNuLz1FigIS7EO6xxcYiB3c,7522
198
+ langfun-0.1.2.dev202511020804.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
199
+ langfun-0.1.2.dev202511020804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
200
+ langfun-0.1.2.dev202511020804.dist-info/RECORD,,